From e0423b3d47d1aab54c55a85cce59555cb076e1ab Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 18 Dec 2025 16:18:31 +0100 Subject: kstd/io: implement pointer formatting --- libs/kstd/include/kstd/bits/formatter.hpp | 33 ++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/libs/kstd/include/kstd/bits/formatter.hpp b/libs/kstd/include/kstd/bits/formatter.hpp index e10fc0c..229e76d 100644 --- a/libs/kstd/include/kstd/bits/formatter.hpp +++ b/libs/kstd/include/kstd/bits/formatter.hpp @@ -7,8 +7,10 @@ #include #include +#include #include #include +#include #include #include #include @@ -30,7 +32,7 @@ namespace kstd return bits::parse_specs(context, specs); } - auto format(T value, format_context & context) -> void + auto format(T value, format_context & context) const -> void { enum struct base { @@ -169,6 +171,31 @@ namespace kstd } }; + template + struct formatter : formatter + { + constexpr auto parse(std::string_view context) -> std::string_view + { + auto result = formatter::parse(context); + if (!this->specs.type) + { + this->specs.type = 'p'; + this->specs.alternative_form = true; + } + return result; + } + + auto format(T const * pointer, format_context & context) const -> void + { + formatter::format(std::bit_cast(pointer), context); + } + }; + + template + struct formatter : formatter + { + }; + template<> struct formatter { @@ -179,7 +206,7 @@ namespace kstd return bits::parse_specs(context, specs); } - auto format(std::string_view string, format_context & context) -> void + auto format(std::string_view string, format_context & context) const -> void { auto const content_length = string.size(); auto const padding_length = (specs.width > content_length) ? (specs.width - content_length) : 0; @@ -214,7 +241,7 @@ namespace kstd return bits::parse_specs(context, specs); } - auto format(char const * string, format_context & context) -> void + auto format(char const * string, format_context & context) const -> void { if (string) { -- cgit v1.2.3