Repository navigation
grep: treat anchors as whole-record delimiters under -z - #258
yunusarda-sudo wants to merge 1 commit into
Conversation
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
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:
|
|
GNU grep testsuite comparison: |
|
Cygwin failure is unrelated: |
|
GNU grep testsuite comparison: |
| continue; | ||
| } | ||
|
|
||
| out.push(b as char); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Fixed: the rewrite copies bytes now; added café$ cases for BRE/ERE/-P.
| continue; | ||
| } | ||
|
|
||
| if b == b'[' { |
There was a problem hiding this comment.
[[: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
There was a problem hiding this comment.
Fixed: brackets go through scan_bracket, with [[:alpha:]$] tests.
| 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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Only the rewrite produces \z now: a user's \A, \Z and \z stay plain letters outside -P, as in GNU. Tests added.
|
|
||
| while i < bytes.len() { | ||
| let b = bytes[i]; | ||
| if in_bracket { |
There was a problem hiding this comment.
for -P, [\]$] keeps going after the escaped ] and ends at the wrong place. please add a test
There was a problem hiding this comment.
Fixed: -P brackets honour backslash escapes; [\]$] is tested.
| .map(|i| i + 2) | ||
| } | ||
|
|
||
| /// In `-z / --null-data` mode, a record ends at NUL (`\0`) and newline is ordinary data. |
There was a problem hiding this comment.
does it really need a 6 lines comment? please make it shorter
There was a problem hiding this comment.
Shortened to two lines.
| .succeeds() | ||
| .stdout_only("1\n"); | ||
|
|
||
| // Escaped and bracketed dollar remains literal |
There was a problem hiding this comment.
almost the same block 10 times, could be a table like the two loops above, no?
There was a problem hiding this comment.
Done: one table now.
| "a.txt:foo a\ngrep: bin.dat: binary file matches\ngrep: missing.txt: No such file or directory\nb.txt:foo b\n" | ||
| ); | ||
| } | ||
| #[test] |
There was a problem hiding this comment.
missing empty line before #[test]
0f57eb5 to
75e5c35
Compare
|
GNU grep testsuite comparison: |
1 similar comment
|
GNU grep testsuite comparison: |
| let mut i = 0; | ||
| while i < bytes.len() { | ||
| match bytes[i] { | ||
| b'\\' => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Done: \Q...\E is copied untouched under -P, with tests (\Qa$\E, \Qa$b, \Qa\E$).
| #[test] | ||
| fn null_data_anchors_are_record_anchors() { | ||
| // (options, pattern, record, whether the record matches) | ||
| let cases: &[(&[&str], &str, &str, bool)] = &[ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
b5917c7 to
074abcf
Compare
|
GNU grep testsuite comparison: |
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:
RegexOptions::REGEX_OPTION_SINGLELINEcompiles^toANCR_BEGIN_BUF(strictly record start), but compiles$toANCR_SEMI_END_BUF.ANCR_SEMI_END_BUFmatches before a trailing newline\nat the end of the buffer (min_semi_end = pre_endifONIGENC_IS_MBC_NEWLINE). Consequently,beta$matchedalpha\nbeta\nunder-z.$anchors strictly to the end of the record buffer (\z/ANCR_END_BUF), not before a trailing\n.Changes
SyntaxOperator::SYNTAX_OPERATOR_ESC_AZ_BUF_ANCHORwhenconfig.null_datais active across regex modes.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\|).tests/test_grep.rscovering BRE, ERE, and PCRE anchor semantics on null-terminated records with and without trailing newlines, as well as preserving literal\$and[$].