@@ -48,9 +48,7 @@ namespace llvm {
48
48
// / situations where the character data resides in some other buffer, whose
49
49
// / lifetime extends past that of the StringRef. For this reason, it is not in
50
50
// / 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 {
54
52
public:
55
53
static constexpr size_t npos = ~size_t (0 );
56
54
@@ -62,6 +60,12 @@ namespace llvm {
62
60
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
63
61
64
62
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
+
65
69
// Workaround memcmp issue with null pointers (undefined behavior)
66
70
// by providing a specialized version
67
71
static int compareMemory (const char *Lhs, const char *Rhs, size_t Length) {
@@ -82,25 +86,27 @@ namespace llvm {
82
86
83
87
// / Construct a string ref from a cstring.
84
88
/* implicit*/ constexpr StringRef (const char *Str)
85
- : Base (Str, Str ?
89
+ : Data (Str), Length( Str ?
86
90
// GCC 7 doesn't have constexpr char_traits. Fall back to __builtin_strlen.
87
91
#if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 8
88
- __builtin_strlen (Str)
92
+ __builtin_strlen (Str)
89
93
#else
90
- std::char_traits<char >::length(Str)
94
+ std::char_traits<char >::length(Str)
91
95
#endif
92
- : 0 ) {
96
+ : 0 ) {
93
97
}
94
98
95
99
// / Construct a string ref from a pointer and length.
96
100
/* implicit*/ constexpr StringRef (const char *data, size_t length)
97
- : Base (data, length) {}
101
+ : Data (data), Length( length) {}
98
102
99
103
// / 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()) {}
101
106
102
107
// / 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()) {}
104
110
105
111
// / @}
106
112
// / @name Iterators
@@ -132,9 +138,16 @@ namespace llvm {
132
138
// / @name String Operations
133
139
// / @{
134
140
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
+
135
145
// / empty - Check if the string is empty.
136
146
[[nodiscard]] constexpr bool empty () const { return size () == 0 ; }
137
147
148
+ // / size - Get the string size.
149
+ [[nodiscard]] constexpr size_t size () const { return Length; }
150
+
138
151
// / front - Get the first character in the string.
139
152
[[nodiscard]] char front () const {
140
153
assert (!empty ());
@@ -235,6 +248,14 @@ namespace llvm {
235
248
std::enable_if_t <std::is_same<T, std::string>::value, StringRef> &
236
249
operator =(T &&Str) = delete ;
237
250
251
+ // / @}
252
+ // / @name Type Conversions
253
+ // / @{
254
+
255
+ constexpr operator std::string_view () const {
256
+ return std::string_view (data (), size ());
257
+ }
258
+
238
259
// / @}
239
260
// / @name String Predicates
240
261
// / @{
0 commit comments