Skip to content

Commit c45f96c

Browse files
aardappelJochen Parmentier
authored andcommitted
FlexBuffers: support "natural utf8" output in ToString (google#8426)
1 parent 1f65e93 commit c45f96c

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

include/flatbuffers/flexbuffers.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ inline void IndentString(std::string &s, int indent,
367367

368368
template<typename T>
369369
void AppendToString(std::string &s, T &&v, bool keys_quoted, bool indented,
370-
int cur_indent, const char *indent_string) {
370+
int cur_indent, const char *indent_string,
371+
bool natural_utf8) {
371372
s += "[";
372373
s += indented ? "\n" : " ";
373374
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,
377378
}
378379
if (indented) IndentString(s, cur_indent, indent_string);
379380
v[i].ToString(true, keys_quoted, s, indented, cur_indent,
380-
indent_string);
381+
indent_string, natural_utf8);
381382
}
382383
if (indented) {
383384
s += "\n";
@@ -567,23 +568,24 @@ class Reference {
567568
// string values at the top level receive "" quotes (inside other values
568569
// they always do). keys_quoted determines if keys are quoted, at any level.
569570
void ToString(bool strings_quoted, bool keys_quoted, std::string &s) const {
570-
ToString(strings_quoted, keys_quoted, s, false, 0, "");
571+
ToString(strings_quoted, keys_quoted, s, false, 0, "", false);
571572
}
572573

573574
// This version additionally allow you to specify if you want indentation.
574575
void ToString(bool strings_quoted, bool keys_quoted, std::string &s,
575-
bool indented, int cur_indent, const char *indent_string) const {
576+
bool indented, int cur_indent, const char *indent_string,
577+
bool natural_utf8 = false) const {
576578
if (type_ == FBT_STRING) {
577579
String str(Indirect(), byte_width_);
578580
if (strings_quoted) {
579-
flatbuffers::EscapeString(str.c_str(), str.length(), &s, true, false);
581+
flatbuffers::EscapeString(str.c_str(), str.length(), &s, true, natural_utf8);
580582
} else {
581583
s.append(str.c_str(), str.length());
582584
}
583585
} else if (IsKey()) {
584586
auto str = AsKey();
585587
if (keys_quoted) {
586-
flatbuffers::EscapeString(str, strlen(str), &s, true, false);
588+
flatbuffers::EscapeString(str, strlen(str), &s, true, natural_utf8);
587589
} else {
588590
s += str;
589591
}
@@ -623,7 +625,8 @@ class Reference {
623625
if (indented) IndentString(s, cur_indent + 1, indent_string);
624626
keys[i].ToString(true, kq, s);
625627
s += ": ";
626-
vals[i].ToString(true, keys_quoted, s, indented, cur_indent + 1, indent_string);
628+
vals[i].ToString(true, keys_quoted, s, indented, cur_indent + 1, indent_string,
629+
natural_utf8);
627630
if (i < keys.size() - 1) {
628631
s += ",";
629632
if (!indented) s += " ";
@@ -635,13 +638,15 @@ class Reference {
635638
s += "}";
636639
} else if (IsVector()) {
637640
AppendToString<Vector>(s, AsVector(), keys_quoted, indented,
638-
cur_indent + 1, indent_string);
641+
cur_indent + 1, indent_string, natural_utf8);
639642
} else if (IsTypedVector()) {
640643
AppendToString<TypedVector>(s, AsTypedVector(), keys_quoted, indented,
641-
cur_indent + 1, indent_string);
644+
cur_indent + 1, indent_string,
645+
natural_utf8);
642646
} else if (IsFixedTypedVector()) {
643647
AppendToString<FixedTypedVector>(s, AsFixedTypedVector(), keys_quoted,
644-
indented, cur_indent + 1, indent_string);
648+
indented, cur_indent + 1, indent_string,
649+
natural_utf8);
645650
} else if (IsBlob()) {
646651
auto blob = AsBlob();
647652
flatbuffers::EscapeString(reinterpret_cast<const char *>(blob.data()),

0 commit comments

Comments
 (0)