Skip to content

Commit 5cfca8e

Browse files
committed
Avoiding __builtin_strlen
1 parent 41b3bed commit 5cfca8e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

include/fmt/base.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ template <typename Char> class basic_string_view {
539539
#endif
540540
FMT_CONSTEXPR20 basic_string_view(const Char* s) : data_(s) {
541541
#if FMT_HAS_BUILTIN(__builtin_strlen) || FMT_GCC_VERSION || FMT_CLANG_VERSION
542-
if (std::is_same<Char, char>::value) {
542+
if (std::is_same<Char, char>::value && !detail::is_constant_evaluated()) {
543543
size_ = __builtin_strlen(detail::narrow(s));
544544
return;
545545
}

test/format-test.cc

+20
Original file line numberDiff line numberDiff line change
@@ -2563,3 +2563,23 @@ auto fmt::formatter<incomplete_type>::format(const incomplete_type&,
25632563
-> fmt::appender {
25642564
return formatter<int>::format(42, ctx);
25652565
}
2566+
2567+
namespace {
2568+
2569+
template <size_t N>
2570+
struct fixed_string {
2571+
char data[N] = {};
2572+
2573+
constexpr fixed_string(char const (&m)[N]) {
2574+
for (size_t i = 0; i != N; ++i) {
2575+
data[i] = m[i];
2576+
}
2577+
}
2578+
};
2579+
2580+
}
2581+
2582+
TEST(format_test, fixed_string_constant) {
2583+
static constexpr auto f = fixed_string<5>("x={}");
2584+
EXPECT_EQ(fmt::format(f.data, 42), "x=42");
2585+
}

0 commit comments

Comments
 (0)