From 0844f2a5aa4e79fedfcfef92edf94a44958b9da2 Mon Sep 17 00:00:00 2001 From: Yuriy Chernyshov Date: Tue, 8 Jun 2021 12:03:30 +0300 Subject: [PATCH 1/2] Make StringPiece constructible from std::string_view This improves interop with certain string-like data structures, allowing to save extra allocation. --- src/google/protobuf/stubs/stringpiece.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/google/protobuf/stubs/stringpiece.h b/src/google/protobuf/stubs/stringpiece.h index e6f5c714eaf1..f6ef582a0426 100644 --- a/src/google/protobuf/stubs/stringpiece.h +++ b/src/google/protobuf/stubs/stringpiece.h @@ -214,6 +214,13 @@ class PROTOBUF_EXPORT StringPiece { : ptr_(str.data()), length_(0) { length_ = CheckSize(str.size()); } + + StringPiece( // NOLINT(runtime/explicit) + std::string_view str) + : ptr_(str.data()), length_(0) { + length_ = CheckSize(str.size()); + } + StringPiece(const char* offset, size_type len) : ptr_(offset), length_(CheckSize(len)) {} From 8360d1dda36899db8ec66e6b7f24d4d38188758a Mon Sep 17 00:00:00 2001 From: Yuriy Chernyshov Date: Wed, 9 Jun 2021 11:36:15 +0300 Subject: [PATCH 2/2] Check feature-testing macro before use --- src/google/protobuf/stubs/stringpiece.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/google/protobuf/stubs/stringpiece.h b/src/google/protobuf/stubs/stringpiece.h index f6ef582a0426..c63e25b2549a 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 @@ -214,13 +218,14 @@ class PROTOBUF_EXPORT StringPiece { : ptr_(str.data()), length_(0) { 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)) {}