StringRef.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //===-- StringRef.cpp - Lightweight String References ---------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. using namespace llvm;
  12. // MSVC emits references to this into the translation units which reference it.
  13. #ifndef _MSC_VER
  14. const size_t StringRef::npos;
  15. #endif
  16. static char ascii_tolower(char x) {
  17. if (x >= 'A' && x <= 'Z')
  18. return x - 'A' + 'a';
  19. return x;
  20. }
  21. /// compare_lower - Compare strings, ignoring case.
  22. int StringRef::compare_lower(StringRef RHS) const {
  23. for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
  24. char LHC = ascii_tolower(Data[I]);
  25. char RHC = ascii_tolower(RHS.Data[I]);
  26. if (LHC != RHC)
  27. return LHC < RHC ? -1 : 1;
  28. }
  29. if (Length == RHS.Length)
  30. return 0;
  31. return Length < RHS.Length ? -1 : 1;
  32. }
  33. // Compute the edit distance between the two given strings.
  34. unsigned StringRef::edit_distance(llvm::StringRef Other,
  35. bool AllowReplacements) {
  36. // The algorithm implemented below is the "classic"
  37. // dynamic-programming algorithm for computing the Levenshtein
  38. // distance, which is described here:
  39. //
  40. // http://en.wikipedia.org/wiki/Levenshtein_distance
  41. //
  42. // Although the algorithm is typically described using an m x n
  43. // array, only two rows are used at a time, so this implemenation
  44. // just keeps two separate vectors for those two rows.
  45. size_type m = size();
  46. size_type n = Other.size();
  47. SmallVector<unsigned, 32> previous(n+1, 0);
  48. for (SmallVector<unsigned, 32>::size_type i = 0; i <= n; ++i)
  49. previous[i] = i;
  50. SmallVector<unsigned, 32> current(n+1, 0);
  51. for (size_type y = 1; y <= m; ++y) {
  52. current.assign(n+1, 0);
  53. current[0] = y;
  54. for (size_type x = 1; x <= n; ++x) {
  55. if (AllowReplacements) {
  56. current[x] = min(previous[x-1] + ((*this)[y-1] == Other[x-1]? 0u:1u),
  57. min(current[x-1], previous[x])+1);
  58. }
  59. else {
  60. if ((*this)[y-1] == Other[x-1]) current[x] = previous[x-1];
  61. else current[x] = min(current[x-1], previous[x]) + 1;
  62. }
  63. }
  64. current.swap(previous);
  65. }
  66. return previous[n];
  67. }
  68. //===----------------------------------------------------------------------===//
  69. // String Searching
  70. //===----------------------------------------------------------------------===//
  71. /// find - Search for the first string \arg Str in the string.
  72. ///
  73. /// \return - The index of the first occurence of \arg Str, or npos if not
  74. /// found.
  75. size_t StringRef::find(StringRef Str, size_t From) const {
  76. size_t N = Str.size();
  77. if (N > Length)
  78. return npos;
  79. for (size_t e = Length - N + 1, i = min(From, e); i != e; ++i)
  80. if (substr(i, N).equals(Str))
  81. return i;
  82. return npos;
  83. }
  84. /// rfind - Search for the last string \arg Str in the string.
  85. ///
  86. /// \return - The index of the last occurence of \arg Str, or npos if not
  87. /// found.
  88. size_t StringRef::rfind(StringRef Str) const {
  89. size_t N = Str.size();
  90. if (N > Length)
  91. return npos;
  92. for (size_t i = Length - N + 1, e = 0; i != e;) {
  93. --i;
  94. if (substr(i, N).equals(Str))
  95. return i;
  96. }
  97. return npos;
  98. }
  99. /// find_first_of - Find the first character in the string that is in \arg
  100. /// Chars, or npos if not found.
  101. ///
  102. /// Note: O(size() * Chars.size())
  103. StringRef::size_type StringRef::find_first_of(StringRef Chars,
  104. size_t From) const {
  105. for (size_type i = min(From, Length), e = Length; i != e; ++i)
  106. if (Chars.find(Data[i]) != npos)
  107. return i;
  108. return npos;
  109. }
  110. /// find_first_not_of - Find the first character in the string that is not
  111. /// \arg C or npos if not found.
  112. StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
  113. for (size_type i = min(From, Length), e = Length; i != e; ++i)
  114. if (Data[i] != C)
  115. return i;
  116. return npos;
  117. }
  118. /// find_first_not_of - Find the first character in the string that is not
  119. /// in the string \arg Chars, or npos if not found.
  120. ///
  121. /// Note: O(size() * Chars.size())
  122. StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
  123. size_t From) const {
  124. for (size_type i = min(From, Length), e = Length; i != e; ++i)
  125. if (Chars.find(Data[i]) == npos)
  126. return i;
  127. return npos;
  128. }
  129. //===----------------------------------------------------------------------===//
  130. // Helpful Algorithms
  131. //===----------------------------------------------------------------------===//
  132. /// count - Return the number of non-overlapped occurrences of \arg Str in
  133. /// the string.
  134. size_t StringRef::count(StringRef Str) const {
  135. size_t Count = 0;
  136. size_t N = Str.size();
  137. if (N > Length)
  138. return 0;
  139. for (size_t i = 0, e = Length - N + 1; i != e; ++i)
  140. if (substr(i, N).equals(Str))
  141. ++Count;
  142. return Count;
  143. }
  144. /// GetAsUnsignedInteger - Workhorse method that converts a integer character
  145. /// sequence of radix up to 36 to an unsigned long long value.
  146. static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,
  147. unsigned long long &Result) {
  148. // Autosense radix if not specified.
  149. if (Radix == 0) {
  150. if (Str.startswith("0x")) {
  151. Str = Str.substr(2);
  152. Radix = 16;
  153. } else if (Str.startswith("0b")) {
  154. Str = Str.substr(2);
  155. Radix = 2;
  156. } else if (Str.startswith("0"))
  157. Radix = 8;
  158. else
  159. Radix = 10;
  160. }
  161. // Empty strings (after the radix autosense) are invalid.
  162. if (Str.empty()) return true;
  163. // Parse all the bytes of the string given this radix. Watch for overflow.
  164. Result = 0;
  165. while (!Str.empty()) {
  166. unsigned CharVal;
  167. if (Str[0] >= '0' && Str[0] <= '9')
  168. CharVal = Str[0]-'0';
  169. else if (Str[0] >= 'a' && Str[0] <= 'z')
  170. CharVal = Str[0]-'a'+10;
  171. else if (Str[0] >= 'A' && Str[0] <= 'Z')
  172. CharVal = Str[0]-'A'+10;
  173. else
  174. return true;
  175. // If the parsed value is larger than the integer radix, the string is
  176. // invalid.
  177. if (CharVal >= Radix)
  178. return true;
  179. // Add in this character.
  180. unsigned long long PrevResult = Result;
  181. Result = Result*Radix+CharVal;
  182. // Check for overflow.
  183. if (Result < PrevResult)
  184. return true;
  185. Str = Str.substr(1);
  186. }
  187. return false;
  188. }
  189. bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
  190. return GetAsUnsignedInteger(*this, Radix, Result);
  191. }
  192. bool StringRef::getAsInteger(unsigned Radix, long long &Result) const {
  193. unsigned long long ULLVal;
  194. // Handle positive strings first.
  195. if (empty() || front() != '-') {
  196. if (GetAsUnsignedInteger(*this, Radix, ULLVal) ||
  197. // Check for value so large it overflows a signed value.
  198. (long long)ULLVal < 0)
  199. return true;
  200. Result = ULLVal;
  201. return false;
  202. }
  203. // Get the positive part of the value.
  204. if (GetAsUnsignedInteger(substr(1), Radix, ULLVal) ||
  205. // Reject values so large they'd overflow as negative signed, but allow
  206. // "-0". This negates the unsigned so that the negative isn't undefined
  207. // on signed overflow.
  208. (long long)-ULLVal > 0)
  209. return true;
  210. Result = -ULLVal;
  211. return false;
  212. }
  213. bool StringRef::getAsInteger(unsigned Radix, int &Result) const {
  214. long long Val;
  215. if (getAsInteger(Radix, Val) ||
  216. (int)Val != Val)
  217. return true;
  218. Result = Val;
  219. return false;
  220. }
  221. bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const {
  222. unsigned long long Val;
  223. if (getAsInteger(Radix, Val) ||
  224. (unsigned)Val != Val)
  225. return true;
  226. Result = Val;
  227. return false;
  228. }