Skip to content

Commit

Permalink
FlexBuffers: support "natural utf8" output in ToString (#8426)
Browse files Browse the repository at this point in the history
  • Loading branch information
aardappel authored Oct 12, 2024
1 parent 2f59a03 commit 807adb7
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions include/flatbuffers/flexbuffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ inline void IndentString(std::string &s, int indent,

template<typename T>
void AppendToString(std::string &s, T &&v, bool keys_quoted, bool indented,
int cur_indent, const char *indent_string) {
int cur_indent, const char *indent_string,
bool natural_utf8) {
s += "[";
s += indented ? "\n" : " ";
for (size_t i = 0; i < v.size(); i++) {
Expand All @@ -377,7 +378,7 @@ void AppendToString(std::string &s, T &&v, bool keys_quoted, bool indented,
}
if (indented) IndentString(s, cur_indent, indent_string);
v[i].ToString(true, keys_quoted, s, indented, cur_indent,
indent_string);
indent_string, natural_utf8);
}
if (indented) {
s += "\n";
Expand Down Expand Up @@ -567,23 +568,24 @@ class Reference {
// string values at the top level receive "" quotes (inside other values
// they always do). keys_quoted determines if keys are quoted, at any level.
void ToString(bool strings_quoted, bool keys_quoted, std::string &s) const {
ToString(strings_quoted, keys_quoted, s, false, 0, "");
ToString(strings_quoted, keys_quoted, s, false, 0, "", false);
}

// This version additionally allow you to specify if you want indentation.
void ToString(bool strings_quoted, bool keys_quoted, std::string &s,
bool indented, int cur_indent, const char *indent_string) const {
bool indented, int cur_indent, const char *indent_string,
bool natural_utf8 = false) const {
if (type_ == FBT_STRING) {
String str(Indirect(), byte_width_);
if (strings_quoted) {
flatbuffers::EscapeString(str.c_str(), str.length(), &s, true, false);
flatbuffers::EscapeString(str.c_str(), str.length(), &s, true, natural_utf8);
} else {
s.append(str.c_str(), str.length());
}
} else if (IsKey()) {
auto str = AsKey();
if (keys_quoted) {
flatbuffers::EscapeString(str, strlen(str), &s, true, false);
flatbuffers::EscapeString(str, strlen(str), &s, true, natural_utf8);
} else {
s += str;
}
Expand Down Expand Up @@ -623,7 +625,8 @@ class Reference {
if (indented) IndentString(s, cur_indent + 1, indent_string);
keys[i].ToString(true, kq, s);
s += ": ";
vals[i].ToString(true, keys_quoted, s, indented, cur_indent + 1, indent_string);
vals[i].ToString(true, keys_quoted, s, indented, cur_indent + 1, indent_string,
natural_utf8);
if (i < keys.size() - 1) {
s += ",";
if (!indented) s += " ";
Expand All @@ -635,13 +638,15 @@ class Reference {
s += "}";
} else if (IsVector()) {
AppendToString<Vector>(s, AsVector(), keys_quoted, indented,
cur_indent + 1, indent_string);
cur_indent + 1, indent_string, natural_utf8);
} else if (IsTypedVector()) {
AppendToString<TypedVector>(s, AsTypedVector(), keys_quoted, indented,
cur_indent + 1, indent_string);
cur_indent + 1, indent_string,
natural_utf8);
} else if (IsFixedTypedVector()) {
AppendToString<FixedTypedVector>(s, AsFixedTypedVector(), keys_quoted,
indented, cur_indent + 1, indent_string);
indented, cur_indent + 1, indent_string,
natural_utf8);
} else if (IsBlob()) {
auto blob = AsBlob();
flatbuffers::EscapeString(reinterpret_cast<const char *>(blob.data()),
Expand Down

0 comments on commit 807adb7

Please sign in to comment.