aboutsummaryrefslogtreecommitdiff
path: root/kernel/kstd/print.cpp
blob: db03816a845b1ae461357a909b13aacf03f441f3 (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
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
#include "kapi/cio.hpp"

#include <kstd/format>
#include <kstd/os/print.hpp>
#include <kstd/print>

#include <array>
#include <cstddef>
#include <iterator>
#include <string_view>

namespace kstd::os
{

  namespace
  {
    struct write_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
      {
        flush();
      }

      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;
        }
      }

      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_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};
    auto context = kstd::format_context{.writer = write_buffer::callback, .user_data = &writer, .args = args};
    auto parse_context = kstd::format_parse_context{format, args.size()};

    auto it = parse_context.begin();
    auto end = parse_context.end();

    while (it != end)
    {
      if (*it != '{' && *it != '}')
      {
        auto start = it;
        while (it != end && *it != '{' && *it != '}')
        {
          std::advance(it, 1);
        }
        parse_context.advance_to(it);
        context.push(std::string_view(start, it - start));
        continue;
      }

      if (*it == '{')
      {
        std::advance(it, 1);
        if (it != end && *it == '{')
        {
          context.push('{');
          std::advance(it, 1);
          parse_context.advance_to(it);
          continue;
        }

        parse_context.advance_to(it);
        auto index = 0uz;

        if (it != end && *it >= '0' && *it <= '9')
        {
          while (it != end && *it >= '0' && *it <= '9')
          {
            index = index * 10 + static_cast<std::size_t>(*it - '0');
            std::advance(it, 1);
          }
          parse_context.check_arg_id(index);
        }
        else
        {
          index = parse_context.next_arg_id();
        }

        if (it != end && *it == ':')
        {
          std::advance(it, 1);
        }

        parse_context.advance_to(it);

        if (index < args.size())
        {
          auto const & arg = args[index];
          if (arg.format_function)
          {
            arg.format_function(arg.value_pointer, parse_context, context);
          }
          else
          {
            context.push("{?}");
          }
        }
        else
        {
          context.push("{fmt-err: bound}");
        }

        it = parse_context.begin();

        if (it != end && *it == '}')
        {
          std::advance(it, 1);
          parse_context.advance_to(it);
        }
        else
        {
          context.push("{fmt-err: unconsumed}");
          while (it != end && *it != '}')
          {
            std::advance(it, 1);
          }

          if (it != end)
          {
            std::advance(it, 1);
            parse_context.advance_to(it);
          }
        }
      }
      else if (*it == '}')
      {
        std::advance(it, 1);
        if (it != end && *it == '}')
        {
          context.push('}');
          std::advance(it, 1);
          parse_context.advance_to(it);
        }
        else
        {
          context.push("{fmt-err: unescaped}");
          parse_context.advance_to(it);
        }
      }
    }
  }

}  // namespace kstd::os