From 143353942b296891361fa670d5b84a3ed5f8c578 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 18 Dec 2025 14:06:49 +0100 Subject: kstd/io: support string_view formatting --- libs/kstd/include/kstd/bits/formatter.hpp | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'libs/kstd/include') diff --git a/libs/kstd/include/kstd/bits/formatter.hpp b/libs/kstd/include/kstd/bits/formatter.hpp index 0fea5ef..895d52b 100644 --- a/libs/kstd/include/kstd/bits/formatter.hpp +++ b/libs/kstd/include/kstd/bits/formatter.hpp @@ -169,6 +169,41 @@ namespace kstd } }; + template<> + struct formatter + { + bits::format_specs specs; + + constexpr auto parse(std::string_view context) -> std::string_view + { + return bits::parse_specs(context, specs); + } + + auto format(std::string_view string, format_context & context) -> void + { + auto const content_length = string.size(); + auto const padding_length = (specs.width > content_length) ? (specs.width - content_length) : 0; + + if (!specs.align_left) + { + for (auto i = 0uz; i < padding_length; ++i) + { + context.push(specs.fill); + } + } + + context.push(string); + + if (specs.align_left) + { + for (auto i = 0uz; i < padding_length; ++i) + { + context.push(specs.fill); + } + } + } + }; + struct format_arg { using formatting_function = std::string_view(void const *, std::string_view, format_context &); -- cgit v1.2.3