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/edit_distance.h"
18 // MSVC emits references to this into the translation units which reference it.
20 const size_t StringRef::npos;
23 static char ascii_tolower(char x) {
24 if (x >= 'A' && x <= 'Z')
29 static char ascii_toupper(char x) {
30 if (x >= 'a' && x <= 'z')
35 static bool ascii_isdigit(char x) {
36 return x >= '0' && x <= '9';
39 // strncasecmp() is not available on non-POSIX systems, so define an
40 // alternative function here.
41 static int ascii_strncasecmp(const char *LHS, const char *RHS, size_t Length) {
42 for (size_t I = 0; I < Length; ++I) {
43 unsigned char LHC = ascii_tolower(LHS[I]);
44 unsigned char RHC = ascii_tolower(RHS[I]);
46 return LHC < RHC ? -1 : 1;
51 /// compare_lower - Compare strings, ignoring case.
52 int StringRef::compare_lower(StringRef RHS) const {
53 if (int Res = ascii_strncasecmp(Data, RHS.Data, std::min(Length, RHS.Length)))
55 if (Length == RHS.Length)
57 return Length < RHS.Length ? -1 : 1;
60 /// Check if this string starts with the given \p Prefix, ignoring case.
61 bool StringRef::startswith_lower(StringRef Prefix) const {
62 return Length >= Prefix.Length &&
63 ascii_strncasecmp(Data, Prefix.Data, Prefix.Length) == 0;
66 /// Check if this string ends with the given \p Suffix, ignoring case.
67 bool StringRef::endswith_lower(StringRef Suffix) const {
68 return Length >= Suffix.Length &&
69 ascii_strncasecmp(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
72 /// compare_numeric - Compare strings, handle embedded numbers.
73 int StringRef::compare_numeric(StringRef RHS) const {
74 for (size_t I = 0, E = std::min(Length, RHS.Length); I != E; ++I) {
75 // Check for sequences of digits.
76 if (ascii_isdigit(Data[I]) && ascii_isdigit(RHS.Data[I])) {
77 // The longer sequence of numbers is considered larger.
78 // This doesn't really handle prefixed zeros well.
80 for (J = I + 1; J != E + 1; ++J) {
81 bool ld = J < Length && ascii_isdigit(Data[J]);
82 bool rd = J < RHS.Length && ascii_isdigit(RHS.Data[J]);
88 // The two number sequences have the same length (J-I), just memcmp them.
89 if (int Res = compareMemory(Data + I, RHS.Data + I, J - I))
90 return Res < 0 ? -1 : 1;
91 // Identical number sequences, continue search after the numbers.
95 if (Data[I] != RHS.Data[I])
96 return (unsigned char)Data[I] < (unsigned char)RHS.Data[I] ? -1 : 1;
98 if (Length == RHS.Length)
100 return Length < RHS.Length ? -1 : 1;
103 // Compute the edit distance between the two given strings.
104 unsigned StringRef::edit_distance(llvm::StringRef Other,
105 bool AllowReplacements,
106 unsigned MaxEditDistance) const {
107 return llvm::ComputeEditDistance(
108 makeArrayRef(data(), size()),
109 makeArrayRef(Other.data(), Other.size()),
110 AllowReplacements, MaxEditDistance);
113 //===----------------------------------------------------------------------===//
115 //===----------------------------------------------------------------------===//
117 std::string StringRef::lower() const {
118 std::string Result(size(), char());
119 for (size_type i = 0, e = size(); i != e; ++i) {
120 Result[i] = ascii_tolower(Data[i]);
125 std::string StringRef::upper() const {
126 std::string Result(size(), char());
127 for (size_type i = 0, e = size(); i != e; ++i) {
128 Result[i] = ascii_toupper(Data[i]);
133 //===----------------------------------------------------------------------===//
135 //===----------------------------------------------------------------------===//
138 /// find - Search for the first string \arg Str in the string.
140 /// \return - The index of the first occurrence of \arg Str, or npos if not
142 size_t StringRef::find(StringRef Str, size_t From) const {
143 size_t N = Str.size();
147 // For short haystacks or unsupported needles fall back to the naive algorithm
148 if (Length < 16 || N > 255 || N == 0) {
149 for (size_t e = Length - N + 1, i = std::min(From, e); i != e; ++i)
150 if (substr(i, N).equals(Str))
158 // Build the bad char heuristic table, with uint8_t to reduce cache thrashing.
159 uint8_t BadCharSkip[256];
160 std::memset(BadCharSkip, N, 256);
161 for (unsigned i = 0; i != N-1; ++i)
162 BadCharSkip[(uint8_t)Str[i]] = N-1-i;
164 unsigned Len = Length-From, Pos = From;
166 if (substr(Pos, N).equals(Str)) // See if this is the correct substring.
169 // Otherwise skip the appropriate number of bytes.
170 uint8_t Skip = BadCharSkip[(uint8_t)(*this)[Pos+N-1]];
178 /// rfind - Search for the last string \arg Str in the string.
180 /// \return - The index of the last occurrence of \arg Str, or npos if not
182 size_t StringRef::rfind(StringRef Str) const {
183 size_t N = Str.size();
186 for (size_t i = Length - N + 1, e = 0; i != e;) {
188 if (substr(i, N).equals(Str))
194 /// find_first_of - Find the first character in the string that is in \arg
195 /// Chars, or npos if not found.
197 /// Note: O(size() + Chars.size())
198 StringRef::size_type StringRef::find_first_of(StringRef Chars,
200 std::bitset<1 << CHAR_BIT> CharBits;
201 for (size_type i = 0; i != Chars.size(); ++i)
202 CharBits.set((unsigned char)Chars[i]);
204 for (size_type i = std::min(From, Length), e = Length; i != e; ++i)
205 if (CharBits.test((unsigned char)Data[i]))
210 /// find_first_not_of - Find the first character in the string that is not
211 /// \arg C or npos if not found.
212 StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
213 for (size_type i = std::min(From, Length), e = Length; i != e; ++i)
219 /// find_first_not_of - Find the first character in the string that is not
220 /// in the string \arg Chars, or npos if not found.
222 /// Note: O(size() + Chars.size())
223 StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
225 std::bitset<1 << CHAR_BIT> CharBits;
226 for (size_type i = 0; i != Chars.size(); ++i)
227 CharBits.set((unsigned char)Chars[i]);
229 for (size_type i = std::min(From, Length), e = Length; i != e; ++i)
230 if (!CharBits.test((unsigned char)Data[i]))
235 /// find_last_of - Find the last character in the string that is in \arg C,
236 /// or npos if not found.
238 /// Note: O(size() + Chars.size())
239 StringRef::size_type StringRef::find_last_of(StringRef Chars,
241 std::bitset<1 << CHAR_BIT> CharBits;
242 for (size_type i = 0; i != Chars.size(); ++i)
243 CharBits.set((unsigned char)Chars[i]);
245 for (size_type i = std::min(From, Length) - 1, e = -1; i != e; --i)
246 if (CharBits.test((unsigned char)Data[i]))
251 /// find_last_not_of - Find the last character in the string that is not
252 /// \arg C, or npos if not found.
253 StringRef::size_type StringRef::find_last_not_of(char C, size_t From) const {
254 for (size_type i = std::min(From, Length) - 1, e = -1; i != e; --i)
260 /// find_last_not_of - Find the last character in the string that is not in
261 /// \arg Chars, or npos if not found.
263 /// Note: O(size() + Chars.size())
264 StringRef::size_type StringRef::find_last_not_of(StringRef Chars,
266 std::bitset<1 << CHAR_BIT> CharBits;
267 for (size_type i = 0, e = Chars.size(); i != e; ++i)
268 CharBits.set((unsigned char)Chars[i]);
270 for (size_type i = std::min(From, Length) - 1, e = -1; i != e; --i)
271 if (!CharBits.test((unsigned char)Data[i]))
276 void StringRef::split(SmallVectorImpl<StringRef> &A,
277 StringRef Separator, int MaxSplit,
278 bool KeepEmpty) const {
281 // Count down from MaxSplit. When MaxSplit is -1, this will just split
282 // "forever". This doesn't support splitting more than 2^31 times
283 // intentionally; if we ever want that we can make MaxSplit a 64-bit integer
284 // but that seems unlikely to be useful.
285 while (MaxSplit-- != 0) {
286 size_t Idx = S.find(Separator);
291 if (KeepEmpty || Idx > 0)
292 A.push_back(S.slice(0, Idx));
295 S = S.slice(Idx + Separator.size(), npos);
299 if (KeepEmpty || !S.empty())
303 void StringRef::split(SmallVectorImpl<StringRef> &A, char Separator,
304 int MaxSplit, bool KeepEmpty) const {
307 // Count down from MaxSplit. When MaxSplit is -1, this will just split
308 // "forever". This doesn't support splitting more than 2^31 times
309 // intentionally; if we ever want that we can make MaxSplit a 64-bit integer
310 // but that seems unlikely to be useful.
311 while (MaxSplit-- != 0) {
312 size_t Idx = S.find(Separator);
317 if (KeepEmpty || Idx > 0)
318 A.push_back(S.slice(0, Idx));
321 S = S.slice(Idx + 1, npos);
325 if (KeepEmpty || !S.empty())
329 //===----------------------------------------------------------------------===//
330 // Helpful Algorithms
331 //===----------------------------------------------------------------------===//
333 /// count - Return the number of non-overlapped occurrences of \arg Str in
335 size_t StringRef::count(StringRef Str) const {
337 size_t N = Str.size();
340 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
341 if (substr(i, N).equals(Str))
346 static unsigned GetAutoSenseRadix(StringRef &Str) {
347 if (Str.startswith("0x")) {
352 if (Str.startswith("0b")) {
357 if (Str.startswith("0o")) {
362 if (Str.startswith("0"))
369 /// GetAsUnsignedInteger - Workhorse method that converts a integer character
370 /// sequence of radix up to 36 to an unsigned long long value.
371 bool llvm::getAsUnsignedInteger(StringRef Str, unsigned Radix,
372 unsigned long long &Result) {
373 // Autosense radix if not specified.
375 Radix = GetAutoSenseRadix(Str);
377 // Empty strings (after the radix autosense) are invalid.
378 if (Str.empty()) return true;
380 // Parse all the bytes of the string given this radix. Watch for overflow.
382 while (!Str.empty()) {
384 if (Str[0] >= '0' && Str[0] <= '9')
385 CharVal = Str[0]-'0';
386 else if (Str[0] >= 'a' && Str[0] <= 'z')
387 CharVal = Str[0]-'a'+10;
388 else if (Str[0] >= 'A' && Str[0] <= 'Z')
389 CharVal = Str[0]-'A'+10;
393 // If the parsed value is larger than the integer radix, the string is
395 if (CharVal >= Radix)
398 // Add in this character.
399 unsigned long long PrevResult = Result;
400 Result = Result*Radix+CharVal;
402 // Check for overflow by shifting back and seeing if bits were lost.
403 if (Result/Radix < PrevResult)
412 bool llvm::getAsSignedInteger(StringRef Str, unsigned Radix,
414 unsigned long long ULLVal;
416 // Handle positive strings first.
417 if (Str.empty() || Str.front() != '-') {
418 if (getAsUnsignedInteger(Str, Radix, ULLVal) ||
419 // Check for value so large it overflows a signed value.
420 (long long)ULLVal < 0)
426 // Get the positive part of the value.
427 if (getAsUnsignedInteger(Str.substr(1), Radix, ULLVal) ||
428 // Reject values so large they'd overflow as negative signed, but allow
429 // "-0". This negates the unsigned so that the negative isn't undefined
430 // on signed overflow.
431 (long long)-ULLVal > 0)
438 bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const {
439 StringRef Str = *this;
441 // Autosense radix if not specified.
443 Radix = GetAutoSenseRadix(Str);
445 assert(Radix > 1 && Radix <= 36);
447 // Empty strings (after the radix autosense) are invalid.
448 if (Str.empty()) return true;
450 // Skip leading zeroes. This can be a significant improvement if
451 // it means we don't need > 64 bits.
452 while (!Str.empty() && Str.front() == '0')
455 // If it was nothing but zeroes....
457 Result = APInt(64, 0);
461 // (Over-)estimate the required number of bits.
462 unsigned Log2Radix = 0;
463 while ((1U << Log2Radix) < Radix) Log2Radix++;
464 bool IsPowerOf2Radix = ((1U << Log2Radix) == Radix);
466 unsigned BitWidth = Log2Radix * Str.size();
467 if (BitWidth < Result.getBitWidth())
468 BitWidth = Result.getBitWidth(); // don't shrink the result
469 else if (BitWidth > Result.getBitWidth())
470 Result = Result.zext(BitWidth);
472 APInt RadixAP, CharAP; // unused unless !IsPowerOf2Radix
473 if (!IsPowerOf2Radix) {
474 // These must have the same bit-width as Result.
475 RadixAP = APInt(BitWidth, Radix);
476 CharAP = APInt(BitWidth, 0);
479 // Parse all the bytes of the string given this radix.
481 while (!Str.empty()) {
483 if (Str[0] >= '0' && Str[0] <= '9')
484 CharVal = Str[0]-'0';
485 else if (Str[0] >= 'a' && Str[0] <= 'z')
486 CharVal = Str[0]-'a'+10;
487 else if (Str[0] >= 'A' && Str[0] <= 'Z')
488 CharVal = Str[0]-'A'+10;
492 // If the parsed value is larger than the integer radix, the string is
494 if (CharVal >= Radix)
497 // Add in this character.
498 if (IsPowerOf2Radix) {
499 Result <<= Log2Radix;
514 // Implementation of StringRef hashing.
515 hash_code llvm::hash_value(StringRef S) {
516 return hash_combine_range(S.begin(), S.end());