Regex.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //===-- Regex.cpp - Regular Expression matcher implementation -------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements a POSIX regular expression matcher.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Support/Regex.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/ADT/Twine.h"
  16. #include <cassert>
  17. #include <string>
  18. // Important this comes last because it defines "_REGEX_H_". At least on
  19. // Darwin, if included before any header that (transitively) includes
  20. // xlocale.h, this will cause trouble, because of missing regex-related types.
  21. #include "regex_impl.h"
  22. using namespace llvm;
  23. Regex::Regex() : preg(nullptr), error(REG_BADPAT) {}
  24. Regex::Regex(StringRef regex, RegexFlags Flags) {
  25. unsigned flags = 0;
  26. preg = new llvm_regex();
  27. preg->re_endp = regex.end();
  28. if (Flags & IgnoreCase)
  29. flags |= REG_ICASE;
  30. if (Flags & Newline)
  31. flags |= REG_NEWLINE;
  32. if (!(Flags & BasicRegex))
  33. flags |= REG_EXTENDED;
  34. error = llvm_regcomp(preg, regex.data(), flags|REG_PEND);
  35. }
  36. Regex::Regex(StringRef regex, unsigned Flags)
  37. : Regex(regex, static_cast<RegexFlags>(Flags)) {}
  38. Regex::Regex(Regex &&regex) {
  39. preg = regex.preg;
  40. error = regex.error;
  41. regex.preg = nullptr;
  42. regex.error = REG_BADPAT;
  43. }
  44. Regex::~Regex() {
  45. if (preg) {
  46. llvm_regfree(preg);
  47. delete preg;
  48. }
  49. }
  50. namespace {
  51. /// Utility to convert a regex error code into a human-readable string.
  52. void RegexErrorToString(int error, struct llvm_regex *preg,
  53. std::string &Error) {
  54. size_t len = llvm_regerror(error, preg, nullptr, 0);
  55. Error.resize(len - 1);
  56. llvm_regerror(error, preg, &Error[0], len);
  57. }
  58. } // namespace
  59. bool Regex::isValid(std::string &Error) const {
  60. if (!error)
  61. return true;
  62. RegexErrorToString(error, preg, Error);
  63. return false;
  64. }
  65. /// getNumMatches - In a valid regex, return the number of parenthesized
  66. /// matches it contains.
  67. unsigned Regex::getNumMatches() const {
  68. return preg->re_nsub;
  69. }
  70. bool Regex::match(StringRef String, SmallVectorImpl<StringRef> *Matches,
  71. std::string *Error) const {
  72. // Reset error, if given.
  73. if (Error && !Error->empty())
  74. *Error = "";
  75. // Check if the regex itself didn't successfully compile.
  76. if (Error ? !isValid(*Error) : !isValid())
  77. return false;
  78. unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
  79. // pmatch needs to have at least one element.
  80. SmallVector<llvm_regmatch_t, 8> pm;
  81. pm.resize(nmatch > 0 ? nmatch : 1);
  82. pm[0].rm_so = 0;
  83. pm[0].rm_eo = String.size();
  84. int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND);
  85. // Failure to match is not an error, it's just a normal return value.
  86. // Any other error code is considered abnormal, and is logged in the Error.
  87. if (rc == REG_NOMATCH)
  88. return false;
  89. if (rc != 0) {
  90. if (Error)
  91. RegexErrorToString(error, preg, *Error);
  92. return false;
  93. }
  94. // There was a match.
  95. if (Matches) { // match position requested
  96. Matches->clear();
  97. for (unsigned i = 0; i != nmatch; ++i) {
  98. if (pm[i].rm_so == -1) {
  99. // this group didn't match
  100. Matches->push_back(StringRef());
  101. continue;
  102. }
  103. assert(pm[i].rm_eo >= pm[i].rm_so);
  104. Matches->push_back(StringRef(String.data()+pm[i].rm_so,
  105. pm[i].rm_eo-pm[i].rm_so));
  106. }
  107. }
  108. return true;
  109. }
  110. std::string Regex::sub(StringRef Repl, StringRef String,
  111. std::string *Error) const {
  112. SmallVector<StringRef, 8> Matches;
  113. // Return the input if there was no match.
  114. if (!match(String, &Matches, Error))
  115. return std::string(String);
  116. // Otherwise splice in the replacement string, starting with the prefix before
  117. // the match.
  118. std::string Res(String.begin(), Matches[0].begin());
  119. // Then the replacement string, honoring possible substitutions.
  120. while (!Repl.empty()) {
  121. // Skip to the next escape.
  122. std::pair<StringRef, StringRef> Split = Repl.split('\\');
  123. // Add the skipped substring.
  124. Res += Split.first;
  125. // Check for terminimation and trailing backslash.
  126. if (Split.second.empty()) {
  127. if (Repl.size() != Split.first.size() &&
  128. Error && Error->empty())
  129. *Error = "replacement string contained trailing backslash";
  130. break;
  131. }
  132. // Otherwise update the replacement string and interpret escapes.
  133. Repl = Split.second;
  134. // FIXME: We should have a StringExtras function for mapping C99 escapes.
  135. switch (Repl[0]) {
  136. // Treat all unrecognized characters as self-quoting.
  137. default:
  138. Res += Repl[0];
  139. Repl = Repl.substr(1);
  140. break;
  141. // Single character escapes.
  142. case 't':
  143. Res += '\t';
  144. Repl = Repl.substr(1);
  145. break;
  146. case 'n':
  147. Res += '\n';
  148. Repl = Repl.substr(1);
  149. break;
  150. // Decimal escapes are backreferences.
  151. case '0': case '1': case '2': case '3': case '4':
  152. case '5': case '6': case '7': case '8': case '9': {
  153. // Extract the backreference number.
  154. StringRef Ref = Repl.slice(0, Repl.find_first_not_of("0123456789"));
  155. Repl = Repl.substr(Ref.size());
  156. unsigned RefValue;
  157. if (!Ref.getAsInteger(10, RefValue) &&
  158. RefValue < Matches.size())
  159. Res += Matches[RefValue];
  160. else if (Error && Error->empty())
  161. *Error = ("invalid backreference string '" + Twine(Ref) + "'").str();
  162. break;
  163. }
  164. }
  165. }
  166. // And finally the suffix.
  167. Res += StringRef(Matches[0].end(), String.end() - Matches[0].end());
  168. return Res;
  169. }
  170. // These are the special characters matched in functions like "p_ere_exp".
  171. static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
  172. bool Regex::isLiteralERE(StringRef Str) {
  173. // Check for regex metacharacters. This list was derived from our regex
  174. // implementation in regcomp.c and double checked against the POSIX extended
  175. // regular expression specification.
  176. return Str.find_first_of(RegexMetachars) == StringRef::npos;
  177. }
  178. std::string Regex::escape(StringRef String) {
  179. std::string RegexStr;
  180. for (char C : String) {
  181. if (strchr(RegexMetachars, C))
  182. RegexStr += '\\';
  183. RegexStr += C;
  184. }
  185. return RegexStr;
  186. }