factor out some parts of print function for entry

This commit is contained in:
Arvid Norberg 2018-10-02 08:57:50 +02:00 committed by Arvid Norberg
parent c096f63f59
commit 949867eca9
2 changed files with 64 additions and 56 deletions

View File

@ -311,8 +311,6 @@ namespace aux {
private: private:
void to_string_impl(std::string& out, int indent, bool single_line) const;
aux::aligned_union<1 aux::aligned_union<1
#if TORRENT_COMPLETE_TYPES_REQUIRED #if TORRENT_COMPLETE_TYPES_REQUIRED
// for implementations that require complete types, use char and hope // for implementations that require complete types, use char and hope

View File

@ -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 { namespace {
bool is_binary(std::string const& str) bool is_binary(std::string const& str)
{ {
@ -689,62 +682,79 @@ namespace {
{ {
out.resize(out.size() + size_t(indent), ' '); out.resize(out.size() + size_t(indent), ' ');
} }
}
void entry::to_string_impl(std::string& out, int const indent void print_list(std::string&, entry const&, int, bool);
, bool const single_line) const 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); TORRENT_ASSERT(indent >= 0);
switch (m_type) switch (e.type())
{ {
case int_t: case entry::int_t:
out += libtorrent::to_string(integer()).data(); out += libtorrent::to_string(e.integer()).data();
break; break;
case string_t: case entry::string_t:
{ out += "'";
out += "'"; out += print_string(e.string());
out += print_string(string()); out += "'";
out += "'"; break;
} break; case entry::list_t:
case list_t: print_list(out, e, indent + 1, single_line);
{ break;
out += single_line ? "[ " : "[\n"; case entry::dictionary_t:
bool first = true; print_dict(out, e, indent + 1, single_line);
for (auto const& item : list()) break;
{ case entry::preformatted_t:
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:
out += "<preformatted>"; out += "<preformatted>";
break; break;
case undefined_t: case entry::undefined_t:
out += "<uninitialized>"; out += "<uninitialized>";
break; break;
default:
out += "<error>";
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;
}
} }