From 5f88b26b5a6a72965b4f0578863fcdd508e41c60 Mon Sep 17 00:00:00 2001 From: AWE Henry Date: Sun, 5 May 2024 16:44:51 +0800 Subject: [PATCH] Update implementation --- include/papilio/format/fundamental.inl | 33 +++++++++++--------------- include/papilio/format/helper.hpp | 8 +++++++ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/include/papilio/format/fundamental.inl b/include/papilio/format/fundamental.inl index f5db975..59fdee1 100644 --- a/include/papilio/format/fundamental.inl +++ b/include/papilio/format/fundamental.inl @@ -34,7 +34,6 @@ namespace detail std::size_t remain = m_data.width - used; switch(m_data.align) { - case format_align::right: return std::make_pair(remain, 0); @@ -63,6 +62,11 @@ namespace detail } }; + inline constexpr char digit_map_lower[16] = + {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + inline constexpr char digit_map_upper[16] = + {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + #ifdef PAPILIO_COMPILER_CLANG # pragma clang diagnostic push # if __clang_major__ >= 16 @@ -71,6 +75,7 @@ namespace detail #endif template + requires(!char_like) class int_formatter : public std_formatter_base { public: @@ -95,19 +100,7 @@ namespace detail auto [base, uppercase] = parse_type_ch(data().type); - // clang-format off - - const CharT digits[16] = { - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - static_cast(uppercase ? 'A' : 'a'), - static_cast(uppercase ? 'B' : 'b'), - static_cast(uppercase ? 'C' : 'c'), - static_cast(uppercase ? 'D' : 'd'), - static_cast(uppercase ? 'E' : 'e'), - static_cast(uppercase ? 'F' : 'f') - }; - - // clang-format on + const auto& digits = uppercase ? digit_map_upper : digit_map_lower; const bool neg = val < 0; if constexpr(std::is_signed_v) @@ -240,7 +233,7 @@ namespace detail uppercase = true; [[fallthrough]]; case U'd': - // base is already 10 + PAPILIO_ASSERT(base == 10); break; default: @@ -255,8 +248,7 @@ namespace detail { switch(base) { - default: - PAPILIO_ASSERT(base != 10); + case 10: return 0; case 2: @@ -265,6 +257,9 @@ namespace detail case 8: return 1; // "o" + + default: + PAPILIO_UNREACHABLE(); } } }; @@ -648,7 +643,7 @@ public: template auto format(utf::codepoint cp, FormatContext& ctx) const -> typename FormatContext::iterator { - if(!m_data.contains_type(U"c")) + if(!m_data.contains_type(U'c')) { detail::int_formatter fmt; fmt.set_data(m_data); @@ -689,7 +684,7 @@ public: template auto format(bool val, FormatContext& ctx) const -> typename FormatContext::iterator { - if(!m_data.contains_type(U"s")) + if(!m_data.contains_type(U's')) { detail::int_formatter fmt; fmt.set_data(m_data); diff --git a/include/papilio/format/helper.hpp b/include/papilio/format/helper.hpp index 903680b..ab52f2b 100644 --- a/include/papilio/format/helper.hpp +++ b/include/papilio/format/helper.hpp @@ -24,6 +24,14 @@ PAPILIO_EXPORT struct std_formatter_data bool alternate_form = false; bool use_locale = false; + [[nodiscard]] + constexpr bool contains_type(char32_t type) const noexcept + { + if(this->type == U'\0') + return true; + return this->type == type; + } + [[nodiscard]] constexpr bool contains_type(std::u32string_view types) const noexcept {