Skip to content

sed: accept first~step as the start of an address range - #619

Merged
sylvestre merged 2 commits into
uutils:mainfrom
jayhemnani9910:step-address-range-start
Nov 27, 2026
Merged

sylvestre merged 2 commits into
uutils:mainfrom
jayhemnani9910:step-address-range-start

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

GNU sed lets first~step start a range: the range starts on every line the step selects and ends at the second address as usual. uutils sed rejects the ,:

$ seq 12 | sed -n '1~3,+1p'
sed: <script argument 1>:1:4: error: invalid command code `,'

GNU sed 4.9 prints 1 2 4 5 7 8 10 11. Same for 1~3,5p, 0~4,~3p, 2~3,/7/p.

The step address was stored as addr1 = first, addr2 = StepMatch(step), so a second address had nowhere to go. When a , follows first~step, the compiler now turns addr1 into a new Address::Step(first, step) and reads the second address as for any range; match_address matches it on first, first+step, ... (only first for a step of 0). 0~0,N is still rejected, as GNU does.

Tests: test_step_match_starts_range with six scripts whose expected output is GNU sed 4.9's (fails on main), and test_step_match_zero_zero_range_start. cargo test: 356 passed (354 on main), clippy and fmt clean.

Copilot AI balanced review requested due to automatic review settings October 26, 2026 03:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.17%. Comparing base (c225cb8) to head (d865e40).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #619      +/-   ##
==========================================
+ Coverage   94.15%   94.17%   +0.02%     
==========================================
  Files          14       14              
  Lines        5745     5768      +23     
  Branches      463      470       +7     
==========================================
+ Hits         5409     5432      +23     
  Misses        335      335              
  Partials        1        1              
Flag Coverage Δ
macos_latest 96.14% <100.00%> (+0.01%) ⬆️
ubuntu_latest 96.02% <100.00%> (+0.01%) ⬆️
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.

Comment thread src/sed/compiler.rs Outdated
&& let Some(Address::Line(first)) = cmd.addr1
&& (first, step_n) != (0, 0)
{
cmd.addr1 = Some(Address::Step(first, step_n));

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.

for a step of 0, could we keep Address::Line(first)?
GNU treats 1~0 as a plain line number, so e.g. 1d;1~0,3p should start the range on line 2 like 1d;1,3p does.
it would also remove the step == 0 case in match_address.

Comment thread src/sed/compiler.rs Outdated

line.eat_spaces();
if n_addr == 1 && !line.eol() && matches!(line.current(), ',' | '~') {
while n_addr == 1 && !line.eol() && matches!(line.current(), ',' | '~') {

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.

turning the if into a while just to loop once more is a bit hard to follow
could the second address be read explicitly after the step instead?

Comment thread src/sed/command.rs Outdated
RelLine(usize), // Relative line
Last, // Last line
StepMatch(usize), // Lines matching specified step from first
Step(usize, usize), // first~step starting a range: first, first+step, ...

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.

Step next to StepMatch and StepEnd is confusing, maybe StepStart?

Comment thread tests/by-util/test_sed.rs
("1~3,+1p", "1\n2\n4\n5\n7\n8\n10\n11\n"),
("1~3,5p", "1\n2\n3\n4\n5\n7\n10\n"),
("0~4,~3p", "4\n5\n6\n8\n9\n12\n"),
("2~3,/7/p", "2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n"),

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 add 0~3,/x/ too, it shouldn't behave like 0,/x/
and a case with a skipped start like 1d;1~0,3p

@jayhemnani9910

Copy link
Copy Markdown
Contributor Author

All four done in 3b6e2c7:

  • compiler.rs:412: first~0,addr2 now stays Address::Line(first), so 1d;1~0,3p starts the range on line 2 like 1d;1,3p (GNU: 2 3; before this it printed nothing). The step == 0 case is gone from match_address.
  • compiler.rs:360: back to an if. Reading ,addr2 / ~N is now compile_second_address, called once more after first~step,, with no loop.
  • command.rs:114: renamed to StepStart.
  • tests: added 1d;1~0,3p and 0~3,/x/p on x 2 3 4 x 6 7. That prints 3 4 x 6 7 as in GNU sed 4.9, where 0,/x/p stops at line 1. 0~3,/x/ was already right before this commit, so that case guards it; 1d;1~0,3p fails without the change.

cargo test, cargo fmt --check and cargo clippy --all-targets -- -D warnings are clean.

Copilot AI balanced review requested due to automatic review settings November 11, 2026 07:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codspeed

codspeed Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 6.56%

2 warnings

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 1 improved benchmark
✅ 10 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ access_log_subst 2.1 s 2 s +6.56%

Tip

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


Comparing jayhemnani9910:step-address-range-start (d865e40) with main (c225cb8)

Open in CodSpeed

GNU sed starts a range on every line first~step selects, so
'1~3,+1p' prints lines 1, 2, 4, 5, ... Until now the ',' after the
step was rejected as an unknown command.
Move the parsing of `,addr2` / `~N` into compile_second_address and
call it a second time after `first~step,` instead of looping. Keep
`first~0` as a plain line number so `1d;1~0,3p` starts the range on
line 2 like `1d;1,3p`, and rename Address::Step to StepStart.
@jayhemnani9910
jayhemnani9910 force-pushed the step-address-range-start branch from 3b6e2c7 to d865e40 Compare November 27, 2026 07:25
Copilot AI balanced review requested due to automatic review settings November 27, 2026 07:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre
sylvestre merged commit d0ff3a4 into uutils:main Nov 27, 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.

3 participants