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)

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