diff options
Diffstat (limited to 'libs/kstd')
| -rw-r--r-- | libs/kstd/include/kstd/bits/format/output_buffer.hpp | 2 | ||||
| -rw-r--r-- | libs/kstd/include/kstd/bits/format/vformat.hpp | 65 | ||||
| -rw-r--r-- | libs/kstd/include/kstd/format | 2 |
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 |
