cmStringReplaceHelper.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmStringReplaceHelper.h"
  4. #include <sstream>
  5. #include <utility>
  6. #include "cmMakefile.h"
  7. #include "cmPolicies.h"
  8. cmStringReplaceHelper::cmStringReplaceHelper(std::string const& regex,
  9. std::string replace_expr,
  10. cmMakefile* makefile)
  11. : RegExString(regex)
  12. , RegularExpression(regex)
  13. , ReplaceExpression(std::move(replace_expr))
  14. , Makefile(makefile)
  15. {
  16. this->ParseReplaceExpression();
  17. }
  18. bool cmStringReplaceHelper::Replace(cm::string_view input, std::string& output)
  19. {
  20. output.clear();
  21. unsigned optAnchor = 0;
  22. if (this->Makefile &&
  23. this->Makefile->GetPolicyStatus(cmPolicies::CMP0186) !=
  24. cmPolicies::NEW) {
  25. optAnchor = cmsys::RegularExpression::BOL_AT_OFFSET;
  26. }
  27. // Scan through the input for all matches.
  28. auto& re = this->RegularExpression;
  29. std::string::size_type base = 0;
  30. unsigned optNonEmpty = 0;
  31. while (re.find(input.data(), base, optAnchor | optNonEmpty)) {
  32. if (this->Makefile) {
  33. this->Makefile->ClearMatches();
  34. this->Makefile->StoreMatches(re);
  35. }
  36. // Concatenate the part of the input that was not matched.
  37. output += input.substr(base, re.start() - base);
  38. // Concatenate the replacement for the match.
  39. for (auto const& replacement : this->Replacements) {
  40. if (replacement.Number < 0) {
  41. // This is just a plain-text part of the replacement.
  42. output += replacement.Value;
  43. } else {
  44. // Replace with part of the match.
  45. auto n = replacement.Number;
  46. if (n > re.num_groups()) {
  47. std::ostringstream error;
  48. error << "replace expression \"" << this->ReplaceExpression
  49. << "\" contains an out-of-range escape for regex \""
  50. << this->RegExString << "\"";
  51. this->ErrorString = error.str();
  52. return false;
  53. }
  54. output += re.match(n);
  55. }
  56. }
  57. // Move past the match.
  58. base = re.end();
  59. if (re.start() == input.length()) {
  60. break;
  61. }
  62. if (re.start() == re.end()) {
  63. optNonEmpty = cmsys::RegularExpression::NONEMPTY_AT_OFFSET;
  64. } else {
  65. optNonEmpty = 0;
  66. }
  67. }
  68. // Concatenate the text after the last match.
  69. output += input.substr(base);
  70. return true;
  71. }
  72. void cmStringReplaceHelper::ParseReplaceExpression()
  73. {
  74. std::string::size_type l = 0;
  75. while (l < this->ReplaceExpression.length()) {
  76. auto r = this->ReplaceExpression.find('\\', l);
  77. if (r == std::string::npos) {
  78. r = this->ReplaceExpression.length();
  79. this->Replacements.emplace_back(
  80. this->ReplaceExpression.substr(l, r - l));
  81. } else {
  82. if (r - l > 0) {
  83. this->Replacements.emplace_back(
  84. this->ReplaceExpression.substr(l, r - l));
  85. }
  86. if (r == (this->ReplaceExpression.length() - 1)) {
  87. this->ValidReplaceExpression = false;
  88. this->ErrorString = "replace-expression ends in a backslash";
  89. return;
  90. }
  91. if ((this->ReplaceExpression[r + 1] >= '0') &&
  92. (this->ReplaceExpression[r + 1] <= '9')) {
  93. this->Replacements.emplace_back(this->ReplaceExpression[r + 1] - '0');
  94. } else if (this->ReplaceExpression[r + 1] == 'n') {
  95. this->Replacements.emplace_back("\n");
  96. } else if (this->ReplaceExpression[r + 1] == '\\') {
  97. this->Replacements.emplace_back("\\");
  98. } else {
  99. this->ValidReplaceExpression = false;
  100. std::ostringstream error;
  101. error << "Unknown escape \"" << this->ReplaceExpression.substr(r, 2)
  102. << "\" in replace-expression";
  103. this->ErrorString = error.str();
  104. return;
  105. }
  106. r += 2;
  107. }
  108. l = r;
  109. }
  110. }