Skip to content

Commit

Permalink
[ADT] Teach StringRef to derive from std::string_view (llvm#113752)
Browse files Browse the repository at this point in the history
This patch makes minimum changes to have StringRef derive from
std::string_view, with an eventual goal of removing StringRef
completely.  Subsequent patches will further clean up the remaining
bits.

I've chosen public inheritance over private one because our codebase
relies on implicit conversions to std::string_view, which is currently
provided by:

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

This implicit conversion stops applying once we use std::string_view
as a base class, public or private.  If we chose a private base class,
we would lose the implicit conversion.
  • Loading branch information
kazutakahirata authored Oct 26, 2024
1 parent 9672375 commit a3181b1
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions llvm/include/llvm/ADT/StringRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ namespace llvm {
/// situations where the character data resides in some other buffer, whose
/// lifetime extends past that of the StringRef. For this reason, it is not in
/// general safe to store a StringRef.
class LLVM_GSL_POINTER StringRef {
class LLVM_GSL_POINTER StringRef : public std::string_view {
using Base = std::string_view;

public:
static constexpr size_t npos = ~size_t(0);

Expand All @@ -60,12 +62,6 @@ namespace llvm {
using const_reverse_iterator = std::reverse_iterator<const_iterator>;

private:
/// The start of the string, in an external buffer.
const char *Data = nullptr;

/// The length of the string.
size_t Length = 0;

// Workaround memcmp issue with null pointers (undefined behavior)
// by providing a specialized version
static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
Expand All @@ -86,27 +82,25 @@ namespace llvm {

/// Construct a string ref from a cstring.
/*implicit*/ constexpr StringRef(const char *Str)
: Data(Str), Length(Str ?
: Base(Str, Str ?
// GCC 7 doesn't have constexpr char_traits. Fall back to __builtin_strlen.
#if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 8
__builtin_strlen(Str)
__builtin_strlen(Str)
#else
std::char_traits<char>::length(Str)
std::char_traits<char>::length(Str)
#endif
: 0) {
: 0) {
}

/// Construct a string ref from a pointer and length.
/*implicit*/ constexpr StringRef(const char *data, size_t length)
: Data(data), Length(length) {}
: Base(data, length) {}

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

/// Construct a string ref from an std::string_view.
/*implicit*/ constexpr StringRef(std::string_view Str)
: Data(Str.data()), Length(Str.size()) {}
/*implicit*/ constexpr StringRef(std::string_view Str) : Base(Str) {}

/// @}
/// @name Iterators
Expand Down Expand Up @@ -138,16 +132,9 @@ namespace llvm {
/// @name String Operations
/// @{

/// data - Get a pointer to the start of the string (which may not be null
/// terminated).
[[nodiscard]] constexpr const char *data() const { return Data; }

/// empty - Check if the string is empty.
[[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());
Expand Down Expand Up @@ -248,14 +235,6 @@ namespace llvm {
std::enable_if_t<std::is_same<T, std::string>::value, StringRef> &
operator=(T &&Str) = delete;

/// @}
/// @name Type Conversions
/// @{

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

/// @}
/// @name String Predicates
/// @{
Expand Down

0 comments on commit a3181b1

Please sign in to comment.