sed: deal with peculiar behavior of '2d;2,1p' in GNU sed

function                                             old     new   delta
process_files                                       2173    2120     -53

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2009-07-10 11:55:20 +02:00
parent 52a426744e
commit ae68f1133f
2 changed files with 16 additions and 12 deletions

View File

@ -894,12 +894,13 @@ static void process_files(void)
|| (!sed_cmd->beg_line && !sed_cmd->end_line || (!sed_cmd->beg_line && !sed_cmd->end_line
&& !sed_cmd->beg_match && !sed_cmd->end_match) && !sed_cmd->beg_match && !sed_cmd->end_match)
/* Or did we match the start of a numerical range? */ /* Or did we match the start of a numerical range? */
|| (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum || (sed_cmd->beg_line > 0
/* "shadowed beginning" case: "1d;1,ENDp" - p still matches at line 2 && (sed_cmd->beg_line == linenum
* even though 1d skipped line 1 which is a start line for p */ /* GNU sed compat:
|| (sed_cmd->end_line && sed_cmd->beg_line < linenum && sed_cmd->end_line >= linenum) * "shadowed beginning" case: "1d;1,ENDp" - p still matches at line 2
|| (sed_cmd->end_match && sed_cmd->beg_line < linenum) * even though 1d skipped line 1 which is a start line for p */
) || (sed_cmd->beg_line < linenum && (sed_cmd->end_line > 0 || sed_cmd->end_match))
)
) )
/* Or does this line match our begin address regex? */ /* Or does this line match our begin address regex? */
|| (beg_match(sed_cmd, pattern_space)) || (beg_match(sed_cmd, pattern_space))
@ -928,12 +929,10 @@ static void process_files(void)
&& (regexec(sed_cmd->end_match, && (regexec(sed_cmd->end_match,
pattern_space, 0, NULL, 0) == 0) pattern_space, 0, NULL, 0) == 0)
); );
if (n && sed_cmd->beg_line > 0) { }
/* Once matched, "n,regex" range is dead, disabling it */ if (n && sed_cmd->beg_line > 0) {
regfree(sed_cmd->end_match); /* once matched, "n,xxx" range is dead, disabling it */
free(sed_cmd->end_match); sed_cmd->beg_line = -2;
sed_cmd->end_match = NULL;
}
} }
sed_cmd->in_match = !n; sed_cmd->in_match = !n;
} }

View File

@ -220,4 +220,9 @@ testing "sed d does not break n,regex matching #2" \
"second2\nthird2\n" "" \ "second2\nthird2\n" "" \
"first\nsecond\nthird\nfourth\n""first2\nsecond2\nthird2\nfourth2\n" "first\nsecond\nthird\nfourth\n""first2\nsecond2\nthird2\nfourth2\n"
testing "sed 2d;2,1p (gnu compat)" \
"sed -n '2d;2,1p'" \
"third\n" "" \
"first\nsecond\nthird\nfourth\n"
exit $FAILCOUNT exit $FAILCOUNT