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

source code of include/llvm-17/llvm/ADT/StringRef.h