Skip to content

Implement GNU R (read one line from file) command - #544

Merged
sylvestre merged 1 commit into
uutils:mainfrom
Jorge-Polanco-Roque:feat/R-read-one-line
Sep 25, 2026
Merged

sylvestre merged 1 commit into
uutils:mainfrom
Jorge-Polanco-Roque:feat/R-read-one-line

Conversation

@Jorge-Polanco-Roque

Copy link
Copy Markdown
Contributor

Closes #394.

Implements the GNU R command, the line-oriented counterpart to r (mirroring how W relates to w, added in #531).

R filename queues the next successive line of filename to be inserted into the output stream at the end of the current cycle. Each cycle consumes one more line; once the file is exhausted no line is queued.

Behavior

$ printf 'a\nb\nc\nd\n' | sed -e 'R lines.txt'   # lines.txt = x1,x2,x3
a
x1
b
x2
c
x3
d
  • The file is opened lazily on first use and read one line at a time (new NamedReader, analogous to NamedWriter).
  • An unreadable or exhausted file yields no further lines — no error is raised, matching GNU sed.
  • Reserved to non-POSIX mode and rejected under --sandbox, exactly like r/w/W.

Tests

  • Unit tests for NamedReader (successive lines, final line without newline, missing file).
  • Integration tests: successive-line behavior, silent missing file, --posix rejection, --sandbox rejection.

cargo fmt --all -- --check, cargo test --all, and cargo clippy --all-targets --workspace -psed -- -D warnings all pass locally.

@codecov

codecov Bot commented Aug 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.41284% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.88%. Comparing base (c46dd6d) to head (48c1a03).

Files with missing lines Patch % Lines
src/sed/compiler.rs 89.18% 4 Missing ⚠️
src/sed/processor.rs 96.77% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #544      +/-   ##
==========================================
+ Coverage   82.84%   83.88%   +1.04%     
==========================================
  Files          13       14       +1     
  Lines        6865     6974     +109     
  Branches      401      405       +4     
==========================================
+ Hits         5687     5850     +163     
+ Misses       1175     1121      -54     
  Partials        3        3              
Flag Coverage Δ
macos_latest 85.03% <95.41%> (+1.06%) ⬆️
ubuntu_latest 85.23% <95.41%> (+1.03%) ⬆️
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 Aug 30, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 3.17%

⚠️ 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_translit 1 s 1 s +3.17%

Tip

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


Comparing Jorge-Polanco-Roque:feat/R-read-one-line (48c1a03) with main (c46dd6d)

Open in CodSpeed

@sylvestre

Copy link
Copy Markdown
Contributor

please add tests to improve the coverage. some tests are missing to reach important cases

@Jorge-Polanco-Roque

Copy link
Copy Markdown
Contributor Author

Thanks — added tests to cover the missing cases: unit tests for compile_read_line_command (sandbox rejection and that it installs a NamedReader), plus integration tests for R appending under -n (suppressed auto-print) and for a source file shorter than the input. The R compile and execute paths are now exercised directly.

@Jorge-Polanco-Roque

Copy link
Copy Markdown
Contributor Author

Added integration tests for R covering the cases that were missing: address-gated R that only advances when it runs, independent cursors across different files, and a final line without a trailing newline. While writing them I also caught and fixed a real divergence from GNU: multiple R on the same file now share one cursor instead of each restarting from line 1. Full suite and clippy are green.

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

GNU sed testsuite comparison:

Test results comparison:
  Current:   TOTAL: 65 / PASSED: 15 / FAILED: 42 / SKIPPED: 8
  Reference: TOTAL: 65 / PASSED: 14 / FAILED: 43 / SKIPPED: 8

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

Test improvements (1):
  + cmd-R

1 similar comment
@github-actions

Copy link
Copy Markdown

GNU sed testsuite comparison:

Test results comparison:
  Current:   TOTAL: 65 / PASSED: 15 / FAILED: 42 / SKIPPED: 8
  Reference: TOTAL: 65 / PASSED: 14 / FAILED: 43 / SKIPPED: 8

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

Test improvements (1):
  + cmd-R

@Jorge-Polanco-Roque

Copy link
Copy Markdown
Contributor Author

Gentle ping — this has been open ~10 days. I added the integration tests you asked for (the missing R cases: multiple files, address ranges, and end-of-input handling), and the GNU sed testsuite comparison is clean. Happy to rebase or adjust if anything else is needed. Thanks!

Comment thread src/sed/compiler.rs
Ok(CommandHandling::Continue)
}

// Handles R

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.

does it make a test of the GNU testsuite pass? if so, please mention it in the commit message

Comment thread src/sed/compiler.rs Outdated
return compilation_error(lines, line, ERR_SANDBOX);
}
let path = read_file_path(lines, line)?;
// GNU sed shares a single cursor per file path among all `R` commands

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 why is already documented on named_readers in command.rs, please keep only one :)

Comment thread src/sed/compiler.rs
n_addr: 2,
handler: compile_empty_command,
}),
'R' if !posix => Ok(CommandSpec {

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.

please also add R to the list of GNU extensions in README.md (next to W)

Comment thread src/sed/named_reader.rs
@@ -0,0 +1,101 @@
// An abstraction for input files read one line at a time by the `R` command

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.

please squash the 5 commits into one, with the usual sed: prefix

The R command reads one line at a time from a named file. All R commands
naming the same file share a single cursor so they read successive lines,
matching GNU sed. Rejected under --sandbox like the other file commands.

Includes unit and integration tests. Makes the GNU sed testsuite `cmd-R`
test pass.
@Jorge-Polanco-Roque

Copy link
Copy Markdown
Contributor Author

Thanks @sylvestre! Addressed all four:

  • Squashed the 5 commits into one with the sed: prefix.
  • It does make the GNU testsuite cmd-R test pass — mentioned that in the commit message now.
  • Dropped the duplicated comment; the "why" now lives only on named_readers in command.rs.
  • Added R to the GNU extensions list in the README, next to W.

Rebased on main while at it; fmt/tests green.

@github-actions

Copy link
Copy Markdown

GNU sed testsuite comparison:

Test results comparison:
  Current:   TOTAL: 65 / PASSED: 15 / FAILED: 42 / SKIPPED: 8
  Reference: TOTAL: 65 / PASSED: 14 / FAILED: 43 / SKIPPED: 8

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

Test improvements (1):
  + cmd-R

@sylvestre
sylvestre merged commit e5474ab into uutils:main Sep 25, 2026
31 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.

Add R command (read one line from file)

2 participants