blob: d5fd12727dbc04300af88edbbfe60d3a461c7054 [file] [log] [blame]
Daniel Dunbare6551282009-09-16 22:38:48 +00001//===-- 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
10#include "llvm/ADT/StringRef.h"
John McCall1e7ad392010-02-28 09:55:58 +000011#include "llvm/ADT/APInt.h"
Ted Kremenek13302ec2010-11-07 06:09:02 +000012#include "llvm/ADT/OwningPtr.h"
Benjamin Kramer250eb002010-08-23 18:16:08 +000013#include <bitset>
Douglas Gregorad6b6da2010-01-07 00:51:54 +000014
Daniel Dunbare6551282009-09-16 22:38:48 +000015using namespace llvm;
16
Daniel Dunbar77696be2009-09-22 03:34:40 +000017// MSVC emits references to this into the translation units which reference it.
18#ifndef _MSC_VER
Daniel Dunbare6551282009-09-16 22:38:48 +000019const size_t StringRef::npos;
Daniel Dunbar77696be2009-09-22 03:34:40 +000020#endif
Chris Lattnercea14382009-09-19 19:47:14 +000021
Benjamin Kramer05872ea2009-11-12 20:36:59 +000022static char ascii_tolower(char x) {
23 if (x >= 'A' && x <= 'Z')
24 return x - 'A' + 'a';
25 return x;
26}
27
Jakob Stoklund Olesen160a3bf2010-05-26 21:47:28 +000028static bool ascii_isdigit(char x) {
29 return x >= '0' && x <= '9';
30}
31
Benjamin Kramer05872ea2009-11-12 20:36:59 +000032/// compare_lower - Compare strings, ignoring case.
33int StringRef::compare_lower(StringRef RHS) const {
Daniel Dunbar58ce7ac2009-11-19 18:53:18 +000034 for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
Benjamin Kramer0043e352010-08-26 14:21:08 +000035 unsigned char LHC = ascii_tolower(Data[I]);
36 unsigned char RHC = ascii_tolower(RHS.Data[I]);
Benjamin Kramer05872ea2009-11-12 20:36:59 +000037 if (LHC != RHC)
38 return LHC < RHC ? -1 : 1;
39 }
40
41 if (Length == RHS.Length)
Benjamin Kramer0043e352010-08-26 14:21:08 +000042 return 0;
Benjamin Kramer05872ea2009-11-12 20:36:59 +000043 return Length < RHS.Length ? -1 : 1;
44}
45
Jakob Stoklund Olesen160a3bf2010-05-26 21:47:28 +000046/// compare_numeric - Compare strings, handle embedded numbers.
47int StringRef::compare_numeric(StringRef RHS) const {
48 for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
49 if (Data[I] == RHS.Data[I])
50 continue;
51 if (ascii_isdigit(Data[I]) && ascii_isdigit(RHS.Data[I])) {
52 // The longer sequence of numbers is larger. This doesn't really handle
53 // prefixed zeros well.
54 for (size_t J = I+1; J != E+1; ++J) {
55 bool ld = J < Length && ascii_isdigit(Data[J]);
56 bool rd = J < RHS.Length && ascii_isdigit(RHS.Data[J]);
57 if (ld != rd)
58 return rd ? -1 : 1;
59 if (!rd)
60 break;
61 }
62 }
Benjamin Kramer837bccd2010-08-26 15:25:35 +000063 return (unsigned char)Data[I] < (unsigned char)RHS.Data[I] ? -1 : 1;
Jakob Stoklund Olesen160a3bf2010-05-26 21:47:28 +000064 }
65 if (Length == RHS.Length)
Benjamin Kramer0043e352010-08-26 14:21:08 +000066 return 0;
Jakob Stoklund Olesen160a3bf2010-05-26 21:47:28 +000067 return Length < RHS.Length ? -1 : 1;
68}
69
Douglas Gregor7e54d5b2009-12-31 04:24:34 +000070// Compute the edit distance between the two given strings.
Douglas Gregor441c8b42009-12-30 17:23:44 +000071unsigned StringRef::edit_distance(llvm::StringRef Other,
Douglas Gregor5ee568a2010-10-19 22:13:48 +000072 bool AllowReplacements,
73 unsigned MaxEditDistance) {
Douglas Gregor7e54d5b2009-12-31 04:24:34 +000074 // The algorithm implemented below is the "classic"
75 // dynamic-programming algorithm for computing the Levenshtein
76 // distance, which is described here:
77 //
78 // http://en.wikipedia.org/wiki/Levenshtein_distance
79 //
80 // Although the algorithm is typically described using an m x n
81 // array, only two rows are used at a time, so this implemenation
82 // just keeps two separate vectors for those two rows.
Douglas Gregor441c8b42009-12-30 17:23:44 +000083 size_type m = size();
84 size_type n = Other.size();
85
Douglas Gregor2772ea82010-01-07 02:24:06 +000086 const unsigned SmallBufferSize = 64;
87 unsigned SmallBuffer[SmallBufferSize];
Ted Kremenek13302ec2010-11-07 06:09:02 +000088 llvm::OwningArrayPtr<unsigned> Allocated;
Douglas Gregor2772ea82010-01-07 02:24:06 +000089 unsigned *previous = SmallBuffer;
Ted Kremenek13302ec2010-11-07 06:09:02 +000090 if (2*(n + 1) > SmallBufferSize) {
91 previous = new unsigned [2*(n+1)];
92 Allocated.reset(previous);
93 }
Douglas Gregor2772ea82010-01-07 02:24:06 +000094 unsigned *current = previous + (n + 1);
Douglas Gregorad6b6da2010-01-07 00:51:54 +000095
96 for (unsigned i = 0; i <= n; ++i)
Douglas Gregor441c8b42009-12-30 17:23:44 +000097 previous[i] = i;
98
Douglas Gregor441c8b42009-12-30 17:23:44 +000099 for (size_type y = 1; y <= m; ++y) {
Douglas Gregor441c8b42009-12-30 17:23:44 +0000100 current[0] = y;
Douglas Gregor5ee568a2010-10-19 22:13:48 +0000101 unsigned BestThisRow = current[0];
102
Douglas Gregor441c8b42009-12-30 17:23:44 +0000103 for (size_type x = 1; x <= n; ++x) {
104 if (AllowReplacements) {
105 current[x] = min(previous[x-1] + ((*this)[y-1] == Other[x-1]? 0u:1u),
106 min(current[x-1], previous[x])+1);
107 }
108 else {
109 if ((*this)[y-1] == Other[x-1]) current[x] = previous[x-1];
110 else current[x] = min(current[x-1], previous[x]) + 1;
111 }
Douglas Gregor5ee568a2010-10-19 22:13:48 +0000112 BestThisRow = min(BestThisRow, current[x]);
Douglas Gregor441c8b42009-12-30 17:23:44 +0000113 }
Douglas Gregorad6b6da2010-01-07 00:51:54 +0000114
Douglas Gregor5ee568a2010-10-19 22:13:48 +0000115 if (MaxEditDistance && BestThisRow > MaxEditDistance)
116 return MaxEditDistance + 1;
117
Douglas Gregorad6b6da2010-01-07 00:51:54 +0000118 unsigned *tmp = current;
119 current = previous;
120 previous = tmp;
Douglas Gregor441c8b42009-12-30 17:23:44 +0000121 }
122
Douglas Gregorad6b6da2010-01-07 00:51:54 +0000123 unsigned Result = previous[n];
Douglas Gregorad6b6da2010-01-07 00:51:54 +0000124 return Result;
Douglas Gregor441c8b42009-12-30 17:23:44 +0000125}
126
Chris Lattner05a32c82009-09-20 01:22:16 +0000127//===----------------------------------------------------------------------===//
128// String Searching
129//===----------------------------------------------------------------------===//
130
131
132/// find - Search for the first string \arg Str in the string.
133///
134/// \return - The index of the first occurence of \arg Str, or npos if not
135/// found.
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000136size_t StringRef::find(StringRef Str, size_t From) const {
Chris Lattner05a32c82009-09-20 01:22:16 +0000137 size_t N = Str.size();
138 if (N > Length)
139 return npos;
Daniel Dunbar58ce7ac2009-11-19 18:53:18 +0000140 for (size_t e = Length - N + 1, i = min(From, e); i != e; ++i)
Chris Lattner05a32c82009-09-20 01:22:16 +0000141 if (substr(i, N).equals(Str))
142 return i;
143 return npos;
144}
145
146/// rfind - Search for the last string \arg Str in the string.
147///
148/// \return - The index of the last occurence of \arg Str, or npos if not
149/// found.
Daniel Dunbar2928c832009-11-06 10:58:06 +0000150size_t StringRef::rfind(StringRef Str) const {
Chris Lattner05a32c82009-09-20 01:22:16 +0000151 size_t N = Str.size();
152 if (N > Length)
153 return npos;
154 for (size_t i = Length - N + 1, e = 0; i != e;) {
155 --i;
156 if (substr(i, N).equals(Str))
157 return i;
158 }
159 return npos;
160}
161
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000162/// find_first_of - Find the first character in the string that is in \arg
163/// Chars, or npos if not found.
164///
Benjamin Kramer250eb002010-08-23 18:16:08 +0000165/// Note: O(size() + Chars.size())
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000166StringRef::size_type StringRef::find_first_of(StringRef Chars,
167 size_t From) const {
Benjamin Kramer250eb002010-08-23 18:16:08 +0000168 std::bitset<1 << CHAR_BIT> CharBits;
169 for (size_type i = 0; i != Chars.size(); ++i)
170 CharBits.set((unsigned char)Chars[i]);
171
Daniel Dunbar58ce7ac2009-11-19 18:53:18 +0000172 for (size_type i = min(From, Length), e = Length; i != e; ++i)
Benjamin Kramer250eb002010-08-23 18:16:08 +0000173 if (CharBits.test((unsigned char)Data[i]))
Chris Lattner05a32c82009-09-20 01:22:16 +0000174 return i;
175 return npos;
176}
177
178/// find_first_not_of - Find the first character in the string that is not
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000179/// \arg C or npos if not found.
180StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
Daniel Dunbar58ce7ac2009-11-19 18:53:18 +0000181 for (size_type i = min(From, Length), e = Length; i != e; ++i)
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000182 if (Data[i] != C)
183 return i;
184 return npos;
185}
186
187/// find_first_not_of - Find the first character in the string that is not
188/// in the string \arg Chars, or npos if not found.
189///
Benjamin Kramer250eb002010-08-23 18:16:08 +0000190/// Note: O(size() + Chars.size())
Daniel Dunbar64066bd2009-11-11 00:28:53 +0000191StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
192 size_t From) const {
Benjamin Kramer250eb002010-08-23 18:16:08 +0000193 std::bitset<1 << CHAR_BIT> CharBits;
194 for (size_type i = 0; i != Chars.size(); ++i)
195 CharBits.set((unsigned char)Chars[i]);
196
Daniel Dunbar58ce7ac2009-11-19 18:53:18 +0000197 for (size_type i = min(From, Length), e = Length; i != e; ++i)
Benjamin Kramer250eb002010-08-23 18:16:08 +0000198 if (!CharBits.test((unsigned char)Data[i]))
Chris Lattner05a32c82009-09-20 01:22:16 +0000199 return i;
200 return npos;
201}
202
203
204//===----------------------------------------------------------------------===//
205// Helpful Algorithms
206//===----------------------------------------------------------------------===//
207
208/// count - Return the number of non-overlapped occurrences of \arg Str in
209/// the string.
Daniel Dunbar2928c832009-11-06 10:58:06 +0000210size_t StringRef::count(StringRef Str) const {
Chris Lattner05a32c82009-09-20 01:22:16 +0000211 size_t Count = 0;
212 size_t N = Str.size();
213 if (N > Length)
214 return 0;
215 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
216 if (substr(i, N).equals(Str))
217 ++Count;
218 return Count;
219}
220
John McCall1e7ad392010-02-28 09:55:58 +0000221static unsigned GetAutoSenseRadix(StringRef &Str) {
222 if (Str.startswith("0x")) {
223 Str = Str.substr(2);
224 return 16;
225 } else if (Str.startswith("0b")) {
226 Str = Str.substr(2);
227 return 2;
228 } else if (Str.startswith("0")) {
229 return 8;
230 } else {
231 return 10;
232 }
233}
234
235
Chris Lattner63c6b7d2009-09-19 23:58:48 +0000236/// GetAsUnsignedInteger - Workhorse method that converts a integer character
237/// sequence of radix up to 36 to an unsigned long long value.
Chris Lattnercea14382009-09-19 19:47:14 +0000238static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,
239 unsigned long long &Result) {
240 // Autosense radix if not specified.
John McCall1e7ad392010-02-28 09:55:58 +0000241 if (Radix == 0)
242 Radix = GetAutoSenseRadix(Str);
Chris Lattnercea14382009-09-19 19:47:14 +0000243
244 // Empty strings (after the radix autosense) are invalid.
245 if (Str.empty()) return true;
246
247 // Parse all the bytes of the string given this radix. Watch for overflow.
248 Result = 0;
249 while (!Str.empty()) {
250 unsigned CharVal;
251 if (Str[0] >= '0' && Str[0] <= '9')
252 CharVal = Str[0]-'0';
253 else if (Str[0] >= 'a' && Str[0] <= 'z')
254 CharVal = Str[0]-'a'+10;
255 else if (Str[0] >= 'A' && Str[0] <= 'Z')
256 CharVal = Str[0]-'A'+10;
257 else
258 return true;
259
260 // If the parsed value is larger than the integer radix, the string is
261 // invalid.
262 if (CharVal >= Radix)
263 return true;
264
265 // Add in this character.
266 unsigned long long PrevResult = Result;
267 Result = Result*Radix+CharVal;
268
269 // Check for overflow.
270 if (Result < PrevResult)
271 return true;
272
273 Str = Str.substr(1);
274 }
275
276 return false;
277}
278
279bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
280 return GetAsUnsignedInteger(*this, Radix, Result);
281}
282
Chris Lattner63c6b7d2009-09-19 23:58:48 +0000283
284bool StringRef::getAsInteger(unsigned Radix, long long &Result) const {
285 unsigned long long ULLVal;
286
287 // Handle positive strings first.
288 if (empty() || front() != '-') {
289 if (GetAsUnsignedInteger(*this, Radix, ULLVal) ||
290 // Check for value so large it overflows a signed value.
291 (long long)ULLVal < 0)
292 return true;
293 Result = ULLVal;
294 return false;
295 }
296
297 // Get the positive part of the value.
298 if (GetAsUnsignedInteger(substr(1), Radix, ULLVal) ||
299 // Reject values so large they'd overflow as negative signed, but allow
300 // "-0". This negates the unsigned so that the negative isn't undefined
301 // on signed overflow.
302 (long long)-ULLVal > 0)
303 return true;
304
305 Result = -ULLVal;
306 return false;
307}
308
309bool StringRef::getAsInteger(unsigned Radix, int &Result) const {
310 long long Val;
311 if (getAsInteger(Radix, Val) ||
312 (int)Val != Val)
313 return true;
314 Result = Val;
315 return false;
316}
317
318bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const {
319 unsigned long long Val;
320 if (getAsInteger(Radix, Val) ||
321 (unsigned)Val != Val)
322 return true;
323 Result = Val;
324 return false;
325}
John McCall1e7ad392010-02-28 09:55:58 +0000326
327bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const {
328 StringRef Str = *this;
329
330 // Autosense radix if not specified.
331 if (Radix == 0)
332 Radix = GetAutoSenseRadix(Str);
333
334 assert(Radix > 1 && Radix <= 36);
335
336 // Empty strings (after the radix autosense) are invalid.
337 if (Str.empty()) return true;
338
339 // Skip leading zeroes. This can be a significant improvement if
340 // it means we don't need > 64 bits.
341 while (!Str.empty() && Str.front() == '0')
342 Str = Str.substr(1);
343
344 // If it was nothing but zeroes....
345 if (Str.empty()) {
346 Result = APInt(64, 0);
347 return false;
348 }
349
350 // (Over-)estimate the required number of bits.
351 unsigned Log2Radix = 0;
352 while ((1U << Log2Radix) < Radix) Log2Radix++;
353 bool IsPowerOf2Radix = ((1U << Log2Radix) == Radix);
354
355 unsigned BitWidth = Log2Radix * Str.size();
356 if (BitWidth < Result.getBitWidth())
357 BitWidth = Result.getBitWidth(); // don't shrink the result
358 else
359 Result.zext(BitWidth);
360
361 APInt RadixAP, CharAP; // unused unless !IsPowerOf2Radix
362 if (!IsPowerOf2Radix) {
363 // These must have the same bit-width as Result.
364 RadixAP = APInt(BitWidth, Radix);
365 CharAP = APInt(BitWidth, 0);
366 }
367
368 // Parse all the bytes of the string given this radix.
369 Result = 0;
370 while (!Str.empty()) {
371 unsigned CharVal;
372 if (Str[0] >= '0' && Str[0] <= '9')
373 CharVal = Str[0]-'0';
374 else if (Str[0] >= 'a' && Str[0] <= 'z')
375 CharVal = Str[0]-'a'+10;
376 else if (Str[0] >= 'A' && Str[0] <= 'Z')
377 CharVal = Str[0]-'A'+10;
378 else
379 return true;
380
381 // If the parsed value is larger than the integer radix, the string is
382 // invalid.
383 if (CharVal >= Radix)
384 return true;
385
386 // Add in this character.
387 if (IsPowerOf2Radix) {
388 Result <<= Log2Radix;
389 Result |= CharVal;
390 } else {
391 Result *= RadixAP;
392 CharAP = CharVal;
393 Result += CharAP;
394 }
395
396 Str = Str.substr(1);
397 }
398
399 return false;
400}