Thanks to visit codestin.com
Credit goes to llvm.org

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