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