2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <algorithm>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <array>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cctype> /* isdigit() */
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cstddef> // size_t, std::byte
|
|
|
|
#include <cstdint> // int64_t
|
2021-09-29 00:09:01 +00:00
|
|
|
#include <deque>
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <optional>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <string>
|
2021-10-15 13:28:47 +00:00
|
|
|
#include <string_view>
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <utility>
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-04-03 18:23:00 +00:00
|
|
|
#include <fmt/compile.h>
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#define LIBTRANSMISSION_VARIANT_MODULE
|
2017-11-14 20:21:28 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/benc.h"
|
|
|
|
#include "libtransmission/quark.h"
|
|
|
|
#include "libtransmission/tr-buffer.h"
|
|
|
|
#include "libtransmission/utils.h"
|
|
|
|
#include "libtransmission/variant.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2021-12-29 08:28:12 +00:00
|
|
|
auto constexpr MaxBencStrLength = size_t{ 128 * 1024 * 1024 }; // arbitrary
|
2017-05-16 18:37:00 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
namespace transmission::benc::impl
|
|
|
|
{
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
/**
|
|
|
|
* The initial i and trailing e are beginning and ending delimiters.
|
|
|
|
* You can have negative numbers such as i-3e. You cannot prefix the
|
|
|
|
* number with a zero such as i04e. However, i0e is valid.
|
|
|
|
* Example: i3e represents the integer "3"
|
2021-11-17 03:29:30 +00:00
|
|
|
*
|
|
|
|
* The maximum number of bit of this integer is unspecified,
|
2012-12-14 04:34:42 +00:00
|
|
|
* but to handle it as a signed 64bit integer is mandatory to handle
|
|
|
|
* "large files" aka .torrent for more that 4Gbyte
|
|
|
|
*/
|
2022-01-24 06:30:00 +00:00
|
|
|
std::optional<int64_t> ParseInt(std::string_view* benc)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-12-29 08:28:12 +00:00
|
|
|
auto constexpr Prefix = "i"sv;
|
|
|
|
auto constexpr Suffix = "e"sv;
|
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
// find the beginning delimiter
|
|
|
|
auto walk = *benc;
|
2023-06-30 14:49:58 +00:00
|
|
|
if (std::size(walk) < 3 || !tr_strv_starts_with(walk, Prefix))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 16:04:44 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
// find the ending delimiter
|
2021-12-29 08:28:12 +00:00
|
|
|
walk.remove_prefix(std::size(Prefix));
|
2022-02-08 05:44:31 +00:00
|
|
|
if (auto const pos = walk.find(Suffix); pos == std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 16:04:44 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
// leading zeroes are not allowed
|
2022-08-05 19:16:25 +00:00
|
|
|
if ((walk[0] == '0' && (isdigit(static_cast<unsigned char>(walk[1])) != 0)) ||
|
|
|
|
(walk[0] == '-' && walk[1] == '0' && (isdigit(static_cast<unsigned char>(walk[2])) != 0)))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 08:28:12 +00:00
|
|
|
// parse the string and make sure the next char is `Suffix`
|
2023-11-21 15:02:03 +00:00
|
|
|
auto value = tr_num_parse<int64_t>(walk, &walk);
|
2023-06-30 14:49:58 +00:00
|
|
|
if (!value || !tr_strv_starts_with(walk, Suffix))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 08:28:12 +00:00
|
|
|
walk.remove_prefix(std::size(Suffix));
|
2021-11-17 03:29:30 +00:00
|
|
|
*benc = walk;
|
2023-11-21 15:02:03 +00:00
|
|
|
return value;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Byte strings are encoded as follows:
|
|
|
|
* <string length encoded in base ten ASCII>:<string data>
|
|
|
|
* Note that there is no constant beginning delimiter, and no ending delimiter.
|
|
|
|
* Example: 4:spam represents the string "spam"
|
|
|
|
*/
|
2022-01-24 06:30:00 +00:00
|
|
|
std::optional<std::string_view> ParseString(std::string_view* benc)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
// find the ':' delimiter
|
|
|
|
auto const colon_pos = benc->find(':');
|
2021-12-17 05:47:51 +00:00
|
|
|
if (colon_pos == std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
return {};
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
// get the string length
|
2021-12-29 08:28:12 +00:00
|
|
|
auto svtmp = benc->substr(0, colon_pos);
|
2022-08-05 16:36:01 +00:00
|
|
|
if (!std::all_of(std::begin(svtmp), std::end(svtmp), [](auto ch) { return isdigit(static_cast<unsigned char>(ch)) != 0; }))
|
2022-04-06 21:39:39 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
auto const len = tr_num_parse<size_t>(svtmp, &svtmp);
|
2021-12-29 08:28:12 +00:00
|
|
|
if (!len || *len >= MaxBencStrLength)
|
2021-11-17 03:29:30 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
// do we have `len` bytes of string data?
|
2021-12-29 08:28:12 +00:00
|
|
|
svtmp = benc->substr(colon_pos + 1);
|
|
|
|
if (std::size(svtmp) < len)
|
2021-11-17 03:29:30 +00:00
|
|
|
{
|
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-12-29 08:28:12 +00:00
|
|
|
auto const string = svtmp.substr(0, *len);
|
|
|
|
*benc = svtmp.substr(*len);
|
2021-11-17 03:29:30 +00:00
|
|
|
return string;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
} // namespace transmission::benc::impl
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-01-24 06:30:00 +00:00
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace parse_helpers
|
|
|
|
{
|
2022-01-24 06:30:00 +00:00
|
|
|
struct MyHandler : public transmission::benc::Handler
|
|
|
|
{
|
|
|
|
tr_variant* const top_;
|
2023-08-17 16:02:45 +00:00
|
|
|
bool inplace_;
|
2022-01-24 06:30:00 +00:00
|
|
|
std::deque<tr_variant*> stack_;
|
|
|
|
std::optional<tr_quark> key_;
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
MyHandler(tr_variant* top, bool inplace)
|
2022-01-24 06:30:00 +00:00
|
|
|
: top_{ top }
|
2023-08-17 16:02:45 +00:00
|
|
|
, inplace_{ inplace }
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-03 17:03:28 +00:00
|
|
|
MyHandler(MyHandler&&) = delete;
|
|
|
|
MyHandler(MyHandler const&) = delete;
|
|
|
|
MyHandler& operator=(MyHandler&&) = delete;
|
|
|
|
MyHandler& operator=(MyHandler const&) = delete;
|
|
|
|
|
2022-01-25 01:05:24 +00:00
|
|
|
~MyHandler() override = default;
|
2022-01-24 19:07:55 +00:00
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool Int64(int64_t value, Context const& /*context*/) final
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-04-06 20:26:13 +00:00
|
|
|
auto* const variant = get_node();
|
|
|
|
if (variant == nullptr)
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2022-04-06 20:26:13 +00:00
|
|
|
return false;
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2022-01-24 06:30:00 +00:00
|
|
|
|
2023-11-10 21:41:32 +00:00
|
|
|
*variant = value;
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool String(std::string_view sv, Context const& /*context*/) final
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2023-11-10 21:41:32 +00:00
|
|
|
if (auto* const variant = get_node(); variant != nullptr)
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2023-11-10 21:41:32 +00:00
|
|
|
*variant = inplace_ ? tr_variant::unmanaged_string(sv) : tr_variant{ sv };
|
|
|
|
return true;
|
2022-04-06 20:26:13 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 21:41:32 +00:00
|
|
|
return false;
|
2022-01-24 06:30:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool StartDict(Context const& /*context*/) final
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-04-06 20:26:13 +00:00
|
|
|
auto* const variant = get_node();
|
|
|
|
if (variant == nullptr)
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2022-04-06 20:26:13 +00:00
|
|
|
return false;
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2022-01-24 06:30:00 +00:00
|
|
|
|
2022-04-06 20:26:13 +00:00
|
|
|
tr_variantInitDict(variant, 0);
|
|
|
|
stack_.push_back(variant);
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool Key(std::string_view sv, Context const& /*context*/) final
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
|
|
|
key_ = tr_quark_new(sv);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-11-18 05:37:35 +00:00
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool EndDict(Context const& /*context*/) final
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-04-06 20:26:13 +00:00
|
|
|
if (std::empty(stack_))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-04-06 20:26:13 +00:00
|
|
|
stack_.pop_back();
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool StartArray(Context const& /*context*/) final
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2022-04-06 20:26:13 +00:00
|
|
|
auto* const variant = get_node();
|
|
|
|
if (variant == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-06 20:26:13 +00:00
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2022-04-06 20:26:13 +00:00
|
|
|
tr_variantInitList(variant, 0);
|
|
|
|
stack_.push_back(variant);
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool EndArray(Context const& /*context*/) final
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-04-06 20:26:13 +00:00
|
|
|
if (std::empty(stack_))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-04-06 20:26:13 +00:00
|
|
|
stack_.pop_back();
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-11-28 01:58:35 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
private:
|
|
|
|
tr_variant* get_node()
|
|
|
|
{
|
|
|
|
tr_variant* node = nullptr;
|
2021-11-17 03:29:30 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
if (std::empty(stack_))
|
|
|
|
{
|
|
|
|
node = top_;
|
|
|
|
}
|
2023-08-21 21:16:54 +00:00
|
|
|
else if (auto* parent = stack_.back(); parent != nullptr && parent->holds_alternative<tr_variant::Vector>())
|
2022-04-06 20:26:13 +00:00
|
|
|
{
|
|
|
|
node = tr_variantListAdd(parent);
|
|
|
|
}
|
2023-08-21 21:16:54 +00:00
|
|
|
else if (key_ && parent != nullptr && parent->holds_alternative<tr_variant::Map>())
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-04-06 20:26:13 +00:00
|
|
|
node = tr_variantDictAdd(parent, *key_);
|
|
|
|
key_.reset();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
return node;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-01-24 06:30:00 +00:00
|
|
|
};
|
2023-01-05 04:16:22 +00:00
|
|
|
} // namespace parse_helpers
|
|
|
|
} // namespace
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
std::optional<tr_variant> tr_variant_serde::parse_benc(std::string_view input)
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2023-01-05 04:16:22 +00:00
|
|
|
using namespace parse_helpers;
|
2022-01-24 06:30:00 +00:00
|
|
|
using Stack = transmission::benc::ParserStack<512>;
|
2023-01-05 04:16:22 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
auto top = tr_variant{};
|
2022-01-24 06:30:00 +00:00
|
|
|
auto stack = Stack{};
|
2023-08-17 16:02:45 +00:00
|
|
|
auto handler = MyHandler{ &top, parse_inplace_ };
|
|
|
|
if (transmission::benc::parse(input, stack, handler, &end_, &error_) && std::empty(stack))
|
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
return std::optional<tr_variant>{ std::move(top) };
|
2023-08-17 16:02:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace to_string_helpers
|
|
|
|
{
|
2023-06-27 15:51:20 +00:00
|
|
|
using OutBuf = libtransmission::StackBuffer<1024U * 8U, std::byte>;
|
2022-10-19 16:42:08 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
void saveIntFunc(tr_variant const& /*var*/, int64_t const val, void* vout)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2023-05-18 21:56:29 +00:00
|
|
|
auto out = static_cast<OutBuf*>(vout);
|
|
|
|
|
|
|
|
auto const [buf, buflen] = out->reserve_space(64U);
|
|
|
|
auto* walk = reinterpret_cast<char*>(buf);
|
|
|
|
auto const* const begin = walk;
|
2023-08-17 16:02:45 +00:00
|
|
|
walk = fmt::format_to(walk, FMT_COMPILE("i{:d}e"), val);
|
2023-05-18 21:56:29 +00:00
|
|
|
out->commit_space(walk - begin);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
void saveBoolFunc(tr_variant const& /*var*/, bool const val, void* vout)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
static_cast<OutBuf*>(vout)->add(val ? "i1e"sv : "i0e"sv);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
void saveStringImpl(OutBuf* out, std::string_view sv)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2022-04-03 18:23:00 +00:00
|
|
|
// `${sv.size()}:${sv}`
|
2023-05-18 21:56:29 +00:00
|
|
|
auto const [buf, buflen] = out->reserve_space(std::size(sv) + 32U);
|
2023-08-21 04:15:23 +00:00
|
|
|
auto* begin = reinterpret_cast<char*>(buf);
|
|
|
|
auto* const end = fmt::format_to(begin, FMT_COMPILE("{:d}:{:s}"), std::size(sv), sv);
|
|
|
|
out->commit_space(end - begin);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
void saveStringFunc(tr_variant const& /*var*/, std::string_view const val, void* vout)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
saveStringImpl(static_cast<OutBuf*>(vout), val);
|
2022-04-03 18:23:00 +00:00
|
|
|
}
|
2019-02-18 22:38:24 +00:00
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
void saveRealFunc(tr_variant const& /*val*/, double const val, void* vout)
|
2022-04-03 18:23:00 +00:00
|
|
|
{
|
|
|
|
// the benc spec doesn't handle floats; save it as a string.
|
|
|
|
auto buf = std::array<char, 64>{};
|
2023-08-17 16:02:45 +00:00
|
|
|
auto const* const out = fmt::format_to(std::data(buf), FMT_COMPILE("{:f}"), val);
|
2023-05-18 21:56:29 +00:00
|
|
|
saveStringImpl(static_cast<OutBuf*>(vout), { std::data(buf), static_cast<size_t>(out - std::data(buf)) });
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
void saveDictBeginFunc(tr_variant const& /*val*/, void* vbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2023-05-18 21:56:29 +00:00
|
|
|
static_cast<OutBuf*>(vbuf)->push_back('d');
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
void saveListBeginFunc(tr_variant const& /*val*/, void* vbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2023-05-18 21:56:29 +00:00
|
|
|
static_cast<OutBuf*>(vbuf)->push_back('l');
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
void saveContainerEndFunc(tr_variant const& /*val*/, void* vbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2023-05-18 21:56:29 +00:00
|
|
|
static_cast<OutBuf*>(vbuf)->push_back('e');
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
} // namespace to_string_helpers
|
|
|
|
} // namespace
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
std::string tr_variant_serde::to_benc_string(tr_variant const& var)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-01-05 04:16:22 +00:00
|
|
|
using namespace to_string_helpers;
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
static auto constexpr Funcs = WalkFuncs{
|
|
|
|
saveIntFunc, //
|
|
|
|
saveBoolFunc, //
|
|
|
|
saveRealFunc, //
|
|
|
|
saveStringFunc, //
|
|
|
|
saveDictBeginFunc, //
|
|
|
|
saveListBeginFunc, //
|
|
|
|
saveContainerEndFunc, //
|
|
|
|
};
|
|
|
|
|
2023-05-18 21:56:29 +00:00
|
|
|
auto buf = OutBuf{};
|
2023-08-17 16:02:45 +00:00
|
|
|
walk(var, Funcs, &buf, true);
|
2023-01-28 02:12:09 +00:00
|
|
|
return buf.to_string();
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|