Skip to content

grep: retain BRE edge anchor behavior when configuring syntax flags - #261

Open
yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:grep-bre-anchors-at-edges
Open

yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:grep-bre-anchors-at-edges

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

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, so grep 'a$b' matches the literal text a$b and grep 'x^2' matches x^2.

Oniguruma's OnigSyntaxGrep defines the correct behavior through the ONIG_SYN_BRE_ANCHOR_AT_EDGE_OF_SUBEXP bit (1 << 14). However, the onig crate's Syntax::enable_behavior() retrieves the active flags using SyntaxBehavior::from_bits_truncate(), which silently drops all bits that lack named constants in the wrapper—including bit 14. When syntax.enable_behavior(SYNTAX_BEHAVIOR_ALLOW_INTERVAL_LOW_ABBREV) was called, bit 14 was inadvertently stripped, treating $ and ^ as anchors anywhere in the expression.

Changes

  1. Added helper enable_syntax_behavior in src/matcher.rs that preserves raw behavior bits via from_bits_retain.
  2. Explicitly cleared 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.
  3. Added regression test bre_anchors_are_literal_away_from_the_edges to tests/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

…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

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.24%. Comparing base (bf6f25e) to head (69708ed).

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              
Flag Coverage Δ
macOS_latest 98.14% <100.00%> (+0.01%) ⬆️
ubuntu_latest 98.14% <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 26, 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-bre-anchors-at-edges (69708ed) 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. ↩

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 - basic regexp treats $ and ^ in the middle of a pattern as anchors

1 participant