Skip to content

Commit 5cfb07a

Browse files
Revert "[ADT] Use std::string_view inside StringRef (#113775)" (#114133)
This patch reverts commit 89b5d88. Some sanitizer failures have been reported, indicating that StringRef and std::string_view handle data == nulptr differently. Also, they support different values for the max size (size_t v.s. ptrdiff_t). Thanks goes to Jorge Gorbe Moya for reporting these.
1 parent a325c53 commit 5cfb07a

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

llvm/include/llvm/ADT/StringRef.h

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

6262
private:
63-
std::string_view View;
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;
6468

6569
// Workaround memcmp issue with null pointers (undefined behavior)
6670
// by providing a specialized version
@@ -82,26 +86,28 @@ namespace llvm {
8286

8387
/// Construct a string ref from a cstring.
8488
/*implicit*/ constexpr StringRef(const char *Str LLVM_LIFETIME_BOUND)
85-
: View(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 LLVM_LIFETIME_BOUND,
97101
size_t length)
98-
: View(data, length) {}
102+
: Data(data), Length(length) {}
99103

100104
/// Construct a string ref from an std::string.
101-
/*implicit*/ StringRef(const std::string &Str) : View(Str) {}
105+
/*implicit*/ StringRef(const std::string &Str)
106+
: Data(Str.data()), Length(Str.length()) {}
102107

103108
/// Construct a string ref from an std::string_view.
104-
/*implicit*/ constexpr StringRef(std::string_view Str) : View(Str) {}
109+
/*implicit*/ constexpr StringRef(std::string_view Str)
110+
: Data(Str.data()), Length(Str.size()) {}
105111

106112
/// @}
107113
/// @name Iterators
@@ -135,13 +141,13 @@ namespace llvm {
135141

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

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

143149
/// size - Get the string size.
144-
[[nodiscard]] constexpr size_t size() const { return View.size(); }
150+
[[nodiscard]] constexpr size_t size() const { return Length; }
145151

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

0 commit comments

Comments
 (0)