Made ToStringVisitor backwards compatible with older behavior.

This to support code that relied on tables being multiline,
but not vectors.

This behavior was changed in:
b1a925dfc2 (diff-c45c8fbffbc64f7ff4aa2978612b10d8)

Change-Id: I4c95471b643b2b3fab95e06b1294e19d686b492c
This commit is contained in:
Wouter van Oortmerssen 2019-03-14 12:00:44 -07:00
parent 4f066c39ce
commit 3b23ff18ea
1 changed files with 31 additions and 13 deletions

View File

@ -287,10 +287,20 @@ struct ToStringVisitor : public IterationVisitor {
bool q;
std::string in;
size_t indent_level;
ToStringVisitor(std::string delimiter, bool quotes, std::string indent)
: d(delimiter), q(quotes), in(indent), indent_level(0) {}
bool vector_delimited;
ToStringVisitor(std::string delimiter, bool quotes, std::string indent,
bool vector_delimited = true)
: d(delimiter),
q(quotes),
in(indent),
indent_level(0),
vector_delimited(vector_delimited) {}
ToStringVisitor(std::string delimiter)
: d(delimiter), q(false), in(""), indent_level(0) {}
: d(delimiter),
q(false),
in(""),
indent_level(0),
vector_delimited(true) {}
void append_indent() {
for (size_t i = 0; i < indent_level; i++) { s += in; }
@ -350,30 +360,38 @@ struct ToStringVisitor : public IterationVisitor {
void Unknown(const uint8_t *) { s += "(?)"; }
void StartVector() {
s += "[";
if (vector_delimited) {
s += d;
indent_level++;
append_indent();
}
}
void EndVector() {
if (vector_delimited) {
s += d;
indent_level--;
append_indent();
}
s += "]";
}
void Element(size_t i, ElementaryType /*type*/,
const TypeTable * /*type_table*/, const uint8_t * /*val*/) {
if (i) {
s += ",";
if (vector_delimited) {
s += d;
append_indent();
}
}
}
};
inline std::string FlatBufferToString(const uint8_t *buffer,
const TypeTable *type_table,
bool multi_line = false) {
ToStringVisitor tostring_visitor(multi_line ? "\n" : " ");
bool multi_line = false,
bool vector_delimited = true) {
ToStringVisitor tostring_visitor(multi_line ? "\n" : " ", false, "",
vector_delimited);
IterateFlatBuffer(buffer, type_table, &tostring_visitor);
return tostring_visitor.s;
}