aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/kstd/print.cpp381
1 files changed, 192 insertions, 189 deletions
diff --git a/kernel/kstd/print.cpp b/kernel/kstd/print.cpp
index beee2e5..a0bb7d6 100644
--- a/kernel/kstd/print.cpp
+++ b/kernel/kstd/print.cpp
@@ -1,12 +1,13 @@
#include "kapi/cio.hpp"
+#include <kstd/bits/format/output_buffer.hpp>
#include <kstd/format>
#include <kstd/os/print.hpp>
#include <kstd/print>
+#include <algorithm>
#include <array>
#include <cstddef>
-#include <iterator>
#include <string_view>
namespace kstd::os
@@ -14,7 +15,7 @@ namespace kstd::os
namespace
{
- struct write_buffer
+ struct write_buffer final : kstd::bits::format::output_buffer
{
using output_stream = kapi::cio::output_stream;
@@ -29,7 +30,7 @@ namespace kstd::os
: m_stream{stream}
{}
- ~write_buffer() noexcept
+ ~write_buffer() noexcept final
{
flush();
}
@@ -44,17 +45,18 @@ namespace kstd::os
}
}
- auto static callback(void * object, std::string_view text) -> void
+ auto push(std::string_view text) -> void final
{
- auto * self = static_cast<write_buffer *>(object);
- for (char const character : text)
+ std::ranges::for_each(text, [this](auto c) { this->push(c); });
+ }
+
+ auto push(char character) -> void final
+ {
+ if (m_position >= size)
{
- if (self->m_position >= size)
- {
- self->flush();
- }
- self->m_buffer.at(self->m_position++) = character;
+ flush();
}
+ m_buffer.at(m_position++) = character;
}
private:
@@ -69,184 +71,185 @@ namespace kstd::os
{
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);
- }
- }
- }
+ kstd::bits::format::vformat_to(writer, format, args);
+ // 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