/src/glaze/include/glaze/json/prettify.hpp
Line | Count | Source |
1 | | // Glaze Library |
2 | | // For the license information refer to glaze.hpp |
3 | | |
4 | | #pragma once |
5 | | |
6 | | // Prettifying JSONC preserves comments of both styles. A line comment runs to the end of its |
7 | | // line, so whatever follows one is put on a new line rather than behind it. |
8 | | |
9 | | #include "glaze/json/json_format.hpp" |
10 | | |
11 | | namespace glz |
12 | | { |
13 | | namespace detail |
14 | | { |
15 | | template <auto Opts> |
16 | | inline void prettify_json(is_context auto&& ctx, auto&& it, auto&& end, auto&& b, auto& ix) |
17 | 8.01k | { |
18 | 8.01k | constexpr bool use_tabs = check_indentation_char(Opts) == '\t'; |
19 | 8.01k | constexpr auto indent_width = check_indentation_width(Opts); |
20 | | |
21 | 8.01k | using enum json_type; |
22 | | |
23 | | // Indexed by nesting level, with level 0 the top. Sized for the depth the readers accept (see |
24 | | // enter_depth), so that every document glz::validate_jsonc passes can be prettified. |
25 | 8.01k | std::array<json_type, max_recursive_depth_limit + 1> state{}; |
26 | 8.01k | int64_t indent{}; |
27 | | |
28 | | // Enters one level of nesting, failing past the readers' limit. |
29 | 1.64M | const auto open_level = [&](const json_type type) { |
30 | 1.64M | if (size_t(indent) >= max_recursive_depth_limit) [[unlikely]] { |
31 | 138 | ctx.error = error_code::exceeded_max_recursive_depth; |
32 | 138 | return false; |
33 | 138 | } |
34 | 1.64M | state[++indent] = type; |
35 | 1.64M | return true; |
36 | 1.64M | }; _ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb0ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlNS0_9json_typeEE_clESQ_ Line | Count | Source | 29 | 812k | const auto open_level = [&](const json_type type) { | 30 | 812k | if (size_t(indent) >= max_recursive_depth_limit) [[unlikely]] { | 31 | 58 | ctx.error = error_code::exceeded_max_recursive_depth; | 32 | 58 | return false; | 33 | 58 | } | 34 | 812k | state[++indent] = type; | 35 | 812k | return true; | 36 | 812k | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlNS0_9json_typeEE_clESQ_ Line | Count | Source | 29 | 415k | const auto open_level = [&](const json_type type) { | 30 | 415k | if (size_t(indent) >= max_recursive_depth_limit) [[unlikely]] { | 31 | 40 | ctx.error = error_code::exceeded_max_recursive_depth; | 32 | 40 | return false; | 33 | 40 | } | 34 | 415k | state[++indent] = type; | 35 | 415k | return true; | 36 | 415k | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb1ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlNS0_9json_typeEE_clESQ_ Line | Count | Source | 29 | 415k | const auto open_level = [&](const json_type type) { | 30 | 415k | if (size_t(indent) >= max_recursive_depth_limit) [[unlikely]] { | 31 | 40 | ctx.error = error_code::exceeded_max_recursive_depth; | 32 | 40 | return false; | 33 | 40 | } | 34 | 415k | state[++indent] = type; | 35 | 415k | return true; | 36 | 415k | }; |
|
37 | | |
38 | | // Set while the output cursor sits on a line that a `//` comment has commented out. Only |
39 | | // the comment-enabled path can set it, so the plain JSON writer keeps the code it had. |
40 | 8.01k | [[maybe_unused]] bool line_comment_open = false; |
41 | | |
42 | | // Every line break the writer emits goes through this, because a break is also what ends a |
43 | | // line comment: doing it in one place is what keeps the two from doubling up. The newline |
44 | | // and the indentation behind it are also what make the output unbounded -- there is no |
45 | | // bound on prettified output that a single reservation up front could use -- so this is |
46 | | // the write a bounded destination is likeliest to run out on. |
47 | 121M | const auto new_line = [&] { |
48 | | if constexpr (has_bounded_capacity<decltype(b)>) { |
49 | | const size_t n = use_tabs ? size_t(indent) : size_t(indent) * indent_width; |
50 | | if (not ensure_space(ctx, b, ix + 1 + n)) [[unlikely]] { |
51 | | return false; |
52 | | } |
53 | | } |
54 | 121M | append_new_line<use_tabs, indent_width>(b, ix, indent); |
55 | 121M | if constexpr (Opts.comments) { |
56 | 76.7M | line_comment_open = false; |
57 | 76.7M | } |
58 | 121M | return true; |
59 | 121M | }; _ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb0ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlvE_clEv Line | Count | Source | 47 | 44.6M | const auto new_line = [&] { | 48 | | if constexpr (has_bounded_capacity<decltype(b)>) { | 49 | | const size_t n = use_tabs ? size_t(indent) : size_t(indent) * indent_width; | 50 | | if (not ensure_space(ctx, b, ix + 1 + n)) [[unlikely]] { | 51 | | return false; | 52 | | } | 53 | | } | 54 | 44.6M | append_new_line<use_tabs, indent_width>(b, ix, indent); | 55 | | if constexpr (Opts.comments) { | 56 | | line_comment_open = false; | 57 | | } | 58 | 44.6M | return true; | 59 | 44.6M | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlvE_clEv Line | Count | Source | 47 | 38.3M | const auto new_line = [&] { | 48 | | if constexpr (has_bounded_capacity<decltype(b)>) { | 49 | | const size_t n = use_tabs ? size_t(indent) : size_t(indent) * indent_width; | 50 | | if (not ensure_space(ctx, b, ix + 1 + n)) [[unlikely]] { | 51 | | return false; | 52 | | } | 53 | | } | 54 | 38.3M | append_new_line<use_tabs, indent_width>(b, ix, indent); | 55 | 38.3M | if constexpr (Opts.comments) { | 56 | 38.3M | line_comment_open = false; | 57 | 38.3M | } | 58 | 38.3M | return true; | 59 | 38.3M | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb1ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlvE_clEv Line | Count | Source | 47 | 38.3M | const auto new_line = [&] { | 48 | | if constexpr (has_bounded_capacity<decltype(b)>) { | 49 | | const size_t n = use_tabs ? size_t(indent) : size_t(indent) * indent_width; | 50 | | if (not ensure_space(ctx, b, ix + 1 + n)) [[unlikely]] { | 51 | | return false; | 52 | | } | 53 | | } | 54 | 38.3M | append_new_line<use_tabs, indent_width>(b, ix, indent); | 55 | 38.3M | if constexpr (Opts.comments) { | 56 | 38.3M | line_comment_open = false; | 57 | 38.3M | } | 58 | 38.3M | return true; | 59 | 38.3M | }; |
|
60 | | |
61 | | // Every write below goes through this, which is what fixes their order in one place: an |
62 | | // open line comment is closed first, because anything written into one is commented out, |
63 | | // and emit_bytes then takes the reservation from the argument rather than from a byte |
64 | | // count spelled out beside the write. |
65 | | // |
66 | | // Deferring that break to the next write rather than emitting it with the comment is what |
67 | | // keeps a comment at the end of the input from leaving a trailing newline behind, and lets |
68 | | // a break the writer was going to emit anyway serve as this one. |
69 | | // |
70 | | // A break that fails is reported rather than swallowed: on a bounded destination that ran |
71 | | // out, carrying on would leave `line_comment_open` set and put this write *inside* the |
72 | | // comment, which is not a truncated document but a wrong one. |
73 | 121M | const auto put = [&](const auto& x) { |
74 | 121M | if constexpr (Opts.comments) { |
75 | 76.9M | if (line_comment_open) [[unlikely]] { |
76 | 15.3k | if (not new_line()) [[unlikely]] { |
77 | 0 | return false; |
78 | 0 | } |
79 | 15.3k | } |
80 | 76.9M | } |
81 | 76.9M | return emit_bytes<true>(ctx, b, ix, x); |
82 | 121M | }; _ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb0ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlRKT_E_clINS8_17basic_string_viewIcSB_EEEEDaSS_ Line | Count | Source | 73 | 149k | const auto put = [&](const auto& x) { | 74 | | if constexpr (Opts.comments) { | 75 | | if (line_comment_open) [[unlikely]] { | 76 | | if (not new_line()) [[unlikely]] { | 77 | | return false; | 78 | | } | 79 | | } | 80 | | } | 81 | 149k | return emit_bytes<true>(ctx, b, ix, x); | 82 | 149k | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb0ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlRKT_E_clIcEEDaSS_ Line | Count | Source | 73 | 44.6M | const auto put = [&](const auto& x) { | 74 | | if constexpr (Opts.comments) { | 75 | | if (line_comment_open) [[unlikely]] { | 76 | | if (not new_line()) [[unlikely]] { | 77 | | return false; | 78 | | } | 79 | | } | 80 | | } | 81 | 44.6M | return emit_bytes<true>(ctx, b, ix, x); | 82 | 44.6M | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb0ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlRKT_E_clIA5_cEEDaSS_ Line | Count | Source | 73 | 3.86k | const auto put = [&](const auto& x) { | 74 | | if constexpr (Opts.comments) { | 75 | | if (line_comment_open) [[unlikely]] { | 76 | | if (not new_line()) [[unlikely]] { | 77 | | return false; | 78 | | } | 79 | | } | 80 | | } | 81 | 3.86k | return emit_bytes<true>(ctx, b, ix, x); | 82 | 3.86k | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb0ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlRKT_E_clIA6_cEEDaSS_ Line | Count | Source | 73 | 1.76k | const auto put = [&](const auto& x) { | 74 | | if constexpr (Opts.comments) { | 75 | | if (line_comment_open) [[unlikely]] { | 76 | | if (not new_line()) [[unlikely]] { | 77 | | return false; | 78 | | } | 79 | | } | 80 | | } | 81 | 1.76k | return emit_bytes<true>(ctx, b, ix, x); | 82 | 1.76k | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlRKT_E_clINS8_17basic_string_viewIcSB_EEEEDaSS_ Line | Count | Source | 73 | 122k | const auto put = [&](const auto& x) { | 74 | 122k | if constexpr (Opts.comments) { | 75 | 122k | if (line_comment_open) [[unlikely]] { | 76 | 1.89k | if (not new_line()) [[unlikely]] { | 77 | 0 | return false; | 78 | 0 | } | 79 | 1.89k | } | 80 | 122k | } | 81 | 122k | return emit_bytes<true>(ctx, b, ix, x); | 82 | 122k | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlRKT_E_clIcEEDaSS_ Line | Count | Source | 73 | 38.3M | const auto put = [&](const auto& x) { | 74 | 38.3M | if constexpr (Opts.comments) { | 75 | 38.3M | if (line_comment_open) [[unlikely]] { | 76 | 1.79k | if (not new_line()) [[unlikely]] { | 77 | 0 | return false; | 78 | 0 | } | 79 | 1.79k | } | 80 | 38.3M | } | 81 | 38.3M | return emit_bytes<true>(ctx, b, ix, x); | 82 | 38.3M | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlRKT_E_clIA5_cEEDaSS_ Line | Count | Source | 73 | 5.84k | const auto put = [&](const auto& x) { | 74 | 5.84k | if constexpr (Opts.comments) { | 75 | 5.84k | if (line_comment_open) [[unlikely]] { | 76 | 3.69k | if (not new_line()) [[unlikely]] { | 77 | 0 | return false; | 78 | 0 | } | 79 | 3.69k | } | 80 | 5.84k | } | 81 | 5.84k | return emit_bytes<true>(ctx, b, ix, x); | 82 | 5.84k | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlRKT_E_clIA6_cEEDaSS_ Line | Count | Source | 73 | 1.22k | const auto put = [&](const auto& x) { | 74 | 1.22k | if constexpr (Opts.comments) { | 75 | 1.22k | if (line_comment_open) [[unlikely]] { | 76 | 269 | if (not new_line()) [[unlikely]] { | 77 | 0 | return false; | 78 | 0 | } | 79 | 269 | } | 80 | 1.22k | } | 81 | 1.22k | return emit_bytes<true>(ctx, b, ix, x); | 82 | 1.22k | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb1ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlRKT_E_clINS8_17basic_string_viewIcSB_EEEEDaSS_ Line | Count | Source | 73 | 122k | const auto put = [&](const auto& x) { | 74 | 122k | if constexpr (Opts.comments) { | 75 | 122k | if (line_comment_open) [[unlikely]] { | 76 | 1.89k | if (not new_line()) [[unlikely]] { | 77 | 0 | return false; | 78 | 0 | } | 79 | 1.89k | } | 80 | 122k | } | 81 | 122k | return emit_bytes<true>(ctx, b, ix, x); | 82 | 122k | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb1ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlRKT_E_clIcEEDaSS_ Line | Count | Source | 73 | 38.3M | const auto put = [&](const auto& x) { | 74 | 38.3M | if constexpr (Opts.comments) { | 75 | 38.3M | if (line_comment_open) [[unlikely]] { | 76 | 1.79k | if (not new_line()) [[unlikely]] { | 77 | 0 | return false; | 78 | 0 | } | 79 | 1.79k | } | 80 | 38.3M | } | 81 | 38.3M | return emit_bytes<true>(ctx, b, ix, x); | 82 | 38.3M | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb1ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlRKT_E_clIA5_cEEDaSS_ Line | Count | Source | 73 | 5.84k | const auto put = [&](const auto& x) { | 74 | 5.84k | if constexpr (Opts.comments) { | 75 | 5.84k | if (line_comment_open) [[unlikely]] { | 76 | 3.69k | if (not new_line()) [[unlikely]] { | 77 | 0 | return false; | 78 | 0 | } | 79 | 3.69k | } | 80 | 5.84k | } | 81 | 5.84k | return emit_bytes<true>(ctx, b, ix, x); | 82 | 5.84k | }; |
_ZZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb1ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ENKUlRKT_E_clIA6_cEEDaSS_ Line | Count | Source | 73 | 1.22k | const auto put = [&](const auto& x) { | 74 | 1.22k | if constexpr (Opts.comments) { | 75 | 1.22k | if (line_comment_open) [[unlikely]] { | 76 | 269 | if (not new_line()) [[unlikely]] { | 77 | 0 | return false; | 78 | 0 | } | 79 | 269 | } | 80 | 1.22k | } | 81 | 1.22k | return emit_bytes<true>(ctx, b, ix, x); | 82 | 1.22k | }; |
|
83 | | |
84 | 121M | while (it < end) { |
85 | 121M | switch (json_types[uint8_t(*it)]) { |
86 | 241k | case String: { |
87 | 241k | const auto value = read_json_string(ctx, it, end); |
88 | 241k | if (bool(ctx.error)) [[unlikely]] { |
89 | 246 | return; |
90 | 246 | } |
91 | | // non-empty: the scanner reports an empty view as an error |
92 | 240k | if (not put(value)) [[unlikely]] { |
93 | 0 | return; |
94 | 0 | } |
95 | 240k | break; |
96 | 240k | } |
97 | 118M | case Comma: { |
98 | 118M | if (not put(',')) [[unlikely]] { |
99 | 0 | return; |
100 | 0 | } |
101 | 118M | ++it; |
102 | 118M | if constexpr (check_new_lines_in_arrays(Opts)) { |
103 | 118M | if (not new_line()) [[unlikely]] { |
104 | 0 | return; |
105 | 0 | } |
106 | | } |
107 | | else { |
108 | | if (state[indent] == Object_Start) { |
109 | | if (not new_line()) [[unlikely]] { |
110 | | return; |
111 | | } |
112 | | } |
113 | | else if (not put(use_tabs ? '\t' : ' ')) [[unlikely]] { |
114 | | return; |
115 | | } |
116 | | } |
117 | 118M | break; |
118 | 118M | } |
119 | 118M | case Number: { |
120 | 84.4k | const auto value = read_json_number<Opts.null_terminated>(it, end); |
121 | | // non-empty: a Number match is one valid character |
122 | 84.4k | if (not put(value)) [[unlikely]] { |
123 | 0 | return; |
124 | 0 | } |
125 | 84.4k | break; |
126 | 84.4k | } |
127 | 84.4k | case Colon: { |
128 | 51.8k | static constexpr sv colon = use_tabs ? sv{":\t"} : sv{": "}; |
129 | 51.8k | if (not put(colon)) [[unlikely]] { |
130 | 0 | return; |
131 | 0 | } |
132 | 51.8k | ++it; |
133 | 51.8k | break; |
134 | 51.8k | } |
135 | 1.19M | case Array_Start: { |
136 | 1.19M | if (not put('[')) [[unlikely]] { |
137 | 0 | return; |
138 | 0 | } |
139 | 1.19M | ++it; |
140 | 1.19M | if (not open_level(Array_Start)) [[unlikely]] { |
141 | 64 | return; |
142 | 64 | } |
143 | 1.19M | if constexpr (check_new_lines_in_arrays(Opts)) { |
144 | 1.19M | if constexpr (not Opts.null_terminated) { |
145 | 894k | if (it != end && *it != ']') { |
146 | 891k | if (not new_line()) [[unlikely]] { |
147 | 0 | return; |
148 | 0 | } |
149 | 891k | } |
150 | | } |
151 | 300k | else { |
152 | 300k | if (*it != ']') { |
153 | 299k | if (not new_line()) [[unlikely]] { |
154 | 0 | return; |
155 | 0 | } |
156 | 299k | } |
157 | 300k | } |
158 | 1.19M | } |
159 | 1.19M | break; |
160 | 1.19M | } |
161 | 1.19M | case Array_End: { |
162 | 103k | --indent; |
163 | 103k | if (indent < 0) { |
164 | 4 | ctx.error = error_code::syntax_error; |
165 | 4 | return; |
166 | 4 | } |
167 | | // This break is the one the bracket wanted anyway, and it closes an open line |
168 | | // comment on its way, leaving the put below nothing to close. |
169 | 103k | if constexpr (check_new_lines_in_arrays(Opts)) { |
170 | 103k | if (it[-1] != '[') { |
171 | 99.3k | if (not new_line()) [[unlikely]] { |
172 | 0 | return; |
173 | 0 | } |
174 | 99.3k | } |
175 | 103k | } |
176 | 103k | if (not put(']')) [[unlikely]] { |
177 | 0 | return; |
178 | 0 | } |
179 | 103k | ++it; |
180 | 103k | break; |
181 | 103k | } |
182 | 5.81k | case Null: { |
183 | | // The type table matched on the first byte alone; see match_literal for why writing |
184 | | // the literal from the writer's own spelling needs the rest of it checked. |
185 | 5.81k | if (not match_literal<"null">(ctx, it, end)) [[unlikely]] { |
186 | 402 | return; |
187 | 402 | } |
188 | 5.41k | if (not put("null")) [[unlikely]] { |
189 | 0 | return; |
190 | 0 | } |
191 | 5.41k | break; |
192 | 5.41k | } |
193 | 15.3k | case Bool: { |
194 | 15.3k | if (*it == 't') { |
195 | 10.5k | if (not match_literal<"true">(ctx, it, end)) [[unlikely]] { |
196 | 428 | return; |
197 | 428 | } |
198 | 10.1k | if (not put("true")) [[unlikely]] { |
199 | 0 | return; |
200 | 0 | } |
201 | 10.1k | break; |
202 | 10.1k | } |
203 | 4.76k | else { |
204 | 4.76k | if (not match_literal<"false">(ctx, it, end)) [[unlikely]] { |
205 | 554 | return; |
206 | 554 | } |
207 | 4.21k | if (not put("false")) [[unlikely]] { |
208 | 0 | return; |
209 | 0 | } |
210 | 4.21k | break; |
211 | 4.21k | } |
212 | 15.3k | } |
213 | 448k | case Object_Start: { |
214 | 448k | if (not put('{')) [[unlikely]] { |
215 | 0 | return; |
216 | 0 | } |
217 | 448k | ++it; |
218 | 448k | if (not open_level(Object_Start)) [[unlikely]] { |
219 | 74 | return; |
220 | 74 | } |
221 | 448k | if constexpr (not Opts.null_terminated) { |
222 | 333k | if (it != end && *it != '}') { |
223 | 325k | if (not new_line()) [[unlikely]] { |
224 | 0 | return; |
225 | 0 | } |
226 | 325k | } |
227 | | } |
228 | 114k | else { |
229 | 114k | if (*it != '}') { |
230 | 112k | if (not new_line()) [[unlikely]] { |
231 | 0 | return; |
232 | 0 | } |
233 | 112k | } |
234 | 114k | } |
235 | 448k | break; |
236 | 448k | } |
237 | 1.13M | case Object_End: { |
238 | 1.13M | --indent; |
239 | 1.13M | if (indent < 0) { |
240 | 4 | ctx.error = error_code::syntax_error; |
241 | 4 | return; |
242 | 4 | } |
243 | | // As in Array_End, this break doubles as the close of an open line comment. |
244 | 1.13M | if (it[-1] != '{') { |
245 | 1.12M | if (not new_line()) [[unlikely]] { |
246 | 0 | return; |
247 | 0 | } |
248 | 1.12M | } |
249 | 1.13M | if (not put('}')) [[unlikely]] { |
250 | 0 | return; |
251 | 0 | } |
252 | 1.13M | ++it; |
253 | 1.13M | break; |
254 | 1.13M | } |
255 | 18.1k | case Comment: { |
256 | 18.1k | if constexpr (Opts.comments) { |
257 | 17.2k | const auto comment = read_jsonc_comment(ctx, it, end); |
258 | 17.2k | if (bool(ctx.error)) [[unlikely]] { |
259 | 322 | return; |
260 | 322 | } |
261 | | // non-empty: an empty view comes with an error |
262 | 16.9k | if (not put(comment.text)) [[unlikely]] { |
263 | 0 | return; |
264 | 0 | } |
265 | 16.9k | line_comment_open = comment.line; |
266 | 16.9k | break; |
267 | | } |
268 | 890 | else { |
269 | | // A '/' opens a comment, and plain JSON has none. Skipping it as though it were |
270 | | // whitespace silently rewrote the document: "{\"a\":1} // tail" came out as |
271 | | // "{...}true", the 't' of "tail" taken for a literal. minify_json has always |
272 | | // reported this, and the two had no business disagreeing. |
273 | 890 | ctx.error = error_code::syntax_error; |
274 | 890 | return; |
275 | 890 | } |
276 | 18.1k | } |
277 | 41.0k | case Whitespace: { |
278 | | // Skip whitespace characters that are part of raw JSON content |
279 | 41.0k | ++it; |
280 | 41.0k | break; |
281 | 18.1k | } |
282 | 2.08k | [[unlikely]] default: { |
283 | 2.08k | ctx.error = error_code::syntax_error; |
284 | 2.08k | return; |
285 | 0 | } |
286 | 121M | } |
287 | 121M | } |
288 | 8.01k | } _ZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb0ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ Line | Count | Source | 17 | 4.00k | { | 18 | 4.00k | constexpr bool use_tabs = check_indentation_char(Opts) == '\t'; | 19 | 4.00k | constexpr auto indent_width = check_indentation_width(Opts); | 20 | | | 21 | 4.00k | using enum json_type; | 22 | | | 23 | | // Indexed by nesting level, with level 0 the top. Sized for the depth the readers accept (see | 24 | | // enter_depth), so that every document glz::validate_jsonc passes can be prettified. | 25 | 4.00k | std::array<json_type, max_recursive_depth_limit + 1> state{}; | 26 | 4.00k | int64_t indent{}; | 27 | | | 28 | | // Enters one level of nesting, failing past the readers' limit. | 29 | 4.00k | const auto open_level = [&](const json_type type) { | 30 | 4.00k | if (size_t(indent) >= max_recursive_depth_limit) [[unlikely]] { | 31 | 4.00k | ctx.error = error_code::exceeded_max_recursive_depth; | 32 | 4.00k | return false; | 33 | 4.00k | } | 34 | 4.00k | state[++indent] = type; | 35 | 4.00k | return true; | 36 | 4.00k | }; | 37 | | | 38 | | // Set while the output cursor sits on a line that a `//` comment has commented out. Only | 39 | | // the comment-enabled path can set it, so the plain JSON writer keeps the code it had. | 40 | 4.00k | [[maybe_unused]] bool line_comment_open = false; | 41 | | | 42 | | // Every line break the writer emits goes through this, because a break is also what ends a | 43 | | // line comment: doing it in one place is what keeps the two from doubling up. The newline | 44 | | // and the indentation behind it are also what make the output unbounded -- there is no | 45 | | // bound on prettified output that a single reservation up front could use -- so this is | 46 | | // the write a bounded destination is likeliest to run out on. | 47 | 4.00k | const auto new_line = [&] { | 48 | 4.00k | if constexpr (has_bounded_capacity<decltype(b)>) { | 49 | 4.00k | const size_t n = use_tabs ? size_t(indent) : size_t(indent) * indent_width; | 50 | 4.00k | if (not ensure_space(ctx, b, ix + 1 + n)) [[unlikely]] { | 51 | 4.00k | return false; | 52 | 4.00k | } | 53 | 4.00k | } | 54 | 4.00k | append_new_line<use_tabs, indent_width>(b, ix, indent); | 55 | 4.00k | if constexpr (Opts.comments) { | 56 | 4.00k | line_comment_open = false; | 57 | 4.00k | } | 58 | 4.00k | return true; | 59 | 4.00k | }; | 60 | | | 61 | | // Every write below goes through this, which is what fixes their order in one place: an | 62 | | // open line comment is closed first, because anything written into one is commented out, | 63 | | // and emit_bytes then takes the reservation from the argument rather than from a byte | 64 | | // count spelled out beside the write. | 65 | | // | 66 | | // Deferring that break to the next write rather than emitting it with the comment is what | 67 | | // keeps a comment at the end of the input from leaving a trailing newline behind, and lets | 68 | | // a break the writer was going to emit anyway serve as this one. | 69 | | // | 70 | | // A break that fails is reported rather than swallowed: on a bounded destination that ran | 71 | | // out, carrying on would leave `line_comment_open` set and put this write *inside* the | 72 | | // comment, which is not a truncated document but a wrong one. | 73 | 4.00k | const auto put = [&](const auto& x) { | 74 | 4.00k | if constexpr (Opts.comments) { | 75 | 4.00k | if (line_comment_open) [[unlikely]] { | 76 | 4.00k | if (not new_line()) [[unlikely]] { | 77 | 4.00k | return false; | 78 | 4.00k | } | 79 | 4.00k | } | 80 | 4.00k | } | 81 | 4.00k | return emit_bytes<true>(ctx, b, ix, x); | 82 | 4.00k | }; | 83 | | | 84 | 44.8M | while (it < end) { | 85 | 44.7M | switch (json_types[uint8_t(*it)]) { | 86 | 96.7k | case String: { | 87 | 96.7k | const auto value = read_json_string(ctx, it, end); | 88 | 96.7k | if (bool(ctx.error)) [[unlikely]] { | 89 | 118 | return; | 90 | 118 | } | 91 | | // non-empty: the scanner reports an empty view as an error | 92 | 96.5k | if (not put(value)) [[unlikely]] { | 93 | 0 | return; | 94 | 0 | } | 95 | 96.5k | break; | 96 | 96.5k | } | 97 | 43.2M | case Comma: { | 98 | 43.2M | if (not put(',')) [[unlikely]] { | 99 | 0 | return; | 100 | 0 | } | 101 | 43.2M | ++it; | 102 | 43.2M | if constexpr (check_new_lines_in_arrays(Opts)) { | 103 | 43.2M | if (not new_line()) [[unlikely]] { | 104 | 0 | return; | 105 | 0 | } | 106 | | } | 107 | | else { | 108 | | if (state[indent] == Object_Start) { | 109 | | if (not new_line()) [[unlikely]] { | 110 | | return; | 111 | | } | 112 | | } | 113 | | else if (not put(use_tabs ? '\t' : ' ')) [[unlikely]] { | 114 | | return; | 115 | | } | 116 | | } | 117 | 43.2M | break; | 118 | 43.2M | } | 119 | 43.2M | case Number: { | 120 | 31.2k | const auto value = read_json_number<Opts.null_terminated>(it, end); | 121 | | // non-empty: a Number match is one valid character | 122 | 31.2k | if (not put(value)) [[unlikely]] { | 123 | 0 | return; | 124 | 0 | } | 125 | 31.2k | break; | 126 | 31.2k | } | 127 | 31.2k | case Colon: { | 128 | 21.6k | static constexpr sv colon = use_tabs ? sv{":\t"} : sv{": "}; | 129 | 21.6k | if (not put(colon)) [[unlikely]] { | 130 | 0 | return; | 131 | 0 | } | 132 | 21.6k | ++it; | 133 | 21.6k | break; | 134 | 21.6k | } | 135 | 593k | case Array_Start: { | 136 | 593k | if (not put('[')) [[unlikely]] { | 137 | 0 | return; | 138 | 0 | } | 139 | 593k | ++it; | 140 | 593k | if (not open_level(Array_Start)) [[unlikely]] { | 141 | 28 | return; | 142 | 28 | } | 143 | 593k | if constexpr (check_new_lines_in_arrays(Opts)) { | 144 | 593k | if constexpr (not Opts.null_terminated) { | 145 | 593k | if (it != end && *it != ']') { | 146 | 591k | if (not new_line()) [[unlikely]] { | 147 | 0 | return; | 148 | 0 | } | 149 | 591k | } | 150 | | } | 151 | | else { | 152 | | if (*it != ']') { | 153 | | if (not new_line()) [[unlikely]] { | 154 | | return; | 155 | | } | 156 | | } | 157 | | } | 158 | 593k | } | 159 | 593k | break; | 160 | 593k | } | 161 | 593k | case Array_End: { | 162 | 49.5k | --indent; | 163 | 49.5k | if (indent < 0) { | 164 | 2 | ctx.error = error_code::syntax_error; | 165 | 2 | return; | 166 | 2 | } | 167 | | // This break is the one the bracket wanted anyway, and it closes an open line | 168 | | // comment on its way, leaving the put below nothing to close. | 169 | 49.5k | if constexpr (check_new_lines_in_arrays(Opts)) { | 170 | 49.5k | if (it[-1] != '[') { | 171 | 47.6k | if (not new_line()) [[unlikely]] { | 172 | 0 | return; | 173 | 0 | } | 174 | 47.6k | } | 175 | 49.5k | } | 176 | 49.5k | if (not put(']')) [[unlikely]] { | 177 | 0 | return; | 178 | 0 | } | 179 | 49.5k | ++it; | 180 | 49.5k | break; | 181 | 49.5k | } | 182 | 2.49k | case Null: { | 183 | | // The type table matched on the first byte alone; see match_literal for why writing | 184 | | // the literal from the writer's own spelling needs the rest of it checked. | 185 | 2.49k | if (not match_literal<"null">(ctx, it, end)) [[unlikely]] { | 186 | 198 | return; | 187 | 198 | } | 188 | 2.30k | if (not put("null")) [[unlikely]] { | 189 | 0 | return; | 190 | 0 | } | 191 | 2.30k | break; | 192 | 2.30k | } | 193 | 3.81k | case Bool: { | 194 | 3.81k | if (*it == 't') { | 195 | 1.77k | if (not match_literal<"true">(ctx, it, end)) [[unlikely]] { | 196 | 210 | return; | 197 | 210 | } | 198 | 1.56k | if (not put("true")) [[unlikely]] { | 199 | 0 | return; | 200 | 0 | } | 201 | 1.56k | break; | 202 | 1.56k | } | 203 | 2.03k | else { | 204 | 2.03k | if (not match_literal<"false">(ctx, it, end)) [[unlikely]] { | 205 | 272 | return; | 206 | 272 | } | 207 | 1.76k | if (not put("false")) [[unlikely]] { | 208 | 0 | return; | 209 | 0 | } | 210 | 1.76k | break; | 211 | 1.76k | } | 212 | 3.81k | } | 213 | 218k | case Object_Start: { | 214 | 218k | if (not put('{')) [[unlikely]] { | 215 | 0 | return; | 216 | 0 | } | 217 | 218k | ++it; | 218 | 218k | if (not open_level(Object_Start)) [[unlikely]] { | 219 | 30 | return; | 220 | 30 | } | 221 | 218k | if constexpr (not Opts.null_terminated) { | 222 | 218k | if (it != end && *it != '}') { | 223 | 213k | if (not new_line()) [[unlikely]] { | 224 | 0 | return; | 225 | 0 | } | 226 | 213k | } | 227 | | } | 228 | | else { | 229 | | if (*it != '}') { | 230 | | if (not new_line()) [[unlikely]] { | 231 | | return; | 232 | | } | 233 | | } | 234 | | } | 235 | 218k | break; | 236 | 218k | } | 237 | 566k | case Object_End: { | 238 | 566k | --indent; | 239 | 566k | if (indent < 0) { | 240 | 2 | ctx.error = error_code::syntax_error; | 241 | 2 | return; | 242 | 2 | } | 243 | | // As in Array_End, this break doubles as the close of an open line comment. | 244 | 566k | if (it[-1] != '{') { | 245 | 561k | if (not new_line()) [[unlikely]] { | 246 | 0 | return; | 247 | 0 | } | 248 | 561k | } | 249 | 566k | if (not put('}')) [[unlikely]] { | 250 | 0 | return; | 251 | 0 | } | 252 | 566k | ++it; | 253 | 566k | break; | 254 | 566k | } | 255 | 890 | case Comment: { | 256 | | if constexpr (Opts.comments) { | 257 | | const auto comment = read_jsonc_comment(ctx, it, end); | 258 | | if (bool(ctx.error)) [[unlikely]] { | 259 | | return; | 260 | | } | 261 | | // non-empty: an empty view comes with an error | 262 | | if (not put(comment.text)) [[unlikely]] { | 263 | | return; | 264 | | } | 265 | | line_comment_open = comment.line; | 266 | | break; | 267 | | } | 268 | 890 | else { | 269 | | // A '/' opens a comment, and plain JSON has none. Skipping it as though it were | 270 | | // whitespace silently rewrote the document: "{\"a\":1} // tail" came out as | 271 | | // "{...}true", the 't' of "tail" taken for a literal. minify_json has always | 272 | | // reported this, and the two had no business disagreeing. | 273 | 890 | ctx.error = error_code::syntax_error; | 274 | 890 | return; | 275 | 890 | } | 276 | 890 | } | 277 | 3.29k | case Whitespace: { | 278 | | // Skip whitespace characters that are part of raw JSON content | 279 | 3.29k | ++it; | 280 | 3.29k | break; | 281 | 890 | } | 282 | 1.40k | [[unlikely]] default: { | 283 | 1.40k | ctx.error = error_code::syntax_error; | 284 | 1.40k | return; | 285 | 0 | } | 286 | 44.7M | } | 287 | 44.7M | } | 288 | 4.00k | } |
_ZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb0ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ Line | Count | Source | 17 | 2.00k | { | 18 | 2.00k | constexpr bool use_tabs = check_indentation_char(Opts) == '\t'; | 19 | 2.00k | constexpr auto indent_width = check_indentation_width(Opts); | 20 | | | 21 | 2.00k | using enum json_type; | 22 | | | 23 | | // Indexed by nesting level, with level 0 the top. Sized for the depth the readers accept (see | 24 | | // enter_depth), so that every document glz::validate_jsonc passes can be prettified. | 25 | 2.00k | std::array<json_type, max_recursive_depth_limit + 1> state{}; | 26 | 2.00k | int64_t indent{}; | 27 | | | 28 | | // Enters one level of nesting, failing past the readers' limit. | 29 | 2.00k | const auto open_level = [&](const json_type type) { | 30 | 2.00k | if (size_t(indent) >= max_recursive_depth_limit) [[unlikely]] { | 31 | 2.00k | ctx.error = error_code::exceeded_max_recursive_depth; | 32 | 2.00k | return false; | 33 | 2.00k | } | 34 | 2.00k | state[++indent] = type; | 35 | 2.00k | return true; | 36 | 2.00k | }; | 37 | | | 38 | | // Set while the output cursor sits on a line that a `//` comment has commented out. Only | 39 | | // the comment-enabled path can set it, so the plain JSON writer keeps the code it had. | 40 | 2.00k | [[maybe_unused]] bool line_comment_open = false; | 41 | | | 42 | | // Every line break the writer emits goes through this, because a break is also what ends a | 43 | | // line comment: doing it in one place is what keeps the two from doubling up. The newline | 44 | | // and the indentation behind it are also what make the output unbounded -- there is no | 45 | | // bound on prettified output that a single reservation up front could use -- so this is | 46 | | // the write a bounded destination is likeliest to run out on. | 47 | 2.00k | const auto new_line = [&] { | 48 | 2.00k | if constexpr (has_bounded_capacity<decltype(b)>) { | 49 | 2.00k | const size_t n = use_tabs ? size_t(indent) : size_t(indent) * indent_width; | 50 | 2.00k | if (not ensure_space(ctx, b, ix + 1 + n)) [[unlikely]] { | 51 | 2.00k | return false; | 52 | 2.00k | } | 53 | 2.00k | } | 54 | 2.00k | append_new_line<use_tabs, indent_width>(b, ix, indent); | 55 | 2.00k | if constexpr (Opts.comments) { | 56 | 2.00k | line_comment_open = false; | 57 | 2.00k | } | 58 | 2.00k | return true; | 59 | 2.00k | }; | 60 | | | 61 | | // Every write below goes through this, which is what fixes their order in one place: an | 62 | | // open line comment is closed first, because anything written into one is commented out, | 63 | | // and emit_bytes then takes the reservation from the argument rather than from a byte | 64 | | // count spelled out beside the write. | 65 | | // | 66 | | // Deferring that break to the next write rather than emitting it with the comment is what | 67 | | // keeps a comment at the end of the input from leaving a trailing newline behind, and lets | 68 | | // a break the writer was going to emit anyway serve as this one. | 69 | | // | 70 | | // A break that fails is reported rather than swallowed: on a bounded destination that ran | 71 | | // out, carrying on would leave `line_comment_open` set and put this write *inside* the | 72 | | // comment, which is not a truncated document but a wrong one. | 73 | 2.00k | const auto put = [&](const auto& x) { | 74 | 2.00k | if constexpr (Opts.comments) { | 75 | 2.00k | if (line_comment_open) [[unlikely]] { | 76 | 2.00k | if (not new_line()) [[unlikely]] { | 77 | 2.00k | return false; | 78 | 2.00k | } | 79 | 2.00k | } | 80 | 2.00k | } | 81 | 2.00k | return emit_bytes<true>(ctx, b, ix, x); | 82 | 2.00k | }; | 83 | | | 84 | 38.5M | while (it < end) { | 85 | 38.5M | switch (json_types[uint8_t(*it)]) { | 86 | 72.1k | case String: { | 87 | 72.1k | const auto value = read_json_string(ctx, it, end); | 88 | 72.1k | if (bool(ctx.error)) [[unlikely]] { | 89 | 64 | return; | 90 | 64 | } | 91 | | // non-empty: the scanner reports an empty view as an error | 92 | 72.1k | if (not put(value)) [[unlikely]] { | 93 | 0 | return; | 94 | 0 | } | 95 | 72.1k | break; | 96 | 72.1k | } | 97 | 37.6M | case Comma: { | 98 | 37.6M | if (not put(',')) [[unlikely]] { | 99 | 0 | return; | 100 | 0 | } | 101 | 37.6M | ++it; | 102 | 37.6M | if constexpr (check_new_lines_in_arrays(Opts)) { | 103 | 37.6M | if (not new_line()) [[unlikely]] { | 104 | 0 | return; | 105 | 0 | } | 106 | | } | 107 | | else { | 108 | | if (state[indent] == Object_Start) { | 109 | | if (not new_line()) [[unlikely]] { | 110 | | return; | 111 | | } | 112 | | } | 113 | | else if (not put(use_tabs ? '\t' : ' ')) [[unlikely]] { | 114 | | return; | 115 | | } | 116 | | } | 117 | 37.6M | break; | 118 | 37.6M | } | 119 | 37.6M | case Number: { | 120 | 26.5k | const auto value = read_json_number<Opts.null_terminated>(it, end); | 121 | | // non-empty: a Number match is one valid character | 122 | 26.5k | if (not put(value)) [[unlikely]] { | 123 | 0 | return; | 124 | 0 | } | 125 | 26.5k | break; | 126 | 26.5k | } | 127 | 26.5k | case Colon: { | 128 | 15.0k | static constexpr sv colon = use_tabs ? sv{":\t"} : sv{": "}; | 129 | 15.0k | if (not put(colon)) [[unlikely]] { | 130 | 0 | return; | 131 | 0 | } | 132 | 15.0k | ++it; | 133 | 15.0k | break; | 134 | 15.0k | } | 135 | 300k | case Array_Start: { | 136 | 300k | if (not put('[')) [[unlikely]] { | 137 | 0 | return; | 138 | 0 | } | 139 | 300k | ++it; | 140 | 300k | if (not open_level(Array_Start)) [[unlikely]] { | 141 | 18 | return; | 142 | 18 | } | 143 | 300k | if constexpr (check_new_lines_in_arrays(Opts)) { | 144 | 300k | if constexpr (not Opts.null_terminated) { | 145 | 300k | if (it != end && *it != ']') { | 146 | 299k | if (not new_line()) [[unlikely]] { | 147 | 0 | return; | 148 | 0 | } | 149 | 299k | } | 150 | | } | 151 | | else { | 152 | | if (*it != ']') { | 153 | | if (not new_line()) [[unlikely]] { | 154 | | return; | 155 | | } | 156 | | } | 157 | | } | 158 | 300k | } | 159 | 300k | break; | 160 | 300k | } | 161 | 300k | case Array_End: { | 162 | 26.8k | --indent; | 163 | 26.8k | if (indent < 0) { | 164 | 1 | ctx.error = error_code::syntax_error; | 165 | 1 | return; | 166 | 1 | } | 167 | | // This break is the one the bracket wanted anyway, and it closes an open line | 168 | | // comment on its way, leaving the put below nothing to close. | 169 | 26.8k | if constexpr (check_new_lines_in_arrays(Opts)) { | 170 | 26.8k | if (it[-1] != '[') { | 171 | 25.8k | if (not new_line()) [[unlikely]] { | 172 | 0 | return; | 173 | 0 | } | 174 | 25.8k | } | 175 | 26.8k | } | 176 | 26.8k | if (not put(']')) [[unlikely]] { | 177 | 0 | return; | 178 | 0 | } | 179 | 26.8k | ++it; | 180 | 26.8k | break; | 181 | 26.8k | } | 182 | 1.65k | case Null: { | 183 | | // The type table matched on the first byte alone; see match_literal for why writing | 184 | | // the literal from the writer's own spelling needs the rest of it checked. | 185 | 1.65k | if (not match_literal<"null">(ctx, it, end)) [[unlikely]] { | 186 | 102 | return; | 187 | 102 | } | 188 | 1.55k | if (not put("null")) [[unlikely]] { | 189 | 0 | return; | 190 | 0 | } | 191 | 1.55k | break; | 192 | 1.55k | } | 193 | 5.76k | case Bool: { | 194 | 5.76k | if (*it == 't') { | 195 | 4.39k | if (not match_literal<"true">(ctx, it, end)) [[unlikely]] { | 196 | 109 | return; | 197 | 109 | } | 198 | 4.28k | if (not put("true")) [[unlikely]] { | 199 | 0 | return; | 200 | 0 | } | 201 | 4.28k | break; | 202 | 4.28k | } | 203 | 1.36k | else { | 204 | 1.36k | if (not match_literal<"false">(ctx, it, end)) [[unlikely]] { | 205 | 141 | return; | 206 | 141 | } | 207 | 1.22k | if (not put("false")) [[unlikely]] { | 208 | 0 | return; | 209 | 0 | } | 210 | 1.22k | break; | 211 | 1.22k | } | 212 | 5.76k | } | 213 | 114k | case Object_Start: { | 214 | 114k | if (not put('{')) [[unlikely]] { | 215 | 0 | return; | 216 | 0 | } | 217 | 114k | ++it; | 218 | 114k | if (not open_level(Object_Start)) [[unlikely]] { | 219 | 22 | return; | 220 | 22 | } | 221 | 114k | if constexpr (not Opts.null_terminated) { | 222 | 114k | if (it != end && *it != '}') { | 223 | 112k | if (not new_line()) [[unlikely]] { | 224 | 0 | return; | 225 | 0 | } | 226 | 112k | } | 227 | | } | 228 | | else { | 229 | | if (*it != '}') { | 230 | | if (not new_line()) [[unlikely]] { | 231 | | return; | 232 | | } | 233 | | } | 234 | | } | 235 | 114k | break; | 236 | 114k | } | 237 | 285k | case Object_End: { | 238 | 285k | --indent; | 239 | 285k | if (indent < 0) { | 240 | 1 | ctx.error = error_code::syntax_error; | 241 | 1 | return; | 242 | 1 | } | 243 | | // As in Array_End, this break doubles as the close of an open line comment. | 244 | 285k | if (it[-1] != '{') { | 245 | 282k | if (not new_line()) [[unlikely]] { | 246 | 0 | return; | 247 | 0 | } | 248 | 282k | } | 249 | 285k | if (not put('}')) [[unlikely]] { | 250 | 0 | return; | 251 | 0 | } | 252 | 285k | ++it; | 253 | 285k | break; | 254 | 285k | } | 255 | 8.61k | case Comment: { | 256 | 8.61k | if constexpr (Opts.comments) { | 257 | 8.61k | const auto comment = read_jsonc_comment(ctx, it, end); | 258 | 8.61k | if (bool(ctx.error)) [[unlikely]] { | 259 | 161 | return; | 260 | 161 | } | 261 | | // non-empty: an empty view comes with an error | 262 | 8.45k | if (not put(comment.text)) [[unlikely]] { | 263 | 0 | return; | 264 | 0 | } | 265 | 8.45k | line_comment_open = comment.line; | 266 | 8.45k | break; | 267 | | } | 268 | | else { | 269 | | // A '/' opens a comment, and plain JSON has none. Skipping it as though it were | 270 | | // whitespace silently rewrote the document: "{\"a\":1} // tail" came out as | 271 | | // "{...}true", the 't' of "tail" taken for a literal. minify_json has always | 272 | | // reported this, and the two had no business disagreeing. | 273 | | ctx.error = error_code::syntax_error; | 274 | | return; | 275 | | } | 276 | 8.61k | } | 277 | 18.8k | case Whitespace: { | 278 | | // Skip whitespace characters that are part of raw JSON content | 279 | 18.8k | ++it; | 280 | 18.8k | break; | 281 | 8.61k | } | 282 | 340 | [[unlikely]] default: { | 283 | 340 | ctx.error = error_code::syntax_error; | 284 | 340 | return; | 285 | 0 | } | 286 | 38.5M | } | 287 | 38.5M | } | 288 | 2.00k | } |
_ZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb1ELb1ELb1ELb1EEETkNS_10is_contextERNS_7contextERPKcS7_RNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEmEEvOT0_OT1_OT2_OT3_RT4_ Line | Count | Source | 17 | 2.00k | { | 18 | 2.00k | constexpr bool use_tabs = check_indentation_char(Opts) == '\t'; | 19 | 2.00k | constexpr auto indent_width = check_indentation_width(Opts); | 20 | | | 21 | 2.00k | using enum json_type; | 22 | | | 23 | | // Indexed by nesting level, with level 0 the top. Sized for the depth the readers accept (see | 24 | | // enter_depth), so that every document glz::validate_jsonc passes can be prettified. | 25 | 2.00k | std::array<json_type, max_recursive_depth_limit + 1> state{}; | 26 | 2.00k | int64_t indent{}; | 27 | | | 28 | | // Enters one level of nesting, failing past the readers' limit. | 29 | 2.00k | const auto open_level = [&](const json_type type) { | 30 | 2.00k | if (size_t(indent) >= max_recursive_depth_limit) [[unlikely]] { | 31 | 2.00k | ctx.error = error_code::exceeded_max_recursive_depth; | 32 | 2.00k | return false; | 33 | 2.00k | } | 34 | 2.00k | state[++indent] = type; | 35 | 2.00k | return true; | 36 | 2.00k | }; | 37 | | | 38 | | // Set while the output cursor sits on a line that a `//` comment has commented out. Only | 39 | | // the comment-enabled path can set it, so the plain JSON writer keeps the code it had. | 40 | 2.00k | [[maybe_unused]] bool line_comment_open = false; | 41 | | | 42 | | // Every line break the writer emits goes through this, because a break is also what ends a | 43 | | // line comment: doing it in one place is what keeps the two from doubling up. The newline | 44 | | // and the indentation behind it are also what make the output unbounded -- there is no | 45 | | // bound on prettified output that a single reservation up front could use -- so this is | 46 | | // the write a bounded destination is likeliest to run out on. | 47 | 2.00k | const auto new_line = [&] { | 48 | 2.00k | if constexpr (has_bounded_capacity<decltype(b)>) { | 49 | 2.00k | const size_t n = use_tabs ? size_t(indent) : size_t(indent) * indent_width; | 50 | 2.00k | if (not ensure_space(ctx, b, ix + 1 + n)) [[unlikely]] { | 51 | 2.00k | return false; | 52 | 2.00k | } | 53 | 2.00k | } | 54 | 2.00k | append_new_line<use_tabs, indent_width>(b, ix, indent); | 55 | 2.00k | if constexpr (Opts.comments) { | 56 | 2.00k | line_comment_open = false; | 57 | 2.00k | } | 58 | 2.00k | return true; | 59 | 2.00k | }; | 60 | | | 61 | | // Every write below goes through this, which is what fixes their order in one place: an | 62 | | // open line comment is closed first, because anything written into one is commented out, | 63 | | // and emit_bytes then takes the reservation from the argument rather than from a byte | 64 | | // count spelled out beside the write. | 65 | | // | 66 | | // Deferring that break to the next write rather than emitting it with the comment is what | 67 | | // keeps a comment at the end of the input from leaving a trailing newline behind, and lets | 68 | | // a break the writer was going to emit anyway serve as this one. | 69 | | // | 70 | | // A break that fails is reported rather than swallowed: on a bounded destination that ran | 71 | | // out, carrying on would leave `line_comment_open` set and put this write *inside* the | 72 | | // comment, which is not a truncated document but a wrong one. | 73 | 2.00k | const auto put = [&](const auto& x) { | 74 | 2.00k | if constexpr (Opts.comments) { | 75 | 2.00k | if (line_comment_open) [[unlikely]] { | 76 | 2.00k | if (not new_line()) [[unlikely]] { | 77 | 2.00k | return false; | 78 | 2.00k | } | 79 | 2.00k | } | 80 | 2.00k | } | 81 | 2.00k | return emit_bytes<true>(ctx, b, ix, x); | 82 | 2.00k | }; | 83 | | | 84 | 38.5M | while (it < end) { | 85 | 38.5M | switch (json_types[uint8_t(*it)]) { | 86 | 72.1k | case String: { | 87 | 72.1k | const auto value = read_json_string(ctx, it, end); | 88 | 72.1k | if (bool(ctx.error)) [[unlikely]] { | 89 | 64 | return; | 90 | 64 | } | 91 | | // non-empty: the scanner reports an empty view as an error | 92 | 72.1k | if (not put(value)) [[unlikely]] { | 93 | 0 | return; | 94 | 0 | } | 95 | 72.1k | break; | 96 | 72.1k | } | 97 | 37.6M | case Comma: { | 98 | 37.6M | if (not put(',')) [[unlikely]] { | 99 | 0 | return; | 100 | 0 | } | 101 | 37.6M | ++it; | 102 | 37.6M | if constexpr (check_new_lines_in_arrays(Opts)) { | 103 | 37.6M | if (not new_line()) [[unlikely]] { | 104 | 0 | return; | 105 | 0 | } | 106 | | } | 107 | | else { | 108 | | if (state[indent] == Object_Start) { | 109 | | if (not new_line()) [[unlikely]] { | 110 | | return; | 111 | | } | 112 | | } | 113 | | else if (not put(use_tabs ? '\t' : ' ')) [[unlikely]] { | 114 | | return; | 115 | | } | 116 | | } | 117 | 37.6M | break; | 118 | 37.6M | } | 119 | 37.6M | case Number: { | 120 | 26.5k | const auto value = read_json_number<Opts.null_terminated>(it, end); | 121 | | // non-empty: a Number match is one valid character | 122 | 26.5k | if (not put(value)) [[unlikely]] { | 123 | 0 | return; | 124 | 0 | } | 125 | 26.5k | break; | 126 | 26.5k | } | 127 | 26.5k | case Colon: { | 128 | 15.0k | static constexpr sv colon = use_tabs ? sv{":\t"} : sv{": "}; | 129 | 15.0k | if (not put(colon)) [[unlikely]] { | 130 | 0 | return; | 131 | 0 | } | 132 | 15.0k | ++it; | 133 | 15.0k | break; | 134 | 15.0k | } | 135 | 300k | case Array_Start: { | 136 | 300k | if (not put('[')) [[unlikely]] { | 137 | 0 | return; | 138 | 0 | } | 139 | 300k | ++it; | 140 | 300k | if (not open_level(Array_Start)) [[unlikely]] { | 141 | 18 | return; | 142 | 18 | } | 143 | 300k | if constexpr (check_new_lines_in_arrays(Opts)) { | 144 | | if constexpr (not Opts.null_terminated) { | 145 | | if (it != end && *it != ']') { | 146 | | if (not new_line()) [[unlikely]] { | 147 | | return; | 148 | | } | 149 | | } | 150 | | } | 151 | 300k | else { | 152 | 300k | if (*it != ']') { | 153 | 299k | if (not new_line()) [[unlikely]] { | 154 | 0 | return; | 155 | 0 | } | 156 | 299k | } | 157 | 300k | } | 158 | 300k | } | 159 | 300k | break; | 160 | 300k | } | 161 | 300k | case Array_End: { | 162 | 26.8k | --indent; | 163 | 26.8k | if (indent < 0) { | 164 | 1 | ctx.error = error_code::syntax_error; | 165 | 1 | return; | 166 | 1 | } | 167 | | // This break is the one the bracket wanted anyway, and it closes an open line | 168 | | // comment on its way, leaving the put below nothing to close. | 169 | 26.8k | if constexpr (check_new_lines_in_arrays(Opts)) { | 170 | 26.8k | if (it[-1] != '[') { | 171 | 25.8k | if (not new_line()) [[unlikely]] { | 172 | 0 | return; | 173 | 0 | } | 174 | 25.8k | } | 175 | 26.8k | } | 176 | 26.8k | if (not put(']')) [[unlikely]] { | 177 | 0 | return; | 178 | 0 | } | 179 | 26.8k | ++it; | 180 | 26.8k | break; | 181 | 26.8k | } | 182 | 1.65k | case Null: { | 183 | | // The type table matched on the first byte alone; see match_literal for why writing | 184 | | // the literal from the writer's own spelling needs the rest of it checked. | 185 | 1.65k | if (not match_literal<"null">(ctx, it, end)) [[unlikely]] { | 186 | 102 | return; | 187 | 102 | } | 188 | 1.55k | if (not put("null")) [[unlikely]] { | 189 | 0 | return; | 190 | 0 | } | 191 | 1.55k | break; | 192 | 1.55k | } | 193 | 5.76k | case Bool: { | 194 | 5.76k | if (*it == 't') { | 195 | 4.39k | if (not match_literal<"true">(ctx, it, end)) [[unlikely]] { | 196 | 109 | return; | 197 | 109 | } | 198 | 4.28k | if (not put("true")) [[unlikely]] { | 199 | 0 | return; | 200 | 0 | } | 201 | 4.28k | break; | 202 | 4.28k | } | 203 | 1.36k | else { | 204 | 1.36k | if (not match_literal<"false">(ctx, it, end)) [[unlikely]] { | 205 | 141 | return; | 206 | 141 | } | 207 | 1.22k | if (not put("false")) [[unlikely]] { | 208 | 0 | return; | 209 | 0 | } | 210 | 1.22k | break; | 211 | 1.22k | } | 212 | 5.76k | } | 213 | 114k | case Object_Start: { | 214 | 114k | if (not put('{')) [[unlikely]] { | 215 | 0 | return; | 216 | 0 | } | 217 | 114k | ++it; | 218 | 114k | if (not open_level(Object_Start)) [[unlikely]] { | 219 | 22 | return; | 220 | 22 | } | 221 | | if constexpr (not Opts.null_terminated) { | 222 | | if (it != end && *it != '}') { | 223 | | if (not new_line()) [[unlikely]] { | 224 | | return; | 225 | | } | 226 | | } | 227 | | } | 228 | 114k | else { | 229 | 114k | if (*it != '}') { | 230 | 112k | if (not new_line()) [[unlikely]] { | 231 | 0 | return; | 232 | 0 | } | 233 | 112k | } | 234 | 114k | } | 235 | 114k | break; | 236 | 114k | } | 237 | 285k | case Object_End: { | 238 | 285k | --indent; | 239 | 285k | if (indent < 0) { | 240 | 1 | ctx.error = error_code::syntax_error; | 241 | 1 | return; | 242 | 1 | } | 243 | | // As in Array_End, this break doubles as the close of an open line comment. | 244 | 285k | if (it[-1] != '{') { | 245 | 282k | if (not new_line()) [[unlikely]] { | 246 | 0 | return; | 247 | 0 | } | 248 | 282k | } | 249 | 285k | if (not put('}')) [[unlikely]] { | 250 | 0 | return; | 251 | 0 | } | 252 | 285k | ++it; | 253 | 285k | break; | 254 | 285k | } | 255 | 8.61k | case Comment: { | 256 | 8.61k | if constexpr (Opts.comments) { | 257 | 8.61k | const auto comment = read_jsonc_comment(ctx, it, end); | 258 | 8.61k | if (bool(ctx.error)) [[unlikely]] { | 259 | 161 | return; | 260 | 161 | } | 261 | | // non-empty: an empty view comes with an error | 262 | 8.45k | if (not put(comment.text)) [[unlikely]] { | 263 | 0 | return; | 264 | 0 | } | 265 | 8.45k | line_comment_open = comment.line; | 266 | 8.45k | break; | 267 | | } | 268 | | else { | 269 | | // A '/' opens a comment, and plain JSON has none. Skipping it as though it were | 270 | | // whitespace silently rewrote the document: "{\"a\":1} // tail" came out as | 271 | | // "{...}true", the 't' of "tail" taken for a literal. minify_json has always | 272 | | // reported this, and the two had no business disagreeing. | 273 | | ctx.error = error_code::syntax_error; | 274 | | return; | 275 | | } | 276 | 8.61k | } | 277 | 18.8k | case Whitespace: { | 278 | | // Skip whitespace characters that are part of raw JSON content | 279 | 18.8k | ++it; | 280 | 18.8k | break; | 281 | 8.61k | } | 282 | 340 | [[unlikely]] default: { | 283 | 340 | ctx.error = error_code::syntax_error; | 284 | 340 | return; | 285 | 0 | } | 286 | 38.5M | } | 287 | 38.5M | } | 288 | 2.00k | } |
|
289 | | |
290 | | // Returns the number of bytes written, which is what a bounded output has no other way to |
291 | | // learn: it has no size to be shrunk to the result the way a resizable one does. |
292 | | template <auto Opts, contiguous In, output_buffer Out> |
293 | | requires(not byte_array_buffer<Out>) |
294 | | inline size_t prettify_json(is_context auto&& ctx, In&& in, Out&& out) |
295 | 8.01k | { |
296 | 8.01k | if constexpr (resizable<Out>) { |
297 | 8.01k | if (in.size() == 0) { |
298 | 0 | out.resize(0); // resize is what `resizable` promises; clear() is not |
299 | 0 | return 0; |
300 | 0 | } |
301 | 8.01k | out.resize(in.size() * 2); |
302 | 8.01k | } |
303 | 0 | size_t ix = 0; |
304 | 8.01k | auto [it, end] = read_iterators<Opts>(in); |
305 | 8.01k | if (bool(ctx.error)) [[unlikely]] { |
306 | 0 | return 0; |
307 | 0 | } |
308 | | |
309 | 8.01k | if constexpr (self_terminating<In>) { |
310 | 2.00k | prettify_json<opt_true<Opts, &opts::null_terminated>>(ctx, it, end, out, ix); |
311 | | } |
312 | 6.00k | else { |
313 | 6.00k | prettify_json<opt_false<Opts, &opts::null_terminated>>(ctx, it, end, out, ix); |
314 | 6.00k | } |
315 | | |
316 | 8.01k | if constexpr (resizable<Out>) { |
317 | 8.01k | out.resize(ix); |
318 | 8.01k | } |
319 | 8.01k | return ix; |
320 | 8.01k | } _ZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb1ELb0ELb1ELb1EEETkNS_10contiguousERKNSt3__16vectorIcNS3_9allocatorIcEEEETkNS_13output_bufferERNS3_12basic_stringIcNS3_11char_traitsIcEES6_EETkNS_10is_contextERNS_7contextEQnt17byte_array_bufferIT1_EEEmOT2_OT0_OSH_ Line | Count | Source | 295 | 4.00k | { | 296 | 4.00k | if constexpr (resizable<Out>) { | 297 | 4.00k | if (in.size() == 0) { | 298 | 0 | out.resize(0); // resize is what `resizable` promises; clear() is not | 299 | 0 | return 0; | 300 | 0 | } | 301 | 4.00k | out.resize(in.size() * 2); | 302 | 4.00k | } | 303 | 0 | size_t ix = 0; | 304 | 4.00k | auto [it, end] = read_iterators<Opts>(in); | 305 | 4.00k | if (bool(ctx.error)) [[unlikely]] { | 306 | 0 | return 0; | 307 | 0 | } | 308 | | | 309 | | if constexpr (self_terminating<In>) { | 310 | | prettify_json<opt_true<Opts, &opts::null_terminated>>(ctx, it, end, out, ix); | 311 | | } | 312 | 4.00k | else { | 313 | 4.00k | prettify_json<opt_false<Opts, &opts::null_terminated>>(ctx, it, end, out, ix); | 314 | 4.00k | } | 315 | | | 316 | 4.00k | if constexpr (resizable<Out>) { | 317 | 4.00k | out.resize(ix); | 318 | 4.00k | } | 319 | 4.00k | return ix; | 320 | 4.00k | } |
_ZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb1ELb1ELb1ELb1EEETkNS_10contiguousERKNSt3__16vectorIcNS3_9allocatorIcEEEETkNS_13output_bufferERNS3_12basic_stringIcNS3_11char_traitsIcEES6_EETkNS_10is_contextERNS_7contextEQnt17byte_array_bufferIT1_EEEmOT2_OT0_OSH_ Line | Count | Source | 295 | 2.00k | { | 296 | 2.00k | if constexpr (resizable<Out>) { | 297 | 2.00k | if (in.size() == 0) { | 298 | 0 | out.resize(0); // resize is what `resizable` promises; clear() is not | 299 | 0 | return 0; | 300 | 0 | } | 301 | 2.00k | out.resize(in.size() * 2); | 302 | 2.00k | } | 303 | 0 | size_t ix = 0; | 304 | 2.00k | auto [it, end] = read_iterators<Opts>(in); | 305 | 2.00k | if (bool(ctx.error)) [[unlikely]] { | 306 | 0 | return 0; | 307 | 0 | } | 308 | | | 309 | | if constexpr (self_terminating<In>) { | 310 | | prettify_json<opt_true<Opts, &opts::null_terminated>>(ctx, it, end, out, ix); | 311 | | } | 312 | 2.00k | else { | 313 | 2.00k | prettify_json<opt_false<Opts, &opts::null_terminated>>(ctx, it, end, out, ix); | 314 | 2.00k | } | 315 | | | 316 | 2.00k | if constexpr (resizable<Out>) { | 317 | 2.00k | out.resize(ix); | 318 | 2.00k | } | 319 | 2.00k | return ix; | 320 | 2.00k | } |
_ZN3glz6detail13prettify_jsonITnDaXtlNS_4optsELj10ELb1ELb1ELb1ELb1EEETkNS_10contiguousERKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEETkNS_13output_bufferERS9_TkNS_10is_contextERNS_7contextEQnt17byte_array_bufferIT1_EEEmOT2_OT0_OSF_ Line | Count | Source | 295 | 2.00k | { | 296 | 2.00k | if constexpr (resizable<Out>) { | 297 | 2.00k | if (in.size() == 0) { | 298 | 0 | out.resize(0); // resize is what `resizable` promises; clear() is not | 299 | 0 | return 0; | 300 | 0 | } | 301 | 2.00k | out.resize(in.size() * 2); | 302 | 2.00k | } | 303 | 0 | size_t ix = 0; | 304 | 2.00k | auto [it, end] = read_iterators<Opts>(in); | 305 | 2.00k | if (bool(ctx.error)) [[unlikely]] { | 306 | 0 | return 0; | 307 | 0 | } | 308 | | | 309 | 2.00k | if constexpr (self_terminating<In>) { | 310 | 2.00k | prettify_json<opt_true<Opts, &opts::null_terminated>>(ctx, it, end, out, ix); | 311 | | } | 312 | | else { | 313 | | prettify_json<opt_false<Opts, &opts::null_terminated>>(ctx, it, end, out, ix); | 314 | | } | 315 | | | 316 | 2.00k | if constexpr (resizable<Out>) { | 317 | 2.00k | out.resize(ix); | 318 | 2.00k | } | 319 | 2.00k | return ix; | 320 | 2.00k | } |
|
321 | | |
322 | | // A C array has no members to serialize through; a span over it keeps the extent as the capacity. |
323 | | template <auto Opts, contiguous In, byte_array_buffer Out> |
324 | | inline size_t prettify_json(is_context auto&& ctx, In&& in, Out& out) |
325 | | { |
326 | | auto view = std::span{out}; |
327 | | return prettify_json<Opts>(ctx, std::forward<In>(in), view); |
328 | | } |
329 | | } |
330 | | |
331 | | // The overloads that write into a caller's buffer report what went wrong, and how many bytes |
332 | | // they wrote, which is the only way a bounded output learns where its result ends. That count is |
333 | | // an offset into the output, not the input, so glz::format_error(ec, source) -- which reads it |
334 | | // as a position in the buffer it is handed -- does not point at the offending byte here. Not |
335 | | // [[nodiscard]]: |
336 | | // prettifying auto-generated JSON does not fail, so the callers that have always ignored the |
337 | | // outcome are right to, and warning at all of them would say nothing useful. The overloads that |
338 | | // return the text have nowhere to put an error, so they come back empty rather than hand over |
339 | | // the prefix the scan managed before it stopped -- routinely an unterminated string or an |
340 | | // unclosed brace, which is not a document but a silent truncation of one. |
341 | | // |
342 | | // Prettifying reports what it actually parses, which is strings, comments and literals. It does |
343 | | // not check that the document is structurally valid JSON -- `[1 2]` prettifies to `[12]` -- so a |
344 | | // document that may not be well formed wants glz::validate_json or glz::validate_jsonc. |
345 | | |
346 | | template <auto Opts = opts{}> |
347 | | inline error_ctx prettify_json(const auto& in, auto& out) |
348 | | { |
349 | | context ctx{}; |
350 | | const auto n = detail::prettify_json<Opts>(ctx, in, out); |
351 | | return {n, ctx.error, ctx.custom_error_message}; |
352 | | } |
353 | | |
354 | | /// <summary> |
355 | | /// allocating version of prettify |
356 | | /// </summary> |
357 | | template <auto Opts = opts{}> |
358 | | inline std::string prettify_json(const auto& in) |
359 | 4.00k | { |
360 | 4.00k | context ctx{}; |
361 | 4.00k | std::string out{}; |
362 | 4.00k | detail::prettify_json<Opts>(ctx, in, out); |
363 | 4.00k | if (bool(ctx.error)) [[unlikely]] { |
364 | 3.15k | return {}; // an incomplete result is not a document; see above |
365 | 3.15k | } |
366 | 848 | return out; |
367 | 4.00k | } |
368 | | |
369 | | template <auto Opts = opts{}> |
370 | | inline error_ctx prettify_jsonc(const auto& in, auto& out) |
371 | | { |
372 | | context ctx{}; |
373 | | const auto n = detail::prettify_json<opt_true<Opts, &opts::comments>>(ctx, in, out); |
374 | | return {n, ctx.error, ctx.custom_error_message}; |
375 | | } |
376 | | |
377 | | /// <summary> |
378 | | /// allocating version of prettify |
379 | | /// </summary> |
380 | | template <auto Opts = opts{}> |
381 | | inline std::string prettify_jsonc(const auto& in) |
382 | 4.00k | { |
383 | 4.00k | context ctx{}; |
384 | 4.00k | std::string out{}; |
385 | 4.00k | detail::prettify_json<opt_true<Opts, &opts::comments>>(ctx, in, out); |
386 | 4.00k | if (bool(ctx.error)) [[unlikely]] { |
387 | 1.91k | return {}; // an incomplete result is not a document; see above |
388 | 1.91k | } |
389 | 2.08k | return out; |
390 | 4.00k | } _ZN3glz14prettify_jsoncITnDaXtlNS_4optsELj10ELb1ELb0ELb1ELb1EEENSt3__16vectorIcNS2_9allocatorIcEEEEEENS2_12basic_stringIcNS2_11char_traitsIcEES5_EERKT0_ Line | Count | Source | 382 | 2.00k | { | 383 | 2.00k | context ctx{}; | 384 | 2.00k | std::string out{}; | 385 | 2.00k | detail::prettify_json<opt_true<Opts, &opts::comments>>(ctx, in, out); | 386 | 2.00k | if (bool(ctx.error)) [[unlikely]] { | 387 | 959 | return {}; // an incomplete result is not a document; see above | 388 | 959 | } | 389 | 1.04k | return out; | 390 | 2.00k | } |
_ZN3glz14prettify_jsoncITnDaXtlNS_4optsELj10ELb1ELb0ELb1ELb1EEENSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEES8_RKT0_ Line | Count | Source | 382 | 2.00k | { | 383 | 2.00k | context ctx{}; | 384 | 2.00k | std::string out{}; | 385 | 2.00k | detail::prettify_json<opt_true<Opts, &opts::comments>>(ctx, in, out); | 386 | 2.00k | if (bool(ctx.error)) [[unlikely]] { | 387 | 959 | return {}; // an incomplete result is not a document; see above | 388 | 959 | } | 389 | 1.04k | return out; | 390 | 2.00k | } |
|
391 | | } |