Regex.cpp 6.2 KB

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