diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-03-19 17:21:21 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-03-19 17:21:21 +0100 |
| commit | 1865f7a162a496592e236ffcff171e7e7bc47ee2 (patch) | |
| tree | cd6cfd4dd5cd63d7f2360a7c380b691d3e595f51 /libs | |
| parent | 63395fecd439989734f80e6420e4961557465ff9 (diff) | |
| download | teachos-1865f7a162a496592e236ffcff171e7e7bc47ee2.tar.xz teachos-1865f7a162a496592e236ffcff171e7e7bc47ee2.zip | |
kstd/format: add support for formatting of orderings
Diffstat (limited to 'libs')
| -rw-r--r-- | libs/kstd/include/kstd/bits/formatter.hpp | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/libs/kstd/include/kstd/bits/formatter.hpp b/libs/kstd/include/kstd/bits/formatter.hpp index a467edd..7c9c31d 100644 --- a/libs/kstd/include/kstd/bits/formatter.hpp +++ b/libs/kstd/include/kstd/bits/formatter.hpp @@ -5,9 +5,11 @@ #include <kstd/bits/format_context.hpp> #include <kstd/bits/format_specs.hpp> +#include <kstd/os/error.hpp> #include <array> #include <bit> +#include <compare> #include <concepts> #include <cstddef> #include <cstdint> @@ -282,6 +284,98 @@ namespace kstd } }; + template<> + struct formatter<std::strong_ordering> + { + bits::format_specs specs{}; + + constexpr auto parse(std::string_view context) -> std::string_view + { + return bits::parse_specs(context, specs); + } + + auto format(std::strong_ordering value, format_context & context) const -> void + { + if (value == std::strong_ordering::equal) + { + return context.push(specs.alternative_form ? "==" : "equal"); + } + else if (value == std::strong_ordering::equivalent) + { + return context.push(specs.alternative_form ? "==" : "equivalent"); + } + else if (value == std::strong_ordering::greater) + { + return context.push(specs.alternative_form ? ">" : "greater"); + } + else if (value == std::strong_ordering::less) + { + return context.push(specs.alternative_form ? "<" : "less"); + } + kstd::os::panic("[kstd:format] Invalid strong ordering value!"); + } + }; + + template<> + struct formatter<std::weak_ordering> + { + bits::format_specs specs{}; + + constexpr auto parse(std::string_view context) -> std::string_view + { + return bits::parse_specs(context, specs); + } + + auto format(std::weak_ordering value, format_context & context) const -> void + { + if (value == std::weak_ordering::equivalent) + { + return context.push(specs.alternative_form ? "==" : "equivalent"); + } + else if (value == std::weak_ordering::greater) + { + return context.push(specs.alternative_form ? ">" : "greater"); + } + else if (value == std::weak_ordering::less) + { + return context.push(specs.alternative_form ? "<" : "less"); + } + kstd::os::panic("[kstd:format] Invalid weak ordering value!"); + } + }; + + template<> + struct formatter<std::partial_ordering> + { + bits::format_specs specs{}; + + constexpr auto parse(std::string_view context) -> std::string_view + { + return bits::parse_specs(context, specs); + } + + auto format(std::partial_ordering value, format_context & context) const -> void + { + if (value == std::partial_ordering::equivalent) + { + return context.push(specs.alternative_form ? "==" : "equivalent"); + } + else if (value == std::partial_ordering::greater) + { + return context.push(specs.alternative_form ? ">" : "greater"); + } + else if (value == std::partial_ordering::less) + { + return context.push(specs.alternative_form ? "<" : "less"); + } + else if (value == std::partial_ordering::unordered) + { + return context.push(specs.alternative_form ? "<=>" : "unordered"); + } + kstd::os::panic("[kstd:format] Invalid partial ordering value!"); + } + }; + struct format_arg { using formatting_function = std::string_view(void const *, std::string_view, format_context &); |
