LLVM  10.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
65  static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
66  if (Length == 0) { return 0; }
67  return ::memcmp(Lhs,Rhs,Length);
68  }
69 
70  // Constexpr version of std::strlen.
71  static constexpr size_t strLen(const char *Str) {
72 #if __cplusplus > 201402L
73  return std::char_traits<char>::length(Str);
74 #elif __has_builtin(__builtin_strlen) || defined(__GNUC__)
75  return __builtin_strlen(Str);
76 #else
77  const char *Begin = Str;
78  while (*Str != '\0')
79  ++Str;
80  return Str - Begin;
81 #endif
82  }
83 
84  public:
85  /// @name Constructors
86  /// @{
87 
88  /// Construct an empty string ref.
89  /*implicit*/ StringRef() = default;
90 
91  /// Disable conversion from nullptr. This prevents things like
92  /// if (S == nullptr)
93  StringRef(std::nullptr_t) = delete;
94 
95  /// Construct a string ref from a cstring.
96  /*implicit*/ constexpr StringRef(const char *Str)
97  : Data(Str), Length(Str ? strLen(Str) : 0) {}
98 
99  /// Construct a string ref from a pointer and length.
100  /*implicit*/ constexpr StringRef(const char *data, size_t length)
101  : Data(data), Length(length) {}
102 
103  /// Construct a string ref from an std::string.
104  /*implicit*/ StringRef(const std::string &Str)
105  : Data(Str.data()), Length(Str.length()) {}
106 
107  static StringRef withNullAsEmpty(const char *data) {
108  return StringRef(data ? data : "");
109  }
110 
111  /// @}
112  /// @name Iterators
113  /// @{
114 
115  iterator begin() const { return Data; }
116 
117  iterator end() const { return Data + Length; }
118 
119  const unsigned char *bytes_begin() const {
120  return reinterpret_cast<const unsigned char *>(begin());
121  }
122  const unsigned char *bytes_end() const {
123  return reinterpret_cast<const unsigned char *>(end());
124  }
126  return make_range(bytes_begin(), bytes_end());
127  }
128 
129  /// @}
130  /// @name String Operations
131  /// @{
132 
133  /// data - Get a pointer to the start of the string (which may not be null
134  /// terminated).
136  const char *data() const { return Data; }
137 
138  /// empty - Check if the string is empty.
140  bool empty() const { return Length == 0; }
141 
142  /// size - Get the string size.
144  size_t size() const { return Length; }
145 
146  /// front - Get the first character in the string.
148  char front() const {
149  assert(!empty());
150  return Data[0];
151  }
152 
153  /// back - Get the last character in the string.
155  char back() const {
156  assert(!empty());
157  return Data[Length-1];
158  }
159 
160  // copy - Allocate copy in Allocator and return StringRef to it.
161  template <typename Allocator>
163  // Don't request a length 0 copy from the allocator.
164  if (empty())
165  return StringRef();
166  char *S = A.template Allocate<char>(Length);
167  std::copy(begin(), end(), S);
168  return StringRef(S, Length);
169  }
170 
171  /// equals - Check for string equality, this is more efficient than
172  /// compare() when the relative ordering of inequal strings isn't needed.
174  bool equals(StringRef RHS) const {
175  return (Length == RHS.Length &&
176  compareMemory(Data, RHS.Data, RHS.Length) == 0);
177  }
178 
179  /// equals_lower - Check for string equality, ignoring case.
181  bool equals_lower(StringRef RHS) const {
182  return Length == RHS.Length && compare_lower(RHS) == 0;
183  }
184 
185  /// compare - Compare two strings; the result is -1, 0, or 1 if this string
186  /// is lexicographically less than, equal to, or greater than the \p RHS.
188  int compare(StringRef RHS) const {
189  // Check the prefix for a mismatch.
190  if (int Res = compareMemory(Data, RHS.Data, std::min(Length, RHS.Length)))
191  return Res < 0 ? -1 : 1;
192 
193  // Otherwise the prefixes match, so we only need to check the lengths.
194  if (Length == RHS.Length)
195  return 0;
196  return Length < RHS.Length ? -1 : 1;
197  }
198 
199  /// compare_lower - Compare two strings, ignoring case.
201  int compare_lower(StringRef RHS) const;
202 
203  /// compare_numeric - Compare two strings, treating sequences of digits as
204  /// numbers.
206  int compare_numeric(StringRef RHS) const;
207 
208  /// Determine the edit distance between this string and another
209  /// string.
210  ///
211  /// \param Other the string to compare this string against.
212  ///
213  /// \param AllowReplacements whether to allow character
214  /// replacements (change one character into another) as a single
215  /// operation, rather than as two operations (an insertion and a
216  /// removal).
217  ///
218  /// \param MaxEditDistance If non-zero, the maximum edit distance that
219  /// this routine is allowed to compute. If the edit distance will exceed
220  /// that maximum, returns \c MaxEditDistance+1.
221  ///
222  /// \returns the minimum number of character insertions, removals,
223  /// or (if \p AllowReplacements is \c true) replacements needed to
224  /// transform one of the given strings into the other. If zero,
225  /// the strings are identical.
227  unsigned edit_distance(StringRef Other, bool AllowReplacements = true,
228  unsigned MaxEditDistance = 0) const;
229 
230  /// str - Get the contents as an std::string.
232  std::string str() const {
233  if (!Data) return std::string();
234  return std::string(Data, Length);
235  }
236 
237  /// @}
238  /// @name Operator Overloads
239  /// @{
240 
242  char operator[](size_t Index) const {
243  assert(Index < Length && "Invalid index!");
244  return Data[Index];
245  }
246 
247  /// Disallow accidental assignment from a temporary std::string.
248  ///
249  /// The declaration here is extra complicated so that `stringRef = {}`
250  /// and `stringRef = "abc"` continue to select the move assignment operator.
251  template <typename T>
252  typename std::enable_if<std::is_same<T, std::string>::value,
253  StringRef>::type &
254  operator=(T &&Str) = delete;
255 
256  /// @}
257  /// @name Type Conversions
258  /// @{
259 
260  operator std::string() const {
261  return str();
262  }
263 
264  /// @}
265  /// @name String Predicates
266  /// @{
267 
268  /// Check if this string starts with the given \p Prefix.
271  return Length >= Prefix.Length &&
272  compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
273  }
274 
275  /// Check if this string starts with the given \p Prefix, ignoring case.
277  bool startswith_lower(StringRef Prefix) const;
278 
279  /// Check if this string ends with the given \p Suffix.
281  bool endswith(StringRef Suffix) const {
282  return Length >= Suffix.Length &&
283  compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
284  }
285 
286  /// Check if this string ends with the given \p Suffix, ignoring case.
288  bool endswith_lower(StringRef Suffix) const;
289 
290  /// @}
291  /// @name String Searching
292  /// @{
293 
294  /// Search for the first character \p C in the string.
295  ///
296  /// \returns The index of the first occurrence of \p C, or npos if not
297  /// found.
299  size_t find(char C, size_t From = 0) const {
300  size_t FindBegin = std::min(From, Length);
301  if (FindBegin < Length) { // Avoid calling memchr with nullptr.
302  // Just forward to memchr, which is faster than a hand-rolled loop.
303  if (const void *P = ::memchr(Data + FindBegin, C, Length - FindBegin))
304  return static_cast<const char *>(P) - Data;
305  }
306  return npos;
307  }
308 
309  /// Search for the first character \p C in the string, ignoring case.
310  ///
311  /// \returns The index of the first occurrence of \p C, or npos if not
312  /// found.
314  size_t find_lower(char C, size_t From = 0) const;
315 
316  /// Search for the first character satisfying the predicate \p F
317  ///
318  /// \returns The index of the first character satisfying \p F starting from
319  /// \p From, or npos if not found.
321  size_t find_if(function_ref<bool(char)> F, size_t From = 0) const {
323  while (!S.empty()) {
324  if (F(S.front()))
325  return size() - S.size();
326  S = S.drop_front();
327  }
328  return npos;
329  }
330 
331  /// Search for the first character not satisfying the predicate \p F
332  ///
333  /// \returns The index of the first character not satisfying \p F starting
334  /// 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.
446  bool contains(StringRef Other) const { return find(Other) != npos; }
447 
448  /// Return true if the given character is contained in *this, and false
449  /// otherwise.
451  bool contains(char C) const { return find_first_of(C) != npos; }
452 
453  /// Return true if the given string is a substring of *this, and false
454  /// otherwise.
456  bool contains_lower(StringRef Other) const {
457  return find_lower(Other) != npos;
458  }
459 
460  /// Return true if the given character is contained in *this, and false
461  /// otherwise.
463  bool contains_lower(char C) const { return find_lower(C) != npos; }
464 
465  /// @}
466  /// @name Helpful Algorithms
467  /// @{
468 
469  /// Return the number of occurrences of \p C in the string.
471  size_t count(char C) const {
472  size_t Count = 0;
473  for (size_t i = 0, e = Length; i != e; ++i)
474  if (Data[i] == C)
475  ++Count;
476  return Count;
477  }
478 
479  /// Return the number of non-overlapped occurrences of \p Str in
480  /// the string.
481  size_t count(StringRef Str) const;
482 
483  /// Parse the current string as an integer of the specified radix. If
484  /// \p Radix is specified as zero, this does radix autosensing using
485  /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
486  ///
487  /// If the string is invalid or if only a subset of the string is valid,
488  /// this returns true to signify the error. The string is considered
489  /// erroneous if empty or if it overflows T.
490  template <typename T>
491  typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type
492  getAsInteger(unsigned Radix, T &Result) const {
493  long long LLVal;
494  if (getAsSignedInteger(*this, Radix, LLVal) ||
495  static_cast<T>(LLVal) != LLVal)
496  return true;
497  Result = LLVal;
498  return false;
499  }
500 
501  template <typename T>
502  typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type
503  getAsInteger(unsigned Radix, T &Result) const {
504  unsigned long long ULLVal;
505  // The additional cast to unsigned long long is required to avoid the
506  // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
507  // 'unsigned __int64' when instantiating getAsInteger with T = bool.
508  if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
509  static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
510  return true;
511  Result = ULLVal;
512  return false;
513  }
514 
515  /// Parse the current string as an integer of the specified radix. If
516  /// \p Radix is specified as zero, this does radix autosensing using
517  /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
518  ///
519  /// If the string does not begin with a number of the specified radix,
520  /// this returns true to signify the error. The string is considered
521  /// erroneous if empty or if it overflows T.
522  /// The portion of the string representing the discovered numeric value
523  /// is removed from the beginning of the string.
524  template <typename T>
525  typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type
526  consumeInteger(unsigned Radix, T &Result) {
527  long long LLVal;
528  if (consumeSignedInteger(*this, Radix, LLVal) ||
529  static_cast<long long>(static_cast<T>(LLVal)) != LLVal)
530  return true;
531  Result = LLVal;
532  return false;
533  }
534 
535  template <typename T>
536  typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type
537  consumeInteger(unsigned Radix, T &Result) {
538  unsigned long long ULLVal;
539  if (consumeUnsignedInteger(*this, Radix, ULLVal) ||
540  static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
541  return true;
542  Result = ULLVal;
543  return false;
544  }
545 
546  /// Parse the current string as an integer of the specified \p Radix, or of
547  /// an autosensed radix if the \p Radix given is 0. The current value in
548  /// \p Result is discarded, and the storage is changed to be wide enough to
549  /// store the parsed integer.
550  ///
551  /// \returns true if the string does not solely consist of a valid
552  /// non-empty number in the appropriate base.
553  ///
554  /// APInt::fromString is superficially similar but assumes the
555  /// string is well-formed in the given radix.
556  bool getAsInteger(unsigned Radix, APInt &Result) const;
557 
558  /// Parse the current string as an IEEE double-precision floating
559  /// point value. The string must be a well-formed double.
560  ///
561  /// If \p AllowInexact is false, the function will fail if the string
562  /// cannot be represented exactly. Otherwise, the function only fails
563  /// in case of an overflow or underflow.
564  bool getAsDouble(double &Result, bool AllowInexact = true) const;
565 
566  /// @}
567  /// @name String Operations
568  /// @{
569 
570  // Convert the given ASCII string to lowercase.
572  std::string lower() const;
573 
574  /// Convert the given ASCII string to uppercase.
576  std::string upper() const;
577 
578  /// @}
579  /// @name Substring Operations
580  /// @{
581 
582  /// Return a reference to the substring from [Start, Start + N).
583  ///
584  /// \param Start The index of the starting character in the substring; if
585  /// the index is npos or greater than the length of the string then the
586  /// empty substring will be returned.
587  ///
588  /// \param N The number of characters to included in the substring. If N
589  /// exceeds the number of characters remaining in the string, the string
590  /// suffix (starting with \p Start) will be returned.
592  StringRef substr(size_t Start, size_t N = npos) const {
593  Start = std::min(Start, Length);
594  return StringRef(Data + Start, std::min(N, Length - Start));
595  }
596 
597  /// Return a StringRef equal to 'this' but with only the first \p N
598  /// elements remaining. If \p N is greater than the length of the
599  /// string, the entire string is returned.
601  StringRef take_front(size_t N = 1) const {
602  if (N >= size())
603  return *this;
604  return drop_back(size() - N);
605  }
606 
607  /// Return a StringRef equal to 'this' but with only the last \p N
608  /// elements remaining. If \p N is greater than the length of the
609  /// string, the entire string is returned.
611  StringRef take_back(size_t N = 1) const {
612  if (N >= size())
613  return *this;
614  return drop_front(size() - N);
615  }
616 
617  /// Return the longest prefix of 'this' such that every character
618  /// in the prefix satisfies the given predicate.
620  StringRef take_while(function_ref<bool(char)> F) const {
621  return substr(0, find_if_not(F));
622  }
623 
624  /// Return the longest prefix of 'this' such that no character in
625  /// the prefix satisfies the given predicate.
627  StringRef take_until(function_ref<bool(char)> F) const {
628  return substr(0, find_if(F));
629  }
630 
631  /// Return a StringRef equal to 'this' but with the first \p N elements
632  /// dropped.
634  StringRef drop_front(size_t N = 1) const {
635  assert(size() >= N && "Dropping more elements than exist");
636  return substr(N);
637  }
638 
639  /// Return a StringRef equal to 'this' but with the last \p N elements
640  /// dropped.
642  StringRef drop_back(size_t N = 1) const {
643  assert(size() >= N && "Dropping more elements than exist");
644  return substr(0, size()-N);
645  }
646 
647  /// Return a StringRef equal to 'this', but with all characters satisfying
648  /// the given predicate dropped from the beginning of the string.
650  StringRef drop_while(function_ref<bool(char)> F) const {
651  return substr(find_if_not(F));
652  }
653 
654  /// Return a StringRef equal to 'this', but with all characters not
655  /// satisfying the given predicate dropped from the beginning of the string.
657  StringRef drop_until(function_ref<bool(char)> F) const {
658  return substr(find_if(F));
659  }
660 
661  /// Returns true if this StringRef has the given prefix and removes that
662  /// prefix.
663  bool consume_front(StringRef Prefix) {
664  if (!startswith(Prefix))
665  return false;
666 
667  *this = drop_front(Prefix.size());
668  return true;
669  }
670 
671  /// Returns true if this StringRef has the given suffix and removes that
672  /// suffix.
673  bool consume_back(StringRef Suffix) {
674  if (!endswith(Suffix))
675  return false;
676 
677  *this = drop_back(Suffix.size());
678  return true;
679  }
680 
681  /// Return a reference to the substring from [Start, End).
682  ///
683  /// \param Start The index of the starting character in the substring; if
684  /// the index is npos or greater than the length of the string then the
685  /// empty substring will be returned.
686  ///
687  /// \param End The index following the last character to include in the
688  /// substring. If this is npos or exceeds the number of characters
689  /// remaining in the string, the string suffix (starting with \p Start)
690  /// will be returned. If this is less than \p Start, an empty string will
691  /// be returned.
693  StringRef slice(size_t Start, size_t End) const {
694  Start = std::min(Start, Length);
695  End = std::min(std::max(Start, End), Length);
696  return StringRef(Data + Start, End - Start);
697  }
698 
699  /// Split into two substrings around the first occurrence of a separator
700  /// character.
701  ///
702  /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
703  /// such that (*this == LHS + Separator + RHS) is true and RHS is
704  /// maximal. If \p Separator is not in the string, then the result is a
705  /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
706  ///
707  /// \param Separator The character to split on.
708  /// \returns The split substrings.
710  std::pair<StringRef, StringRef> split(char Separator) const {
711  return split(StringRef(&Separator, 1));
712  }
713 
714  /// Split into two substrings around the first occurrence of a separator
715  /// string.
716  ///
717  /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
718  /// such that (*this == LHS + Separator + RHS) is true and RHS is
719  /// maximal. If \p Separator is not in the string, then the result is a
720  /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
721  ///
722  /// \param Separator - The string to split on.
723  /// \return - The split substrings.
725  std::pair<StringRef, StringRef> split(StringRef Separator) const {
726  size_t Idx = find(Separator);
727  if (Idx == npos)
728  return std::make_pair(*this, StringRef());
729  return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
730  }
731 
732  /// Split into two substrings around the last occurrence of a separator
733  /// string.
734  ///
735  /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
736  /// such that (*this == LHS + Separator + RHS) is true and RHS is
737  /// minimal. If \p Separator is not in the string, then the result is a
738  /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
739  ///
740  /// \param Separator - The string to split on.
741  /// \return - The split substrings.
743  std::pair<StringRef, StringRef> rsplit(StringRef Separator) const {
744  size_t Idx = rfind(Separator);
745  if (Idx == npos)
746  return std::make_pair(*this, StringRef());
747  return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
748  }
749 
750  /// Split into substrings around the occurrences of a separator string.
751  ///
752  /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
753  /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
754  /// elements are added to A.
755  /// If \p KeepEmpty is false, empty strings are not added to \p A. They
756  /// still count when considering \p MaxSplit
757  /// An useful invariant is that
758  /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
759  ///
760  /// \param A - Where to put the substrings.
761  /// \param Separator - The string to split on.
762  /// \param MaxSplit - The maximum number of times the string is split.
763  /// \param KeepEmpty - True if empty substring should be added.
765  StringRef Separator, int MaxSplit = -1,
766  bool KeepEmpty = true) const;
767 
768  /// Split into substrings around the occurrences of a separator character.
769  ///
770  /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
771  /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
772  /// elements are added to A.
773  /// If \p KeepEmpty is false, empty strings are not added to \p A. They
774  /// still count when considering \p MaxSplit
775  /// An useful invariant is that
776  /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
777  ///
778  /// \param A - Where to put the substrings.
779  /// \param Separator - The string to split on.
780  /// \param MaxSplit - The maximum number of times the string is split.
781  /// \param KeepEmpty - True if empty substring should be added.
782  void split(SmallVectorImpl<StringRef> &A, char Separator, int MaxSplit = -1,
783  bool KeepEmpty = true) const;
784 
785  /// Split into two substrings around the last occurrence of a separator
786  /// character.
787  ///
788  /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
789  /// such that (*this == LHS + Separator + RHS) is true and RHS is
790  /// minimal. If \p Separator is not in the string, then the result is a
791  /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
792  ///
793  /// \param Separator - The character to split on.
794  /// \return - The split substrings.
796  std::pair<StringRef, StringRef> rsplit(char Separator) const {
797  return rsplit(StringRef(&Separator, 1));
798  }
799 
800  /// Return string with consecutive \p Char characters starting from the
801  /// the left removed.
803  StringRef ltrim(char Char) const {
804  return drop_front(std::min(Length, find_first_not_of(Char)));
805  }
806 
807  /// Return string with consecutive characters in \p Chars starting from
808  /// the left removed.
810  StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
811  return drop_front(std::min(Length, find_first_not_of(Chars)));
812  }
813 
814  /// Return string with consecutive \p Char characters starting from the
815  /// right removed.
817  StringRef rtrim(char Char) const {
818  return drop_back(Length - std::min(Length, find_last_not_of(Char) + 1));
819  }
820 
821  /// Return string with consecutive characters in \p Chars starting from
822  /// the right removed.
824  StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {
825  return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1));
826  }
827 
828  /// Return string with consecutive \p Char characters starting from the
829  /// left and right removed.
831  StringRef trim(char Char) const {
832  return ltrim(Char).rtrim(Char);
833  }
834 
835  /// Return string with consecutive characters in \p Chars starting from
836  /// the left and right removed.
838  StringRef trim(StringRef Chars = " \t\n\v\f\r") const {
839  return ltrim(Chars).rtrim(Chars);
840  }
841 
842  /// @}
843  };
844 
845  /// A wrapper around a string literal that serves as a proxy for constructing
846  /// global tables of StringRefs with the length computed at compile time.
847  /// In order to avoid the invocation of a global constructor, StringLiteral
848  /// should *only* be used in a constexpr context, as such:
849  ///
850  /// constexpr StringLiteral S("test");
851  ///
852  class StringLiteral : public StringRef {
853  private:
854  constexpr StringLiteral(const char *Str, size_t N) : StringRef(Str, N) {
855  }
856 
857  public:
858  template <size_t N>
859  constexpr StringLiteral(const char (&Str)[N])
860 #if defined(__clang__) && __has_attribute(enable_if)
861 #pragma clang diagnostic push
862 #pragma clang diagnostic ignored "-Wgcc-compat"
863  __attribute((enable_if(__builtin_strlen(Str) == N - 1,
864  "invalid string literal")))
865 #pragma clang diagnostic pop
866 #endif
867  : StringRef(Str, N - 1) {
868  }
869 
870  // Explicit construction for strings like "foo\0bar".
871  template <size_t N>
872  static constexpr StringLiteral withInnerNUL(const char (&Str)[N]) {
873  return StringLiteral(Str, N - 1);
874  }
875  };
876 
877  /// @name StringRef Comparison Operators
878  /// @{
879 
880  inline bool operator==(StringRef LHS, StringRef RHS) {
881  return LHS.equals(RHS);
882  }
883 
884  inline bool operator!=(StringRef LHS, StringRef RHS) { return !(LHS == RHS); }
885 
886  inline bool operator<(StringRef LHS, StringRef RHS) {
887  return LHS.compare(RHS) == -1;
888  }
889 
890  inline bool operator<=(StringRef LHS, StringRef RHS) {
891  return LHS.compare(RHS) != 1;
892  }
893 
894  inline bool operator>(StringRef LHS, StringRef RHS) {
895  return LHS.compare(RHS) == 1;
896  }
897 
898  inline bool operator>=(StringRef LHS, StringRef RHS) {
899  return LHS.compare(RHS) != -1;
900  }
901 
902  inline std::string &operator+=(std::string &buffer, StringRef string) {
903  return buffer.append(string.data(), string.size());
904  }
905 
906  /// @}
907 
908  /// Compute a hash_code for a StringRef.
911 
912 } // end namespace llvm
913 
914 #endif // LLVM_ADT_STRINGREF_H
uint64_t CallInst * C
std::string & operator+=(std::string &buffer, StringRef string)
Definition: StringRef.h:902
std::enable_if<!std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Definition: StringRef.h:503
static StringRef withNullAsEmpty(const char *data)
Definition: StringRef.h:107
LLVM_NODISCARD 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:601
LLVM_NODISCARD char operator[](size_t Index) const
Definition: StringRef.h:242
LLVM_NODISCARD bool startswith_lower(StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
Definition: StringRef.cpp:46
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:232
LLVM_NODISCARD bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:281
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const unsigned char * bytes_end() const
Definition: StringRef.h:122
LLVM_NODISCARD 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:627
LLVM_NODISCARD bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
Definition: StringRef.h:181
bool consumeUnsignedInteger(StringRef &Str, unsigned Radix, unsigned long long &Result)
Definition: StringRef.cpp:410
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:270
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 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:188
bool operator>(int64_t V1, const APSInt &V2)
Definition: APSInt.h:344
static constexpr StringLiteral withInnerNUL(const char(&Str)[N])
Definition: StringRef.h:872
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:663
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:953
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:104
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:743
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:803
std::enable_if<!std::numeric_limits< T >::is_signed, bool >::type consumeInteger(unsigned Radix, T &Result)
Definition: StringRef.h:537
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
bool operator<=(int64_t V1, const APSInt &V2)
Definition: APSInt.h:341
LLVM_NODISCARD bool contains(char C) const
Return true if the given character is contained in *this, and false otherwise.
Definition: StringRef.h:451
bool operator>=(int64_t V1, const APSInt &V2)
Definition: APSInt.h:342
LLVM_NODISCARD 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:634
LLVM_NODISCARD StringRef copy(Allocator &A) const
Definition: StringRef.h:162
LLVM_NODISCARD size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:471
LLVM_NODISCARD 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:611
LLVM_NODISCARD StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:693
iterator_range< const unsigned char * > bytes() const
Definition: StringRef.h:125
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:592
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
LLVM_NODISCARD 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:620
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:526
LLVM_NODISCARD StringRef rtrim(StringRef Chars=" \\\) const
Return string with consecutive characters in Chars starting from the right removed.
Definition: StringRef.h:824
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:796
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:4470
StringRef()=default
Construct an empty string ref.
LLVM_NODISCARD std::string upper() const
Convert the given ASCII string to uppercase.
Definition: StringRef.cpp:115
LLVM_NODISCARD 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:657
#define P(N)
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:852
LLVM_NODISCARD StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Definition: StringRef.h:831
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 find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:299
LLVM_NODISCARD bool contains_lower(char C) const
Return true if the given character is contained in *this, and false otherwise.
Definition: StringRef.h:463
constexpr double e
Definition: MathExtras.h:57
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 bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:446
LLVM_NODISCARD 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 StringRef ltrim(StringRef Chars=" \\\) const
Return string with consecutive characters in Chars starting from the left removed.
Definition: StringRef.h:810
LLVM_NODISCARD char back() const
back - Get the last character in the string.
Definition: StringRef.h:155
StringRef(const std::string &Str)
Construct a string ref from an std::string.
Definition: StringRef.h:104
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:492
LLVM_NODISCARD int compare_lower(StringRef RHS) const
compare_lower - Compare two strings, ignoring case.
Definition: StringRef.cpp:37
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Basic Register Allocator
BlockVerifier::State From
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:390
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:710
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 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:70
iterator begin() const
Definition: StringRef.h:115
LLVM_NODISCARD int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: StringRef.cpp:63
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:174
constexpr StringLiteral(const char(&Str)[N])
Definition: StringRef.h:859
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:1977
const unsigned char * bytes_begin() const
Definition: StringRef.h:119
constexpr StringRef(const char *Str)
Construct a string ref from a cstring.
Definition: StringRef.h:96
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:927
static const size_t npos
Definition: StringRef.h:50
LLVM_NODISCARD 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:650
LLVM_NODISCARD StringRef trim(StringRef Chars=" \\\) const
Return string with consecutive characters in Chars starting from the left and right removed...
Definition: StringRef.h:838
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
LLVM_NODISCARD bool contains_lower(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:456
LLVM_NODISCARD 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:321
bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
Definition: StringRef.h:673
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
Definition: Compiler.h:153
LLVM_NODISCARD std::string lower() const
Definition: StringRef.cpp:107
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:136
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM_NODISCARD char front() const
front - Get the first character in the string.
Definition: StringRef.h:148
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:343
LLVM_NODISCARD 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:642
constexpr StringRef(const char *data, size_t length)
Construct a string ref from a pointer and length.
Definition: StringRef.h:100
LLVM_NODISCARD StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
Definition: StringRef.h:817
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:725
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:1975
bool consumeSignedInteger(StringRef &Str, unsigned Radix, long long &Result)
Definition: StringRef.cpp:458
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1217
iterator end() const
Definition: StringRef.h:117
bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result)
Definition: StringRef.cpp:498