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 #include <type_traits>
28 template <typename T
> class SmallVectorImpl
;
31 /// Helper functions for StringRef::getAsInteger.
32 bool getAsUnsignedInteger(StringRef Str
, unsigned Radix
,
33 unsigned long long &Result
);
35 bool getAsSignedInteger(StringRef Str
, unsigned Radix
, long long &Result
);
37 bool consumeUnsignedInteger(StringRef
&Str
, unsigned Radix
,
38 unsigned long long &Result
);
39 bool consumeSignedInteger(StringRef
&Str
, unsigned Radix
, long long &Result
);
41 /// StringRef - Represent a constant reference to a string, i.e. a character
42 /// array and a length, which need not be null terminated.
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.
50 static const size_t npos
= ~size_t(0);
52 using iterator
= const char *;
53 using const_iterator
= const char *;
54 using size_type
= size_t;
57 /// The start of the string, in an external buffer.
58 const char *Data
= nullptr;
60 /// The length of the string.
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
);
71 /// @name Constructors
74 /// Construct an empty string ref.
75 /*implicit*/ StringRef() = default;
77 /// Disable conversion from nullptr. This prevents things like
79 StringRef(std::nullptr_t
) = delete;
81 /// Construct a string ref from a cstring.
82 /*implicit*/ StringRef(const char *Str
)
83 : Data(Str
), Length(Str
? ::strlen(Str
) : 0) {}
85 /// Construct a string ref from a pointer and length.
86 /*implicit*/ constexpr StringRef(const char *data
, size_t length
)
87 : Data(data
), Length(length
) {}
89 /// Construct a string ref from an std::string.
90 /*implicit*/ StringRef(const std::string
&Str
)
91 : Data(Str
.data()), Length(Str
.length()) {}
93 static StringRef
withNullAsEmpty(const char *data
) {
94 return StringRef(data
? data
: "");
101 iterator
begin() const { return Data
; }
103 iterator
end() const { return Data
+ Length
; }
105 const unsigned char *bytes_begin() const {
106 return reinterpret_cast<const unsigned char *>(begin());
108 const unsigned char *bytes_end() const {
109 return reinterpret_cast<const unsigned char *>(end());
111 iterator_range
<const unsigned char *> bytes() const {
112 return make_range(bytes_begin(), bytes_end());
116 /// @name String Operations
119 /// data - Get a pointer to the start of the string (which may not be null
122 const char *data() const { return Data
; }
124 /// empty - Check if the string is empty.
126 bool empty() const { return Length
== 0; }
128 /// size - Get the string size.
130 size_t size() const { return Length
; }
132 /// front - Get the first character in the string.
139 /// back - Get the last character in the string.
143 return Data
[Length
-1];
146 // copy - Allocate copy in Allocator and return StringRef to it.
147 template <typename Allocator
>
148 LLVM_NODISCARD StringRef
copy(Allocator
&A
) const {
149 // Don't request a length 0 copy from the allocator.
152 char *S
= A
.template Allocate
<char>(Length
);
153 std::copy(begin(), end(), S
);
154 return StringRef(S
, Length
);
157 /// equals - Check for string equality, this is more efficient than
158 /// compare() when the relative ordering of inequal strings isn't needed.
160 bool equals(StringRef RHS
) const {
161 return (Length
== RHS
.Length
&&
162 compareMemory(Data
, RHS
.Data
, RHS
.Length
) == 0);
165 /// equals_lower - Check for string equality, ignoring case.
167 bool equals_lower(StringRef RHS
) const {
168 return Length
== RHS
.Length
&& compare_lower(RHS
) == 0;
171 /// compare - Compare two strings; the result is -1, 0, or 1 if this string
172 /// is lexicographically less than, equal to, or greater than the \p RHS.
174 int compare(StringRef RHS
) const {
175 // Check the prefix for a mismatch.
176 if (int Res
= compareMemory(Data
, RHS
.Data
, std::min(Length
, RHS
.Length
)))
177 return Res
< 0 ? -1 : 1;
179 // Otherwise the prefixes match, so we only need to check the lengths.
180 if (Length
== RHS
.Length
)
182 return Length
< RHS
.Length
? -1 : 1;
185 /// compare_lower - Compare two strings, ignoring case.
187 int compare_lower(StringRef RHS
) const;
189 /// compare_numeric - Compare two strings, treating sequences of digits as
192 int compare_numeric(StringRef RHS
) const;
194 /// Determine the edit distance between this string and another
197 /// \param Other the string to compare this string against.
199 /// \param AllowReplacements whether to allow character
200 /// replacements (change one character into another) as a single
201 /// operation, rather than as two operations (an insertion and a
204 /// \param MaxEditDistance If non-zero, the maximum edit distance that
205 /// this routine is allowed to compute. If the edit distance will exceed
206 /// that maximum, returns \c MaxEditDistance+1.
208 /// \returns the minimum number of character insertions, removals,
209 /// or (if \p AllowReplacements is \c true) replacements needed to
210 /// transform one of the given strings into the other. If zero,
211 /// the strings are identical.
213 unsigned edit_distance(StringRef Other
, bool AllowReplacements
= true,
214 unsigned MaxEditDistance
= 0) const;
216 /// str - Get the contents as an std::string.
218 std::string
str() const {
219 if (!Data
) return std::string();
220 return std::string(Data
, Length
);
224 /// @name Operator Overloads
228 char operator[](size_t Index
) const {
229 assert(Index
< Length
&& "Invalid index!");
233 /// Disallow accidental assignment from a temporary std::string.
235 /// The declaration here is extra complicated so that `stringRef = {}`
236 /// and `stringRef = "abc"` continue to select the move assignment operator.
237 template <typename T
>
238 typename
std::enable_if
<std::is_same
<T
, std::string
>::value
,
240 operator=(T
&&Str
) = delete;
243 /// @name Type Conversions
246 operator std::string() const {
251 /// @name String Predicates
254 /// Check if this string starts with the given \p Prefix.
256 bool startswith(StringRef Prefix
) const {
257 return Length
>= Prefix
.Length
&&
258 compareMemory(Data
, Prefix
.Data
, Prefix
.Length
) == 0;
261 /// Check if this string starts with the given \p Prefix, ignoring case.
263 bool startswith_lower(StringRef Prefix
) const;
265 /// Check if this string ends with the given \p Suffix.
267 bool endswith(StringRef Suffix
) const {
268 return Length
>= Suffix
.Length
&&
269 compareMemory(end() - Suffix
.Length
, Suffix
.Data
, Suffix
.Length
) == 0;
272 /// Check if this string ends with the given \p Suffix, ignoring case.
274 bool endswith_lower(StringRef Suffix
) const;
277 /// @name String Searching
280 /// Search for the first character \p C in the string.
282 /// \returns The index of the first occurrence of \p C, or npos if not
285 size_t find(char C
, size_t From
= 0) const {
286 size_t FindBegin
= std::min(From
, Length
);
287 if (FindBegin
< Length
) { // Avoid calling memchr with nullptr.
288 // Just forward to memchr, which is faster than a hand-rolled loop.
289 if (const void *P
= ::memchr(Data
+ FindBegin
, C
, Length
- FindBegin
))
290 return static_cast<const char *>(P
) - Data
;
295 /// Search for the first character \p C in the string, ignoring case.
297 /// \returns The index of the first occurrence of \p C, or npos if not
300 size_t find_lower(char C
, size_t From
= 0) const;
302 /// Search for the first character satisfying the predicate \p F
304 /// \returns The index of the first character satisfying \p F starting from
305 /// \p From, or npos if not found.
307 size_t find_if(function_ref
<bool(char)> F
, size_t From
= 0) const {
308 StringRef S
= drop_front(From
);
311 return size() - S
.size();
317 /// Search for the first character not satisfying the predicate \p F
319 /// \returns The index of the first character not satisfying \p F starting
320 /// from \p From, or npos if not found.
322 size_t find_if_not(function_ref
<bool(char)> F
, size_t From
= 0) const {
323 return find_if([F
](char c
) { return !F(c
); }, From
);
326 /// Search for the first string \p Str in the string.
328 /// \returns The index of the first occurrence of \p Str, or npos if not
331 size_t find(StringRef Str
, size_t From
= 0) const;
333 /// Search for the first string \p Str in the string, ignoring case.
335 /// \returns The index of the first occurrence of \p Str, or npos if not
338 size_t find_lower(StringRef Str
, size_t From
= 0) const;
340 /// Search for the last character \p C in the string.
342 /// \returns The index of the last occurrence of \p C, or npos if not
345 size_t rfind(char C
, size_t From
= npos
) const {
346 From
= std::min(From
, Length
);
356 /// Search for the last character \p C in the string, ignoring case.
358 /// \returns The index of the last occurrence of \p C, or npos if not
361 size_t rfind_lower(char C
, size_t From
= npos
) const;
363 /// Search for the last string \p Str in the string.
365 /// \returns The index of the last occurrence of \p Str, or npos if not
368 size_t rfind(StringRef Str
) const;
370 /// Search for the last string \p Str in the string, ignoring case.
372 /// \returns The index of the last occurrence of \p Str, or npos if not
375 size_t rfind_lower(StringRef Str
) const;
377 /// Find the first character in the string that is \p C, or npos if not
378 /// found. Same as find.
380 size_t find_first_of(char C
, size_t From
= 0) const {
381 return find(C
, From
);
384 /// Find the first character in the string that is in \p Chars, or npos if
387 /// Complexity: O(size() + Chars.size())
389 size_t find_first_of(StringRef Chars
, size_t From
= 0) const;
391 /// Find the first character in the string that is not \p C or npos if not
394 size_t find_first_not_of(char C
, size_t From
= 0) const;
396 /// Find the first character in the string that is not in the string
397 /// \p Chars, or npos if not found.
399 /// Complexity: O(size() + Chars.size())
401 size_t find_first_not_of(StringRef Chars
, size_t From
= 0) const;
403 /// Find the last character in the string that is \p C, or npos if not
406 size_t find_last_of(char C
, size_t From
= npos
) const {
407 return rfind(C
, From
);
410 /// Find the last character in the string that is in \p C, or npos if not
413 /// Complexity: O(size() + Chars.size())
415 size_t find_last_of(StringRef Chars
, size_t From
= npos
) const;
417 /// Find the last character in the string that is not \p C, or npos if not
420 size_t find_last_not_of(char C
, size_t From
= npos
) const;
422 /// Find the last character in the string that is not in \p Chars, or
423 /// npos if not found.
425 /// Complexity: O(size() + Chars.size())
427 size_t find_last_not_of(StringRef Chars
, size_t From
= npos
) const;
429 /// Return true if the given string is a substring of *this, and false
432 bool contains(StringRef Other
) const { return find(Other
) != npos
; }
434 /// Return true if the given character is contained in *this, and false
437 bool contains(char C
) const { return find_first_of(C
) != npos
; }
439 /// Return true if the given string is a substring of *this, and false
442 bool contains_lower(StringRef Other
) const {
443 return find_lower(Other
) != npos
;
446 /// Return true if the given character is contained in *this, and false
449 bool contains_lower(char C
) const { return find_lower(C
) != npos
; }
452 /// @name Helpful Algorithms
455 /// Return the number of occurrences of \p C in the string.
457 size_t count(char C
) const {
459 for (size_t i
= 0, e
= Length
; i
!= e
; ++i
)
465 /// Return the number of non-overlapped occurrences of \p Str in
467 size_t count(StringRef Str
) const;
469 /// Parse the current string as an integer of the specified radix. If
470 /// \p Radix is specified as zero, this does radix autosensing using
471 /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
473 /// If the string is invalid or if only a subset of the string is valid,
474 /// this returns true to signify the error. The string is considered
475 /// erroneous if empty or if it overflows T.
476 template <typename T
>
477 typename
std::enable_if
<std::numeric_limits
<T
>::is_signed
, bool>::type
478 getAsInteger(unsigned Radix
, T
&Result
) const {
480 if (getAsSignedInteger(*this, Radix
, LLVal
) ||
481 static_cast<T
>(LLVal
) != LLVal
)
487 template <typename T
>
488 typename
std::enable_if
<!std::numeric_limits
<T
>::is_signed
, bool>::type
489 getAsInteger(unsigned Radix
, T
&Result
) const {
490 unsigned long long ULLVal
;
491 // The additional cast to unsigned long long is required to avoid the
492 // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
493 // 'unsigned __int64' when instantiating getAsInteger with T = bool.
494 if (getAsUnsignedInteger(*this, Radix
, ULLVal
) ||
495 static_cast<unsigned long long>(static_cast<T
>(ULLVal
)) != ULLVal
)
501 /// Parse the current string as an integer of the specified radix. If
502 /// \p Radix is specified as zero, this does radix autosensing using
503 /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
505 /// If the string does not begin with a number of the specified radix,
506 /// this returns true to signify the error. The string is considered
507 /// erroneous if empty or if it overflows T.
508 /// The portion of the string representing the discovered numeric value
509 /// is removed from the beginning of the string.
510 template <typename T
>
511 typename
std::enable_if
<std::numeric_limits
<T
>::is_signed
, bool>::type
512 consumeInteger(unsigned Radix
, T
&Result
) {
514 if (consumeSignedInteger(*this, Radix
, LLVal
) ||
515 static_cast<long long>(static_cast<T
>(LLVal
)) != LLVal
)
521 template <typename T
>
522 typename
std::enable_if
<!std::numeric_limits
<T
>::is_signed
, bool>::type
523 consumeInteger(unsigned Radix
, T
&Result
) {
524 unsigned long long ULLVal
;
525 if (consumeUnsignedInteger(*this, Radix
, ULLVal
) ||
526 static_cast<unsigned long long>(static_cast<T
>(ULLVal
)) != ULLVal
)
532 /// Parse the current string as an integer of the specified \p Radix, or of
533 /// an autosensed radix if the \p Radix given is 0. The current value in
534 /// \p Result is discarded, and the storage is changed to be wide enough to
535 /// store the parsed integer.
537 /// \returns true if the string does not solely consist of a valid
538 /// non-empty number in the appropriate base.
540 /// APInt::fromString is superficially similar but assumes the
541 /// string is well-formed in the given radix.
542 bool getAsInteger(unsigned Radix
, APInt
&Result
) const;
544 /// Parse the current string as an IEEE double-precision floating
545 /// point value. The string must be a well-formed double.
547 /// If \p AllowInexact is false, the function will fail if the string
548 /// cannot be represented exactly. Otherwise, the function only fails
549 /// in case of an overflow or underflow.
550 bool getAsDouble(double &Result
, bool AllowInexact
= true) const;
553 /// @name String Operations
556 // Convert the given ASCII string to lowercase.
558 std::string
lower() const;
560 /// Convert the given ASCII string to uppercase.
562 std::string
upper() const;
565 /// @name Substring Operations
568 /// Return a reference to the substring from [Start, Start + N).
570 /// \param Start The index of the starting character in the substring; if
571 /// the index is npos or greater than the length of the string then the
572 /// empty substring will be returned.
574 /// \param N The number of characters to included in the substring. If N
575 /// exceeds the number of characters remaining in the string, the string
576 /// suffix (starting with \p Start) will be returned.
578 StringRef
substr(size_t Start
, size_t N
= npos
) const {
579 Start
= std::min(Start
, Length
);
580 return StringRef(Data
+ Start
, std::min(N
, Length
- Start
));
583 /// Return a StringRef equal to 'this' but with only the first \p N
584 /// elements remaining. If \p N is greater than the length of the
585 /// string, the entire string is returned.
587 StringRef
take_front(size_t N
= 1) const {
590 return drop_back(size() - N
);
593 /// Return a StringRef equal to 'this' but with only the last \p N
594 /// elements remaining. If \p N is greater than the length of the
595 /// string, the entire string is returned.
597 StringRef
take_back(size_t N
= 1) const {
600 return drop_front(size() - N
);
603 /// Return the longest prefix of 'this' such that every character
604 /// in the prefix satisfies the given predicate.
606 StringRef
take_while(function_ref
<bool(char)> F
) const {
607 return substr(0, find_if_not(F
));
610 /// Return the longest prefix of 'this' such that no character in
611 /// the prefix satisfies the given predicate.
613 StringRef
take_until(function_ref
<bool(char)> F
) const {
614 return substr(0, find_if(F
));
617 /// Return a StringRef equal to 'this' but with the first \p N elements
620 StringRef
drop_front(size_t N
= 1) const {
621 assert(size() >= N
&& "Dropping more elements than exist");
625 /// Return a StringRef equal to 'this' but with the last \p N elements
628 StringRef
drop_back(size_t N
= 1) const {
629 assert(size() >= N
&& "Dropping more elements than exist");
630 return substr(0, size()-N
);
633 /// Return a StringRef equal to 'this', but with all characters satisfying
634 /// the given predicate dropped from the beginning of the string.
636 StringRef
drop_while(function_ref
<bool(char)> F
) const {
637 return substr(find_if_not(F
));
640 /// Return a StringRef equal to 'this', but with all characters not
641 /// satisfying the given predicate dropped from the beginning of the string.
643 StringRef
drop_until(function_ref
<bool(char)> F
) const {
644 return substr(find_if(F
));
647 /// Returns true if this StringRef has the given prefix and removes that
649 bool consume_front(StringRef Prefix
) {
650 if (!startswith(Prefix
))
653 *this = drop_front(Prefix
.size());
657 /// Returns true if this StringRef has the given suffix and removes that
659 bool consume_back(StringRef Suffix
) {
660 if (!endswith(Suffix
))
663 *this = drop_back(Suffix
.size());
667 /// Return a reference to the substring from [Start, End).
669 /// \param Start The index of the starting character in the substring; if
670 /// the index is npos or greater than the length of the string then the
671 /// empty substring will be returned.
673 /// \param End The index following the last character to include in the
674 /// substring. If this is npos or exceeds the number of characters
675 /// remaining in the string, the string suffix (starting with \p Start)
676 /// will be returned. If this is less than \p Start, an empty string will
679 StringRef
slice(size_t Start
, size_t End
) const {
680 Start
= std::min(Start
, Length
);
681 End
= std::min(std::max(Start
, End
), Length
);
682 return StringRef(Data
+ Start
, End
- Start
);
685 /// Split into two substrings around the first occurrence of a separator
688 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
689 /// such that (*this == LHS + Separator + RHS) is true and RHS is
690 /// maximal. If \p Separator is not in the string, then the result is a
691 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
693 /// \param Separator The character to split on.
694 /// \returns The split substrings.
696 std::pair
<StringRef
, StringRef
> split(char Separator
) const {
697 return split(StringRef(&Separator
, 1));
700 /// Split into two substrings around the first occurrence of a separator
703 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
704 /// such that (*this == LHS + Separator + RHS) is true and RHS is
705 /// maximal. If \p Separator is not in the string, then the result is a
706 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
708 /// \param Separator - The string to split on.
709 /// \return - The split substrings.
711 std::pair
<StringRef
, StringRef
> split(StringRef Separator
) const {
712 size_t Idx
= find(Separator
);
714 return std::make_pair(*this, StringRef());
715 return std::make_pair(slice(0, Idx
), slice(Idx
+ Separator
.size(), npos
));
718 /// Split into two substrings around the last occurrence of a separator
721 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
722 /// such that (*this == LHS + Separator + RHS) is true and RHS is
723 /// minimal. If \p Separator is not in the string, then the result is a
724 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
726 /// \param Separator - The string to split on.
727 /// \return - The split substrings.
729 std::pair
<StringRef
, StringRef
> rsplit(StringRef Separator
) const {
730 size_t Idx
= rfind(Separator
);
732 return std::make_pair(*this, StringRef());
733 return std::make_pair(slice(0, Idx
), slice(Idx
+ Separator
.size(), npos
));
736 /// Split into substrings around the occurrences of a separator string.
738 /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
739 /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
740 /// elements are added to A.
741 /// If \p KeepEmpty is false, empty strings are not added to \p A. They
742 /// still count when considering \p MaxSplit
743 /// An useful invariant is that
744 /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
746 /// \param A - Where to put the substrings.
747 /// \param Separator - The string to split on.
748 /// \param MaxSplit - The maximum number of times the string is split.
749 /// \param KeepEmpty - True if empty substring should be added.
750 void split(SmallVectorImpl
<StringRef
> &A
,
751 StringRef Separator
, int MaxSplit
= -1,
752 bool KeepEmpty
= true) const;
754 /// Split into substrings around the occurrences of a separator character.
756 /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
757 /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
758 /// elements are added to A.
759 /// If \p KeepEmpty is false, empty strings are not added to \p A. They
760 /// still count when considering \p MaxSplit
761 /// An useful invariant is that
762 /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
764 /// \param A - Where to put the substrings.
765 /// \param Separator - The string to split on.
766 /// \param MaxSplit - The maximum number of times the string is split.
767 /// \param KeepEmpty - True if empty substring should be added.
768 void split(SmallVectorImpl
<StringRef
> &A
, char Separator
, int MaxSplit
= -1,
769 bool KeepEmpty
= true) const;
771 /// Split into two substrings around the last occurrence of a separator
774 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
775 /// such that (*this == LHS + Separator + RHS) is true and RHS is
776 /// minimal. If \p Separator is not in the string, then the result is a
777 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
779 /// \param Separator - The character to split on.
780 /// \return - The split substrings.
782 std::pair
<StringRef
, StringRef
> rsplit(char Separator
) const {
783 return rsplit(StringRef(&Separator
, 1));
786 /// Return string with consecutive \p Char characters starting from the
787 /// the left removed.
789 StringRef
ltrim(char Char
) const {
790 return drop_front(std::min(Length
, find_first_not_of(Char
)));
793 /// Return string with consecutive characters in \p Chars starting from
794 /// the left removed.
796 StringRef
ltrim(StringRef Chars
= " \t\n\v\f\r") const {
797 return drop_front(std::min(Length
, find_first_not_of(Chars
)));
800 /// Return string with consecutive \p Char characters starting from the
803 StringRef
rtrim(char Char
) const {
804 return drop_back(Length
- std::min(Length
, find_last_not_of(Char
) + 1));
807 /// Return string with consecutive characters in \p Chars starting from
808 /// the right removed.
810 StringRef
rtrim(StringRef Chars
= " \t\n\v\f\r") const {
811 return drop_back(Length
- std::min(Length
, find_last_not_of(Chars
) + 1));
814 /// Return string with consecutive \p Char characters starting from the
815 /// left and right removed.
817 StringRef
trim(char Char
) const {
818 return ltrim(Char
).rtrim(Char
);
821 /// Return string with consecutive characters in \p Chars starting from
822 /// the left and right removed.
824 StringRef
trim(StringRef Chars
= " \t\n\v\f\r") const {
825 return ltrim(Chars
).rtrim(Chars
);
831 /// A wrapper around a string literal that serves as a proxy for constructing
832 /// global tables of StringRefs with the length computed at compile time.
833 /// In order to avoid the invocation of a global constructor, StringLiteral
834 /// should *only* be used in a constexpr context, as such:
836 /// constexpr StringLiteral S("test");
838 class StringLiteral
: public StringRef
{
840 constexpr StringLiteral(const char *Str
, size_t N
) : StringRef(Str
, N
) {
845 constexpr StringLiteral(const char (&Str
)[N
])
846 #if defined(__clang__) && __has_attribute(enable_if)
847 #pragma clang diagnostic push
848 #pragma clang diagnostic ignored "-Wgcc-compat"
849 __attribute((enable_if(__builtin_strlen(Str
) == N
- 1,
850 "invalid string literal")))
851 #pragma clang diagnostic pop
853 : StringRef(Str
, N
- 1) {
856 // Explicit construction for strings like "foo\0bar".
858 static constexpr StringLiteral
withInnerNUL(const char (&Str
)[N
]) {
859 return StringLiteral(Str
, N
- 1);
863 /// @name StringRef Comparison Operators
866 inline bool operator==(StringRef LHS
, StringRef RHS
) {
867 return LHS
.equals(RHS
);
870 inline bool operator!=(StringRef LHS
, StringRef RHS
) { return !(LHS
== RHS
); }
872 inline bool operator<(StringRef LHS
, StringRef RHS
) {
873 return LHS
.compare(RHS
) == -1;
876 inline bool operator<=(StringRef LHS
, StringRef RHS
) {
877 return LHS
.compare(RHS
) != 1;
880 inline bool operator>(StringRef LHS
, StringRef RHS
) {
881 return LHS
.compare(RHS
) == 1;
884 inline bool operator>=(StringRef LHS
, StringRef RHS
) {
885 return LHS
.compare(RHS
) != -1;
888 inline std::string
&operator+=(std::string
&buffer
, StringRef string
) {
889 return buffer
.append(string
.data(), string
.size());
894 /// Compute a hash_code for a StringRef.
896 hash_code
hash_value(StringRef S
);
898 } // end namespace llvm
900 #endif // LLVM_ADT_STRINGREF_H