After reading the next input line, uutils sed's n command ends the current cycle. The new line is processed from the beginning of the script instead of continuing with the command after n.
GNU sed continues with the next command.
This was reproduced with GNU sed 4.10 and uutils sed built from cffecd15, both under LC_ALL=C.
For example, n;d should keep every other line:
$ printf 'a\nb\nc\nd\n' | sed 'n;d'
a
c
$ printf 'a\nb\nc\nd\n' | ./target/release/sed 'n;d'
a
b
c
d
The example used in the GNU sed manual shows the same difference:
$ seq 6 | sed 'n;n;s/./x/'
1
2
x
4
5
x
$ seq 6 | ./target/release/sed 'n;n;s/./x/'
1
2
3
4
5
6
POSIX describes n as: "Write the pattern space to standard output if the default output has not been suppressed, and replace the pattern space with the next line of input, less its terminating <newline>. If no next line of input is available, the n command verb shall branch to the end of the script and quit without starting a new cycle."
The n branch in src/sed/processor.rs currently uses break, which ends the cycle. The adjacent N implementation already preserves the next command in context.input_action and resumes there after reading another line. The same mechanism can be used for n, replacing the pattern space instead of appending to it.
I have a patch for this. Because the control-flow change also exposes the existing #541, I wanted to confirm the approach before opening a PR.
After reading the next input line, uutils sed's
ncommand ends the current cycle. The new line is processed from the beginning of the script instead of continuing with the command aftern.GNU sed continues with the next command.
This was reproduced with GNU sed 4.10 and uutils sed built from
cffecd15, both underLC_ALL=C.For example,
n;dshould keep every other line:The example used in the GNU sed manual shows the same difference:
POSIX describes
nas: "Write the pattern space to standard output if the default output has not been suppressed, and replace the pattern space with the next line of input, less its terminating<newline>. If no next line of input is available, the n command verb shall branch to the end of the script and quit without starting a new cycle."The
nbranch insrc/sed/processor.rscurrently usesbreak, which ends the cycle. The adjacentNimplementation already preserves the next command incontext.input_actionand resumes there after reading another line. The same mechanism can be used forn, replacing the pattern space instead of appending to it.I have a patch for this. Because the control-flow change also exposes the existing #541, I wanted to confirm the approach before opening a PR.