Skip to content

[ADT] Use data() and size() within StringRef (NFC) #113657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions llvm/include/llvm/ADT/StringRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace llvm {

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

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

iterator begin() const { return Data; }
iterator begin() const { return data(); }

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

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

/// empty - Check if the string is empty.
[[nodiscard]] constexpr bool empty() const { return Length == 0; }
[[nodiscard]] constexpr bool empty() const { return size() == 0; }

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

/// front - Get the first character in the string.
[[nodiscard]] char front() const {
assert(!empty());
return Data[0];
return data()[0];
}

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

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

/// Check for string equality, ignoring case.
[[nodiscard]] bool equals_insensitive(StringRef RHS) const {
return Length == RHS.Length && compare_insensitive(RHS) == 0;
return size() == RHS.size() && compare_insensitive(RHS) == 0;
}

/// compare - Compare two strings; the result is negative, zero, or positive
/// if this string is lexicographically less than, equal to, or greater than
/// the \p RHS.
[[nodiscard]] int compare(StringRef RHS) const {
// Check the prefix for a mismatch.
if (int Res = compareMemory(Data, RHS.Data, std::min(Length, RHS.Length)))
if (int Res =
compareMemory(data(), RHS.data(), std::min(size(), RHS.size())))
return Res < 0 ? -1 : 1;

// Otherwise the prefixes match, so we only need to check the lengths.
if (Length == RHS.Length)
if (size() == RHS.size())
return 0;
return Length < RHS.Length ? -1 : 1;
return size() < RHS.size() ? -1 : 1;
}

/// Compare two strings, ignoring case.
Expand Down Expand Up @@ -225,17 +226,18 @@ namespace llvm {

/// str - Get the contents as an std::string.
[[nodiscard]] std::string str() const {
if (!Data) return std::string();
return std::string(Data, Length);
if (!data())
return std::string();
return std::string(data(), size());
}

/// @}
/// @name Operator Overloads
/// @{

[[nodiscard]] char operator[](size_t Index) const {
assert(Index < Length && "Invalid index!");
return Data[Index];
assert(Index < size() && "Invalid index!");
return data()[Index];
}

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

/// Check if this string starts with the given \p Prefix.
[[nodiscard]] bool starts_with(StringRef Prefix) const {
return Length >= Prefix.Length &&
compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
return size() >= Prefix.size() &&
compareMemory(data(), Prefix.data(), Prefix.size()) == 0;
}
[[nodiscard]] bool starts_with(char Prefix) const {
return !empty() && front() == Prefix;
Expand All @@ -272,9 +274,9 @@ namespace llvm {

/// Check if this string ends with the given \p Suffix.
[[nodiscard]] bool ends_with(StringRef Suffix) const {
return Length >= Suffix.Length &&
compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) ==
0;
return size() >= Suffix.size() &&
compareMemory(end() - Suffix.size(), Suffix.data(),
Suffix.size()) == 0;
}
[[nodiscard]] bool ends_with(char Suffix) const {
return !empty() && back() == Suffix;
Expand Down Expand Up @@ -342,10 +344,10 @@ namespace llvm {
/// \returns The index of the last occurrence of \p C, or npos if not
/// found.
[[nodiscard]] size_t rfind(char C, size_t From = npos) const {
size_t I = std::min(From, Length);
size_t I = std::min(From, size());
while (I) {
--I;
if (Data[I] == C)
if (data()[I] == C)
return I;
}
return npos;
Expand Down Expand Up @@ -447,8 +449,8 @@ namespace llvm {
/// Return the number of occurrences of \p C in the string.
[[nodiscard]] size_t count(char C) const {
size_t Count = 0;
for (size_t I = 0; I != Length; ++I)
if (Data[I] == C)
for (size_t I = 0; I != size(); ++I)
if (data()[I] == C)
++Count;
return Count;
}
Expand Down Expand Up @@ -567,8 +569,8 @@ namespace llvm {
/// suffix (starting with \p Start) will be returned.
[[nodiscard]] constexpr StringRef substr(size_t Start,
size_t N = npos) const {
Start = std::min(Start, Length);
return StringRef(Data + Start, std::min(N, Length - Start));
Start = std::min(Start, size());
return StringRef(data() + Start, std::min(N, size() - Start));
}

/// Return a StringRef equal to 'this' but with only the first \p N
Expand Down Expand Up @@ -679,9 +681,9 @@ namespace llvm {
/// will be returned. If this is less than \p Start, an empty string will
/// be returned.
[[nodiscard]] StringRef slice(size_t Start, size_t End) const {
Start = std::min(Start, Length);
End = std::clamp(End, Start, Length);
return StringRef(Data + Start, End - Start);
Start = std::min(Start, size());
End = std::clamp(End, Start, size());
return StringRef(data() + Start, End - Start);
}

/// Split into two substrings around the first occurrence of a separator
Expand Down Expand Up @@ -786,25 +788,25 @@ namespace llvm {
/// Return string with consecutive \p Char characters starting from the
/// the left removed.
[[nodiscard]] StringRef ltrim(char Char) const {
return drop_front(std::min(Length, find_first_not_of(Char)));
return drop_front(std::min(size(), find_first_not_of(Char)));
}

/// Return string with consecutive characters in \p Chars starting from
/// the left removed.
[[nodiscard]] StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
return drop_front(std::min(Length, find_first_not_of(Chars)));
return drop_front(std::min(size(), find_first_not_of(Chars)));
}

/// Return string with consecutive \p Char characters starting from the
/// right removed.
[[nodiscard]] StringRef rtrim(char Char) const {
return drop_back(Length - std::min(Length, find_last_not_of(Char) + 1));
return drop_back(size() - std::min(size(), find_last_not_of(Char) + 1));
}

/// Return string with consecutive characters in \p Chars starting from
/// the right removed.
[[nodiscard]] StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {
return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1));
return drop_back(size() - std::min(size(), find_last_not_of(Chars) + 1));
}

/// Return string with consecutive \p Char characters starting from the
Expand All @@ -831,9 +833,9 @@ namespace llvm {
// If there is no carriage return, assume unix
return "\n";
}
if (Pos + 1 < Length && Data[Pos + 1] == '\n')
if (Pos + 1 < size() && data()[Pos + 1] == '\n')
return "\r\n"; // Windows
if (Pos > 0 && Data[Pos - 1] == '\n')
if (Pos > 0 && data()[Pos - 1] == '\n')
return "\n\r"; // You monster!
return "\r"; // Classic Mac
}
Expand Down
Loading
Loading