aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-04-13 14:14:08 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-04-13 14:14:08 +0200
commite38fd2056a8cab7a3c6f3da8150fed69530bde6c (patch)
treedebece045de178ac110b8f81d8714fe9d681309e /libs
parent4d2a1d028f8ba28b655026b93124e71a12562619 (diff)
downloadteachos-e38fd2056a8cab7a3c6f3da8150fed69530bde6c.tar.xz
teachos-e38fd2056a8cab7a3c6f3da8150fed69530bde6c.zip
kstd: introduce output buffer for formatting
Diffstat (limited to 'libs')
-rw-r--r--libs/kstd/include/kstd/bits/format/context.hpp12
-rw-r--r--libs/kstd/include/kstd/bits/format/output_buffer.hpp30
2 files changed, 36 insertions, 6 deletions
diff --git a/libs/kstd/include/kstd/bits/format/context.hpp b/libs/kstd/include/kstd/bits/format/context.hpp
index 478a48f..e8d0302 100644
--- a/libs/kstd/include/kstd/bits/format/context.hpp
+++ b/libs/kstd/include/kstd/bits/format/context.hpp
@@ -4,6 +4,7 @@
// IWYU pragma: private, include <kstd/format>
#include "arg.hpp"
+#include "kstd/bits/format/output_buffer.hpp"
#include <kstd/os/error.hpp>
@@ -29,11 +30,7 @@ namespace kstd
struct format_context
{
- using writer_function = void(void *, std::string_view);
using format_args = std::span<format_arg const>;
-
- writer_function * writer{};
- void * user_data{};
format_args args{};
[[nodiscard]] auto arg(std::size_t const id) const -> format_arg const &
@@ -47,13 +44,16 @@ namespace kstd
constexpr auto push(std::string_view string) -> void
{
- writer(user_data, string);
+ m_buffer->push(string);
}
constexpr auto push(char character) -> void
{
- writer(user_data, std::string_view(&character, 1));
+ m_buffer->push(character);
}
+
+ private:
+ bits::format::output_buffer * m_buffer;
};
} // namespace kstd
diff --git a/libs/kstd/include/kstd/bits/format/output_buffer.hpp b/libs/kstd/include/kstd/bits/format/output_buffer.hpp
new file mode 100644
index 0000000..be2034f
--- /dev/null
+++ b/libs/kstd/include/kstd/bits/format/output_buffer.hpp
@@ -0,0 +1,30 @@
+#ifndef KSTD_BITS_FORMAT_OUTPUT_BUFFER_HPP
+#define KSTD_BITS_FORMAT_OUTPUT_BUFFER_HPP
+
+#include <string_view>
+
+namespace kstd::bits::format
+{
+
+ //! An abstract interface for formatted output buffers.
+ //!
+ //! This interface is intended to be use for functions dealing with string formatting, like the print and the format
+ //! family.
+ struct output_buffer
+ {
+ virtual ~output_buffer() = default;
+
+ //! Push a text segment into the buffer.
+ //!
+ //! @param text The text segment to push.
+ virtual auto push(std::string_view text) -> void = 0;
+
+ //! Push a single character into the buffer.
+ //!
+ //! @param character The character to push into the buffer.
+ virtual auto push(char character) -> void = 0;
+ };
+
+} // namespace kstd::bits::format
+
+#endif \ No newline at end of file