From 95c579df686f115c0fd3697f2723fa73476c4584 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Tue, 23 Mar 2021 18:50:55 +0300 Subject: Add regex_replace_parse() overloads --- tests/builtin/sed.testscript | 6 ++-- tests/regex/driver.cxx | 22 +++++++++++---- tests/regex/testscript | 67 ++++++++++++++++++++++++++------------------ 3 files changed, 59 insertions(+), 36 deletions(-) (limited to 'tests') diff --git a/tests/builtin/sed.testscript b/tests/builtin/sed.testscript index 7fbc9b2..2ed3088 100644 --- a/tests/builtin/sed.testscript +++ b/tests/builtin/sed.testscript @@ -166,13 +166,13 @@ test.options += -c : unterminated : $* -e 's/foo' 2>>EOE != 0 - sed: unterminated 's' command regex in 's/foo' + sed: invalid 's' command 's/foo': no delimiter after regex EOE : empty : $* -e 's///' 2>>EOE != 0 - sed: empty regex in 's' command in 's///' + sed: invalid 's' command 's///': empty regex EOE : invalid @@ -188,7 +188,7 @@ test.options += -c : unterminated-replacement : $* -e 's/foo/bar' 2>>EOE != 0 - sed: unterminated 's' command replacement in 's/foo/bar' + sed: invalid 's' command 's/foo/bar': no delimiter after replacement EOE : invalid-flags diff --git a/tests/regex/driver.cxx b/tests/regex/driver.cxx index f78a100..cb59cd8 100644 --- a/tests/regex/driver.cxx +++ b/tests/regex/driver.cxx @@ -4,8 +4,11 @@ #include #ifndef __cpp_lib_modules_ts +#include #include +#include // pair #include +#include // invalid_argument #include #endif @@ -27,7 +30,7 @@ import butl.utility; // operator<<(ostream, exception) using namespace std; using namespace butl; -// Usage: argv[0] [-ffo] [-fnc] [-m] +// Usage: argv[0] [-ffo] [-fnc] [-m] "///" // // Perform substitution of matched substrings with formatted replacement // strings using regex_replace_*() functions. If the string matches the regex @@ -66,11 +69,13 @@ try break; } - assert (i + 3 == argc); + assert (i + 2 == argc); - string s (argv[i++]); - regex re (argv[i++]); - string fmt (argv[i]); + string s (argv[i++]); + pair rf (regex_replace_parse (argv[i])); + + const regex& re (rf.first); + const string& fmt (rf.second); auto r (match ? regex_replace_match (s, re, fmt) @@ -86,8 +91,13 @@ catch (const regex_error& e) cerr << "invalid regex" << e << endl; // Print sanitized. return 2; } -catch (const exception& e) +catch (const invalid_argument& e) { cerr << e << endl; return 2; } +catch (const exception&) +{ + assert (false); + return 2; +} diff --git a/tests/regex/testscript b/tests/regex/testscript index fbee1d6..93ad4b6 100644 --- a/tests/regex/testscript +++ b/tests/regex/testscript @@ -4,38 +4,38 @@ : replace-search : { - $* abcbd b x >axcxd : all - $* -ffo abcbd b x >axcbd : first-only - $* -fnc abcbd b x >xx : no-copy + $* abcbd /b/x/ >axcxd : all + $* -ffo abcbd /b/x/ >axcbd : first-only + $* -fnc abcbd /b/x/ >xx : no-copy : ecma-escape : { - $* xay a '$b' >'x$by' : none - $* xay a '$' >'x$y' : none-term - $* xay a '$$' >'x$y' : self - $* xay a 'b$&c' >'xbacy' : match - $* xay a 'b$`c' >'xbxcy' : match-precede - $* xay a "b\\\$'c" >'xbycy' : match-follow + $* xay '/a/$b/' >'x$by' : none + $* xay '/a/$/' >'x$y' : none-term + $* xay '/a/$$/' >'x$y' : self + $* xay '/a/b$&c/' >'xbacy' : match + $* xay '/a/b$`c/' >'xbxcy' : match-precede + $* xay "/a/b\\\$'c/" >'xbycy' : match-follow : capture : { - $* abcdefghij '(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)' '$1$10' >aj : matched - $* a '(a)|(b)' '$1$2$3' >a : unmatched + $* abcdefghij '/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)/$1$10/' >aj : matched + $* a '/(a)|(b)/$1$2$3/' >a : unmatched } } : perl-escape : { - $* xay a '\b' >'xby' : none - $* xay a '\' >'xy' : none-term - $* xay a '\\' >'x\y' : self + $* xay '/a/\b/' >'xby' : none + $* xay '/a/\/' >'xy' : none-term + $* xay '/a/\\/' >'x\y' : self : newline : - $* xay a '\n' >>EOO + $* xay '/a/\n/' >>EOO x y EOO @@ -43,25 +43,25 @@ : capture : { - $* abcdefghij '(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)' '\1\10' >aa0 : matched - $* a '(a)|(b)' '\1\2\3' >a : unmatched + $* abcdefghij '/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)/\1\10/' >aa0 : matched + $* a '/(a)|(b)/\1\2\3/' >a : unmatched } : upper : { - $* xay a '\U' >xy : none - $* xay a '\Uvz' >xVZy : repl - $* xay a '\Uv\Ez' >xVzy : end - $* aa a 'v\Uz' >vZvZ : locality - $* xay '(a)' '\U\1' >xAy : capt - $* x-y '(a?)-' '\U\1z' >xZy : capt-empty - $* xay a '\uvz' >xVzy : once + $* xay '/a/\U/' >xy : none + $* xay '/a/\Uvz/' >xVZy : repl + $* xay '/a/\Uv\Ez/' >xVzy : end + $* aa '/a/v\Uz/' >vZvZ : locality + $* xay '/(a)/\U\1/' >xAy : capt + $* x-y '/(a?)-/\U\1z/' >xZy : capt-empty + $* xay '/a/\uvz/' >xVzy : once } : lower : - $* xay a '\lVZ' >xvZy + $* xay '/a/\lVZ/' >xvZy } } @@ -70,6 +70,19 @@ { test.options += -m - $* abc 'a(b)c' 'x\1y' >xby : match - $* abcd 'a(b)c' 'x\1yd' == 1 : no-match + $* abc '/a(b)c/x\1y/' >xby : match + $* abcd '/a(b)c/x\1yd/' == 1 : no-match +} + +: invalid-regex-fmt +: +{ + test.arguments += '' # Note: we will fail before the matching. + + $* '' 2> 'no leading delimiter' != 0 : no-leading-delim + $* '/a' 2> 'no delimiter after regex' != 0 : no-mid-delim + $* '//' 2> 'empty regex' != 0 : no-regex + $* '/a[b/c/' 2>~'/invalid regex.*/' != 0 : regex + $* '/a/b' 2> 'no delimiter after replacement' != 0 : no-trailing-delim + $* '/a/b/s' 2> 'junk after trailing delimiter' != 0 : junk } -- cgit v1.1