LLVM: include/llvm/ADT/StringRef.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9#ifndef LLVM_ADT_STRINGREF_H
10#define LLVM_ADT_STRINGREF_H
11
16#include
17#include
18#include
19#include
20#include
21#include
22#include
23#include <string_view>
24#include <type_traits>
25#include
26
27namespace llvm {
28
33
34
36 unsigned long long &Result);
37
39 long long &Result);
40
42
44 unsigned long long &Result);
46 long long &Result);
47
48
49
50
51
52
53
54
56 public:
57 static constexpr size_t npos = ~size_t(0);
58
65
66 private:
67
68 const char *Data = nullptr;
69
70
71 size_t Length = 0;
72
73
74
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
82
83
84
86
87
88
90
91
94
95
97 size_t length)
98 : Data(data), Length(length) {}
99
100
101 StringRef(const std::string &Str)
102 : Data(Str.data()), Length(Str.length()) {}
103
104
107
108
109
110
111
113
115
117 return std::make_reverse_iterator(end());
118 }
119
121 return std::make_reverse_iterator(begin());
122 }
123
125 return reinterpret_cast<const unsigned char *>(begin());
126 }
128 return reinterpret_cast<const unsigned char *>(end());
129 }
133
134
135
136
137
138
139
140 [[nodiscard]] constexpr const char *data() const { return Data; }
141
142
143 [[nodiscard]] constexpr bool empty() const { return size() == 0; }
144
145
146 [[nodiscard]] constexpr size_t size() const { return Length; }
147
148
149 [[nodiscard]] char front() const {
151 return data()[0];
152 }
153
154
155 [[nodiscard]] char back() const {
158 }
159
160
161 template
163
166 char *S = A.template Allocate(size());
167 std::copy(begin(), end(), S);
169 }
170
171
175
176
177
178
180
181 if (int Res =
182 compareMemory(data(), RHS.data(), std::min(size(), RHS.size())))
183 return Res < 0 ? -1 : 1;
184
185
187 return 0;
188 return size() < RHS.size() ? -1 : 1;
189 }
190
191
193
194
195
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
225 [[nodiscard]] std::string str() const {
227 return std::string();
228 return std::string(data(), size());
229 }
230
231
232
233
234
235 [[nodiscard]] char operator[](size_t Index) const {
236 assert(Index < size() && "Invalid index!");
237 return data()[Index];
238 }
239
240
241
242
243
244 template
245 std::enable_if_t<std::is_same<T, std::string>::value, StringRef> &
247
248
249
250
251
252 constexpr operator std::string_view() const {
253 return std::string_view(data(), size());
254 }
255
256
257
258
259
260
262 return size() >= Prefix.size() &&
263 compareMemory(data(), Prefix.data(), Prefix.size()) == 0;
264 }
268
269
270 [[nodiscard]] LLVM_ABI bool starts_with_insensitive(StringRef Prefix) const;
271
272
274 return size() >= Suffix.size() &&
275 compareMemory(end() - Suffix.size(), Suffix.data(),
276 Suffix.size()) == 0;
277 }
281
282
283 [[nodiscard]] LLVM_ABI bool ends_with_insensitive(StringRef Suffix) const;
284
285
286
287
288
289
290
291
292
293 [[nodiscard]] size_t find(char C, size_t From = 0) const {
294 return std::string_view(*this).find(C, From);
295 }
296
297
298
299
300
301 [[nodiscard]] LLVM_ABI size_t find_insensitive(char C,
302 size_t From = 0) const;
303
304
305
306
307
309 size_t From = 0) const {
311 while (!S.empty()) {
315 }
317 }
318
319
320
321
322
324 size_t From = 0) const {
325 return find_if([F](char c) { return (c); }, From);
326 }
327
328
329
330
331
333
334
335
336
337
339 size_t From = 0) const;
340
341
342
343
344
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;
350 return I;
351 }
353 }
354
355
356
357
358
359 [[nodiscard]] LLVM_ABI size_t rfind_insensitive(char C,
360 size_t From = npos) const;
361
362
363
364
365
367
368
369
370
371
372 [[nodiscard]] LLVM_ABI size_t rfind_insensitive(StringRef Str) const;
373
374
375
379
380
381
382
383
385 size_t From = 0) const;
386
387
388
389 [[nodiscard]] LLVM_ABI size_t find_first_not_of(char C,
390 size_t From = 0) const;
391
392
393
394
395
397 size_t From = 0) const;
398
399
400
403 }
404
405
406
407
408
410 size_t From = npos) const;
411
412
413
414 [[nodiscard]] LLVM_ABI size_t find_last_not_of(char C,
415 size_t From = npos) const;
416
417
418
419
420
422 size_t From = npos) const;
423
424
425
429
430
431
435
436
437
441
442
443
447
448
449
450
451
452
453 [[nodiscard]] size_t count(char C) const {
454 size_t Count = 0;
455 for (size_t I = 0; I != size(); ++I)
459 }
460
461
462
464
465
466
467
468
469
470
471
472 template bool getAsInteger(unsigned Radix, T &Result) const {
473 if constexpr (std::numeric_limits::is_signed) {
474 long long LLVal;
476 static_cast<T>(LLVal) != LLVal)
477 return true;
478 Result = LLVal;
479 } else {
480 unsigned long long ULLVal;
481
482
483
485 static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
486 return true;
487 Result = ULLVal;
488 }
489 return false;
490 }
491
492
493
494
495
496
497
498
499
500
501 template bool consumeInteger(unsigned Radix, T &Result) {
502 if constexpr (std::numeric_limits::is_signed) {
503 long long LLVal;
505 static_cast<long long>(static_cast<T>(LLVal)) != LLVal)
506 return true;
507 Result = LLVal;
508 } else {
509 unsigned long long 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
519
520
521
522
523
524
525
526
527
528 LLVM_ABI bool getAsInteger(unsigned Radix, APInt &Result) const;
529
530
531
532
533
534
535
536
537
538
539 LLVM_ABI bool consumeInteger(unsigned Radix, APInt &Result);
540
541
542
543
544
545
546
547
548 LLVM_ABI bool getAsDouble(double &Result, bool AllowInexact = true) const;
549
550
551
552
553
554
555 [[nodiscard]] LLVM_ABI std::string lower() const;
556
557
558 [[nodiscard]] LLVM_ABI std::string upper() const;
559
560
561
562
563
564
565
566
567
568
569
570
571
572
575 Start = std::min(Start, size());
577 }
578
579
580
581
587
588
589
590
596
597
598
602
603
604
608
609
610
612 assert(size() >= N && "Dropping more elements than exist");
614 }
615
616
617
619 assert(size() >= N && "Dropping more elements than exist");
621 }
622
623
624
628
629
630
634
635
636
639 return false;
640
641 *this = substr(Prefix.size());
642 return true;
643 }
644
645
646
649 return false;
650
651 *this = substr(Prefix.size());
652 return true;
653 }
654
655
656
659 return false;
660
662 return true;
663 }
664
665
666
669 return false;
670
672 return true;
673 }
674
675
676
677
678
679
680
681
682
683
684
685
687 Start = std::min(Start, size());
688 End = std::clamp(End, Start, size());
690 }
691
692
693
694
695
696
697
698
699
700
701
702 [[nodiscard]] std::pair<StringRef, StringRef> split(char Separator) const {
704 }
705
706
707
708
709
710
711
712
713
714
715
716 [[nodiscard]] std::pair<StringRef, StringRef>
718 size_t Idx = find(Separator);
719 if (Idx == npos)
722 }
723
724
725
726
727
728
729
730
731
732
733
734 [[nodiscard]] std::pair<StringRef, StringRef>
736 size_t Idx = rfind(Separator);
737 if (Idx == npos)
740 }
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
757 int MaxSplit = -1, bool KeepEmpty = true) const;
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
774 int MaxSplit = -1, bool KeepEmpty = true) const;
775
776
777
778
779
780
781
782
783
784
785
786 [[nodiscard]] std::pair<StringRef, StringRef> rsplit(char Separator) const {
788 }
789
790
791
795
796
797
801
802
803
807
808
809
813
814
815
817 return ltrim(Char).rtrim(Char);
818 }
819
820
821
823 return ltrim(Chars).rtrim(Chars);
824 }
825
826
827
828
829
830
831
833 size_t Pos = find('\r');
834 if (Pos == npos) {
835
836 return "\n";
837 }
838 if (Pos + 1 < size() && data()[Pos + 1] == '\n')
839 return "\r\n";
840 if (Pos > 0 && data()[Pos - 1] == '\n')
841 return "\n\r";
842 return "\r";
843 }
844
845 };
846
847
848
849
850
851
852
853
855 private:
856 constexpr StringLiteral(const char *Str, size_t N) : StringRef(Str, N) {
857 }
858
859 public:
860 template <size_t 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
870 }
871
872
873 template <size_t N>
874 static constexpr StringLiteral withInnerNUL(const char (&Str)[N]) {
875 return StringLiteral(Str, N - 1);
876 }
877 };
878
879
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
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
909 return buffer.append(string.data(), string.size());
910 }
911
912
913
914
916
917
921 reinterpret_cast<const char *>(~static_cast<uintptr_t>(0)), 0);
922 }
923
926 reinterpret_cast<const char *>(~static_cast<uintptr_t>(1)), 0);
927 }
928
930
938 };
939
940}
941
942#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_LIFETIME_BOUND
#define LLVM_GSL_POINTER
LLVM_GSL_POINTER - Apply this to non-owning classes like StringRef to enable lifetime warnings.
static constexpr size_t npos
This file defines DenseMapInfo traits for DenseMap.
static StringRef substr(StringRef Str, uint64_t Len)
static const BasicSubtargetSubTypeKV * find(StringRef S, ArrayRef< BasicSubtargetSubTypeKV > A)
Find KV in array using binary search.
Class for arbitrary precision integers.
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
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: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.
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.
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
char value_type
Definition StringRef.h:62
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.
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.
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.
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.
This is an optimization pass for GlobalISel generic memory operations.
bool operator<(int64_t V1, const APSInt &V2)
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.
LLVM_ABI unsigned getAutoSenseRadix(StringRef &Str)
bool operator!=(uint64_t V1, const APInt &V2)
bool operator>=(int64_t V1, const APSInt &V2)
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)
FunctionAddr VTableAddr Count
iterator_range< SplittingIterator > split(StringRef Str, StringRef Separator)
Split the specified string over a separator and return a range-compatible iterable over its partition...
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...
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)
Implement std::hash so that hash_code can be used in STL containers.
static StringRef getEmptyKey()
Definition StringRef.h:919
static bool isEqual(StringRef LHS, StringRef RHS)
Definition StringRef.h:931
static LLVM_ABI unsigned getHashValue(StringRef Val)
static StringRef getTombstoneKey()
Definition StringRef.h:924
An information struct used to provide DenseMap with the various necessary components for a given valu...