1 //===-- StringRef.cpp - Lightweight String References ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/StringRef.h"
10 #include "llvm/ADT/APFloat.h"
11 #include "llvm/ADT/APInt.h"
12 #include "llvm/ADT/Hashing.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/edit_distance.h"
15 #include "llvm/Support/Error.h"
16 #include <bitset>
17 
18 using namespace llvm;
19 
20 // MSVC emits references to this into the translation units which reference it.
21 #ifndef _MSC_VER
22 constexpr size_t StringRef::npos;
23 #endif
24 
25 // strncasecmp() is not available on non-POSIX systems, so define an
26 // alternative function here.
27 static int ascii_strncasecmp(const char *LHS, const char *RHS, size_t Length) {
28   for (size_t I = 0; I < Length; ++I) {
29     unsigned char LHC = toLower(LHS[I]);
30     unsigned char RHC = toLower(RHS[I]);
31     if (LHC != RHC)
32       return LHC < RHC ? -1 : 1;
33   }
34   return 0;
35 }
36 
37 /// compare_lower - Compare strings, ignoring case.
38 int StringRef::compare_lower(StringRef RHS) const {
39   if (int Res = ascii_strncasecmp(Data, RHS.Data, std::min(Length, RHS.Length)))
40     return Res;
41   if (Length == RHS.Length)
42     return 0;
43   return Length < RHS.Length ? -1 : 1;
44 }
45 
46 /// Check if this string starts with the given \p Prefix, ignoring case.
47 bool StringRef::startswith_lower(StringRef Prefix) const {
48   return Length >= Prefix.Length &&
49       ascii_strncasecmp(Data, Prefix.Data, Prefix.Length) == 0;
50 }
51 
52 /// Check if this string ends with the given \p Suffix, ignoring case.
53 bool StringRef::endswith_lower(StringRef Suffix) const {
54   return Length >= Suffix.Length &&
55       ascii_strncasecmp(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
56 }
57 
58 size_t StringRef::find_lower(char C, size_t From) const {
59   char L = toLower(C);
60   return find_if([L](char D) { return toLower(D) == L; }, From);
61 }
62 
63 /// compare_numeric - Compare strings, handle embedded numbers.
64 int StringRef::compare_numeric(StringRef RHS) const {
65   for (size_t I = 0, E = std::min(Length, RHS.Length); I != E; ++I) {
66     // Check for sequences of digits.
67     if (isDigit(Data[I]) && isDigit(RHS.Data[I])) {
68       // The longer sequence of numbers is considered larger.
69       // This doesn't really handle prefixed zeros well.
70       size_t J;
71       for (J = I + 1; J != E + 1; ++J) {
72         bool ld = J < Length && isDigit(Data[J]);
73         bool rd = J < RHS.Length && isDigit(RHS.Data[J]);
74         if (ld != rd)
75           return rd ? -1 : 1;
76         if (!rd)
77           break;
78       }
79       // The two number sequences have the same length (J-I), just memcmp them.
80       if (int Res = compareMemory(Data + I, RHS.Data + I, J - I))
81         return Res < 0 ? -1 : 1;
82       // Identical number sequences, continue search after the numbers.
83       I = J - 1;
84       continue;
85     }
86     if (Data[I] != RHS.Data[I])
87       return (unsigned char)Data[I] < (unsigned char)RHS.Data[I] ? -1 : 1;
88   }
89   if (Length == RHS.Length)
90     return 0;
91   return Length < RHS.Length ? -1 : 1;
92 }
93 
94 // Compute the edit distance between the two given strings.
95 unsigned StringRef::edit_distance(llvm::StringRef Other,
96                                   bool AllowReplacements,
97                                   unsigned MaxEditDistance) const {
98   return llvm::ComputeEditDistance(
99       makeArrayRef(data(), size()),
100       makeArrayRef(Other.data(), Other.size()),
101       AllowReplacements, MaxEditDistance);
102 }
103 
104 //===----------------------------------------------------------------------===//
105 // String Operations
106 //===----------------------------------------------------------------------===//
107 
108 std::string StringRef::lower() const {
109   return std::string(map_iterator(begin(), toLower),
110                      map_iterator(end(), toLower));
111 }
112 
113 std::string StringRef::upper() const {
114   return std::string(map_iterator(begin(), toUpper),
115                      map_iterator(end(), toUpper));
116 }
117 
118 //===----------------------------------------------------------------------===//
119 // String Searching
120 //===----------------------------------------------------------------------===//
121 
122 
123 /// find - Search for the first string \arg Str in the string.
124 ///
125 /// \return - The index of the first occurrence of \arg Str, or npos if not
126 /// found.
127 size_t StringRef::find(StringRef Str, size_t From) const {
128   if (From > Length)
129     return npos;
130 
131   const char *Start = Data + From;
132   size_t Size = Length - From;
133 
134   const char *Needle = Str.data();
135   size_t N = Str.size();
136   if (N == 0)
137     return From;
138   if (Size < N)
139     return npos;
140   if (N == 1) {
141     const char *Ptr = (const char *)::memchr(Start, Needle[0], Size);
142     return Ptr == nullptr ? npos : Ptr - Data;
143   }
144 
145   const char *Stop = Start + (Size - N + 1);
146 
147   // For short haystacks or unsupported needles fall back to the naive algorithm
148   if (Size < 16 || N > 255) {
149     do {
150       if (std::memcmp(Start, Needle, N) == 0)
151         return Start - Data;
152       ++Start;
153     } while (Start < Stop);
154     return npos;
155   }
156 
157   // Build the bad char heuristic table, with uint8_t to reduce cache thrashing.
158   uint8_t BadCharSkip[256];
159   std::memset(BadCharSkip, N, 256);
160   for (unsigned i = 0; i != N-1; ++i)
161     BadCharSkip[(uint8_t)Str[i]] = N-1-i;
162 
163   do {
164     uint8_t Last = Start[N - 1];
165     if (LLVM_UNLIKELY(Last == (uint8_t)Needle[N - 1]))
166       if (std::memcmp(Start, Needle, N - 1) == 0)
167         return Start - Data;
168 
169     // Otherwise skip the appropriate number of bytes.
170     Start += BadCharSkip[Last];
171   } while (Start < Stop);
172 
173   return npos;
174 }
175 
176 size_t StringRef::find_lower(StringRef Str, size_t From) const {
177   StringRef This = substr(From);
178   while (This.size() >= Str.size()) {
179     if (This.startswith_lower(Str))
180       return From;
181     This = This.drop_front();
182     ++From;
183   }
184   return npos;
185 }
186 
187 size_t StringRef::rfind_lower(char C, size_t From) const {
188   From = std::min(From, Length);
189   size_t i = From;
190   while (i != 0) {
191     --i;
192     if (toLower(Data[i]) == toLower(C))
193       return i;
194   }
195   return npos;
196 }
197 
198 /// rfind - Search for the last string \arg Str in the string.
199 ///
200 /// \return - The index of the last occurrence of \arg Str, or npos if not
201 /// found.
202 size_t StringRef::rfind(StringRef Str) const {
203   size_t N = Str.size();
204   if (N > Length)
205     return npos;
206   for (size_t i = Length - N + 1, e = 0; i != e;) {
207     --i;
208     if (substr(i, N).equals(Str))
209       return i;
210   }
211   return npos;
212 }
213 
214 size_t StringRef::rfind_lower(StringRef Str) const {
215   size_t N = Str.size();
216   if (N > Length)
217     return npos;
218   for (size_t i = Length - N + 1, e = 0; i != e;) {
219     --i;
220     if (substr(i, N).equals_lower(Str))
221       return i;
222   }
223   return npos;
224 }
225 
226 /// find_first_of - Find the first character in the string that is in \arg
227 /// Chars, or npos if not found.
228 ///
229 /// Note: O(size() + Chars.size())
230 StringRef::size_type StringRef::find_first_of(StringRef Chars,
231                                               size_t From) const {
232   std::bitset<1 << CHAR_BIT> CharBits;
233   for (size_type i = 0; i != Chars.size(); ++i)
234     CharBits.set((unsigned char)Chars[i]);
235 
236   for (size_type i = std::min(From, Length), e = Length; i != e; ++i)
237     if (CharBits.test((unsigned char)Data[i]))
238       return i;
239   return npos;
240 }
241 
242 /// find_first_not_of - Find the first character in the string that is not
243 /// \arg C or npos if not found.
244 StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
245   for (size_type i = std::min(From, Length), e = Length; i != e; ++i)
246     if (Data[i] != C)
247       return i;
248   return npos;
249 }
250 
251 /// find_first_not_of - Find the first character in the string that is not
252 /// in the string \arg Chars, or npos if not found.
253 ///
254 /// Note: O(size() + Chars.size())
255 StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
256                                                   size_t From) const {
257   std::bitset<1 << CHAR_BIT> CharBits;
258   for (size_type i = 0; i != Chars.size(); ++i)
259     CharBits.set((unsigned char)Chars[i]);
260 
261   for (size_type i = std::min(From, Length), e = Length; i != e; ++i)
262     if (!CharBits.test((unsigned char)Data[i]))
263       return i;
264   return npos;
265 }
266 
267 /// find_last_of - Find the last character in the string that is in \arg C,
268 /// or npos if not found.
269 ///
270 /// Note: O(size() + Chars.size())
271 StringRef::size_type StringRef::find_last_of(StringRef Chars,
272                                              size_t From) const {
273   std::bitset<1 << CHAR_BIT> CharBits;
274   for (size_type i = 0; i != Chars.size(); ++i)
275     CharBits.set((unsigned char)Chars[i]);
276 
277   for (size_type i = std::min(From, Length) - 1, e = -1; i != e; --i)
278     if (CharBits.test((unsigned char)Data[i]))
279       return i;
280   return npos;
281 }
282 
283 /// find_last_not_of - Find the last character in the string that is not
284 /// \arg C, or npos if not found.
285 StringRef::size_type StringRef::find_last_not_of(char C, size_t From) const {
286   for (size_type i = std::min(From, Length) - 1, e = -1; i != e; --i)
287     if (Data[i] != C)
288       return i;
289   return npos;
290 }
291 
292 /// find_last_not_of - Find the last character in the string that is not in
293 /// \arg Chars, or npos if not found.
294 ///
295 /// Note: O(size() + Chars.size())
296 StringRef::size_type StringRef::find_last_not_of(StringRef Chars,
297                                                  size_t From) const {
298   std::bitset<1 << CHAR_BIT> CharBits;
299   for (size_type i = 0, e = Chars.size(); i != e; ++i)
300     CharBits.set((unsigned char)Chars[i]);
301 
302   for (size_type i = std::min(From, Length) - 1, e = -1; i != e; --i)
303     if (!CharBits.test((unsigned char)Data[i]))
304       return i;
305   return npos;
306 }
307 
308 void StringRef::split(SmallVectorImpl<StringRef> &A,
309                       StringRef Separator, int MaxSplit,
310                       bool KeepEmpty) const {
311   StringRef S = *this;
312 
313   // Count down from MaxSplit. When MaxSplit is -1, this will just split
314   // "forever". This doesn't support splitting more than 2^31 times
315   // intentionally; if we ever want that we can make MaxSplit a 64-bit integer
316   // but that seems unlikely to be useful.
317   while (MaxSplit-- != 0) {
318     size_t Idx = S.find(Separator);
319     if (Idx == npos)
320       break;
321 
322     // Push this split.
323     if (KeepEmpty || Idx > 0)
324       A.push_back(S.slice(0, Idx));
325 
326     // Jump forward.
327     S = S.slice(Idx + Separator.size(), npos);
328   }
329 
330   // Push the tail.
331   if (KeepEmpty || !S.empty())
332     A.push_back(S);
333 }
334 
335 void StringRef::split(SmallVectorImpl<StringRef> &A, char Separator,
336                       int MaxSplit, bool KeepEmpty) const {
337   StringRef S = *this;
338 
339   // Count down from MaxSplit. When MaxSplit is -1, this will just split
340   // "forever". This doesn't support splitting more than 2^31 times
341   // intentionally; if we ever want that we can make MaxSplit a 64-bit integer
342   // but that seems unlikely to be useful.
343   while (MaxSplit-- != 0) {
344     size_t Idx = S.find(Separator);
345     if (Idx == npos)
346       break;
347 
348     // Push this split.
349     if (KeepEmpty || Idx > 0)
350       A.push_back(S.slice(0, Idx));
351 
352     // Jump forward.
353     S = S.slice(Idx + 1, npos);
354   }
355 
356   // Push the tail.
357   if (KeepEmpty || !S.empty())
358     A.push_back(S);
359 }
360 
361 //===----------------------------------------------------------------------===//
362 // Helpful Algorithms
363 //===----------------------------------------------------------------------===//
364 
365 /// count - Return the number of non-overlapped occurrences of \arg Str in
366 /// the string.
367 size_t StringRef::count(StringRef Str) const {
368   size_t Count = 0;
369   size_t N = Str.size();
370   if (!N || N > Length)
371     return 0;
372   for (size_t i = 0, e = Length - N + 1; i < e;) {
373     if (substr(i, N).equals(Str)) {
374       ++Count;
375       i += N;
376     }
377     else
378       ++i;
379   }
380   return Count;
381 }
382 
383 static unsigned GetAutoSenseRadix(StringRef &Str) {
384   if (Str.empty())
385     return 10;
386 
387   if (Str.startswith("0x") || Str.startswith("0X")) {
388     Str = Str.substr(2);
389     return 16;
390   }
391 
392   if (Str.startswith("0b") || Str.startswith("0B")) {
393     Str = Str.substr(2);
394     return 2;
395   }
396 
397   if (Str.startswith("0o")) {
398     Str = Str.substr(2);
399     return 8;
400   }
401 
402   if (Str[0] == '0' && Str.size() > 1 && isDigit(Str[1])) {
403     Str = Str.substr(1);
404     return 8;
405   }
406 
407   return 10;
408 }
409 
410 bool llvm::consumeUnsignedInteger(StringRef &Str, unsigned Radix,
411                                   unsigned long long &Result) {
412   // Autosense radix if not specified.
413   if (Radix == 0)
414     Radix = GetAutoSenseRadix(Str);
415 
416   // Empty strings (after the radix autosense) are invalid.
417   if (Str.empty()) return true;
418 
419   // Parse all the bytes of the string given this radix.  Watch for overflow.
420   StringRef Str2 = Str;
421   Result = 0;
422   while (!Str2.empty()) {
423     unsigned CharVal;
424     if (Str2[0] >= '0' && Str2[0] <= '9')
425       CharVal = Str2[0] - '0';
426     else if (Str2[0] >= 'a' && Str2[0] <= 'z')
427       CharVal = Str2[0] - 'a' + 10;
428     else if (Str2[0] >= 'A' && Str2[0] <= 'Z')
429       CharVal = Str2[0] - 'A' + 10;
430     else
431       break;
432 
433     // If the parsed value is larger than the integer radix, we cannot
434     // consume any more characters.
435     if (CharVal >= Radix)
436       break;
437 
438     // Add in this character.
439     unsigned long long PrevResult = Result;
440     Result = Result * Radix + CharVal;
441 
442     // Check for overflow by shifting back and seeing if bits were lost.
443     if (Result / Radix < PrevResult)
444       return true;
445 
446     Str2 = Str2.substr(1);
447   }
448 
449   // We consider the operation a failure if no characters were consumed
450   // successfully.
451   if (Str.size() == Str2.size())
452     return true;
453 
454   Str = Str2;
455   return false;
456 }
457 
458 bool llvm::consumeSignedInteger(StringRef &Str, unsigned Radix,
459                                 long long &Result) {
460   unsigned long long ULLVal;
461 
462   // Handle positive strings first.
463   if (Str.empty() || Str.front() != '-') {
464     if (consumeUnsignedInteger(Str, Radix, ULLVal) ||
465         // Check for value so large it overflows a signed value.
466         (long long)ULLVal < 0)
467       return true;
468     Result = ULLVal;
469     return false;
470   }
471 
472   // Get the positive part of the value.
473   StringRef Str2 = Str.drop_front(1);
474   if (consumeUnsignedInteger(Str2, Radix, ULLVal) ||
475       // Reject values so large they'd overflow as negative signed, but allow
476       // "-0".  This negates the unsigned so that the negative isn't undefined
477       // on signed overflow.
478       (long long)-ULLVal > 0)
479     return true;
480 
481   Str = Str2;
482   Result = -ULLVal;
483   return false;
484 }
485 
486 /// GetAsUnsignedInteger - Workhorse method that converts a integer character
487 /// sequence of radix up to 36 to an unsigned long long value.
488 bool llvm::getAsUnsignedInteger(StringRef Str, unsigned Radix,
489                                 unsigned long long &Result) {
490   if (consumeUnsignedInteger(Str, Radix, Result))
491     return true;
492 
493   // For getAsUnsignedInteger, we require the whole string to be consumed or
494   // else we consider it a failure.
495   return !Str.empty();
496 }
497 
498 bool llvm::getAsSignedInteger(StringRef Str, unsigned Radix,
499                               long long &Result) {
500   if (consumeSignedInteger(Str, Radix, Result))
501     return true;
502 
503   // For getAsSignedInteger, we require the whole string to be consumed or else
504   // we consider it a failure.
505   return !Str.empty();
506 }
507 
508 bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const {
509   StringRef Str = *this;
510 
511   // Autosense radix if not specified.
512   if (Radix == 0)
513     Radix = GetAutoSenseRadix(Str);
514 
515   assert(Radix > 1 && Radix <= 36);
516 
517   // Empty strings (after the radix autosense) are invalid.
518   if (Str.empty()) return true;
519 
520   // Skip leading zeroes.  This can be a significant improvement if
521   // it means we don't need > 64 bits.
522   while (!Str.empty() && Str.front() == '0')
523     Str = Str.substr(1);
524 
525   // If it was nothing but zeroes....
526   if (Str.empty()) {
527     Result = APInt(64, 0);
528     return false;
529   }
530 
531   // (Over-)estimate the required number of bits.
532   unsigned Log2Radix = 0;
533   while ((1U << Log2Radix) < Radix) Log2Radix++;
534   bool IsPowerOf2Radix = ((1U << Log2Radix) == Radix);
535 
536   unsigned BitWidth = Log2Radix * Str.size();
537   if (BitWidth < Result.getBitWidth())
538     BitWidth = Result.getBitWidth(); // don't shrink the result
539   else if (BitWidth > Result.getBitWidth())
540     Result = Result.zext(BitWidth);
541 
542   APInt RadixAP, CharAP; // unused unless !IsPowerOf2Radix
543   if (!IsPowerOf2Radix) {
544     // These must have the same bit-width as Result.
545     RadixAP = APInt(BitWidth, Radix);
546     CharAP = APInt(BitWidth, 0);
547   }
548 
549   // Parse all the bytes of the string given this radix.
550   Result = 0;
551   while (!Str.empty()) {
552     unsigned CharVal;
553     if (Str[0] >= '0' && Str[0] <= '9')
554       CharVal = Str[0]-'0';
555     else if (Str[0] >= 'a' && Str[0] <= 'z')
556       CharVal = Str[0]-'a'+10;
557     else if (Str[0] >= 'A' && Str[0] <= 'Z')
558       CharVal = Str[0]-'A'+10;
559     else
560       return true;
561 
562     // If the parsed value is larger than the integer radix, the string is
563     // invalid.
564     if (CharVal >= Radix)
565       return true;
566 
567     // Add in this character.
568     if (IsPowerOf2Radix) {
569       Result <<= Log2Radix;
570       Result |= CharVal;
571     } else {
572       Result *= RadixAP;
573       CharAP = CharVal;
574       Result += CharAP;
575     }
576 
577     Str = Str.substr(1);
578   }
579 
580   return false;
581 }
582 
583 bool StringRef::getAsDouble(double &Result, bool AllowInexact) const {
584   APFloat F(0.0);
585   auto StatusOrErr = F.convertFromString(*this, APFloat::rmNearestTiesToEven);
586   if (errorToBool(StatusOrErr.takeError()))
587     return true;
588 
589   APFloat::opStatus Status = *StatusOrErr;
590   if (Status != APFloat::opOK) {
591     if (!AllowInexact || !(Status & APFloat::opInexact))
592       return true;
593   }
594 
595   Result = F.convertToDouble();
596   return false;
597 }
598 
599 // Implementation of StringRef hashing.
600 hash_code llvm::hash_value(StringRef S) {
601   return hash_combine_range(S.begin(), S.end());
602 }
603