Skip to content

Commit

Permalink
Replace std::string and const char* CreateSharedString with string_vi…
Browse files Browse the repository at this point in the history
…ew (google#6315)

It is useful to be able to call CreateSharedString with a string_view.
A string_view can be implicitly converted from a std::string or a const
char*.  This means if string_view is available, we can use it instead of
both other functions and get all 3.
  • Loading branch information
AustinSchuh authored Dec 7, 2020
1 parent bc7eb8a commit fd4c1b5
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/flatbuffers/flatbuffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,16 @@ class FlatBufferBuilder {
return off;
}

#ifdef FLATBUFFERS_HAS_STRING_VIEW
/// @brief Store a string in the buffer, which can contain any binary data.
/// If a string with this exact contents has already been serialized before,
/// instead simply returns the offset of the existing string.
/// @param[in] str A const std::string_view to store in the buffer.
/// @return Returns the offset in the buffer where the string starts
Offset<String> CreateSharedString(const flatbuffers::string_view str) {
return CreateSharedString(str.data(), str.size());
}
#else
/// @brief Store a string in the buffer, which null-terminated.
/// If a string with this exact contents has already been serialized before,
/// instead simply returns the offset of the existing string.
Expand All @@ -1628,6 +1638,7 @@ class FlatBufferBuilder {
Offset<String> CreateSharedString(const std::string &str) {
return CreateSharedString(str.c_str(), str.length());
}
#endif

/// @brief Store a string in the buffer, which can contain any binary data.
/// If a string with this exact contents has already been serialized before,
Expand Down

0 comments on commit fd4c1b5

Please sign in to comment.