mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-18 21:18:56 +00:00
Add string view [C++] (#4730)
* Add view() method on flatbuffers::String, to return a string_view type if support for std::string_view (or alternately std::experimental::string_view) is found * Move detection/definition of FLATBUFFERS_STRING_VIEW to base.h, use the macro (if it is defined) as the argument type for an overload of CreateString * Rename String::view() to String::string_view() and use the existing c_str() method for the data pointer * Add and explain minimum C++ standard version checks for FLATBUFFERS_STRING_VIEW implementations * Updated preprocessor indenting for FLATBUFFERS_STRING_VIEW * Convert FLATBUFFERS_STRING_VIEW macro to typedef in flatbuffers:: namespace, and boolean feature toggle macro FLATBUFFERS_HAS_STRING_VIEW * Prepend flatbuffers:: namespace to disambiguate flatbuffers::string_view typedef from String::string_view() * clang-format as-she-is-spoke for FLATBUFFERS_HAS_STRING_VIEW
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
b10123ff63
commit
a0a33d94a7
@@ -344,6 +344,14 @@ struct String : public Vector<char> {
|
||||
const char *c_str() const { return reinterpret_cast<const char *>(Data()); }
|
||||
std::string str() const { return std::string(c_str(), Length()); }
|
||||
|
||||
// clang-format off
|
||||
#ifdef FLATBUFFERS_HAS_STRING_VIEW
|
||||
flatbuffers::string_view string_view() const {
|
||||
return flatbuffers::string_view(c_str(), Length());
|
||||
}
|
||||
#endif // FLATBUFFERS_HAS_STRING_VIEW
|
||||
// clang-format on
|
||||
|
||||
bool operator<(const String &o) const {
|
||||
return strcmp(c_str(), o.c_str()) < 0;
|
||||
}
|
||||
@@ -1077,6 +1085,17 @@ class FlatBufferBuilder {
|
||||
return CreateString(str.c_str(), str.length());
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
#ifdef FLATBUFFERS_HAS_STRING_VIEW
|
||||
/// @brief Store a string in the buffer, which can contain any binary data.
|
||||
/// @param[in] str A const string_view to copy in to the buffer.
|
||||
/// @return Returns the offset in the buffer where the string starts.
|
||||
Offset<String> CreateString(flatbuffers::string_view str) {
|
||||
return CreateString(str.data(), str.size());
|
||||
}
|
||||
#endif // FLATBUFFERS_HAS_STRING_VIEW
|
||||
// clang-format on
|
||||
|
||||
/// @brief Store a string in the buffer, which can contain any binary data.
|
||||
/// @param[in] str A const pointer to a `String` struct to add to the buffer.
|
||||
/// @return Returns the offset in the buffer where the string starts
|
||||
|
||||
Reference in New Issue
Block a user