Make StringPiece constructible from std::string_view (#8707)
* Make StringPiece constructible from std::string_view
This improves interop with certain string-like data structures, allowing to save extra allocation.
* Check feature-testing macro before use
diff --git a/src/google/protobuf/stubs/stringpiece.h b/src/google/protobuf/stubs/stringpiece.h
index e6f5c71..c63e25b 100644
--- a/src/google/protobuf/stubs/stringpiece.h
+++ b/src/google/protobuf/stubs/stringpiece.h
@@ -148,6 +148,10 @@
#include <limits>
#include <string>
+#if defined(__cpp_lib_string_view)
+#include <string_view>
+#endif
+
#include <google/protobuf/stubs/hash.h>
#include <google/protobuf/port_def.inc>
@@ -215,6 +219,14 @@
length_ = CheckSize(str.size());
}
+#if defined(__cpp_lib_string_view)
+ StringPiece( // NOLINT(runtime/explicit)
+ std::string_view str)
+ : ptr_(str.data()), length_(0) {
+ length_ = CheckSize(str.size());
+ }
+#endif
+
StringPiece(const char* offset, size_type len)
: ptr_(offset), length_(CheckSize(len)) {}