diff options
Diffstat (limited to 'externals/fmt/include/fmt/format.h')
-rw-r--r-- | externals/fmt/include/fmt/format.h | 1958 |
1 files changed, 1138 insertions, 820 deletions
diff --git a/externals/fmt/include/fmt/format.h b/externals/fmt/include/fmt/format.h index 0bd2fdb1..87a34b97 100644 --- a/externals/fmt/include/fmt/format.h +++ b/externals/fmt/include/fmt/format.h @@ -33,13 +33,14 @@ #ifndef FMT_FORMAT_H_ #define FMT_FORMAT_H_ -#include <cmath> // std::signbit -#include <cstdint> // uint32_t -#include <cstring> // std::memcpy -#include <limits> // std::numeric_limits -#include <memory> // std::uninitialized_copy -#include <stdexcept> // std::runtime_error -#include <system_error> // std::system_error +#include <cmath> // std::signbit +#include <cstdint> // uint32_t +#include <cstring> // std::memcpy +#include <initializer_list> // std::initializer_list +#include <limits> // std::numeric_limits +#include <memory> // std::uninitialized_copy +#include <stdexcept> // std::runtime_error +#include <system_error> // std::system_error #ifdef __cpp_lib_bit_cast # include <bit> // std::bitcast @@ -47,16 +48,55 @@ #include "core.h" -#if FMT_GCC_VERSION -# define FMT_GCC_VISIBILITY_HIDDEN __attribute__((visibility("hidden"))) +#if defined __cpp_inline_variables && __cpp_inline_variables >= 201606L +# define FMT_INLINE_VARIABLE inline #else -# define FMT_GCC_VISIBILITY_HIDDEN +# define FMT_INLINE_VARIABLE #endif -#ifdef __NVCC__ -# define FMT_CUDA_VERSION (__CUDACC_VER_MAJOR__ * 100 + __CUDACC_VER_MINOR__) +#if FMT_HAS_CPP17_ATTRIBUTE(fallthrough) +# define FMT_FALLTHROUGH [[fallthrough]] +#elif defined(__clang__) +# define FMT_FALLTHROUGH [[clang::fallthrough]] +#elif FMT_GCC_VERSION >= 700 && \ + (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 520) +# define FMT_FALLTHROUGH [[gnu::fallthrough]] #else -# define FMT_CUDA_VERSION 0 +# define FMT_FALLTHROUGH +#endif + +#ifndef FMT_DEPRECATED +# if FMT_HAS_CPP14_ATTRIBUTE(deprecated) || FMT_MSC_VERSION >= 1900 +# define FMT_DEPRECATED [[deprecated]] +# else +# if (defined(__GNUC__) && !defined(__LCC__)) || defined(__clang__) +# define FMT_DEPRECATED __attribute__((deprecated)) +# elif FMT_MSC_VERSION +# define FMT_DEPRECATED __declspec(deprecated) +# else +# define FMT_DEPRECATED /* deprecated */ +# endif +# endif +#endif + +#ifndef FMT_NO_UNIQUE_ADDRESS +# if FMT_CPLUSPLUS >= 202002L +# if FMT_HAS_CPP_ATTRIBUTE(no_unique_address) +# define FMT_NO_UNIQUE_ADDRESS [[no_unique_address]] +// VS2019 v16.10 and later except clang-cl (https://reviews.llvm.org/D110485) +# elif (FMT_MSC_VERSION >= 1929) && !FMT_CLANG_VERSION +# define FMT_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]] +# endif +# endif +#endif +#ifndef FMT_NO_UNIQUE_ADDRESS +# define FMT_NO_UNIQUE_ADDRESS +#endif + +#if FMT_GCC_VERSION || defined(__clang__) +# define FMT_VISIBILITY(value) __attribute__((visibility(value))) +#else +# define FMT_VISIBILITY(value) #endif #ifdef __has_builtin @@ -71,12 +111,6 @@ # define FMT_NOINLINE #endif -#if FMT_MSC_VERSION -# define FMT_MSC_DEFAULT = default -#else -# define FMT_MSC_DEFAULT -#endif - #ifndef FMT_THROW # if FMT_EXCEPTIONS # if FMT_MSC_VERSION || defined(__NVCC__) @@ -95,10 +129,8 @@ FMT_END_NAMESPACE # define FMT_THROW(x) throw x # endif # else -# define FMT_THROW(x) \ - do { \ - FMT_ASSERT(false, (x).what()); \ - } while (false) +# define FMT_THROW(x) \ + ::fmt::detail::assert_fail(__FILE__, __LINE__, (x).what()) # endif #endif @@ -200,7 +232,8 @@ inline auto clzll(uint64_t x) -> int { _BitScanReverse64(&r, x); # else // Scan the high 32 bits. - if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32))) return 63 ^ (r + 32); + if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32))) + return 63 ^ static_cast<int>(r + 32); // Scan the low 32 bits. _BitScanReverse(&r, static_cast<uint32_t>(x)); # endif @@ -240,6 +273,19 @@ FMT_END_NAMESPACE #endif FMT_BEGIN_NAMESPACE + +template <typename...> struct disjunction : std::false_type {}; +template <typename P> struct disjunction<P> : P {}; +template <typename P1, typename... Pn> +struct disjunction<P1, Pn...> + : conditional_t<bool(P1::value), P1, disjunction<Pn...>> {}; + +template <typename...> struct conjunction : std::true_type {}; +template <typename P> struct conjunction<P> : P {}; +template <typename P1, typename... Pn> +struct conjunction<P1, Pn...> + : conditional_t<bool(P1::value), conjunction<Pn...>, P1> {}; + namespace detail { FMT_CONSTEXPR inline void abort_fuzzing_if(bool condition) { @@ -249,6 +295,18 @@ FMT_CONSTEXPR inline void abort_fuzzing_if(bool condition) { #endif } +template <typename CharT, CharT... C> struct string_literal { + static constexpr CharT value[sizeof...(C)] = {C...}; + constexpr operator basic_string_view<CharT>() const { + return {value, sizeof...(C)}; + } +}; + +#if FMT_CPLUSPLUS < 201703L +template <typename CharT, CharT... C> +constexpr CharT string_literal<CharT, C...>::value[sizeof...(C)]; +#endif + template <typename Streambuf> class formatbuf : public Streambuf { private: using char_type = typename Streambuf::char_type; @@ -287,7 +345,8 @@ FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To { if (is_constant_evaluated()) return std::bit_cast<To>(from); #endif auto to = To(); - std::memcpy(&to, &from, sizeof(to)); + // The cast suppresses a bogus -Wclass-memaccess on GCC. + std::memcpy(static_cast<void*>(&to), &from, sizeof(to)); return to; } @@ -310,8 +369,6 @@ class uint128_fallback { private: uint64_t lo_, hi_; - friend uint128_fallback umul128(uint64_t x, uint64_t y) noexcept; - public: constexpr uint128_fallback(uint64_t hi, uint64_t lo) : lo_(lo), hi_(hi) {} constexpr uint128_fallback(uint64_t value = 0) : lo_(value), hi_(0) {} @@ -346,6 +403,10 @@ class uint128_fallback { -> uint128_fallback { return {lhs.hi_ & rhs.hi_, lhs.lo_ & rhs.lo_}; } + friend constexpr auto operator~(const uint128_fallback& n) + -> uint128_fallback { + return {~n.hi_, ~n.lo_}; + } friend auto operator+(const uint128_fallback& lhs, const uint128_fallback& rhs) -> uint128_fallback { auto result = uint128_fallback(lhs); @@ -366,10 +427,12 @@ class uint128_fallback { } FMT_CONSTEXPR auto operator>>(int shift) const -> uint128_fallback { if (shift == 64) return {0, hi_}; + if (shift > 64) return uint128_fallback(0, hi_) >> (shift - 64); return {hi_ >> shift, (hi_ << (64 - shift)) | (lo_ >> shift)}; } FMT_CONSTEXPR auto operator<<(int shift) const -> uint128_fallback { if (shift == 64) return {lo_, 0}; + if (shift > 64) return uint128_fallback(lo_, 0) << (shift - 64); return {hi_ << shift | (lo_ >> (64 - shift)), (lo_ << shift)}; } FMT_CONSTEXPR auto operator>>=(int shift) -> uint128_fallback& { @@ -382,6 +445,10 @@ class uint128_fallback { lo_ = new_lo; hi_ = new_hi; } + FMT_CONSTEXPR void operator&=(uint128_fallback n) { + lo_ &= n.lo_; + hi_ &= n.hi_; + } FMT_CONSTEXPR20 uint128_fallback& operator+=(uint64_t n) noexcept { if (is_constant_evaluated()) { @@ -389,11 +456,11 @@ class uint128_fallback { hi_ += (lo_ < n ? 1 : 0); return *this; } -#if FMT_HAS_BUILTIN(__builtin_addcll) +#if FMT_HAS_BUILTIN(__builtin_addcll) && !defined(__ibmxl__) unsigned long long carry; lo_ = __builtin_addcll(lo_, n, 0, &carry); hi_ += carry; -#elif FMT_HAS_BUILTIN(__builtin_ia32_addcarryx_u64) +#elif FMT_HAS_BUILTIN(__builtin_ia32_addcarryx_u64) && !defined(__ibmxl__) unsigned long long result; auto carry = __builtin_ia32_addcarryx_u64(0, lo_, n, &result); lo_ = result; @@ -448,10 +515,34 @@ inline auto bit_cast(const From& from) -> To { return result; } +template <typename UInt> +FMT_CONSTEXPR20 inline auto countl_zero_fallback(UInt n) -> int { + int lz = 0; + constexpr UInt msb_mask = static_cast<UInt>(1) << (num_bits<UInt>() - 1); + for (; (n & msb_mask) == 0; n <<= 1) lz++; + return lz; +} + +FMT_CONSTEXPR20 inline auto countl_zero(uint32_t n) -> int { +#ifdef FMT_BUILTIN_CLZ + if (!is_constant_evaluated()) return FMT_BUILTIN_CLZ(n); +#endif + return countl_zero_fallback(n); +} + +FMT_CONSTEXPR20 inline auto countl_zero(uint64_t n) -> int { +#ifdef FMT_BUILTIN_CLZLL + if (!is_constant_evaluated()) return FMT_BUILTIN_CLZLL(n); +#endif + return countl_zero_fallback(n); +} + FMT_INLINE void assume(bool condition) { (void)condition; #if FMT_HAS_BUILTIN(__builtin_assume) && !FMT_ICC_VERSION __builtin_assume(condition); +#elif FMT_GCC_VERSION + if (!condition) __builtin_unreachable(); #endif } @@ -470,20 +561,6 @@ inline auto get_data(Container& c) -> typename Container::value_type* { return c.data(); } -#if defined(_SECURE_SCL) && _SECURE_SCL -// Make a checked iterator to avoid MSVC warnings. -template <typename T> using checked_ptr = stdext::checked_array_iterator<T*>; -template <typename T> -constexpr auto make_checked(T* p, size_t size) -> checked_ptr<T> { - return {p, size}; -} -#else -template <typename T> using checked_ptr = T*; -template <typename T> constexpr auto make_checked(T* p, size_t) -> T* { - return p; -} -#endif - // Attempts to reserve space for n extra characters in the output range. // Returns a pointer to the reserved range or a reference to it. template <typename Container, FMT_ENABLE_IF(is_contiguous<Container>::value)> @@ -491,12 +568,12 @@ template <typename Container, FMT_ENABLE_IF(is_contiguous<Container>::value)> __attribute__((no_sanitize("undefined"))) #endif inline auto -reserve(std::back_insert_iterator<Container> it, size_t n) - -> checked_ptr<typename Container::value_type> { +reserve(std::back_insert_iterator<Container> it, size_t n) -> + typename Container::value_type* { Container& c = get_container(it); size_t size = c.size(); c.resize(size + n); - return make_checked(get_data(c) + size, n); + return get_data(c) + size; } template <typename T> @@ -528,8 +605,8 @@ template <typename T> auto to_pointer(buffer_appender<T> it, size_t n) -> T* { } template <typename Container, FMT_ENABLE_IF(is_contiguous<Container>::value)> -inline auto base_iterator(std::back_insert_iterator<Container>& it, - checked_ptr<typename Container::value_type>) +inline auto base_iterator(std::back_insert_iterator<Container> it, + typename Container::value_type*) -> std::back_insert_iterator<Container> { return it; } @@ -592,19 +669,24 @@ FMT_CONSTEXPR inline auto utf8_decode(const char* s, uint32_t* c, int* e) constexpr const int shiftc[] = {0, 18, 12, 6, 0}; constexpr const int shifte[] = {0, 6, 4, 2, 0}; - int len = code_point_length(s); - const char* next = s + len; + int len = "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0\0\0\2\2\2\2\3\3\4" + [static_cast<unsigned char>(*s) >> 3]; + // Compute the pointer to the next character early so that the next + // iteration can start working on the next character. Neither Clang + // nor GCC figure out this reordering on their own. + const char* next = s + len + !len; + + using uchar = unsigned char; // Assume a four-byte character and load four bytes. Unused bits are // shifted out. - *c = uint32_t(s[0] & masks[len]) << 18; - *c |= uint32_t(s[1] & 0x3f) << 12; - *c |= uint32_t(s[2] & 0x3f) << 6; - *c |= uint32_t(s[3] & 0x3f) << 0; + *c = uint32_t(uchar(s[0]) & masks[len]) << 18; + *c |= uint32_t(uchar(s[1]) & 0x3f) << 12; + *c |= uint32_t(uchar(s[2]) & 0x3f) << 6; + *c |= uint32_t(uchar(s[3]) & 0x3f) << 0; *c >>= shiftc[len]; // Accumulate the various error conditions. - using uchar = unsigned char; *e = (*c < mins[len]) << 6; // non-canonical encoding *e |= ((*c >> 11) == 0x1b) << 7; // surrogate half? *e |= (*c > 0x10FFFF) << 8; // out of range? @@ -617,7 +699,7 @@ FMT_CONSTEXPR inline auto utf8_decode(const char* s, uint32_t* c, int* e) return next; } -constexpr uint32_t invalid_code_point = ~uint32_t(); +constexpr FMT_INLINE_VARIABLE uint32_t invalid_code_point = ~uint32_t(); // Invokes f(cp, sv) for every code point cp in s with sv being the string view // corresponding to the code point. cp is invalid_code_point on error. @@ -628,8 +710,8 @@ FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) { auto error = 0; auto end = utf8_decode(buf_ptr, &cp, &error); bool result = f(error ? invalid_code_point : cp, - string_view(ptr, to_unsigned(end - buf_ptr))); - return result ? end : nullptr; + string_view(ptr, error ? 1 : to_unsigned(end - buf_ptr))); + return result ? (error ? buf_ptr + 1 : end) : nullptr; }; auto p = s.data(); const size_t block_size = 4; // utf8_decode always reads blocks of 4 chars. @@ -687,6 +769,7 @@ FMT_CONSTEXPR inline size_t compute_width(string_view s) { return true; } }; + // We could avoid branches by using utf8_decode directly. for_each_codepoint(s, count_code_points{&num_code_points}); return num_code_points; } @@ -718,13 +801,48 @@ inline auto code_point_index(basic_string_view<char8_type> s, size_t n) string_view(reinterpret_cast<const char*>(s.data()), s.size()), n); } +template <typename T> struct is_integral : std::is_integral<T> {}; +template <> struct is_integral<int128_opt> : std::true_type {}; +template <> struct is_integral<uint128_t> : std::true_type {}; + +template <typename T> +using is_signed = + std::integral_constant<bool, std::numeric_limits<T>::is_signed || + std::is_same<T, int128_opt>::value>; + +template <typename T> +using is_integer = + bool_constant<is_integral<T>::value && !std::is_same<T, bool>::value && + !std::is_same<T, char>::value && + !std::is_same<T, wchar_t>::value>; + +#ifndef FMT_USE_FLOAT +# define FMT_USE_FLOAT 1 +#endif +#ifndef FMT_USE_DOUBLE +# define FMT_USE_DOUBLE 1 +#endif +#ifndef FMT_USE_LONG_DOUBLE +# define FMT_USE_LONG_DOUBLE 1 +#endif + #ifndef FMT_USE_FLOAT128 -# ifdef __SIZEOF_FLOAT128__ -# define FMT_USE_FLOAT128 1 -# else +# ifdef __clang__ +// Clang emulates GCC, so it has to appear early. +# if FMT_HAS_INCLUDE(<quadmath.h>) +# define FMT_USE_FLOAT128 1 +# endif +# elif defined(__GNUC__) +// GNU C++: +# if defined(_GLIBCXX_USE_FLOAT128) && !defined(__STRICT_ANSI__) +# define FMT_USE_FLOAT128 1 +# endif +# endif +# ifndef FMT_USE_FLOAT128 # define FMT_USE_FLOAT128 0 # endif #endif + #if FMT_USE_FLOAT128 using float128 = __float128; #else @@ -756,7 +874,7 @@ void buffer<T>::append(const U* begin, const U* end) { try_reserve(size_ + count); auto free_cap = capacity_ - size_; if (free_cap < count) count = free_cap; - std::uninitialized_copy_n(begin, count, make_checked(ptr_ + size_, count)); + std::uninitialized_copy_n(begin, count, ptr_ + size_); size_ += count; begin += count; } @@ -768,7 +886,7 @@ template <typename T> struct is_locale<T, void_t<decltype(T::classic())>> : std::true_type {}; } // namespace detail -FMT_MODULE_EXPORT_BEGIN +FMT_BEGIN_EXPORT // The number of characters to store in the basic_memory_buffer object itself // to avoid dynamic memory allocation. @@ -801,8 +919,8 @@ class basic_memory_buffer final : public detail::buffer<T> { private: T store_[SIZE]; - // Don't inherit from Allocator avoid generating type_info for it. - Allocator alloc_; + // Don't inherit from Allocator to avoid generating type_info for it. + FMT_NO_UNIQUE_ADDRESS Allocator alloc_; // Deallocate memory allocated by the buffer. FMT_CONSTEXPR20 void deallocate() { @@ -811,7 +929,28 @@ class basic_memory_buffer final : public detail::buffer<T> { } protected: - FMT_CONSTEXPR20 void grow(size_t size) override; + FMT_CONSTEXPR20 void grow(size_t size) override { + detail::abort_fuzzing_if(size > 5000); + const size_t max_size = std::allocator_traits<Allocator>::max_size(alloc_); + size_t old_capacity = this->capacity(); + size_t new_capacity = old_capacity + old_capacity / 2; + if (size > new_capacity) + new_capacity = size; + else if (new_capacity > max_size) + new_capacity = size > max_size ? size : max_size; + T* old_data = this->data(); + T* new_data = + std::allocator_traits<Allocator>::allocate(alloc_, new_capacity); + // Suppress a bogus -Wstringop-overflow in gcc 13.1 (#3481). + detail::assume(this->size() <= new_capacity); + // The following code doesn't throw, so the raw pointer above doesn't leak. + std::uninitialized_copy_n(old_data, this->size(), new_data); + this->set(new_data, new_capacity); + // deallocate must not throw according to the standard, but even if it does, + // the buffer already uses the new storage and will deallocate it in + // destructor. + if (old_data != store_) alloc_.deallocate(old_data, old_capacity); + } public: using value_type = T; @@ -833,8 +972,7 @@ class basic_memory_buffer final : public detail::buffer<T> { size_t size = other.size(), capacity = other.capacity(); if (data == other.store_) { this->set(store_, capacity); - detail::copy_str<T>(other.store_, other.store_ + size, - detail::make_checked(store_, capacity)); + detail::copy_str<T>(other.store_, other.store_ + size, store_); } else { this->set(data, capacity); // Set pointer to the inline array so that delete is not called @@ -888,52 +1026,29 @@ class basic_memory_buffer final : public detail::buffer<T> { } }; -template <typename T, size_t SIZE, typename Allocator> -FMT_CONSTEXPR20 void basic_memory_buffer<T, SIZE, Allocator>::grow( - size_t size) { - detail::abort_fuzzing_if(size > 5000); - const size_t max_size = std::allocator_traits<Allocator>::max_size(alloc_); - size_t old_capacity = this->capacity(); - size_t new_capacity = old_capacity + old_capacity / 2; - if (size > new_capacity) - new_capacity = size; - else if (new_capacity > max_size) - new_capacity = size > max_size ? size : max_size; - T* old_data = this->data(); - T* new_data = - std::allocator_traits<Allocator>::allocate(alloc_, new_capacity); - // The following code doesn't throw, so the raw pointer above doesn't leak. - std::uninitialized_copy(old_data, old_data + this->size(), - detail::make_checked(new_data, new_capacity)); - this->set(new_data, new_capacity); - // deallocate must not throw according to the standard, but even if it does, - // the buffer already uses the new storage and will deallocate it in - // destructor. - if (old_data != store_) alloc_.deallocate(old_data, old_capacity); -} - using memory_buffer = basic_memory_buffer<char>; template <typename T, size_t SIZE, typename Allocator> struct is_contiguous<basic_memory_buffer<T, SIZE, Allocator>> : std::true_type { }; +FMT_END_EXPORT namespace detail { +FMT_API bool write_console(std::FILE* f, string_view text); FMT_API void print(std::FILE*, string_view); -} +} // namespace detail + +FMT_BEGIN_EXPORT -/** A formatting error such as invalid format string. */ -FMT_CLASS_API -class FMT_API format_error : public std::runtime_error { +// Suppress a misleading warning in older versions of clang. +#if FMT_CLANG_VERSION +# pragma clang diagnostic ignored "-Wweak-vtables" +#endif + +/** An error reported from a formatting function. */ +class FMT_VISIBILITY("default") format_error : public std::runtime_error { public: - explicit format_error(const char* message) : std::runtime_error(message) {} - explicit format_error(const std::string& message) - : std::runtime_error(message) {} - format_error(const format_error&) = default; - format_error& operator=(const format_error&) = default; - format_error(format_error&&) = default; - format_error& operator=(format_error&&) = default; - ~format_error() noexcept override FMT_MSC_DEFAULT; + using std::runtime_error::runtime_error; }; namespace detail_exported { @@ -962,16 +1077,52 @@ constexpr auto compile_string_to_view(detail::std_string_view<Char> s) } } // namespace detail_exported -FMT_BEGIN_DETAIL_NAMESPACE +class loc_value { + private: + basic_format_arg<format_context> value_; -template <typename T> struct is_integral : std::is_integral<T> {}; -template <> struct is_integral<int128_opt> : std::true_type {}; -template <> struct is_integral<uint128_t> : std::true_type {}; + public: + template <typename T, FMT_ENABLE_IF(!detail::is_float128<T>::value)> + loc_value(T value) : value_(detail::make_arg<format_context>(value)) {} -template <typename T> -using is_signed = - std::integral_constant<bool, std::numeric_limits<T>::is_signed || - std::is_same<T, int128_opt>::value>; + template <typename T, FMT_ENABLE_IF(detail::is_float128<T>::value)> + loc_value(T) {} + + template <typename Visitor> auto visit(Visitor&& vis) -> decltype(vis(0)) { + return visit_format_arg(vis, value_); + } +}; + +// A locale facet that formats values in UTF-8. +// It is parameterized on the locale to avoid the heavy <locale> include. +template <typename Locale> class format_facet : public Locale::facet { + private: + std::string separator_; + std::string grouping_; + std::string decimal_point_; + + protected: + virtual auto do_put(appender out, loc_value val, + const format_specs<>& specs) const -> bool; + + public: + static FMT_API typename Locale::id id; + + explicit format_facet(Locale& loc); + explicit format_facet(string_view sep = "", + std::initializer_list<unsigned char> g = {3}, + std::string decimal_point = ".") + : separator_(sep.data(), sep.size()), + grouping_(g.begin(), g.end()), + decimal_point_(decimal_point) {} + + auto put(appender out, loc_value val, const format_specs<>& specs) const + -> bool { + return do_put(out, val, specs); + } +}; + +namespace detail { // Returns true if value is negative, false otherwise. // Same as `value < 0` but doesn't produce warnings if T is an unsigned type. @@ -1100,7 +1251,7 @@ FMT_CONSTEXPR auto count_digits(UInt n) -> int { FMT_INLINE auto do_count_digits(uint32_t n) -> int { // An optimization by Kendall Willets from https://bit.ly/3uOIQrB. // This increments the upper 32 bits (log10(T) - 1) when >= T is added. -# define FMT_INC(T) (((sizeof(# T) - 1ull) << 32) - T) +# define FMT_INC(T) (((sizeof(#T) - 1ull) << 32) - T) static constexpr uint64_t table[] = { FMT_INC(0), FMT_INC(0), FMT_INC(0), // 8 FMT_INC(10), FMT_INC(10), FMT_INC(10), // 64 @@ -1213,10 +1364,10 @@ FMT_CONSTEXPR20 auto format_decimal(Char* out, UInt value, int size) template <typename Char, typename UInt, typename Iterator, FMT_ENABLE_IF(!std::is_pointer<remove_cvref_t<Iterator>>::value)> -inline auto format_decimal(Iterator out, UInt value, int size) +FMT_CONSTEXPR inline auto format_decimal(Iterator out, UInt value, int size) -> format_decimal_result<Iterator> { // Buffer is large enough to hold all digits (digits10 + 1). - Char buffer[digits10<UInt>() + 1]; + Char buffer[digits10<UInt>() + 1] = {}; auto end = format_decimal(buffer, value, size).end; return {out, detail::copy_str_noinline<Char>(buffer, end, out)}; } @@ -1236,8 +1387,8 @@ FMT_CONSTEXPR auto format_uint(Char* buffer, UInt value, int num_digits, } template <unsigned BASE_BITS, typename Char, typename It, typename UInt> -inline auto format_uint(It out, UInt value, int num_digits, bool upper = false) - -> It { +FMT_CONSTEXPR inline auto format_uint(It out, UInt value, int num_digits, + bool upper = false) -> It { if (auto ptr = to_pointer<Char>(out, to_unsigned(num_digits))) { format_uint<BASE_BITS>(ptr, value, num_digits, upper); return out; @@ -1261,7 +1412,139 @@ class utf8_to_utf16 { auto str() const -> std::wstring { return {&buffer_[0], size()}; } }; +enum class to_utf8_error_policy { abort, replace }; + +// A converter from UTF-16/UTF-32 (host endian) to UTF-8. +template <typename WChar, typename Buffer = memory_buffer> class to_utf8 { + private: + Buffer buffer_; + + public: + to_utf8() {} + explicit to_utf8(basic_string_view<WChar> s, + to_utf8_error_policy policy = to_utf8_error_policy::abort) { + static_assert(sizeof(WChar) == 2 || sizeof(WChar) == 4, + "Expect utf16 or utf32"); + if (!convert(s, policy)) + FMT_THROW(std::runtime_error(sizeof(WChar) == 2 ? "invalid utf16" + : "invalid utf32")); + } + operator string_view() const { return string_view(&buffer_[0], size()); } + size_t size() const { return buffer_.size() - 1; } + const char* c_str() const { return &buffer_[0]; } + std::string str() const { return std::string(&buffer_[0], size()); } + + // Performs conversion returning a bool instead of throwing exception on + // conversion error. This method may still throw in case of memory allocation + // error. + bool convert(basic_string_view<WChar> s, + to_utf8_error_policy policy = to_utf8_error_policy::abort) { + if (!convert(buffer_, s, policy)) return false; + buffer_.push_back(0); + return true; + } + static bool convert( + Buffer& buf, basic_string_view<WChar> s, + to_utf8_error_policy policy = to_utf8_error_policy::abort) { + for (auto p = s.begin(); p != s.end(); ++p) { + uint32_t c = static_cast<uint32_t>(*p); + if (sizeof(WChar) == 2 && c >= 0xd800 && c <= 0xdfff) { + // Handle a surrogate pair. + ++p; + if (p == s.end() || (c & 0xfc00) != 0xd800 || (*p & 0xfc00) != 0xdc00) { + if (policy == to_utf8_error_policy::abort) return false; + buf.append(string_view("\xEF\xBF\xBD")); + --p; + } else { + c = (c << 10) + static_cast<uint32_t>(*p) - 0x35fdc00; + } + } else if (c < 0x80) { + buf.push_back(static_cast<char>(c)); + } else if (c < 0x800) { + buf.push_back(static_cast<char>(0xc0 | (c >> 6))); + buf.push_back(static_cast<char>(0x80 | (c & 0x3f))); + } else if ((c >= 0x800 && c <= 0xd7ff) || (c >= 0xe000 && c <= 0xffff)) { + buf.push_back(static_cast<char>(0xe0 | (c >> 12))); + buf.push_back(static_cast<char>(0x80 | ((c & 0xfff) >> 6))); + buf.push_back(static_cast<char>(0x80 | (c & 0x3f))); + } else if (c >= 0x10000 && c <= 0x10ffff) { + buf.push_back(static_cast<char>(0xf0 | (c >> 18))); + buf.push_back(static_cast<char>(0x80 | ((c & 0x3ffff) >> 12))); + buf.push_back(static_cast<char>(0x80 | ((c & 0xfff) >> 6))); + buf.push_back(static_cast<char>(0x80 | (c & 0x3f))); + } else { + return false; + } + } + return true; + } +}; + +// Computes 128-bit result of multiplication of two 64-bit unsigned integers. +inline uint128_fallback umul128(uint64_t x, uint64_t y) noexcept { +#if FMT_USE_INT128 + auto p = static_cast<uint128_opt>(x) * static_cast<uint128_opt>(y); + return {static_cast<uint64_t>(p >> 64), static_cast<uint64_t>(p)}; +#elif defined(_MSC_VER) && defined(_M_X64) + auto hi = uint64_t(); + auto lo = _umul128(x, y, &hi); + return {hi, lo}; +#else + const uint64_t mask = static_cast<uint64_t>(max_value<uint32_t>()); + + uint64_t a = x >> 32; + uint64_t b = x & mask; + uint64_t c = y >> 32; + uint64_t d = y & mask; + + uint64_t ac = a * c; + uint64_t bc = b * c; + uint64_t ad = a * d; + uint64_t bd = b * d; + + uint64_t intermediate = (bd >> 32) + (ad & mask) + (bc & mask); + + return {ac + (intermediate >> 32) + (ad >> 32) + (bc >> 32), + (intermediate << 32) + (bd & mask)}; +#endif +} + namespace dragonbox { +// Computes floor(log10(pow(2, e))) for e in [-2620, 2620] using the method from +// https://fmt.dev/papers/Dragonbox.pdf#page=28, section 6.1. +inline int floor_log10_pow2(int e) noexcept { + FMT_ASSERT(e <= 2620 && e >= -2620, "too large exponent"); + static_assert((-1 >> 1) == -1, "right shift is not arithmetic"); + return (e * 315653) >> 20; +} + +inline int floor_log2_pow10(int e) noexcept { + FMT_ASSERT(e <= 1233 && e >= -1233, "too large exponent"); + return (e * 1741647) >> 19; +} + +// Computes upper 64 bits of multiplication of two 64-bit unsigned integers. +inline uint64_t umul128_upper64(uint64_t x, uint64_t y) noexcept { +#if FMT_USE_INT128 + auto p = static_cast<uint128_opt>(x) * static_cast<uint128_opt>(y); + return static_cast<uint64_t>(p >> 64); +#elif defined(_MSC_VER) && defined(_M_X64) + return __umulh(x, y); +#else + return umul128(x, y).high(); +#endif +} + +// Computes upper 128 bits of multiplication of a 64-bit unsigned integer and a +// 128-bit unsigned integer. +inline uint128_fallback umul192_upper128(uint64_t x, + uint128_fallback y) noexcept { + uint128_fallback r = umul128(x, y.high()); + r += umul128_upper64(x, y.low()); + return r; +} + +FMT_API uint128_fallback get_cached_power(int k) noexcept; // Type-specific information that Dragonbox uses. template <typename T, typename Enable = void> struct float_info; @@ -1274,8 +1557,6 @@ template <> struct float_info<float> { static const int small_divisor = 10; static const int min_k = -31; static const int max_k = 46; - static const int divisibility_check_by_5_threshold = 39; - static const int case_fc_pm_half_lower_threshold = -1; static const int shorter_interval_tie_lower_threshold = -35; static const int shorter_interval_tie_upper_threshold = -35; }; @@ -1287,9 +1568,7 @@ template <> struct float_info<double> { static const int big_divisor = 1000; static const int small_divisor = 100; static const int min_k = -292; - static const int max_k = 326; - static const int divisibility_check_by_5_threshold = 86; - static const int case_fc_pm_half_lower_threshold = -2; + static const int max_k = 341; static const int shorter_interval_tie_lower_threshold = -77; static const int shorter_interval_tie_upper_threshold = -77; }; @@ -1336,8 +1615,8 @@ template <typename Float> constexpr int num_significand_bits() { template <typename Float> constexpr auto exponent_mask() -> typename dragonbox::float_info<Float>::carrier_uint { - using uint = typename dragonbox::float_info<Float>::carrier_uint; - return ((uint(1) << dragonbox::float_info<Float>::exponent_bits) - 1) + using float_uint = typename dragonbox::float_info<Float>::carrier_uint; + return ((float_uint(1) << dragonbox::float_info<Float>::exponent_bits) - 1) << num_significand_bits<Float>(); } template <typename Float> constexpr auto exponent_bias() -> int { @@ -1458,154 +1737,31 @@ FMT_CONSTEXPR inline fp operator*(fp x, fp y) { } template <typename T = void> struct basic_data { - // Normalized 64-bit significands of pow(10, k), for k = -348, -340, ..., 340. - // These are generated by support/compute-powers.py. - static constexpr uint64_t pow10_significands[87] = { - 0xfa8fd5a0081c0288, 0xbaaee17fa23ebf76, 0x8b16fb203055ac76, - 0xcf42894a5dce35ea, 0x9a6bb0aa55653b2d, 0xe61acf033d1a45df, - 0xab70fe17c79ac6ca, 0xff77b1fcbebcdc4f, 0xbe5691ef416bd60c, - 0x8dd01fad907ffc3c, 0xd3515c2831559a83, 0x9d71ac8fada6c9b5, - 0xea9c227723ee8bcb, 0xaecc49914078536d, 0x823c12795db6ce57, - 0xc21094364dfb5637, 0x9096ea6f3848984f, 0xd77485cb25823ac7, - 0xa086cfcd97bf97f4, 0xef340a98172aace5, 0xb23867fb2a35b28e, - 0x84c8d4dfd2c63f3b, 0xc5dd44271ad3cdba, 0x936b9fcebb25c996, - 0xdbac6c247d62a584, 0xa3ab66580d5fdaf6, 0xf3e2f893dec3f126, - 0xb5b5ada8aaff80b8, 0x87625f056c7c4a8b, 0xc9bcff6034c13053, - 0x964e858c91ba2655, 0xdff9772470297ebd, 0xa6dfbd9fb8e5b88f, - 0xf8a95fcf88747d94, 0xb94470938fa89bcf, 0x8a08f0f8bf0f156b, - 0xcdb02555653131b6, 0x993fe2c6d07b7fac, 0xe45c10c42a2b3b06, - 0xaa242499697392d3, 0xfd87b5f28300ca0e, 0xbce5086492111aeb, - 0x8cbccc096f5088cc, 0xd1b71758e219652c, 0x9c40000000000000, - 0xe8d4a51000000000, 0xad78ebc5ac620000, 0x813f3978f8940984, - 0xc097ce7bc90715b3, 0x8f7e32ce7bea5c70, 0xd5d238a4abe98068, - 0x9f4f2726179a2245, 0xed63a231d4c4fb27, 0xb0de65388cc8ada8, - 0x83c7088e1aab65db, 0xc45d1df942711d9a, 0x924d692ca61be758, - 0xda01ee641a708dea, 0xa26da3999aef774a, 0xf209787bb47d6b85, - 0xb454e4a179dd1877, 0x865b86925b9bc5c2, 0xc83553c5c8965d3d, - 0x952ab45cfa97a0b3, 0xde469fbd99a05fe3, 0xa59bc234db398c25, - 0xf6c69a72a3989f5c, 0xb7dcbf5354e9bece, 0x88fcf317f22241e2, - 0xcc20ce9bd35c78a5, 0x98165af37b2153df, 0xe2a0b5dc971f303a, - 0xa8d9d1535ce3b396, 0xfb9b7cd9a4a7443c, 0xbb764c4ca7a44410, - 0x8bab8eefb6409c1a, 0xd01fef10a657842c, 0x9b10a4e5e9913129, - 0xe7109bfba19c0c9d, 0xac2820d9623bf429, 0x80444b5e7aa7cf85, - 0xbf21e44003acdd2d, 0x8e679c2f5e44ff8f, 0xd433179d9c8cb841, - 0x9e19db92b4e31ba9, 0xeb96bf6ebadf77d9, 0xaf87023b9bf0ee6b, + // For checking rounding thresholds. + // The kth entry is chosen to be the smallest integer such that the + // upper 32-bits of 10^(k+1) times it is strictly bigger than 5 * 10^k. + static constexpr uint32_t fractional_part_rounding_thresholds[8] = { + 2576980378U, // ceil(2^31 + 2^32/10^1) + 2190433321U, // ceil(2^31 + 2^32/10^2) + 2151778616U, // ceil(2^31 + 2^32/10^3) + 2147913145U, // ceil(2^31 + 2^32/10^4) + 2147526598U, // ceil(2^31 + 2^32/10^5) + 2147487943U, // ceil(2^31 + 2^32/10^6) + 2147484078U, // ceil(2^31 + 2^32/10^7) + 2147483691U // ceil(2^31 + 2^32/10^8) }; - -#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409 -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wnarrowing" -#endif - // Binary exponents of pow(10, k), for k = -348, -340, ..., 340, corresponding - // to significands above. - static constexpr int16_t pow10_exponents[87] = { - -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, -954, - -927, -901, -874, -847, -821, -794, -768, -741, -715, -688, -661, - -635, -608, -582, -555, -529, -502, -475, -449, -422, -396, -369, - -343, -316, -289, -263, -236, -210, -183, -157, -130, -103, -77, - -50, -24, 3, 30, 56, 83, 109, 136, 162, 189, 216, - 242, 269, 295, 322, 348, 375, 402, 428, 455, 481, 508, - 534, 561, 588, 614, 641, 667, 694, 720, 747, 774, 800, - 827, 853, 880, 907, 933, 960, 986, 1013, 1039, 1066}; -#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409 -# pragma GCC diagnostic pop -#endif - - static constexpr uint64_t power_of_10_64[20] = { - 1, FMT_POWERS_OF_10(1ULL), FMT_POWERS_OF_10(1000000000ULL), - 10000000000000000000ULL}; }; - -#if FMT_CPLUSPLUS < 201703L -template <typename T> constexpr uint64_t basic_data<T>::pow10_significands[]; -template <typename T> constexpr int16_t basic_data<T>::pow10_exponents[]; -template <typename T> constexpr uint64_t basic_data<T>::power_of_10_64[]; -#endif - // This is a struct rather than an alias to avoid shadowing warnings in gcc. struct data : basic_data<> {}; -// Returns a cached power of 10 `c_k = c_k.f * pow(2, c_k.e)` such that its -// (binary) exponent satisfies `min_exponent <= c_k.e <= min_exponent + 28`. -FMT_CONSTEXPR inline fp get_cached_power(int min_exponent, - int& pow10_exponent) { - const int shift = 32; - // log10(2) = 0x0.4d104d427de7fbcc... - const int64_t significand = 0x4d104d427de7fbcc; - int index = static_cast<int>( - ((min_exponent + fp::num_significand_bits - 1) * (significand >> shift) + - ((int64_t(1) << shift) - 1)) // ceil - >> 32 // arithmetic shift - ); - // Decimal exponent of the first (smallest) cached power of 10. - const int first_dec_exp = -348; - // Difference between 2 consecutive decimal exponents in cached powers of 10. - const int dec_exp_step = 8; - index = (index - first_dec_exp - 1) / dec_exp_step + 1; - pow10_exponent = first_dec_exp + index * dec_exp_step; - return {data::pow10_significands[index], data::pow10_exponents[index]}; -} - -#ifndef _MSC_VER -# define FMT_SNPRINTF snprintf -#else -FMT_API auto fmt_snprintf(char* buf, size_t size, const char* fmt, ...) -> int; -# define FMT_SNPRINTF fmt_snprintf -#endif // _MSC_VER - -// Formats a floating-point number with snprintf using the hexfloat format. +#if FMT_CPLUSPLUS < 201703L template <typename T> -auto snprintf_float(T value, int precision, float_specs specs, - buffer<char>& buf) -> int { - // Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail. - FMT_ASSERT(buf.capacity() > buf.size(), "empty buffer"); - FMT_ASSERT(specs.format == float_format::hex, ""); - static_assert(!std::is_same<T, float>::value, ""); - - // Build the format string. - char format[7]; // The longest format is "%#.*Le". - char* format_ptr = format; - *format_ptr++ = '%'; - if (specs.showpoint) *format_ptr++ = '#'; - if (precision >= 0) { - *format_ptr++ = '.'; - *format_ptr++ = '*'; - } - if (std::is_same<T, long double>()) *format_ptr++ = 'L'; - *format_ptr++ = specs.upper ? 'A' : 'a'; - *format_ptr = '\0'; - - // Format using snprintf. - auto offset = buf.size(); - for (;;) { - auto begin = buf.data() + offset; - auto capacity = buf.capacity() - offset; - abort_fuzzing_if(precision > 100000); - // Suppress the warning about a nonliteral format string. - // Cannot use auto because of a bug in MinGW (#1532). - int (*snprintf_ptr)(char*, size_t, const char*, ...) = FMT_SNPRINTF; - int result = precision >= 0 - ? snprintf_ptr(begin, capacity, format, precision, value) - : snprintf_ptr(begin, capacity, format, value); - if (result < 0) { - // The buffer will grow exponentially. - buf.try_reserve(buf.capacity() + 1); - continue; - } - auto size = to_unsigned(result); - // Size equal to capacity means that the last character was truncated. - if (size < capacity) { - buf.try_resize(size + offset); - return 0; - } - buf.try_reserve(size + offset + 1); // Add 1 for the terminating '\0'. - } -} +constexpr uint32_t basic_data<T>::fractional_part_rounding_thresholds[]; +#endif -template <typename T> +template <typename T, bool doublish = num_bits<T>() == num_bits<double>()> using convert_float_result = - conditional_t<std::is_same<T, float>::value || sizeof(T) == sizeof(double), - double, T>; + conditional_t<std::is_same<T, float>::value || doublish, double, T>; template <typename T> constexpr auto convert_float(T value) -> convert_float_result<T> { @@ -1628,8 +1784,7 @@ FMT_NOINLINE FMT_CONSTEXPR auto fill(OutputIt it, size_t n, // width: output display width in (terminal) column positions. template <align::type align = align::left, typename OutputIt, typename Char, typename F> -FMT_CONSTEXPR auto write_padded(OutputIt out, - const basic_format_specs<Char>& specs, +FMT_CONSTEXPR auto write_padded(OutputIt out, const format_specs<Char>& specs, size_t size, size_t width, F&& f) -> OutputIt { static_assert(align == align::left || align == align::right, ""); unsigned spec_width = to_unsigned(specs.width); @@ -1648,15 +1803,14 @@ FMT_CONSTEXPR auto write_padded(OutputIt out, template <align::type align = align::left, typename OutputIt, typename Char, typename F> -constexpr auto write_padded(OutputIt out, const basic_format_specs<Char>& specs, +constexpr auto write_padded(OutputIt out, const format_specs<Char>& specs, size_t size, F&& f) -> OutputIt { return write_padded<align>(out, specs, size, size, f); } template <align::type align = align::left, typename Char, typename OutputIt> FMT_CONSTEXPR auto write_bytes(OutputIt out, string_view bytes, - const basic_format_specs<Char>& specs) - -> OutputIt { + const format_specs<Char>& specs) -> OutputIt { return write_padded<align>( out, specs, bytes.size(), [bytes](reserve_iterator<OutputIt> it) { const char* data = bytes.data(); @@ -1665,8 +1819,8 @@ FMT_CONSTEXPR auto write_bytes(OutputIt out, string_view bytes, } template <typename Char, typename OutputIt, typename UIntPtr> -auto write_ptr(OutputIt out, UIntPtr value, - const basic_format_specs<Char>* specs) -> OutputIt { +auto write_ptr(OutputIt out, UIntPtr value, const format_specs<Char>* specs) + -> OutputIt { int num_digits = count_digits<4>(value); auto size = to_unsigned(num_digits) + size_t(2); auto write = [=](reserve_iterator<OutputIt> it) { @@ -1724,18 +1878,18 @@ inline auto find_escape(const char* begin, const char* end) return result; } -#define FMT_STRING_IMPL(s, base, explicit) \ - [] { \ - /* Use the hidden visibility as a workaround for a GCC bug (#1973). */ \ - /* Use a macro-like name to avoid shadowing warnings. */ \ - struct FMT_GCC_VISIBILITY_HIDDEN FMT_COMPILE_STRING : base { \ - using char_type = fmt::remove_cvref_t<decltype(s[0])>; \ - FMT_MAYBE_UNUSED FMT_CONSTEXPR explicit \ - operator fmt::basic_string_view<char_type>() const { \ - return fmt::detail_exported::compile_string_to_view<char_type>(s); \ - } \ - }; \ - return FMT_COMPILE_STRING(); \ +#define FMT_STRING_IMPL(s, base, explicit) \ + [] { \ + /* Use the hidden visibility as a workaround for a GCC bug (#1973). */ \ + /* Use a macro-like name to avoid shadowing warnings. */ \ + struct FMT_VISIBILITY("hidden") FMT_COMPILE_STRING : base { \ + using char_type FMT_MAYBE_UNUSED = fmt::remove_cvref_t<decltype(s[0])>; \ + FMT_MAYBE_UNUSED FMT_CONSTEXPR explicit \ + operator fmt::basic_string_view<char_type>() const { \ + return fmt::detail_exported::compile_string_to_view<char_type>(s); \ + } \ + }; \ + return FMT_COMPILE_STRING(); \ }() /** @@ -1785,16 +1939,14 @@ auto write_escaped_cp(OutputIt out, const find_escape_result<Char>& escape) *out++ = static_cast<Char>('\\'); break; default: - if (is_utf8()) { - if (escape.cp < 0x100) { - return write_codepoint<2, Char>(out, 'x', escape.cp); - } - if (escape.cp < 0x10000) { - return write_codepoint<4, Char>(out, 'u', escape.cp); - } - if (escape.cp < 0x110000) { - return write_codepoint<8, Char>(out, 'U', escape.cp); - } + if (escape.cp < 0x100) { + return write_codepoint<2, Char>(out, 'x', escape.cp); + } + if (escape.cp < 0x10000) { + return write_codepoint<4, Char>(out, 'u', escape.cp); + } + if (escape.cp < 0x110000) { + return write_codepoint<8, Char>(out, 'U', escape.cp); } for (Char escape_char : basic_string_view<Char>( escape.begin, to_unsigned(escape.end - escape.begin))) { @@ -1839,8 +1991,7 @@ auto write_escaped_char(OutputIt out, Char v) -> OutputIt { template <typename Char, typename OutputIt> FMT_CONSTEXPR auto write_char(OutputIt out, Char value, - const basic_format_specs<Char>& specs) - -> OutputIt { + const format_specs<Char>& specs) -> OutputIt { bool is_debug = specs.type == presentation_type::debug; return write_padded(out, specs, 1, [=](reserve_iterator<OutputIt> it) { if (is_debug) return write_escaped_char(it, value); @@ -1850,11 +2001,14 @@ FMT_CONSTEXPR auto write_char(OutputIt out, Char value, } template <typename Char, typename OutputIt> FMT_CONSTEXPR auto write(OutputIt out, Char value, - const basic_format_specs<Char>& specs, - locale_ref loc = {}) -> OutputIt { + const format_specs<Char>& specs, locale_ref loc = {}) + -> OutputIt { + // char is formatted as unsigned char for consistency across platforms. + using unsigned_type = + conditional_t<std::is_same<Char, char>::value, unsigned char, unsigned>; return check_char_specs(specs) ? write_char(out, value, specs) - : write(out, static_cast<int>(value), specs, loc); + : write(out, static_cast<unsigned_type>(value), specs, loc); } // Data for write_int that doesn't depend on output iterator type. It is used to @@ -1864,7 +2018,7 @@ template <typename Char> struct write_int_data { size_t padding; FMT_CONSTEXPR write_int_data(int num_digits, unsigned prefix, - const basic_format_specs<Char>& specs) + const format_specs<Char>& specs) : size((prefix >> 24) + to_unsigned(num_digits)), padding(0) { if (specs.align == align::numeric) { auto width = to_unsigned(specs.width); @@ -1886,7 +2040,7 @@ template <typename Char> struct write_int_data { template <typename OutputIt, typename Char, typename W> FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, int num_digits, unsigned prefix, - const basic_format_specs<Char>& specs, + const format_specs<Char>& specs, W write_digits) -> OutputIt { // Slightly faster check for specs.width == 0 && specs.precision == -1. if ((specs.width | (specs.precision + 1)) == 0) { @@ -1909,19 +2063,19 @@ FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, int num_digits, template <typename Char> class digit_grouping { private: - thousands_sep_result<Char> sep_; + std::string grouping_; + std::basic_string<Char> thousands_sep_; struct next_state { std::string::const_iterator group; int pos; }; - next_state initial_state() const { return {sep_.grouping.begin(), 0}; } + next_state initial_state() const { return {grouping_.begin(), 0}; } // Returns the next digit group separator position. int next(next_state& state) const { - if (!sep_.thousands_sep) return max_value<int>(); - if (state.group == sep_.grouping.end()) - return state.pos += sep_.grouping.back(); + if (thousands_sep_.empty()) return max_value<int>(); + if (state.group == grouping_.end()) return state.pos += grouping_.back(); if (*state.group <= 0 || *state.group == max_value<char>()) return max_value<int>(); state.pos += *state.group++; @@ -1930,14 +2084,15 @@ template <typename Char> class digit_grouping { public: explicit digit_grouping(locale_ref loc, bool localized = true) { - if (localized) - sep_ = thousands_sep<Char>(loc); - else - sep_.thousands_sep = Char(); + if (!localized) return; + auto sep = thousands_sep<Char>(loc); + grouping_ = sep.grouping; + if (sep.thousands_sep) thousands_sep_.assign(1, sep.thousands_sep); } - explicit digit_grouping(thousands_sep_result<Char> sep) : sep_(sep) {} + digit_grouping(std::string grouping, std::basic_string<Char> sep) + : grouping_(std::move(grouping)), thousands_sep_(std::move(sep)) {} - Char separator() const { return sep_.thousands_sep; } + bool has_separator() const { return !thousands_sep_.empty(); } int count_separators(int num_digits) const { int count = 0; @@ -1960,7 +2115,9 @@ template <typename Char> class digit_grouping { for (int i = 0, sep_index = static_cast<int>(separators.size() - 1); i < num_digits; ++i) { if (num_digits - i == separators[sep_index]) { - *out++ = separator(); + out = + copy_str<Char>(thousands_sep_.data(), + thousands_sep_.data() + thousands_sep_.size(), out); --sep_index; } *out++ = static_cast<Char>(digits[to_unsigned(i)]); @@ -1969,10 +2126,11 @@ template <typename Char> class digit_grouping { } }; +// Writes a decimal integer with digit grouping. template <typename OutputIt, typename UInt, typename Char> -auto write_int_localized(OutputIt out, UInt value, unsigned prefix, - const basic_format_specs<Char>& specs, - const digit_grouping<Char>& grouping) -> OutputIt { +auto write_int(OutputIt out, UInt value, unsigned prefix, + const format_specs<Char>& specs, + const digit_grouping<Char>& grouping) -> OutputIt { static_assert(std::is_same<uint64_or_128_t<UInt>, UInt>::value, ""); int num_digits = count_digits(value); char digits[40]; @@ -1981,18 +2139,21 @@ auto write_int_localized(OutputIt out, UInt value, unsigned prefix, grouping.count_separators(num_digits)); return write_padded<align::right>( out, specs, size, size, [&](reserve_iterator<OutputIt> it) { - if (prefix != 0) *it++ = static_cast<Char>(prefix); + if (prefix != 0) { + char sign = static_cast<char>(prefix); + *it++ = static_cast<Char>(sign); + } return grouping.apply(it, string_view(digits, to_unsigned(num_digits))); }); } -template <typename OutputIt, typename UInt, typename Char> -auto write_int_localized(OutputIt& out, UInt value, unsigned prefix, - const basic_format_specs<Char>& specs, locale_ref loc) - -> bool { - auto grouping = digit_grouping<Char>(loc); - out = write_int_localized(out, value, prefix, specs, grouping); - return true; +// Writes a localized value. +FMT_API auto write_loc(appender out, loc_value value, + const format_specs<>& specs, locale_ref loc) -> bool; +template <typename OutputIt, typename Char> +inline auto write_loc(OutputIt, loc_value, const format_specs<Char>&, + locale_ref) -> bool { + return false; } FMT_CONSTEXPR inline void prefix_append(unsigned& prefix, unsigned value) { @@ -2021,21 +2182,37 @@ FMT_CONSTEXPR auto make_write_int_arg(T value, sign_t sign) return {abs_value, prefix}; } +template <typename Char = char> struct loc_writer { + buffer_appender<Char> out; + const format_specs<Char>& specs; + std::basic_string<Char> sep; + std::string grouping; + std::basic_string<Char> decimal_point; + + template <typename T, FMT_ENABLE_IF(is_integer<T>::value)> + auto operator()(T value) -> bool { + auto arg = make_write_int_arg(value, specs.sign); + write_int(out, static_cast<uint64_or_128_t<T>>(arg.abs_value), arg.prefix, + specs, digit_grouping<Char>(grouping, sep)); + return true; + } + + template <typename T, FMT_ENABLE_IF(!is_integer<T>::value)> + auto operator()(T) -> bool { + return false; + } +}; + template <typename Char, typename OutputIt, typename T> FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg<T> arg, - const basic_format_specs<Char>& specs, - locale_ref loc) -> OutputIt { + const format_specs<Char>& specs, + locale_ref) -> OutputIt { static_assert(std::is_same<T, uint32_or_64_or_128_t<T>>::value, ""); auto abs_value = arg.abs_value; auto prefix = arg.prefix; switch (specs.type) { case presentation_type::none: case presentation_type::dec: { - if (specs.localized && - write_int_localized(out, static_cast<uint64_or_128_t<T>>(abs_value), - prefix, specs, loc)) { - return out; - } auto num_digits = count_digits(abs_value); return write_int( out, num_digits, prefix, specs, [=](reserve_iterator<OutputIt> it) { @@ -2078,13 +2255,13 @@ FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg<T> arg, case presentation_type::chr: return write_char(out, static_cast<Char>(abs_value), specs); default: - throw_format_error("invalid type specifier"); + throw_format_error("invalid format specifier"); } return out; } template <typename Char, typename OutputIt, typename T> FMT_CONSTEXPR FMT_NOINLINE auto write_int_noinline( - OutputIt out, write_int_arg<T> arg, const basic_format_specs<Char>& specs, + OutputIt out, write_int_arg<T> arg, const format_specs<Char>& specs, locale_ref loc) -> OutputIt { return write_int(out, arg, specs, loc); } @@ -2093,8 +2270,9 @@ template <typename Char, typename OutputIt, typename T, !std::is_same<T, bool>::value && std::is_same<OutputIt, buffer_appender<Char>>::value)> FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value, - const basic_format_specs<Char>& specs, + const format_specs<Char>& specs, locale_ref loc) -> OutputIt { + if (specs.localized && write_loc(out, value, specs, loc)) return out; return write_int_noinline(out, make_write_int_arg(value, specs.sign), specs, loc); } @@ -2104,8 +2282,9 @@ template <typename Char, typename OutputIt, typename T, !std::is_same<T, bool>::value && !std::is_same<OutputIt, buffer_appender<Char>>::value)> FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value, - const basic_format_specs<Char>& specs, + const format_specs<Char>& specs, locale_ref loc) -> OutputIt { + if (specs.localized && write_loc(out, value, specs, loc)) return out; return write_int(out, make_write_int_arg(value, specs.sign), specs, loc); } @@ -2123,34 +2302,35 @@ class counting_iterator { FMT_UNCHECKED_ITERATOR(counting_iterator); struct value_type { - template <typename T> void operator=(const T&) {} + template <typename T> FMT_CONSTEXPR void operator=(const T&) {} }; - counting_iterator() : count_(0) {} + FMT_CONSTEXPR counting_iterator() : count_(0) {} - size_t count() const { return count_; } + FMT_CONSTEXPR size_t count() const { return count_; } - counting_iterator& operator++() { + FMT_CONSTEXPR counting_iterator& operator++() { ++count_; return *this; } - counting_iterator operator++(int) { + FMT_CONSTEXPR counting_iterator operator++(int) { auto it = *this; ++*this; return it; } - friend counting_iterator operator+(counting_iterator it, difference_type n) { + FMT_CONSTEXPR friend counting_iterator operator+(counting_iterator it, + difference_type n) { it.count_ += static_cast<size_t>(n); return it; } - value_type operator*() const { return {}; } + FMT_CONSTEXPR value_type operator*() const { return {}; } }; template <typename Char, typename OutputIt> FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s, - const basic_format_specs<Char>& specs) -> OutputIt { + const format_specs<Char>& specs) -> OutputIt { auto data = s.data(); auto size = s.size(); if (specs.precision >= 0 && to_unsigned(specs.precision) < size) @@ -2172,16 +2352,15 @@ FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s, template <typename Char, typename OutputIt> FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<type_identity_t<Char>> s, - const basic_format_specs<Char>& specs, locale_ref) + const format_specs<Char>& specs, locale_ref) -> OutputIt { - check_string_type_spec(specs.type); return write(out, s, specs); } template <typename Char, typename OutputIt> FMT_CONSTEXPR auto write(OutputIt out, const Char* s, - const basic_format_specs<Char>& specs, locale_ref) + const format_specs<Char>& specs, locale_ref) -> OutputIt { - return check_cstring_type_spec(specs.type) + return specs.type != presentation_type::pointer ? write(out, basic_string_view<Char>(s), specs, {}) : write_ptr<Char>(out, bit_cast<uintptr_t>(s), &specs); } @@ -2208,9 +2387,114 @@ FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt { return base_iterator(out, it); } +// DEPRECATED! +template <typename Char> +FMT_CONSTEXPR auto parse_align(const Char* begin, const Char* end, + format_specs<Char>& specs) -> const Char* { + FMT_ASSERT(begin != end, ""); + auto align = align::none; + auto p = begin + code_point_length(begin); + if (end - p <= 0) p = begin; + for (;;) { + switch (to_ascii(*p)) { + case '<': + align = align::left; + break; + case '>': + align = align::right; + break; + case '^': + align = align::center; + break; + } + if (align != align::none) { + if (p != begin) { + auto c = *begin; + if (c == '}') return begin; + if (c == '{') { + throw_format_error("invalid fill character '{'"); + return begin; + } + specs.fill = {begin, to_unsigned(p - begin)}; + begin = p + 1; + } else { + ++begin; + } + break; + } else if (p == begin) { + break; + } + p = begin; + } + specs.align = align; + return begin; +} + +// A floating-point presentation format. +enum class float_format : unsigned char { + general, // General: exponent notation or fixed point based on magnitude. + exp, // Exponent notation with the default precision of 6, e.g. 1.2e-3. + fixed, // Fixed point with the default precision of 6, e.g. 0.0012. + hex +}; + +struct float_specs { + int precision; + float_format format : 8; + sign_t sign : 8; + bool upper : 1; + bool locale : 1; + bool binary32 : 1; + bool showpoint : 1; +}; + +template <typename ErrorHandler = error_handler, typename Char> +FMT_CONSTEXPR auto parse_float_type_spec(const format_specs<Char>& specs, + ErrorHandler&& eh = {}) + -> float_specs { + auto result = float_specs(); + result.showpoint = specs.alt; + result.locale = specs.localized; + switch (specs.type) { + case presentation_type::none: + result.format = float_format::general; + break; + case presentation_type::general_upper: + result.upper = true; + FMT_FALLTHROUGH; + case presentation_type::general_lower: + result.format = float_format::general; + break; + case presentation_type::exp_upper: + result.upper = true; + FMT_FALLTHROUGH; + case presentation_type::exp_lower: + result.format = float_format::exp; + result.showpoint |= specs.precision != 0; + break; + case presentation_type::fixed_upper: + result.upper = true; + FMT_FALLTHROUGH; + case presentation_type::fixed_lower: + result.format = float_format::fixed; + result.showpoint |= specs.precision != 0; + break; + case presentation_type::hexfloat_upper: + result.upper = true; + FMT_FALLTHROUGH; + case presentation_type::hexfloat_lower: + result.format = float_format::hex; + break; + default: + eh.on_error("invalid format specifier"); + break; + } + return result; +} + template <typename Char, typename OutputIt> FMT_CONSTEXPR20 auto write_nonfinite(OutputIt out, bool isnan, - basic_format_specs<Char> specs, + format_specs<Char> specs, const float_specs& fspecs) -> OutputIt { auto str = isnan ? (fspecs.upper ? "NAN" : "nan") : (fspecs.upper ? "INF" : "inf"); @@ -2256,7 +2540,7 @@ template <typename Char, typename OutputIt, typename T, typename Grouping> FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand, int significand_size, int exponent, const Grouping& grouping) -> OutputIt { - if (!grouping.separator()) { + if (!grouping.has_separator()) { out = write_significand<Char>(out, significand, significand_size); return detail::fill_n(out, exponent, static_cast<Char>('0')); } @@ -2318,7 +2602,7 @@ FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand, int significand_size, int integral_size, Char decimal_point, const Grouping& grouping) -> OutputIt { - if (!grouping.separator()) { + if (!grouping.has_separator()) { return write_significand(out, significand, significand_size, integral_size, decimal_point); } @@ -2334,7 +2618,7 @@ FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand, template <typename OutputIt, typename DecimalFP, typename Char, typename Grouping = digit_grouping<Char>> FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f, - const basic_format_specs<Char>& specs, + const format_specs<Char>& specs, float_specs fspecs, locale_ref loc) -> OutputIt { auto significand = f.significand; @@ -2393,7 +2677,7 @@ FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f, abort_fuzzing_if(num_zeros > 5000); if (fspecs.showpoint) { ++size; - if (num_zeros <= 0 && fspecs.format != float_format::fixed) num_zeros = 1; + if (num_zeros <= 0 && fspecs.format != float_format::fixed) num_zeros = 0; if (num_zeros > 0) size += to_unsigned(num_zeros); } auto grouping = Grouping(loc, fspecs.locale); @@ -2411,7 +2695,7 @@ FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f, int num_zeros = fspecs.showpoint ? fspecs.precision - significand_size : 0; size += 1 + to_unsigned(num_zeros > 0 ? num_zeros : 0); auto grouping = Grouping(loc, fspecs.locale); - size += to_unsigned(grouping.count_separators(significand_size)); + size += to_unsigned(grouping.count_separators(exp)); return write_padded<align::right>(out, specs, size, [&](iterator it) { if (sign) *it++ = detail::sign<Char>(sign); it = write_significand(it, significand, significand_size, exp, @@ -2441,7 +2725,7 @@ template <typename Char> class fallback_digit_grouping { public: constexpr fallback_digit_grouping(locale_ref, bool) {} - constexpr Char separator() const { return Char(); } + constexpr bool has_separator() const { return false; } constexpr int count_separators(int) const { return 0; } @@ -2453,7 +2737,7 @@ template <typename Char> class fallback_digit_grouping { template <typename OutputIt, typename DecimalFP, typename Char> FMT_CONSTEXPR20 auto write_float(OutputIt out, const DecimalFP& f, - const basic_format_specs<Char>& specs, + const format_specs<Char>& specs, float_specs fspecs, locale_ref loc) -> OutputIt { if (is_constant_evaluated()) { @@ -2481,14 +2765,14 @@ template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value&& FMT_CONSTEXPR20 bool isfinite(T value) { constexpr T inf = T(std::numeric_limits<double>::infinity()); if (is_constant_evaluated()) - return !detail::isnan(value) && value != inf && value != -inf; + return !detail::isnan(value) && value < inf && value > -inf; return std::isfinite(value); } template <typename T, FMT_ENABLE_IF(!has_isfinite<T>::value)> FMT_CONSTEXPR bool isfinite(T value) { T inf = T(std::numeric_limits<double>::infinity()); // std::isfinite doesn't support __float128. - return !detail::isnan(value) && value != inf && value != -inf; + return !detail::isnan(value) && value < inf && value > -inf; } template <typename T, FMT_ENABLE_IF(is_floating_point<T>::value)> @@ -2504,78 +2788,6 @@ FMT_INLINE FMT_CONSTEXPR bool signbit(T value) { return std::signbit(static_cast<double>(value)); } -enum class round_direction { unknown, up, down }; - -// Given the divisor (normally a power of 10), the remainder = v % divisor for -// some number v and the error, returns whether v should be rounded up, down, or -// whether the rounding direction can't be determined due to error. -// error should be less than divisor / 2. -FMT_CONSTEXPR inline round_direction get_round_direction(uint64_t divisor, - uint64_t remainder, - uint64_t error) { - FMT_ASSERT(remainder < divisor, ""); // divisor - remainder won't overflow. - FMT_ASSERT(error < divisor, ""); // divisor - error won't overflow. - FMT_ASSERT(error < divisor - error, ""); // error * 2 won't overflow. - // Round down if (remainder + error) * 2 <= divisor. - if (remainder <= divisor - remainder && error * 2 <= divisor - remainder * 2) - return round_direction::down; - // Round up if (remainder - error) * 2 >= divisor. - if (remainder >= error && - remainder - error >= divisor - (remainder - error)) { - return round_direction::up; - } - return round_direction::unknown; -} - -namespace digits { -enum result { - more, // Generate more digits. - done, // Done generating digits. - error // Digit generation cancelled due to an error. -}; -} - -struct gen_digits_handler { - char* buf; - int size; - int precision; - int exp10; - bool fixed; - - FMT_CONSTEXPR digits::result on_digit(char digit, uint64_t divisor, - uint64_t remainder, uint64_t error, - bool integral) { - FMT_ASSERT(remainder < divisor, ""); - buf[size++] = digit; - if (!integral && error >= remainder) return digits::error; - if (size < precision) return digits::more; - if (!integral) { - // Check if error * 2 < divisor with overflow prevention. - // The check is not needed for the integral part because error = 1 - // and divisor > (1 << 32) there. - if (error >= divisor || error >= divisor - error) return digits::error; - } else { - FMT_ASSERT(error == 1 && divisor > 2, ""); - } - auto dir = get_round_direction(divisor, remainder, error); - if (dir != round_direction::up) - return dir == round_direction::down ? digits::done : digits::error; - ++buf[size - 1]; - for (int i = size - 1; i > 0 && buf[i] > '9'; --i) { - buf[i] = '0'; - ++buf[i - 1]; - } - if (buf[0] > '9') { - buf[0] = '1'; - if (fixed) - buf[size++] = '0'; - else - ++exp10; - } - return digits::done; - } -}; - inline FMT_CONSTEXPR20 void adjust_precision(int& precision, int exp10) { // Adjust fixed precision by exponent because it is relative to decimal // point. @@ -2584,101 +2796,6 @@ inline FMT_CONSTEXPR20 void adjust_precision(int& precision, int exp10) { precision += exp10; } -// Generates output using the Grisu digit-gen algorithm. -// error: the size of the region (lower, upper) outside of which numbers -// definitely do not round to value (Delta in Grisu3). -FMT_INLINE FMT_CONSTEXPR20 auto grisu_gen_digits(fp value, uint64_t error, - int& exp, - gen_digits_handler& handler) - -> digits::result { - const fp one(1ULL << -value.e, value.e); - // The integral part of scaled value (p1 in Grisu) = value / one. It cannot be - // zero because it contains a product of two 64-bit numbers with MSB set (due - // to normalization) - 1, shifted right by at most 60 bits. - auto integral = static_cast<uint32_t>(value.f >> -one.e); - FMT_ASSERT(integral != 0, ""); - FMT_ASSERT(integral == value.f >> -one.e, ""); - // The fractional part of scaled value (p2 in Grisu) c = value % one. - uint64_t fractional = value.f & (one.f - 1); - exp = count_digits(integral); // kappa in Grisu. - // Non-fixed formats require at least one digit and no precision adjustment. - if (handler.fixed) { - adjust_precision(handler.precision, exp + handler.exp10); - // Check if precision is satisfied just by leading zeros, e.g. - // format("{:.2f}", 0.001) gives "0.00" without generating any digits. - if (handler.precision <= 0) { - if (handler.precision < 0) return digits::done; - // Divide by 10 to prevent overflow. - uint64_t divisor = data::power_of_10_64[exp - 1] << -one.e; - auto dir = get_round_direction(divisor, value.f / 10, error * 10); - if (dir == round_direction::unknown) return digits::error; - handler.buf[handler.size++] = dir == round_direction::up ? '1' : '0'; - return digits::done; - } - } - // Generate digits for the integral part. This can produce up to 10 digits. - do { - uint32_t digit = 0; - auto divmod_integral = [&](uint32_t divisor) { - digit = integral / divisor; - integral %= divisor; - }; - // This optimization by Milo Yip reduces the number of integer divisions by - // one per iteration. - switch (exp) { - case 10: - divmod_integral(1000000000); - break; - case 9: - divmod_integral(100000000); - break; - case 8: - divmod_integral(10000000); - break; - case 7: - divmod_integral(1000000); - break; - case 6: - divmod_integral(100000); - break; - case 5: - divmod_integral(10000); - break; - case 4: - divmod_integral(1000); - break; - case 3: - divmod_integral(100); - break; - case 2: - divmod_integral(10); - break; - case 1: - digit = integral; - integral = 0; - break; - default: - FMT_ASSERT(false, "invalid number of digits"); - } - --exp; - auto remainder = (static_cast<uint64_t>(integral) << -one.e) + fractional; - auto result = handler.on_digit(static_cast<char>('0' + digit), - data::power_of_10_64[exp] << -one.e, - remainder, error, true); - if (result != digits::more) return result; - } while (exp > 0); - // Generate digits for the fractional part. - for (;;) { - fractional *= 10; - error *= 10; - char digit = static_cast<char>('0' + (fractional >> -one.e)); - fractional &= one.f - 1; - --exp; - auto result = handler.on_digit(digit, one.f, fractional, error, false); - if (result != digits::more) return result; - } -} - class bigint { private: // A bigint is stored as an array of bigits (big digits), with bigit at index @@ -2779,7 +2896,7 @@ class bigint { auto size = other.bigits_.size(); bigits_.resize(size); auto data = other.bigits_.data(); - std::copy(data, data + size, make_checked(bigits_.data(), size)); + copy_str<bigit>(data, data + size, bigits_.data()); exp_ = other.exp_; } @@ -2991,8 +3108,9 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value, upper = &upper_store; } } - bool even = (value.f & 1) == 0; + int even = static_cast<int>((value.f & 1) == 0); if (!upper) upper = &lower; + bool shortest = num_digits < 0; if ((flags & dragon::fixup) != 0) { if (add_compare(numerator, *upper, denominator) + even <= 0) { --exp10; @@ -3005,7 +3123,7 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value, if ((flags & dragon::fixed) != 0) adjust_precision(num_digits, exp10 + 1); } // Invariant: value == (numerator / denominator) * pow(10, exp10). - if (num_digits < 0) { + if (shortest) { // Generate the shortest representation. num_digits = 0; char* data = buf.data(); @@ -3035,7 +3153,7 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value, } // Generate the given number of digits. exp10 -= num_digits - 1; - if (num_digits == 0) { + if (num_digits <= 0) { denominator *= 10; auto digit = add_compare(numerator, numerator, denominator) > 0 ? '1' : '0'; buf.push_back(digit); @@ -3060,7 +3178,8 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value, } if (buf[0] == overflow) { buf[0] = '1'; - ++exp10; + if ((flags & dragon::fixed) != 0) buf.push_back('0'); + else ++exp10; } return; } @@ -3069,6 +3188,94 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value, buf[num_digits - 1] = static_cast<char>('0' + digit); } +// Formats a floating-point number using the hexfloat format. +template <typename Float, FMT_ENABLE_IF(!is_double_double<Float>::value)> +FMT_CONSTEXPR20 void format_hexfloat(Float value, int precision, + float_specs specs, buffer<char>& buf) { + // float is passed as double to reduce the number of instantiations and to + // simplify implementation. + static_assert(!std::is_same<Float, float>::value, ""); + + using info = dragonbox::float_info<Float>; + + // Assume Float is in the format [sign][exponent][significand]. + using carrier_uint = typename info::carrier_uint; + + constexpr auto num_float_significand_bits = + detail::num_significand_bits<Float>(); + + basic_fp<carrier_uint> f(value); + f.e += num_float_significand_bits; + if (!has_implicit_bit<Float>()) --f.e; + + constexpr auto num_fraction_bits = + num_float_significand_bits + (has_implicit_bit<Float>() ? 1 : 0); + constexpr auto num_xdigits = (num_fraction_bits + 3) / 4; + + constexpr auto leading_shift = ((num_xdigits - 1) * 4); + const auto leading_mask = carrier_uint(0xF) << leading_shift; + const auto leading_xdigit = + static_cast<uint32_t>((f.f & leading_mask) >> leading_shift); + if (leading_xdigit > 1) f.e -= (32 - countl_zero(leading_xdigit) - 1); + + int print_xdigits = num_xdigits - 1; + if (precision >= 0 && print_xdigits > precision) { + const int shift = ((print_xdigits - precision - 1) * 4); + const auto mask = carrier_uint(0xF) << shift; + const auto v = static_cast<uint32_t>((f.f & mask) >> shift); + + if (v >= 8) { + const auto inc = carrier_uint(1) << (shift + 4); + f.f += inc; + f.f &= ~(inc - 1); + } + + // Check long double overflow + if (!has_implicit_bit<Float>()) { + const auto implicit_bit = carrier_uint(1) << num_float_significand_bits; + if ((f.f & implicit_bit) == implicit_bit) { + f.f >>= 4; + f.e += 4; + } + } + + print_xdigits = precision; + } + + char xdigits[num_bits<carrier_uint>() / 4]; + detail::fill_n(xdigits, sizeof(xdigits), '0'); + format_uint<4>(xdigits, f.f, num_xdigits, specs.upper); + + // Remove zero tail + while (print_xdigits > 0 && xdigits[print_xdigits] == '0') --print_xdigits; + + buf.push_back('0'); + buf.push_back(specs.upper ? 'X' : 'x'); + buf.push_back(xdigits[0]); + if (specs.showpoint || print_xdigits > 0 || print_xdigits < precision) + buf.push_back('.'); + buf.append(xdigits + 1, xdigits + 1 + print_xdigits); + for (; print_xdigits < precision; ++print_xdigits) buf.push_back('0'); + + buf.push_back(specs.upper ? 'P' : 'p'); + + uint32_t abs_e; + if (f.e < 0) { + buf.push_back('-'); + abs_e = static_cast<uint32_t>(-f.e); + } else { + buf.push_back('+'); + abs_e = static_cast<uint32_t>(f.e); + } + format_decimal<char>(appender(buf), abs_e, detail::count_digits(abs_e)); +} + +template <typename Float, FMT_ENABLE_IF(is_double_double<Float>::value)> +FMT_CONSTEXPR20 void format_hexfloat(Float value, int precision, + float_specs specs, buffer<char>& buf) { + format_hexfloat(static_cast<double>(value), precision, specs, buf); +} + template <typename Float> FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, buffer<char>& buf) -> int { @@ -3091,7 +3298,7 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, int exp = 0; bool use_dragon = true; unsigned dragon_flags = 0; - if (!is_fast_float<Float>()) { + if (!is_fast_float<Float>() || is_constant_evaluated()) { const auto inv_log2_10 = 0.3010299956639812; // 1 / log2(10) using info = dragonbox::float_info<decltype(converted_value)>; const auto f = basic_fp<typename info::carrier_uint>(converted_value); @@ -3099,10 +3306,11 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, // 10^(exp - 1) <= value < 10^exp or 10^exp <= value < 10^(exp + 1). // This is based on log10(value) == log2(value) / log2(10) and approximation // of log2(value) by e + num_fraction_bits idea from double-conversion. - exp = static_cast<int>( - std::ceil((f.e + count_digits<1>(f.f) - 1) * inv_log2_10 - 1e-10)); + auto e = (f.e + count_digits<1>(f.f) - 1) * inv_log2_10 - 1e-10; + exp = static_cast<int>(e); + if (e > exp) ++exp; // Compute ceil. dragon_flags = dragon::fixup; - } else if (!is_constant_evaluated() && precision < 0) { + } else if (precision < 0) { // Use Dragonbox for the shortest format. if (specs.binary32) { auto dec = dragonbox::to_decimal(static_cast<float>(value)); @@ -3113,23 +3321,244 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, write<char>(buffer_appender<char>(buf), dec.significand); return dec.exponent; } else { - // Use Grisu + Dragon4 for the given precision: - // https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf. - const int min_exp = -60; // alpha in Grisu. - int cached_exp10 = 0; // K in Grisu. - fp normalized = normalize(fp(converted_value)); - const auto cached_pow = get_cached_power( - min_exp - (normalized.e + fp::num_significand_bits), cached_exp10); - normalized = normalized * cached_pow; - gen_digits_handler handler{buf.data(), 0, precision, -cached_exp10, fixed}; - if (grisu_gen_digits(normalized, 1, exp, handler) != digits::error && - !is_constant_evaluated()) { - exp += handler.exp10; - buf.try_resize(to_unsigned(handler.size)); - use_dragon = false; + // Extract significand bits and exponent bits. + using info = dragonbox::float_info<double>; + auto br = bit_cast<uint64_t>(static_cast<double>(value)); + + const uint64_t significand_mask = + (static_cast<uint64_t>(1) << num_significand_bits<double>()) - 1; + uint64_t significand = (br & significand_mask); + int exponent = static_cast<int>((br & exponent_mask<double>()) >> + num_significand_bits<double>()); + + if (exponent != 0) { // Check if normal. + exponent -= exponent_bias<double>() + num_significand_bits<double>(); + significand |= + (static_cast<uint64_t>(1) << num_significand_bits<double>()); + significand <<= 1; } else { - exp += handler.size - cached_exp10 - 1; - precision = handler.precision; + // Normalize subnormal inputs. + FMT_ASSERT(significand != 0, "zeros should not appear here"); + int shift = countl_zero(significand); + FMT_ASSERT(shift >= num_bits<uint64_t>() - num_significand_bits<double>(), + ""); + shift -= (num_bits<uint64_t>() - num_significand_bits<double>() - 2); + exponent = (std::numeric_limits<double>::min_exponent - + num_significand_bits<double>()) - + shift; + significand <<= shift; + } + + // Compute the first several nonzero decimal significand digits. + // We call the number we get the first segment. + const int k = info::kappa - dragonbox::floor_log10_pow2(exponent); + exp = -k; + const int beta = exponent + dragonbox::floor_log2_pow10(k); + uint64_t first_segment; + bool has_more_segments; + int digits_in_the_first_segment; + { + const auto r = dragonbox::umul192_upper128( + significand << beta, dragonbox::get_cached_power(k)); + first_segment = r.high(); + has_more_segments = r.low() != 0; + + // The first segment can have 18 ~ 19 digits. + if (first_segment >= 1000000000000000000ULL) { + digits_in_the_first_segment = 19; + } else { + // When it is of 18-digits, we align it to 19-digits by adding a bogus + // zero at the end. + digits_in_the_first_segment = 18; + first_segment *= 10; + } + } + + // Compute the actual number of decimal digits to print. + if (fixed) adjust_precision(precision, exp + digits_in_the_first_segment); + + // Use Dragon4 only when there might be not enough digits in the first + // segment. + if (digits_in_the_first_segment > precision) { + use_dragon = false; + + if (precision <= 0) { + exp += digits_in_the_first_segment; + + if (precision < 0) { + // Nothing to do, since all we have are just leading zeros. + buf.try_resize(0); + } else { + // We may need to round-up. + buf.try_resize(1); + if ((first_segment | static_cast<uint64_t>(has_more_segments)) > + 5000000000000000000ULL) { + buf[0] = '1'; + } else { + buf[0] = '0'; + } + } + } // precision <= 0 + else { + exp += digits_in_the_first_segment - precision; + + // When precision > 0, we divide the first segment into three + // subsegments, each with 9, 9, and 0 ~ 1 digits so that each fits + // in 32-bits which usually allows faster calculation than in + // 64-bits. Since some compiler (e.g. MSVC) doesn't know how to optimize + // division-by-constant for large 64-bit divisors, we do it here + // manually. The magic number 7922816251426433760 below is equal to + // ceil(2^(64+32) / 10^10). + const uint32_t first_subsegment = static_cast<uint32_t>( + dragonbox::umul128_upper64(first_segment, 7922816251426433760ULL) >> + 32); + const uint64_t second_third_subsegments = + first_segment - first_subsegment * 10000000000ULL; + + uint64_t prod; + uint32_t digits; + bool should_round_up; + int number_of_digits_to_print = precision > 9 ? 9 : precision; + + // Print a 9-digits subsegment, either the first or the second. + auto print_subsegment = [&](uint32_t subsegment, char* buffer) { + int number_of_digits_printed = 0; + + // If we want to print an odd number of digits from the subsegment, + if ((number_of_digits_to_print & 1) != 0) { + // Convert to 64-bit fixed-point fractional form with 1-digit + // integer part. The magic number 720575941 is a good enough + // approximation of 2^(32 + 24) / 10^8; see + // https://jk-jeon.github.io/posts/2022/12/fixed-precision-formatting/#fixed-length-case + // for details. + prod = ((subsegment * static_cast<uint64_t>(720575941)) >> 24) + 1; + digits = static_cast<uint32_t>(prod >> 32); + *buffer = static_cast<char>('0' + digits); + number_of_digits_printed++; + } + // If we want to print an even number of digits from the + // first_subsegment, + else { + // Convert to 64-bit fixed-point fractional form with 2-digits + // integer part. The magic number 450359963 is a good enough + // approximation of 2^(32 + 20) / 10^7; see + // https://jk-jeon.github.io/posts/2022/12/fixed-precision-formatting/#fixed-length-case + // for details. + prod = ((subsegment * static_cast<uint64_t>(450359963)) >> 20) + 1; + digits = static_cast<uint32_t>(prod >> 32); + copy2(buffer, digits2(digits)); + number_of_digits_printed += 2; + } + + // Print all digit pairs. + while (number_of_digits_printed < number_of_digits_to_print) { + prod = static_cast<uint32_t>(prod) * static_cast<uint64_t>(100); + digits = static_cast<uint32_t>(prod >> 32); + copy2(buffer + number_of_digits_printed, digits2(digits)); + number_of_digits_printed += 2; + } + }; + + // Print first subsegment. + print_subsegment(first_subsegment, buf.data()); + + // Perform rounding if the first subsegment is the last subsegment to + // print. + if (precision <= 9) { + // Rounding inside the subsegment. + // We round-up if: + // - either the fractional part is strictly larger than 1/2, or + // - the fractional part is exactly 1/2 and the last digit is odd. + // We rely on the following observations: + // - If fractional_part >= threshold, then the fractional part is + // strictly larger than 1/2. + // - If the MSB of fractional_part is set, then the fractional part + // must be at least 1/2. + // - When the MSB of fractional_part is set, either + // second_third_subsegments being nonzero or has_more_segments + // being true means there are further digits not printed, so the + // fractional part is strictly larger than 1/2. + if (precision < 9) { + uint32_t fractional_part = static_cast<uint32_t>(prod); + should_round_up = fractional_part >= + data::fractional_part_rounding_thresholds + [8 - number_of_digits_to_print] || + ((fractional_part >> 31) & + ((digits & 1) | (second_third_subsegments != 0) | + has_more_segments)) != 0; + } + // Rounding at the subsegment boundary. + // In this case, the fractional part is at least 1/2 if and only if + // second_third_subsegments >= 5000000000ULL, and is strictly larger + // than 1/2 if we further have either second_third_subsegments > + // 5000000000ULL or has_more_segments == true. + else { + should_round_up = second_third_subsegments > 5000000000ULL || + (second_third_subsegments == 5000000000ULL && + ((digits & 1) != 0 || has_more_segments)); + } + } + // Otherwise, print the second subsegment. + else { + // Compilers are not aware of how to leverage the maximum value of + // second_third_subsegments to find out a better magic number which + // allows us to eliminate an additional shift. 1844674407370955162 = + // ceil(2^64/10) < ceil(2^64*(10^9/(10^10 - 1))). + const uint32_t second_subsegment = + static_cast<uint32_t>(dragonbox::umul128_upper64( + second_third_subsegments, 1844674407370955162ULL)); + const uint32_t third_subsegment = + static_cast<uint32_t>(second_third_subsegments) - + second_subsegment * 10; + + number_of_digits_to_print = precision - 9; + print_subsegment(second_subsegment, buf.data() + 9); + + // Rounding inside the subsegment. + if (precision < 18) { + // The condition third_subsegment != 0 implies that the segment was + // of 19 digits, so in this case the third segment should be + // consisting of a genuine digit from the input. + uint32_t fractional_part = static_cast<uint32_t>(prod); + should_round_up = fractional_part >= + data::fractional_part_rounding_thresholds + [8 - number_of_digits_to_print] || + ((fractional_part >> 31) & + ((digits & 1) | (third_subsegment != 0) | + has_more_segments)) != 0; + } + // Rounding at the subsegment boundary. + else { + // In this case, the segment must be of 19 digits, thus + // the third subsegment should be consisting of a genuine digit from + // the input. + should_round_up = third_subsegment > 5 || + (third_subsegment == 5 && + ((digits & 1) != 0 || has_more_segments)); + } + } + + // Round-up if necessary. + if (should_round_up) { + ++buf[precision - 1]; + for (int i = precision - 1; i > 0 && buf[i] > '9'; --i) { + buf[i] = '0'; + ++buf[i - 1]; + } + if (buf[0] > '9') { + buf[0] = '1'; + if (fixed) + buf[precision++] = '0'; + else + ++exp; + } + } + buf.try_resize(to_unsigned(precision)); + } + } // if (digits_in_the_first_segment > precision) + else { + // Adjust the exponent for its use in Dragon4. + exp += digits_in_the_first_segment - 1; } } if (use_dragon) { @@ -3156,13 +3585,10 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, } return exp; } - -template <typename Char, typename OutputIt, typename T, - FMT_ENABLE_IF(is_floating_point<T>::value)> -FMT_CONSTEXPR20 auto write(OutputIt out, T value, - basic_format_specs<Char> specs, locale_ref loc = {}) +template <typename Char, typename OutputIt, typename T> +FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, + format_specs<Char> specs, locale_ref loc) -> OutputIt { - if (const_check(!is_supported_floating_point(value))) return out; float_specs fspecs = parse_float_type_spec(specs); fspecs.sign = specs.sign; if (detail::signbit(value)) { // value < 0 is false for NaN so use signbit. @@ -3186,7 +3612,7 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value, memory_buffer buffer; if (fspecs.format == float_format::hex) { if (fspecs.sign) buffer.push_back(detail::sign<char>(fspecs.sign)); - snprintf_float(convert_float(value), specs.precision, fspecs, buffer); + format_hexfloat(convert_float(value), specs.precision, fspecs, buffer); return write_bytes<align::right>(out, {buffer.data(), buffer.size()}, specs); } @@ -3209,10 +3635,19 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value, } template <typename Char, typename OutputIt, typename T, + FMT_ENABLE_IF(is_floating_point<T>::value)> +FMT_CONSTEXPR20 auto write(OutputIt out, T value, format_specs<Char> specs, + locale_ref loc = {}) -> OutputIt { + if (const_check(!is_supported_floating_point(value))) return out; + return specs.localized && write_loc(out, value, specs, loc) + ? out + : write_float(out, value, specs, loc); +} + +template <typename Char, typename OutputIt, typename T, FMT_ENABLE_IF(is_fast_float<T>::value)> FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt { - if (is_constant_evaluated()) - return write(out, value, basic_format_specs<Char>()); + if (is_constant_evaluated()) return write(out, value, format_specs<Char>()); if (const_check(!is_supported_floating_point(value))) return out; auto fspecs = float_specs(); @@ -3221,11 +3656,11 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt { value = -value; } - constexpr auto specs = basic_format_specs<Char>(); + constexpr auto specs = format_specs<Char>(); using floaty = conditional_t<std::is_same<T, long double>::value, double, T>; - using uint = typename dragonbox::float_info<floaty>::carrier_uint; - uint mask = exponent_mask<floaty>(); - if ((bit_cast<uint>(value) & mask) == mask) + using floaty_uint = typename dragonbox::float_info<floaty>::carrier_uint; + floaty_uint mask = exponent_mask<floaty>(); + if ((bit_cast<floaty_uint>(value) & mask) == mask) return write_nonfinite(out, std::isnan(value), specs, fspecs); auto dec = dragonbox::to_decimal(static_cast<floaty>(value)); @@ -3236,12 +3671,12 @@ template <typename Char, typename OutputIt, typename T, FMT_ENABLE_IF(is_floating_point<T>::value && !is_fast_float<T>::value)> inline auto write(OutputIt out, T value) -> OutputIt { - return write(out, value, basic_format_specs<Char>()); + return write(out, value, format_specs<Char>()); } template <typename Char, typename OutputIt> -auto write(OutputIt out, monostate, basic_format_specs<Char> = {}, - locale_ref = {}) -> OutputIt { +auto write(OutputIt out, monostate, format_specs<Char> = {}, locale_ref = {}) + -> OutputIt { FMT_ASSERT(false, ""); return out; } @@ -3275,8 +3710,8 @@ FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt { template <typename Char, typename OutputIt, typename T, FMT_ENABLE_IF(std::is_same<T, bool>::value)> FMT_CONSTEXPR auto write(OutputIt out, T value, - const basic_format_specs<Char>& specs = {}, - locale_ref = {}) -> OutputIt { + const format_specs<Char>& specs = {}, locale_ref = {}) + -> OutputIt { return specs.type != presentation_type::none && specs.type != presentation_type::string ? write(out, value ? 1 : 0, specs, {}) @@ -3293,20 +3728,15 @@ FMT_CONSTEXPR auto write(OutputIt out, Char value) -> OutputIt { template <typename Char, typename OutputIt> FMT_CONSTEXPR_CHAR_TRAITS auto write(OutputIt out, const Char* value) -> OutputIt { - if (!value) { - throw_format_error("string pointer is null"); - } else { - out = write(out, basic_string_view<Char>(value)); - } + if (value) return write(out, basic_string_view<Char>(value)); + throw_format_error("string pointer is null"); return out; } template <typename Char, typename OutputIt, typename T, FMT_ENABLE_IF(std::is_same<T, void>::value)> -auto write(OutputIt out, const T* value, - const basic_format_specs<Char>& specs = {}, locale_ref = {}) - -> OutputIt { - check_pointer_type_spec(specs.type, error_handler()); +auto write(OutputIt out, const T* value, const format_specs<Char>& specs = {}, + locale_ref = {}) -> OutputIt { return write_ptr<Char>(out, bit_cast<uintptr_t>(value), &specs); } @@ -3316,8 +3746,8 @@ template <typename Char, typename OutputIt, typename T, FMT_CONSTEXPR auto write(OutputIt out, const T& value) -> enable_if_t< std::is_class<T>::value && !is_string<T>::value && !is_floating_point<T>::value && !std::is_same<T, Char>::value && - !std::is_same<const T&, - decltype(arg_mapper<Context>().map(value))>::value, + !std::is_same<T, remove_cvref_t<decltype(arg_mapper<Context>().map( + value))>>::value, OutputIt> { return write<Char>(out, arg_mapper<Context>().map(value)); } @@ -3327,12 +3757,8 @@ template <typename Char, typename OutputIt, typename T, FMT_CONSTEXPR auto write(OutputIt out, const T& value) -> enable_if_t<mapped_type_constant<T, Context>::value == type::custom_type, OutputIt> { - using formatter_type = - conditional_t<has_formatter<T, Context>::value, - typename Context::template formatter_type<T>, - fallback_formatter<T, Char>>; auto ctx = Context(out, {}, {}); - return formatter_type().format(value, ctx); + return typename Context::template formatter_type<T>().format(value, ctx); } // An argument visitor that formats the argument and writes it via the output @@ -3361,7 +3787,7 @@ template <typename Char> struct arg_formatter { using context = buffer_context<Char>; iterator out; - const basic_format_specs<Char>& specs; + const format_specs<Char>& specs; locale_ref locale; template <typename T> @@ -3386,12 +3812,6 @@ template <typename Char> struct custom_formatter { template <typename T> void operator()(T) const {} }; -template <typename T> -using is_integer = - bool_constant<is_integral<T>::value && !std::is_same<T, bool>::value && - !std::is_same<T, char>::value && - !std::is_same<T, wchar_t>::value>; - template <typename ErrorHandler> class width_checker { public: explicit FMT_CONSTEXPR width_checker(ErrorHandler& eh) : handler_(eh) {} @@ -3441,55 +3861,12 @@ FMT_CONSTEXPR auto get_dynamic_spec(FormatArg arg, ErrorHandler eh) -> int { } template <typename Context, typename ID> -FMT_CONSTEXPR auto get_arg(Context& ctx, ID id) -> - typename Context::format_arg { +FMT_CONSTEXPR auto get_arg(Context& ctx, ID id) -> decltype(ctx.arg(id)) { auto arg = ctx.arg(id); if (!arg) ctx.on_error("argument not found"); return arg; } -// The standard format specifier handler with checking. -template <typename Char> class specs_handler : public specs_setter<Char> { - private: - basic_format_parse_context<Char>& parse_context_; - buffer_context<Char>& context_; - - // This is only needed for compatibility with gcc 4.4. - using format_arg = basic_format_arg<buffer_context<Char>>; - - FMT_CONSTEXPR auto get_arg(auto_id) -> format_arg { - return detail::get_arg(context_, parse_context_.next_arg_id()); - } - - FMT_CONSTEXPR auto get_arg(int arg_id) -> format_arg { - parse_context_.check_arg_id(arg_id); - return detail::get_arg(context_, arg_id); - } - - FMT_CONSTEXPR auto get_arg(basic_string_view<Char> arg_id) -> format_arg { - parse_context_.check_arg_id(arg_id); - return detail::get_arg(context_, arg_id); - } - - public: - FMT_CONSTEXPR specs_handler(basic_format_specs<Char>& specs, - basic_format_parse_context<Char>& parse_ctx, - buffer_context<Char>& ctx) - : specs_setter<Char>(specs), parse_context_(parse_ctx), context_(ctx) {} - - template <typename Id> FMT_CONSTEXPR void on_dynamic_width(Id arg_id) { - this->specs_.width = get_dynamic_spec<width_checker>( - get_arg(arg_id), context_.error_handler()); - } - - template <typename Id> FMT_CONSTEXPR void on_dynamic_precision(Id arg_id) { - this->specs_.precision = get_dynamic_spec<precision_checker>( - get_arg(arg_id), context_.error_handler()); - } - - void on_error(const char* message) { context_.on_error(message); } -}; - template <template <typename> class Handler, typename Context> FMT_CONSTEXPR void handle_dynamic_spec(int& value, arg_ref<typename Context::char_type> ref, @@ -3498,26 +3875,17 @@ FMT_CONSTEXPR void handle_dynamic_spec(int& value, case arg_id_kind::none: break; case arg_id_kind::index: - value = detail::get_dynamic_spec<Handler>(ctx.arg(ref.val.index), + value = detail::get_dynamic_spec<Handler>(get_arg(ctx, ref.val.index), ctx.error_handler()); break; case arg_id_kind::name: - value = detail::get_dynamic_spec<Handler>(ctx.arg(ref.val.name), + value = detail::get_dynamic_spec<Handler>(get_arg(ctx, ref.val.name), ctx.error_handler()); break; } } #if FMT_USE_USER_DEFINED_LITERALS -template <typename Char> struct udl_formatter { - basic_string_view<Char> str; - - template <typename... T> - auto operator()(T&&... args) const -> std::basic_string<Char> { - return vformat(str, fmt::make_format_args<buffer_context<Char>>(args...)); - } -}; - # if FMT_USE_NONTYPE_TEMPLATE_ARGS template <typename T, typename Char, size_t N, fmt::detail_exported::fixed_string<Char, N> Str> @@ -3556,12 +3924,12 @@ template <typename Char> struct udl_arg { #endif // FMT_USE_USER_DEFINED_LITERALS template <typename Locale, typename Char> -auto vformat(const Locale& loc, basic_string_view<Char> format_str, +auto vformat(const Locale& loc, basic_string_view<Char> fmt, basic_format_args<buffer_context<type_identity_t<Char>>> args) -> std::basic_string<Char> { - basic_memory_buffer<Char> buffer; - detail::vformat_to(buffer, format_str, args, detail::locale_ref(loc)); - return {buffer.data(), buffer.size()}; + auto buf = basic_memory_buffer<Char>(); + detail::vformat_to(buf, fmt, args, detail::locale_ref(loc)); + return {buf.data(), buf.size()}; } using format_func = void (*)(detail::buffer<char>&, int, const char*); @@ -3571,28 +3939,28 @@ FMT_API void format_error_code(buffer<char>& out, int error_code, FMT_API void report_error(format_func func, int error_code, const char* message) noexcept; -FMT_END_DETAIL_NAMESPACE +} // namespace detail FMT_API auto vsystem_error(int error_code, string_view format_str, format_args args) -> std::system_error; /** - \rst - Constructs :class:`std::system_error` with a message formatted with - ``fmt::format(fmt, args...)``. + \rst + Constructs :class:`std::system_error` with a message formatted with + ``fmt::format(fmt, args...)``. *error_code* is a system error code as given by ``errno``. - **Example**:: - - // This throws std::system_error with the description - // cannot open file 'madeup': No such file or directory - // or similar (system message may vary). - const char* filename = "madeup"; - std::FILE* file = std::fopen(filename, "r"); - if (!file) - throw fmt::system_error(errno, "cannot open file '{}'", filename); - \endrst -*/ + **Example**:: + + // This throws std::system_error with the description + // cannot open file 'madeup': No such file or directory + // or similar (system message may vary). + const char* filename = "madeup"; + std::FILE* file = std::fopen(filename, "r"); + if (!file) + throw fmt::system_error(errno, "cannot open file '{}'", filename); + \endrst + */ template <typename... T> auto system_error(int error_code, format_string<T...> fmt, T&&... args) -> std::system_error { @@ -3683,93 +4051,35 @@ class format_int { }; template <typename T, typename Char> -template <typename FormatContext> -FMT_CONSTEXPR FMT_INLINE auto -formatter<T, Char, - enable_if_t<detail::type_constant<T, Char>::value != - detail::type::custom_type>>::format(const T& val, - FormatContext& ctx) - const -> decltype(ctx.out()) { - if (specs_.width_ref.kind != detail::arg_id_kind::none || - specs_.precision_ref.kind != detail::arg_id_kind::none) { - auto specs = specs_; - detail::handle_dynamic_spec<detail::width_checker>(specs.width, - specs.width_ref, ctx); - detail::handle_dynamic_spec<detail::precision_checker>( - specs.precision, specs.precision_ref, ctx); - return detail::write<Char>(ctx.out(), val, specs, ctx.locale()); - } - return detail::write<Char>(ctx.out(), val, specs_, ctx.locale()); -} +struct formatter<T, Char, enable_if_t<detail::has_format_as<T>::value>> + : private formatter<detail::format_as_t<T>, Char> { + using base = formatter<detail::format_as_t<T>, Char>; + using base::parse; -template <typename Char> -struct formatter<void*, Char> : formatter<const void*, Char> { template <typename FormatContext> - auto format(void* val, FormatContext& ctx) const -> decltype(ctx.out()) { - return formatter<const void*, Char>::format(val, ctx); + auto format(const T& value, FormatContext& ctx) const -> decltype(ctx.out()) { + return base::format(format_as(value), ctx); } }; -template <typename Char, size_t N> -struct formatter<Char[N], Char> : formatter<basic_string_view<Char>, Char> { - template <typename FormatContext> - FMT_CONSTEXPR auto format(const Char* val, FormatContext& ctx) const - -> decltype(ctx.out()) { - return formatter<basic_string_view<Char>, Char>::format(val, ctx); - } -}; +#define FMT_FORMAT_AS(Type, Base) \ + template <typename Char> \ + struct formatter<Type, Char> : formatter<Base, Char> {} + +FMT_FORMAT_AS(signed char, int); +FMT_FORMAT_AS(unsigned char, unsigned); +FMT_FORMAT_AS(short, int); +FMT_FORMAT_AS(unsigned short, unsigned); +FMT_FORMAT_AS(long, detail::long_type); +FMT_FORMAT_AS(unsigned long, detail::ulong_type); +FMT_FORMAT_AS(Char*, const Char*); +FMT_FORMAT_AS(std::basic_string<Char>, basic_string_view<Char>); +FMT_FORMAT_AS(std::nullptr_t, const void*); +FMT_FORMAT_AS(detail::std_string_view<Char>, basic_string_view<Char>); +FMT_FORMAT_AS(void*, const void*); -// A formatter for types known only at run time such as variant alternatives. -// -// Usage: -// using variant = std::variant<int, std::string>; -// template <> -// struct formatter<variant>: dynamic_formatter<> { -// auto format(const variant& v, format_context& ctx) { -// return visit([&](const auto& val) { -// return dynamic_formatter<>::format(val, ctx); -// }, v); -// } -// }; -template <typename Char = char> class dynamic_formatter { - private: - detail::dynamic_format_specs<Char> specs_; - const Char* format_str_; - - struct null_handler : detail::error_handler { - void on_align(align_t) {} - void on_sign(sign_t) {} - void on_hash() {} - }; - - template <typename Context> void handle_specs(Context& ctx) { - detail::handle_dynamic_spec<detail::width_checker>(specs_.width, - specs_.width_ref, ctx); - detail::handle_dynamic_spec<detail::precision_checker>( - specs_.precision, specs_.precision_ref, ctx); - } - - public: - template <typename ParseContext> - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - format_str_ = ctx.begin(); - // Checks are deferred to formatting time when the argument type is known. - detail::dynamic_specs_handler<ParseContext> handler(specs_, ctx); - return detail::parse_format_specs(ctx.begin(), ctx.end(), handler); - } - - template <typename T, typename FormatContext> - auto format(const T& val, FormatContext& ctx) -> decltype(ctx.out()) { - handle_specs(ctx); - detail::specs_checker<null_handler> checker( - null_handler(), detail::mapped_type_constant<T, FormatContext>::value); - checker.on_align(specs_.align); - if (specs_.sign != sign::none) checker.on_sign(specs_.sign); - if (specs_.alt) checker.on_hash(); - if (specs_.precision >= 0) checker.end_precision(); - return detail::write<Char>(ctx.out(), val, specs_, ctx.locale()); - } -}; +template <typename Char, size_t N> +struct formatter<Char[N], Char> : formatter<basic_string_view<Char>, Char> {}; /** \rst @@ -3784,7 +4094,8 @@ template <typename T> auto ptr(T p) -> const void* { static_assert(std::is_pointer<T>::value, ""); return detail::bit_cast<const void*>(p); } -template <typename T> auto ptr(const std::unique_ptr<T>& p) -> const void* { +template <typename T, typename Deleter> +auto ptr(const std::unique_ptr<T, Deleter>& p) -> const void* { return p.get(); } template <typename T> auto ptr(const std::shared_ptr<T>& p) -> const void* { @@ -3824,17 +4135,13 @@ class bytes { template <> struct formatter<bytes> { private: - detail::dynamic_format_specs<char> specs_; + detail::dynamic_format_specs<> specs_; public: template <typename ParseContext> - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - using handler_type = detail::dynamic_specs_handler<ParseContext>; - detail::specs_checker<handler_type> handler(handler_type(specs_, ctx), - detail::type::string_type); - auto it = parse_format_specs(ctx.begin(), ctx.end(), handler); - detail::check_string_type_spec(specs_.type, ctx.error_handler()); - return it; + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const char* { + return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, + detail::type::string_type); } template <typename FormatContext> @@ -3848,7 +4155,9 @@ template <> struct formatter<bytes> { }; // group_digits_view is not derived from view because it copies the argument. -template <typename T> struct group_digits_view { T value; }; +template <typename T> struct group_digits_view { + T value; +}; /** \rst @@ -3867,17 +4176,13 @@ template <typename T> auto group_digits(T value) -> group_digits_view<T> { template <typename T> struct formatter<group_digits_view<T>> : formatter<T> { private: - detail::dynamic_format_specs<char> specs_; + detail::dynamic_format_specs<> specs_; public: template <typename ParseContext> - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - using handler_type = detail::dynamic_specs_handler<ParseContext>; - detail::specs_checker<handler_type> handler(handler_type(specs_, ctx), - detail::type::int_type); - auto it = parse_format_specs(ctx.begin(), ctx.end(), handler); - detail::check_string_type_spec(specs_.type, ctx.error_handler()); - return it; + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const char* { + return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, + detail::type::int_type); } template <typename FormatContext> @@ -3887,12 +4192,13 @@ template <typename T> struct formatter<group_digits_view<T>> : formatter<T> { specs_.width_ref, ctx); detail::handle_dynamic_spec<detail::precision_checker>( specs_.precision, specs_.precision_ref, ctx); - return detail::write_int_localized( + return detail::write_int( ctx.out(), static_cast<detail::uint64_or_128_t<T>>(t.value), 0, specs_, - detail::digit_grouping<char>({"\3", ','})); + detail::digit_grouping<char>("\3", ",")); } }; +// DEPRECATED! join_view will be moved to ranges.h. template <typename It, typename Sentinel, typename Char = char> struct join_view : detail::view { It begin; @@ -3912,30 +4218,11 @@ struct formatter<join_view<It, Sentinel, Char>, Char> { #else typename std::iterator_traits<It>::value_type; #endif - using context = buffer_context<Char>; - using mapper = detail::arg_mapper<context>; - - template <typename T, FMT_ENABLE_IF(has_formatter<T, context>::value)> - static auto map(const T& value) -> const T& { - return value; - } - template <typename T, FMT_ENABLE_IF(!has_formatter<T, context>::value)> - static auto map(const T& value) -> decltype(mapper().map(value)) { - return mapper().map(value); - } - - using formatter_type = - conditional_t<is_formattable<value_type, Char>::value, - formatter<remove_cvref_t<decltype(map( - std::declval<const value_type&>()))>, - Char>, - detail::fallback_formatter<value_type, Char>>; - - formatter_type value_formatter_; + formatter<remove_cvref_t<value_type>, Char> value_formatter_; public: template <typename ParseContext> - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const Char* { return value_formatter_.parse(ctx); } @@ -3945,12 +4232,12 @@ struct formatter<join_view<It, Sentinel, Char>, Char> { auto it = value.begin; auto out = ctx.out(); if (it != value.end) { - out = value_formatter_.format(map(*it), ctx); + out = value_formatter_.format(*it, ctx); ++it; while (it != value.end) { out = detail::copy_str<Char>(value.sep.begin(), value.sep.end(), out); ctx.advance_to(out); - out = value_formatter_.format(map(*it), ctx); + out = value_formatter_.format(*it, ctx); ++it; } } @@ -4000,11 +4287,12 @@ auto join(Range&& range, string_view sep) std::string answer = fmt::to_string(42); \endrst */ -template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)> +template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value && + !detail::has_format_as<T>::value)> inline auto to_string(const T& value) -> std::string { - auto result = std::string(); - detail::write<char>(std::back_inserter(result), value); - return result; + auto buffer = memory_buffer(); + detail::write<char>(appender(buffer), value); + return {buffer.data(), buffer.size()}; } template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)> @@ -4025,27 +4313,19 @@ FMT_NODISCARD auto to_string(const basic_memory_buffer<Char, SIZE>& buf) return std::basic_string<Char>(buf.data(), size); } -FMT_BEGIN_DETAIL_NAMESPACE +template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value && + detail::has_format_as<T>::value)> +inline auto to_string(const T& value) -> std::string { + return to_string(format_as(value)); +} + +FMT_END_EXPORT + +namespace detail { template <typename Char> -void vformat_to( - buffer<Char>& buf, basic_string_view<Char> fmt, - basic_format_args<FMT_BUFFER_CONTEXT(type_identity_t<Char>)> args, - locale_ref loc) { - // workaround for msvc bug regarding name-lookup in module - // link names into function scope - using detail::arg_formatter; - using detail::buffer_appender; - using detail::custom_formatter; - using detail::default_arg_formatter; - using detail::get_arg; - using detail::locale_ref; - using detail::parse_format_specs; - using detail::specs_checker; - using detail::specs_handler; - using detail::to_unsigned; - using detail::type; - using detail::write; +void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt, + typename vformat_args<Char>::type args, locale_ref loc) { auto out = buffer_appender<Char>(buf); if (fmt.size() == 2 && equal2(fmt.data(), "{}")) { auto arg = args.get(0); @@ -4092,15 +4372,16 @@ void vformat_to( -> const Char* { auto arg = get_arg(context, id); if (arg.type() == type::custom_type) { - parse_context.advance_to(parse_context.begin() + - (begin - &*parse_context.begin())); + parse_context.advance_to(begin); visit_format_arg(custom_formatter<Char>{parse_context, context}, arg); return parse_context.begin(); } - auto specs = basic_format_specs<Char>(); - specs_checker<specs_handler<Char>> handler( - specs_handler<Char>(specs, parse_context, context), arg.type()); - begin = parse_format_specs(begin, end, handler); + auto specs = detail::dynamic_format_specs<Char>(); + begin = parse_format_specs(begin, end, specs, parse_context, arg.type()); + detail::handle_dynamic_spec<detail::width_checker>( + specs.width, specs.width_ref, context); + detail::handle_dynamic_spec<detail::precision_checker>( + specs.precision, specs.precision_ref, context); if (begin == end || *begin != '}') on_error("missing '}' in format string"); auto f = arg_formatter<Char>{context.out(), specs, context.locale()}; @@ -4111,7 +4392,12 @@ void vformat_to( detail::parse_format_string<false>(fmt, format_handler(out, fmt, args, loc)); } +FMT_BEGIN_EXPORT + #ifndef FMT_HEADER_ONLY +extern template FMT_API void vformat_to(buffer<char>&, string_view, + typename vformat_args<>::type, + locale_ref); extern template FMT_API auto thousands_sep_impl<char>(locale_ref) -> thousands_sep_result<char>; extern template FMT_API auto thousands_sep_impl<wchar_t>(locale_ref) @@ -4120,7 +4406,7 @@ extern template FMT_API auto decimal_point_impl(locale_ref) -> char; extern template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t; #endif // FMT_HEADER_ONLY -FMT_END_DETAIL_NAMESPACE +} // namespace detail #if FMT_USE_USER_DEFINED_LITERALS inline namespace literals { @@ -4157,7 +4443,7 @@ template <typename Locale, typename... T, FMT_ENABLE_IF(detail::is_locale<Locale>::value)> inline auto format(const Locale& loc, format_string<T...> fmt, T&&... args) -> std::string { - return vformat(loc, string_view(fmt), fmt::make_format_args(args...)); + return fmt::vformat(loc, string_view(fmt), fmt::make_format_args(args...)); } template <typename OutputIt, typename Locale, @@ -4168,7 +4454,7 @@ auto vformat_to(OutputIt out, const Locale& loc, string_view fmt, using detail::get_buffer; auto&& buf = get_buffer<char>(out); detail::vformat_to(buf, fmt, args, detail::locale_ref(loc)); - return detail::get_iterator(buf); + return detail::get_iterator(buf, out); } template <typename OutputIt, typename Locale, typename... T, @@ -4179,7 +4465,39 @@ FMT_INLINE auto format_to(OutputIt out, const Locale& loc, return vformat_to(out, loc, fmt, fmt::make_format_args(args...)); } -FMT_MODULE_EXPORT_END +template <typename Locale, typename... T, + FMT_ENABLE_IF(detail::is_locale<Locale>::value)> +FMT_NODISCARD FMT_INLINE auto formatted_size(const Locale& loc, + format_string<T...> fmt, + T&&... args) -> size_t { + auto buf = detail::counting_buffer<>(); + detail::vformat_to<char>(buf, fmt, fmt::make_format_args(args...), + detail::locale_ref(loc)); + return buf.count(); +} + +FMT_END_EXPORT + +template <typename T, typename Char> +template <typename FormatContext> +FMT_CONSTEXPR FMT_INLINE auto +formatter<T, Char, + enable_if_t<detail::type_constant<T, Char>::value != + detail::type::custom_type>>::format(const T& val, + FormatContext& ctx) + const -> decltype(ctx.out()) { + if (specs_.width_ref.kind != detail::arg_id_kind::none || + specs_.precision_ref.kind != detail::arg_id_kind::none) { + auto specs = specs_; + detail::handle_dynamic_spec<detail::width_checker>(specs.width, + specs.width_ref, ctx); + detail::handle_dynamic_spec<detail::precision_checker>( + specs.precision, specs.precision_ref, ctx); + return detail::write<Char>(ctx.out(), val, specs, ctx.locale()); + } + return detail::write<Char>(ctx.out(), val, specs_, ctx.locale()); +} + FMT_END_NAMESPACE #ifdef FMT_HEADER_ONLY |