diff options
Diffstat (limited to 'libs/kstd/include')
| -rw-r--r-- | libs/kstd/include/kstd/bits/format/vformat.hpp | 42 |
1 files changed, 37 insertions, 5 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 |
