LLVM  9.0.0svn
StringRef.h
Go to the documentation of this file.
1 //===- StringRef.h - Constant String Reference Wrapper ----------*- C++ -*-===//
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 #ifndef LLVM_ADT_STRINGREF_H
10 #define LLVM_ADT_STRINGREF_H
11 
12 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/Support/Compiler.h"
15 #include <algorithm>
16 #include <cassert>
17 #include <cstddef>
18 #include <cstring>
19 #include <limits>
20 #include <string>
21 #include <type_traits>
22 #include <utility>
23 
24 namespace llvm {
25 
26  class APInt;
27  class hash_code;
28  template <typename T> class SmallVectorImpl;
29  class StringRef;
30 
31  /// Helper functions for StringRef::getAsInteger.
32  bool getAsUnsignedInteger(StringRef Str, unsigned Radix,
33  unsigned long long &Result);
34 
35  bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result);
36 
37  bool consumeUnsignedInteger(StringRef &Str, unsigned Radix,
38  unsigned long long &Result);
39  bool consumeSignedInteger(StringRef &Str, unsigned Radix, long long &Result);
40 
41  /// StringRef - Represent a constant reference to a string, i.e. a character
42  /// array and a length, which need not be null terminated.
43  ///
44  /// This class does not own the string data, it is expected to be used in
45  /// situations where the character data resides in some other buffer, whose
46  /// lifetime extends past that of the StringRef. For this reason, it is not in
47  /// general safe to store a StringRef.
48  class StringRef {
49  public:
50  static const size_t npos = ~size_t(0);
51 
52  using iterator = const char *;
53  using const_iterator = const char *;
54  using size_type = size_t;
55 
56  private:
57  /// The start of the string, in an external buffer.
58  const char *Data = nullptr;
59 
60  /// The length of the string.
61  size_t Length = 0;
62 
63  // Workaround memcmp issue with null pointers (undefined behavior)
64  // by providing a specialized version
66  static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
67  if (Length == 0) { return 0; }
68  return ::memcmp(Lhs,Rhs,Length);
69  }
70 
71  public:
72  /// @name Constructors
73  /// @{
74 
75  /// Construct an empty string ref.
76  /*implicit*/ StringRef() = default;
77 
78  /// Disable conversion from nullptr. This prevents things like
79  /// if (S == nullptr)
80  StringRef(std::nullptr_t) = delete;
81 
82  /// Construct a string ref from a cstring.
84  /*implicit*/ StringRef(const char *Str)
85  : Data(Str), Length(Str ? ::strlen(Str) : 0) {}
86 
87  /// Construct a string ref from a pointer and length.
89  /*implicit*/ constexpr StringRef(const char *data, size_t length)
90  : Data(data), Length(length) {}
91 
92  /// Construct a string ref from an std::string.
94  /*implicit*/ StringRef(const std::string &Str)
95  : Data(Str.data()), Length(Str.length()) {}
96 
97  static StringRef withNullAsEmpty(const char *data) {
98  return StringRef(data ? data : "");
99  }
100 
101  /// @}
102  /// @name Iterators
103  /// @{
104 
105  iterator begin() const { return Data; }
106 
107  iterator end() const { return Data + Length; }
108 
109  const unsigned char *bytes_begin() const {
110  return reinterpret_cast<const unsigned char *>(begin());
111  }
112  const unsigned char *bytes_end() const {
113  return reinterpret_cast<const unsigned char *>(end());
114  }
116  return make_range(bytes_begin(), bytes_end());
117  }
118 
119  /// @}
120  /// @name String Operations
121  /// @{
122 
123  /// data - Get a pointer to the start of the string (which may not be null
124  /// terminated).
127  const char *data() const { return Data; }
128 
129  /// empty - Check if the string is empty.
132  bool empty() const { return Length == 0; }
133 
134  /// size - Get the string size.
137  size_t size() const { return Length; }
138 
139  /// front - Get the first character in the string.
141  char front() const {
142  assert(!empty());
143  return Data[0];
144  }
145 
146  /// back - Get the last character in the string.
148  char back() const {
149  assert(!empty());
150  return Data[Length-1];
151  }
152 
153  // copy - Allocate copy in Allocator and return StringRef to it.
154  template <typename Allocator>
156  // Don't request a length 0 copy from the allocator.
157  if (empty())
158  return StringRef();
159  char *S = A.template Allocate<char>(Length);
160  std::copy(begin(), end(), S);
161  return StringRef(S, Length);
162  }
163 
164  /// equals - Check for string equality, this is more efficient than
165  /// compare() when the relative ordering of inequal strings isn't needed.
168  bool equals(StringRef RHS) const {
169  return (Length == RHS.Length &&
170  compareMemory(Data, RHS.Data, RHS.Length) == 0);
171  }
172 
173  /// equals_lower - Check for string equality, ignoring case.
175  bool equals_lower(StringRef RHS) const {
176  return Length == RHS.Length && compare_lower(RHS) == 0;
177  }
178 
179  /// compare - Compare two strings; the result is -1, 0, or 1 if this string
180  /// is lexicographically less than, equal to, or greater than the \p RHS.
183  int compare(StringRef RHS) const {
184  // Check the prefix for a mismatch.
185  if (int Res = compareMemory(Data, RHS.Data, std::min(Length, RHS.Length)))
186  return Res < 0 ? -1 : 1;
187 
188  // Otherwise the prefixes match, so we only need to check the lengths.
189  if (Length == RHS.Length)
190  return 0;
191  return Length < RHS.Length ? -1 : 1;
192  }
193 
194  /// compare_lower - Compare two strings, ignoring case.
196  int compare_lower(StringRef RHS) const;
197 
198  /// compare_numeric - Compare two strings, treating sequences of digits as
199  /// numbers.
201  int compare_numeric(StringRef RHS) const;
202 
203  /// Determine the edit distance between this string and another
204  /// string.
205  ///
206  /// \param Other the string to compare this string against.
207  ///
208  /// \param AllowReplacements whether to allow character
209  /// replacements (change one character into another) as a single
210  /// operation, rather than as two operations (an insertion and a
211  /// removal).
212  ///
213  /// \param MaxEditDistance If non-zero, the maximum edit distance that
214  /// this routine is allowed to compute. If the edit distance will exceed
215  /// that maximum, returns \c MaxEditDistance+1.
216  ///
217  /// \returns the minimum number of character insertions, removals,
218  /// or (if \p AllowReplacements is \c true) replacements needed to
219  /// transform one of the given strings into the other. If zero,
220  /// the strings are identical.
222  unsigned edit_distance(StringRef Other, bool AllowReplacements = true,
223  unsigned MaxEditDistance = 0) const;
224 
225  /// str - Get the contents as an std::string.
227  std::string str() const {
228  if (!Data) return std::string();
229  return std::string(Data, Length);
230  }
231 
232  /// @}
233  /// @name Operator Overloads
234  /// @{
235 
237  char operator[](size_t Index) const {
238  assert(Index < Length && "Invalid index!");
239  return Data[Index];
240  }
241 
242  /// Disallow accidental assignment from a temporary std::string.
243  ///
244  /// The declaration here is extra complicated so that `stringRef = {}`
245  /// and `stringRef = "abc"` continue to select the move assignment operator.
246  template <typename T>
247  typename std::enable_if<std::is_same<T, std::string>::value,
248  StringRef>::type &
249  operator=(T &&Str) = delete;
250 
251  /// @}
252  /// @name Type Conversions
253  /// @{
254 
255  operator std::string() const {
256  return str();
257  }
258 
259  /// @}
260  /// @name String Predicates
261  /// @{
262 
263  /// Check if this string starts with the given \p Prefix.
267  return Length >= Prefix.Length &&
268  compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
269  }
270 
271  /// Check if this string starts with the given \p Prefix, ignoring case.
273  bool startswith_lower(StringRef Prefix) const;
274 
275  /// Check if this string ends with the given \p Suffix.
278  bool endswith(StringRef Suffix) const {
279  return Length >= Suffix.Length &&
280  compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
281  }
282 
283  /// Check if this string ends with the given \p Suffix, ignoring case.
285  bool endswith_lower(StringRef Suffix) const;
286 
287  /// @}
288  /// @name String Searching
289  /// @{
290 
291  /// Search for the first character \p C in the string.
292  ///
293  /// \returns The index of the first occurrence of \p C, or npos if not
294  /// found.
297  size_t find(char C, size_t From = 0) const {
298  size_t FindBegin = std::min(From, Length);
299  if (FindBegin < Length) { // Avoid calling memchr with nullptr.
300  // Just forward to memchr, which is faster than a hand-rolled loop.
301  if (const void *P = ::memchr(Data + FindBegin, C, Length - FindBegin))
302  return static_cast<const char *>(P) - Data;
303  }
304  return npos;
305  }
306 
307  /// Search for the first character \p C in the string, ignoring case.
308  ///
309  /// \returns The index of the first occurrence of \p C, or npos if not
310  /// found.
312  size_t find_lower(char C, size_t From = 0) const;
313 
314  /// Search for the first character satisfying the predicate \p F
315  ///
316  /// \returns The index of the first character satisfying \p F starting from
317  /// \p From, or npos if not found.
320  size_t find_if(function_ref<bool(char)> F, size_t From = 0) const {
322  while (!S.empty()) {
323  if (F(S.front()))
324  return size() - S.size();
325  S = S.drop_front();
326  }
327  return npos;
328  }
329 
330  /// Search for the first character not satisfying the predicate \p F
331  ///
332  /// \returns The index of the first character not satisfying \p F starting
333  /// from \p From, or npos if not found.
336  size_t find_if_not(function_ref<bool(char)> F, size_t From = 0) const {
337  return find_if([F](char c) { return !F(c); }, From);
338  }
339 
340  /// Search for the first string \p Str in the string.
341  ///
342  /// \returns The index of the first occurrence of \p Str, or npos if not
343  /// found.
345  size_t find(StringRef Str, size_t From = 0) const;
346 
347  /// Search for the first string \p Str in the string, ignoring case.
348  ///
349  /// \returns The index of the first occurrence of \p Str, or npos if not
350  /// found.
352  size_t find_lower(StringRef Str, size_t From = 0) const;
353 
354  /// Search for the last character \p C in the string.
355  ///
356  /// \returns The index of the last occurrence of \p C, or npos if not
357  /// found.
359  size_t rfind(char C, size_t From = npos) const {
360  From = std::min(From, Length);
361  size_t i = From;
362  while (i != 0) {
363  --i;
364  if (Data[i] == C)
365  return i;
366  }
367  return npos;
368  }
369 
370  /// Search for the last character \p C in the string, ignoring case.
371  ///
372  /// \returns The index of the last occurrence of \p C, or npos if not
373  /// found.
375  size_t rfind_lower(char C, size_t From = npos) const;
376 
377  /// Search for the last string \p Str in the string.
378  ///
379  /// \returns The index of the last occurrence of \p Str, or npos if not
380  /// found.
382  size_t rfind(StringRef Str) const;
383 
384  /// Search for the last string \p Str in the string, ignoring case.
385  ///
386  /// \returns The index of the last occurrence of \p Str, or npos if not
387  /// found.
389  size_t rfind_lower(StringRef Str) const;
390 
391  /// Find the first character in the string that is \p C, or npos if not
392  /// found. Same as find.
394  size_t find_first_of(char C, size_t From = 0) const {
395  return find(C, From);
396  }
397 
398  /// Find the first character in the string that is in \p Chars, or npos if
399  /// not found.
400  ///
401  /// Complexity: O(size() + Chars.size())
403  size_t find_first_of(StringRef Chars, size_t From = 0) const;
404 
405  /// Find the first character in the string that is not \p C or npos if not
406  /// found.
408  size_t find_first_not_of(char C, size_t From = 0) const;
409 
410  /// Find the first character in the string that is not in the string
411  /// \p Chars, or npos if not found.
412  ///
413  /// Complexity: O(size() + Chars.size())
415  size_t find_first_not_of(StringRef Chars, size_t From = 0) const;
416 
417  /// Find the last character in the string that is \p C, or npos if not
418  /// found.
420  size_t find_last_of(char C, size_t From = npos) const {
421  return rfind(C, From);
422  }
423 
424  /// Find the last character in the string that is in \p C, or npos if not
425  /// found.
426  ///
427  /// Complexity: O(size() + Chars.size())
429  size_t find_last_of(StringRef Chars, size_t From = npos) const;
430 
431  /// Find the last character in the string that is not \p C, or npos if not
432  /// found.
434  size_t find_last_not_of(char C, size_t From = npos) const;
435 
436  /// Find the last character in the string that is not in \p Chars, or
437  /// npos if not found.
438  ///
439  /// Complexity: O(size() + Chars.size())
441  size_t find_last_not_of(StringRef Chars, size_t From = npos) const;
442 
443  /// Return true if the given string is a substring of *this, and false
444  /// otherwise.
447  bool contains(StringRef Other) const { return find(Other) != npos; }
448 
449  /// Return true if the given character is contained in *this, and false
450  /// otherwise.
453  bool contains(char C) const { return find_first_of(C) != npos; }
454 
455  /// Return true if the given string is a substring of *this, and false
456  /// otherwise.
459  bool contains_lower(StringRef Other) const {
460  return find_lower(Other) != npos;
461  }
462 
463  /// Return true if the given character is contained in *this, and false
464  /// otherwise.
467  bool contains_lower(char C) const { return find_lower(C) != npos; }
468 
469  /// @}
470  /// @name Helpful Algorithms
471  /// @{
472 
473  /// Return the number of occurrences of \p C in the string.
475  size_t count(char C) const {
476  size_t Count = 0;
477  for (size_t i = 0, e = Length; i != e; ++i)
478  if (Data[i] == C)
479  ++Count;
480  return Count;
481  }
482 
483  /// Return the number of non-overlapped occurrences of \p Str in
484  /// the string.
485  size_t count(StringRef Str) const;
486 
487  /// Parse the current string as an integer of the specified radix. If
488  /// \p Radix is specified as zero, this does radix autosensing using
489  /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
490  ///
491  /// If the string is invalid or if only a subset of the string is valid,
492  /// this returns true to signify the error. The string is considered
493  /// erroneous if empty or if it overflows T.
494  template <typename T>
495  typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type
496  getAsInteger(unsigned Radix, T &Result) const {
497  long long LLVal;
498  if (getAsSignedInteger(*this, Radix, LLVal) ||
499  static_cast<T>(LLVal) != LLVal)
500  return true;
501  Result = LLVal;
502  return false;
503  }
504 
505  template <typename T>
506  typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type
507  getAsInteger(unsigned Radix, T &Result) const {
508  unsigned long long ULLVal;
509  // The additional cast to unsigned long long is required to avoid the
510  // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
511  // 'unsigned __int64' when instantiating getAsInteger with T = bool.
512  if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
513  static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
514  return true;
515  Result = ULLVal;
516  return false;
517  }
518 
519  /// Parse the current string as an integer of the specified radix. If
520  /// \p Radix is specified as zero, this does radix autosensing using
521  /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
522  ///
523  /// If the string does not begin with a number of the specified radix,
524  /// this returns true to signify the error. The string is considered
525  /// erroneous if empty or if it overflows T.
526  /// The portion of the string representing the discovered numeric value
527  /// is removed from the beginning of the string.
528  template <typename T>
529  typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type
530  consumeInteger(unsigned Radix, T &Result) {
531  long long LLVal;
532  if (consumeSignedInteger(*this, Radix, LLVal) ||
533  static_cast<long long>(static_cast<T>(LLVal)) != LLVal)
534  return true;
535  Result = LLVal;
536  return false;
537  }
538 
539  template <typename T>
540  typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type
541  consumeInteger(unsigned Radix, T &Result) {
542  unsigned long long ULLVal;
543  if (consumeUnsignedInteger(*this, Radix, ULLVal) ||
544  static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
545  return true;
546  Result = ULLVal;
547  return false;
548  }
549 
550  /// Parse the current string as an integer of the specified \p Radix, or of
551  /// an autosensed radix if the \p Radix given is 0. The current value in
552  /// \p Result is discarded, and the storage is changed to be wide enough to
553  /// store the parsed integer.
554  ///
555  /// \returns true if the string does not solely consist of a valid
556  /// non-empty number in the appropriate base.
557  ///
558  /// APInt::fromString is superficially similar but assumes the
559  /// string is well-formed in the given radix.
560  bool getAsInteger(unsigned Radix, APInt &Result) const;
561 
562  /// Parse the current string as an IEEE double-precision floating
563  /// point value. The string must be a well-formed double.
564  ///
565  /// If \p AllowInexact is false, the function will fail if the string
566  /// cannot be represented exactly. Otherwise, the function only fails
567  /// in case of an overflow or underflow.
568  bool getAsDouble(double &Result, bool AllowInexact = true) const;
569 
570  /// @}
571  /// @name String Operations
572  /// @{
573 
574  // Convert the given ASCII string to lowercase.
576  std::string lower() const;
577 
578  /// Convert the given ASCII string to uppercase.
580  std::string upper() const;
581 
582  /// @}
583  /// @name Substring Operations
584  /// @{
585 
586  /// Return a reference to the substring from [Start, Start + N).
587  ///
588  /// \param Start The index of the starting character in the substring; if
589  /// the index is npos or greater than the length of the string then the
590  /// empty substring will be returned.
591  ///
592  /// \param N The number of characters to included in the substring. If N
593  /// exceeds the number of characters remaining in the string, the string
594  /// suffix (starting with \p Start) will be returned.
597  StringRef substr(size_t Start, size_t N = npos) const {
598  Start = std::min(Start, Length);
599  return StringRef(Data + Start, std::min(N, Length - Start));
600  }
601 
602  /// Return a StringRef equal to 'this' but with only the first \p N
603  /// elements remaining. If \p N is greater than the length of the
604  /// string, the entire string is returned.
607  StringRef take_front(size_t N = 1) const {
608  if (N >= size())
609  return *this;
610  return drop_back(size() - N);
611  }
612 
613  /// Return a StringRef equal to 'this' but with only the last \p N
614  /// elements remaining. If \p N is greater than the length of the
615  /// string, the entire string is returned.
618  StringRef take_back(size_t N = 1) const {
619  if (N >= size())
620  return *this;
621  return drop_front(size() - N);
622  }
623 
624  /// Return the longest prefix of 'this' such that every character
625  /// in the prefix satisfies the given predicate.
628  StringRef take_while(function_ref<bool(char)> F) const {
629  return substr(0, find_if_not(F));
630  }
631 
632  /// Return the longest prefix of 'this' such that no character in
633  /// the prefix satisfies the given predicate.
636  StringRef take_until(function_ref<bool(char)> F) const {
637  return substr(0, find_if(F));
638  }
639 
640  /// Return a StringRef equal to 'this' but with the first \p N elements
641  /// dropped.
644  StringRef drop_front(size_t N = 1) const {
645  assert(size() >= N && "Dropping more elements than exist");
646  return substr(N);
647  }
648 
649  /// Return a StringRef equal to 'this' but with the last \p N elements
650  /// dropped.
653  StringRef drop_back(size_t N = 1) const {
654  assert(size() >= N && "Dropping more elements than exist");
655  return substr(0, size()-N);
656  }
657 
658  /// Return a StringRef equal to 'this', but with all characters satisfying
659  /// the given predicate dropped from the beginning of the string.
662  StringRef drop_while(function_ref<bool(char)> F) const {
663  return substr(find_if_not(F));
664  }
665 
666  /// Return a StringRef equal to 'this', but with all characters not
667  /// satisfying the given predicate dropped from the beginning of the string.
670  StringRef drop_until(function_ref<bool(char)> F) const {
671  return substr(find_if(F));
672  }
673 
674  /// Returns true if this StringRef has the given prefix and removes that
675  /// prefix.
677  bool consume_front(StringRef Prefix) {
678  if (!startswith(Prefix))
679  return false;
680 
681  *this = drop_front(Prefix.size());
682  return true;
683  }
684 
685  /// Returns true if this StringRef has the given suffix and removes that
686  /// suffix.
688  bool consume_back(StringRef Suffix) {
689  if (!endswith(Suffix))
690  return false;
691 
692  *this = drop_back(Suffix.size());
693  return true;
694  }
695 
696  /// Return a reference to the substring from [Start, End).
697  ///
698  /// \param Start The index of the starting character in the substring; if
699  /// the index is npos or greater than the length of the string then the
700  /// empty substring will be returned.
701  ///
702  /// \param End The index following the last character to include in the
703  /// substring. If this is npos or exceeds the number of characters
704  /// remaining in the string, the string suffix (starting with \p Start)
705  /// will be returned. If this is less than \p Start, an empty string will
706  /// be returned.
709  StringRef slice(size_t Start, size_t End) const {
710  Start = std::min(Start, Length);
711  End = std::min(std::max(Start, End), Length);
712  return StringRef(Data + Start, End - Start);
713  }
714 
715  /// Split into two substrings around the first occurrence of a separator
716  /// character.
717  ///
718  /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
719  /// such that (*this == LHS + Separator + RHS) is true and RHS is
720  /// maximal. If \p Separator is not in the string, then the result is a
721  /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
722  ///
723  /// \param Separator The character to split on.
724  /// \returns The split substrings.
726  std::pair<StringRef, StringRef> split(char Separator) const {
727  return split(StringRef(&Separator, 1));
728  }
729 
730  /// Split into two substrings around the first occurrence of a separator
731  /// string.
732  ///
733  /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
734  /// such that (*this == LHS + Separator + RHS) is true and RHS is
735  /// maximal. If \p Separator is not in the string, then the result is a
736  /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
737  ///
738  /// \param Separator - The string to split on.
739  /// \return - The split substrings.
741  std::pair<StringRef, StringRef> split(StringRef Separator) const {
742  size_t Idx = find(Separator);
743  if (Idx == npos)
744  return std::make_pair(*this, StringRef());
745  return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
746  }
747 
748  /// Split into two substrings around the last occurrence of a separator
749  /// string.
750  ///
751  /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
752  /// such that (*this == LHS + Separator + RHS) is true and RHS is
753  /// minimal. If \p Separator is not in the string, then the result is a
754  /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
755  ///
756  /// \param Separator - The string to split on.
757  /// \return - The split substrings.
759  std::pair<StringRef, StringRef> rsplit(StringRef Separator) const {
760  size_t Idx = rfind(Separator);
761  if (Idx == npos)
762  return std::make_pair(*this, StringRef());
763  return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
764  }
765 
766  /// Split into substrings around the occurrences of a separator string.
767  ///
768  /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
769  /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
770  /// elements are added to A.
771  /// If \p KeepEmpty is false, empty strings are not added to \p A. They
772  /// still count when considering \p MaxSplit
773  /// An useful invariant is that
774  /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
775  ///
776  /// \param A - Where to put the substrings.
777  /// \param Separator - The string to split on.
778  /// \param MaxSplit - The maximum number of times the string is split.
779  /// \param KeepEmpty - True if empty substring should be added.
781  StringRef Separator, int MaxSplit = -1,
782  bool KeepEmpty = true) const;
783 
784  /// Split into substrings around the occurrences of a separator character.
785  ///
786  /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
787  /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
788  /// elements are added to A.
789  /// If \p KeepEmpty is false, empty strings are not added to \p A. They
790  /// still count when considering \p MaxSplit
791  /// An useful invariant is that
792  /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
793  ///
794  /// \param A - Where to put the substrings.
795  /// \param Separator - The string to split on.
796  /// \param MaxSplit - The maximum number of times the string is split.
797  /// \param KeepEmpty - True if empty substring should be added.
798  void split(SmallVectorImpl<StringRef> &A, char Separator, int MaxSplit = -1,
799  bool KeepEmpty = true) const;
800 
801  /// Split into two substrings around the last occurrence of a separator
802  /// character.
803  ///
804  /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
805  /// such that (*this == LHS + Separator + RHS) is true and RHS is
806  /// minimal. If \p Separator is not in the string, then the result is a
807  /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
808  ///
809  /// \param Separator - The character to split on.
810  /// \return - The split substrings.
812  std::pair<StringRef, StringRef> rsplit(char Separator) const {
813  return rsplit(StringRef(&Separator, 1));
814  }
815 
816  /// Return string with consecutive \p Char characters starting from the
817  /// the left removed.
819  StringRef ltrim(char Char) const {
820  return drop_front(std::min(Length, find_first_not_of(Char)));
821  }
822 
823  /// Return string with consecutive characters in \p Chars starting from
824  /// the left removed.
826  StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
827  return drop_front(std::min(Length, find_first_not_of(Chars)));
828  }
829 
830  /// Return string with consecutive \p Char characters starting from the
831  /// right removed.
833  StringRef rtrim(char Char) const {
834  return drop_back(Length - std::min(Length, find_last_not_of(Char) + 1));
835  }
836 
837  /// Return string with consecutive characters in \p Chars starting from
838  /// the right removed.
840  StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {
841  return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1));
842  }
843 
844  /// Return string with consecutive \p Char characters starting from the
845  /// left and right removed.
847  StringRef trim(char Char) const {
848  return ltrim(Char).rtrim(Char);
849  }
850 
851  /// Return string with consecutive characters in \p Chars starting from
852  /// the left and right removed.
854  StringRef trim(StringRef Chars = " \t\n\v\f\r") const {
855  return ltrim(Chars).rtrim(Chars);
856  }
857 
858  /// @}
859  };
860 
861  /// A wrapper around a string literal that serves as a proxy for constructing
862  /// global tables of StringRefs with the length computed at compile time.
863  /// In order to avoid the invocation of a global constructor, StringLiteral
864  /// should *only* be used in a constexpr context, as such:
865  ///
866  /// constexpr StringLiteral S("test");
867  ///
868  class StringLiteral : public StringRef {
869  private:
870  constexpr StringLiteral(const char *Str, size_t N) : StringRef(Str, N) {
871  }
872 
873  public:
874  template <size_t N>
875  constexpr StringLiteral(const char (&Str)[N])
876 #if defined(__clang__) && __has_attribute(enable_if)
877 #pragma clang diagnostic push
878 #pragma clang diagnostic ignored "-Wgcc-compat"
879  __attribute((enable_if(__builtin_strlen(Str) == N - 1,
880  "invalid string literal")))
881 #pragma clang diagnostic pop
882 #endif
883  : StringRef(Str, N - 1) {
884  }
885 
886  // Explicit construction for strings like "foo\0bar".
887  template <size_t N>
888  static constexpr StringLiteral withInnerNUL(const char (&Str)[N]) {
889  return StringLiteral(Str, N - 1);
890  }
891  };
892 
893  /// @name StringRef Comparison Operators
894  /// @{
895 
897  inline bool operator==(StringRef LHS, StringRef RHS) {
898  return LHS.equals(RHS);
899  }
900 
902  inline bool operator!=(StringRef LHS, StringRef RHS) { return !(LHS == RHS); }
903 
904  inline bool operator<(StringRef LHS, StringRef RHS) {
905  return LHS.compare(RHS) == -1;
906  }
907 
908  inline bool operator<=(StringRef LHS, StringRef RHS) {
909  return LHS.compare(RHS) != 1;
910  }
911 
912  inline bool operator>(StringRef LHS, StringRef RHS) {
913  return LHS.compare(RHS) == 1;
914  }
915 
916  inline bool operator>=(StringRef LHS, StringRef RHS) {
917  return LHS.compare(RHS) != -1;
918  }
919 
920  inline std::string &operator+=(std::string &buffer, StringRef string) {
921  return buffer.append(string.data(), string.size());
922  }
923 
924  /// @}
925 
926  /// Compute a hash_code for a StringRef.
929 
930  // StringRefs can be treated like a POD type.
931  template <typename T> struct isPodLike;
932  template <> struct isPodLike<StringRef> { static const bool value = true; };
933 
934 } // end namespace llvm
935 
936 #endif // LLVM_ADT_STRINGREF_H
uint64_t CallInst * C
std::string & operator+=(std::string &buffer, StringRef string)
Definition: StringRef.h:920
std::enable_if<!std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Definition: StringRef.h:507
static StringRef withNullAsEmpty(const char *data)
Definition: StringRef.h:97
LLVM_NODISCARD char operator[](size_t Index) const
Definition: StringRef.h:237
LLVM_NODISCARD bool startswith_lower(StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
Definition: StringRef.cpp:46
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:227
This class represents lattice values for constants.
Definition: AllocatorList.h:23
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:447
const unsigned char * bytes_end() const
Definition: StringRef.h:112
LLVM_NODISCARD bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
Definition: StringRef.h:175
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find_if(function_ref< bool(char)> F, size_t From=0) const
Search for the first character satisfying the predicate F.
Definition: StringRef.h:320
bool consumeUnsignedInteger(StringRef &Str, unsigned Radix, unsigned long long &Result)
Definition: StringRef.cpp:410
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:359
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
Definition: StringRef.cpp:488
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:137
bool operator>(int64_t V1, const APSInt &V2)
Definition: APSInt.h:326
static constexpr StringLiteral withInnerNUL(const char(&Str)[N])
Definition: StringRef.h:888
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:116
LLVM_NODISCARD size_t find_last_of(char C, size_t From=npos) const
Find the last character in the string that is C, or npos if not found.
Definition: StringRef.h:420
LLVM_NODISCARD std::pair< StringRef, StringRef > rsplit(StringRef Separator) const
Split into two substrings around the last occurrence of a separator string.
Definition: StringRef.h:759
F(f)
LLVM_NODISCARD bool endswith_lower(StringRef Suffix) const
Check if this string ends with the given Suffix, ignoring case.
Definition: StringRef.cpp:52
LLVM_NODISCARD StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
Definition: StringRef.h:819
std::enable_if<!std::numeric_limits< T >::is_signed, bool >::type consumeInteger(unsigned Radix, T &Result)
Definition: StringRef.h:541
LLVM_NODISCARD unsigned edit_distance(StringRef Other, bool AllowReplacements=true, unsigned MaxEditDistance=0) const
Determine the edit distance between this string and another string.
Definition: StringRef.cpp:94
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:127
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef take_back(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with only the last N elements remaining.
Definition: StringRef.h:618
bool operator<=(int64_t V1, const APSInt &V2)
Definition: APSInt.h:323
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:278
bool operator>=(int64_t V1, const APSInt &V2)
Definition: APSInt.h:324
LLVM_NODISCARD StringRef copy(Allocator &A) const
Definition: StringRef.h:155
LLVM_NODISCARD size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:475
LLVM_ATTRIBUTE_ALWAYS_INLINE bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
Definition: StringRef.h:688
#define LLVM_ATTRIBUTE_ALWAYS_INLINE
LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do so, mark a method "always...
Definition: Compiler.h:213
iterator_range< const unsigned char * > bytes() const
Definition: StringRef.h:115
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef take_until(function_ref< bool(char)> F) const
Return the longest prefix of &#39;this&#39; such that no character in the prefix satisfies the given predicat...
Definition: StringRef.h:636
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:783
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:266
LLVM_ATTRIBUTE_ALWAYS_INLINE bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:677
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:530
LLVM_NODISCARD StringRef rtrim(StringRef Chars=" \\\) const
Return string with consecutive characters in Chars starting from the right removed.
Definition: StringRef.h:840
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:132
LLVM_NODISCARD std::pair< StringRef, StringRef > rsplit(char Separator) const
Split into two substrings around the last occurrence of a separator character.
Definition: StringRef.h:812
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:4430
StringRef()=default
Construct an empty string ref.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:597
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool contains_lower(char C) const
Return true if the given character is contained in *this, and false otherwise.
Definition: StringRef.h:467
LLVM_ATTRIBUTE_ALWAYS_INLINE constexpr StringRef(const char *data, size_t length)
Construct a string ref from a pointer and length.
Definition: StringRef.h:89
LLVM_NODISCARD std::string upper() const
Convert the given ASCII string to uppercase.
Definition: StringRef.cpp:115
#define P(N)
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:868
LLVM_NODISCARD StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Definition: StringRef.h:847
LLVM_NODISCARD size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition: StringRef.cpp:249
LLVM_NODISCARD size_t rfind_lower(char C, size_t From=npos) const
Search for the last character C in the string, ignoring case.
Definition: StringRef.cpp:192
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE int compare(StringRef RHS) const
compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less tha...
Definition: StringRef.h:183
LLVM_NODISCARD StringRef ltrim(StringRef Chars=" \\\) const
Return string with consecutive characters in Chars starting from the left removed.
Definition: StringRef.h:826
LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef(const std::string &Str)
Construct a string ref from an std::string.
Definition: StringRef.h:94
LLVM_NODISCARD char back() const
back - Get the last character in the string.
Definition: StringRef.h:148
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find_if_not(function_ref< bool(char)> F, size_t From=0) const
Search for the first character not satisfying the predicate F.
Definition: StringRef.h:336
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_back(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with the last N elements dropped.
Definition: StringRef.h:653
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:496
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef take_while(function_ref< bool(char)> F) const
Return the longest prefix of &#39;this&#39; such that every character in the prefix satisfies the given predi...
Definition: StringRef.h:628
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_front(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with the first N elements dropped.
Definition: StringRef.h:644
LLVM_NODISCARD int compare_lower(StringRef RHS) const
compare_lower - Compare two strings, ignoring case.
Definition: StringRef.cpp:37
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_until(function_ref< bool(char)> F) const
Return a StringRef equal to &#39;this&#39;, but with all characters not satisfying the given predicate droppe...
Definition: StringRef.h:670
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Basic Register Allocator
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool contains(char C) const
Return true if the given character is contained in *this, and false otherwise.
Definition: StringRef.h:453
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: ArrayRef.h:529
BlockVerifier::State From
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:726
bool getAsDouble(double &Result, bool AllowInexact=true) const
Parse the current string as an IEEE double-precision floating point value.
Definition: StringRef.cpp:583
std::enable_if< std::is_same< T, std::string >::value, StringRef >::type & operator=(T &&Str)=delete
Disallow accidental assignment from a temporary std::string.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:709
LLVM_NODISCARD size_t find_lower(char C, size_t From=0) const
Search for the first character C in the string, ignoring case.
Definition: StringRef.cpp:57
LLVM_NODISCARD size_t find_last_not_of(char C, size_t From=npos) const
Find the last character in the string that is not C, or npos if not found.
Definition: StringRef.cpp:290
A range adaptor for a pair of iterators.
Class for arbitrary precision integers.
Definition: APInt.h:69
An opaque object representing a hash code.
Definition: Hashing.h:71
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:168
iterator begin() const
Definition: StringRef.h:105
LLVM_NODISCARD int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: StringRef.cpp:63
constexpr StringLiteral(const char(&Str)[N])
Definition: StringRef.h:875
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:1968
const unsigned char * bytes_begin() const
Definition: StringRef.h:109
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:823
static const size_t npos
Definition: StringRef.h:50
LLVM_NODISCARD StringRef trim(StringRef Chars=" \\\) const
Return string with consecutive characters in Chars starting from the left and right removed...
Definition: StringRef.h:854
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:394
#define N
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
Definition: Compiler.h:128
LLVM_NODISCARD std::string lower() const
Definition: StringRef.cpp:107
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM_NODISCARD char front() const
front - Get the first character in the string.
Definition: StringRef.h:141
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:325
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool contains_lower(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:459
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_while(function_ref< bool(char)> F) const
Return a StringRef equal to &#39;this&#39;, but with all characters satisfying the given predicate dropped fr...
Definition: StringRef.h:662
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef take_front(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with only the first N elements remaining.
Definition: StringRef.h:607
LLVM_NODISCARD StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
Definition: StringRef.h:833
LLVM_NODISCARD std::pair< StringRef, StringRef > split(StringRef Separator) const
Split into two substrings around the first occurrence of a separator string.
Definition: StringRef.h:741
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1966
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:297
bool consumeSignedInteger(StringRef &Str, unsigned Radix, long long &Result)
Definition: StringRef.cpp:458
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1237
iterator end() const
Definition: StringRef.h:107
bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result)
Definition: StringRef.cpp:498
LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef(const char *Str)
Construct a string ref from a cstring.
Definition: StringRef.h:84