From 2bff7d18f21d25a5c85ed9de8e0182eb0120c6ce Mon Sep 17 00:00:00 2001 From: Yuriy Chernyshov Date: Wed, 9 Jun 2021 23:08:38 +0300 Subject: [PATCH] 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 --- src/google/protobuf/stubs/stringpiece.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/google/protobuf/stubs/stringpiece.h b/src/google/protobuf/stubs/stringpiece.h index e6f5c714eaf..c63e25b2549 100644 --- a/src/google/protobuf/stubs/stringpiece.h +++ b/src/google/protobuf/stubs/stringpiece.h @@ -148,6 +148,10 @@ #include #include +#if defined(__cpp_lib_string_view) +#include +#endif + #include #include @@ -215,6 +219,14 @@ class PROTOBUF_EXPORT StringPiece { 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)) {}