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 (074abcf) with main (280669a)

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

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.39%. Comparing base (280669a) to head (074abcf).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #258      +/-   ##
==========================================
+ Coverage   97.32%   97.39%   +0.06%     
==========================================
  Files           6        6              
  Lines        2316     2378      +62     
  Branches      250      265      +15     
==========================================
+ Hits         2254     2316      +62     
  Misses         62       62              
Flag Coverage Δ
macOS_latest 98.26% <100.00%> (+0.04%) ⬆️
ubuntu_latest 98.26% <100.00%> (+0.04%) ⬆️
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

@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

Cygwin failure is unrelated: color_on_a_terminal_needs_option_and_term_not_dumb is flaky there (same failures on main, https://github.com/uutils/grep/actions/runs/45587825191).

@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

Comment thread src/matcher.rs Outdated
continue;
}

out.push(b as char);

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.

b as char breaks non-UTF-8-safe here: a non-ASCII byte becomes a Latin-1 char, so printf 'café\0' | grep -z 'café$' no longer matches.
same on 734 and 747. please copy byte slices (or work on Vec<u8>) and add a test with a non-ASCII pattern

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.

Fixed: the rewrite copies bytes now; added café$ cases for BRE/ERE/-P.

Comment thread src/matcher.rs Outdated
continue;
}

if b == b'[' {

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.

[[:alpha:]$] ends the bracket at the first ], so this $ becomes \z.
could you please reuse scan_bracket instead of tracking it by hand? with a test

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.

Fixed: brackets go through scan_bracket, with [[:alpha:]$] tests.

Comment thread src/matcher.rs
syntax.disable_behavior(SyntaxBehavior::SYNTAX_BEHAVIOR_NOT_NEWLINE_IN_NEGATIVE_CC);
}
if config.regex_mode != RegexMode::Fixed {
syntax.enable_operators(SyntaxOperator::SYNTAX_OPERATOR_ESC_AZ_BUF_ANCHOR);

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 also makes a user's \A, \Z and \z anchors in BRE/ERE, but only with -z. is it wanted?
maybe only \z should be produced by the rewrite, without exposing the operator

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.

Only the rewrite produces \z now: a user's \A, \Z and \z stay plain letters outside -P, as in GNU. Tests added.

Comment thread src/matcher.rs Outdated

while i < bytes.len() {
let b = bytes[i];
if in_bracket {

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.

for -P, [\]$] keeps going after the escaped ] and ends at the wrong place. please add a test

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.

Fixed: -P brackets honour backslash escapes; [\]$] is tested.

Comment thread src/matcher.rs Outdated
.map(|i| i + 2)
}

/// In `-z / --null-data` mode, a record ends at NUL (`\0`) and newline is ordinary data.

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.

does it really need a 6 lines comment? please make it shorter

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.

Shortened to two lines.

Comment thread tests/test_grep.rs Outdated
.succeeds()
.stdout_only("1\n");

// Escaped and bracketed dollar remains literal

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.

almost the same block 10 times, could be a table like the two loops above, no?

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.

Done: one table now.

Comment thread tests/test_grep.rs
"a.txt:foo a\ngrep: bin.dat: binary file matches\ngrep: missing.txt: No such file or directory\nb.txt:foo b\n"
);
}
#[test]

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.

missing empty line before #[test]

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.

Fixed.

@yunusarda-sudo
yunusarda-sudo force-pushed the grep-null-data-record-anchors branch from 0f57eb5 to 75e5c35 Compare December 4, 2026 04:54
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 79 / FAILED: 28 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21

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

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

1 similar comment
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 79 / FAILED: 28 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21

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

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

Comment thread src/matcher.rs
let mut i = 0;
while i < bytes.len() {
match bytes[i] {
b'\\' => {

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.

with -P, \Qa$\E becomes \Qa\z\E and then matches a literal a\z instead of a$.
could you please leave the content of \Q...\E untouched? with a test

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.

Done: \Q...\E is copied untouched under -P, with tests (\Qa$\E, \Qa$b, \Qa\E$).

Comment thread tests/test_grep.rs
#[test]
fn null_data_anchors_are_record_anchors() {
// (options, pattern, record, whether the record matches)
let cases: &[(&[&str], &str, &str, bool)] = &[

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.

two and one aren't in alpha\nbeta\n, so these 4 cases pass even without the fix.
please use one\ntwo as in the issue

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.

Done: those cases use one\ntwo now and fail on main.

…#195)

Under -z a record ends at NUL and newline is ordinary data, so `^` and `$` anchor to the whole record. Rewrite anchor `$` to `\z` since Oniguruma's `$` also matches before a final newline.
@yunusarda-sudo
yunusarda-sudo force-pushed the grep-null-data-record-anchors branch from b5917c7 to 074abcf Compare December 5, 2026 04:53
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 80 / FAILED: 27 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 78 / FAILED: 29 / 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