From 3b23ff18ea27c6cac33c8211d405d57d4595db1a Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Thu, 14 Mar 2019 12:00:44 -0700 Subject: [PATCH] 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: https://github.com/google/flatbuffers/commit/b1a925dfc2ac78508aee89029e950057405f0a0a#diff-c45c8fbffbc64f7ff4aa2978612b10d8 Change-Id: I4c95471b643b2b3fab95e06b1294e19d686b492c --- include/flatbuffers/minireflect.h | 44 ++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/include/flatbuffers/minireflect.h b/include/flatbuffers/minireflect.h index fc0cc43cf..f3fa5a488 100644 --- a/include/flatbuffers/minireflect.h +++ b/include/flatbuffers/minireflect.h @@ -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 += "["; - s += d; - indent_level++; - append_indent(); + if (vector_delimited) { + s += d; + indent_level++; + append_indent(); + } } void EndVector() { - s += d; - indent_level--; - append_indent(); + 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 += ","; - s += d; - append_indent(); + 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; }