MXS-2785: Make rewrite_src a regular expression

The use of a regular expression allows multiple rewrite rules to be
combined into one. This allows more versatile conversions but, given the
simple nature of regular expressions, also makes accidental changes more
likely.

Addd mxs::pcre2_substitute that is a more C++-friendly version of
mxs_pcre2_substitute to make. This makes string replacement a lot easier
to do when the source and destination are not C strings.
This commit is contained in:
Markus Mäkelä
2019-12-09 11:03:47 +02:00
parent f3f2748490
commit 689a284892
5 changed files with 49 additions and 25 deletions

View File

@ -245,3 +245,26 @@ bool mxs_pcre2_check_match_exclude(pcre2_code* re_match,
}
return rval;
}
namespace maxscale
{
std::string pcre2_substitute(pcre2_code* re, const std::string& subject, const std::string& replace)
{
std::string rval = subject;
size_t size_tmp = rval.size();
int rc;
while ((rc = pcre2_substitute(re, (PCRE2_SPTR) subject.c_str(), subject.length(),
0, PCRE2_SUBSTITUTE_GLOBAL, NULL, NULL,
(PCRE2_SPTR) replace.c_str(), replace.length(),
(PCRE2_UCHAR*) &rval[0], &size_tmp)) == PCRE2_ERROR_NOMEMORY)
{
rval.resize(rval.size() * 2);
size_tmp = rval.size();
}
rval.resize(size_tmp);
return rval;
}
}