mirror of
https://github.com/Kitware/CMake.git
synced 2026-02-22 06:59:01 -06:00
string() Refactoring: creates an helper for REGEX REPLACE
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include "cmCryptoHash.h"
|
||||
#include "cmGeneratorExpression.h"
|
||||
#include "cmMakefile.h"
|
||||
#include "cmStringReplaceHelper.h"
|
||||
#include "cmSystemTools.h"
|
||||
#include "cmTimestamp.h"
|
||||
#include "cmUuid.h"
|
||||
@@ -344,46 +345,17 @@ bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
|
||||
std::string const& regex = args[2];
|
||||
std::string const& replace = args[3];
|
||||
std::string const& outvar = args[4];
|
||||
cmStringReplaceHelper replaceHelper(regex, replace, this->Makefile);
|
||||
|
||||
// Pull apart the replace expression to find the escaped [0-9] values.
|
||||
std::vector<RegexReplacement> replacement;
|
||||
std::string::size_type l = 0;
|
||||
while (l < replace.length()) {
|
||||
std::string::size_type r = replace.find('\\', l);
|
||||
if (r == std::string::npos) {
|
||||
r = replace.length();
|
||||
replacement.push_back(replace.substr(l, r - l));
|
||||
} else {
|
||||
if (r - l > 0) {
|
||||
replacement.push_back(replace.substr(l, r - l));
|
||||
}
|
||||
if (r == (replace.length() - 1)) {
|
||||
this->SetError("sub-command REGEX, mode REPLACE: "
|
||||
"replace-expression ends in a backslash.");
|
||||
return false;
|
||||
}
|
||||
if ((replace[r + 1] >= '0') && (replace[r + 1] <= '9')) {
|
||||
replacement.push_back(replace[r + 1] - '0');
|
||||
} else if (replace[r + 1] == 'n') {
|
||||
replacement.push_back("\n");
|
||||
} else if (replace[r + 1] == '\\') {
|
||||
replacement.push_back("\\");
|
||||
} else {
|
||||
std::string e = "sub-command REGEX, mode REPLACE: Unknown escape \"";
|
||||
e += replace.substr(r, 2);
|
||||
e += "\" in replace-expression.";
|
||||
this->SetError(e);
|
||||
return false;
|
||||
}
|
||||
r += 2;
|
||||
}
|
||||
l = r;
|
||||
if (!replaceHelper.IsReplaceExpressionValid()) {
|
||||
this->SetError("sub-command REGEX, mode REPLACE: " +
|
||||
replaceHelper.GetError() + ".");
|
||||
return false;
|
||||
}
|
||||
|
||||
this->Makefile->ClearMatches();
|
||||
// Compile the regular expression.
|
||||
cmsys::RegularExpression re;
|
||||
if (!re.compile(regex.c_str())) {
|
||||
|
||||
if (!replaceHelper.IsRegularExpressionValid()) {
|
||||
std::string e =
|
||||
"sub-command REGEX, mode REPLACE failed to compile regex \"" + regex +
|
||||
"\".";
|
||||
@@ -392,60 +364,16 @@ bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
|
||||
}
|
||||
|
||||
// Concatenate all the last arguments together.
|
||||
std::string input = cmJoin(cmMakeRange(args).advance(5), std::string());
|
||||
|
||||
// Scan through the input for all matches.
|
||||
const std::string input =
|
||||
cmJoin(cmMakeRange(args).advance(5), std::string());
|
||||
std::string output;
|
||||
std::string::size_type base = 0;
|
||||
while (re.find(input.c_str() + base)) {
|
||||
this->Makefile->ClearMatches();
|
||||
this->Makefile->StoreMatches(re);
|
||||
std::string::size_type l2 = re.start();
|
||||
std::string::size_type r = re.end();
|
||||
|
||||
// Concatenate the part of the input that was not matched.
|
||||
output += input.substr(base, l2);
|
||||
|
||||
// Make sure the match had some text.
|
||||
if (r - l2 == 0) {
|
||||
std::string e = "sub-command REGEX, mode REPLACE regex \"" + regex +
|
||||
"\" matched an empty string.";
|
||||
this->SetError(e);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Concatenate the replacement for the match.
|
||||
for (RegexReplacement const& i : replacement) {
|
||||
if (i.number < 0) {
|
||||
// This is just a plain-text part of the replacement.
|
||||
output += i.value;
|
||||
} else {
|
||||
// Replace with part of the match.
|
||||
int n = i.number;
|
||||
std::string::size_type start = re.start(n);
|
||||
std::string::size_type end = re.end(n);
|
||||
std::string::size_type len = input.length() - base;
|
||||
if ((start != std::string::npos) && (end != std::string::npos) &&
|
||||
(start <= len) && (end <= len)) {
|
||||
output += input.substr(base + start, end - start);
|
||||
} else {
|
||||
std::string e =
|
||||
"sub-command REGEX, mode REPLACE: replace expression \"" +
|
||||
replace + "\" contains an out-of-range escape for regex \"" +
|
||||
regex + "\".";
|
||||
this->SetError(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Move past the match.
|
||||
base += r;
|
||||
if (!replaceHelper.Replace(input, output)) {
|
||||
this->SetError("sub-command REGEX, mode REPLACE: " +
|
||||
replaceHelper.GetError() + ".");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Concatenate the text after the last match.
|
||||
output += input.substr(base, input.length() - base);
|
||||
|
||||
// Store the output in the provided variable.
|
||||
this->Makefile->AddDefinition(outvar, output.c_str());
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user