Skip to content

Conversation

@rynewang
Copy link
Contributor

@rynewang rynewang commented Jan 3, 2026

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 Bug

# Expected (GNU):
printf 'abc' | cut -d $'\n' -f1 -s    # (no output)

# Actual (before fix):
printf 'abc' | cut -d $'\n' -f1 -s    # abc\n

Changes

  • Adds only_delimited parameter to cut_fields_newline_char_delim()
  • Checks if input has no delimiter and only_delimited is true, returns early without output
  • Adds test case for newline delimiter with -s flag

Fixes #10012

@github-actions
Copy link

github-actions bot commented Jan 3, 2026

GNU testsuite comparison:

GNU test failed: tests/cut/cut. tests/cut/cut is passing on 'main'. Maybe you have to rebase?

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
@rynewang rynewang force-pushed the fix-cut-newline-delimiter-only-delimited branch from b9649a6 to 3e15738 Compare January 3, 2026 22:20
- 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
@rynewang rynewang force-pushed the fix-cut-newline-delimiter-only-delimited branch from 9a58d7b to 526b5d8 Compare January 4, 2026 19:33
@github-actions
Copy link

github-actions bot commented Jan 4, 2026

GNU testsuite comparison:

Skip an intermittent issue tests/timeout/timeout (fails in this run but passes in the 'main' branch)

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
@github-actions
Copy link

github-actions bot commented Jan 6, 2026

GNU testsuite comparison:

Skipping an intermittent issue tests/timeout/timeout (passes in this run but fails in the 'main' branch)

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 6, 2026

Merging this PR will improve performance by 4.09%

Summary

⚡ 1 improved benchmark
✅ 138 untouched benchmarks
⏩ 37 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
tsort_input_parsing_heavy[5000] 73.1 ms 70.2 ms +4.09%

Comparing rynewang:fix-cut-newline-delimiter-only-delimited (43986aa) with main (130f780)

Open in CodSpeed

Footnotes

  1. 37 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.

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.

cut: incorrect delimiter handling when delimiter is newline

2 participants