Skip to content

Commit 89b5d88

Browse files
[ADT] Use std::string_view inside StringRef (#113775)
This patch makes minimum changes to replace Data and Length with an instance of std::string_view. Previously, I opted for public inheritance (#113752), but I encountered a lot of errors from gcc stemming from ambiguity between std::string_view and StringRef. The composition approach in this patch gives us greater control at the expense of forwarder functions.
1 parent 597ccb8 commit 89b5d88

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

llvm/include/llvm/ADT/StringRef.h

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ namespace llvm {
6060
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
6161

6262
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;
63+
std::string_view View;
6864

6965
// Workaround memcmp issue with null pointers (undefined behavior)
7066
// by providing a specialized version
@@ -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+
: View(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+
: View(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) : View(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) : View(Str) {}
110104

111105
/// @}
112106
/// @name Iterators
@@ -140,13 +134,13 @@ namespace llvm {
140134

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

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

148142
/// size - Get the string size.
149-
[[nodiscard]] constexpr size_t size() const { return Length; }
143+
[[nodiscard]] constexpr size_t size() const { return View.size(); }
150144

151145
/// front - Get the first character in the string.
152146
[[nodiscard]] char front() const {

0 commit comments

Comments
 (0)