Avoiding __builtin_strlen (#4429)

This commit is contained in:
Barry Revzin
2025-04-27 11:32:10 -05:00
committed by GitHub
parent c936e2e44e
commit 8978ab09b2
2 changed files with 24 additions and 1 deletions

View File

@ -539,7 +539,7 @@ template <typename Char> 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<Char, char>::value) {
if (std::is_same<Char, char>::value && !detail::is_constant_evaluated()) {
size_ = __builtin_strlen(detail::narrow(s));
return;
}

View File

@ -92,6 +92,29 @@ TEST(string_view_test, compare) {
check_op<std::greater_equal>();
}
#if FMT_USE_CONSTEVAL
namespace {
template <size_t N>
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<const char(&)[3]>());
}