1 //===-- StringRef.cpp - Lightweight String References ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/StringRef.h"
15 // MSVC emits references to this into the translation units which reference it.
17 const size_t StringRef::npos;
20 static char ascii_tolower(char x) {
21 if (x >= 'A' && x <= 'Z')
26 /// compare_lower - Compare strings, ignoring case.
27 int StringRef::compare_lower(StringRef RHS) const {
28 for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
29 char LHC = ascii_tolower(Data[I]);
30 char RHC = ascii_tolower(RHS.Data[I]);
32 return LHC < RHC ? -1 : 1;
35 if (Length == RHS.Length)
37 return Length < RHS.Length ? -1 : 1;
40 // Compute the edit distance between the two given strings.
41 unsigned StringRef::edit_distance(llvm::StringRef Other,
42 bool AllowReplacements) {
43 // The algorithm implemented below is the "classic"
44 // dynamic-programming algorithm for computing the Levenshtein
45 // distance, which is described here:
47 // http://en.wikipedia.org/wiki/Levenshtein_distance
49 // Although the algorithm is typically described using an m x n
50 // array, only two rows are used at a time, so this implemenation
51 // just keeps two separate vectors for those two rows.
53 size_type n = Other.size();
55 unsigned SmallPrevious[32];
56 unsigned SmallCurrent[32];
58 unsigned *previous = SmallPrevious;
59 unsigned *current = SmallCurrent;
61 previous = new unsigned [n+1];
62 current = new unsigned [n+1];
65 for (unsigned i = 0; i <= n; ++i)
68 for (size_type y = 1; y <= m; ++y) {
69 std::memset(current, 0, (n + 1) * sizeof(unsigned));
71 for (size_type x = 1; x <= n; ++x) {
72 if (AllowReplacements) {
73 current[x] = min(previous[x-1] + ((*this)[y-1] == Other[x-1]? 0u:1u),
74 min(current[x-1], previous[x])+1);
77 if ((*this)[y-1] == Other[x-1]) current[x] = previous[x-1];
78 else current[x] = min(current[x-1], previous[x]) + 1;
82 unsigned *tmp = current;
87 unsigned Result = previous[n];
96 //===----------------------------------------------------------------------===//
98 //===----------------------------------------------------------------------===//
101 /// find - Search for the first string \arg Str in the string.
103 /// \return - The index of the first occurence of \arg Str, or npos if not
105 size_t StringRef::find(StringRef Str, size_t From) const {
106 size_t N = Str.size();
109 for (size_t e = Length - N + 1, i = min(From, e); i != e; ++i)
110 if (substr(i, N).equals(Str))
115 /// rfind - Search for the last string \arg Str in the string.
117 /// \return - The index of the last occurence of \arg Str, or npos if not
119 size_t StringRef::rfind(StringRef Str) const {
120 size_t N = Str.size();
123 for (size_t i = Length - N + 1, e = 0; i != e;) {
125 if (substr(i, N).equals(Str))
131 /// find_first_of - Find the first character in the string that is in \arg
132 /// Chars, or npos if not found.
134 /// Note: O(size() * Chars.size())
135 StringRef::size_type StringRef::find_first_of(StringRef Chars,
137 for (size_type i = min(From, Length), e = Length; i != e; ++i)
138 if (Chars.find(Data[i]) != npos)
143 /// find_first_not_of - Find the first character in the string that is not
144 /// \arg C or npos if not found.
145 StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
146 for (size_type i = min(From, Length), e = Length; i != e; ++i)
152 /// find_first_not_of - Find the first character in the string that is not
153 /// in the string \arg Chars, or npos if not found.
155 /// Note: O(size() * Chars.size())
156 StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
158 for (size_type i = min(From, Length), e = Length; i != e; ++i)
159 if (Chars.find(Data[i]) == npos)
165 //===----------------------------------------------------------------------===//
166 // Helpful Algorithms
167 //===----------------------------------------------------------------------===//
169 /// count - Return the number of non-overlapped occurrences of \arg Str in
171 size_t StringRef::count(StringRef Str) const {
173 size_t N = Str.size();
176 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
177 if (substr(i, N).equals(Str))
182 /// GetAsUnsignedInteger - Workhorse method that converts a integer character
183 /// sequence of radix up to 36 to an unsigned long long value.
184 static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,
185 unsigned long long &Result) {
186 // Autosense radix if not specified.
188 if (Str.startswith("0x")) {
191 } else if (Str.startswith("0b")) {
194 } else if (Str.startswith("0"))
200 // Empty strings (after the radix autosense) are invalid.
201 if (Str.empty()) return true;
203 // Parse all the bytes of the string given this radix. Watch for overflow.
205 while (!Str.empty()) {
207 if (Str[0] >= '0' && Str[0] <= '9')
208 CharVal = Str[0]-'0';
209 else if (Str[0] >= 'a' && Str[0] <= 'z')
210 CharVal = Str[0]-'a'+10;
211 else if (Str[0] >= 'A' && Str[0] <= 'Z')
212 CharVal = Str[0]-'A'+10;
216 // If the parsed value is larger than the integer radix, the string is
218 if (CharVal >= Radix)
221 // Add in this character.
222 unsigned long long PrevResult = Result;
223 Result = Result*Radix+CharVal;
225 // Check for overflow.
226 if (Result < PrevResult)
235 bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
236 return GetAsUnsignedInteger(*this, Radix, Result);
240 bool StringRef::getAsInteger(unsigned Radix, long long &Result) const {
241 unsigned long long ULLVal;
243 // Handle positive strings first.
244 if (empty() || front() != '-') {
245 if (GetAsUnsignedInteger(*this, Radix, ULLVal) ||
246 // Check for value so large it overflows a signed value.
247 (long long)ULLVal < 0)
253 // Get the positive part of the value.
254 if (GetAsUnsignedInteger(substr(1), Radix, ULLVal) ||
255 // Reject values so large they'd overflow as negative signed, but allow
256 // "-0". This negates the unsigned so that the negative isn't undefined
257 // on signed overflow.
258 (long long)-ULLVal > 0)
265 bool StringRef::getAsInteger(unsigned Radix, int &Result) const {
267 if (getAsInteger(Radix, Val) ||
274 bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const {
275 unsigned long long Val;
276 if (getAsInteger(Radix, Val) ||
277 (unsigned)Val != Val)