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"
12 #include "llvm/ADT/Hashing.h"
13 #include "llvm/ADT/OwningPtr.h"
14 #include "llvm/ADT/edit_distance.h"
19 // MSVC emits references to this into the translation units which reference it.
21 const size_t StringRef::npos;
24 static char ascii_tolower(char x) {
25 if (x >= 'A' && x <= 'Z')
30 static char ascii_toupper(char x) {
31 if (x >= 'a' && x <= 'z')
36 static bool ascii_isdigit(char x) {
37 return x >= '0' && x <= '9';
40 // strncasecmp() is not available on non-POSIX systems, so define an
41 // alternative function here.
42 static int ascii_strncasecmp(const char *LHS, const char *RHS, size_t Length) {
43 for (size_t I = 0; I < Length; ++I) {
44 unsigned char LHC = ascii_tolower(LHS[I]);
45 unsigned char RHC = ascii_tolower(RHS[I]);
47 return LHC < RHC ? -1 : 1;
52 /// compare_lower - Compare strings, ignoring case.
53 int StringRef::compare_lower(StringRef RHS) const {
54 if (int Res = ascii_strncasecmp(Data, RHS.Data, min(Length, RHS.Length)))
56 if (Length == RHS.Length)
58 return Length < RHS.Length ? -1 : 1;
61 /// Check if this string starts with the given \p Prefix, ignoring case.
62 bool StringRef::startswith_lower(StringRef Prefix) const {
63 return Length >= Prefix.Length &&
64 ascii_strncasecmp(Data, Prefix.Data, Prefix.Length) == 0;
67 /// Check if this string ends with the given \p Suffix, ignoring case.
68 bool StringRef::endswith_lower(StringRef Suffix) const {
69 return Length >= Suffix.Length &&
70 ascii_strncasecmp(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
73 /// compare_numeric - Compare strings, handle embedded numbers.
74 int StringRef::compare_numeric(StringRef RHS) const {
75 for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
76 // Check for sequences of digits.
77 if (ascii_isdigit(Data[I]) && ascii_isdigit(RHS.Data[I])) {
78 // The longer sequence of numbers is considered larger.
79 // This doesn't really handle prefixed zeros well.
81 for (J = I + 1; J != E + 1; ++J) {
82 bool ld = J < Length && ascii_isdigit(Data[J]);
83 bool rd = J < RHS.Length && ascii_isdigit(RHS.Data[J]);
89 // The two number sequences have the same length (J-I), just memcmp them.
90 if (int Res = compareMemory(Data + I, RHS.Data + I, J - I))
91 return Res < 0 ? -1 : 1;
92 // Identical number sequences, continue search after the numbers.
96 if (Data[I] != RHS.Data[I])
97 return (unsigned char)Data[I] < (unsigned char)RHS.Data[I] ? -1 : 1;
99 if (Length == RHS.Length)
101 return Length < RHS.Length ? -1 : 1;
104 // Compute the edit distance between the two given strings.
105 unsigned StringRef::edit_distance(llvm::StringRef Other,
106 bool AllowReplacements,
107 unsigned MaxEditDistance) const {
108 return llvm::ComputeEditDistance(
109 llvm::ArrayRef<char>(data(), size()),
110 llvm::ArrayRef<char>(Other.data(), Other.size()),
111 AllowReplacements, MaxEditDistance);
114 //===----------------------------------------------------------------------===//
116 //===----------------------------------------------------------------------===//
118 std::string StringRef::lower() const {
119 std::string Result(size(), char());
120 for (size_type i = 0, e = size(); i != e; ++i) {
121 Result[i] = ascii_tolower(Data[i]);
126 std::string StringRef::upper() const {
127 std::string Result(size(), char());
128 for (size_type i = 0, e = size(); i != e; ++i) {
129 Result[i] = ascii_toupper(Data[i]);
134 //===----------------------------------------------------------------------===//
136 //===----------------------------------------------------------------------===//
139 /// find - Search for the first string \arg Str in the string.
141 /// \return - The index of the first occurrence of \arg Str, or npos if not
143 size_t StringRef::find(StringRef Str, size_t From) const {
144 size_t N = Str.size();
148 // For short haystacks or unsupported needles fall back to the naive algorithm
149 if (Length < 16 || N > 255 || N == 0) {
150 for (size_t e = Length - N + 1, i = min(From, e); i != e; ++i)
151 if (substr(i, N).equals(Str))
159 // Build the bad char heuristic table, with uint8_t to reduce cache thrashing.
160 uint8_t BadCharSkip[256];
161 std::memset(BadCharSkip, N, 256);
162 for (unsigned i = 0; i != N-1; ++i)
163 BadCharSkip[(uint8_t)Str[i]] = N-1-i;
165 unsigned Len = Length-From, Pos = From;
167 if (substr(Pos, N).equals(Str)) // See if this is the correct substring.
170 // Otherwise skip the appropriate number of bytes.
171 uint8_t Skip = BadCharSkip[(uint8_t)(*this)[Pos+N-1]];
179 /// rfind - Search for the last string \arg Str in the string.
181 /// \return - The index of the last occurrence of \arg Str, or npos if not
183 size_t StringRef::rfind(StringRef Str) const {
184 size_t N = Str.size();
187 for (size_t i = Length - N + 1, e = 0; i != e;) {
189 if (substr(i, N).equals(Str))
195 /// find_first_of - Find the first character in the string that is in \arg
196 /// Chars, or npos if not found.
198 /// Note: O(size() + Chars.size())
199 StringRef::size_type StringRef::find_first_of(StringRef Chars,
201 std::bitset<1 << CHAR_BIT> CharBits;
202 for (size_type i = 0; i != Chars.size(); ++i)
203 CharBits.set((unsigned char)Chars[i]);
205 for (size_type i = min(From, Length), e = Length; i != e; ++i)
206 if (CharBits.test((unsigned char)Data[i]))
211 /// find_first_not_of - Find the first character in the string that is not
212 /// \arg C or npos if not found.
213 StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
214 for (size_type i = min(From, Length), e = Length; i != e; ++i)
220 /// find_first_not_of - Find the first character in the string that is not
221 /// in the string \arg Chars, or npos if not found.
223 /// Note: O(size() + Chars.size())
224 StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
226 std::bitset<1 << CHAR_BIT> CharBits;
227 for (size_type i = 0; i != Chars.size(); ++i)
228 CharBits.set((unsigned char)Chars[i]);
230 for (size_type i = min(From, Length), e = Length; i != e; ++i)
231 if (!CharBits.test((unsigned char)Data[i]))
236 /// find_last_of - Find the last character in the string that is in \arg C,
237 /// or npos if not found.
239 /// Note: O(size() + Chars.size())
240 StringRef::size_type StringRef::find_last_of(StringRef Chars,
242 std::bitset<1 << CHAR_BIT> CharBits;
243 for (size_type i = 0; i != Chars.size(); ++i)
244 CharBits.set((unsigned char)Chars[i]);
246 for (size_type i = min(From, Length) - 1, e = -1; i != e; --i)
247 if (CharBits.test((unsigned char)Data[i]))
252 /// find_last_not_of - Find the last character in the string that is not
253 /// \arg C, or npos if not found.
254 StringRef::size_type StringRef::find_last_not_of(char C, size_t From) const {
255 for (size_type i = min(From, Length) - 1, e = -1; i != e; --i)
261 /// find_last_not_of - Find the last character in the string that is not in
262 /// \arg Chars, or npos if not found.
264 /// Note: O(size() + Chars.size())
265 StringRef::size_type StringRef::find_last_not_of(StringRef Chars,
267 std::bitset<1 << CHAR_BIT> CharBits;
268 for (size_type i = 0, e = Chars.size(); i != e; ++i)
269 CharBits.set((unsigned char)Chars[i]);
271 for (size_type i = min(From, Length) - 1, e = -1; i != e; --i)
272 if (!CharBits.test((unsigned char)Data[i]))
277 void StringRef::split(SmallVectorImpl<StringRef> &A,
278 StringRef Separators, int MaxSplit,
279 bool KeepEmpty) const {
280 StringRef rest = *this;
282 // rest.data() is used to distinguish cases like "a," that splits into
283 // "a" + "" and "a" that splits into "a" + 0.
285 rest.data() != NULL && (MaxSplit < 0 || splits < MaxSplit);
287 std::pair<StringRef, StringRef> p = rest.split(Separators);
289 if (KeepEmpty || p.first.size() != 0)
290 A.push_back(p.first);
293 // If we have a tail left, add it.
294 if (rest.data() != NULL && (rest.size() != 0 || KeepEmpty))
298 //===----------------------------------------------------------------------===//
299 // Helpful Algorithms
300 //===----------------------------------------------------------------------===//
302 /// count - Return the number of non-overlapped occurrences of \arg Str in
304 size_t StringRef::count(StringRef Str) const {
306 size_t N = Str.size();
309 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
310 if (substr(i, N).equals(Str))
315 static unsigned GetAutoSenseRadix(StringRef &Str) {
316 if (Str.startswith("0x")) {
321 if (Str.startswith("0b")) {
326 if (Str.startswith("0o")) {
331 if (Str.startswith("0"))
338 /// GetAsUnsignedInteger - Workhorse method that converts a integer character
339 /// sequence of radix up to 36 to an unsigned long long value.
340 bool llvm::getAsUnsignedInteger(StringRef Str, unsigned Radix,
341 unsigned long long &Result) {
342 // Autosense radix if not specified.
344 Radix = GetAutoSenseRadix(Str);
346 // Empty strings (after the radix autosense) are invalid.
347 if (Str.empty()) return true;
349 // Parse all the bytes of the string given this radix. Watch for overflow.
351 while (!Str.empty()) {
353 if (Str[0] >= '0' && Str[0] <= '9')
354 CharVal = Str[0]-'0';
355 else if (Str[0] >= 'a' && Str[0] <= 'z')
356 CharVal = Str[0]-'a'+10;
357 else if (Str[0] >= 'A' && Str[0] <= 'Z')
358 CharVal = Str[0]-'A'+10;
362 // If the parsed value is larger than the integer radix, the string is
364 if (CharVal >= Radix)
367 // Add in this character.
368 unsigned long long PrevResult = Result;
369 Result = Result*Radix+CharVal;
371 // Check for overflow by shifting back and seeing if bits were lost.
372 if (Result/Radix < PrevResult)
381 bool llvm::getAsSignedInteger(StringRef Str, unsigned Radix,
383 unsigned long long ULLVal;
385 // Handle positive strings first.
386 if (Str.empty() || Str.front() != '-') {
387 if (getAsUnsignedInteger(Str, Radix, ULLVal) ||
388 // Check for value so large it overflows a signed value.
389 (long long)ULLVal < 0)
395 // Get the positive part of the value.
396 if (getAsUnsignedInteger(Str.substr(1), Radix, ULLVal) ||
397 // Reject values so large they'd overflow as negative signed, but allow
398 // "-0". This negates the unsigned so that the negative isn't undefined
399 // on signed overflow.
400 (long long)-ULLVal > 0)
407 bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const {
408 StringRef Str = *this;
410 // Autosense radix if not specified.
412 Radix = GetAutoSenseRadix(Str);
414 assert(Radix > 1 && Radix <= 36);
416 // Empty strings (after the radix autosense) are invalid.
417 if (Str.empty()) return true;
419 // Skip leading zeroes. This can be a significant improvement if
420 // it means we don't need > 64 bits.
421 while (!Str.empty() && Str.front() == '0')
424 // If it was nothing but zeroes....
426 Result = APInt(64, 0);
430 // (Over-)estimate the required number of bits.
431 unsigned Log2Radix = 0;
432 while ((1U << Log2Radix) < Radix) Log2Radix++;
433 bool IsPowerOf2Radix = ((1U << Log2Radix) == Radix);
435 unsigned BitWidth = Log2Radix * Str.size();
436 if (BitWidth < Result.getBitWidth())
437 BitWidth = Result.getBitWidth(); // don't shrink the result
438 else if (BitWidth > Result.getBitWidth())
439 Result = Result.zext(BitWidth);
441 APInt RadixAP, CharAP; // unused unless !IsPowerOf2Radix
442 if (!IsPowerOf2Radix) {
443 // These must have the same bit-width as Result.
444 RadixAP = APInt(BitWidth, Radix);
445 CharAP = APInt(BitWidth, 0);
448 // Parse all the bytes of the string given this radix.
450 while (!Str.empty()) {
452 if (Str[0] >= '0' && Str[0] <= '9')
453 CharVal = Str[0]-'0';
454 else if (Str[0] >= 'a' && Str[0] <= 'z')
455 CharVal = Str[0]-'a'+10;
456 else if (Str[0] >= 'A' && Str[0] <= 'Z')
457 CharVal = Str[0]-'A'+10;
461 // If the parsed value is larger than the integer radix, the string is
463 if (CharVal >= Radix)
466 // Add in this character.
467 if (IsPowerOf2Radix) {
468 Result <<= Log2Radix;
483 // Implementation of StringRef hashing.
484 hash_code llvm::hash_value(StringRef S) {
485 return hash_combine_range(S.begin(), S.end());