Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4d1d7f

Browse files
committed
Improve debug codegen
1 parent 1e0771c commit b4d1d7f

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

include/fmt/base.h

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@
7878
#else
7979
# define FMT_HAS_INCLUDE(x) 0
8080
#endif
81+
#ifdef __has_builtin
82+
# define FMT_HAS_BUILTIN(x) __has_builtin(x)
83+
#else
84+
# define FMT_HAS_BUILTIN(x) 0
85+
#endif
8186
#ifdef __has_cpp_attribute
8287
# define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
8388
#else
@@ -455,19 +460,16 @@ constexpr auto is_utf8_enabled() -> bool { return "\u00A7"[1] == '\xA7'; }
455460
// It is a macro for better debug codegen without if constexpr.
456461
#define FMT_USE_UTF8 (!FMT_MSC_VERSION || fmt::detail::is_utf8_enabled())
457462

463+
template <typename T> constexpr const char* narrow(const T*) { return nullptr; }
464+
constexpr FMT_ALWAYS_INLINE const char* narrow(const char* s) { return s; }
465+
458466
#ifndef FMT_UNICODE
459467
# define FMT_UNICODE 1
460468
#endif
461469

462470
static_assert(!FMT_UNICODE || FMT_USE_UTF8,
463471
"Unicode support requires compiling with /utf-8");
464472

465-
template <typename Char> FMT_CONSTEXPR auto length(const Char* s) -> size_t {
466-
size_t len = 0;
467-
while (*s++) ++len;
468-
return len;
469-
}
470-
471473
template <typename Char>
472474
FMT_CONSTEXPR auto compare(const Char* s1, const Char* s2, std::size_t n)
473475
-> int {
@@ -536,13 +538,20 @@ template <typename Char> class basic_string_view {
536538
constexpr basic_string_view(std::nullptr_t) = delete;
537539

538540
/// Constructs a string reference object from a C string.
539-
FMT_CONSTEXPR20
540-
basic_string_view(const Char* s)
541-
: data_(s),
542-
size_(detail::const_check(std::is_same<Char, char>::value &&
543-
!detail::is_constant_evaluated(false))
544-
? strlen(reinterpret_cast<const char*>(s))
545-
: detail::length(s)) {}
541+
#if FMT_GCC_VERSION
542+
FMT_ALWAYS_INLINE
543+
#endif
544+
FMT_CONSTEXPR20 basic_string_view(const Char* s) : data_(s) {
545+
#if FMT_HAS_BUILTIN(__buitin_strlen) || FMT_GCC_VERSION || FMT_CLANG_VERSION
546+
if (std::is_same<Char, char>::value) {
547+
size_ = __builtin_strlen(detail::narrow(s));
548+
return;
549+
}
550+
#endif
551+
size_t len = 0;
552+
while (*s++) ++len;
553+
size_ = len;
554+
}
546555

547556
/// Constructs a string reference from a `std::basic_string` or a
548557
/// `std::basic_string_view` object.

include/fmt/format.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@
8181
# define FMT_SO_VISIBILITY(value)
8282
#endif
8383

84-
#ifdef __has_builtin
85-
# define FMT_HAS_BUILTIN(x) __has_builtin(x)
86-
#else
87-
# define FMT_HAS_BUILTIN(x) 0
88-
#endif
89-
9084
#if FMT_GCC_VERSION || FMT_CLANG_VERSION
9185
# define FMT_NOINLINE __attribute__((noinline))
9286
#else

0 commit comments

Comments
 (0)