@@ -102,7 +102,7 @@ namespace llvm {
102
102
103
103
// / Construct a string ref from an std::string.
104
104
/* implicit*/ StringRef(const std::string &Str)
105
- : Data(Str.data()), Length(Str.length()) {}
105
+ : Data(Str.data()), Length(Str.length()) {}
106
106
107
107
// / Construct a string ref from an std::string_view.
108
108
/* implicit*/ constexpr StringRef (std::string_view Str)
@@ -112,9 +112,9 @@ namespace llvm {
112
112
// / @name Iterators
113
113
// / @{
114
114
115
- iterator begin () const { return Data ; }
115
+ iterator begin () const { return data () ; }
116
116
117
- iterator end () const { return Data + Length ; }
117
+ iterator end () const { return data () + size () ; }
118
118
119
119
reverse_iterator rbegin () const {
120
120
return std::make_reverse_iterator (end ());
@@ -143,21 +143,21 @@ namespace llvm {
143
143
[[nodiscard]] constexpr const char *data () const { return Data; }
144
144
145
145
// / empty - Check if the string is empty.
146
- [[nodiscard]] constexpr bool empty () const { return Length == 0 ; }
146
+ [[nodiscard]] constexpr bool empty () const { return size () == 0 ; }
147
147
148
148
// / size - Get the string size.
149
149
[[nodiscard]] constexpr size_t size () const { return Length; }
150
150
151
151
// / front - Get the first character in the string.
152
152
[[nodiscard]] char front () const {
153
153
assert (!empty ());
154
- return Data [0 ];
154
+ return data () [0 ];
155
155
}
156
156
157
157
// / back - Get the last character in the string.
158
158
[[nodiscard]] char back () const {
159
159
assert (!empty ());
160
- return Data[Length- 1 ];
160
+ return data ()[ size () - 1 ];
161
161
}
162
162
163
163
// copy - Allocate copy in Allocator and return StringRef to it.
@@ -166,28 +166,29 @@ namespace llvm {
166
166
// Don't request a length 0 copy from the allocator.
167
167
if (empty ())
168
168
return StringRef ();
169
- char *S = A.template Allocate <char >(Length );
169
+ char *S = A.template Allocate <char >(size () );
170
170
std::copy (begin (), end (), S);
171
- return StringRef (S, Length );
171
+ return StringRef (S, size () );
172
172
}
173
173
174
174
// / Check for string equality, ignoring case.
175
175
[[nodiscard]] bool equals_insensitive (StringRef RHS) const {
176
- return Length == RHS.Length && compare_insensitive (RHS) == 0 ;
176
+ return size () == RHS.size () && compare_insensitive (RHS) == 0 ;
177
177
}
178
178
179
179
// / compare - Compare two strings; the result is negative, zero, or positive
180
180
// / if this string is lexicographically less than, equal to, or greater than
181
181
// / the \p RHS.
182
182
[[nodiscard]] int compare (StringRef RHS) const {
183
183
// Check the prefix for a mismatch.
184
- if (int Res = compareMemory (Data, RHS.Data , std::min (Length, RHS.Length )))
184
+ if (int Res =
185
+ compareMemory (data (), RHS.data (), std::min (size (), RHS.size ())))
185
186
return Res < 0 ? -1 : 1 ;
186
187
187
188
// Otherwise the prefixes match, so we only need to check the lengths.
188
- if (Length == RHS.Length )
189
+ if (size () == RHS.size () )
189
190
return 0 ;
190
- return Length < RHS.Length ? -1 : 1 ;
191
+ return size () < RHS.size () ? -1 : 1 ;
191
192
}
192
193
193
194
// / Compare two strings, ignoring case.
@@ -225,17 +226,18 @@ namespace llvm {
225
226
226
227
// / str - Get the contents as an std::string.
227
228
[[nodiscard]] std::string str () const {
228
- if (!Data) return std::string ();
229
- return std::string (Data, Length);
229
+ if (!data ())
230
+ return std::string ();
231
+ return std::string (data (), size ());
230
232
}
231
233
232
234
// / @}
233
235
// / @name Operator Overloads
234
236
// / @{
235
237
236
238
[[nodiscard]] char operator [](size_t Index) const {
237
- assert (Index < Length && " Invalid index!" );
238
- return Data [Index];
239
+ assert (Index < size () && " Invalid index!" );
240
+ return data () [Index];
239
241
}
240
242
241
243
// / Disallow accidental assignment from a temporary std::string.
@@ -260,8 +262,8 @@ namespace llvm {
260
262
261
263
// / Check if this string starts with the given \p Prefix.
262
264
[[nodiscard]] bool starts_with (StringRef Prefix) const {
263
- return Length >= Prefix.Length &&
264
- compareMemory (Data , Prefix.Data , Prefix.Length ) == 0 ;
265
+ return size () >= Prefix.size () &&
266
+ compareMemory (data () , Prefix.data () , Prefix.size () ) == 0 ;
265
267
}
266
268
[[nodiscard]] bool starts_with (char Prefix) const {
267
269
return !empty () && front () == Prefix;
@@ -272,9 +274,9 @@ namespace llvm {
272
274
273
275
// / Check if this string ends with the given \p Suffix.
274
276
[[nodiscard]] bool ends_with (StringRef Suffix) const {
275
- return Length >= Suffix.Length &&
276
- compareMemory (end () - Suffix.Length , Suffix.Data , Suffix. Length ) ==
277
- 0 ;
277
+ return size () >= Suffix.size () &&
278
+ compareMemory (end () - Suffix.size () , Suffix.data (),
279
+ Suffix. size ()) == 0 ;
278
280
}
279
281
[[nodiscard]] bool ends_with (char Suffix) const {
280
282
return !empty () && back () == Suffix;
@@ -342,10 +344,10 @@ namespace llvm {
342
344
// / \returns The index of the last occurrence of \p C, or npos if not
343
345
// / found.
344
346
[[nodiscard]] size_t rfind (char C, size_t From = npos) const {
345
- size_t I = std::min (From, Length );
347
+ size_t I = std::min (From, size () );
346
348
while (I) {
347
349
--I;
348
- if (Data [I] == C)
350
+ if (data () [I] == C)
349
351
return I;
350
352
}
351
353
return npos;
@@ -447,8 +449,8 @@ namespace llvm {
447
449
// / Return the number of occurrences of \p C in the string.
448
450
[[nodiscard]] size_t count (char C) const {
449
451
size_t Count = 0 ;
450
- for (size_t I = 0 ; I != Length ; ++I)
451
- if (Data [I] == C)
452
+ for (size_t I = 0 ; I != size () ; ++I)
453
+ if (data () [I] == C)
452
454
++Count;
453
455
return Count;
454
456
}
@@ -567,8 +569,8 @@ namespace llvm {
567
569
// / suffix (starting with \p Start) will be returned.
568
570
[[nodiscard]] constexpr StringRef substr (size_t Start,
569
571
size_t N = npos) const {
570
- Start = std::min (Start, Length );
571
- return StringRef (Data + Start, std::min (N, Length - Start));
572
+ Start = std::min (Start, size () );
573
+ return StringRef (data () + Start, std::min (N, size () - Start));
572
574
}
573
575
574
576
// / Return a StringRef equal to 'this' but with only the first \p N
@@ -679,9 +681,9 @@ namespace llvm {
679
681
// / will be returned. If this is less than \p Start, an empty string will
680
682
// / be returned.
681
683
[[nodiscard]] StringRef slice (size_t Start, size_t End) const {
682
- Start = std::min (Start, Length );
683
- End = std::clamp (End, Start, Length );
684
- return StringRef (Data + Start, End - Start);
684
+ Start = std::min (Start, size () );
685
+ End = std::clamp (End, Start, size () );
686
+ return StringRef (data () + Start, End - Start);
685
687
}
686
688
687
689
// / Split into two substrings around the first occurrence of a separator
@@ -786,25 +788,25 @@ namespace llvm {
786
788
// / Return string with consecutive \p Char characters starting from the
787
789
// / the left removed.
788
790
[[nodiscard]] StringRef ltrim (char Char) const {
789
- return drop_front (std::min (Length , find_first_not_of (Char)));
791
+ return drop_front (std::min (size () , find_first_not_of (Char)));
790
792
}
791
793
792
794
// / Return string with consecutive characters in \p Chars starting from
793
795
// / the left removed.
794
796
[[nodiscard]] StringRef ltrim (StringRef Chars = " \t\n\v\f\r " ) const {
795
- return drop_front (std::min (Length , find_first_not_of (Chars)));
797
+ return drop_front (std::min (size () , find_first_not_of (Chars)));
796
798
}
797
799
798
800
// / Return string with consecutive \p Char characters starting from the
799
801
// / right removed.
800
802
[[nodiscard]] StringRef rtrim (char Char) const {
801
- return drop_back (Length - std::min (Length , find_last_not_of (Char) + 1 ));
803
+ return drop_back (size () - std::min (size () , find_last_not_of (Char) + 1 ));
802
804
}
803
805
804
806
// / Return string with consecutive characters in \p Chars starting from
805
807
// / the right removed.
806
808
[[nodiscard]] StringRef rtrim (StringRef Chars = " \t\n\v\f\r " ) const {
807
- return drop_back (Length - std::min (Length , find_last_not_of (Chars) + 1 ));
809
+ return drop_back (size () - std::min (size () , find_last_not_of (Chars) + 1 ));
808
810
}
809
811
810
812
// / Return string with consecutive \p Char characters starting from the
@@ -831,9 +833,9 @@ namespace llvm {
831
833
// If there is no carriage return, assume unix
832
834
return " \n " ;
833
835
}
834
- if (Pos + 1 < Length && Data [Pos + 1 ] == ' \n ' )
836
+ if (Pos + 1 < size () && data () [Pos + 1 ] == ' \n ' )
835
837
return " \r\n " ; // Windows
836
- if (Pos > 0 && Data [Pos - 1 ] == ' \n ' )
838
+ if (Pos > 0 && data () [Pos - 1 ] == ' \n ' )
837
839
return " \n\r " ; // You monster!
838
840
return " \r " ; // Classic Mac
839
841
}
0 commit comments