NeBuild dev
Loading...
Searching...
No Matches
yaml_formatter.inl
Go to the documentation of this file.
1// # This file is a part of toml++ and is subject to the the terms of the MIT license.
2// # Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
3// # See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
4// SPDX-License-Identifier: MIT
5#pragma once
6
7#include "preprocessor.hpp"
8// # {{
9#if !TOML_IMPLEMENTATION
10#error This is an implementation-only header.
11#endif
12// # }}
13#if TOML_ENABLE_FORMATTERS
14
15#include "array.hpp"
16#include "header_start.hpp"
17#include "print_to_stream.hpp"
18#include "table.hpp"
19#include "yaml_formatter.hpp"
20
23 void yaml_formatter::print_yaml_string(const value<std::string>& str) {
24 if (str->empty()) {
25 base::print(str);
26 return;
27 }
28
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';
32
33 if (contains_newline) {
34 print_unformatted("|-"sv);
35
36 increase_indent();
37
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++);
44
45 if TOML_LIKELY (line_start != line_end || line_end != end) {
46 print_newline();
47 print_indent();
48 print_unformatted(
49 std::string_view{line_start, static_cast<size_t>(line_end - line_start)});
50 }
51 }
52
53 decrease_indent();
54 } else
55 print_string(*str, false, true);
56 }
57
59 void yaml_formatter::print(const toml::table& tbl, bool parent_is_array) {
60 if (tbl.empty()) {
61 print_unformatted("{}"sv);
62 return;
63 }
64
65 increase_indent();
66
67 for (auto&& [k, v] : tbl) {
68 if (!parent_is_array) {
69 print_newline();
70 print_indent();
71 }
72 parent_is_array = false;
73
74 print_string(k.str(), false, true);
75 if (terse_kvps())
76 print_unformatted(":"sv);
77 else
78 print_unformatted(": "sv);
79
80 const auto type = v.type();
81 TOML_ASSUME(type != node_type::none);
82 switch (type) {
83 case node_type::table:
84 print(*reinterpret_cast<const table*>(&v));
85 break;
86 case node_type::array:
87 print(*reinterpret_cast<const array*>(&v));
88 break;
89 case node_type::string:
90 print_yaml_string(*reinterpret_cast<const value<std::string>*>(&v));
91 break;
92 default:
93 print_value(v, type);
94 }
95 }
96
97 decrease_indent();
98 }
99
101 void yaml_formatter::print(const toml::array& arr, bool parent_is_array) {
102 if (arr.empty()) {
103 print_unformatted("[]"sv);
104 return;
105 }
106
107 increase_indent();
108
109 for (auto&& v : arr) {
110 if (!parent_is_array) {
111 print_newline();
112 print_indent();
113 }
114 parent_is_array = false;
115
116 print_unformatted("- "sv);
117
118 const auto type = v.type();
119 TOML_ASSUME(type != node_type::none);
120 switch (type) {
121 case node_type::table:
122 print(*reinterpret_cast<const table*>(&v), true);
123 break;
124 case node_type::array:
125 print(*reinterpret_cast<const array*>(&v), true);
126 break;
127 case node_type::string:
128 print_yaml_string(*reinterpret_cast<const value<std::string>*>(&v));
129 break;
130 default:
131 print_value(v, type);
132 }
133 }
134
135 decrease_indent();
136 }
137
139 void yaml_formatter::print() {
140 if (dump_failed_parse_result()) return;
141
142 switch (auto source_type = source().type()) {
143 case node_type::table:
144 decrease_indent(); // so root kvps and tables have the same indent
145 print(*reinterpret_cast<const table*>(&source()));
146 break;
147
148 case node_type::array:
149 print(*reinterpret_cast<const array*>(&source()));
150 break;
151
152 case node_type::string:
153 print_yaml_string(*reinterpret_cast<const value<std::string>*>(&source()));
154 break;
155
156 default:
157 print_value(source(), source_type);
158 }
159 }
160}
162
163#include "header_end.hpp"
164#endif // TOML_ENABLE_FORMATTERS
A TOML array.
Definition array.hpp:285
A TOML table.
Definition table.hpp:220
#define TOML_LIKELY(...)
Definition preprocessor.hpp:525
#define TOML_EXTERNAL_LINKAGE
Definition preprocessor.hpp:1339
#define TOML_NAMESPACE_END
Definition preprocessor.hpp:1320
#define TOML_ASSUME(expr)
Definition preprocessor.hpp:506
TOML_NAMESPACE_START
Definition yaml_formatter.inl:21