Skip to content

grep: treat anchors as whole-record delimiters under -z - #258

Open
yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:grep-null-data-record-anchors
Open

yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:grep-null-data-record-anchors

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

Fixes #195

Summary

With -z / --null-data, records are delimited by ASCII NUL (\0) and newline is ordinary data inside the record. In GNU grep, ^ and $ anchor strictly to the record boundaries rather than matching next to an embedded newline or before a trailing newline.

In Oniguruma:

  1. RegexOptions::REGEX_OPTION_SINGLELINE compiles ^ to ANCR_BEGIN_BUF (strictly record start), but compiles $ to ANCR_SEMI_END_BUF.
  2. ANCR_SEMI_END_BUF matches before a trailing newline \n at the end of the buffer (min_semi_end = pre_end if ONIGENC_IS_MBC_NEWLINE). Consequently, beta$ matched alpha\nbeta\n under -z.
  3. In GNU grep, $ anchors strictly to the end of the record buffer (\z / ANCR_END_BUF), not before a trailing \n.

Changes

  • Enabled SyntaxOperator::SYNTAX_OPERATOR_ESC_AZ_BUF_ANCHOR when config.null_data is active across regex modes.
  • Added normalize_null_data_dollar(pattern, mode) which normalizes unescaped $ outside bracket expressions to \z (in BRE, only when at the end of the pattern or before subexpression end \) or \|).
  • Added integration tests in tests/test_grep.rs covering BRE, ERE, and PCRE anchor semantics on null-terminated records with and without trailing newlines, as well as preserving literal \$ and [$].

@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-null-data-record-anchors (d8903dd) 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. ↩

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.93151% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.83%. Comparing base (bf6f25e) to head (d8903dd).

Files with missing lines Patch % Lines
src/matcher.rs 84.93% 10 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #258      +/-   ##
==========================================
- Coverage   97.22%   96.83%   -0.39%     
==========================================
  Files           6        6              
  Lines        2270     2340      +70     
  Branches      245      260      +15     
==========================================
+ Hits         2207     2266      +59     
- Misses         63       73      +10     
- Partials        0        1       +1     
Flag Coverage Δ
macOS_latest 97.71% <84.93%> (-0.42%) ⬇️
ubuntu_latest 97.71% <84.93%> (-0.42%) ⬇️
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: 78 / FAILED: 29 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 76 / FAILED: 31 / 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

1 participant