aboutsummaryrefslogtreecommitdiff
path: root/libs/kstd
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-04-13 15:05:38 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-04-13 15:05:38 +0200
commita0720bbe3cdfe3174e3d064356b21f0fcd37832e (patch)
tree407fc0b80c5ba505998021b20c18cf78c6a65ddc /libs/kstd
parent45091789eb9e49856ea6c2f3606639d43f4584e7 (diff)
downloadteachos-a0720bbe3cdfe3174e3d064356b21f0fcd37832e.tar.xz
teachos-a0720bbe3cdfe3174e3d064356b21f0fcd37832e.zip
kstd/format: add kstd::format_to
Diffstat (limited to 'libs/kstd')
-rw-r--r--libs/kstd/include/kstd/bits/format/vformat.hpp42
-rw-r--r--libs/kstd/src/vformat.cpp15
2 files changed, 37 insertions, 20 deletions
diff --git a/libs/kstd/include/kstd/bits/format/vformat.hpp b/libs/kstd/include/kstd/bits/format/vformat.hpp
index d032c3a..69c7f33 100644
--- a/libs/kstd/include/kstd/bits/format/vformat.hpp
+++ b/libs/kstd/include/kstd/bits/format/vformat.hpp
@@ -8,6 +8,8 @@
#include <kstd/bits/format/string.hpp>
#include <kstd/string>
+#include <algorithm>
+#include <iterator>
#include <string_view>
#include <type_traits>
@@ -36,15 +38,30 @@ namespace kstd
string m_result{};
};
- struct string_iterator_writer : output_buffer
+ template<std::output_iterator<char> Output>
+ struct iterator_writer : output_buffer
{
- explicit string_iterator_writer(string::iterator iterator);
+ explicit iterator_writer(Output iterator)
+ : m_output{iterator}
+ {}
- auto push(std::string_view text) -> void override;
- auto push(char character) -> void override;
+ auto iterator() const -> Output
+ {
+ return m_output;
+ }
+
+ auto push(std::string_view text) -> void override
+ {
+ m_output = std::ranges::copy(text, m_output);
+ }
+
+ auto push(char character) -> void override
+ {
+ *m_output++ = character;
+ }
private:
- string::iterator m_iter{};
+ Output m_output{};
};
} // namespace bits::format
@@ -62,6 +79,21 @@ namespace kstd
return buffer.release();
}
+ //! Format a given string with the provided arguments.
+ //!
+ //! @param iterator The iterator to write to.
+ //! @param format The format string.
+ //! @param args The arguments for the format string.
+ //! @return An iterator past the last element written.
+ template<std::output_iterator<char> Output, typename... ArgumentTypes>
+ auto format_to(Output iterator, format_string<std::type_identity_t<ArgumentTypes>...> format,
+ ArgumentTypes &&... args) -> Output
+ {
+ auto buffer = bits::format::iterator_writer{iterator};
+ bits::format::vformat_to(buffer, format.str_view, std::forward<ArgumentTypes>(args)...);
+ return buffer.iterator();
+ }
+
} // namespace kstd
#endif \ No newline at end of file
diff --git a/libs/kstd/src/vformat.cpp b/libs/kstd/src/vformat.cpp
index 51aca84..b7c5121 100644
--- a/libs/kstd/src/vformat.cpp
+++ b/libs/kstd/src/vformat.cpp
@@ -1,7 +1,6 @@
#include <kstd/format>
#include <kstd/string>
-#include <algorithm>
#include <cstddef>
#include <iterator>
#include <string_view>
@@ -207,18 +206,4 @@ namespace kstd::bits::format
return std::move(m_result);
}
- string_iterator_writer::string_iterator_writer(string::iterator it)
- : m_iter{it}
- {}
-
- auto string_iterator_writer::push(std::string_view text) -> void
- {
- std::ranges::for_each(text, [this](auto c) { push(c); });
- }
-
- auto string_iterator_writer::push(char character) -> void
- {
- *m_iter++ = character;
- }
-
} // namespace kstd::bits::format \ No newline at end of file