1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
#include "kapi/cio.hpp"
#include <kstd/format>
#include <array>
#include <cstddef>
#include <iterator>
#include <optional>
#include <string_view>
#include <utility>
namespace teachos::cio
{
namespace
{
struct null_device final : public output_device
{
null_device static instance;
auto write(std::string_view) -> void override {}
auto writeln(std::string_view) -> void override {}
auto write_error(std::string_view) -> void override {}
auto writeln_error(std::string_view) -> void override {}
};
constinit null_device null_device::instance;
struct write_buffer
{
constexpr auto static size = 128uz;
write_buffer(write_buffer const &) = delete;
write_buffer(write_buffer &&) = delete;
auto operator=(write_buffer const &) -> write_buffer & = delete;
auto operator=(write_buffer &&) -> write_buffer & = delete;
explicit write_buffer(output_device * device, bool write_to_error)
: m_device{device}
, m_write_to_error{write_to_error}
{}
~write_buffer() noexcept
{
flush();
}
auto flush() noexcept -> void
{
if (m_position > 0)
{
std::string_view chunk{m_buffer.data(), m_position};
if (m_write_to_error)
{
m_device->write_error(chunk);
}
else
{
m_device->write(chunk);
}
m_position = 0;
}
}
auto static callback(void * object, std::string_view text) -> void
{
auto * self = static_cast<write_buffer *>(object);
for (char const character : text)
{
if (self->m_position >= size)
{
self->flush();
}
self->m_buffer.at(self->m_position++) = character;
}
}
private:
output_device * m_device;
bool m_write_to_error;
std::array<char, size> m_buffer{};
std::size_t m_position{};
};
} // namespace
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
constinit auto active_device = static_cast<output_device *>(&null_device::instance);
auto set_output_device(output_device & device) -> std::optional<output_device *>
{
if (&device == active_device)
{
return {};
}
return std::exchange(active_device, &device);
}
auto print(std::string_view text) -> void
{
active_device->write(text);
}
auto println(std::string_view text) -> void
{
active_device->writeln(text);
}
auto print_error(std::string_view text) -> void
{
active_device->write_error(text);
}
auto println_error(std::string_view text) -> void
{
active_device->writeln_error(text);
}
namespace
{
auto static vprint_impl(std::string_view fmt, kstd::format_args args, bool write_to_error) -> void
{
if (!active_device)
return;
auto writer = write_buffer{active_device, write_to_error};
auto context = kstd::format_context{.writer = write_buffer::callback, .user_data = &writer};
auto current = fmt.begin();
auto end = fmt.end();
auto next_automatic_index = 0uz;
while (current != end)
{
if (*current != '{')
{
auto start = current;
while (current != end && *current != '{')
{
std::advance(current, 1);
}
context.push(std::string_view(start, current - start));
continue;
}
if (std::next(current) != end && *(std::next(current)) == '{')
{
context.push('{');
std::advance(current, 2);
continue;
}
std::advance(current, 1);
auto index = 0uz;
if (current != end && *current >= '0' && *current <= '9')
{
while (current != end && *current >= '0' && *current <= '9')
{
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
index = index * 10 + static_cast<std::size_t>(*current - '0');
std::advance(current, 1);
}
}
else
{
index = next_automatic_index++;
}
auto remaining_fmt = std::string_view{current, static_cast<std::size_t>(std::distance(current, end))};
auto const arg = args.get(index);
if (arg.format)
{
auto const after_specs = arg.format(arg.value, remaining_fmt, context);
auto const consumed = remaining_fmt.size() - after_specs.size();
std::advance(current, consumed);
}
else
{
context.push("{?}");
while (current != end && *current != '}')
std::advance(current, 1);
}
if (current != end && *current == '}')
{
std::advance(current, 1);
}
else
{
context.push("{fmt-err}");
while (current != end && *current != '}')
std::advance(current, 1);
if (current != end)
std::advance(current, 1);
}
}
}
} // namespace
auto vprint(std::string_view fmt, kstd::format_args args) -> void
{
vprint_impl(fmt, args, false);
}
auto vprint_error(std::string_view fmt, kstd::format_args args) -> void
{
vprint_impl(fmt, args, true);
}
} // namespace teachos::cio
|