Repository navigation
grep: withhold only the invalid UTF-8 line, keep printing later matches - #200
yunusarda-sudo wants to merge 5 commits into
Conversation
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
|
GNU grep testsuite comparison: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #200 +/- ##
==========================================
+ Coverage 97.19% 97.21% +0.01%
==========================================
Files 6 6
Lines 2106 2152 +46
Branches 230 241 +11
==========================================
+ Hits 2047 2092 +45
- Misses 59 60 +1
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:
|
| onig = { version = "~6.5.1", default-features = false } | ||
| onig_sys = { version = "69.9.3", default-features = false } | ||
| uucore = "0.12.0" | ||
| uucore = { version = "0.12.0", features = ["i18n-common"] } |
There was a problem hiding this comment.
this pulls icu_locale and ~20 new crates just to read LC_CTYPE
what is the impact on the binary size?
There was a problem hiding this comment.
Measured with cargo build --release: main is 1,889,712 bytes, with i18n-common it is 1,929,208 (+39.5 KB, about 2%), and with a local LC_ALL/LC_CTYPE/LANG lookup it is 1,893,992 (+4.3 KB). Since only the encoding is needed, I went back to the small local lookup (locale_is_utf8) and dropped the feature, so Cargo.toml and Cargo.lock are unchanged now.
|
|
||
| // An invalid-UTF-8 matching line is withheld, but the search | ||
| // goes on and the notice is emitted once at the end. | ||
| if encoding_error_line { |
There was a problem hiding this comment.
almost the same bookkeeping as at the end of the loop, could be dedup, no?
only the write_line needs to be skipped
There was a problem hiding this comment.
Done: the withheld case now only skips the write_line (and its line number), and the count/matched/offset bookkeeping is shared.
| self.session_last_printed_line = ctx.line_number; | ||
| } | ||
|
|
||
| if withhold { |
There was a problem hiding this comment.
please add a test with -A/-B around a withheld line
There was a problem hiding this comment.
Added invalid_utf8_line_withheld_with_context (-A1, -B1, -n -C1): the withheld line keeps its before-context but is not printed and starts no after-context. The output matches GNU grep in C.UTF-8.
Withholding only the invalid line and printing the later matches is what happens in a UTF-8 locale. In a unibyte locale such as C the line makes the file binary and the output stops there, as before. The locale is read from LC_ALL, LC_CTYPE and LANG.
Use uucore's get_ctype_encoding instead of a local LC_ALL/LC_CTYPE/LANG lookup.
…eeping, test context
cded745 to
996eae2
Compare
| // In the C locale an encoding error makes the file binary: output stops at | ||
| // that line and the notice follows. Covers both search paths. | ||
| let (scene, _) = ucmd(); | ||
| scene.fixtures.write_bytes("u", b"a\n\x80\nb\n"); |
There was a problem hiding this comment.
this is the fixture of the GNU unibyte-binary test
please use your own input
There was a problem hiding this comment.
Updated in commit 169ad13: replaced with a custom multi-line text fixture.
| if let Some(positions) = self.session_match_line(line) { | ||
| // TODO: GNU grep respects LANG. Here, I'm always checking for valid UTF-8. | ||
| if self.config.binary_mode != BinaryMode::WithoutMatch | ||
| if !self.withhold_encoding_errors |
There was a problem hiding this comment.
the utf-8 check is now in two places (here and session_withholds_line), with opposite locale conditions
could be merged, no?
There was a problem hiding this comment.
Done in commit 169ad13: merged both UTF-8 validation branches into a single session_check_encoding method.
A selected line that is not valid UTF-8 was handled like a NUL byte: output stopped there and every later match was lost. In a UTF-8 locale GNU grep only leaves that line out, keeps printing the other matches, and writes
binary file matchesonce at the end.Both the line-at-a-time path and the literal fast path now skip just that line (it still counts as a match, so
-mand the exit status are unchanged) and report the notice when the file is done. NUL handling,-aand-Iare unchanged.This only applies when the locale is UTF-8 (judged from
LC_ALL,LC_CTYPE,LANG). In a unibyte locale such asC, an encoding error still marks the file binary and stops the output at that line, which keeps the GNUunibyte-binarytest passing.Tests cover both paths in a UTF-8 locale (including
-m2) and the unibyte case.Closes #172