aboutsummaryrefslogtreecommitdiff
path: root/libs/kstd/include
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-04-13 14:37:20 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-04-13 14:37:20 +0200
commit98fc294deb6f7c27eda3f9ed3b616572a1ab2009 (patch)
treeab2d0730cc51b4a1a0bc23741cb39cc022e3bbbd /libs/kstd/include
parente38fd2056a8cab7a3c6f3da8150fed69530bde6c (diff)
downloadteachos-98fc294deb6f7c27eda3f9ed3b616572a1ab2009.tar.xz
teachos-98fc294deb6f7c27eda3f9ed3b616572a1ab2009.zip
kstd/format: hook up vformat_to
Diffstat (limited to 'libs/kstd/include')
-rw-r--r--libs/kstd/include/kstd/bits/format/output_buffer.hpp2
-rw-r--r--libs/kstd/include/kstd/bits/format/vformat.hpp65
-rw-r--r--libs/kstd/include/kstd/format2
3 files changed, 69 insertions, 0 deletions
diff --git a/libs/kstd/include/kstd/bits/format/output_buffer.hpp b/libs/kstd/include/kstd/bits/format/output_buffer.hpp
index be2034f..fd7a2b4 100644
--- a/libs/kstd/include/kstd/bits/format/output_buffer.hpp
+++ b/libs/kstd/include/kstd/bits/format/output_buffer.hpp
@@ -1,6 +1,8 @@
#ifndef KSTD_BITS_FORMAT_OUTPUT_BUFFER_HPP
#define KSTD_BITS_FORMAT_OUTPUT_BUFFER_HPP
+// IWYU pragma: private, include <kstd/format>
+
#include <string_view>
namespace kstd::bits::format
diff --git a/libs/kstd/include/kstd/bits/format/vformat.hpp b/libs/kstd/include/kstd/bits/format/vformat.hpp
new file mode 100644
index 0000000..90b74a9
--- /dev/null
+++ b/libs/kstd/include/kstd/bits/format/vformat.hpp
@@ -0,0 +1,65 @@
+#ifndef KSTD_BITS_FORMAT_VFORMAT_HPP
+#define KSTD_BITS_FORMAT_VFORMAT_HPP
+
+// IWYU pragma: private, include <kstd/format>
+
+#include <kstd/bits/format/args.hpp>
+#include <kstd/bits/format/output_buffer.hpp>
+#include <kstd/bits/format/string.hpp>
+#include <kstd/string>
+
+#include <string_view>
+#include <type_traits>
+
+namespace kstd
+{
+
+ namespace bits::format
+ {
+ //! Format a string with the given arguments into the given buffer.
+ //!
+ //! External implementations of `vprint` may call this function.
+ //!
+ //! @param buffer The buffer to format into.
+ //! @param format The format string to use.
+ //! @param args The arguments for the format string.
+ auto vformat_to(output_buffer & buffer, std::string_view format, format_args args) -> void;
+
+ struct string_writer : output_buffer
+ {
+ auto push(std::string_view text) -> void override;
+ auto push(char character) -> void override;
+
+ auto release() -> string &&;
+
+ private:
+ string m_result{};
+ };
+
+ struct string_iterator_writer : output_buffer
+ {
+ auto push(std::string_view text) -> void override;
+ auto push(char character) -> void override;
+
+ private:
+ string::iterator m_iter{};
+ };
+
+ } // namespace bits::format
+
+ //! Format a given string with the provided arguments.
+ //!
+ //! @param format The format string.
+ //! @param args The arguments for the format string.
+ //! @return A new string containing the result of the format operation.
+ template<typename... ArgumentTypes>
+ auto format(format_string<std::type_identity_t<ArgumentTypes>...> format, ArgumentTypes &&... args) -> string
+ {
+ auto buffer = bits::format::string_writer{};
+ bits::format::vformat_to(buffer, format.str_view, std::forward<ArgumentTypes>(args)...);
+ return buffer.release();
+ }
+
+} // namespace kstd
+
+#endif \ No newline at end of file
diff --git a/libs/kstd/include/kstd/format b/libs/kstd/include/kstd/format
index adee5f8..d11c221 100644
--- a/libs/kstd/include/kstd/format
+++ b/libs/kstd/include/kstd/format
@@ -13,7 +13,9 @@
#include "bits/format/formatter/pointer.hpp" // IWYU pragma: export
#include "bits/format/formatter/range.hpp" // IWYU pragma: export
#include "bits/format/formatter/string_view.hpp" // IWYU pragma: export
+#include "bits/format/output_buffer.hpp" // IWYU pragma: export
#include "bits/format/parse_context.hpp" // IWYU pragma: export
#include "bits/format/string.hpp" // IWYU pragma: export
+#include "bits/format/vformat.hpp" // IWYU pragma: export
#endif \ No newline at end of file