-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
cut: fix -s flag ignored when delimiter is newline #10037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
cut: fix -s flag ignored when delimiter is newline #10037
Conversation
|
GNU testsuite comparison: |
When using newline as the delimiter (-d $'\n') with -s (only-delimited), cut should suppress lines that do not contain the delimiter. However, cut_fields_newline_char_delim() was not checking the only_delimited flag, causing it to always output even when -s was specified. The fix uses read_until() instead of split() to read segments. Unlike split(), read_until() includes the delimiter in the buffer when found, allowing us to detect whether a delimiter was actually present or if we just hit EOF. This commit: - Adds only_delimited parameter to cut_fields_newline_char_delim() - Uses read_until() to detect delimiter presence while reading - If no delimiter found and only_delimited is true, returns early - Adds test case for newline delimiter with -s flag Fixes uutils#10012
b9649a6 to
3e15738
Compare
- Use read_to_end + split instead of read_until loop - Use Vec<&[u8]> instead of Vec<Vec<u8>> (no extra allocations) - Rename found_delimiter to has_delimiter - Check has_delimiter before removing trailing empty segment - Only write trailing newline if we output something - Remove unused BufRead import - Add tests for edge cases: just newline, empty input
9a58d7b to
526b5d8
Compare
|
GNU testsuite comparison: |
Address PR review comments: - Replace read_to_end() with streaming DelimReader iterator - DelimReader uses read_until() and tracks delimiter per segment - Only collect selected fields, not entire input - Remove b"".as_slice() check (DelimReader doesn't create trailing empties) - Simplify output with split_first() join pattern
|
GNU testsuite comparison: |
Merging this PR will improve performance by 4.09%Summary
Performance Changes
Comparing Footnotes
|
When using newline as the delimiter (
-d $'\n') with-s(only-delimited), cut should suppress lines that do not contain the delimiter. However,cut_fields_newline_char_delim()was not checking theonly_delimitedflag, causing it to always output even when-swas specified.The Bug
Changes
only_delimitedparameter tocut_fields_newline_char_delim()only_delimitedis true, returns early without output-sflagFixes #10012