From 949867eca97f14a0eaa48d7af9cdc3b3e7c30382 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 2 Oct 2018 08:57:50 +0200 Subject: [PATCH] factor out some parts of print function for entry --- include/libtorrent/entry.hpp | 2 - src/entry.cpp | 118 +++++++++++++++++++---------------- 2 files changed, 64 insertions(+), 56 deletions(-) diff --git a/include/libtorrent/entry.hpp b/include/libtorrent/entry.hpp index 970225dde..6be8a501f 100644 --- a/include/libtorrent/entry.hpp +++ b/include/libtorrent/entry.hpp @@ -311,8 +311,6 @@ namespace aux { private: - void to_string_impl(std::string& out, int indent, bool single_line) const; - aux::aligned_union<1 #if TORRENT_COMPLETE_TYPES_REQUIRED // for implementations that require complete types, use char and hope diff --git a/src/entry.cpp b/src/entry.cpp index 12028009c..6ce039bb8 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -662,13 +662,6 @@ namespace { } } - std::string entry::to_string(bool const single_line) const - { - std::string ret; - to_string_impl(ret, 0, single_line); - return ret; - } - namespace { bool is_binary(std::string const& str) { @@ -689,62 +682,79 @@ namespace { { out.resize(out.size() + size_t(indent), ' '); } -} - void entry::to_string_impl(std::string& out, int const indent - , bool const single_line) const + void print_list(std::string&, entry const&, int, bool); + void print_dict(std::string&, entry const&, int, bool); + + void to_string_impl(std::string& out, entry const& e, int const indent + , bool const single_line) { TORRENT_ASSERT(indent >= 0); - switch (m_type) + switch (e.type()) { - case int_t: - out += libtorrent::to_string(integer()).data(); + case entry::int_t: + out += libtorrent::to_string(e.integer()).data(); break; - case string_t: - { - out += "'"; - out += print_string(string()); - out += "'"; - } break; - case list_t: - { - out += single_line ? "[ " : "[\n"; - bool first = true; - for (auto const& item : list()) - { - if (!first) out += single_line ? ", " : ",\n"; - first = false; - if (!single_line) add_indent(out, indent+1); - item.to_string_impl(out, indent+1, single_line); - } - out += " ]"; - } break; - case dictionary_t: - { - out += single_line ? "{ " : "{\n"; - bool first = true; - for (auto const& item : dict()) - { - if (!first) out += single_line ? ", " : ",\n"; - first = false; - if (!single_line) add_indent(out, indent+1); - out += "'"; - out += print_string(item.first); - out += "': "; - - item.second.to_string_impl(out, indent+2, single_line); - } - out += " }"; - } break; - case preformatted_t: + case entry::string_t: + out += "'"; + out += print_string(e.string()); + out += "'"; + break; + case entry::list_t: + print_list(out, e, indent + 1, single_line); + break; + case entry::dictionary_t: + print_dict(out, e, indent + 1, single_line); + break; + case entry::preformatted_t: out += ""; break; - case undefined_t: + case entry::undefined_t: out += ""; break; - default: - out += ""; - break; } } + + void print_list(std::string& out, entry const& e + , int const indent, bool const single_line) + { + out += single_line ? "[ " : "[\n"; + bool first = true; + for (auto const& item : e.list()) + { + if (!first) out += single_line ? ", " : ",\n"; + first = false; + if (!single_line) add_indent(out, indent); + to_string_impl(out, item, indent, single_line); + } + out += " ]"; + } + + void print_dict(std::string& out, entry const& e + , int const indent, bool const single_line) + { + out += single_line ? "{ " : "{\n"; + bool first = true; + for (auto const& item : e.dict()) + { + if (!first) out += single_line ? ", " : ",\n"; + first = false; + if (!single_line) add_indent(out, indent); + out += "'"; + out += print_string(item.first); + out += "': "; + + to_string_impl(out, item.second, indent+1, single_line); + } + out += " }"; + } +} + + std::string entry::to_string(bool const single_line) const + { + std::string ret; + to_string_impl(ret, *this, 0, single_line); + return ret; + } + }