blob: d0611b21305314ea2fc8cebfa163fb4d7e939e69 (
plain)
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
|
#include <kstd/os/print.hpp>
#include <kapi/cio.hpp>
#include <kstd/bits/format/output_buffer.hpp>
#include <kstd/format>
#include <kstd/print>
#include <algorithm>
#include <array>
#include <cstddef>
#include <string_view>
namespace kstd::os
{
namespace
{
struct write_buffer final : kstd::bits::format::output_buffer
{
using output_stream = kapi::cio::output_stream;
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_stream stream)
: m_stream{stream}
{}
~write_buffer() noexcept final
{
flush();
}
auto push(std::string_view text) -> void final
{
std::ranges::for_each(text, [this](auto c) { this->push(c); });
}
auto push(char character) -> void final
{
if (m_position >= size)
{
flush();
}
m_buffer.at(m_position++) = character;
}
private:
auto flush() noexcept -> void
{
if (m_position > 0)
{
std::string_view chunk{m_buffer.data(), m_position};
kapi::cio::write(m_stream, chunk);
m_position = 0;
}
}
output_stream m_stream;
std::array<char, size> m_buffer{};
std::size_t m_position{};
};
} // namespace
auto vprint(print_sink sink, std::string_view format, kstd::format_args args) -> void
{
auto writer = write_buffer{(sink == print_sink::stderr) ? kapi::cio::output_stream::stderr
: kapi::cio::output_stream::stdout};
kstd::bits::format::vformat_to(writer, format, args);
}
} // namespace kstd::os
|