aboutsummaryrefslogtreecommitdiff
path: root/kernel/kstd
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/kstd')
-rw-r--r--kernel/kstd/os.cpp2
-rw-r--r--kernel/kstd/print.cpp113
-rw-r--r--kernel/kstd/print.tests.cpp23
3 files changed, 47 insertions, 91 deletions
diff --git a/kernel/kstd/os.cpp b/kernel/kstd/os.cpp
index 21254c4..ae69e7e 100644
--- a/kernel/kstd/os.cpp
+++ b/kernel/kstd/os.cpp
@@ -1,4 +1,4 @@
-#include "kapi/system.hpp"
+#include <kapi/system.hpp>
#include <kstd/os/error.hpp>
diff --git a/kernel/kstd/print.cpp b/kernel/kstd/print.cpp
index c7d26ba..d0611b2 100644
--- a/kernel/kstd/print.cpp
+++ b/kernel/kstd/print.cpp
@@ -1,12 +1,14 @@
-#include "kapi/cio.hpp"
+#include <kstd/os/print.hpp>
+
+#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 +16,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,35 +31,36 @@ namespace kstd::os
: m_stream{stream}
{}
- ~write_buffer() noexcept
+ ~write_buffer() noexcept final
{
flush();
}
- auto flush() noexcept -> void
+ auto push(std::string_view text) -> void final
{
- if (m_position > 0)
+ std::ranges::for_each(text, [this](auto c) { this->push(c); });
+ }
+
+ auto push(char character) -> void final
+ {
+ if (m_position >= size)
{
- std::string_view chunk{m_buffer.data(), m_position};
- kapi::cio::write(m_stream, chunk);
- m_position = 0;
+ flush();
}
+ m_buffer.at(m_position++) = character;
}
- auto static callback(void * object, std::string_view text) -> void
+ private:
+ auto flush() noexcept -> void
{
- auto * self = static_cast<write_buffer *>(object);
- for (char const character : text)
+ if (m_position > 0)
{
- if (self->m_position >= size)
- {
- self->flush();
- }
- self->m_buffer.at(self->m_position++) = character;
+ std::string_view chunk{m_buffer.data(), m_position};
+ kapi::cio::write(m_stream, chunk);
+ m_position = 0;
}
}
- private:
output_stream m_stream;
std::array<char, size> m_buffer{};
std::size_t m_position{};
@@ -69,77 +72,7 @@ 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};
-
- auto current = format.begin();
- auto end = format.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')
- {
- 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);
- }
- }
+ kstd::bits::format::vformat_to(writer, format, args);
}
} // namespace kstd::os
diff --git a/kernel/kstd/print.tests.cpp b/kernel/kstd/print.tests.cpp
new file mode 100644
index 0000000..4963f46
--- /dev/null
+++ b/kernel/kstd/print.tests.cpp
@@ -0,0 +1,23 @@
+#include <kstd/print>
+
+#include <kernel/test_support/cio.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+
+SCENARIO("Kernel testing kstd shims", "[support]")
+{
+ GIVEN("the test support infrastructure is initialized")
+ {
+ WHEN("a regular print is issued")
+ {
+ kernel::tests::cio::log_buffer().clear();
+
+ kstd::println("[kernel:tests] Test Print");
+
+ THEN("the message is appended to the log buffer")
+ {
+ REQUIRE(kernel::tests::cio::log_buffer().flat_messages().contains("[kernel:tests] Test Print"));
+ }
+ }
+ }
+} \ No newline at end of file