diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2025-12-18 16:18:31 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2025-12-18 16:18:31 +0100 |
| commit | e0423b3d47d1aab54c55a85cce59555cb076e1ab (patch) | |
| tree | fc2965bc3538b7fc3b22e7f46a3f0976c3d91ecc | |
| parent | 971b45692224b3c52be21cd3a883fb6cd5089776 (diff) | |
| download | teachos-e0423b3d47d1aab54c55a85cce59555cb076e1ab.tar.xz teachos-e0423b3d47d1aab54c55a85cce59555cb076e1ab.zip | |
kstd/io: implement pointer formatting
| -rw-r--r-- | libs/kstd/include/kstd/bits/formatter.hpp | 33 |
1 files 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 <kstd/bits/format_specs.hpp> #include <array> +#include <bit> #include <concepts> #include <cstddef> +#include <cstdint> #include <iterator> #include <string_view> #include <type_traits> @@ -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<typename T> + struct formatter<T const *> : formatter<std::uintptr_t> + { + constexpr auto parse(std::string_view context) -> std::string_view + { + auto result = formatter<std::uintptr_t>::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<std::uintptr_t>::format(std::bit_cast<std::uintptr_t>(pointer), context); + } + }; + + template<typename T> + struct formatter<T *> : formatter<T const *> + { + }; + template<> struct formatter<std::string_view> { @@ -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) { |
