sed: fix handling of escaped delimiters in s/// replacement

function                                             old     new   delta
parse_regex_delim                                    111     140     +29

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2022-01-23 19:04:27 +01:00
parent e998c7c032
commit f12fb1e409
2 changed files with 11 additions and 3 deletions

View File

@ -355,7 +355,10 @@ static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
/* save the replacement string */ /* save the replacement string */
cmdstr_ptr += idx + 1; cmdstr_ptr += idx + 1;
idx = index_of_next_unescaped_regexp_delim(- (int)delimiter, cmdstr_ptr); idx = index_of_next_unescaped_regexp_delim(- (int)delimiter, cmdstr_ptr);
*replace = copy_parsing_escapes(cmdstr_ptr, idx, 0); //GNU sed 4.8:
// echo 789 | sed 's&8&\&&' - 7&9 ("\&" remained "\&")
// echo 789 | sed 's1\(8\)1\1\11' - 7119 ("\1\1" become "11")
*replace = copy_parsing_escapes(cmdstr_ptr, idx, delimiter != '&' ? delimiter : 0);
return ((cmdstr_ptr - cmdstr) + idx); return ((cmdstr_ptr - cmdstr) + idx);
} }

View File

@ -329,10 +329,15 @@ testing "sed special char as s/// delimiter, in pattern" \
"sed 's+9\++X+'" \ "sed 's+9\++X+'" \
"X8=17\n" "" "9+8=17\n" "X8=17\n" "" "9+8=17\n"
# but in replacement string, "\&" remains "\&", not interpreted as "&" # Matching GNU sed 4.8:
testing "sed special char as s/// delimiter, in replacement" \ # in replacement string, "\&" remains "\&", not interpreted as "&"
testing "sed special char as s/// delimiter, in replacement 1" \
"sed 's&9&X\&&'" \ "sed 's&9&X\&&'" \
"X&+8=17\n" "" "9+8=17\n" "X&+8=17\n" "" "9+8=17\n"
# in replacement string, "\1" is interpreted as "1"
testing "sed special char as s/// delimiter, in replacement 2" \
"sed 's1\(9\)1X\11'" \
"X1+8=17\n" "" "9+8=17\n"
testing "sed /\$_in_regex/ should not match newlines, only end-of-line" \ testing "sed /\$_in_regex/ should not match newlines, only end-of-line" \
"sed ': testcont; /\\\\$/{ =; N; b testcont }'" \ "sed ': testcont; /\\\\$/{ =; N; b testcont }'" \