Skip to content

sed: write appended text, R text and e output as GNU sed does - #589

Merged
sylvestre merged 1 commit into
uutils:mainfrom
DePasqualeOrg:fix-r-separator-and-q-appends
Oct 5, 2026
Merged

sylvestre merged 1 commit into
uutils:mainfrom
DePasqualeOrg:fix-r-separator-and-q-appends

Conversation

@DePasqualeOrg

@DePasqualeOrg DePasqualeOrg commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

When the last line of input has no final newline and the cycle ends with text queued by a, r or R, GNU sed first writes the missing newline and then the queued text, even when an r file is missing or empty. uutils sed joins them instead: printf a | sed 'r f' prints a immediately followed by the contents of f. This PR writes the missing newline before any text queued for the end of the cycle.

GNU sed copies the line that R reads, and the output of the e command form, exactly as they are. uutils sed treats them as lines and adds a newline when they lack one. With a file f that contains x and no newline, printf 'a\nb\n' | sed 'R f' prints x and b on separate lines, while GNU sed prints xb. Likewise, printf 'a\n' | sed '1e printf hi' prints hi on a line of its own, while GNU sed prints hia. This PR adds OutputBuffer::write_raw, which writes a newline that the previous output line lacks and then the bytes unchanged, and uses it for this text.

@codecov

codecov Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.81%. Comparing base (3676eac) to head (76bbc1d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #589      +/-   ##
==========================================
- Coverage   94.82%   94.81%   -0.02%     
==========================================
  Files          14       14              
  Lines        7212     7217       +5     
  Branches      428      428              
==========================================
+ Hits         6839     6843       +4     
- Misses        372      373       +1     
  Partials        1        1              
Flag Coverage Δ
macos_latest 96.35% <100.00%> (-0.02%) ⬇️
ubuntu_latest 96.29% <100.00%> (-0.02%) ⬇️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 35.79%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 10 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ access_log_append 1.5 s 1.1 s +35.79%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing DePasqualeOrg:fix-r-separator-and-q-appends (b9941aa) with main (479c4be)

Open in CodSpeed

@DePasqualeOrg
DePasqualeOrg force-pushed the fix-r-separator-and-q-appends branch from 3ab6bde to ae5f8dc Compare October 2, 2026 18:17
@DePasqualeOrg DePasqualeOrg changed the title sed: end a line that lacks its newline before appended text, and discard appended text on Q sed: write appended text, R text and e output as GNU sed does Oct 2, 2026
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown

GNU sed testsuite comparison:

Test results comparison:
  Current:   TOTAL: 65 / PASSED: 18 / FAILED: 39 / SKIPPED: 8
  Reference: TOTAL: 65 / PASSED: 17 / FAILED: 40 / SKIPPED: 8

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + distrib

@DePasqualeOrg
DePasqualeOrg force-pushed the fix-r-separator-and-q-appends branch from ae5f8dc to ed72266 Compare October 3, 2026 17:28
@DePasqualeOrg
DePasqualeOrg marked this pull request as ready for review October 3, 2026 18:29
Comment thread src/sed/processor.rs Outdated

/// Output any data queued for output at the end of the cycle.
fn flush_appends(output: &mut OutputBuffer, context: &mut ProcessingContext) -> UResult<()> {
if !context.append_elements.is_empty() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could copy_file flush the pending newline itself, before the open?
then this block isn't needed, write_raw already does it for text

Comment thread src/sed/processor.rs Outdated
context.stop_processing = true;
context.quiet = true;
// Like GNU sed, discard text queued by `a`, `r` and `R`.
context.append_elements.clear();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Q and l fixes are unrelated to the r/R/e output change
please split them into separate PRs, thanks

Comment thread src/sed/fast_io.rs Outdated
pub fn copy_file(&mut self, path: &PathBuf) -> io::Result<()> {
// As in GNU sed, write the deferred newline even when the file is
// empty or cannot be read.
self.flush_pending_newline()?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could copy_file just call self.write_raw(b"")? here?
same flush_pending_newline + flush_mmap as write_raw

@DePasqualeOrg
DePasqualeOrg force-pushed the fix-r-separator-and-q-appends branch from 698bcb7 to 76bbc1d Compare October 4, 2026 14:02
@DePasqualeOrg
DePasqualeOrg force-pushed the fix-r-separator-and-q-appends branch from 76bbc1d to b9941aa Compare October 5, 2026 08:20
@sylvestre
sylvestre merged commit 8a7fc1b into uutils:main Oct 5, 2026
29 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

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.

2 participants