From b4d1d7f8e6719a928f6eea0786dec296dc64b123 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 4 Sep 2024 08:21:18 -0700 Subject: [PATCH] Improve debug codegen --- include/fmt/base.h | 35 ++++++++++++++++++++++------------- include/fmt/format.h | 6 ------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index f2e2b109..64c6a07b 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -78,6 +78,11 @@ #else # define FMT_HAS_INCLUDE(x) 0 #endif +#ifdef __has_builtin +# define FMT_HAS_BUILTIN(x) __has_builtin(x) +#else +# define FMT_HAS_BUILTIN(x) 0 +#endif #ifdef __has_cpp_attribute # define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) #else @@ -455,6 +460,9 @@ constexpr auto is_utf8_enabled() -> bool { return "\u00A7"[1] == '\xA7'; } // It is a macro for better debug codegen without if constexpr. #define FMT_USE_UTF8 (!FMT_MSC_VERSION || fmt::detail::is_utf8_enabled()) +template constexpr const char* narrow(const T*) { return nullptr; } +constexpr FMT_ALWAYS_INLINE const char* narrow(const char* s) { return s; } + #ifndef FMT_UNICODE # define FMT_UNICODE 1 #endif @@ -462,12 +470,6 @@ constexpr auto is_utf8_enabled() -> bool { return "\u00A7"[1] == '\xA7'; } static_assert(!FMT_UNICODE || FMT_USE_UTF8, "Unicode support requires compiling with /utf-8"); -template FMT_CONSTEXPR auto length(const Char* s) -> size_t { - size_t len = 0; - while (*s++) ++len; - return len; -} - template FMT_CONSTEXPR auto compare(const Char* s1, const Char* s2, std::size_t n) -> int { @@ -536,13 +538,20 @@ template class basic_string_view { constexpr basic_string_view(std::nullptr_t) = delete; /// Constructs a string reference object from a C string. - FMT_CONSTEXPR20 - basic_string_view(const Char* s) - : data_(s), - size_(detail::const_check(std::is_same::value && - !detail::is_constant_evaluated(false)) - ? strlen(reinterpret_cast(s)) - : detail::length(s)) {} +#if FMT_GCC_VERSION + FMT_ALWAYS_INLINE +#endif + FMT_CONSTEXPR20 basic_string_view(const Char* s) : data_(s) { +#if FMT_HAS_BUILTIN(__buitin_strlen) || FMT_GCC_VERSION || FMT_CLANG_VERSION + if (std::is_same::value) { + size_ = __builtin_strlen(detail::narrow(s)); + return; + } +#endif + size_t len = 0; + while (*s++) ++len; + size_ = len; + } /// Constructs a string reference from a `std::basic_string` or a /// `std::basic_string_view` object. diff --git a/include/fmt/format.h b/include/fmt/format.h index 122b62b5..c0a82ae8 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -81,12 +81,6 @@ # define FMT_SO_VISIBILITY(value) #endif -#ifdef __has_builtin -# define FMT_HAS_BUILTIN(x) __has_builtin(x) -#else -# define FMT_HAS_BUILTIN(x) 0 -#endif - #if FMT_GCC_VERSION || FMT_CLANG_VERSION # define FMT_NOINLINE __attribute__((noinline)) #else