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/// Represent a constant reference to a string, i.e. a character array and a
49/// 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 /// 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 /// Check if the string is empty.
140 [[nodiscard]] constexpr bool empty() const { return size() == 0; }
141
142 /// Get the string size.
143 [[nodiscard]] constexpr size_t size() const { return Length; }
144
145 /// Get the first character in the string.
146 [[nodiscard]] char front() const {
147 assert(!empty());
148 return data()[0];
149 }
150
151 /// Get the last character in the string.
152 [[nodiscard]] char back() const {
153 assert(!empty());
154 return data()[size() - 1];
155 }
156
157 // 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 two strings; the result is negative, zero, or positive if this
174 /// string is lexicographically less than, equal to, or greater than the
175 /// \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 two strings, treating sequences of digits as numbers.
192 [[nodiscard]] LLVM_ABI int compare_numeric(StringRef RHS) const;
193
194 /// Determine the edit distance between this string and another
195 /// string.
196 ///
197 /// \param Other the string to compare this string against.
198 ///
199 /// \param AllowReplacements whether to allow character
200 /// replacements (change one character into another) as a single
201 /// operation, rather than as two operations (an insertion and a
202 /// removal).
203 ///
204 /// \param MaxEditDistance If non-zero, the maximum edit distance that
205 /// this routine is allowed to compute. If the edit distance will exceed
206 /// that maximum, returns \c MaxEditDistance+1.
207 ///
208 /// \returns the minimum number of character insertions, removals,
209 /// or (if \p AllowReplacements is \c true) replacements needed to
210 /// transform one of the given strings into the other. If zero,
211 /// the strings are identical.
212 [[nodiscard]] LLVM_ABI unsigned
213 edit_distance(StringRef Other, bool AllowReplacements = true,
214 unsigned MaxEditDistance = 0) const;
215
216 [[nodiscard]] LLVM_ABI unsigned
217 edit_distance_insensitive(StringRef Other, bool AllowReplacements = true,
218 unsigned MaxEditDistance = 0) const;
219
220 /// Get the contents as an std::string.
221 [[nodiscard]] std::string str() const {
222 if (!data())
223 return std::string();
224 return std::string(data(), size());
225 }
226
227 /// @}
228 /// @name Operator Overloads
229 /// @{
230
231 [[nodiscard]] char operator[](size_t Index) const {
232 assert(Index < size() && "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 constexpr 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 size() >= Prefix.size() &&
259 compareMemory(data(), Prefix.data(), Prefix.size()) == 0;
260 }
261 [[nodiscard]] bool starts_with(char Prefix) const {
262 return !empty() && front() == Prefix;
263 }
264
265 /// Check if this string starts with the given \p Prefix, ignoring case.
266 [[nodiscard]] LLVM_ABI bool starts_with_insensitive(StringRef Prefix) const;
267
268 /// Check if this string ends with the given \p Suffix.
269 [[nodiscard]] bool ends_with(StringRef Suffix) const {
270 return size() >= Suffix.size() &&
271 compareMemory(end() - Suffix.size(), Suffix.data(), Suffix.size()) ==
272 0;
273 }
274 [[nodiscard]] bool ends_with(char Suffix) const {
275 return !empty() && back() == Suffix;
276 }
277
278 /// Check if this string ends with the given \p Suffix, ignoring case.
279 [[nodiscard]] LLVM_ABI bool ends_with_insensitive(StringRef Suffix) const;
280
281 /// @}
282 /// @name String Searching
283 /// @{
284
285 /// Search for the first character \p C in the string.
286 ///
287 /// \returns The index of the first occurrence of \p C, or npos if not
288 /// found.
289 [[nodiscard]] size_t find(char C, size_t From = 0) const {
290 return std::string_view(*this).find(C, From);
291 }
292
293 /// Search for the first character \p C in the string, ignoring case.
294 ///
295 /// \returns The index of the first occurrence of \p C, or npos if not
296 /// found.
297 [[nodiscard]] LLVM_ABI size_t find_insensitive(char C, size_t From = 0) const;
298
299 /// Search for the first character satisfying the predicate \p F
300 ///
301 /// \returns The index of the first character satisfying \p F starting from
302 /// \p From, or npos if not found.
303 [[nodiscard]] size_t find_if(function_ref<bool(char)> F,
304 size_t From = 0) const {
305 StringRef S = drop_front(From);
306 while (!S.empty()) {
307 if (F(S.front()))
308 return size() - S.size();
309 S = S.drop_front();
310 }
311 return npos;
312 }
313
314 /// Search for the first character not satisfying the predicate \p F
315 ///
316 /// \returns The index of the first character not satisfying \p F starting
317 /// from \p From, or npos if not found.
318 [[nodiscard]] size_t find_if_not(function_ref<bool(char)> F,
319 size_t From = 0) const {
320 return find_if([F](char c) { return !F(c); }, From);
321 }
322
323 /// Search for the last character satisfying the predicate \p F
324 ///
325 /// \returns The index of the last character satisfying \p F before \p End,
326 /// or npos if not found.
327 [[nodiscard]] size_t rfind_if(function_ref<bool(char)> F,
328 size_t End = npos) const {
329 size_t I = std::min(End, size());
330 while (I) {
331 --I;
332 if (F(data()[I]))
333 return I;
334 }
335 return npos;
336 }
337
338 /// Search for the last character not satisfying the predicate \p F
339 ///
340 /// \returns The index of the last character not satisfying \p F before \p
341 /// End, or npos if not found.
342 [[nodiscard]] size_t rfind_if_not(function_ref<bool(char)> F,
343 size_t End = npos) const {
344 return rfind_if(std::not_fn(F), End);
345 }
346
347 /// Search for the first string \p Str in the string.
348 ///
349 /// \returns The index of the first occurrence of \p Str, or npos if not
350 /// found.
351 [[nodiscard]] LLVM_ABI size_t find(StringRef Str, size_t From = 0) const;
352
353 /// Search for the first string \p Str in the string, ignoring case.
354 ///
355 /// \returns The index of the first occurrence of \p Str, or npos if not
356 /// found.
357 [[nodiscard]] LLVM_ABI size_t find_insensitive(StringRef Str,
358 size_t From = 0) const;
359
360 /// Search for the last character \p C in the string.
361 ///
362 /// \returns The index of the last occurrence of \p C, or npos if not
363 /// found.
364 [[nodiscard]] size_t rfind(char C, size_t From = npos) const {
365 size_t I = std::min(From, size());
366 while (I) {
367 --I;
368 if (data()[I] == C)
369 return I;
370 }
371 return npos;
372 }
373
374 /// Search for the last character \p C in the string, ignoring case.
375 ///
376 /// \returns The index of the last occurrence of \p C, or npos if not
377 /// found.
378 [[nodiscard]] LLVM_ABI size_t rfind_insensitive(char C,
379 size_t From = npos) const;
380
381 /// Search for the last string \p Str in the string.
382 ///
383 /// \returns The index of the last occurrence of \p Str, or npos if not
384 /// found.
385 [[nodiscard]] LLVM_ABI size_t rfind(StringRef Str) const;
386
387 /// Search for the last string \p Str in the string, ignoring case.
388 ///
389 /// \returns The index of the last occurrence of \p Str, or npos if not
390 /// found.
391 [[nodiscard]] LLVM_ABI size_t rfind_insensitive(StringRef Str) const;
392
393 /// Find the first character in the string that is \p C, or npos if not
394 /// found. Same as find.
395 [[nodiscard]] size_t find_first_of(char C, size_t From = 0) const {
396 return find(C, From);
397 }
398
399 /// Find the first character in the string that is in \p Chars, or npos if
400 /// not found.
401 ///
402 /// Complexity: O(size() + Chars.size())
403 [[nodiscard]] LLVM_ABI size_t find_first_of(StringRef Chars,
404 size_t From = 0) const;
405
406 /// Find the first character in the string that is not \p C or npos if not
407 /// found.
408 [[nodiscard]] LLVM_ABI size_t find_first_not_of(char C,
409 size_t From = 0) const;
410
411 /// Find the first character in the string that is not in the string
412 /// \p Chars, or npos if not found.
413 ///
414 /// Complexity: O(size() + Chars.size())
415 [[nodiscard]] LLVM_ABI size_t find_first_not_of(StringRef Chars,
416 size_t From = 0) const;
417
418 /// Find the last character in the string that is \p C, or npos if not
419 /// found.
420 [[nodiscard]] size_t find_last_of(char C, size_t From = npos) const {
421 return rfind(C, From);
422 }
423
424 /// Find the last character in the string that is in \p C, or npos if not
425 /// found.
426 ///
427 /// Complexity: O(size() + Chars.size())
428 [[nodiscard]] LLVM_ABI size_t find_last_of(StringRef Chars,
429 size_t From = npos) const;
430
431 /// Find the last character in the string that is not \p C, or npos if not
432 /// found.
433 [[nodiscard]] LLVM_ABI size_t find_last_not_of(char C,
434 size_t From = npos) const;
435
436 /// Find the last character in the string that is not in \p Chars, or
437 /// npos if not found.
438 ///
439 /// Complexity: O(size() + Chars.size())
440 [[nodiscard]] LLVM_ABI size_t find_last_not_of(StringRef Chars,
441 size_t From = npos) const;
442
443 /// Return true if the given string is a substring of *this, and false
444 /// otherwise.
445 [[nodiscard]] bool contains(StringRef Other) const {
446 return find(Other) != npos;
447 }
448
449 /// Return true if the given character is contained in *this, and false
450 /// otherwise.
451 [[nodiscard]] bool contains(char C) const { return find_first_of(C) != npos; }
452
453 /// Return true if the given string is a substring of *this, and false
454 /// otherwise.
455 [[nodiscard]] bool contains_insensitive(StringRef Other) const {
456 return find_insensitive(Other) != npos;
457 }
458
459 /// Return true if the given character is contained in *this, and false
460 /// otherwise.
461 [[nodiscard]] bool contains_insensitive(char C) const {
462 return find_insensitive(C) != npos;
463 }
464
465 /// @}
466 /// @name Helpful Algorithms
467 /// @{
468
469 /// Return the number of occurrences of \p C in the string.
470 [[nodiscard]] size_t count(char C) const {
471 size_t Count = 0;
472 for (size_t I = 0; I != size(); ++I)
473 if (data()[I] == C)
474 ++Count;
475 return Count;
476 }
477
478 /// Return the number of non-overlapped occurrences of \p Str in
479 /// the string.
480 LLVM_ABI size_t count(StringRef Str) const;
481
482 /// Parse the current string as an integer of the specified radix. If
483 /// \p Radix is specified as zero, this does radix autosensing using
484 /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
485 ///
486 /// If the string is invalid or if only a subset of the string is valid,
487 /// this returns true to signify the error. The string is considered
488 /// erroneous if empty or if it overflows T.
489 template <typename T> bool getAsInteger(unsigned Radix, T &Result) const {
490 if constexpr (std::numeric_limits<T>::is_signed) {
491 long long LLVal;
492 if (getAsSignedInteger(*this, Radix, LLVal) ||
493 static_cast<T>(LLVal) != LLVal)
494 return true;
495 Result = LLVal;
496 } else {
497 unsigned long long ULLVal;
498 // The additional cast to unsigned long long is required to avoid the
499 // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
500 // 'unsigned __int64' when instantiating getAsInteger with T = bool.
501 if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
502 static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
503 return true;
504 Result = ULLVal;
505 }
506 return false;
507 }
508
509 /// Parse the current string as an integer of the specified radix. If
510 /// \p Radix is specified as zero, this does radix autosensing using
511 /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
512 ///
513 /// If the string does not begin with a number of the specified radix,
514 /// this returns true to signify the error. The string is considered
515 /// erroneous if empty or if it overflows T.
516 /// The portion of the string representing the discovered numeric value
517 /// is removed from the beginning of the string.
518 template <typename T> bool consumeInteger(unsigned Radix, T &Result) {
519 if constexpr (std::numeric_limits<T>::is_signed) {
520 long long LLVal;
521 if (consumeSignedInteger(*this, Radix, LLVal) ||
522 static_cast<long long>(static_cast<T>(LLVal)) != LLVal)
523 return true;
524 Result = LLVal;
525 } else {
526 unsigned long long ULLVal;
527 if (consumeUnsignedInteger(*this, Radix, ULLVal) ||
528 static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
529 return true;
530 Result = ULLVal;
531 }
532 return false;
533 }
534
535 /// Parse the current string as an integer of the specified \p Radix, or of
536 /// an autosensed radix if the \p Radix given is 0. The current value in
537 /// \p Result is discarded, and the storage is changed to be wide enough to
538 /// store the parsed integer.
539 ///
540 /// \returns true if the string does not solely consist of a valid
541 /// non-empty number in the appropriate base.
542 ///
543 /// APInt::fromString is superficially similar but assumes the
544 /// string is well-formed in the given radix.
545 LLVM_ABI bool getAsInteger(unsigned Radix, APInt &Result) const;
546
547 /// Parse the current string as an integer of the specified \p Radix. If
548 /// \p Radix is specified as zero, this does radix autosensing using
549 /// extended C rules: 0 is octal, 0x is hex, 0b is binary.
550 ///
551 /// If the string does not begin with a number of the specified radix,
552 /// this returns true to signify the error. The string is considered
553 /// erroneous if empty.
554 /// The portion of the string representing the discovered numeric value
555 /// is removed from the beginning of the string.
556 LLVM_ABI bool consumeInteger(unsigned Radix, APInt &Result);
557
558 /// Parse the current string as an IEEE double-precision floating
559 /// point value. The string must be a well-formed double.
560 ///
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, or an invalid floating point
564 /// representation.
565 LLVM_ABI bool getAsDouble(double &Result, bool AllowInexact = true) const;
566
567 /// @}
568 /// @name String Operations
569 /// @{
570
571 // Convert the given ASCII string to lowercase.
572 [[nodiscard]] LLVM_ABI std::string lower() const;
573
574 /// Convert the given ASCII string to uppercase.
575 [[nodiscard]] LLVM_ABI std::string upper() const;
576
577 /// @}
578 /// @name Substring Operations
579 /// @{
580
581 /// Return a reference to the substring from [Start, Start + N).
582 ///
583 /// \param Start The index of the starting character in the substring; if
584 /// the index is npos or greater than the length of the string then the
585 /// empty substring will be returned.
586 ///
587 /// \param N The number of characters to included in the substring. If N
588 /// exceeds the number of characters remaining in the string, the string
589 /// suffix (starting with \p Start) will be returned.
590 [[nodiscard]] constexpr StringRef substr(size_t Start,
591 size_t N = npos) const {
592 Start = std::min(Start, size());
593 return StringRef(data() + Start, std::min(N, size() - Start));
594 }
595
596 /// Return a StringRef equal to 'this' but with only the first \p N
597 /// elements remaining. If \p N is greater than the length of the
598 /// string, the entire string is returned.
599 [[nodiscard]] StringRef take_front(size_t N = 1) const {
600 if (N >= size())
601 return *this;
602 return drop_back(size() - N);
603 }
604
605 /// Return a StringRef equal to 'this' but with only the last \p N
606 /// elements remaining. If \p N is greater than the length of the
607 /// string, the entire string is returned.
608 [[nodiscard]] StringRef take_back(size_t N = 1) const {
609 if (N >= size())
610 return *this;
611 return drop_front(size() - N);
612 }
613
614 /// Return the longest prefix of 'this' such that every character
615 /// in the prefix satisfies the given predicate.
616 [[nodiscard]] StringRef take_while(function_ref<bool(char)> F) const {
617 return substr(0, find_if_not(F));
618 }
619
620 /// Return the longest prefix of 'this' such that no character in
621 /// the prefix satisfies the given predicate.
622 [[nodiscard]] StringRef take_until(function_ref<bool(char)> F) const {
623 return substr(0, find_if(F));
624 }
625
626 /// Return a StringRef equal to 'this' but with the first \p N elements
627 /// dropped.
628 [[nodiscard]] StringRef drop_front(size_t N = 1) const {
629 assert(size() >= N && "Dropping more elements than exist");
630 return substr(N);
631 }
632
633 /// Return a StringRef equal to 'this' but with the last \p N elements
634 /// dropped.
635 [[nodiscard]] StringRef drop_back(size_t N = 1) const {
636 assert(size() >= N && "Dropping more elements than exist");
637 return substr(0, size() - N);
638 }
639
640 /// Return a StringRef equal to 'this', but with all characters satisfying
641 /// the given predicate dropped from the beginning of the string.
642 [[nodiscard]] StringRef drop_while(function_ref<bool(char)> F) const {
643 return substr(find_if_not(F));
644 }
645
646 /// Return a StringRef equal to 'this', but with all characters not
647 /// satisfying the given predicate dropped from the beginning of the string.
648 [[nodiscard]] StringRef drop_until(function_ref<bool(char)> F) const {
649 return substr(find_if(F));
650 }
651
652 /// Returns true if this StringRef has the given prefix and removes that
653 /// prefix.
654 bool consume_front(char Prefix) {
655 if (!starts_with(Prefix))
656 return false;
657
658 *this = drop_front();
659 return true;
660 }
661
662 /// Returns true if this StringRef has the given prefix and removes that
663 /// prefix.
665 if (!starts_with(Prefix))
666 return false;
667
668 *this = substr(Prefix.size());
669 return true;
670 }
671
672 /// Returns true if this StringRef has the given prefix, ignoring case,
673 /// and removes that prefix.
675 if (!starts_with_insensitive(Prefix))
676 return false;
677
678 *this = substr(Prefix.size());
679 return true;
680 }
681
682 /// Returns true if this StringRef has the given suffix and removes that
683 /// suffix.
684 bool consume_back(StringRef Suffix) {
685 if (!ends_with(Suffix))
686 return false;
687
688 *this = substr(0, size() - Suffix.size());
689 return true;
690 }
691
692 /// Returns true if this StringRef has the given suffix, ignoring case,
693 /// and removes that suffix.
695 if (!ends_with_insensitive(Suffix))
696 return false;
697
698 *this = substr(0, size() - Suffix.size());
699 return true;
700 }
701
702 /// Return a reference to the substring from [Start, End).
703 ///
704 /// \param Start The index of the starting character in the substring; if
705 /// the index is npos or greater than the length of the string then the
706 /// empty substring will be returned.
707 ///
708 /// \param End The index following the last character to include in the
709 /// substring. If this is npos or exceeds the number of characters
710 /// remaining in the string, the string suffix (starting with \p Start)
711 /// will be returned. If this is less than \p Start, an empty string will
712 /// be returned.
713 [[nodiscard]] StringRef slice(size_t Start, size_t End) const {
714 Start = std::min(Start, size());
715 End = std::clamp(End, Start, size());
716 return StringRef(data() + Start, End - Start);
717 }
718
719 /// Split into two substrings around the first occurrence of a separator
720 /// character.
721 ///
722 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
723 /// such that (*this == LHS + Separator + RHS) is true and RHS is
724 /// maximal. If \p Separator is not in the string, then the result is a
725 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
726 ///
727 /// \param Separator The character to split on.
728 /// \returns The split substrings.
729 [[nodiscard]] std::pair<StringRef, StringRef> split(char Separator) const {
730 return split(StringRef(&Separator, 1));
731 }
732
733 /// Split into two substrings around the first occurrence of a separator
734 /// string.
735 ///
736 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
737 /// such that (*this == LHS + Separator + RHS) is true and RHS is
738 /// maximal. If \p Separator is not in the string, then the result is a
739 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
740 ///
741 /// \param Separator - The string to split on.
742 /// \return - The split substrings.
743 [[nodiscard]] std::pair<StringRef, StringRef>
744 split(StringRef Separator) const {
745 size_t Idx = find(Separator);
746 if (Idx == npos)
747 return {*this, StringRef()};
748 return {slice(0, Idx), substr(Idx + Separator.size())};
749 }
750
751 /// Split into two substrings around the last occurrence of a separator
752 /// string.
753 ///
754 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
755 /// such that (*this == LHS + Separator + RHS) is true and RHS is
756 /// minimal. If \p Separator is not in the string, then the result is a
757 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
758 ///
759 /// \param Separator - The string to split on.
760 /// \return - The split substrings.
761 [[nodiscard]] std::pair<StringRef, StringRef>
762 rsplit(StringRef Separator) const {
763 size_t Idx = rfind(Separator);
764 if (Idx == npos)
765 return {*this, StringRef()};
766 return {slice(0, Idx), substr(Idx + Separator.size())};
767 }
768
769 /// Split into substrings around the occurrences of a separator string.
770 ///
771 /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
772 /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
773 /// elements are added to A.
774 /// If \p KeepEmpty is false, empty strings are not added to \p A. They
775 /// still count when considering \p MaxSplit
776 /// An useful invariant is that
777 /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
778 ///
779 /// \param A - Where to put the substrings.
780 /// \param Separator - The string to split on.
781 /// \param MaxSplit - The maximum number of times the string is split.
782 /// \param KeepEmpty - True if empty substring should be added.
784 int MaxSplit = -1, bool KeepEmpty = true) const;
785
786 /// Split into substrings around the occurrences of a separator character.
787 ///
788 /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most
789 /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1
790 /// elements are added to A.
791 /// If \p KeepEmpty is false, empty strings are not added to \p A. They
792 /// still count when considering \p MaxSplit
793 /// An useful invariant is that
794 /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true
795 ///
796 /// \param A - Where to put the substrings.
797 /// \param Separator - The string to split on.
798 /// \param MaxSplit - The maximum number of times the string is split.
799 /// \param KeepEmpty - True if empty substring should be added.
800 LLVM_ABI void split(SmallVectorImpl<StringRef> &A, char Separator,
801 int MaxSplit = -1, bool KeepEmpty = true) const;
802
803 /// Split into two substrings around the last occurrence of a separator
804 /// character.
805 ///
806 /// If \p Separator is in the string, then the result is a pair (LHS, RHS)
807 /// such that (*this == LHS + Separator + RHS) is true and RHS is
808 /// minimal. If \p Separator is not in the string, then the result is a
809 /// pair (LHS, RHS) where (*this == LHS) and (RHS == "").
810 ///
811 /// \param Separator - The character to split on.
812 /// \return - The split substrings.
813 [[nodiscard]] std::pair<StringRef, StringRef> rsplit(char Separator) const {
814 return rsplit(StringRef(&Separator, 1));
815 }
816
817 /// Return string with consecutive \p Char characters starting from the
818 /// the left removed.
819 [[nodiscard]] StringRef ltrim(char Char) const {
820 return drop_front(std::min(size(), find_first_not_of(Char)));
821 }
822
823 /// Return string with consecutive characters in \p Chars starting from
824 /// the left removed.
825 [[nodiscard]] StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
826 return drop_front(std::min(size(), find_first_not_of(Chars)));
827 }
828
829 /// Return string with consecutive \p Char characters starting from the
830 /// right removed.
831 [[nodiscard]] StringRef rtrim(char Char) const {
832 return drop_back(size() - std::min(size(), find_last_not_of(Char) + 1));
833 }
834
835 /// Return string with consecutive characters in \p Chars starting from
836 /// the right removed.
837 [[nodiscard]] StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {
838 return drop_back(size() - std::min(size(), find_last_not_of(Chars) + 1));
839 }
840
841 /// Return string with consecutive \p Char characters starting from the
842 /// left and right removed.
843 [[nodiscard]] StringRef trim(char Char) const {
844 return ltrim(Char).rtrim(Char);
845 }
846
847 /// Return string with consecutive characters in \p Chars starting from
848 /// the left and right removed.
849 [[nodiscard]] StringRef trim(StringRef Chars = " \t\n\v\f\r") const {
850 return ltrim(Chars).rtrim(Chars);
851 }
852
853 /// Detect the line ending style of the string.
854 ///
855 /// If the string contains a line ending, return the line ending character
856 /// sequence that is detected. Otherwise return '\n' for unix line endings.
857 ///
858 /// \return - The line ending character sequence.
859 [[nodiscard]] StringRef detectEOL() const {
860 size_t Pos = find('\r');
861 if (Pos == npos) {
862 // If there is no carriage return, assume unix
863 return "\n";
864 }
865 if (Pos + 1 < size() && data()[Pos + 1] == '\n')
866 return "\r\n"; // Windows
867 if (Pos > 0 && data()[Pos - 1] == '\n')
868 return "\n\r"; // You monster!
869 return "\r"; // Classic Mac
870 }
871 /// @}
872};
873
874/// A wrapper around a string literal that serves as a proxy for constructing
875/// global tables of StringRefs with the length computed at compile time.
876/// In order to avoid the invocation of a global constructor, StringLiteral
877/// should *only* be used in a constexpr context, as such:
878///
879/// constexpr StringLiteral S("test");
880///
881class StringLiteral : public StringRef {
882private:
883 constexpr StringLiteral(const char *Str, size_t N) : StringRef(Str, N) {}
884
885public:
886 template <size_t N>
887 constexpr StringLiteral(const char (&Str)[N])
888#if defined(__clang__) && __has_attribute(enable_if)
889#pragma clang diagnostic push
890#pragma clang diagnostic ignored "-Wgcc-compat"
891 __attribute((enable_if(__builtin_strlen(Str) == N - 1,
892 "invalid string literal")))
893#pragma clang diagnostic pop
894#endif
895 : StringRef(Str, N - 1) {
896 }
897
898 // Explicit construction for strings like "foo\0bar".
899 template <size_t N>
900 static constexpr StringLiteral withInnerNUL(const char (&Str)[N]) {
901 return StringLiteral(Str, N - 1);
902 }
903};
904
905/// @name StringRef Comparison Operators
906/// @{
907
909 if (LHS.size() != RHS.size())
910 return false;
911 if (LHS.empty())
912 return true;
913 return ::memcmp(LHS.data(), RHS.data(), LHS.size()) == 0;
914}
915
916inline bool operator!=(StringRef LHS, StringRef RHS) { return !(LHS == RHS); }
917
919 return LHS.compare(RHS) < 0;
920}
921
923 return LHS.compare(RHS) <= 0;
924}
925
927 return LHS.compare(RHS) > 0;
928}
929
931 return LHS.compare(RHS) >= 0;
932}
933
934inline std::string &operator+=(std::string &buffer, StringRef string) {
935 return buffer.append(string.data(), string.size());
936}
937
938/// @}
939
940/// Compute a hash_code for a StringRef.
941[[nodiscard]] LLVM_ABI hash_code hash_value(StringRef S);
942
943// Provide DenseMapInfo for StringRefs.
944template <> struct DenseMapInfo<StringRef, void> {
945 static inline StringRef getEmptyKey() {
946 return StringRef(reinterpret_cast<const char *>(~static_cast<uintptr_t>(0)),
947 0);
948 }
949
950 static inline StringRef getTombstoneKey() {
951 return StringRef(reinterpret_cast<const char *>(~static_cast<uintptr_t>(1)),
952 0);
953 }
954
955 LLVM_ABI static unsigned getHashValue(StringRef Val);
956
958 if (RHS.data() == getEmptyKey().data())
959 return LHS.data() == getEmptyKey().data();
960 if (RHS.data() == getTombstoneKey().data())
961 return LHS.data() == getTombstoneKey().data();
962 return LHS == RHS;
963 }
964};
965
966} // end namespace llvm
967
968#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:887
static constexpr StringLiteral withInnerNUL(const char(&Str)[N])
Definition StringRef.h:900
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:729
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:849
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:684
bool consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
Definition StringRef.h:518
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:489
const char * iterator
Definition StringRef.h:59
iterator_range< const unsigned char * > bytes() const
Definition StringRef.h:127
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:221
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:303
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:590
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:257
constexpr bool empty() const
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:455
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:616
bool ends_with(char Suffix) const
Definition StringRef.h:274
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:342
char operator[](size_t Index) const
Definition StringRef.h:231
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:628
bool contains_insensitive(char C) const
Return true if the given character is contained in *this, and false otherwise.
Definition StringRef.h:461
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:813
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:648
size_t size_type
Definition StringRef.h:61
char back() const
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:713
constexpr size_t size() const
Get the string size.
Definition StringRef.h:143
char front() const
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:261
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:420
constexpr const char * data() const
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:327
StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
Definition StringRef.h:819
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition StringRef.h:445
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition StringRef.h:664
StringRef detectEOL() const
Detect the line ending style of the string.
Definition StringRef.h:859
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:395
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:364
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:831
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:451
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:608
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:599
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:622
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition StringRef.h:289
StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Definition StringRef.h:843
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:470
bool consume_back_insensitive(StringRef Suffix)
Returns true if this StringRef has the given suffix, ignoring case, and removes that suffix.
Definition StringRef.h:694
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:269
std::pair< StringRef, StringRef > rsplit(StringRef Separator) const
Split into two substrings around the last occurrence of a separator string.
Definition StringRef.h:762
std::pair< StringRef, StringRef > split(StringRef Separator) const
Split into two substrings around the first occurrence of a separator string.
Definition StringRef.h:744
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:825
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:837
bool consume_front(char Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition StringRef.h:654
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:642
const unsigned char * bytes_begin() const
Definition StringRef.h:121
int compare(StringRef RHS) const
Compare two strings; the result is negative, zero, or positive if this string is lexicographically le...
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:635
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:674
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:248
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:318
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.
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:1668
LLVM_ABI unsigned getAutoSenseRadix(StringRef &Str)
bool operator!=(uint64_t V1, const APInt &V2)
Definition APInt.h:2142
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:2011
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:874
#define N
static bool isEqual(StringRef LHS, StringRef RHS)
Definition StringRef.h:957
static LLVM_ABI unsigned getHashValue(StringRef Val)
An information struct used to provide DenseMap with the various necessary components for a given valu...