From 8978ab09b2577c374494d55c574b72d317f908a3 Mon Sep 17 00:00:00 2001 From: Barry Revzin Date: Sun, 27 Apr 2025 11:32:10 -0500 Subject: [PATCH] Avoiding __builtin_strlen (#4429) --- include/fmt/base.h | 2 +- test/base-test.cc | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 39ef747c..3395e4ab 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -539,7 +539,7 @@ template class basic_string_view { #endif FMT_CONSTEXPR20 basic_string_view(const Char* s) : data_(s) { #if FMT_HAS_BUILTIN(__builtin_strlen) || FMT_GCC_VERSION || FMT_CLANG_VERSION - if (std::is_same::value) { + if (std::is_same::value && !detail::is_constant_evaluated()) { size_ = __builtin_strlen(detail::narrow(s)); return; } diff --git a/test/base-test.cc b/test/base-test.cc index f8af275d..0d8ae91b 100644 --- a/test/base-test.cc +++ b/test/base-test.cc @@ -92,6 +92,29 @@ TEST(string_view_test, compare) { check_op(); } +#if FMT_USE_CONSTEVAL +namespace { + +template +struct fixed_string { + char data[N] = {}; + + constexpr fixed_string(char const (&m)[N]) { + for (size_t i = 0; i != N; ++i) { + data[i] = m[i]; + } + } +}; + +} + +TEST(string_view_test, from_constexpr_fixed_string) { + static constexpr auto fs = fixed_string<5>("x={}"); + static constexpr auto fmt = fmt::string_view(fs.data); + EXPECT_EQ(fmt, "x={}"); +} +#endif + TEST(base_test, is_locking) { EXPECT_FALSE(fmt::detail::is_locking()); }