|
78 | 78 | #else |
79 | 79 | # define FMT_HAS_INCLUDE(x) 0 |
80 | 80 | #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 |
81 | 86 | #ifdef __has_cpp_attribute |
82 | 87 | # define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) |
83 | 88 | #else |
@@ -455,19 +460,16 @@ constexpr auto is_utf8_enabled() -> bool { return "\u00A7"[1] == '\xA7'; } |
455 | 460 | // It is a macro for better debug codegen without if constexpr. |
456 | 461 | #define FMT_USE_UTF8 (!FMT_MSC_VERSION || fmt::detail::is_utf8_enabled()) |
457 | 462 |
|
| 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 | + |
458 | 466 | #ifndef FMT_UNICODE |
459 | 467 | # define FMT_UNICODE 1 |
460 | 468 | #endif |
461 | 469 |
|
462 | 470 | static_assert(!FMT_UNICODE || FMT_USE_UTF8, |
463 | 471 | "Unicode support requires compiling with /utf-8"); |
464 | 472 |
|
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 | | - |
471 | 473 | template <typename Char> |
472 | 474 | FMT_CONSTEXPR auto compare(const Char* s1, const Char* s2, std::size_t n) |
473 | 475 | -> int { |
@@ -536,13 +538,20 @@ template <typename Char> class basic_string_view { |
536 | 538 | constexpr basic_string_view(std::nullptr_t) = delete; |
537 | 539 |
|
538 | 540 | /// 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 | + } |
546 | 555 |
|
547 | 556 | /// Constructs a string reference from a `std::basic_string` or a |
548 | 557 | /// `std::basic_string_view` object. |
|
0 commit comments