Fix duplicated output from empty match after trailing newline (--across) - #352
Open
yhuikzdtguioaert wants to merge 1 commit into
Open
Fix duplicated output from empty match after trailing newline (--across)#352yhuikzdtguioaert wants to merge 1 commit into
yhuikzdtguioaert wants to merge 1 commit into
Conversation
In whole-input mode the multi-line regex treats the position right after
a trailing newline as the start of an empty final line. Patterns that can
match nothing there (`.*`, `^`, `$`, ...) matched it and emitted an extra
replacement, so `echo abc | sd -A '(.*)' '${1}def'` returned two lines
("abcdef" and "def") instead of one.
Ignore a zero-width match at end-of-input when the input ends in a
newline, matching sed's behaviour. Genuine empty lines and zero-width
matches elsewhere are left untouched.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Reported in #318. In whole-input (
-A/--across) mode, matching a pattern that can match the empty string duplicates the output:Using
'${1}'on its own only appeared to work because the extra match expands to an empty string, so it was invisible.Cause
In
--acrossmode the whole input is handed to a multi-line regex. The position right after the trailing\nis treated as the start of an empty final "line", so patterns like.*,^and$produce a zero-width match there and emit one extra replacement.Fix
Skip a zero-width match located at the end of the input when the input ends with a newline. This lines up with
sed:Matches that are non-empty, that are not at end-of-input, or cases where the input has no trailing newline are untouched, so genuine empty lines (e.g.
abc\n\n),^-prefixing,$-suffixing and patterns such asx*all keep their existing behaviour.Tests
Added regression tests in
sd/src/replacer/tests.rscovering the whole-input replace path, plus an end-to-end CLI test insd-cli/tests/cli.rs.cargo testis green.Fixes #318