Skip to content

fix(sed): suppress null-match refire after a non-empty match - #1292

Merged
caomengxuan666 merged 1 commit into
mainfrom
fix/1284-sed-empty-match
Oct 25, 2026
Merged

caomengxuan666 merged 1 commit into
mainfrom
fix/1284-sed-empty-match

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1284

Summary

Ports GNU sed/execute.c do_subst() semantics into substitute_line: an empty match is only substituted when it is the first match or does not immediately follow the previous match's end (offset > last_end). A skipped vacuous match neither substitutes nor advances the Nth-occurrence counter, and consumes one input character before scanning resumes.

Before (WinuxCmd 1.1.20)

printf '2 g x\n'  | sed 's/[0-9]*/N/g'   -> NN NgN NxN
printf 'aab\n'    | sed 's/a*/x/g'       -> xxbx
printf 'baa\n'    | sed 's/a*/x/g'       -> xbxx
printf 'baaaac\n' | sed 's/a*/x/g'       -> xbxxcx
printf 'aab\n'    | sed 's/a*/x/2'       -> aaxb
printf 'baa\n'    | sed 's/a*/x/2g'      -> bxx
printf 'ab\n'     | sed 's/[ab]*/_/g'    -> __
printf 'aa bb\n'  | sed 's/ */>/g'       -> >a>a>>b>b>
printf 'x\n'      | sed 's/x*/-/g'       -> --

After (this PR, matches WSL GNU sed 4.9 / bash 5.3.20 oracle)

N NgN NxN
xbx
xbx
xbxcx
aabx
bx
_
>a>a>b>b>
-

Test plan

  • 12-probe differential vs WSL GNU sed — all now identical
  • 4 new regression tests in tests/unit/sed/sed_unit_test.cpp (incl. s/a*/x/2 count semantics — the skipped null match does not count)
  • winuxcmd-tests --run-test sed: 125/125 pass
  • scripts/format.py clean

Generated with Devin

Mirror GNU sed/execute.c do_subst(): an empty match is substituted only
when it is the first match or does not immediately follow the previous
match's end; a skipped vacuous match neither substitutes nor advances
the Nth-occurrence counter, and consumes one input character.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 merged commit 501d316 into main Oct 25, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/1284-sed-empty-match branch October 30, 2026 01:42
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.

[bug] sed: s/X*/repl/g emits an extra replacement for the empty match at position 0 after a non-empty match

1 participant