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