Skip to content

sed: accept Ng substitute flags and point carets at the culprit - #608

Closed
sylvestre wants to merge 1 commit into
uutils:mainfrom
sylvestre:subst-flag-diagnostics
Closed

sylvestre wants to merge 1 commit into
uutils:mainfrom
sylvestre:subst-flag-diagnostics

Conversation

@sylvestre

Copy link
Copy Markdown
Contributor
  • s/x/y/Ng and s/x/y/gN are now accepted, like GNU: replace the Nth match and every one after. They were rejected with "multiple 'g' or numeric flags in substitute command" (and unit tests asserted that).
  • Repeated flags now use GNU's messages, multiple g' options to s' command and multiple number options to s' command. The latter points at the second number (3ins/a/b/2g3, char 9 like GNU) instead of at the g`.
  • An invalid back-reference (s/\(a\)/\2/) was reported after the flags were parsed, so the UUTILS_DIAG caret sat past the end of the command. It now points at the \2, including when the replacement spans several lines.
$ echo aaaa | sed 's/a/b/2g'
abbb
$ UUTILS_DIAG=always sed 's/\(a\)/\2/'
sed: <script argument 1>:1:9: error: invalid reference \2 on `s' command's RHS
   ╭─[ <script argument 1>:1:9 ]
   │
 1 │ s/\(a\)/\2/
   │         ─
───╯

`s/x/y/2g` (replace the 2nd match and every later one) is valid in GNU
sed, but was rejected with "multiple 'g' or numeric flags". Track `g`
separately from the occurrence number, and report repeated flags with
GNU's two messages: "multiple `g' options to `s' command" and "multiple
number options to `s' command". The latter now points at the second
number, e.g. the `3` in `2g3`, as GNU does.

An invalid back-reference such as `s/\(a\)/\2/` was reported after the
flags were read, so the diagnostic caret landed past the end of the
command. Record where the highest `\N` appears and report it there.
Copilot AI balanced review requested due to automatic review settings October 19, 2026 11:53

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.

🟢 Approval recommended

The focused implementation and regression coverage support the intended behavior, with no unresolved findings.

No open issues 🎉

What changed in this PR

Improves sed’s GNU compatibility for substitution flags and makes error locations more precise.

Changes:

  • Accepts Ng and gN to replace the Nth match and all later matches.
  • Uses GNU-style duplicate-flag messages and points diagnostics at offending flags or back-references.
  • Adds regression tests for substitution behavior and diagnostic locations.
File Description
tests/​by-util/​test_sed.rs Tests combined flags and diagnostic carets.
src/​sed/​processor.rs Applies global replacement from the selected occurrence.
src/​sed/​compiler.rs Parses combined flags and records precise error locations.
src/​sed/​command.rs Stores global mode and back-reference locations.

Review effort: Balanced

@codspeed

codspeed Bot commented Oct 19, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 7.68%

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

⚡ 2 improved benchmarks
✅ 9 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ number_fix 1.3 s 1.2 s +12.08%
⚡ access_log_subst 2.2 s 2.1 s +3.46%

Tip

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


Comparing sylvestre:subst-flag-diagnostics (294b1a1) with main (b1ea0dc)

Open in CodSpeed

@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.61905% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 94.84%. Comparing base (b1ea0dc) to head (294b1a1).

Files with missing lines Patch % Lines
src/sed/compiler.rs 97.36% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #608      +/-   ##
==========================================
- Coverage   94.85%   94.84%   -0.01%     
==========================================
  Files          14       14              
  Lines        7230     7241      +11     
  Branches      432      432              
==========================================
+ Hits         6858     6868      +10     
- Misses        371      372       +1     
  Partials        1        1              
Flag Coverage Δ
macos_latest 96.38% <97.61%> (-0.01%) ⬇️
ubuntu_latest 96.32% <97.61%> (-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.

@sylvestre

Copy link
Copy Markdown
Contributor Author

Split into #609, #610 and #611.

@sylvestre sylvestre closed this Oct 19, 2026
@sylvestre
sylvestre deleted the subst-flag-diagnostics branch October 19, 2026 15:52
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