Skip to content

grep: align PCRE syntax behavior and digit options with GNU grep - #255

Open
yunusarda-sudo wants to merge 2 commits into
uutils:mainfrom
yunusarda-sudo:grep-pcre-syntax-flags
Open

yunusarda-sudo wants to merge 2 commits into
uutils:mainfrom
yunusarda-sudo:grep-pcre-syntax-flags

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

Under -P, configure Oniguruma syntax behavior and options to align with GNU grep:

  1. Retain ONIG_SYN_ISOLATED_OPTION_CONTINUE_BRANCH so inline (?i) carries over across branches (previously stripped by enable_behavior()).
  2. Enable ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND so look-behind alternatives of differing lengths (e.g. (?<=Host: |host=)) are accepted.
  3. Clear ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP so numbered backreferences (\1) work alongside named capture groups (e.g. (?<w>\w+) \1).
  4. Set ONIG_OPTION_DIGIT_IS_ASCII so \d matches ASCII [0-9] only, rather than non-ASCII Unicode digits.

Fixes #182

Under -P, configure Oniguruma syntax behavior and options to match GNU grep:
- Retain ONIG_SYN_ISOLATED_OPTION_CONTINUE_BRANCH so inline (?i) carries
  over across branches.
- Enable ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND so look-behind alternatives
  of differing lengths (e.g. (?<=Host: |host=)) are accepted.
- Clear ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP so numbered backreferences (\1)
  work alongside named capture groups.
- Set ONIG_OPTION_DIGIT_IS_ASCII so \d matches ASCII [0-9] only.

Fixes uutils#182
@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.23%. Comparing base (bf6f25e) to head (0e4e008).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #255      +/-   ##
==========================================
+ Coverage   97.22%   97.23%   +0.01%     
==========================================
  Files           6        6              
  Lines        2270     2279       +9     
  Branches      245      246       +1     
==========================================
+ Hits         2207     2216       +9     
  Misses         63       63              
Flag Coverage Δ
macOS_latest 98.13% <100.00%> (+<0.01%) ⬆️
ubuntu_latest 98.13% <100.00%> (+<0.01%) ⬆️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 3 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing yunusarda-sudo:grep-pcre-syntax-flags (0e4e008) with main (bf6f25e)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Comment thread src/matcher.rs Outdated
Comment on lines +364 to +369
let raw_syntax = &raw mut syntax as *mut onig_sys::OnigSyntaxType;
let mut behavior = unsafe { onig_sys::onig_get_syntax_behavior(raw_syntax) };
behavior |= onig_sys::ONIG_SYN_ISOLATED_OPTION_CONTINUE_BRANCH;
behavior |= onig_sys::ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND;
behavior &= !onig_sys::ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP;
unsafe { onig_sys::onig_set_syntax_behavior(raw_syntax, behavior) };

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these not exposed via onig?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! I originally checked Syntax::enable_behavior() and Syntax::disable_behavior(), but hit an issue with onig's wrapper:

  1. onig::SyntaxBehavior only defines flags up to 0x4000 (so ONIG_SYN_ISOLATED_OPTION_CONTINUE_BRANCH is omitted in the bitflags definition).
  2. More crucially, Syntax::enable_behavior() and Syntax::disable_behavior() internally call self.behavior(), which uses SyntaxBehavior::from_bits_truncate(). Calling enable_behavior() on Syntax::perl() silently drops any bits not explicitly bound in onig::SyntaxBehavior—truncating the Perl behavior mask from 0x88a0040b down to 0xa0004b and breaking (?i).
  3. I've updated the write-back in commit 0e4e008 to use syntax.set_behavior(SyntaxBehavior::from_bits_retain(behavior)) instead of the FFI setter, minimizing the unsafe surface while safely preserving the full bitmask.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grep - -P rejects or misreads some everyday PCRE patterns because of Oniguruma syntax bits

2 participants