Skip to content

Commit

Permalink
Make StringPiece constructible from std::string_view (#8707)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
georgthegreat committed Jun 9, 2021
1 parent ab229be commit 2bff7d1
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/google/protobuf/stubs/stringpiece.h
Expand Up @@ -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>
Expand Down Expand Up @@ -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)) {}

Expand Down

0 comments on commit 2bff7d1

Please sign in to comment.