diff options
author | Marc Chevrier <marc.chevrier@sap.com> | 2018-04-03 13:09:53 (GMT) |
---|---|---|
committer | Marc Chevrier <marc.chevrier@sap.com> | 2018-04-16 13:04:14 (GMT) |
commit | cdae12f8f86730e075598118ebe5fd2f11746af7 (patch) | |
tree | 9e8dfbd6dffbdcb571126f78346af549e176ce48 /Source/cmStringCommand.cxx | |
parent | 912c2a6c7f4c0e0f478740254d063f4f9cd2c892 (diff) | |
download | CMake-cdae12f8f86730e075598118ebe5fd2f11746af7.zip CMake-cdae12f8f86730e075598118ebe5fd2f11746af7.tar.gz CMake-cdae12f8f86730e075598118ebe5fd2f11746af7.tar.bz2 |
string() Refactoring: creates an helper for REGEX REPLACE
Diffstat (limited to 'Source/cmStringCommand.cxx')
-rw-r--r-- | Source/cmStringCommand.cxx | 100 |
1 files changed, 14 insertions, 86 deletions
diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx index 9631912..dabec47 100644 --- a/Source/cmStringCommand.cxx +++ b/Source/cmStringCommand.cxx @@ -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; |