diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2025-12-18 14:06:49 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2025-12-18 14:06:49 +0100 |
| commit | 143353942b296891361fa670d5b84a3ed5f8c578 (patch) | |
| tree | 556963b9ac9f1f849f3bcddc3a73582281c37451 /libs | |
| parent | 8042003647b50e9fddfe500677a132130449d69e (diff) | |
| download | teachos-143353942b296891361fa670d5b84a3ed5f8c578.tar.xz teachos-143353942b296891361fa670d5b84a3ed5f8c578.zip | |
kstd/io: support string_view formatting
Diffstat (limited to 'libs')
| -rw-r--r-- | libs/kstd/include/kstd/bits/formatter.hpp | 35 |
1 files changed, 35 insertions, 0 deletions
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<std::string_view> + { + 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 &); |
