Repository navigation
grep: retain BRE edge anchor behavior when configuring syntax flags - #261
yunusarda-sudo wants to merge 1 commit into
Conversation
…ixes uutils#157) In basic regular expressions (BRE, default), '$' and '^' are anchors only at the edges of the pattern or of a subexpression; in the middle they are ordinary literal characters. Oniguruma's OnigSyntaxGrep enables ONIG_SYN_BRE_ANCHOR_AT_EDGE_OF_SUBEXP by default, but onig's Syntax::enable_behavior uses from_bits_truncate, silently stripping unmapped bits. Retain the existing behavior bits and clear ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC so that reversed ranges such as [b-a] continue to be rejected with exit status 2. Add regression tests verifying literal '$' and '^' in the middle of BRE patterns while preserving anchoring at edges and groups.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #261 +/- ##
==========================================
+ Coverage 97.22% 97.24% +0.01%
==========================================
Files 6 6
Lines 2270 2283 +13
Branches 245 245
==========================================
+ Hits 2207 2220 +13
Misses 63 63
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
Fixes #157.
Motivation and Root Cause
In a basic regular expression (BRE, the default),
$is an anchor only at the end of the pattern (or of a\(...\)group /\|alternative), and^only at the start. Anywhere else in the pattern they are ordinary literal characters, sogrep 'a$b'matches the literal texta$bandgrep 'x^2'matchesx^2.Oniguruma's
OnigSyntaxGrepdefines the correct behavior through theONIG_SYN_BRE_ANCHOR_AT_EDGE_OF_SUBEXPbit (1 << 14). However, theonigcrate'sSyntax::enable_behavior()retrieves the active flags usingSyntaxBehavior::from_bits_truncate(), which silently drops all bits that lack named constants in the wrapper—including bit 14. Whensyntax.enable_behavior(SYNTAX_BEHAVIOR_ALLOW_INTERVAL_LOW_ABBREV)was called, bit 14 was inadvertently stripped, treating$and^as anchors anywhere in the expression.Changes
enable_syntax_behaviorinsrc/matcher.rsthat preserves raw behavior bits viafrom_bits_retain.ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC(1 << 22) so that GNU grep's rejection of reversed bracket ranges like[b-a]with exit 2 ("Invalid range end") continues to hold.bre_anchors_are_literal_away_from_the_edgestotests/test_grep.rs, verifying literal matching of middle anchors while ensuring edge and subexpression group anchors continue to anchor properly.Verification
cargo test --test test_grep: all 229 tests pass.cargo clippy --all-targets -- -D warnings: 0 warnings.cargo fmt --all -- --check: clean.Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com