[SLP] limit vectorization of Constant subclasses (PR33958)
[llvm-core.git] / include / llvm / ADT / StringRef.h
blob52baab17bede7f7131be11609a6a2141d3c23933
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 //===----------------------------------------------------------------------===//
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"
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>
24 namespace llvm {
26 class APInt;
27 class hash_code;
28 template <typename T> class SmallVectorImpl;
29 class StringRef;
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.
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);
52 using iterator = const char *;
53 using const_iterator = const char *;
54 using size_type = size_t;
56 private:
57 /// The start of the string, in an external buffer.
58 const char *Data = nullptr;
60 /// The length of the string.
61 size_t Length = 0;
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);
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
84 public:
85 /// @name Constructors
86 /// @{
88 /// Construct an empty string ref.
89 /*implicit*/ StringRef() = default;
91 /// Disable conversion from nullptr. This prevents things like
92 /// if (S == nullptr)
93 StringRef(std::nullptr_t) = delete;
95 /// Construct a string ref from a cstring.
96 /*implicit*/ constexpr StringRef(const char *Str)
97 : Data(Str), Length(Str ? strLen(Str) : 0) {}
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) {}
103 /// Construct a string ref from an std::string.
104 /*implicit*/ StringRef(const std::string &Str)
105 : Data(Str.data()), Length(Str.length()) {}
107 static StringRef withNullAsEmpty(const char *data) {
108 return StringRef(data ? data : "");
111 /// @}
112 /// @name Iterators
113 /// @{
115 iterator begin() const { return Data; }
117 iterator end() const { return Data + Length; }
119 const unsigned char *bytes_begin() const {
120 return reinterpret_cast<const unsigned char *>(begin());
122 const unsigned char *bytes_end() const {
123 return reinterpret_cast<const unsigned char *>(end());
125 iterator_range<const unsigned char *> bytes() const {
126 return make_range(bytes_begin(), bytes_end());
129 /// @}
130 /// @name String Operations
131 /// @{
133 /// data - Get a pointer to the start of the string (which may not be null
134 /// terminated).
135 LLVM_NODISCARD
136 const char *data() const { return Data; }
138 /// empty - Check if the string is empty.
139 LLVM_NODISCARD
140 bool empty() const { return Length == 0; }
142 /// size - Get the string size.
143 LLVM_NODISCARD
144 size_t size() const { return Length; }
146 /// front - Get the first character in the string.
147 LLVM_NODISCARD
148 char front() const {
149 assert(!empty());
150 return Data[0];
153 /// back - Get the last character in the string.
154 LLVM_NODISCARD
155 char back() const {
156 assert(!empty());
157 return Data[Length-1];
160 // copy - Allocate copy in Allocator and return StringRef to it.
161 template <typename Allocator>
162 LLVM_NODISCARD StringRef copy(Allocator &A) const {
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);
171 /// equals - Check for string equality, this is more efficient than
172 /// compare() when the relative ordering of inequal strings isn't needed.
173 LLVM_NODISCARD
174 bool equals(StringRef RHS) const {
175 return (Length == RHS.Length &&
176 compareMemory(Data, RHS.Data, RHS.Length) == 0);
179 /// equals_lower - Check for string equality, ignoring case.
180 LLVM_NODISCARD
181 bool equals_lower(StringRef RHS) const {
182 return Length == RHS.Length && compare_lower(RHS) == 0;
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.
187 LLVM_NODISCARD
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;
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;
199 /// compare_lower - Compare two strings, ignoring case.
200 LLVM_NODISCARD
201 int compare_lower(StringRef RHS) const;
203 /// compare_numeric - Compare two strings, treating sequences of digits as
204 /// numbers.
205 LLVM_NODISCARD
206 int compare_numeric(StringRef RHS) const;
208 /// Determine the edit distance between this string and another
209 /// string.
211 /// \param Other the string to compare this string against.
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).
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.
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.
226 LLVM_NODISCARD
227 unsigned edit_distance(StringRef Other, bool AllowReplacements = true,
228 unsigned MaxEditDistance = 0) const;
230 /// str - Get the contents as an std::string.
231 LLVM_NODISCARD
232 std::string str() const {
233 if (!Data) return std::string();
234 return std::string(Data, Length);
237 /// @}
238 /// @name Operator Overloads
239 /// @{
241 LLVM_NODISCARD
242 char operator[](size_t Index) const {
243 assert(Index < Length && "Invalid index!");
244 return Data[Index];
247 /// Disallow accidental assignment from a temporary std::string.
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;
256 /// @}
257 /// @name Type Conversions
258 /// @{
260 operator std::string() const {
261 return str();
264 /// @}
265 /// @name String Predicates
266 /// @{
268 /// Check if this string starts with the given \p Prefix.
269 LLVM_NODISCARD
270 bool startswith(StringRef Prefix) const {
271 return Length >= Prefix.Length &&
272 compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
275 /// Check if this string starts with the given \p Prefix, ignoring case.
276 LLVM_NODISCARD
277 bool startswith_lower(StringRef Prefix) const;
279 /// Check if this string ends with the given \p Suffix.
280 LLVM_NODISCARD
281 bool endswith(StringRef Suffix) const {
282 return Length >= Suffix.Length &&
283 compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
286 /// Check if this string ends with the given \p Suffix, ignoring case.
287 LLVM_NODISCARD
288 bool endswith_lower(StringRef Suffix) const;
290 /// @}
291 /// @name String Searching
292 /// @{
294 /// Search for the first character \p C in the string.
296 /// \returns The index of the first occurrence of \p C, or npos if not
297 /// found.
298 LLVM_NODISCARD
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;
306 return npos;
309 /// Search for the first character \p C in the string, ignoring case.
311 /// \returns The index of the first occurrence of \p C, or npos if not
312 /// found.
313 LLVM_NODISCARD
314 size_t find_lower(char C, size_t From = 0) const;
316 /// Search for the first character satisfying the predicate \p F
318 /// \returns The index of the first character satisfying \p F starting from
319 /// \p From, or npos if not found.
320 LLVM_NODISCARD
321 size_t find_if(function_ref<bool(char)> F, size_t From = 0) const {
322 StringRef S = drop_front(From);
323 while (!S.empty()) {
324 if (F(S.front()))
325 return size() - S.size();
326 S = S.drop_front();
328 return npos;
331 /// Search for the first character not satisfying the predicate \p F
333 /// \returns The index of the first character not satisfying \p F starting
334 /// from \p From, or npos if not found.
335 LLVM_NODISCARD
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);
340 /// Search for the first string \p Str in the string.
342 /// \returns The index of the first occurrence of \p Str, or npos if not
343 /// found.
344 LLVM_NODISCARD
345 size_t find(StringRef Str, size_t From = 0) const;
347 /// Search for the first string \p Str in the string, ignoring case.
349 /// \returns The index of the first occurrence of \p Str, or npos if not
350 /// found.
351 LLVM_NODISCARD
352 size_t find_lower(StringRef Str, size_t From = 0) const;
354 /// Search for the last character \p C in the string.
356 /// \returns The index of the last occurrence of \p C, or npos if not
357 /// found.
358 LLVM_NODISCARD
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;
367 return npos;
370 /// Search for the last character \p C in the string, ignoring case.
372 /// \returns The index of the last occurrence of \p C, or npos if not
373 /// found.
374 LLVM_NODISCARD
375 size_t rfind_lower(char C, size_t From = npos) const;
377 /// Search for the last string \p Str in the string.
379 /// \returns The index of the last occurrence of \p Str, or npos if not
380 /// found.
381 LLVM_NODISCARD
382 size_t rfind(StringRef Str) const;
384 /// Search for the last string \p Str in the string, ignoring case.
386 /// \returns The index of the last occurrence of \p Str, or npos if not
387 /// found.
388 LLVM_NODISCARD
389 size_t rfind_lower(StringRef Str) const;
391 /// Find the first character in the string that is \p C, or npos if not
392 /// found. Same as find.
393 LLVM_NODISCARD
394 size_t find_first_of(char C, size_t From = 0) const {
395 return find(C, From);
398 /// Find the first character in the string that is in \p Chars, or npos if
399 /// not found.
401 /// Complexity: O(size() + Chars.size())
402 LLVM_NODISCARD
403 size_t find_first_of(StringRef Chars, size_t From = 0) const;
405 /// Find the first character in the string that is not \p C or npos if not
406 /// found.
407 LLVM_NODISCARD
408 size_t find_first_not_of(char C, size_t From = 0) const;
410 /// Find the first character in the string that is not in the string
411 /// \p Chars, or npos if not found.
413 /// Complexity: O(size() + Chars.size())
414 LLVM_NODISCARD
415 size_t find_first_not_of(StringRef Chars, size_t From = 0) const;
417 /// Find the last character in the string that is \p C, or npos if not
418 /// found.
419 LLVM_NODISCARD
420 size_t find_last_of(char C, size_t From = npos) const {
421 return rfind(C, From);
424 /// Find the last character in the string that is in \p C, or npos if not
425 /// found.
427 /// Complexity: O(size() + Chars.size())
428 LLVM_NODISCARD
429 size_t find_last_of(StringRef Chars, size_t From = npos) const;
431 /// Find the last character in the string that is not \p C, or npos if not
432 /// found.
433 LLVM_NODISCARD
434 size_t find_last_not_of(char C, size_t From = npos) const;
436 /// Find the last character in the string that is not in \p Chars, or
437 /// npos if not found.
439 /// Complexity: O(size() + Chars.size())
440 LLVM_NODISCARD
441 size_t find_last_not_of(StringRef Chars, size_t From = npos) const;
443 /// Return true if the given string is a substring of *this, and false
444 /// otherwise.
445 LLVM_NODISCARD
446 bool contains(StringRef Other) const { return find(Other) != npos; }
448 /// Return true if the given character is contained in *this, and false
449 /// otherwise.
450 LLVM_NODISCARD
451 bool contains(char C) const { return find_first_of(C) != npos; }
453 /// Return true if the given string is a substring of *this, and false
454 /// otherwise.
455 LLVM_NODISCARD
456 bool contains_lower(StringRef Other) const {
457 return find_lower(Other) != npos;
460 /// Return true if the given character is contained in *this, and false
461 /// otherwise.
462 LLVM_NODISCARD
463 bool contains_lower(char C) const { return find_lower(C) != npos; }
465 /// @}
466 /// @name Helpful Algorithms
467 /// @{
469 /// Return the number of occurrences of \p C in the string.
470 LLVM_NODISCARD
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;
479 /// Return the number of non-overlapped occurrences of \p Str in
480 /// the string.
481 size_t count(StringRef Str) const;
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.
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;
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;
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.
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;
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;
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.
551 /// \returns true if the string does not solely consist of a valid
552 /// non-empty number in the appropriate base.
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;
558 /// Parse the current string as an IEEE double-precision floating
559 /// point value. The string must be a well-formed double.
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;
566 /// @}
567 /// @name String Operations
568 /// @{
570 // Convert the given ASCII string to lowercase.
571 LLVM_NODISCARD
572 std::string lower() const;
574 /// Convert the given ASCII string to uppercase.
575 LLVM_NODISCARD
576 std::string upper() const;
578 /// @}
579 /// @name Substring Operations
580 /// @{
582 /// Return a reference to the substring from [Start, Start + N).
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.
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.
591 LLVM_NODISCARD
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));
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.
600 LLVM_NODISCARD
601 StringRef take_front(size_t N = 1) const {
602 if (N >= size())
603 return *this;
604 return drop_back(size() - N);
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.
610 LLVM_NODISCARD
611 StringRef take_back(size_t N = 1) const {
612 if (N >= size())
613 return *this;
614 return drop_front(size() - N);
617 /// Return the longest prefix of 'this' such that every character
618 /// in the prefix satisfies the given predicate.
619 LLVM_NODISCARD
620 StringRef take_while(function_ref<bool(char)> F) const {
621 return substr(0, find_if_not(F));
624 /// Return the longest prefix of 'this' such that no character in
625 /// the prefix satisfies the given predicate.
626 LLVM_NODISCARD
627 StringRef take_until(function_ref<bool(char)> F) const {
628 return substr(0, find_if(F));
631 /// Return a StringRef equal to 'this' but with the first \p N elements
632 /// dropped.
633 LLVM_NODISCARD
634 StringRef drop_front(size_t N = 1) const {
635 assert(size() >= N && "Dropping more elements than exist");
636 return substr(N);
639 /// Return a StringRef equal to 'this' but with the last \p N elements
640 /// dropped.
641 LLVM_NODISCARD
642 StringRef drop_back(size_t N = 1) const {
643 assert(size() >= N && "Dropping more elements than exist");
644 return substr(0, size()-N);
647 /// Return a StringRef equal to 'this', but with all characters satisfying
648 /// the given predicate dropped from the beginning of the string.
649 LLVM_NODISCARD
650 StringRef drop_while(function_ref<bool(char)> F) const {
651 return substr(find_if_not(F));
654 /// Return a StringRef equal to 'this', but with all characters not
655 /// satisfying the given predicate dropped from the beginning of the string.
656 LLVM_NODISCARD
657 StringRef drop_until(function_ref<bool(char)> F) const {
658 return substr(find_if(F));
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;
667 *this = drop_front(Prefix.size());
668 return true;
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;
677 *this = drop_back(Suffix.size());
678 return true;
681 /// Return a reference to the substring from [Start, End).
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.
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.
692 LLVM_NODISCARD
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);
699 /// Split into two substrings around the first occurrence of a separator
700 /// character.
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 == "").
707 /// \param Separator The character to split on.
708 /// \returns The split substrings.
709 LLVM_NODISCARD
710 std::pair<StringRef, StringRef> split(char Separator) const {
711 return split(StringRef(&Separator, 1));
714 /// Split into two substrings around the first occurrence of a separator
715 /// string.
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 == "").
722 /// \param Separator - The string to split on.
723 /// \return - The split substrings.
724 LLVM_NODISCARD
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));
732 /// Split into two substrings around the last occurrence of a separator
733 /// string.
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 == "").
740 /// \param Separator - The string to split on.
741 /// \return - The split substrings.
742 LLVM_NODISCARD
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));
750 /// Split into substrings around the occurrences of a separator string.
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
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.
764 void split(SmallVectorImpl<StringRef> &A,
765 StringRef Separator, int MaxSplit = -1,
766 bool KeepEmpty = true) const;
768 /// Split into substrings around the occurrences of a separator character.
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
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;
785 /// Split into two substrings around the last occurrence of a separator
786 /// character.
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 == "").
793 /// \param Separator - The character to split on.
794 /// \return - The split substrings.
795 LLVM_NODISCARD
796 std::pair<StringRef, StringRef> rsplit(char Separator) const {
797 return rsplit(StringRef(&Separator, 1));
800 /// Return string with consecutive \p Char characters starting from the
801 /// the left removed.
802 LLVM_NODISCARD
803 StringRef ltrim(char Char) const {
804 return drop_front(std::min(Length, find_first_not_of(Char)));
807 /// Return string with consecutive characters in \p Chars starting from
808 /// the left removed.
809 LLVM_NODISCARD
810 StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
811 return drop_front(std::min(Length, find_first_not_of(Chars)));
814 /// Return string with consecutive \p Char characters starting from the
815 /// right removed.
816 LLVM_NODISCARD
817 StringRef rtrim(char Char) const {
818 return drop_back(Length - std::min(Length, find_last_not_of(Char) + 1));
821 /// Return string with consecutive characters in \p Chars starting from
822 /// the right removed.
823 LLVM_NODISCARD
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));
828 /// Return string with consecutive \p Char characters starting from the
829 /// left and right removed.
830 LLVM_NODISCARD
831 StringRef trim(char Char) const {
832 return ltrim(Char).rtrim(Char);
835 /// Return string with consecutive characters in \p Chars starting from
836 /// the left and right removed.
837 LLVM_NODISCARD
838 StringRef trim(StringRef Chars = " \t\n\v\f\r") const {
839 return ltrim(Chars).rtrim(Chars);
842 /// @}
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:
850 /// constexpr StringLiteral S("test");
852 class StringLiteral : public StringRef {
853 private:
854 constexpr StringLiteral(const char *Str, size_t N) : StringRef(Str, N) {
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) {
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);
877 /// @name StringRef Comparison Operators
878 /// @{
880 inline bool operator==(StringRef LHS, StringRef RHS) {
881 return LHS.equals(RHS);
884 inline bool operator!=(StringRef LHS, StringRef RHS) { return !(LHS == RHS); }
886 inline bool operator<(StringRef LHS, StringRef RHS) {
887 return LHS.compare(RHS) == -1;
890 inline bool operator<=(StringRef LHS, StringRef RHS) {
891 return LHS.compare(RHS) != 1;
894 inline bool operator>(StringRef LHS, StringRef RHS) {
895 return LHS.compare(RHS) == 1;
898 inline bool operator>=(StringRef LHS, StringRef RHS) {
899 return LHS.compare(RHS) != -1;
902 inline std::string &operator+=(std::string &buffer, StringRef string) {
903 return buffer.append(string.data(), string.size());
906 /// @}
908 /// Compute a hash_code for a StringRef.
909 LLVM_NODISCARD
910 hash_code hash_value(StringRef S);
912 } // end namespace llvm
914 #endif // LLVM_ADT_STRINGREF_H