Skip to content

[ADT] Teach StringRef to take basic features from std::string_view (NFC) #113797

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
49 changes: 20 additions & 29 deletions llvm/include/llvm/ADT/StringRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ namespace llvm {
/// general safe to store a StringRef.
class LLVM_GSL_POINTER StringRef {
public:
static constexpr size_t npos = ~size_t(0);
static constexpr size_t npos = std::string_view::npos;

using iterator = const char *;
using const_iterator = const char *;
using size_type = size_t;
using value_type = char;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using iterator = std::string_view::iterator;
using const_iterator = std::string_view::const_iterator;
using size_type = std::string_view::size_type;
using value_type = std::string_view::value_type;
using reverse_iterator = std::string_view::reverse_iterator;
using const_reverse_iterator = std::string_view::const_reverse_iterator;

private:
std::string_view View;
Expand Down Expand Up @@ -106,23 +106,16 @@ namespace llvm {
/// @name Iterators
/// @{

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

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

reverse_iterator rbegin() const {
return std::make_reverse_iterator(end());
}

reverse_iterator rend() const {
return std::make_reverse_iterator(begin());
}
iterator begin() const { return View.begin(); }
iterator end() const { return View.end(); }
reverse_iterator rbegin() const { return View.rbegin(); }
reverse_iterator rend() const { return View.rend(); }

const unsigned char *bytes_begin() const {
return reinterpret_cast<const unsigned char *>(begin());
return reinterpret_cast<const unsigned char *>(data());
}
const unsigned char *bytes_end() const {
return reinterpret_cast<const unsigned char *>(end());
return reinterpret_cast<const unsigned char *>(data() + size());
}
iterator_range<const unsigned char *> bytes() const {
return make_range(bytes_begin(), bytes_end());
Expand All @@ -137,21 +130,21 @@ namespace llvm {
[[nodiscard]] constexpr const char *data() const { return View.data(); }

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

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

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

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

// copy - Allocate copy in Allocator and return StringRef to it.
Expand Down Expand Up @@ -222,7 +215,7 @@ namespace llvm {
[[nodiscard]] std::string str() const {
if (!data())
return std::string();
return std::string(data(), size());
return std::string(View);
}

/// @}
Expand All @@ -246,9 +239,7 @@ namespace llvm {
/// @name Type Conversions
/// @{

constexpr operator std::string_view() const {
return std::string_view(data(), size());
}
constexpr operator std::string_view() const { return View; }

/// @}
/// @name String Predicates
Expand All @@ -269,7 +260,7 @@ namespace llvm {
/// Check if this string ends with the given \p Suffix.
[[nodiscard]] bool ends_with(StringRef Suffix) const {
return size() >= Suffix.size() &&
compareMemory(end() - Suffix.size(), Suffix.data(),
compareMemory(data() + size() - Suffix.size(), Suffix.data(),
Suffix.size()) == 0;
}
[[nodiscard]] bool ends_with(char Suffix) const {
Expand All @@ -288,7 +279,7 @@ namespace llvm {
/// \returns The index of the first occurrence of \p C, or npos if not
/// found.
[[nodiscard]] size_t find(char C, size_t From = 0) const {
return std::string_view(*this).find(C, From);
return View.find(C, From);
}

/// Search for the first character \p C in the string, ignoring case.
Expand Down
6 changes: 2 additions & 4 deletions llvm/lib/Support/StringRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,7 @@ size_t StringRef::rfind_insensitive(char C, size_t From) const {
///
/// \return - The index of the last occurrence of \arg Str, or npos if not
/// found.
size_t StringRef::rfind(StringRef Str) const {
return std::string_view(*this).rfind(Str);
}
size_t StringRef::rfind(StringRef Str) const { return View.rfind(Str); }

size_t StringRef::rfind_insensitive(StringRef Str) const {
size_t N = Str.size();
Expand Down Expand Up @@ -251,7 +249,7 @@ StringRef::size_type StringRef::find_first_of(StringRef Chars,
/// find_first_not_of - Find the first character in the string that is not
/// \arg C or npos if not found.
StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
return std::string_view(*this).find_first_not_of(C, From);
return View.find_first_not_of(C, From);
}

/// find_first_not_of - Find the first character in the string that is not
Expand Down
Loading