From c9287eb9f7f88e94f7b0ae36a7969b2449df08e6 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 3 Jan 2024 09:59:44 -0800 Subject: [PATCH] Reduce char_traits usage --- include/fmt/core.h | 27 ++++++++++++++++----------- include/fmt/format.h | 8 ++++---- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 297cb34e..af4510bf 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -8,10 +8,10 @@ #ifndef FMT_CORE_H_ #define FMT_CORE_H_ -#include // std::byte -#include // std::FILE -#include // std::strlen -#include // std::numeric_limits +#include // std::byte +#include // std::FILE +#include // std::strlen +#include // std::numeric_limits #include #include @@ -445,6 +445,12 @@ FMT_CONSTEXPR inline auto is_utf8() -> bool { return FMT_UNICODE || (sizeof(section) == 3 && uchar(section[0]) == 0xC2 && uchar(section[1]) == 0xA7); } + +template FMT_CONSTEXPR auto length(const Char* s) -> size_t { + size_t len = 0; + while (*s++) ++len; + return len; +} } // namespace detail /** @@ -476,14 +482,14 @@ template class basic_string_view { the size with ``std::char_traits::length``. \endrst */ - FMT_CONSTEXPR_CHAR_TRAITS + FMT_CONSTEXPR20 FMT_INLINE basic_string_view(const Char* s) : data_(s), size_(detail::const_check(std::is_same::value && - !detail::is_constant_evaluated(true)) + !detail::is_constant_evaluated(false)) ? std::strlen(reinterpret_cast(s)) - : std::char_traits::length(s)) {} + : detail::length(s)) {} /** Constructs a string reference from a ``std::basic_string`` object. */ template @@ -519,8 +525,8 @@ template class basic_string_view { return size_ >= sv.size_ && std::char_traits::compare(data_, sv.data_, sv.size_) == 0; } - FMT_CONSTEXPR_CHAR_TRAITS auto starts_with(Char c) const noexcept -> bool { - return size_ >= 1 && std::char_traits::eq(*data_, c); + FMT_CONSTEXPR auto starts_with(Char c) const noexcept -> bool { + return size_ >= 1 && *data_ == c; } FMT_CONSTEXPR_CHAR_TRAITS auto starts_with(const Char* s) const -> bool { return starts_with(basic_string_view(s)); @@ -1544,8 +1550,7 @@ template <> struct is_output_iterator : std::true_type {}; template struct is_output_iterator< - It, T, - void_t()++ = std::declval())>> + It, T, void_t()++ = std::declval())>> : std::true_type {}; template struct is_back_insert_iterator : std::false_type {}; diff --git a/include/fmt/format.h b/include/fmt/format.h index 196ccc01..8f77d600 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -38,10 +38,10 @@ #include // std::memcpy #include // std::initializer_list #include -#include // std::numeric_limits -#include // std::uninitialized_copy -#include // std::runtime_error -#include // std::system_error +#include // std::numeric_limits +#include // std::uninitialized_copy +#include // std::runtime_error +#include // std::system_error #ifdef __cpp_lib_bit_cast # include // std::bit_cast