/src/glaze/include/glaze/util/validate.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Glaze Library |
2 | | // For the license information refer to glaze.hpp |
3 | | |
4 | | #pragma once |
5 | | |
6 | | #include <algorithm> |
7 | | #include <optional> |
8 | | #include <string> |
9 | | |
10 | | #include "glaze/core/write_chars.hpp" |
11 | | #include "glaze/util/dump.hpp" |
12 | | |
13 | | namespace glz |
14 | | { |
15 | | namespace detail |
16 | | { |
17 | | struct source_info final |
18 | | { |
19 | | size_t line{}; |
20 | | size_t column{}; |
21 | | std::string context{}; |
22 | | size_t index{}; |
23 | | size_t front_truncation{}; |
24 | | size_t rear_truncation{}; |
25 | | }; |
26 | | |
27 | | // We convert to only single spaces for error messages in order to keep the source info |
28 | | // calculation more efficient and avoid needing to allocate more memory. |
29 | | inline void convert_tabs_to_single_spaces(std::string& input) noexcept |
30 | 0 | { |
31 | 0 | for (auto& c : input) { |
32 | 0 | if (c == '\t') { |
33 | 0 | c = ' '; |
34 | 0 | } |
35 | 0 | } |
36 | 0 | } |
37 | | |
38 | | inline source_info get_source_info(const has_size auto& buffer, const size_t index) |
39 | | { |
40 | | using V = std::decay_t<decltype(buffer[0])>; |
41 | | |
42 | | if constexpr (std::same_as<V, std::byte>) { |
43 | | return {.context = "", .index = index}; |
44 | | } |
45 | | else { |
46 | | if (index >= buffer.size()) { |
47 | | return {.context = "", .index = index}; |
48 | | } |
49 | | |
50 | | const auto start = std::begin(buffer) + index; |
51 | | const auto line = size_t(std::count(std::begin(buffer), start, static_cast<V>('\n')) + 1); |
52 | | const auto rstart = std::rbegin(buffer) + buffer.size() - index - 1; |
53 | | const auto prev_new_line = |
54 | | std::find((std::min)(rstart + 1, std::rend(buffer)), std::rend(buffer), static_cast<V>('\n')); |
55 | | const auto column = size_t(std::distance(rstart, prev_new_line)); |
56 | | const auto next_new_line = |
57 | | std::find((std::min)(start + 1, std::end(buffer)), std::end(buffer), static_cast<V>('\n')); |
58 | | |
59 | | const auto offset = (prev_new_line == std::rend(buffer) ? 0 : index - column + 1); |
60 | | auto context_begin = std::begin(buffer) + offset; |
61 | | auto context_end = next_new_line; |
62 | | |
63 | | size_t front_truncation = 0; |
64 | | size_t rear_truncation = 0; |
65 | | |
66 | | if (std::distance(context_begin, context_end) > 64) { |
67 | | // reduce the context length so that we can more easily see errors, especially for non-prettified buffers |
68 | | if (column <= 32) { |
69 | | rear_truncation = 64; |
70 | | context_end = context_begin + rear_truncation; |
71 | | } |
72 | | else { |
73 | | front_truncation = column - 32; |
74 | | context_begin += front_truncation; |
75 | | if (std::distance(context_begin, context_end) > 64) { |
76 | | rear_truncation = front_truncation + 64; |
77 | | context_end = std::begin(buffer) + offset + rear_truncation; |
78 | | } |
79 | | } |
80 | | } |
81 | | |
82 | | std::string context{context_begin, context_end}; |
83 | | convert_tabs_to_single_spaces(context); |
84 | | return {line, column, context, index, front_truncation, rear_truncation}; |
85 | | } |
86 | | } |
87 | | |
88 | | template <class B> |
89 | | requires(!has_size<B>) |
90 | | inline source_info get_source_info(const B* buffer, const size_t index) |
91 | | { |
92 | | return get_source_info(sv{buffer}, index); |
93 | | } |
94 | | |
95 | | inline std::string generate_error_string(const std::string_view error, const source_info& info, |
96 | | const std::string_view filename = "") |
97 | 0 | { |
98 | 0 | std::string b{}; |
99 | 0 | b.resize(error.size() + info.context.size() + filename.size() + 128); |
100 | 0 | size_t ix{}; |
101 | 0 |
|
102 | 0 | if (not filename.empty()) { |
103 | 0 | dump_not_empty(filename, b, ix); |
104 | 0 | dump(':', b, ix); |
105 | 0 | } |
106 | 0 |
|
107 | 0 | glz::context ctx{}; |
108 | 0 |
|
109 | 0 | if (info.context.empty()) { |
110 | 0 | dump<"index ">(b, ix); |
111 | 0 | write_chars::op<opts{}>(info.index, ctx, b, ix); |
112 | 0 | dump<": ">(b, ix); |
113 | 0 | dump_maybe_empty(error, b, ix); |
114 | 0 | } |
115 | 0 | else { |
116 | 0 | write_chars::op<opts{}>(info.line, ctx, b, ix); |
117 | 0 | dump(':', b, ix); |
118 | 0 | write_chars::op<opts{}>(info.column, ctx, b, ix); |
119 | 0 | dump<": ">(b, ix); |
120 | 0 | dump_maybe_empty(error, b, ix); |
121 | 0 | dump('\n', b, ix); |
122 | 0 | if (info.front_truncation) { |
123 | 0 | if (info.rear_truncation) { |
124 | 0 | dump<"...">(b, ix); |
125 | 0 | dump_maybe_empty(info.context, b, ix); |
126 | 0 | dump<"...\n ">(b, ix); |
127 | 0 | } |
128 | 0 | else { |
129 | 0 | dump<"...">(b, ix); |
130 | 0 | dump_maybe_empty(info.context, b, ix); |
131 | 0 | dump<"\n ">(b, ix); |
132 | 0 | } |
133 | 0 | } |
134 | 0 | else { |
135 | 0 | dump<" ">(b, ix); |
136 | 0 | dump_maybe_empty(info.context, b, ix); |
137 | 0 | dump<"\n ">(b, ix); |
138 | 0 | } |
139 | 0 | dumpn<' '>(info.column - 1 - info.front_truncation, b, ix); |
140 | 0 | dump('^', b, ix); |
141 | 0 | } |
142 | 0 |
|
143 | 0 | b.resize(ix); |
144 | 0 | return b; |
145 | 0 | } |
146 | | } |
147 | | } |