diff options
| -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 &); |
