aboutsummaryrefslogtreecommitdiff
path: root/kernel/kstd/print.cpp
blob: beee2e5f25507bcf65b4b17ca8a5c8fba138db5c (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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#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];
          switch (arg.type)
          {
            case kstd::bits::format::arg_type::boolean:
            {
              auto fmt = kstd::formatter<bool>{};
              auto const parsed = fmt.parse(parse_context);
              parse_context.advance_to(parsed);
              fmt.format(arg.value.boolean, context);
              break;
            }
            case kstd::bits::format::arg_type::character:
            {
              auto fmt = kstd::formatter<char>{};
              auto const parsed = fmt.parse(parse_context);
              parse_context.advance_to(parsed);
              fmt.format(arg.value.character, context);
              break;
            }
            case kstd::bits::format::arg_type::integer:
            {
              auto fmt = kstd::formatter<long long>{};
              auto const parsed = fmt.parse(parse_context);
              parse_context.advance_to(parsed);
              fmt.format(arg.value.integer, context);
              break;
            }
            case kstd::bits::format::arg_type::unsigned_integer:
            {
              auto fmt = kstd::formatter<unsigned long long>{};
              auto const parsed = fmt.parse(parse_context);
              parse_context.advance_to(parsed);
              fmt.format(arg.value.unsigned_integer, context);
              break;
            }
            case kstd::bits::format::arg_type::string_view:
            {
              auto fmt = kstd::formatter<std::string_view>{};
              auto const parsed = fmt.parse(parse_context);
              parse_context.advance_to(parsed);
              fmt.format(arg.value.string_view, context);
              break;
            }
            case kstd::bits::format::arg_type::c_string:
            {
              auto fmt = kstd::formatter<char const *>{};
              auto const parsed = fmt.parse(parse_context);
              parse_context.advance_to(parsed);
              fmt.format(arg.value.c_string, context);
              break;
            }
            case kstd::bits::format::arg_type::pointer:
            {
              auto fmt = kstd::formatter<void const *>{};
              auto const parsed = fmt.parse(parse_context);
              parse_context.advance_to(parsed);
              fmt.format(arg.value.pointer, context);
              break;
            }
            case kstd::bits::format::arg_type::user_defined:
            {
              if (arg.value.user_defined.format)
              {
                arg.value.user_defined.format(arg.value.user_defined.pointer, parse_context, context);
              }
              else
              {
                context.push("{?}");
              }
              break;
            }
            default:
            {
              context.push("{fmt-err: unknown-type}");
              break;
            }
          }
        }
        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