Skip to content

Commit 8b19ed3

Browse files
[ADT] Use data() and size() within StringRef (NFC)
This patch uses data() and size() within StringRef instead of Data and Length. This makes it easier to replace Data and Length with std::string_view in the future, which in turn allows us to forward most of StringRef functions to the counterparts in std::string_view.
1 parent 21b3769 commit 8b19ed3

File tree

2 files changed

+77
-73
lines changed

2 files changed

+77
-73
lines changed

llvm/include/llvm/ADT/StringRef.h

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ namespace llvm {
102102

103103
/// Construct a string ref from an std::string.
104104
/*implicit*/ StringRef(const std::string &Str)
105-
: Data(Str.data()), Length(Str.length()) {}
105+
: Data(Str.data()), Length(Str.length()) {}
106106

107107
/// Construct a string ref from an std::string_view.
108108
/*implicit*/ constexpr StringRef(std::string_view Str)
@@ -112,9 +112,9 @@ namespace llvm {
112112
/// @name Iterators
113113
/// @{
114114

115-
iterator begin() const { return Data; }
115+
iterator begin() const { return data(); }
116116

117-
iterator end() const { return Data + Length; }
117+
iterator end() const { return data() + size(); }
118118

119119
reverse_iterator rbegin() const {
120120
return std::make_reverse_iterator(end());
@@ -143,21 +143,21 @@ namespace llvm {
143143
[[nodiscard]] constexpr const char *data() const { return Data; }
144144

145145
/// 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; }
147147

148148
/// size - Get the string size.
149149
[[nodiscard]] constexpr size_t size() const { return Length; }
150150

151151
/// front - Get the first character in the string.
152152
[[nodiscard]] char front() const {
153153
assert(!empty());
154-
return Data[0];
154+
return data()[0];
155155
}
156156

157157
/// back - Get the last character in the string.
158158
[[nodiscard]] char back() const {
159159
assert(!empty());
160-
return Data[Length-1];
160+
return data()[size() - 1];
161161
}
162162

163163
// copy - Allocate copy in Allocator and return StringRef to it.
@@ -166,28 +166,29 @@ namespace llvm {
166166
// Don't request a length 0 copy from the allocator.
167167
if (empty())
168168
return StringRef();
169-
char *S = A.template Allocate<char>(Length);
169+
char *S = A.template Allocate<char>(size());
170170
std::copy(begin(), end(), S);
171-
return StringRef(S, Length);
171+
return StringRef(S, size());
172172
}
173173

174174
/// Check for string equality, ignoring case.
175175
[[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;
177177
}
178178

179179
/// compare - Compare two strings; the result is negative, zero, or positive
180180
/// if this string is lexicographically less than, equal to, or greater than
181181
/// the \p RHS.
182182
[[nodiscard]] int compare(StringRef RHS) const {
183183
// 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())))
185186
return Res < 0 ? -1 : 1;
186187

187188
// Otherwise the prefixes match, so we only need to check the lengths.
188-
if (Length == RHS.Length)
189+
if (size() == RHS.size())
189190
return 0;
190-
return Length < RHS.Length ? -1 : 1;
191+
return size() < RHS.size() ? -1 : 1;
191192
}
192193

193194
/// Compare two strings, ignoring case.
@@ -225,17 +226,18 @@ namespace llvm {
225226

226227
/// str - Get the contents as an std::string.
227228
[[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());
230232
}
231233

232234
/// @}
233235
/// @name Operator Overloads
234236
/// @{
235237

236238
[[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];
239241
}
240242

241243
/// Disallow accidental assignment from a temporary std::string.
@@ -260,8 +262,8 @@ namespace llvm {
260262

261263
/// Check if this string starts with the given \p Prefix.
262264
[[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;
265267
}
266268
[[nodiscard]] bool starts_with(char Prefix) const {
267269
return !empty() && front() == Prefix;
@@ -272,9 +274,9 @@ namespace llvm {
272274

273275
/// Check if this string ends with the given \p Suffix.
274276
[[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;
278280
}
279281
[[nodiscard]] bool ends_with(char Suffix) const {
280282
return !empty() && back() == Suffix;
@@ -342,10 +344,10 @@ namespace llvm {
342344
/// \returns The index of the last occurrence of \p C, or npos if not
343345
/// found.
344346
[[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());
346348
while (I) {
347349
--I;
348-
if (Data[I] == C)
350+
if (data()[I] == C)
349351
return I;
350352
}
351353
return npos;
@@ -447,8 +449,8 @@ namespace llvm {
447449
/// Return the number of occurrences of \p C in the string.
448450
[[nodiscard]] size_t count(char C) const {
449451
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)
452454
++Count;
453455
return Count;
454456
}
@@ -567,8 +569,8 @@ namespace llvm {
567569
/// suffix (starting with \p Start) will be returned.
568570
[[nodiscard]] constexpr StringRef substr(size_t Start,
569571
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));
572574
}
573575

574576
/// Return a StringRef equal to 'this' but with only the first \p N
@@ -679,9 +681,9 @@ namespace llvm {
679681
/// will be returned. If this is less than \p Start, an empty string will
680682
/// be returned.
681683
[[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);
685687
}
686688

687689
/// Split into two substrings around the first occurrence of a separator
@@ -786,25 +788,25 @@ namespace llvm {
786788
/// Return string with consecutive \p Char characters starting from the
787789
/// the left removed.
788790
[[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)));
790792
}
791793

792794
/// Return string with consecutive characters in \p Chars starting from
793795
/// the left removed.
794796
[[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)));
796798
}
797799

798800
/// Return string with consecutive \p Char characters starting from the
799801
/// right removed.
800802
[[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));
802804
}
803805

804806
/// Return string with consecutive characters in \p Chars starting from
805807
/// the right removed.
806808
[[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));
808810
}
809811

810812
/// Return string with consecutive \p Char characters starting from the
@@ -831,9 +833,9 @@ namespace llvm {
831833
// If there is no carriage return, assume unix
832834
return "\n";
833835
}
834-
if (Pos + 1 < Length && Data[Pos + 1] == '\n')
836+
if (Pos + 1 < size() && data()[Pos + 1] == '\n')
835837
return "\r\n"; // Windows
836-
if (Pos > 0 && Data[Pos - 1] == '\n')
838+
if (Pos > 0 && data()[Pos - 1] == '\n')
837839
return "\n\r"; // You monster!
838840
return "\r"; // Classic Mac
839841
}

0 commit comments

Comments
 (0)