23 void yaml_formatter::print_yaml_string(
const value<std::string>& str) {
29 bool contains_newline =
false;
30 for (
auto c = str->c_str(), e = str->c_str() + str->length(); c < e && !contains_newline; c++)
31 contains_newline = *c ==
'\n';
33 if (contains_newline) {
34 print_unformatted(
"|-"sv);
38 auto line_end = str->c_str() - 1u;
39 const auto end = str->c_str() + str->length();
40 while (line_end != end) {
41 auto line_start = line_end + 1u;
42 line_end = line_start;
43 for (; line_end != end && *line_end !=
'\n'; line_end++);
45 if TOML_LIKELY (line_start != line_end || line_end != end) {
49 std::string_view{line_start,
static_cast<size_t>(line_end - line_start)});
55 print_string(*str,
false,
true);
59 void yaml_formatter::print(
const toml::table& tbl,
bool parent_is_array) {
61 print_unformatted(
"{}"sv);
67 for (
auto&& [k, v] : tbl) {
68 if (!parent_is_array) {
72 parent_is_array =
false;
74 print_string(k.str(),
false,
true);
76 print_unformatted(
":"sv);
78 print_unformatted(
": "sv);
80 const auto type = v.type();
83 case node_type::table:
84 print(*
reinterpret_cast<const table*
>(&v));
86 case node_type::array:
87 print(*
reinterpret_cast<const array*
>(&v));
89 case node_type::string:
90 print_yaml_string(*
reinterpret_cast<const value<std::string>*
>(&v));
101 void yaml_formatter::print(
const toml::array& arr,
bool parent_is_array) {
103 print_unformatted(
"[]"sv);
109 for (
auto&& v : arr) {
110 if (!parent_is_array) {
114 parent_is_array =
false;
116 print_unformatted(
"- "sv);
118 const auto type = v.type();
121 case node_type::table:
122 print(*
reinterpret_cast<const table*
>(&v),
true);
124 case node_type::array:
125 print(*
reinterpret_cast<const array*
>(&v),
true);
127 case node_type::string:
128 print_yaml_string(*
reinterpret_cast<const value<std::string>*
>(&v));
131 print_value(v, type);
139 void yaml_formatter::print() {
140 if (dump_failed_parse_result())
return;
142 switch (
auto source_type = source().type()) {
143 case node_type::table:
145 print(*
reinterpret_cast<const table*
>(&source()));
148 case node_type::array:
149 print(*
reinterpret_cast<const array*
>(&source()));
152 case node_type::string:
153 print_yaml_string(*
reinterpret_cast<const value<std::string>*
>(&source()));
157 print_value(source(), source_type);