From 7dbfe2459be9e8aac2d1a678b304891319f3999c Mon Sep 17 00:00:00 2001 From: Evan Driscoll Date: Sat, 2 Jun 2018 20:15:07 -0500 Subject: [PATCH] Refactor: move string handling to dump_string --- .../detail/output/fancy_serializer.hpp | 120 +++++++++--------- single_include/nlohmann/json.hpp | 120 +++++++++--------- 2 files changed, 126 insertions(+), 114 deletions(-) diff --git a/include/nlohmann/detail/output/fancy_serializer.hpp b/include/nlohmann/detail/output/fancy_serializer.hpp index 361a76ae9..e26e119a5 100644 --- a/include/nlohmann/detail/output/fancy_serializer.hpp +++ b/include/nlohmann/detail/output/fancy_serializer.hpp @@ -220,63 +220,7 @@ class fancy_serializer case value_t::string: { - o->write_character('\"'); - if (style.strings_maximum_length == 0) - { - prim_serializer.dump_escaped(*o, *val.m_value.string, ensure_ascii); - } - else - { - std::stringstream ss; - nlohmann::detail::output_adapter o_string(ss); - nlohmann::detail::output_adapter_t oo_string = o_string; - prim_serializer.dump_escaped(*oo_string, *val.m_value.string, ensure_ascii); - - std::string str = ss.str(); - if (str.size() <= style.strings_maximum_length) - { - o->write_characters(str.c_str(), str.size()); - } - else - { - const unsigned start_len = [](unsigned int maxl) - { - if (maxl <= 3) - { - // There is only room for the ellipsis, - // no characters from the string - return 0u; - } - else if (maxl <= 5) - { - // With four allowed characters, we add in the - // first from the string. With five, we add in - // the *last* instead, so still just one at - // the start. - return 1u; - } - else - { - // We subtract three for the ellipsis - // and one for the last character. - return maxl - 4; - } - }(style.strings_maximum_length); - - const unsigned end_len = - style.strings_maximum_length >= 5 ? 1 : 0; - - const unsigned ellipsis_length = - style.strings_maximum_length >= 3 - ? 3 - : style.strings_maximum_length; - - o->write_characters(str.c_str(), start_len); - o->write_characters("...", ellipsis_length); - o->write_characters(str.c_str() + str.size() - end_len, end_len); - } - } - o->write_character('\"'); + dump_string(*val.m_value.string, ensure_ascii); return; } @@ -325,6 +269,68 @@ class fancy_serializer } } + private: + void dump_string(string_t const& str, bool ensure_ascii) + { + o->write_character('\"'); + if (style.strings_maximum_length == 0) + { + prim_serializer.dump_escaped(*o, str, ensure_ascii); + } + else + { + std::stringstream ss; + nlohmann::detail::output_adapter o_string(ss); + nlohmann::detail::output_adapter_t oo_string = o_string; + prim_serializer.dump_escaped(*oo_string, str, ensure_ascii); + + std::string full_str = ss.str(); + if (full_str.size() <= style.strings_maximum_length) + { + o->write_characters(full_str.c_str(), full_str.size()); + } + else + { + const unsigned start_len = [](unsigned int maxl) + { + if (maxl <= 3) + { + // There is only room for the ellipsis, + // no characters from the string + return 0u; + } + else if (maxl <= 5) + { + // With four allowed characters, we add in the + // first from the string. With five, we add in + // the *last* instead, so still just one at + // the start. + return 1u; + } + else + { + // We subtract three for the ellipsis + // and one for the last character. + return maxl - 4; + } + }(style.strings_maximum_length); + + const unsigned end_len = + style.strings_maximum_length >= 5 ? 1 : 0; + + const unsigned ellipsis_length = + style.strings_maximum_length >= 3 + ? 3 + : style.strings_maximum_length; + + o->write_characters(full_str.c_str(), start_len); + o->write_characters("...", ellipsis_length); + o->write_characters(full_str.c_str() + str.size() - end_len, end_len); + } + } + o->write_character('\"'); + } + private: /// the output of the fancy_serializer output_adapter_t o = nullptr; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 979e55671..e693cfc12 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10290,63 +10290,7 @@ class fancy_serializer case value_t::string: { - o->write_character('\"'); - if (style.strings_maximum_length == 0) - { - prim_serializer.dump_escaped(*o, *val.m_value.string, ensure_ascii); - } - else - { - std::stringstream ss; - nlohmann::detail::output_adapter o_string(ss); - nlohmann::detail::output_adapter_t oo_string = o_string; - prim_serializer.dump_escaped(*oo_string, *val.m_value.string, ensure_ascii); - - std::string str = ss.str(); - if (str.size() <= style.strings_maximum_length) - { - o->write_characters(str.c_str(), str.size()); - } - else - { - const unsigned start_len = [](unsigned int maxl) - { - if (maxl <= 3) - { - // There is only room for the ellipsis, - // no characters from the string - return 0u; - } - else if (maxl <= 5) - { - // With four allowed characters, we add in the - // first from the string. With five, we add in - // the *last* instead, so still just one at - // the start. - return 1u; - } - else - { - // We subtract three for the ellipsis - // and one for the last character. - return maxl - 4; - } - }(style.strings_maximum_length); - - const unsigned end_len = - style.strings_maximum_length >= 5 ? 1 : 0; - - const unsigned ellipsis_length = - style.strings_maximum_length >= 3 - ? 3 - : style.strings_maximum_length; - - o->write_characters(str.c_str(), start_len); - o->write_characters("...", ellipsis_length); - o->write_characters(str.c_str() + str.size() - end_len, end_len); - } - } - o->write_character('\"'); + dump_string(*val.m_value.string, ensure_ascii); return; } @@ -10395,6 +10339,68 @@ class fancy_serializer } } + private: + void dump_string(string_t const& str, bool ensure_ascii) + { + o->write_character('\"'); + if (style.strings_maximum_length == 0) + { + prim_serializer.dump_escaped(*o, str, ensure_ascii); + } + else + { + std::stringstream ss; + nlohmann::detail::output_adapter o_string(ss); + nlohmann::detail::output_adapter_t oo_string = o_string; + prim_serializer.dump_escaped(*oo_string, str, ensure_ascii); + + std::string full_str = ss.str(); + if (full_str.size() <= style.strings_maximum_length) + { + o->write_characters(full_str.c_str(), full_str.size()); + } + else + { + const unsigned start_len = [](unsigned int maxl) + { + if (maxl <= 3) + { + // There is only room for the ellipsis, + // no characters from the string + return 0u; + } + else if (maxl <= 5) + { + // With four allowed characters, we add in the + // first from the string. With five, we add in + // the *last* instead, so still just one at + // the start. + return 1u; + } + else + { + // We subtract three for the ellipsis + // and one for the last character. + return maxl - 4; + } + }(style.strings_maximum_length); + + const unsigned end_len = + style.strings_maximum_length >= 5 ? 1 : 0; + + const unsigned ellipsis_length = + style.strings_maximum_length >= 3 + ? 3 + : style.strings_maximum_length; + + o->write_characters(full_str.c_str(), start_len); + o->write_characters("...", ellipsis_length); + o->write_characters(full_str.c_str() + str.size() - end_len, end_len); + } + } + o->write_character('\"'); + } + private: /// the output of the fancy_serializer output_adapter_t o = nullptr;