@@ -48,9 +48,7 @@ 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 : public std::string_view {
52- using Base = std::string_view;
53-
51+ class LLVM_GSL_POINTER StringRef {
5452 public:
5553 static constexpr size_t npos = ~size_t (0 );
5654
@@ -62,6 +60,12 @@ namespace llvm {
6260 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
6361
6462 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+
6569 // Workaround memcmp issue with null pointers (undefined behavior)
6670 // by providing a specialized version
6771 static int compareMemory (const char *Lhs, const char *Rhs, size_t Length) {
@@ -82,25 +86,27 @@ namespace llvm {
8286
8387 // / Construct a string ref from a cstring.
8488 /* implicit*/ constexpr StringRef (const char *Str)
85- : Base (Str, Str ?
89+ : Data (Str), Length( Str ?
8690 // GCC 7 doesn't have constexpr char_traits. Fall back to __builtin_strlen.
8791#if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 8
88- __builtin_strlen (Str)
92+ __builtin_strlen (Str)
8993#else
90- std::char_traits<char >::length(Str)
94+ std::char_traits<char >::length(Str)
9195#endif
92- : 0 ) {
96+ : 0 ) {
9397 }
9498
9599 // / Construct a string ref from a pointer and length.
96100 /* implicit*/ constexpr StringRef (const char *data, size_t length)
97- : Base (data, length) {}
101+ : Data (data), Length( length) {}
98102
99103 // / Construct a string ref from an std::string.
100- /* implicit*/ StringRef(const std::string &Str) : Base(Str) {}
104+ /* implicit*/ StringRef(const std::string &Str)
105+ : Data(Str.data()), Length(Str.length()) {}
101106
102107 // / Construct a string ref from an std::string_view.
103- /* implicit*/ constexpr StringRef (std::string_view Str) : Base(Str) {}
108+ /* implicit*/ constexpr StringRef (std::string_view Str)
109+ : Data(Str.data()), Length(Str.size()) {}
104110
105111 // / @}
106112 // / @name Iterators
@@ -132,9 +138,16 @@ namespace llvm {
132138 // / @name String Operations
133139 // / @{
134140
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+
135145 // / empty - Check if the string is empty.
136146 [[nodiscard]] constexpr bool empty () const { return size () == 0 ; }
137147
148+ // / size - Get the string size.
149+ [[nodiscard]] constexpr size_t size () const { return Length; }
150+
138151 // / front - Get the first character in the string.
139152 [[nodiscard]] char front () const {
140153 assert (!empty ());
@@ -235,6 +248,14 @@ namespace llvm {
235248 std::enable_if_t <std::is_same<T, std::string>::value, StringRef> &
236249 operator =(T &&Str) = delete ;
237250
251+ // / @}
252+ // / @name Type Conversions
253+ // / @{
254+
255+ constexpr operator std::string_view () const {
256+ return std::string_view (data (), size ());
257+ }
258+
238259 // / @}
239260 // / @name String Predicates
240261 // / @{
0 commit comments