Skip to content

Commit a3181b1

Browse files
[ADT] Teach StringRef to derive from std::string_view (#113752)
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.
1 parent 9672375 commit a3181b1

File tree

1 file changed

+10
-31
lines changed

1 file changed

+10
-31
lines changed

llvm/include/llvm/ADT/StringRef.h

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ namespace llvm {
4848
/// situations where the character data resides in some other buffer, whose
4949
/// lifetime extends past that of the StringRef. For this reason, it is not in
5050
/// general safe to store a StringRef.
51-
class LLVM_GSL_POINTER StringRef {
51+
class LLVM_GSL_POINTER StringRef : public std::string_view {
52+
using Base = std::string_view;
53+
5254
public:
5355
static constexpr size_t npos = ~size_t(0);
5456

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

6264
private:
63-
/// The start of the string, in an external buffer.
64-
const char *Data = nullptr;
65-
66-
/// The length of the string.
67-
size_t Length = 0;
68-
6965
// Workaround memcmp issue with null pointers (undefined behavior)
7066
// by providing a specialized version
7167
static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
@@ -86,27 +82,25 @@ namespace llvm {
8682

8783
/// Construct a string ref from a cstring.
8884
/*implicit*/ constexpr StringRef(const char *Str)
89-
: Data(Str), Length(Str ?
85+
: Base(Str, Str ?
9086
// GCC 7 doesn't have constexpr char_traits. Fall back to __builtin_strlen.
9187
#if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 8
92-
__builtin_strlen(Str)
88+
__builtin_strlen(Str)
9389
#else
94-
std::char_traits<char>::length(Str)
90+
std::char_traits<char>::length(Str)
9591
#endif
96-
: 0) {
92+
: 0) {
9793
}
9894

9995
/// Construct a string ref from a pointer and length.
10096
/*implicit*/ constexpr StringRef(const char *data, size_t length)
101-
: Data(data), Length(length) {}
97+
: Base(data, length) {}
10298

10399
/// Construct a string ref from an std::string.
104-
/*implicit*/ StringRef(const std::string &Str)
105-
: Data(Str.data()), Length(Str.length()) {}
100+
/*implicit*/ StringRef(const std::string &Str) : Base(Str) {}
106101

107102
/// Construct a string ref from an std::string_view.
108-
/*implicit*/ constexpr StringRef(std::string_view Str)
109-
: Data(Str.data()), Length(Str.size()) {}
103+
/*implicit*/ constexpr StringRef(std::string_view Str) : Base(Str) {}
110104

111105
/// @}
112106
/// @name Iterators
@@ -138,16 +132,9 @@ namespace llvm {
138132
/// @name String Operations
139133
/// @{
140134

141-
/// data - Get a pointer to the start of the string (which may not be null
142-
/// terminated).
143-
[[nodiscard]] constexpr const char *data() const { return Data; }
144-
145135
/// empty - Check if the string is empty.
146136
[[nodiscard]] constexpr bool empty() const { return size() == 0; }
147137

148-
/// size - Get the string size.
149-
[[nodiscard]] constexpr size_t size() const { return Length; }
150-
151138
/// front - Get the first character in the string.
152139
[[nodiscard]] char front() const {
153140
assert(!empty());
@@ -248,14 +235,6 @@ namespace llvm {
248235
std::enable_if_t<std::is_same<T, std::string>::value, StringRef> &
249236
operator=(T &&Str) = delete;
250237

251-
/// @}
252-
/// @name Type Conversions
253-
/// @{
254-
255-
constexpr operator std::string_view() const {
256-
return std::string_view(data(), size());
257-
}
258-
259238
/// @}
260239
/// @name String Predicates
261240
/// @{

0 commit comments

Comments
 (0)