Skip to content

Commit

Permalink
Improve debug codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Sep 4, 2024
1 parent 1e0771c commit b4d1d7f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
35 changes: 22 additions & 13 deletions include/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -455,19 +460,16 @@ 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 <typename T> 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

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

template <typename Char> FMT_CONSTEXPR auto length(const Char* s) -> size_t {
size_t len = 0;
while (*s++) ++len;
return len;
}

template <typename Char>
FMT_CONSTEXPR auto compare(const Char* s1, const Char* s2, std::size_t n)
-> int {
Expand Down Expand Up @@ -536,13 +538,20 @@ template <typename Char> 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<Char, char>::value &&
!detail::is_constant_evaluated(false))
? strlen(reinterpret_cast<const char*>(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<Char, char>::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.
Expand Down
6 changes: 0 additions & 6 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b4d1d7f

Please sign in to comment.