mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-02 05:58:18 +00:00
FlexBuffers: support "natural utf8" output in ToString (#8426)
This commit is contained in:
committed by
GitHub
parent
2f59a0319b
commit
807adb73b2
@@ -367,7 +367,8 @@ inline void IndentString(std::string &s, int indent,
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void AppendToString(std::string &s, T &&v, bool keys_quoted, bool indented,
|
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 += "[";
|
||||||
s += indented ? "\n" : " ";
|
s += indented ? "\n" : " ";
|
||||||
for (size_t i = 0; i < v.size(); i++) {
|
for (size_t i = 0; i < v.size(); i++) {
|
||||||
@@ -377,7 +378,7 @@ void AppendToString(std::string &s, T &&v, bool keys_quoted, bool indented,
|
|||||||
}
|
}
|
||||||
if (indented) IndentString(s, cur_indent, indent_string);
|
if (indented) IndentString(s, cur_indent, indent_string);
|
||||||
v[i].ToString(true, keys_quoted, s, indented, cur_indent,
|
v[i].ToString(true, keys_quoted, s, indented, cur_indent,
|
||||||
indent_string);
|
indent_string, natural_utf8);
|
||||||
}
|
}
|
||||||
if (indented) {
|
if (indented) {
|
||||||
s += "\n";
|
s += "\n";
|
||||||
@@ -567,23 +568,24 @@ class Reference {
|
|||||||
// string values at the top level receive "" quotes (inside other values
|
// string values at the top level receive "" quotes (inside other values
|
||||||
// they always do). keys_quoted determines if keys are quoted, at any level.
|
// 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 {
|
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.
|
// This version additionally allow you to specify if you want indentation.
|
||||||
void ToString(bool strings_quoted, bool keys_quoted, std::string &s,
|
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) {
|
if (type_ == FBT_STRING) {
|
||||||
String str(Indirect(), byte_width_);
|
String str(Indirect(), byte_width_);
|
||||||
if (strings_quoted) {
|
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 {
|
} else {
|
||||||
s.append(str.c_str(), str.length());
|
s.append(str.c_str(), str.length());
|
||||||
}
|
}
|
||||||
} else if (IsKey()) {
|
} else if (IsKey()) {
|
||||||
auto str = AsKey();
|
auto str = AsKey();
|
||||||
if (keys_quoted) {
|
if (keys_quoted) {
|
||||||
flatbuffers::EscapeString(str, strlen(str), &s, true, false);
|
flatbuffers::EscapeString(str, strlen(str), &s, true, natural_utf8);
|
||||||
} else {
|
} else {
|
||||||
s += str;
|
s += str;
|
||||||
}
|
}
|
||||||
@@ -623,7 +625,8 @@ class Reference {
|
|||||||
if (indented) IndentString(s, cur_indent + 1, indent_string);
|
if (indented) IndentString(s, cur_indent + 1, indent_string);
|
||||||
keys[i].ToString(true, kq, s);
|
keys[i].ToString(true, kq, s);
|
||||||
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) {
|
if (i < keys.size() - 1) {
|
||||||
s += ",";
|
s += ",";
|
||||||
if (!indented) s += " ";
|
if (!indented) s += " ";
|
||||||
@@ -635,13 +638,15 @@ class Reference {
|
|||||||
s += "}";
|
s += "}";
|
||||||
} else if (IsVector()) {
|
} else if (IsVector()) {
|
||||||
AppendToString<Vector>(s, AsVector(), keys_quoted, indented,
|
AppendToString<Vector>(s, AsVector(), keys_quoted, indented,
|
||||||
cur_indent + 1, indent_string);
|
cur_indent + 1, indent_string, natural_utf8);
|
||||||
} else if (IsTypedVector()) {
|
} else if (IsTypedVector()) {
|
||||||
AppendToString<TypedVector>(s, AsTypedVector(), keys_quoted, indented,
|
AppendToString<TypedVector>(s, AsTypedVector(), keys_quoted, indented,
|
||||||
cur_indent + 1, indent_string);
|
cur_indent + 1, indent_string,
|
||||||
|
natural_utf8);
|
||||||
} else if (IsFixedTypedVector()) {
|
} else if (IsFixedTypedVector()) {
|
||||||
AppendToString<FixedTypedVector>(s, AsFixedTypedVector(), keys_quoted,
|
AppendToString<FixedTypedVector>(s, AsFixedTypedVector(), keys_quoted,
|
||||||
indented, cur_indent + 1, indent_string);
|
indented, cur_indent + 1, indent_string,
|
||||||
|
natural_utf8);
|
||||||
} else if (IsBlob()) {
|
} else if (IsBlob()) {
|
||||||
auto blob = AsBlob();
|
auto blob = AsBlob();
|
||||||
flatbuffers::EscapeString(reinterpret_cast<const char *>(blob.data()),
|
flatbuffers::EscapeString(reinterpret_cast<const char *>(blob.data()),
|
||||||
|
|||||||
Reference in New Issue
Block a user