Skip to content

grep: make ^ and $ record anchors under -z - #197

Open
ilainy wants to merge 3 commits into
uutils:mainfrom
ilainy:main
Open

ilainy wants to merge 3 commits into
uutils:mainfrom
ilainy:main

Conversation

@ilainy

@ilainy ilainy commented Nov 16, 2026

Copy link
Copy Markdown

Fixes #195.

With -z, records end at NUL, so newline is just data and ^/$ should only
anchor at record edges. The matcher only set REGEX_OPTION_MULTILINE, which in Oniguruma just makes
. match newline. The anchors stayed line-based, and under -P $ still
matched before a trailing newline.

Now BRE/ERE also set REGEX_OPTION_SINGLELINE under -z and rewrite $ to
\', and Perl rewrites $ to \z. Added a regression test and a unit test
for the Perl rewrite. cargo test passes.

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.90722% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.10%. Comparing base (9598faf) to head (3d1616e).
⚠️ Report is 112 commits behind head on main.

Files with missing lines Patch % Lines
src/matcher.rs 96.90% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #197      +/-   ##
==========================================
+ Coverage   95.28%   97.10%   +1.81%     
==========================================
  Files           6        6              
  Lines        1422     2207     +785     
  Branches      140      244     +104     
==========================================
+ Hits         1355     2143     +788     
+ Misses         66       64       -2     
+ Partials        1        0       -1     
Flag Coverage Δ
macOS_latest 97.67% <96.90%> (+1.57%) ⬆️
ubuntu_latest 97.67% <96.90%> (+1.57%) ⬆️
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.

@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 75 / FAILED: 32 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: +2
  FAILED: -2

Test improvements (2):
  + pcre
  + z-anchor-newline

@codspeed

codspeed Bot commented Nov 17, 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 ilainy:main (3d1616e) with main (de374a3)

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
normalize_null_data_posix_dollar_anchors(pattern, config.regex_mode)
}
RegexMode::Perl => normalize_null_data_perl_dollar_anchors(pattern),
RegexMode::Fixed => unreachable!(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could be a single match config.regex_mode with if config.null_data guards returning Option, no?
then there's no need for the matches! + unreachable!()

Comment thread src/matcher.rs Outdated
false
}

fn find_bracket_expression_end(pattern: &[u8], start: usize) -> Option<usize> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is almost the same scan as scan_bracket below, could you please reuse it?

Comment thread src/matcher.rs Outdated
&& matches!(bytes.get(i + 2), Some(b')' | b'|')))
}
RegexMode::Extended => true,
_ => false,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

a basic: bool would be simpler than passing the mode, then the debug_assert and this _ => false can go

Comment thread tests/test_grep.rs
}

#[test]
fn null_data_anchors_are_record_anchors() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please add BRE alpha$\|x and \(alpha$\), -E '^beta', and a positive beta$ on alpha\nbeta\0 for each mode

@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 75 / FAILED: 32 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: +2
  FAILED: -2

Test improvements (2):
  + pcre
  + z-anchor-newline

Comment thread tests/test_grep.rs Outdated
}

#[test]
fn null_data_anchors_cover_bre_ere_and_record_end() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could be merged into the table of null_data_anchors_are_record_anchors (one list of failing, one of matching args), no?
six almost identical blocks here

rstest is probably good to use here

@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 75 / FAILED: 32 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: +2
  FAILED: -2

Test improvements (2):
  + pcre
  + z-anchor-newline

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 - with -z, ^ and $ also match next to a newline inside a record

2 participants