-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[ADT] Use std::string_view inside StringRef #113775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ADT] Use std::string_view inside StringRef #113775
Conversation
This patch makes minimum changes to replace Data and Length with an instance of std::string_view. Previously, I opted for public inheritance (llvm#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.
|
@llvm/pr-subscribers-llvm-adt Author: Kazu Hirata (kazutakahirata) ChangesThis patch makes minimum changes to replace Data and Length with an Previously, I opted for public inheritance (#113752), but I The composition approach in this patch gives us greater control at the Full diff: https://github.com/llvm/llvm-project/pull/113775.diff 1 Files Affected:
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index d5f30b88c4c6a2..f879bbf7164fd6 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -60,11 +60,7 @@ namespace llvm {
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
private:
- /// The start of the string, in an external buffer.
- const char *Data = nullptr;
-
- /// The length of the string.
- size_t Length = 0;
+ std::string_view View;
// Workaround memcmp issue with null pointers (undefined behavior)
// by providing a specialized version
@@ -86,27 +82,25 @@ namespace llvm {
/// Construct a string ref from a cstring.
/*implicit*/ constexpr StringRef(const char *Str)
- : Data(Str), Length(Str ?
+ : View(Str, Str ?
// GCC 7 doesn't have constexpr char_traits. Fall back to __builtin_strlen.
#if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 8
- __builtin_strlen(Str)
+ __builtin_strlen(Str)
#else
- std::char_traits<char>::length(Str)
+ std::char_traits<char>::length(Str)
#endif
- : 0) {
+ : 0) {
}
/// Construct a string ref from a pointer and length.
/*implicit*/ constexpr StringRef(const char *data, size_t length)
- : Data(data), Length(length) {}
+ : View(data, length) {}
/// Construct a string ref from an std::string.
- /*implicit*/ StringRef(const std::string &Str)
- : Data(Str.data()), Length(Str.length()) {}
+ /*implicit*/ StringRef(const std::string &Str) : View(Str) {}
/// Construct a string ref from an std::string_view.
- /*implicit*/ constexpr StringRef(std::string_view Str)
- : Data(Str.data()), Length(Str.size()) {}
+ /*implicit*/ constexpr StringRef(std::string_view Str) : View(Str) {}
/// @}
/// @name Iterators
@@ -140,13 +134,13 @@ namespace llvm {
/// data - Get a pointer to the start of the string (which may not be null
/// terminated).
- [[nodiscard]] constexpr const char *data() const { return Data; }
+ [[nodiscard]] constexpr const char *data() const { return View.data(); }
/// empty - Check if the string is empty.
[[nodiscard]] constexpr bool empty() const { return size() == 0; }
/// size - Get the string size.
- [[nodiscard]] constexpr size_t size() const { return Length; }
+ [[nodiscard]] constexpr size_t size() const { return View.size(); }
/// front - Get the first character in the string.
[[nodiscard]] char front() const {
|
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/139/builds/5462 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/52/builds/3251 Here is the relevant piece of the build log for the reference |
|
With inheritance I can sort of see the path forward to migration a bit (pending RFC discussion that that's the direction we're going in) - but with composition, I find it a bit harder to see the path forward. Could you clarify what this migration looks like using composition instead of (or as a precursor to) inheritance? |
This patch reverts commit 89b5d88. Some sanitizer failures have been reported, indivating 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.
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.
This patch makes minimum changes to replace Data and Length with an instance of std::string_view. Previously, I opted for public inheritance (llvm#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.
…lvm#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.
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.