2020-01-06 18:21:16 +00:00
|
|
|
#pragma once
|
|
|
|
#include "toml_formatter.h"
|
|
|
|
#include "toml_table.h"
|
|
|
|
#include "toml_array.h"
|
|
|
|
#include "toml_utf8.h"
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
namespace toml::impl
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-12 15:37:02 +00:00
|
|
|
TOML_PUSH_WARNINGS
|
|
|
|
TOML_DISABLE_ALL_WARNINGS
|
|
|
|
|
2020-01-11 21:15:24 +00:00
|
|
|
[[nodiscard]]
|
2020-01-07 15:52:50 +00:00
|
|
|
inline toml::string default_formatter_make_key_segment(const toml::string& str) noexcept
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
if (str.empty())
|
|
|
|
return TOML_STRING_PREFIX("''"s);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool requiresQuotes = false;
|
|
|
|
{
|
|
|
|
impl::utf8_decoder decoder;
|
|
|
|
for (size_t i = 0; i < str.length() && !requiresQuotes; i++)
|
|
|
|
{
|
|
|
|
decoder(static_cast<uint8_t>(str[i]));
|
|
|
|
if (decoder.error())
|
|
|
|
requiresQuotes = true;
|
|
|
|
else if (decoder.has_code_point())
|
|
|
|
requiresQuotes = !impl::is_bare_key_character(decoder.codepoint);
|
|
|
|
}
|
|
|
|
}
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
if (requiresQuotes)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
toml::string s;
|
|
|
|
s.reserve(str.length() + 2_sz);
|
|
|
|
s += TOML_STRING_PREFIX('"');
|
|
|
|
for (auto c : str)
|
2020-01-12 15:37:02 +00:00
|
|
|
{
|
|
|
|
if (c >= TOML_STRING_PREFIX('\x00') && c <= TOML_STRING_PREFIX('\x1F')) TOML_UNLIKELY
|
|
|
|
s.append(low_character_escape_table[c]);
|
|
|
|
else if (c == TOML_STRING_PREFIX('\x7F')) TOML_UNLIKELY
|
|
|
|
s.append(TOML_STRING_PREFIX("\\u007F"sv));
|
|
|
|
else if (c == TOML_STRING_PREFIX('"')) TOML_UNLIKELY
|
|
|
|
s.append(TOML_STRING_PREFIX("\\\""sv));
|
|
|
|
else
|
|
|
|
s += c;
|
|
|
|
}
|
2020-01-07 15:52:50 +00:00
|
|
|
s += TOML_STRING_PREFIX('"');
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:37:02 +00:00
|
|
|
TOML_POP_WARNINGS
|
|
|
|
|
2020-01-11 21:15:24 +00:00
|
|
|
[[nodiscard]]
|
2020-01-07 15:52:50 +00:00
|
|
|
inline size_t default_formatter_inline_columns(const node& node) noexcept
|
|
|
|
{
|
|
|
|
return node.visit([](const auto& n) noexcept
|
|
|
|
-> size_t
|
|
|
|
{
|
2020-01-13 06:31:49 +00:00
|
|
|
if constexpr (is_table<decltype(n)>)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
if (n.empty())
|
|
|
|
return 2_sz; // "{}"
|
|
|
|
size_t weight = 3_sz; // "{ }"
|
|
|
|
for (auto [k, v] : n)
|
|
|
|
weight += k.length() + default_formatter_inline_columns(v) + 2_sz; // + ", "
|
|
|
|
return weight;
|
|
|
|
}
|
2020-01-13 06:31:49 +00:00
|
|
|
else if constexpr (is_array<decltype(n)>)
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
|
|
|
if (n.empty())
|
|
|
|
return 2_sz; // "[]"
|
|
|
|
size_t weight = 3_sz; // "[ ]"
|
|
|
|
for (auto& elem : n)
|
|
|
|
weight += default_formatter_inline_columns(elem) + 2_sz; // + ", "
|
|
|
|
return weight;
|
|
|
|
}
|
2020-01-13 06:31:49 +00:00
|
|
|
else if constexpr (is_string<decltype(n)>)
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
|
|
|
return n.get().length() + 2_sz; // + ""
|
|
|
|
}
|
2020-01-13 06:31:49 +00:00
|
|
|
else if constexpr (is_integer<decltype(n)>)
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
|
|
|
auto v = n.get();
|
|
|
|
if (!v)
|
|
|
|
return 1_sz;
|
|
|
|
size_t weight = {};
|
|
|
|
if (v < 0)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
weight += 1;
|
|
|
|
v *= -1;
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
2020-01-07 15:52:50 +00:00
|
|
|
return weight + static_cast<size_t>(std::log10(static_cast<double>(v)));
|
|
|
|
}
|
2020-01-13 06:31:49 +00:00
|
|
|
else if constexpr (is_floating_point<decltype(n)>)
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
|
|
|
auto v = n.get();
|
|
|
|
if (v == 0.0)
|
|
|
|
return 3_sz;
|
|
|
|
size_t weight = 2_sz; // ".0"
|
|
|
|
if (v < 0.0)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
weight += 1;
|
|
|
|
v *= -1.0;
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
2020-01-07 15:52:50 +00:00
|
|
|
return weight + static_cast<size_t>(std::log10(v));
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
2020-01-13 06:31:49 +00:00
|
|
|
else if constexpr (is_boolean<decltype(n)>)
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
|
|
|
return 5_sz;
|
|
|
|
}
|
2020-01-13 06:31:49 +00:00
|
|
|
else if constexpr (is_date<decltype(n)> || is_time<decltype(n)>)
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
|
|
|
return 10_sz;
|
|
|
|
}
|
2020-01-13 06:31:49 +00:00
|
|
|
else if constexpr (is_date_time<decltype(n)>)
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
|
|
|
return 30_sz;
|
|
|
|
}
|
|
|
|
TOML_UNREACHABLE;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-11 21:15:24 +00:00
|
|
|
[[nodiscard]]
|
2020-01-07 15:52:50 +00:00
|
|
|
inline bool default_formatter_forces_multiline(const node& node, size_t starting_column_bias = 0) noexcept
|
|
|
|
{
|
|
|
|
return (default_formatter_inline_columns(node) + starting_column_bias) > 120_sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace toml
|
|
|
|
{
|
|
|
|
template <typename CHAR = char>
|
|
|
|
class default_formatter final : impl::formatter<CHAR>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
using base = impl::formatter<CHAR>;
|
|
|
|
std::vector<toml::string> key_path;
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
void print_key_segment(const toml::string& str) TOML_MAY_THROW
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
|
|
|
if (str.empty())
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream("''"sv, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bool requiresQuotes = false;
|
|
|
|
{
|
|
|
|
impl::utf8_decoder decoder;
|
|
|
|
for (size_t i = 0; i < str.length() && !requiresQuotes; i++)
|
|
|
|
{
|
|
|
|
decoder(static_cast<uint8_t>(str[i]));
|
|
|
|
if (decoder.error())
|
|
|
|
requiresQuotes = true;
|
|
|
|
else if (decoder.has_code_point())
|
|
|
|
requiresQuotes = !impl::is_bare_key_character(decoder.codepoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requiresQuotes)
|
2020-01-07 15:52:50 +00:00
|
|
|
base::print_quoted_string(str);
|
2020-01-06 18:21:16 +00:00
|
|
|
else
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream(str, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
2020-01-07 15:52:50 +00:00
|
|
|
base::clear_naked_newline();
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
void print_key_path() TOML_MAY_THROW
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
|
|
|
for (const auto& segment : key_path)
|
|
|
|
{
|
|
|
|
if (std::addressof(segment) > key_path.data())
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream('.', base::stream());
|
|
|
|
impl::print_to_stream(segment, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
2020-01-07 15:52:50 +00:00
|
|
|
base::clear_naked_newline();
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
inline void print_inline(const table& /*tbl*/) TOML_MAY_THROW;
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
void print(const array& arr) TOML_MAY_THROW
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
|
|
|
if (arr.empty())
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream("[]"sv, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
else
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
const auto original_indent = base::indent();
|
|
|
|
const auto multiline = impl::default_formatter_forces_multiline(
|
2020-01-06 18:21:16 +00:00
|
|
|
arr,
|
2020-01-12 15:37:02 +00:00
|
|
|
base::indent_columns * static_cast<size_t>(original_indent < 0 ? 0 : original_indent)
|
2020-01-06 18:21:16 +00:00
|
|
|
);
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream("["sv, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
if (multiline)
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
if (original_indent < 0)
|
|
|
|
base::indent(0);
|
|
|
|
base::increase_indent();
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
else
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream(' ', base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < arr.size(); i++)
|
|
|
|
{
|
|
|
|
if (i > 0_sz)
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream(',', base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
if (!multiline)
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream(' ', base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (multiline)
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
base::print_newline(true);
|
|
|
|
base::print_indent();
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
auto& v = arr[i];
|
|
|
|
const auto type = v.type();
|
2020-01-06 18:21:16 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
case node_type::table: print_inline(*reinterpret_cast<const table*>(&v)); break;
|
|
|
|
case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
|
2020-01-06 18:21:16 +00:00
|
|
|
default:
|
2020-01-07 15:52:50 +00:00
|
|
|
base::print(v, type);
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (multiline)
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
base::indent(original_indent);
|
|
|
|
base::print_newline(true);
|
|
|
|
base::print_indent();
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
else
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream(' ', base::stream());
|
|
|
|
impl::print_to_stream("]"sv, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
2020-01-07 15:52:50 +00:00
|
|
|
base::clear_naked_newline();
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
void print(const table& tbl) TOML_MAY_THROW
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
|
|
|
//values, arrays, and inline tables
|
2020-01-07 15:52:50 +00:00
|
|
|
for (auto [k, v] : tbl)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
const auto type = v.type();
|
|
|
|
if ((type == node_type::table && !reinterpret_cast<const table*>(&v)->is_inline())
|
|
|
|
|| (type == node_type::array && reinterpret_cast<const array*>(&v)->is_array_of_tables()))
|
2020-01-06 18:21:16 +00:00
|
|
|
continue;
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
base::print_newline();
|
|
|
|
base::print_indent();
|
|
|
|
print_key_segment(k);
|
|
|
|
impl::print_to_stream(" = "sv, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
case node_type::table: print_inline(*reinterpret_cast<const table*>(&v)); break;
|
|
|
|
case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
|
2020-01-06 18:21:16 +00:00
|
|
|
default:
|
2020-01-07 15:52:50 +00:00
|
|
|
base::print(v, type);
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//non-inline tables
|
2020-01-07 15:52:50 +00:00
|
|
|
for (auto [k, v] : tbl)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
const auto type = v.type();
|
|
|
|
if (type != node_type::table || reinterpret_cast<const table*>(&v)->is_inline())
|
2020-01-06 18:21:16 +00:00
|
|
|
continue;
|
2020-01-07 15:52:50 +00:00
|
|
|
auto& child_tbl = *reinterpret_cast<const table*>(&v);
|
2020-01-06 18:21:16 +00:00
|
|
|
|
|
|
|
//we can skip indenting and emitting the headers for tables that only contain other tables
|
|
|
|
//(so we don't over-nest)
|
|
|
|
size_t child_value_count{}; //includes inline tables and non-table arrays
|
|
|
|
size_t child_table_count{};
|
|
|
|
size_t child_table_array_count{};
|
2020-01-07 15:52:50 +00:00
|
|
|
for (auto [child_k, child_v] : child_tbl)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
const auto child_type = child_v.type();
|
2020-01-06 18:21:16 +00:00
|
|
|
switch (child_type)
|
|
|
|
{
|
|
|
|
case node_type::table:
|
2020-01-07 15:52:50 +00:00
|
|
|
if (reinterpret_cast<const table*>(&child_v)->is_inline())
|
2020-01-06 18:21:16 +00:00
|
|
|
child_value_count++;
|
|
|
|
else
|
|
|
|
child_table_count++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case node_type::array:
|
2020-01-07 15:52:50 +00:00
|
|
|
if (reinterpret_cast<const array*>(&child_v)->is_array_of_tables())
|
2020-01-06 18:21:16 +00:00
|
|
|
child_table_array_count++;
|
|
|
|
else
|
|
|
|
child_value_count++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
child_value_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool skip_self = false;
|
|
|
|
if (child_value_count == 0_sz && (child_table_count > 0_sz || child_table_array_count > 0_sz))
|
|
|
|
skip_self = true;
|
|
|
|
|
|
|
|
if (!skip_self)
|
2020-01-07 15:52:50 +00:00
|
|
|
base::increase_indent();
|
|
|
|
key_path.push_back(impl::default_formatter_make_key_segment(k));
|
2020-01-06 18:21:16 +00:00
|
|
|
|
|
|
|
if (!skip_self)
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
base::print_newline();
|
|
|
|
base::print_newline(true);
|
|
|
|
base::print_indent();
|
|
|
|
impl::print_to_stream("["sv, base::stream());
|
|
|
|
print_key_path();
|
|
|
|
impl::print_to_stream("]"sv, base::stream());
|
|
|
|
base::print_newline(true);
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
print(child_tbl);
|
2020-01-06 18:21:16 +00:00
|
|
|
|
|
|
|
key_path.pop_back();
|
|
|
|
if (!skip_self)
|
2020-01-07 15:52:50 +00:00
|
|
|
base::decrease_indent();
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//table arrays
|
2020-01-07 15:52:50 +00:00
|
|
|
for (auto [k, v] : tbl)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
const auto type = v.type();
|
|
|
|
if (type != node_type::array || !reinterpret_cast<const array*>(&v)->is_array_of_tables())
|
2020-01-06 18:21:16 +00:00
|
|
|
continue;
|
2020-01-07 15:52:50 +00:00
|
|
|
auto& arr = *reinterpret_cast<const array*>(&v);
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
base::increase_indent();
|
|
|
|
key_path.push_back(impl::default_formatter_make_key_segment(k));
|
2020-01-06 18:21:16 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < arr.size(); i++)
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
base::print_newline();
|
|
|
|
base::print_newline(true);
|
|
|
|
base::print_indent();
|
|
|
|
impl::print_to_stream("[["sv, base::stream());
|
|
|
|
print_key_path();
|
|
|
|
impl::print_to_stream("]]"sv, base::stream());
|
|
|
|
base::print_newline(true);
|
|
|
|
print(*reinterpret_cast<const table*>(&arr[i]));
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key_path.pop_back();
|
2020-01-07 15:52:50 +00:00
|
|
|
base::decrease_indent();
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:37:02 +00:00
|
|
|
void print() TOML_MAY_THROW
|
|
|
|
{
|
|
|
|
switch (auto source_type = base::source().type())
|
|
|
|
{
|
|
|
|
case node_type::table:
|
|
|
|
{
|
|
|
|
auto& tbl = *reinterpret_cast<const table*>(&base::source());
|
|
|
|
if (tbl.is_inline() || (base::flags() & format_flags::always_print_as_inline) != format_flags::none)
|
|
|
|
print_inline(tbl);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
base::decrease_indent(); // so root kvps and tables have the same indent
|
|
|
|
print(tbl);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case node_type::array:
|
|
|
|
print(*reinterpret_cast<const array*>(&base::source()));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
base::print(base::source(), source_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 18:21:16 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
TOML_NODISCARD_CTOR
|
2020-01-12 15:37:02 +00:00
|
|
|
explicit default_formatter(const toml::node& source, format_flags flags = {}) noexcept
|
|
|
|
: base{ source, flags }
|
2020-01-06 18:21:16 +00:00
|
|
|
{}
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
template <typename T>
|
|
|
|
friend std::basic_ostream<CHAR>& operator << (std::basic_ostream<T>& lhs, default_formatter& rhs)
|
2020-01-06 18:21:16 +00:00
|
|
|
TOML_MAY_THROW
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
rhs.attach(lhs);
|
2020-01-06 18:21:16 +00:00
|
|
|
rhs.key_path.clear();
|
2020-01-12 15:37:02 +00:00
|
|
|
rhs.print();
|
2020-01-06 18:21:16 +00:00
|
|
|
rhs.detach();
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
template <typename T>
|
|
|
|
friend std::basic_ostream<CHAR>& operator << (std::basic_ostream<T>& lhs, default_formatter&& rhs)
|
2020-01-06 18:21:16 +00:00
|
|
|
TOML_MAY_THROW
|
|
|
|
{
|
|
|
|
return lhs << rhs; //as lvalue
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
template <typename CHAR>
|
|
|
|
inline void default_formatter<CHAR>::print_inline(const toml::table& tbl) TOML_MAY_THROW
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
if (tbl.empty())
|
|
|
|
impl::print_to_stream("{}"sv, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
else
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream("{ "sv, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
|
|
|
|
bool first = false;
|
2020-01-07 15:52:50 +00:00
|
|
|
for (auto [k, v] : tbl)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
|
|
|
if (first)
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream(", "sv, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
first = true;
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
print_key_segment(k);
|
|
|
|
impl::print_to_stream(" = "sv, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
const auto type = v.type();
|
2020-01-06 18:21:16 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
case node_type::table: print_inline(*reinterpret_cast<const table*>(&v)); break;
|
|
|
|
case node_type::array: print(*reinterpret_cast<const array*>(&v)); break;
|
2020-01-06 18:21:16 +00:00
|
|
|
default:
|
2020-01-07 15:52:50 +00:00
|
|
|
base::print(v, type);
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
impl::print_to_stream(" }"sv, base::stream());
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
2020-01-07 15:52:50 +00:00
|
|
|
base::clear_naked_newline();
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename CHAR>
|
|
|
|
inline std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, const table& rhs) TOML_MAY_THROW
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
return lhs << default_formatter<CHAR>{ rhs };
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
2020-01-12 15:37:02 +00:00
|
|
|
|
|
|
|
template <typename CHAR>
|
|
|
|
inline std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, const array& rhs) TOML_MAY_THROW
|
|
|
|
{
|
|
|
|
return lhs << default_formatter<CHAR>{ rhs };
|
|
|
|
}
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|