From 0cf84e1f006988c114bdca36715d3a2c0601a7d5 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 30 Aug 2017 10:23:06 +0300 Subject: Generalize regex_replace_ex() function --- tests/regex/buildfile | 7 ++++++ tests/regex/driver.cxx | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/regex/testscript | 54 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 tests/regex/buildfile create mode 100644 tests/regex/driver.cxx create mode 100644 tests/regex/testscript (limited to 'tests/regex') diff --git a/tests/regex/buildfile b/tests/regex/buildfile new file mode 100644 index 0000000..baf4bca --- /dev/null +++ b/tests/regex/buildfile @@ -0,0 +1,7 @@ +# file : tests/regex/buildfile +# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +import libs = libbutl%lib{butl} + +exe{driver}: {hxx cxx}{*} $libs test{testscript} diff --git a/tests/regex/driver.cxx b/tests/regex/driver.cxx new file mode 100644 index 0000000..054eb31 --- /dev/null +++ b/tests/regex/driver.cxx @@ -0,0 +1,66 @@ +// file : tests/regex/driver.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include +#include +#include +#include + +#include +#include // operator<<(ostream, exception) + +using namespace std; +using namespace butl; + +// Usage: argv[0] [-ffo] [-fnc] +// +// Perform substitution of matched substrings with formatted replacement +// strings using regex_replace_ex() function. If the string matches the regex +// then print the replacement to STDOUT and exit with zero code. Exit with +// code one if it doesn't match, and with code two on failure (print error +// description to STDERR). +// +// -ffo +// Use format_first_only replacement flag. +// +// -fnc +// Use format_no_copy replacement flag. +// +int +main (int argc, const char* argv[]) +try +{ + regex_constants::match_flag_type fl (regex_constants::match_default); + + int i (1); + for (; i != argc; ++i) + { + string op (argv[i]); + + if (op == "-ffo") + fl |= regex_constants::format_first_only; + else if (op == "-fnc") + fl |= regex_constants::format_no_copy; + else + break; + } + + assert (i + 3 == argc); + + string s (argv[i++]); + regex re (argv[i++]); + string fmt (argv[i]); + + auto r (regex_replace_ex (s, re, fmt, fl)); + + if (r.second) + cout << r.first << endl; + + return r.second ? 0 : 1; +} +catch (const exception& e) +{ + cerr << e << endl; + return 2; +} diff --git a/tests/regex/testscript b/tests/regex/testscript new file mode 100644 index 0000000..1af604c --- /dev/null +++ b/tests/regex/testscript @@ -0,0 +1,54 @@ +# file : tests/regex/testscript +# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +: match +: +{ + $* 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 + + : capture + : + $* abcdefghij '(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)' '$1$10' >aj + } + + : perl-escape + : + { + $* xay a '\b' >'xby' : none + $* xay a '\' >'xy' : none-term + $* xay a '\\' >'x\y' : self + + : capture + : + $* abcdefghij '(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)' '\1\10' >aa0 + + : 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 + } + + : lower + : + $* xay a '\lVZ' >xvZy + } +} -- cgit v1.1