Refactor: dump_array doesn't split cases for indent

This commit is contained in:
Evan Driscoll 2018-06-02 21:01:22 -05:00
parent cc4206a718
commit 9cdf54b886
2 changed files with 42 additions and 82 deletions

View File

@ -229,10 +229,6 @@ class fancy_serializer
return; return;
} }
if (style.indent_step > 0)
{
o->write_characters("[\n", 2);
// variable to hold indentation for recursive calls // variable to hold indentation for recursive calls
const auto old_indent = depth * style.indent_step; const auto old_indent = depth * style.indent_step;
const auto new_indent = (depth + 1) * style.indent_step; const auto new_indent = (depth + 1) * style.indent_step;
@ -240,6 +236,9 @@ class fancy_serializer
{ {
indent_string.resize(indent_string.size() * 2, ' '); indent_string.resize(indent_string.size() * 2, ' ');
} }
const int newline_len = (style.indent_step > 0);
o->write_characters("[\n", 1 + newline_len);
// first n-1 elements // first n-1 elements
for (auto i = val.m_value.array->cbegin(); for (auto i = val.m_value.array->cbegin();
@ -247,7 +246,7 @@ class fancy_serializer
{ {
o->write_characters(indent_string.c_str(), new_indent); o->write_characters(indent_string.c_str(), new_indent);
dump(*i, ensure_ascii, depth + 1); dump(*i, ensure_ascii, depth + 1);
o->write_characters(",\n", 2); o->write_characters(",\n", 1 + newline_len);
} }
// last element // last element
@ -255,29 +254,10 @@ class fancy_serializer
o->write_characters(indent_string.c_str(), new_indent); o->write_characters(indent_string.c_str(), new_indent);
dump(val.m_value.array->back(), ensure_ascii, depth + 1); dump(val.m_value.array->back(), ensure_ascii, depth + 1);
o->write_character('\n'); o->write_characters("\n", newline_len);
o->write_characters(indent_string.c_str(), old_indent); o->write_characters(indent_string.c_str(), old_indent);
o->write_character(']'); o->write_character(']');
} }
else
{
o->write_character('[');
// first n-1 elements
for (auto i = val.m_value.array->cbegin();
i != val.m_value.array->cend() - 1; ++i)
{
dump(*i, ensure_ascii, depth + 1);
o->write_character(',');
}
// last element
assert(not val.m_value.array->empty());
dump(val.m_value.array->back(), ensure_ascii, depth + 1);
o->write_character(']');
}
}
void dump_string(const string_t& str, bool ensure_ascii) void dump_string(const string_t& str, bool ensure_ascii)
{ {

View File

@ -10299,10 +10299,6 @@ class fancy_serializer
return; return;
} }
if (style.indent_step > 0)
{
o->write_characters("[\n", 2);
// variable to hold indentation for recursive calls // variable to hold indentation for recursive calls
const auto old_indent = depth * style.indent_step; const auto old_indent = depth * style.indent_step;
const auto new_indent = (depth + 1) * style.indent_step; const auto new_indent = (depth + 1) * style.indent_step;
@ -10310,6 +10306,9 @@ class fancy_serializer
{ {
indent_string.resize(indent_string.size() * 2, ' '); indent_string.resize(indent_string.size() * 2, ' ');
} }
const int newline_len = (style.indent_step > 0);
o->write_characters("[\n", 1 + newline_len);
// first n-1 elements // first n-1 elements
for (auto i = val.m_value.array->cbegin(); for (auto i = val.m_value.array->cbegin();
@ -10317,7 +10316,7 @@ class fancy_serializer
{ {
o->write_characters(indent_string.c_str(), new_indent); o->write_characters(indent_string.c_str(), new_indent);
dump(*i, ensure_ascii, depth + 1); dump(*i, ensure_ascii, depth + 1);
o->write_characters(",\n", 2); o->write_characters(",\n", 1 + newline_len);
} }
// last element // last element
@ -10325,29 +10324,10 @@ class fancy_serializer
o->write_characters(indent_string.c_str(), new_indent); o->write_characters(indent_string.c_str(), new_indent);
dump(val.m_value.array->back(), ensure_ascii, depth + 1); dump(val.m_value.array->back(), ensure_ascii, depth + 1);
o->write_character('\n'); o->write_characters("\n", newline_len);
o->write_characters(indent_string.c_str(), old_indent); o->write_characters(indent_string.c_str(), old_indent);
o->write_character(']'); o->write_character(']');
} }
else
{
o->write_character('[');
// first n-1 elements
for (auto i = val.m_value.array->cbegin();
i != val.m_value.array->cend() - 1; ++i)
{
dump(*i, ensure_ascii, depth + 1);
o->write_character(',');
}
// last element
assert(not val.m_value.array->empty());
dump(val.m_value.array->back(), ensure_ascii, depth + 1);
o->write_character(']');
}
}
void dump_string(const string_t& str, bool ensure_ascii) void dump_string(const string_t& str, bool ensure_ascii)
{ {