1 //===- StringRef.h - Constant String Reference Wrapper ----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_ADT_STRINGREF_H
10 #define LLVM_ADT_STRINGREF_H
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/ADT/iterator_range.h"
14 #include "llvm/Support/Compiler.h"
21 #if __cplusplus > 201402L
22 #include <string_view>
24 #include <type_traits>
27 // Declare the __builtin_strlen intrinsic for MSVC so it can be used in
30 extern "C" size_t __builtin_strlen(const char *);
37 template <typename T> class SmallVectorImpl;
40 /// Helper functions for StringRef::getAsInteger.
41 bool getAsUnsignedInteger(StringRef Str, unsigned Radix,
42 unsigned long long &Result);
44 bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result);
46 bool consumeUnsignedInteger(StringRef &Str, unsigned Radix,
47 unsigned long long &Result);
48 bool consumeSignedInteger(StringRef &Str, unsigned Radix, long long &Result);
50 /// StringRef - Represent a constant reference to a string, i.e. a character
51 /// array and a length, which need not be null terminated.
53 /// This class does not own the string data, it is expected to be used in
54 /// situations where the character data resides in some other buffer, whose
55 /// lifetime extends past that of the StringRef. For this reason, it is not in
56 /// general safe to store a StringRef.
57 class LLVM_GSL_POINTER StringRef {
59 static constexpr size_t npos = ~size_t(0);
61 using iterator = const char *;
62 using const_iterator = const char *;
63 using size_type = size_t;
66 /// The start of the string, in an external buffer.
67 const char *Data = nullptr;
69 /// The length of the string.
72 // Workaround memcmp issue with null pointers (undefined behavior)
73 // by providing a specialized version
74 static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
75 if (Length == 0) { return 0; }
76 return ::memcmp(Lhs,Rhs,Length);
79 // Constexpr version of std::strlen.
80 static constexpr size_t strLen(const char *Str) {
81 #if __cplusplus > 201402L
82 return std::char_traits<char>::length(Str);
83 #elif __has_builtin(__builtin_strlen) || defined(__GNUC__) || \
84 (defined(_MSC_VER) && _MSC_VER >= 1916)
85 return __builtin_strlen(Str);
87 const char *Begin = Str;
95 /// @name Constructors
98 /// Construct an empty string ref.
99 /*implicit*/ StringRef() = default;
101 /// Disable conversion from nullptr. This prevents things like
102 /// if (S == nullptr)
103 StringRef(std::nullptr_t) = delete;
105 /// Construct a string ref from a cstring.
106 /*implicit*/ constexpr StringRef(const char *Str)
107 : Data(Str), Length(Str ? strLen(Str) : 0) {}
109 /// Construct a string ref from a pointer and length.
110 /*implicit*/ constexpr StringRef(const char *data, size_t length)
111 : Data(data), Length(length) {}
113 /// Construct a string ref from an std::string.
114 /*implicit*/ StringRef(const std::string &Str)
115 : Data(Str.data()), Length(Str.length()) {}
117 #if __cplusplus > 201402L
118 /// Construct a string ref from an std::string_view.
119 /*implicit*/ constexpr StringRef(std::string_view Str)
120 : Data(Str.data()), Length(Str.size()) {}
123 static StringRef withNullAsEmpty(const char *data) {
124 return StringRef(data ? data : "");
131 iterator begin() const { return Data; }
133 iterator end() const { return Data + Length; }
135 const unsigned char *bytes_begin() const {
136 return reinterpret_cast<const unsigned char *>(begin());
138 const unsigned char *bytes_end() const {
139 return reinterpret_cast<const unsigned char *>(end());
141 iterator_range<const unsigned char *> bytes() const {
142 return make_range(bytes_begin(), bytes_end());
146 /// @name String Operations
149 /// data - Get a pointer to the start of the string (which may not be null
152 const char *data() const { return Data; }
154 /// empty - Check if the string is empty.
156 bool empty() const { return Length == 0; }
158 /// size - Get the string size.
160 size_t size() const { return Length; }
162 /// front - Get the first character in the string.
169 /// back - Get the last character in the string.
173 return Data[Length-1];
176 // copy - Allocate copy in Allocator and return StringRef to it.
177 template <typename Allocator>
178 LLVM_NODISCARD StringRef copy(Allocator &A) const {
179 // Don't request a length 0 copy from the allocator.
182 char *S = A.template Allocate<char>(Length);
183 std::copy(begin(), end(), S);
184 return StringRef(S, Length);
187 /// equals - Check for string equality, this is more efficient than
188 /// compare() when the relative ordering of inequal strings isn't needed.
190 bool equals(StringRef RHS) const {
191 return (Length == RHS.Length &&
192 compareMemory(Data, RHS.Data, RHS.Length) == 0);
195 /// equals_lower - Check for string equality, ignoring case.
197 bool equals_lower(StringRef RHS) const {
198 return Length == RHS.Length && compare_lower(RHS) == 0;
201 /// compare - Compare two strings; the result is -1, 0, or 1 if this string
202 /// is lexicographically less than, equal to, or greater than the \p RHS.
204 int compare(StringRef RHS) const {
205 // Check the prefix for a mismatch.
206 if (int Res = compareMemory(Data, RHS.Data, std::min(Length, RHS.Length)))
207 return Res < 0 ? -1 : 1;
209 // Otherwise the prefixes match, so we only need to check the lengths.
210 if (Length == RHS.Length)
212 return Length < RHS.Length ? -1 : 1;
215 /// compare_lower - Compare two strings, ignoring case.
217 int compare_lower(StringRef RHS) const;
219 /// compare_numeric - Compare two strings, treating sequences of digits as
222 int compare_numeric(StringRef RHS) const;
224 /// Determine the edit distance between this string and another
227 /// \param Other the string to compare this string against.
229 /// \param AllowReplacements whether to allow character
230 /// replacements (change one character into another) as a single
231 /// operation, rather than as two operations (an insertion and a
234 /// \param MaxEditDistance If non-zero, the maximum edit distance that
235 /// this routine is allowed to compute. If the edit distance will exceed
236 /// that maximum, returns \c MaxEditDistance+1.
238 /// \returns the minimum number of character insertions, removals,
239 /// or (if \p AllowReplacements is \c true) replacements needed to
240 /// transform one of the given strings into the other. If zero,
241 /// the strings are identical.
243 unsigned edit_distance(StringRef Other, bool AllowReplacements = true,
244 unsigned MaxEditDistance = 0) const;
246 /// str - Get the contents as an std::string.
248 std::string str() const {
249 if (!Data) return std::string();
250 return std::string(Data, Length);
254 /// @name Operator Overloads
258 char operator[](size_t Index) const {
259 assert(Index < Length && "Invalid index!");
263 /// Disallow accidental assignment from a temporary std::string.
265 /// The declaration here is extra complicated so that `stringRef = {}`
266 /// and `stringRef = "abc"` continue to select the move assignment operator.
267 template <typename T>
268 std::enable_if_t<std::is_same<T, std::string>::value, StringRef> &
269 operator=(T &&Str) = delete;
272 /// @name Type Conversions
275 explicit operator std::string() const { return str(); }
277 #if __cplusplus > 201402L
278 operator std::string_view() const {
279 return std::string_view(data(), size());
284 /// @name String Predicates
287 /// Check if this string starts with the given \p Prefix.
289 bool startswith(StringRef Prefix) const {
290 return Length >= Prefix.Length &&
291 compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
294 /// Check if this string starts with the given \p Prefix, ignoring case.
296 bool startswith_lower(StringRef Prefix) const;
298 /// Check if this string ends with the given \p Suffix.
300 bool endswith(StringRef Suffix) const {
301 return Length >= Suffix.Length &&
302 compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
305 /// Check if this string ends with the given \p Suffix, ignoring case.
307 bool endswith_lower(StringRef Suffix) const;
310 /// @name String Searching
313 /// Search for the first character \p C in the string.
315 /// \returns The index of the first occurrence of \p C, or npos if not
318 size_t find(char C, size_t From = 0) const {
319 size_t FindBegin = std::min(From, Length);
320 if (FindBegin < Length) { // Avoid calling memchr with nullptr.
321 // Just forward to memchr, which is faster than a hand-rolled loop.
322 if (const void *P = ::memchr(Data + FindBegin, C, Length - FindBegin))
323 return static_cast<const char *>(P) - Data;
328 /// Search for the first character \p C in the string, ignoring case.
330 /// \returns The index of the first occurrence of \p C, or npos if not
333 size_t find_lower(char C, size_t From = 0) const;
335 /// Search for the first character satisfying the predicate \p F
337 /// \returns The index of the first character satisfying \p F starting from
338 /// \p From, or npos if not found.
340 size_t find_if(function_ref<bool(char)> F, size_t From = 0) const {
341 StringRef S = drop_front(From);
344 return size() - S.size();
350 /// Search for the first character not satisfying the predicate \p F
352 /// \returns The index of the first character not satisfying \p F starting
353 /// from \p From, or npos if not found.
355 size_t find_if_not(function_ref<bool(char)> F, size_t From = 0) const {
356 return find_if([F](char c) { return !F(c); }, From);
359 /// Search for the first string \p Str in the string.
361 /// \returns The index of the first occurrence of \p Str, or npos if not
364 size_t find(StringRef Str, size_t From = 0) const;
366 /// Search for the first string \p Str in the string, ignoring case.
368 /// \returns The index of the first occurrence of \p Str, or npos if not
371 size_t find_lower(StringRef Str, size_t From = 0) const;
373 /// Search for the last character \p C in the string.
375 /// \returns The index of the last occurrence of \p C, or npos if not
378 size_t rfind(char C, size_t From = npos) const {
379 From = std::min(From, Length);
389 /// Search for the last character \p C in the string, ignoring case.
391 /// \returns The index of the last occurrence of \p C, or npos if not
394 size_t rfind_lower(char C, size_t From = npos) const;
396 /// Search for the last string \p Str in the string.
398 /// \returns The index of the last occurrence of \p Str, or npos if not
401 size_t rfind(StringRef Str) const;
403 /// Search for the last string \p Str in the string, ignoring case.
405 /// \returns The index of the last occurrence of \p Str, or npos if not
408 size_t rfind_lower(StringRef Str) const;
410 /// Find the first character in the string that is \p C, or npos if not
411 /// found. Same as find.
413 size_t find_first_of(char C, size_t From = 0) const {
414 return find(C, From);
417 /// Find the first character in the string that is in \p Chars, or npos if
420 /// Complexity: O(size() + Chars.size())
422 size_t find_first_of(StringRef Chars, size_t From = 0) const;
424 /// Find the first character in the string that is not \p C or npos if not
427 size_t find_first_not_of(char C, size_t From = 0) const;
429 /// Find the first character in the string that is not in the string
430 /// \p Chars, or npos if not found.
432 /// Complexity: O(size() + Chars.size())
434 size_t find_first_not_of(StringRef Chars, size_t From = 0) const;
436 /// Find the last character in the string that is \p C, or npos if not
439 size_t find_last_of(char C, size_t From = npos) const {
440 return rfind(C, From);
443 /// Find the last character in the string that is in \p C, or npos if not
446 /// Complexity: O(size() + Chars.size())
448 size_t find_last_of(StringRef Chars, size_t From = npos) const;
450 /// Find the last character in the string that is not \p C, or npos if not
453 size_t find_last_not_of(char C, size_t From = npos) const;
455 /// Find the last character in the string that is not in \p Chars, or
456 /// npos if not found.
458 /// Complexity: O(size() + Chars.size())
460 size_t find_last_not_of(StringRef Chars, size_t From = npos) const;
462 /// Return true if the given string is a substring of *this, and false
465 bool contains(StringRef Other) const { return find(Other) != npos; }
467 /// Return true if the given character is contained in *this, and false
470 bool contains(char C) const { return find_first_of(C) != npos; }
472 /// Return true if the given string is a substring of *this, and false
475 bool contains_lower(StringRef Other) const {
476 return find_lower(Other) != npos;
479 /// Return true if the given character is contained in *this, and false
482 bool contains_lower(char C) const { return find_lower(C) != npos; }
485 /// @name Helpful Algorithms
488 /// Return the number of occurrences of \p C in the string.
490 size_t count(char C) const {
492 for (size_t i = 0, e = Length; i != e; ++i)
498 /// Return the number of non-overlapped occurrences of \p Str in
500 size_t count(StringRef Str) const;
502 /// Parse the current string as an integer of the specified radix. If
503 /// \p Radix is specified as zero, this does radix autosensing using
504 /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
506 /// If the string is invalid or if only a subset of the string is valid,
507 /// this returns true to signify the error. The string is considered
508 /// erroneous if empty or if it overflows T.
509 template <typename T>
510 std::enable_if_t<std::numeric_limits<T>::is_signed, bool>
511 getAsInteger(unsigned Radix, T &Result) const {
513 if (getAsSignedInteger(*this, Radix, LLVal) ||
514 static_cast<T>(LLVal) != LLVal)
520 template <typename T>
521 std::enable_if_t<!std::numeric_limits<T>::is_signed, bool>
522 getAsInteger(unsigned Radix, T &Result) const {
523 unsigned long long ULLVal;
524 // The additional cast to unsigned long long is required to avoid the
525 // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
526 // 'unsigned __int64' when instantiating getAsInteger with T = bool.
527 if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
528 static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
534 /// Parse the current string as an integer of the specified radix. If
535 /// \p Radix is specified as zero, this does radix autosensing using
536 /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
538 /// If the string does not begin with a number of the specified radix,
539 /// this returns true to signify the error. The string is considered
540 /// erroneous if empty or if it overflows T.
541 /// The portion of the string representing the discovered numeric value
542 /// is removed from the beginning of the string.
543 template <typename T>
544 std::enable_if_t<std::numeric_limits<T>::is_signed, bool>
545 consumeInteger(unsigned Radix, T &Result) {
547 if (consumeSignedInteger(*this, Radix, LLVal) ||
548 static_cast<long long>(static_cast<T>(LLVal)) != LLVal)
554 template <typename T>
555 std::enable_if_t<!std::numeric_limits<T>::is_signed, bool>
556 consumeInteger(unsigned Radix, T &Result) {
557 unsigned long long ULLVal;
558 if (consumeUnsignedInteger(*this, Radix, ULLVal) ||
559 static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
565 /// Parse the current string as an integer of the specified \p Radix, or of
566 /// an autosensed radix if the \p Radix given is 0. The current value in
567 /// \p Result is discarded, and the storage is changed to be wide enough to
568 /// store the parsed integer.
570 /// \returns true if the string does not solely consist of a valid
571 /// non-empty number in the appropriate base.
573 /// APInt::fromString is superficially similar but assumes the
574 /// string is well-formed in the given radix.
575 bool getAsInteger(unsigned Radix, APInt &Result) const;
577 /// Parse the current string as an IEEE double-precision floating
578 /// point value. The string must be a well-formed double.
580 /// If \p AllowInexact is false, the function will fail if the string
581 /// cannot be represented exactly. Otherwise, the function only fails
582 /// in case of an overflow or underflow, or an invalid floating point
584 bool getAsDouble(double &Result, bool AllowInexact = true) const;
587 /// @name String Operations
590 // Convert the given ASCII string to lowercase.
592 std::string lower() const;
594 /// Convert the given ASCII string to uppercase.
596 std::string upper() const;
599 /// @name Substring Operations
602 /// Return a reference to the substring from [Start, Start + N).
604 /// \param Start The index of the starting character in the substring; if
605 /// the index is npos or greater than the length of the string then the
606 /// empty substring will be returned.
608 /// \param N The number of characters to included in the substring. If N
609 /// exceeds the number of characters remaining in the string, the string
610 /// suffix (starting with \p Start) will be returned.
612 StringRef substr(size_t Start, size_t N = npos) const {
613 Start = std::min(Start, Length);
614 return StringRef(Data + Start, std::min(N, Length - Start));
617 /// Return a StringRef equal to 'this' but with only the first \p N
618 /// elements remaining. If \p N is greater than the length of the
619 /// string, the entire string is returned.
621 StringRef take_front(size_t N = 1) const {
624 return drop_back(size() - N);
627 /// Return a StringRef equal to 'this' but with only the last \p N
628 /// elements remaining. If \p N is greater than the length of the
629 /// string, the entire string is returned.
631 StringRef take_back(size_t N = 1) const {
634 return drop_front(size() - N);
637 /// Return the longest prefix of 'this' such that every character
638 /// in the prefix satisfies the given predicate.
640 StringRef take_while(function_ref<bool(char)> F) const {
641 return substr(0, find_if_not(F));
644 /// Return the longest prefix of 'this' such that no character in
645 /// the prefix satisfies the given predicate.
647 StringRef take_until(function_ref<bool(char)> F) const {
648 return substr(0, find_if(F));
651 /// Return a StringRef equal to 'this' but with the first \p N elements
654 StringRef drop_front(size_t N = 1) const {
655 assert(size() >= N && "Dropping more elements than exist");
659 /// Return a StringRef equal to 'this' but with the last \p N elements
662 StringRef drop_back(size_t N = 1) const {
663 assert(size() >= N && "Dropping more elements than exist");
664 return substr(0, size()-N);
667 /// Return a StringRef equal to 'this', but with all characters satisfying
668 /// the given predicate dropped from the beginning of the string.
670 StringRef drop_while(function_ref<bool(char)> F) const {
671 return substr(find_if_not(F));
674 /// Return a StringRef equal to 'this', but with all characters not
675 /// satisfying the given predicate dropped from the beginning of the string.
677 StringRef drop_until(function_ref<bool(char)> F) const {
678 return substr(find_if(F));
681 /// Returns true if this StringRef has the given prefix and removes that
683 bool consume_front(StringRef Prefix) {
684 if (!startswith(Prefix))
687 *this = drop_front(Prefix.size());
691 /// Returns true if this StringRef has the given suffix and removes that
693 bool consume_back(StringRef Suffix) {
694 if (!endswith(Suffix))
697 *this = drop_back(Suffix.size());
701 /// Return a reference to the substring from [Start, End).
703 /// \param Start The index of the starting character in the substring; if
704 /// the index is npos or greater than the length of the string then the
705 /// empty substring will be returned.
707 /// \param End The index following the last character to include in the
708 /// substring. If this is npos or exceeds the number of characters
709 /// remaining in the string, the string suffix (starting with \p Start)
710 /// will be returned. If this is less than \p Start, an empty string will
713 StringRef slice(size_t Start, size_t End) const {
714 Start = std::min(Start, Length);
715 End = std::min(std::max(Start, End), Length);
716 return StringRef(Data + Start, End - Start);
719 /// Split into two substrings around the first occurrence of a separator
722 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
723 /// such that (*this == LHS + Separator + RHS) is true and RHS is
724 /// maximal. If \p Separator is not in the string, then the result is a
725 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
727 /// \param Separator The character to split on.
728 /// \returns The split substrings.
730 std::pair<StringRef, StringRef> split(char Separator) const {
731 return split(StringRef(&Separator, 1));
734 /// Split into two substrings around the first occurrence of a separator
737 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
738 /// such that (*this == LHS + Separator + RHS) is true and RHS is
739 /// maximal. If \p Separator is not in the string, then the result is a
740 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
742 /// \param Separator - The string to split on.
743 /// \return - The split substrings.
745 std::pair<StringRef, StringRef> split(StringRef Separator) const {
746 size_t Idx = find(Separator);
748 return std::make_pair(*this, StringRef());
749 return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
752 /// Split into two substrings around the last occurrence of a separator
755 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
756 /// such that (*this == LHS + Separator + RHS) is true and RHS is
757 /// minimal. If \p Separator is not in the string, then the result is a
758 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
760 /// \param Separator - The string to split on.
761 /// \return - The split substrings.
763 std::pair<StringRef, StringRef> rsplit(StringRef Separator) const {
764 size_t Idx = rfind(Separator);
766 return std::make_pair(*this, StringRef());
767 return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
770 /// Split into substrings around the occurrences of a separator string.
772 /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
773 /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
774 /// elements are added to A.
775 /// If \p KeepEmpty is false, empty strings are not added to \p A. They
776 /// still count when considering \p MaxSplit
777 /// An useful invariant is that
778 /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
780 /// \param A - Where to put the substrings.
781 /// \param Separator - The string to split on.
782 /// \param MaxSplit - The maximum number of times the string is split.
783 /// \param KeepEmpty - True if empty substring should be added.
784 void split(SmallVectorImpl<StringRef> &A,
785 StringRef Separator, int MaxSplit = -1,
786 bool KeepEmpty = true) const;
788 /// Split into substrings around the occurrences of a separator character.
790 /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
791 /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
792 /// elements are added to A.
793 /// If \p KeepEmpty is false, empty strings are not added to \p A. They
794 /// still count when considering \p MaxSplit
795 /// An useful invariant is that
796 /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
798 /// \param A - Where to put the substrings.
799 /// \param Separator - The string to split on.
800 /// \param MaxSplit - The maximum number of times the string is split.
801 /// \param KeepEmpty - True if empty substring should be added.
802 void split(SmallVectorImpl<StringRef> &A, char Separator, int MaxSplit = -1,
803 bool KeepEmpty = true) const;
805 /// Split into two substrings around the last occurrence of a separator
808 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
809 /// such that (*this == LHS + Separator + RHS) is true and RHS is
810 /// minimal. If \p Separator is not in the string, then the result is a
811 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
813 /// \param Separator - The character to split on.
814 /// \return - The split substrings.
816 std::pair<StringRef, StringRef> rsplit(char Separator) const {
817 return rsplit(StringRef(&Separator, 1));
820 /// Return string with consecutive \p Char characters starting from the
821 /// the left removed.
823 StringRef ltrim(char Char) const {
824 return drop_front(std::min(Length, find_first_not_of(Char)));
827 /// Return string with consecutive characters in \p Chars starting from
828 /// the left removed.
830 StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
831 return drop_front(std::min(Length, find_first_not_of(Chars)));
834 /// Return string with consecutive \p Char characters starting from the
837 StringRef rtrim(char Char) const {
838 return drop_back(Length - std::min(Length, find_last_not_of(Char) + 1));
841 /// Return string with consecutive characters in \p Chars starting from
842 /// the right removed.
844 StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {
845 return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1));
848 /// Return string with consecutive \p Char characters starting from the
849 /// left and right removed.
851 StringRef trim(char Char) const {
852 return ltrim(Char).rtrim(Char);
855 /// Return string with consecutive characters in \p Chars starting from
856 /// the left and right removed.
858 StringRef trim(StringRef Chars = " \t\n\v\f\r") const {
859 return ltrim(Chars).rtrim(Chars);
865 /// A wrapper around a string literal that serves as a proxy for constructing
866 /// global tables of StringRefs with the length computed at compile time.
867 /// In order to avoid the invocation of a global constructor, StringLiteral
868 /// should *only* be used in a constexpr context, as such:
870 /// constexpr StringLiteral S("test");
872 class StringLiteral : public StringRef {
874 constexpr StringLiteral(const char *Str, size_t N) : StringRef(Str, N) {
879 constexpr StringLiteral(const char (&Str)[N])
880 #if defined(__clang__) && __has_attribute(enable_if)
881 #pragma clang diagnostic push
882 #pragma clang diagnostic ignored "-Wgcc-compat"
883 __attribute((enable_if(__builtin_strlen(Str) == N - 1,
884 "invalid string literal")))
885 #pragma clang diagnostic pop
887 : StringRef(Str, N - 1) {
890 // Explicit construction for strings like "foo\0bar".
892 static constexpr StringLiteral withInnerNUL(const char (&Str)[N]) {
893 return StringLiteral(Str, N - 1);
897 /// @name StringRef Comparison Operators
900 inline bool operator==(StringRef LHS, StringRef RHS) {
901 return LHS.equals(RHS);
904 inline bool operator!=(StringRef LHS, StringRef RHS) { return !(LHS == RHS); }
906 inline bool operator<(StringRef LHS, StringRef RHS) {
907 return LHS.compare(RHS) == -1;
910 inline bool operator<=(StringRef LHS, StringRef RHS) {
911 return LHS.compare(RHS) != 1;
914 inline bool operator>(StringRef LHS, StringRef RHS) {
915 return LHS.compare(RHS) == 1;
918 inline bool operator>=(StringRef LHS, StringRef RHS) {
919 return LHS.compare(RHS) != -1;
922 inline std::string &operator+=(std::string &buffer, StringRef string) {
923 return buffer.append(string.data(), string.size());
928 /// Compute a hash_code for a StringRef.
930 hash_code hash_value(StringRef S);
932 } // end namespace llvm
934 #endif // LLVM_ADT_STRINGREF_H