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"
11 #include "llvm/ADT/APInt.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 static bool ascii_isdigit(char x) {
27 return x >= '0' && x <= '9';
30 /// compare_lower - Compare strings, ignoring case.
31 int StringRef::compare_lower(StringRef RHS) const {
32 for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
33 char LHC = ascii_tolower(Data[I]);
34 char RHC = ascii_tolower(RHS.Data[I]);
36 return LHC < RHC ? -1 : 1;
39 if (Length == RHS.Length)
41 return Length < RHS.Length ? -1 : 1;
44 /// compare_numeric - Compare strings, handle embedded numbers.
45 int StringRef::compare_numeric(StringRef RHS) const {
46 for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
47 if (Data[I] == RHS.Data[I])
49 if (ascii_isdigit(Data[I]) && ascii_isdigit(RHS.Data[I])) {
50 // The longer sequence of numbers is larger. This doesn't really handle
51 // prefixed zeros well.
52 for (size_t J = I+1; J != E+1; ++J) {
53 bool ld = J < Length && ascii_isdigit(Data[J]);
54 bool rd = J < RHS.Length && ascii_isdigit(RHS.Data[J]);
61 return Data[I] < RHS.Data[I] ? -1 : 1;
63 if (Length == RHS.Length)
65 return Length < RHS.Length ? -1 : 1;
68 // Compute the edit distance between the two given strings.
69 unsigned StringRef::edit_distance(llvm::StringRef Other,
70 bool AllowReplacements) {
71 // The algorithm implemented below is the "classic"
72 // dynamic-programming algorithm for computing the Levenshtein
73 // distance, which is described here:
75 // http://en.wikipedia.org/wiki/Levenshtein_distance
77 // Although the algorithm is typically described using an m x n
78 // array, only two rows are used at a time, so this implemenation
79 // just keeps two separate vectors for those two rows.
81 size_type n = Other.size();
83 const unsigned SmallBufferSize = 64;
84 unsigned SmallBuffer[SmallBufferSize];
85 unsigned *Allocated = 0;
86 unsigned *previous = SmallBuffer;
87 if (2*(n + 1) > SmallBufferSize)
88 Allocated = previous = new unsigned [2*(n+1)];
89 unsigned *current = previous + (n + 1);
91 for (unsigned i = 0; i <= n; ++i)
94 for (size_type y = 1; y <= m; ++y) {
96 for (size_type x = 1; x <= n; ++x) {
97 if (AllowReplacements) {
98 current[x] = min(previous[x-1] + ((*this)[y-1] == Other[x-1]? 0u:1u),
99 min(current[x-1], previous[x])+1);
102 if ((*this)[y-1] == Other[x-1]) current[x] = previous[x-1];
103 else current[x] = min(current[x-1], previous[x]) + 1;
107 unsigned *tmp = current;
112 unsigned Result = previous[n];
118 //===----------------------------------------------------------------------===//
120 //===----------------------------------------------------------------------===//
123 /// find - Search for the first string \arg Str in the string.
125 /// \return - The index of the first occurence of \arg Str, or npos if not
127 size_t StringRef::find(StringRef Str, size_t From) const {
128 size_t N = Str.size();
131 for (size_t e = Length - N + 1, i = min(From, e); i != e; ++i)
132 if (substr(i, N).equals(Str))
137 /// rfind - Search for the last string \arg Str in the string.
139 /// \return - The index of the last occurence of \arg Str, or npos if not
141 size_t StringRef::rfind(StringRef Str) const {
142 size_t N = Str.size();
145 for (size_t i = Length - N + 1, e = 0; i != e;) {
147 if (substr(i, N).equals(Str))
153 /// find_first_of - Find the first character in the string that is in \arg
154 /// Chars, or npos if not found.
156 /// Note: O(size() * Chars.size())
157 StringRef::size_type StringRef::find_first_of(StringRef Chars,
159 for (size_type i = min(From, Length), e = Length; i != e; ++i)
160 if (Chars.find(Data[i]) != npos)
165 /// find_first_not_of - Find the first character in the string that is not
166 /// \arg C or npos if not found.
167 StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
168 for (size_type i = min(From, Length), e = Length; i != e; ++i)
174 /// find_first_not_of - Find the first character in the string that is not
175 /// in the string \arg Chars, or npos if not found.
177 /// Note: O(size() * Chars.size())
178 StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
180 for (size_type i = min(From, Length), e = Length; i != e; ++i)
181 if (Chars.find(Data[i]) == npos)
187 //===----------------------------------------------------------------------===//
188 // Helpful Algorithms
189 //===----------------------------------------------------------------------===//
191 /// count - Return the number of non-overlapped occurrences of \arg Str in
193 size_t StringRef::count(StringRef Str) const {
195 size_t N = Str.size();
198 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
199 if (substr(i, N).equals(Str))
204 static unsigned GetAutoSenseRadix(StringRef &Str) {
205 if (Str.startswith("0x")) {
208 } else if (Str.startswith("0b")) {
211 } else if (Str.startswith("0")) {
219 /// GetAsUnsignedInteger - Workhorse method that converts a integer character
220 /// sequence of radix up to 36 to an unsigned long long value.
221 static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,
222 unsigned long long &Result) {
223 // Autosense radix if not specified.
225 Radix = GetAutoSenseRadix(Str);
227 // Empty strings (after the radix autosense) are invalid.
228 if (Str.empty()) return true;
230 // Parse all the bytes of the string given this radix. Watch for overflow.
232 while (!Str.empty()) {
234 if (Str[0] >= '0' && Str[0] <= '9')
235 CharVal = Str[0]-'0';
236 else if (Str[0] >= 'a' && Str[0] <= 'z')
237 CharVal = Str[0]-'a'+10;
238 else if (Str[0] >= 'A' && Str[0] <= 'Z')
239 CharVal = Str[0]-'A'+10;
243 // If the parsed value is larger than the integer radix, the string is
245 if (CharVal >= Radix)
248 // Add in this character.
249 unsigned long long PrevResult = Result;
250 Result = Result*Radix+CharVal;
252 // Check for overflow.
253 if (Result < PrevResult)
262 bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
263 return GetAsUnsignedInteger(*this, Radix, Result);
267 bool StringRef::getAsInteger(unsigned Radix, long long &Result) const {
268 unsigned long long ULLVal;
270 // Handle positive strings first.
271 if (empty() || front() != '-') {
272 if (GetAsUnsignedInteger(*this, Radix, ULLVal) ||
273 // Check for value so large it overflows a signed value.
274 (long long)ULLVal < 0)
280 // Get the positive part of the value.
281 if (GetAsUnsignedInteger(substr(1), Radix, ULLVal) ||
282 // Reject values so large they'd overflow as negative signed, but allow
283 // "-0". This negates the unsigned so that the negative isn't undefined
284 // on signed overflow.
285 (long long)-ULLVal > 0)
292 bool StringRef::getAsInteger(unsigned Radix, int &Result) const {
294 if (getAsInteger(Radix, Val) ||
301 bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const {
302 unsigned long long Val;
303 if (getAsInteger(Radix, Val) ||
304 (unsigned)Val != Val)
310 bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const {
311 StringRef Str = *this;
313 // Autosense radix if not specified.
315 Radix = GetAutoSenseRadix(Str);
317 assert(Radix > 1 && Radix <= 36);
319 // Empty strings (after the radix autosense) are invalid.
320 if (Str.empty()) return true;
322 // Skip leading zeroes. This can be a significant improvement if
323 // it means we don't need > 64 bits.
324 while (!Str.empty() && Str.front() == '0')
327 // If it was nothing but zeroes....
329 Result = APInt(64, 0);
333 // (Over-)estimate the required number of bits.
334 unsigned Log2Radix = 0;
335 while ((1U << Log2Radix) < Radix) Log2Radix++;
336 bool IsPowerOf2Radix = ((1U << Log2Radix) == Radix);
338 unsigned BitWidth = Log2Radix * Str.size();
339 if (BitWidth < Result.getBitWidth())
340 BitWidth = Result.getBitWidth(); // don't shrink the result
342 Result.zext(BitWidth);
344 APInt RadixAP, CharAP; // unused unless !IsPowerOf2Radix
345 if (!IsPowerOf2Radix) {
346 // These must have the same bit-width as Result.
347 RadixAP = APInt(BitWidth, Radix);
348 CharAP = APInt(BitWidth, 0);
351 // Parse all the bytes of the string given this radix.
353 while (!Str.empty()) {
355 if (Str[0] >= '0' && Str[0] <= '9')
356 CharVal = Str[0]-'0';
357 else if (Str[0] >= 'a' && Str[0] <= 'z')
358 CharVal = Str[0]-'a'+10;
359 else if (Str[0] >= 'A' && Str[0] <= 'Z')
360 CharVal = Str[0]-'A'+10;
364 // If the parsed value is larger than the integer radix, the string is
366 if (CharVal >= Radix)
369 // Add in this character.
370 if (IsPowerOf2Radix) {
371 Result <<= Log2Radix;