You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4735 lines
170 KiB

  1. /*
  2. Formatting library for C++
  3. Copyright (c) 2012 - present, Victor Zverovich
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. --- Optional exception to the license ---
  21. As an exception, if, as a result of your compiling your source code, portions
  22. of this Software are embedded into a machine-executable object form of such
  23. source code, you may redistribute such embedded portions in such object form
  24. without including the above copyright and permission notices.
  25. */
  26. #ifndef FMT_FORMAT_H_
  27. #define FMT_FORMAT_H_
  28. #include <cmath> // std::signbit
  29. #include <cstdint> // uint32_t
  30. #include <cstring> // std::memcpy
  31. #include <initializer_list> // std::initializer_list
  32. #include <limits> // std::numeric_limits
  33. #include <memory> // std::uninitialized_copy
  34. #include <stdexcept> // std::runtime_error
  35. #include <system_error> // std::system_error
  36. #ifdef __cpp_lib_bit_cast
  37. # include <bit> // std::bitcast
  38. #endif
  39. #include "core.h"
  40. #ifndef FMT_BEGIN_DETAIL_NAMESPACE
  41. # define FMT_BEGIN_DETAIL_NAMESPACE namespace detail {
  42. # define FMT_END_DETAIL_NAMESPACE }
  43. #endif
  44. #if FMT_HAS_CPP17_ATTRIBUTE(fallthrough)
  45. # define FMT_FALLTHROUGH [[fallthrough]]
  46. #elif defined(__clang__)
  47. # define FMT_FALLTHROUGH [[clang::fallthrough]]
  48. #elif FMT_GCC_VERSION >= 700 && \
  49. (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 520)
  50. # define FMT_FALLTHROUGH [[gnu::fallthrough]]
  51. #else
  52. # define FMT_FALLTHROUGH
  53. #endif
  54. #ifndef FMT_DEPRECATED
  55. # if FMT_HAS_CPP14_ATTRIBUTE(deprecated) || FMT_MSC_VERSION >= 1900
  56. # define FMT_DEPRECATED [[deprecated]]
  57. # else
  58. # if (defined(__GNUC__) && !defined(__LCC__)) || defined(__clang__)
  59. # define FMT_DEPRECATED __attribute__((deprecated))
  60. # elif FMT_MSC_VERSION
  61. # define FMT_DEPRECATED __declspec(deprecated)
  62. # else
  63. # define FMT_DEPRECATED /* deprecated */
  64. # endif
  65. # endif
  66. #endif
  67. #if FMT_GCC_VERSION
  68. # define FMT_GCC_VISIBILITY_HIDDEN __attribute__((visibility("hidden")))
  69. #else
  70. # define FMT_GCC_VISIBILITY_HIDDEN
  71. #endif
  72. #ifdef __NVCC__
  73. # define FMT_CUDA_VERSION (__CUDACC_VER_MAJOR__ * 100 + __CUDACC_VER_MINOR__)
  74. #else
  75. # define FMT_CUDA_VERSION 0
  76. #endif
  77. #ifdef __has_builtin
  78. # define FMT_HAS_BUILTIN(x) __has_builtin(x)
  79. #else
  80. # define FMT_HAS_BUILTIN(x) 0
  81. #endif
  82. #if FMT_GCC_VERSION || FMT_CLANG_VERSION
  83. # define FMT_NOINLINE __attribute__((noinline))
  84. #else
  85. # define FMT_NOINLINE
  86. #endif
  87. #ifndef FMT_THROW
  88. # if FMT_EXCEPTIONS
  89. # if FMT_MSC_VERSION || defined(__NVCC__)
  90. FMT_BEGIN_NAMESPACE
  91. namespace detail {
  92. template <typename Exception> inline void do_throw(const Exception& x) {
  93. // Silence unreachable code warnings in MSVC and NVCC because these
  94. // are nearly impossible to fix in a generic code.
  95. volatile bool b = true;
  96. if (b) throw x;
  97. }
  98. } // namespace detail
  99. FMT_END_NAMESPACE
  100. # define FMT_THROW(x) detail::do_throw(x)
  101. # else
  102. # define FMT_THROW(x) throw x
  103. # endif
  104. # else
  105. # define FMT_THROW(x) \
  106. do { \
  107. FMT_ASSERT(false, (x).what()); \
  108. } while (false)
  109. # endif
  110. #endif
  111. #if FMT_EXCEPTIONS
  112. # define FMT_TRY try
  113. # define FMT_CATCH(x) catch (x)
  114. #else
  115. # define FMT_TRY if (true)
  116. # define FMT_CATCH(x) if (false)
  117. #endif
  118. #ifndef FMT_MAYBE_UNUSED
  119. # if FMT_HAS_CPP17_ATTRIBUTE(maybe_unused)
  120. # define FMT_MAYBE_UNUSED [[maybe_unused]]
  121. # else
  122. # define FMT_MAYBE_UNUSED
  123. # endif
  124. #endif
  125. #ifndef FMT_USE_USER_DEFINED_LITERALS
  126. // EDG based compilers (Intel, NVIDIA, Elbrus, etc), GCC and MSVC support UDLs.
  127. # if (FMT_HAS_FEATURE(cxx_user_literals) || FMT_GCC_VERSION >= 407 || \
  128. FMT_MSC_VERSION >= 1900) && \
  129. (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= /* UDL feature */ 480)
  130. # define FMT_USE_USER_DEFINED_LITERALS 1
  131. # else
  132. # define FMT_USE_USER_DEFINED_LITERALS 0
  133. # endif
  134. #endif
  135. // Defining FMT_REDUCE_INT_INSTANTIATIONS to 1, will reduce the number of
  136. // integer formatter template instantiations to just one by only using the
  137. // largest integer type. This results in a reduction in binary size but will
  138. // cause a decrease in integer formatting performance.
  139. #if !defined(FMT_REDUCE_INT_INSTANTIATIONS)
  140. # define FMT_REDUCE_INT_INSTANTIATIONS 0
  141. #endif
  142. // __builtin_clz is broken in clang with Microsoft CodeGen:
  143. // https://github.com/fmtlib/fmt/issues/519.
  144. #if !FMT_MSC_VERSION
  145. # if FMT_HAS_BUILTIN(__builtin_clz) || FMT_GCC_VERSION || FMT_ICC_VERSION
  146. # define FMT_BUILTIN_CLZ(n) __builtin_clz(n)
  147. # endif
  148. # if FMT_HAS_BUILTIN(__builtin_clzll) || FMT_GCC_VERSION || FMT_ICC_VERSION
  149. # define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n)
  150. # endif
  151. #endif
  152. // __builtin_ctz is broken in Intel Compiler Classic on Windows:
  153. // https://github.com/fmtlib/fmt/issues/2510.
  154. #ifndef __ICL
  155. # if FMT_HAS_BUILTIN(__builtin_ctz) || FMT_GCC_VERSION || FMT_ICC_VERSION || \
  156. defined(__NVCOMPILER)
  157. # define FMT_BUILTIN_CTZ(n) __builtin_ctz(n)
  158. # endif
  159. # if FMT_HAS_BUILTIN(__builtin_ctzll) || FMT_GCC_VERSION || \
  160. FMT_ICC_VERSION || defined(__NVCOMPILER)
  161. # define FMT_BUILTIN_CTZLL(n) __builtin_ctzll(n)
  162. # endif
  163. #endif
  164. #if FMT_MSC_VERSION
  165. # include <intrin.h> // _BitScanReverse[64], _BitScanForward[64], _umul128
  166. #endif
  167. // Some compilers masquerade as both MSVC and GCC-likes or otherwise support
  168. // __builtin_clz and __builtin_clzll, so only define FMT_BUILTIN_CLZ using the
  169. // MSVC intrinsics if the clz and clzll builtins are not available.
  170. #if FMT_MSC_VERSION && !defined(FMT_BUILTIN_CLZLL) && \
  171. !defined(FMT_BUILTIN_CTZLL)
  172. FMT_BEGIN_NAMESPACE
  173. namespace detail {
  174. // Avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning.
  175. # if !defined(__clang__)
  176. # pragma intrinsic(_BitScanForward)
  177. # pragma intrinsic(_BitScanReverse)
  178. # if defined(_WIN64)
  179. # pragma intrinsic(_BitScanForward64)
  180. # pragma intrinsic(_BitScanReverse64)
  181. # endif
  182. # endif
  183. inline auto clz(uint32_t x) -> int {
  184. unsigned long r = 0;
  185. _BitScanReverse(&r, x);
  186. FMT_ASSERT(x != 0, "");
  187. // Static analysis complains about using uninitialized data
  188. // "r", but the only way that can happen is if "x" is 0,
  189. // which the callers guarantee to not happen.
  190. FMT_MSC_WARNING(suppress : 6102)
  191. return 31 ^ static_cast<int>(r);
  192. }
  193. # define FMT_BUILTIN_CLZ(n) detail::clz(n)
  194. inline auto clzll(uint64_t x) -> int {
  195. unsigned long r = 0;
  196. # ifdef _WIN64
  197. _BitScanReverse64(&r, x);
  198. # else
  199. // Scan the high 32 bits.
  200. if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32)))
  201. return 63 ^ static_cast<int>(r + 32);
  202. // Scan the low 32 bits.
  203. _BitScanReverse(&r, static_cast<uint32_t>(x));
  204. # endif
  205. FMT_ASSERT(x != 0, "");
  206. FMT_MSC_WARNING(suppress : 6102) // Suppress a bogus static analysis warning.
  207. return 63 ^ static_cast<int>(r);
  208. }
  209. # define FMT_BUILTIN_CLZLL(n) detail::clzll(n)
  210. inline auto ctz(uint32_t x) -> int {
  211. unsigned long r = 0;
  212. _BitScanForward(&r, x);
  213. FMT_ASSERT(x != 0, "");
  214. FMT_MSC_WARNING(suppress : 6102) // Suppress a bogus static analysis warning.
  215. return static_cast<int>(r);
  216. }
  217. # define FMT_BUILTIN_CTZ(n) detail::ctz(n)
  218. inline auto ctzll(uint64_t x) -> int {
  219. unsigned long r = 0;
  220. FMT_ASSERT(x != 0, "");
  221. FMT_MSC_WARNING(suppress : 6102) // Suppress a bogus static analysis warning.
  222. # ifdef _WIN64
  223. _BitScanForward64(&r, x);
  224. # else
  225. // Scan the low 32 bits.
  226. if (_BitScanForward(&r, static_cast<uint32_t>(x))) return static_cast<int>(r);
  227. // Scan the high 32 bits.
  228. _BitScanForward(&r, static_cast<uint32_t>(x >> 32));
  229. r += 32;
  230. # endif
  231. return static_cast<int>(r);
  232. }
  233. # define FMT_BUILTIN_CTZLL(n) detail::ctzll(n)
  234. } // namespace detail
  235. FMT_END_NAMESPACE
  236. #endif
  237. FMT_BEGIN_NAMESPACE
  238. template <typename...> struct disjunction : std::false_type {};
  239. template <typename P> struct disjunction<P> : P {};
  240. template <typename P1, typename... Pn>
  241. struct disjunction<P1, Pn...>
  242. : conditional_t<bool(P1::value), P1, disjunction<Pn...>> {};
  243. template <typename...> struct conjunction : std::true_type {};
  244. template <typename P> struct conjunction<P> : P {};
  245. template <typename P1, typename... Pn>
  246. struct conjunction<P1, Pn...>
  247. : conditional_t<bool(P1::value), conjunction<Pn...>, P1> {};
  248. namespace detail {
  249. FMT_CONSTEXPR inline void abort_fuzzing_if(bool condition) {
  250. ignore_unused(condition);
  251. #ifdef FMT_FUZZ
  252. if (condition) throw std::runtime_error("fuzzing limit reached");
  253. #endif
  254. }
  255. template <typename CharT, CharT... C> struct string_literal {
  256. static constexpr CharT value[sizeof...(C)] = {C...};
  257. constexpr operator basic_string_view<CharT>() const {
  258. return {value, sizeof...(C)};
  259. }
  260. };
  261. #if FMT_CPLUSPLUS < 201703L
  262. template <typename CharT, CharT... C>
  263. constexpr CharT string_literal<CharT, C...>::value[sizeof...(C)];
  264. #endif
  265. template <typename Streambuf> class formatbuf : public Streambuf {
  266. private:
  267. using char_type = typename Streambuf::char_type;
  268. using streamsize = decltype(std::declval<Streambuf>().sputn(nullptr, 0));
  269. using int_type = typename Streambuf::int_type;
  270. using traits_type = typename Streambuf::traits_type;
  271. buffer<char_type>& buffer_;
  272. public:
  273. explicit formatbuf(buffer<char_type>& buf) : buffer_(buf) {}
  274. protected:
  275. // The put area is always empty. This makes the implementation simpler and has
  276. // the advantage that the streambuf and the buffer are always in sync and
  277. // sputc never writes into uninitialized memory. A disadvantage is that each
  278. // call to sputc always results in a (virtual) call to overflow. There is no
  279. // disadvantage here for sputn since this always results in a call to xsputn.
  280. auto overflow(int_type ch) -> int_type override {
  281. if (!traits_type::eq_int_type(ch, traits_type::eof()))
  282. buffer_.push_back(static_cast<char_type>(ch));
  283. return ch;
  284. }
  285. auto xsputn(const char_type* s, streamsize count) -> streamsize override {
  286. buffer_.append(s, s + count);
  287. return count;
  288. }
  289. };
  290. // Implementation of std::bit_cast for pre-C++20.
  291. template <typename To, typename From, FMT_ENABLE_IF(sizeof(To) == sizeof(From))>
  292. FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To {
  293. #ifdef __cpp_lib_bit_cast
  294. if (is_constant_evaluated()) return std::bit_cast<To>(from);
  295. #endif
  296. auto to = To();
  297. // The cast suppresses a bogus -Wclass-memaccess on GCC.
  298. std::memcpy(static_cast<void*>(&to), &from, sizeof(to));
  299. return to;
  300. }
  301. inline auto is_big_endian() -> bool {
  302. #ifdef _WIN32
  303. return false;
  304. #elif defined(__BIG_ENDIAN__)
  305. return true;
  306. #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
  307. return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
  308. #else
  309. struct bytes {
  310. char data[sizeof(int)];
  311. };
  312. return bit_cast<bytes>(1).data[0] == 0;
  313. #endif
  314. }
  315. class uint128_fallback {
  316. private:
  317. uint64_t lo_, hi_;
  318. friend uint128_fallback umul128(uint64_t x, uint64_t y) noexcept;
  319. public:
  320. constexpr uint128_fallback(uint64_t hi, uint64_t lo) : lo_(lo), hi_(hi) {}
  321. constexpr uint128_fallback(uint64_t value = 0) : lo_(value), hi_(0) {}
  322. constexpr uint64_t high() const noexcept { return hi_; }
  323. constexpr uint64_t low() const noexcept { return lo_; }
  324. template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
  325. constexpr explicit operator T() const {
  326. return static_cast<T>(lo_);
  327. }
  328. friend constexpr auto operator==(const uint128_fallback& lhs,
  329. const uint128_fallback& rhs) -> bool {
  330. return lhs.hi_ == rhs.hi_ && lhs.lo_ == rhs.lo_;
  331. }
  332. friend constexpr auto operator!=(const uint128_fallback& lhs,
  333. const uint128_fallback& rhs) -> bool {
  334. return !(lhs == rhs);
  335. }
  336. friend constexpr auto operator>(const uint128_fallback& lhs,
  337. const uint128_fallback& rhs) -> bool {
  338. return lhs.hi_ != rhs.hi_ ? lhs.hi_ > rhs.hi_ : lhs.lo_ > rhs.lo_;
  339. }
  340. friend constexpr auto operator|(const uint128_fallback& lhs,
  341. const uint128_fallback& rhs)
  342. -> uint128_fallback {
  343. return {lhs.hi_ | rhs.hi_, lhs.lo_ | rhs.lo_};
  344. }
  345. friend constexpr auto operator&(const uint128_fallback& lhs,
  346. const uint128_fallback& rhs)
  347. -> uint128_fallback {
  348. return {lhs.hi_ & rhs.hi_, lhs.lo_ & rhs.lo_};
  349. }
  350. friend constexpr auto operator~(const uint128_fallback& n)
  351. -> uint128_fallback {
  352. return {~n.hi_, ~n.lo_};
  353. }
  354. friend auto operator+(const uint128_fallback& lhs,
  355. const uint128_fallback& rhs) -> uint128_fallback {
  356. auto result = uint128_fallback(lhs);
  357. result += rhs;
  358. return result;
  359. }
  360. friend auto operator*(const uint128_fallback& lhs, uint32_t rhs)
  361. -> uint128_fallback {
  362. FMT_ASSERT(lhs.hi_ == 0, "");
  363. uint64_t hi = (lhs.lo_ >> 32) * rhs;
  364. uint64_t lo = (lhs.lo_ & ~uint32_t()) * rhs;
  365. uint64_t new_lo = (hi << 32) + lo;
  366. return {(hi >> 32) + (new_lo < lo ? 1 : 0), new_lo};
  367. }
  368. friend auto operator-(const uint128_fallback& lhs, uint64_t rhs)
  369. -> uint128_fallback {
  370. return {lhs.hi_ - (lhs.lo_ < rhs ? 1 : 0), lhs.lo_ - rhs};
  371. }
  372. FMT_CONSTEXPR auto operator>>(int shift) const -> uint128_fallback {
  373. if (shift == 64) return {0, hi_};
  374. if (shift > 64) return uint128_fallback(0, hi_) >> (shift - 64);
  375. return {hi_ >> shift, (hi_ << (64 - shift)) | (lo_ >> shift)};
  376. }
  377. FMT_CONSTEXPR auto operator<<(int shift) const -> uint128_fallback {
  378. if (shift == 64) return {lo_, 0};
  379. if (shift > 64) return uint128_fallback(lo_, 0) << (shift - 64);
  380. return {hi_ << shift | (lo_ >> (64 - shift)), (lo_ << shift)};
  381. }
  382. FMT_CONSTEXPR auto operator>>=(int shift) -> uint128_fallback& {
  383. return *this = *this >> shift;
  384. }
  385. FMT_CONSTEXPR void operator+=(uint128_fallback n) {
  386. uint64_t new_lo = lo_ + n.lo_;
  387. uint64_t new_hi = hi_ + n.hi_ + (new_lo < lo_ ? 1 : 0);
  388. FMT_ASSERT(new_hi >= hi_, "");
  389. lo_ = new_lo;
  390. hi_ = new_hi;
  391. }
  392. FMT_CONSTEXPR void operator&=(uint128_fallback n) {
  393. lo_ &= n.lo_;
  394. hi_ &= n.hi_;
  395. }
  396. FMT_CONSTEXPR20 uint128_fallback& operator+=(uint64_t n) noexcept {
  397. if (is_constant_evaluated()) {
  398. lo_ += n;
  399. hi_ += (lo_ < n ? 1 : 0);
  400. return *this;
  401. }
  402. #if FMT_HAS_BUILTIN(__builtin_addcll) && !defined(__ibmxl__)
  403. unsigned long long carry;
  404. lo_ = __builtin_addcll(lo_, n, 0, &carry);
  405. hi_ += carry;
  406. #elif FMT_HAS_BUILTIN(__builtin_ia32_addcarryx_u64) && !defined(__ibmxl__)
  407. unsigned long long result;
  408. auto carry = __builtin_ia32_addcarryx_u64(0, lo_, n, &result);
  409. lo_ = result;
  410. hi_ += carry;
  411. #elif defined(_MSC_VER) && defined(_M_X64)
  412. auto carry = _addcarry_u64(0, lo_, n, &lo_);
  413. _addcarry_u64(carry, hi_, 0, &hi_);
  414. #else
  415. lo_ += n;
  416. hi_ += (lo_ < n ? 1 : 0);
  417. #endif
  418. return *this;
  419. }
  420. };
  421. using uint128_t = conditional_t<FMT_USE_INT128, uint128_opt, uint128_fallback>;
  422. #ifdef UINTPTR_MAX
  423. using uintptr_t = ::uintptr_t;
  424. #else
  425. using uintptr_t = uint128_t;
  426. #endif
  427. // Returns the largest possible value for type T. Same as
  428. // std::numeric_limits<T>::max() but shorter and not affected by the max macro.
  429. template <typename T> constexpr auto max_value() -> T {
  430. return (std::numeric_limits<T>::max)();
  431. }
  432. template <typename T> constexpr auto num_bits() -> int {
  433. return std::numeric_limits<T>::digits;
  434. }
  435. // std::numeric_limits<T>::digits may return 0 for 128-bit ints.
  436. template <> constexpr auto num_bits<int128_opt>() -> int { return 128; }
  437. template <> constexpr auto num_bits<uint128_t>() -> int { return 128; }
  438. // A heterogeneous bit_cast used for converting 96-bit long double to uint128_t
  439. // and 128-bit pointers to uint128_fallback.
  440. template <typename To, typename From, FMT_ENABLE_IF(sizeof(To) > sizeof(From))>
  441. inline auto bit_cast(const From& from) -> To {
  442. constexpr auto size = static_cast<int>(sizeof(From) / sizeof(unsigned));
  443. struct data_t {
  444. unsigned value[static_cast<unsigned>(size)];
  445. } data = bit_cast<data_t>(from);
  446. auto result = To();
  447. if (const_check(is_big_endian())) {
  448. for (int i = 0; i < size; ++i)
  449. result = (result << num_bits<unsigned>()) | data.value[i];
  450. } else {
  451. for (int i = size - 1; i >= 0; --i)
  452. result = (result << num_bits<unsigned>()) | data.value[i];
  453. }
  454. return result;
  455. }
  456. template <typename UInt>
  457. FMT_CONSTEXPR20 inline auto countl_zero_fallback(UInt n) -> int {
  458. int lz = 0;
  459. constexpr UInt msb_mask = static_cast<UInt>(1) << (num_bits<UInt>() - 1);
  460. for (; (n & msb_mask) == 0; n <<= 1) lz++;
  461. return lz;
  462. }
  463. FMT_CONSTEXPR20 inline auto countl_zero(uint32_t n) -> int {
  464. #ifdef FMT_BUILTIN_CLZ
  465. if (!is_constant_evaluated()) return FMT_BUILTIN_CLZ(n);
  466. #endif
  467. return countl_zero_fallback(n);
  468. }
  469. FMT_CONSTEXPR20 inline auto countl_zero(uint64_t n) -> int {
  470. #ifdef FMT_BUILTIN_CLZLL
  471. if (!is_constant_evaluated()) return FMT_BUILTIN_CLZLL(n);
  472. #endif
  473. return countl_zero_fallback(n);
  474. }
  475. FMT_INLINE void assume(bool condition) {
  476. (void)condition;
  477. #if FMT_HAS_BUILTIN(__builtin_assume) && !FMT_ICC_VERSION
  478. __builtin_assume(condition);
  479. #endif
  480. }
  481. // An approximation of iterator_t for pre-C++20 systems.
  482. template <typename T>
  483. using iterator_t = decltype(std::begin(std::declval<T&>()));
  484. template <typename T> using sentinel_t = decltype(std::end(std::declval<T&>()));
  485. // A workaround for std::string not having mutable data() until C++17.
  486. template <typename Char>
  487. inline auto get_data(std::basic_string<Char>& s) -> Char* {
  488. return &s[0];
  489. }
  490. template <typename Container>
  491. inline auto get_data(Container& c) -> typename Container::value_type* {
  492. return c.data();
  493. }
  494. #if defined(_SECURE_SCL) && _SECURE_SCL
  495. // Make a checked iterator to avoid MSVC warnings.
  496. template <typename T> using checked_ptr = stdext::checked_array_iterator<T*>;
  497. template <typename T>
  498. constexpr auto make_checked(T* p, size_t size) -> checked_ptr<T> {
  499. return {p, size};
  500. }
  501. #else
  502. template <typename T> using checked_ptr = T*;
  503. template <typename T> constexpr auto make_checked(T* p, size_t) -> T* {
  504. return p;
  505. }
  506. #endif
  507. // Attempts to reserve space for n extra characters in the output range.
  508. // Returns a pointer to the reserved range or a reference to it.
  509. template <typename Container, FMT_ENABLE_IF(is_contiguous<Container>::value)>
  510. #if FMT_CLANG_VERSION >= 307 && !FMT_ICC_VERSION
  511. __attribute__((no_sanitize("undefined")))
  512. #endif
  513. inline auto
  514. reserve(std::back_insert_iterator<Container> it, size_t n)
  515. -> checked_ptr<typename Container::value_type> {
  516. Container& c = get_container(it);
  517. size_t size = c.size();
  518. c.resize(size + n);
  519. return make_checked(get_data(c) + size, n);
  520. }
  521. template <typename T>
  522. inline auto reserve(buffer_appender<T> it, size_t n) -> buffer_appender<T> {
  523. buffer<T>& buf = get_container(it);
  524. buf.try_reserve(buf.size() + n);
  525. return it;
  526. }
  527. template <typename Iterator>
  528. constexpr auto reserve(Iterator& it, size_t) -> Iterator& {
  529. return it;
  530. }
  531. template <typename OutputIt>
  532. using reserve_iterator =
  533. remove_reference_t<decltype(reserve(std::declval<OutputIt&>(), 0))>;
  534. template <typename T, typename OutputIt>
  535. constexpr auto to_pointer(OutputIt, size_t) -> T* {
  536. return nullptr;
  537. }
  538. template <typename T> auto to_pointer(buffer_appender<T> it, size_t n) -> T* {
  539. buffer<T>& buf = get_container(it);
  540. auto size = buf.size();
  541. if (buf.capacity() < size + n) return nullptr;
  542. buf.try_resize(size + n);
  543. return buf.data() + size;
  544. }
  545. template <typename Container, FMT_ENABLE_IF(is_contiguous<Container>::value)>
  546. inline auto base_iterator(std::back_insert_iterator<Container>& it,
  547. checked_ptr<typename Container::value_type>)
  548. -> std::back_insert_iterator<Container> {
  549. return it;
  550. }
  551. template <typename Iterator>
  552. constexpr auto base_iterator(Iterator, Iterator it) -> Iterator {
  553. return it;
  554. }
  555. // <algorithm> is spectacularly slow to compile in C++20 so use a simple fill_n
  556. // instead (#1998).
  557. template <typename OutputIt, typename Size, typename T>
  558. FMT_CONSTEXPR auto fill_n(OutputIt out, Size count, const T& value)
  559. -> OutputIt {
  560. for (Size i = 0; i < count; ++i) *out++ = value;
  561. return out;
  562. }
  563. template <typename T, typename Size>
  564. FMT_CONSTEXPR20 auto fill_n(T* out, Size count, char value) -> T* {
  565. if (is_constant_evaluated()) {
  566. return fill_n<T*, Size, T>(out, count, value);
  567. }
  568. std::memset(out, value, to_unsigned(count));
  569. return out + count;
  570. }
  571. #ifdef __cpp_char8_t
  572. using char8_type = char8_t;
  573. #else
  574. enum char8_type : unsigned char {};
  575. #endif
  576. template <typename OutChar, typename InputIt, typename OutputIt>
  577. FMT_CONSTEXPR FMT_NOINLINE auto copy_str_noinline(InputIt begin, InputIt end,
  578. OutputIt out) -> OutputIt {
  579. return copy_str<OutChar>(begin, end, out);
  580. }
  581. // A public domain branchless UTF-8 decoder by Christopher Wellons:
  582. // https://github.com/skeeto/branchless-utf8
  583. /* Decode the next character, c, from s, reporting errors in e.
  584. *
  585. * Since this is a branchless decoder, four bytes will be read from the
  586. * buffer regardless of the actual length of the next character. This
  587. * means the buffer _must_ have at least three bytes of zero padding
  588. * following the end of the data stream.
  589. *
  590. * Errors are reported in e, which will be non-zero if the parsed
  591. * character was somehow invalid: invalid byte sequence, non-canonical
  592. * encoding, or a surrogate half.
  593. *
  594. * The function returns a pointer to the next character. When an error
  595. * occurs, this pointer will be a guess that depends on the particular
  596. * error, but it will always advance at least one byte.
  597. */
  598. FMT_CONSTEXPR inline auto utf8_decode(const char* s, uint32_t* c, int* e)
  599. -> const char* {
  600. constexpr const int masks[] = {0x00, 0x7f, 0x1f, 0x0f, 0x07};
  601. constexpr const uint32_t mins[] = {4194304, 0, 128, 2048, 65536};
  602. constexpr const int shiftc[] = {0, 18, 12, 6, 0};
  603. constexpr const int shifte[] = {0, 6, 4, 2, 0};
  604. 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"
  605. [static_cast<unsigned char>(*s) >> 3];
  606. // Compute the pointer to the next character early so that the next
  607. // iteration can start working on the next character. Neither Clang
  608. // nor GCC figure out this reordering on their own.
  609. const char* next = s + len + !len;
  610. using uchar = unsigned char;
  611. // Assume a four-byte character and load four bytes. Unused bits are
  612. // shifted out.
  613. *c = uint32_t(uchar(s[0]) & masks[len]) << 18;
  614. *c |= uint32_t(uchar(s[1]) & 0x3f) << 12;
  615. *c |= uint32_t(uchar(s[2]) & 0x3f) << 6;
  616. *c |= uint32_t(uchar(s[3]) & 0x3f) << 0;
  617. *c >>= shiftc[len];
  618. // Accumulate the various error conditions.
  619. *e = (*c < mins[len]) << 6; // non-canonical encoding
  620. *e |= ((*c >> 11) == 0x1b) << 7; // surrogate half?
  621. *e |= (*c > 0x10FFFF) << 8; // out of range?
  622. *e |= (uchar(s[1]) & 0xc0) >> 2;
  623. *e |= (uchar(s[2]) & 0xc0) >> 4;
  624. *e |= uchar(s[3]) >> 6;
  625. *e ^= 0x2a; // top two bits of each tail byte correct?
  626. *e >>= shifte[len];
  627. return next;
  628. }
  629. constexpr FMT_INLINE_VARIABLE uint32_t invalid_code_point = ~uint32_t();
  630. // Invokes f(cp, sv) for every code point cp in s with sv being the string view
  631. // corresponding to the code point. cp is invalid_code_point on error.
  632. template <typename F>
  633. FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) {
  634. auto decode = [f](const char* buf_ptr, const char* ptr) {
  635. auto cp = uint32_t();
  636. auto error = 0;
  637. auto end = utf8_decode(buf_ptr, &cp, &error);
  638. bool result = f(error ? invalid_code_point : cp,
  639. string_view(ptr, error ? 1 : to_unsigned(end - buf_ptr)));
  640. return result ? (error ? buf_ptr + 1 : end) : nullptr;
  641. };
  642. auto p = s.data();
  643. const size_t block_size = 4; // utf8_decode always reads blocks of 4 chars.
  644. if (s.size() >= block_size) {
  645. for (auto end = p + s.size() - block_size + 1; p < end;) {
  646. p = decode(p, p);
  647. if (!p) return;
  648. }
  649. }
  650. if (auto num_chars_left = s.data() + s.size() - p) {
  651. char buf[2 * block_size - 1] = {};
  652. copy_str<char>(p, p + num_chars_left, buf);
  653. const char* buf_ptr = buf;
  654. do {
  655. auto end = decode(buf_ptr, p);
  656. if (!end) return;
  657. p += end - buf_ptr;
  658. buf_ptr = end;
  659. } while (buf_ptr - buf < num_chars_left);
  660. }
  661. }
  662. template <typename Char>
  663. inline auto compute_width(basic_string_view<Char> s) -> size_t {
  664. return s.size();
  665. }
  666. // Computes approximate display width of a UTF-8 string.
  667. FMT_CONSTEXPR inline size_t compute_width(string_view s) {
  668. size_t num_code_points = 0;
  669. // It is not a lambda for compatibility with C++14.
  670. struct count_code_points {
  671. size_t* count;
  672. FMT_CONSTEXPR auto operator()(uint32_t cp, string_view) const -> bool {
  673. *count += detail::to_unsigned(
  674. 1 +
  675. (cp >= 0x1100 &&
  676. (cp <= 0x115f || // Hangul Jamo init. consonants
  677. cp == 0x2329 || // LEFT-POINTING ANGLE BRACKET
  678. cp == 0x232a || // RIGHT-POINTING ANGLE BRACKET
  679. // CJK ... Yi except IDEOGRAPHIC HALF FILL SPACE:
  680. (cp >= 0x2e80 && cp <= 0xa4cf && cp != 0x303f) ||
  681. (cp >= 0xac00 && cp <= 0xd7a3) || // Hangul Syllables
  682. (cp >= 0xf900 && cp <= 0xfaff) || // CJK Compatibility Ideographs
  683. (cp >= 0xfe10 && cp <= 0xfe19) || // Vertical Forms
  684. (cp >= 0xfe30 && cp <= 0xfe6f) || // CJK Compatibility Forms
  685. (cp >= 0xff00 && cp <= 0xff60) || // Fullwidth Forms
  686. (cp >= 0xffe0 && cp <= 0xffe6) || // Fullwidth Forms
  687. (cp >= 0x20000 && cp <= 0x2fffd) || // CJK
  688. (cp >= 0x30000 && cp <= 0x3fffd) ||
  689. // Miscellaneous Symbols and Pictographs + Emoticons:
  690. (cp >= 0x1f300 && cp <= 0x1f64f) ||
  691. // Supplemental Symbols and Pictographs:
  692. (cp >= 0x1f900 && cp <= 0x1f9ff))));
  693. return true;
  694. }
  695. };
  696. // We could avoid branches by using utf8_decode directly.
  697. for_each_codepoint(s, count_code_points{&num_code_points});
  698. return num_code_points;
  699. }
  700. inline auto compute_width(basic_string_view<char8_type> s) -> size_t {
  701. return compute_width(
  702. string_view(reinterpret_cast<const char*>(s.data()), s.size()));
  703. }
  704. template <typename Char>
  705. inline auto code_point_index(basic_string_view<Char> s, size_t n) -> size_t {
  706. size_t size = s.size();
  707. return n < size ? n : size;
  708. }
  709. // Calculates the index of the nth code point in a UTF-8 string.
  710. inline auto code_point_index(string_view s, size_t n) -> size_t {
  711. const char* data = s.data();
  712. size_t num_code_points = 0;
  713. for (size_t i = 0, size = s.size(); i != size; ++i) {
  714. if ((data[i] & 0xc0) != 0x80 && ++num_code_points > n) return i;
  715. }
  716. return s.size();
  717. }
  718. inline auto code_point_index(basic_string_view<char8_type> s, size_t n)
  719. -> size_t {
  720. return code_point_index(
  721. string_view(reinterpret_cast<const char*>(s.data()), s.size()), n);
  722. }
  723. template <typename T> struct is_integral : std::is_integral<T> {};
  724. template <> struct is_integral<int128_opt> : std::true_type {};
  725. template <> struct is_integral<uint128_t> : std::true_type {};
  726. template <typename T>
  727. using is_signed =
  728. std::integral_constant<bool, std::numeric_limits<T>::is_signed ||
  729. std::is_same<T, int128_opt>::value>;
  730. template <typename T>
  731. using is_integer =
  732. bool_constant<is_integral<T>::value && !std::is_same<T, bool>::value &&
  733. !std::is_same<T, char>::value &&
  734. !std::is_same<T, wchar_t>::value>;
  735. #ifndef FMT_USE_FLOAT
  736. # define FMT_USE_FLOAT 1
  737. #endif
  738. #ifndef FMT_USE_DOUBLE
  739. # define FMT_USE_DOUBLE 1
  740. #endif
  741. #ifndef FMT_USE_LONG_DOUBLE
  742. # define FMT_USE_LONG_DOUBLE 1
  743. #endif
  744. #ifndef FMT_USE_FLOAT128
  745. # ifdef __clang__
  746. // Clang emulates GCC, so it has to appear early.
  747. # if FMT_HAS_INCLUDE(<quadmath.h>)
  748. # define FMT_USE_FLOAT128 1
  749. # endif
  750. # elif defined(__GNUC__)
  751. // GNU C++:
  752. # if defined(_GLIBCXX_USE_FLOAT128) && !defined(__STRICT_ANSI__)
  753. # define FMT_USE_FLOAT128 1
  754. # endif
  755. # endif
  756. # ifndef FMT_USE_FLOAT128
  757. # define FMT_USE_FLOAT128 0
  758. # endif
  759. #endif
  760. #if FMT_USE_FLOAT128
  761. using float128 = __float128;
  762. #else
  763. using float128 = void;
  764. #endif
  765. template <typename T> using is_float128 = std::is_same<T, float128>;
  766. template <typename T>
  767. using is_floating_point =
  768. bool_constant<std::is_floating_point<T>::value || is_float128<T>::value>;
  769. template <typename T, bool = std::is_floating_point<T>::value>
  770. struct is_fast_float : bool_constant<std::numeric_limits<T>::is_iec559 &&
  771. sizeof(T) <= sizeof(double)> {};
  772. template <typename T> struct is_fast_float<T, false> : std::false_type {};
  773. template <typename T>
  774. using is_double_double = bool_constant<std::numeric_limits<T>::digits == 106>;
  775. #ifndef FMT_USE_FULL_CACHE_DRAGONBOX
  776. # define FMT_USE_FULL_CACHE_DRAGONBOX 0
  777. #endif
  778. template <typename T>
  779. template <typename U>
  780. void buffer<T>::append(const U* begin, const U* end) {
  781. while (begin != end) {
  782. auto count = to_unsigned(end - begin);
  783. try_reserve(size_ + count);
  784. auto free_cap = capacity_ - size_;
  785. if (free_cap < count) count = free_cap;
  786. std::uninitialized_copy_n(begin, count, make_checked(ptr_ + size_, count));
  787. size_ += count;
  788. begin += count;
  789. }
  790. }
  791. template <typename T, typename Enable = void>
  792. struct is_locale : std::false_type {};
  793. template <typename T>
  794. struct is_locale<T, void_t<decltype(T::classic())>> : std::true_type {};
  795. } // namespace detail
  796. FMT_BEGIN_EXPORT
  797. // The number of characters to store in the basic_memory_buffer object itself
  798. // to avoid dynamic memory allocation.
  799. enum { inline_buffer_size = 500 };
  800. /**
  801. \rst
  802. A dynamically growing memory buffer for trivially copyable/constructible types
  803. with the first ``SIZE`` elements stored in the object itself.
  804. You can use the ``memory_buffer`` type alias for ``char`` instead.
  805. **Example**::
  806. auto out = fmt::memory_buffer();
  807. format_to(std::back_inserter(out), "The answer is {}.", 42);
  808. This will append the following output to the ``out`` object:
  809. .. code-block:: none
  810. The answer is 42.
  811. The output can be converted to an ``std::string`` with ``to_string(out)``.
  812. \endrst
  813. */
  814. template <typename T, size_t SIZE = inline_buffer_size,
  815. typename Allocator = std::allocator<T>>
  816. class basic_memory_buffer final : public detail::buffer<T> {
  817. private:
  818. T store_[SIZE];
  819. // Don't inherit from Allocator avoid generating type_info for it.
  820. Allocator alloc_;
  821. // Deallocate memory allocated by the buffer.
  822. FMT_CONSTEXPR20 void deallocate() {
  823. T* data = this->data();
  824. if (data != store_) alloc_.deallocate(data, this->capacity());
  825. }
  826. protected:
  827. FMT_CONSTEXPR20 void grow(size_t size) override {
  828. detail::abort_fuzzing_if(size > 5000);
  829. const size_t max_size = std::allocator_traits<Allocator>::max_size(alloc_);
  830. size_t old_capacity = this->capacity();
  831. size_t new_capacity = old_capacity + old_capacity / 2;
  832. if (size > new_capacity)
  833. new_capacity = size;
  834. else if (new_capacity > max_size)
  835. new_capacity = size > max_size ? size : max_size;
  836. T* old_data = this->data();
  837. T* new_data =
  838. std::allocator_traits<Allocator>::allocate(alloc_, new_capacity);
  839. // The following code doesn't throw, so the raw pointer above doesn't leak.
  840. std::uninitialized_copy(old_data, old_data + this->size(),
  841. detail::make_checked(new_data, new_capacity));
  842. this->set(new_data, new_capacity);
  843. // deallocate must not throw according to the standard, but even if it does,
  844. // the buffer already uses the new storage and will deallocate it in
  845. // destructor.
  846. if (old_data != store_) alloc_.deallocate(old_data, old_capacity);
  847. }
  848. public:
  849. using value_type = T;
  850. using const_reference = const T&;
  851. FMT_CONSTEXPR20 explicit basic_memory_buffer(
  852. const Allocator& alloc = Allocator())
  853. : alloc_(alloc) {
  854. this->set(store_, SIZE);
  855. if (detail::is_constant_evaluated()) detail::fill_n(store_, SIZE, T());
  856. }
  857. FMT_CONSTEXPR20 ~basic_memory_buffer() { deallocate(); }
  858. private:
  859. // Move data from other to this buffer.
  860. FMT_CONSTEXPR20 void move(basic_memory_buffer& other) {
  861. alloc_ = std::move(other.alloc_);
  862. T* data = other.data();
  863. size_t size = other.size(), capacity = other.capacity();
  864. if (data == other.store_) {
  865. this->set(store_, capacity);
  866. detail::copy_str<T>(other.store_, other.store_ + size,
  867. detail::make_checked(store_, capacity));
  868. } else {
  869. this->set(data, capacity);
  870. // Set pointer to the inline array so that delete is not called
  871. // when deallocating.
  872. other.set(other.store_, 0);
  873. other.clear();
  874. }
  875. this->resize(size);
  876. }
  877. public:
  878. /**
  879. \rst
  880. Constructs a :class:`fmt::basic_memory_buffer` object moving the content
  881. of the other object to it.
  882. \endrst
  883. */
  884. FMT_CONSTEXPR20 basic_memory_buffer(basic_memory_buffer&& other) noexcept {
  885. move(other);
  886. }
  887. /**
  888. \rst
  889. Moves the content of the other ``basic_memory_buffer`` object to this one.
  890. \endrst
  891. */
  892. auto operator=(basic_memory_buffer&& other) noexcept -> basic_memory_buffer& {
  893. FMT_ASSERT(this != &other, "");
  894. deallocate();
  895. move(other);
  896. return *this;
  897. }
  898. // Returns a copy of the allocator associated with this buffer.
  899. auto get_allocator() const -> Allocator { return alloc_; }
  900. /**
  901. Resizes the buffer to contain *count* elements. If T is a POD type new
  902. elements may not be initialized.
  903. */
  904. FMT_CONSTEXPR20 void resize(size_t count) { this->try_resize(count); }
  905. /** Increases the buffer capacity to *new_capacity*. */
  906. void reserve(size_t new_capacity) { this->try_reserve(new_capacity); }
  907. // Directly append data into the buffer
  908. using detail::buffer<T>::append;
  909. template <typename ContiguousRange>
  910. void append(const ContiguousRange& range) {
  911. append(range.data(), range.data() + range.size());
  912. }
  913. };
  914. using memory_buffer = basic_memory_buffer<char>;
  915. template <typename T, size_t SIZE, typename Allocator>
  916. struct is_contiguous<basic_memory_buffer<T, SIZE, Allocator>> : std::true_type {
  917. };
  918. FMT_END_EXPORT
  919. namespace detail {
  920. FMT_API bool write_console(std::FILE* f, string_view text);
  921. FMT_API void print(std::FILE*, string_view);
  922. } // namespace detail
  923. FMT_BEGIN_EXPORT
  924. // Suppress a misleading warning in older versions of clang.
  925. #if FMT_CLANG_VERSION
  926. # pragma clang diagnostic ignored "-Wweak-vtables"
  927. #endif
  928. /** An error reported from a formatting function. */
  929. class FMT_API format_error : public std::runtime_error {
  930. public:
  931. using std::runtime_error::runtime_error;
  932. };
  933. namespace detail_exported {
  934. #if FMT_USE_NONTYPE_TEMPLATE_ARGS
  935. template <typename Char, size_t N> struct fixed_string {
  936. constexpr fixed_string(const Char (&str)[N]) {
  937. detail::copy_str<Char, const Char*, Char*>(static_cast<const Char*>(str),
  938. str + N, data);
  939. }
  940. Char data[N] = {};
  941. };
  942. #endif
  943. // Converts a compile-time string to basic_string_view.
  944. template <typename Char, size_t N>
  945. constexpr auto compile_string_to_view(const Char (&s)[N])
  946. -> basic_string_view<Char> {
  947. // Remove trailing NUL character if needed. Won't be present if this is used
  948. // with a raw character array (i.e. not defined as a string).
  949. return {s, N - (std::char_traits<Char>::to_int_type(s[N - 1]) == 0 ? 1 : 0)};
  950. }
  951. template <typename Char>
  952. constexpr auto compile_string_to_view(detail::std_string_view<Char> s)
  953. -> basic_string_view<Char> {
  954. return {s.data(), s.size()};
  955. }
  956. } // namespace detail_exported
  957. class loc_value {
  958. private:
  959. basic_format_arg<format_context> value_;
  960. public:
  961. template <typename T, FMT_ENABLE_IF(!detail::is_float128<T>::value)>
  962. loc_value(T value) : value_(detail::make_arg<format_context>(value)) {}
  963. template <typename T, FMT_ENABLE_IF(detail::is_float128<T>::value)>
  964. loc_value(T) {}
  965. template <typename Visitor> auto visit(Visitor&& vis) -> decltype(vis(0)) {
  966. return visit_format_arg(vis, value_);
  967. }
  968. };
  969. // A locale facet that formats values in UTF-8.
  970. // It is parameterized on the locale to avoid the heavy <locale> include.
  971. template <typename Locale> class format_facet : public Locale::facet {
  972. private:
  973. std::string separator_;
  974. std::string grouping_;
  975. std::string decimal_point_;
  976. protected:
  977. virtual auto do_put(appender out, loc_value val,
  978. const format_specs<>& specs) const -> bool;
  979. public:
  980. static FMT_API typename Locale::id id;
  981. explicit format_facet(Locale& loc);
  982. explicit format_facet(string_view sep = "",
  983. std::initializer_list<unsigned char> g = {3},
  984. std::string decimal_point = ".")
  985. : separator_(sep.data(), sep.size()),
  986. grouping_(g.begin(), g.end()),
  987. decimal_point_(decimal_point) {}
  988. auto put(appender out, loc_value val, const format_specs<>& specs) const
  989. -> bool {
  990. return do_put(out, val, specs);
  991. }
  992. };
  993. FMT_BEGIN_DETAIL_NAMESPACE
  994. // Returns true if value is negative, false otherwise.
  995. // Same as `value < 0` but doesn't produce warnings if T is an unsigned type.
  996. template <typename T, FMT_ENABLE_IF(is_signed<T>::value)>
  997. constexpr auto is_negative(T value) -> bool {
  998. return value < 0;
  999. }
  1000. template <typename T, FMT_ENABLE_IF(!is_signed<T>::value)>
  1001. constexpr auto is_negative(T) -> bool {
  1002. return false;
  1003. }
  1004. template <typename T>
  1005. FMT_CONSTEXPR auto is_supported_floating_point(T) -> bool {
  1006. if (std::is_same<T, float>()) return FMT_USE_FLOAT;
  1007. if (std::is_same<T, double>()) return FMT_USE_DOUBLE;
  1008. if (std::is_same<T, long double>()) return FMT_USE_LONG_DOUBLE;
  1009. return true;
  1010. }
  1011. // Smallest of uint32_t, uint64_t, uint128_t that is large enough to
  1012. // represent all values of an integral type T.
  1013. template <typename T>
  1014. using uint32_or_64_or_128_t =
  1015. conditional_t<num_bits<T>() <= 32 && !FMT_REDUCE_INT_INSTANTIATIONS,
  1016. uint32_t,
  1017. conditional_t<num_bits<T>() <= 64, uint64_t, uint128_t>>;
  1018. template <typename T>
  1019. using uint64_or_128_t = conditional_t<num_bits<T>() <= 64, uint64_t, uint128_t>;
  1020. #define FMT_POWERS_OF_10(factor) \
  1021. factor * 10, (factor)*100, (factor)*1000, (factor)*10000, (factor)*100000, \
  1022. (factor)*1000000, (factor)*10000000, (factor)*100000000, \
  1023. (factor)*1000000000
  1024. // Converts value in the range [0, 100) to a string.
  1025. constexpr const char* digits2(size_t value) {
  1026. // GCC generates slightly better code when value is pointer-size.
  1027. return &"0001020304050607080910111213141516171819"
  1028. "2021222324252627282930313233343536373839"
  1029. "4041424344454647484950515253545556575859"
  1030. "6061626364656667686970717273747576777879"
  1031. "8081828384858687888990919293949596979899"[value * 2];
  1032. }
  1033. // Sign is a template parameter to workaround a bug in gcc 4.8.
  1034. template <typename Char, typename Sign> constexpr Char sign(Sign s) {
  1035. #if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 604
  1036. static_assert(std::is_same<Sign, sign_t>::value, "");
  1037. #endif
  1038. return static_cast<Char>("\0-+ "[s]);
  1039. }
  1040. template <typename T> FMT_CONSTEXPR auto count_digits_fallback(T n) -> int {
  1041. int count = 1;
  1042. for (;;) {
  1043. // Integer division is slow so do it for a group of four digits instead
  1044. // of for every digit. The idea comes from the talk by Alexandrescu
  1045. // "Three Optimization Tips for C++". See speed-test for a comparison.
  1046. if (n < 10) return count;
  1047. if (n < 100) return count + 1;
  1048. if (n < 1000) return count + 2;
  1049. if (n < 10000) return count + 3;
  1050. n /= 10000u;
  1051. count += 4;
  1052. }
  1053. }
  1054. #if FMT_USE_INT128
  1055. FMT_CONSTEXPR inline auto count_digits(uint128_opt n) -> int {
  1056. return count_digits_fallback(n);
  1057. }
  1058. #endif
  1059. #ifdef FMT_BUILTIN_CLZLL
  1060. // It is a separate function rather than a part of count_digits to workaround
  1061. // the lack of static constexpr in constexpr functions.
  1062. inline auto do_count_digits(uint64_t n) -> int {
  1063. // This has comparable performance to the version by Kendall Willets
  1064. // (https://github.com/fmtlib/format-benchmark/blob/master/digits10)
  1065. // but uses smaller tables.
  1066. // Maps bsr(n) to ceil(log10(pow(2, bsr(n) + 1) - 1)).
  1067. static constexpr uint8_t bsr2log10[] = {
  1068. 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
  1069. 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10,
  1070. 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
  1071. 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20};
  1072. auto t = bsr2log10[FMT_BUILTIN_CLZLL(n | 1) ^ 63];
  1073. static constexpr const uint64_t zero_or_powers_of_10[] = {
  1074. 0, 0, FMT_POWERS_OF_10(1U), FMT_POWERS_OF_10(1000000000ULL),
  1075. 10000000000000000000ULL};
  1076. return t - (n < zero_or_powers_of_10[t]);
  1077. }
  1078. #endif
  1079. // Returns the number of decimal digits in n. Leading zeros are not counted
  1080. // except for n == 0 in which case count_digits returns 1.
  1081. FMT_CONSTEXPR20 inline auto count_digits(uint64_t n) -> int {
  1082. #ifdef FMT_BUILTIN_CLZLL
  1083. if (!is_constant_evaluated()) {
  1084. return do_count_digits(n);
  1085. }
  1086. #endif
  1087. return count_digits_fallback(n);
  1088. }
  1089. // Counts the number of digits in n. BITS = log2(radix).
  1090. template <int BITS, typename UInt>
  1091. FMT_CONSTEXPR auto count_digits(UInt n) -> int {
  1092. #ifdef FMT_BUILTIN_CLZ
  1093. if (!is_constant_evaluated() && num_bits<UInt>() == 32)
  1094. return (FMT_BUILTIN_CLZ(static_cast<uint32_t>(n) | 1) ^ 31) / BITS + 1;
  1095. #endif
  1096. // Lambda avoids unreachable code warnings from NVHPC.
  1097. return [](UInt m) {
  1098. int num_digits = 0;
  1099. do {
  1100. ++num_digits;
  1101. } while ((m >>= BITS) != 0);
  1102. return num_digits;
  1103. }(n);
  1104. }
  1105. #ifdef FMT_BUILTIN_CLZ
  1106. // It is a separate function rather than a part of count_digits to workaround
  1107. // the lack of static constexpr in constexpr functions.
  1108. FMT_INLINE auto do_count_digits(uint32_t n) -> int {
  1109. // An optimization by Kendall Willets from https://bit.ly/3uOIQrB.
  1110. // This increments the upper 32 bits (log10(T) - 1) when >= T is added.
  1111. # define FMT_INC(T) (((sizeof(# T) - 1ull) << 32) - T)
  1112. static constexpr uint64_t table[] = {
  1113. FMT_INC(0), FMT_INC(0), FMT_INC(0), // 8
  1114. FMT_INC(10), FMT_INC(10), FMT_INC(10), // 64
  1115. FMT_INC(100), FMT_INC(100), FMT_INC(100), // 512
  1116. FMT_INC(1000), FMT_INC(1000), FMT_INC(1000), // 4096
  1117. FMT_INC(10000), FMT_INC(10000), FMT_INC(10000), // 32k
  1118. FMT_INC(100000), FMT_INC(100000), FMT_INC(100000), // 256k
  1119. FMT_INC(1000000), FMT_INC(1000000), FMT_INC(1000000), // 2048k
  1120. FMT_INC(10000000), FMT_INC(10000000), FMT_INC(10000000), // 16M
  1121. FMT_INC(100000000), FMT_INC(100000000), FMT_INC(100000000), // 128M
  1122. FMT_INC(1000000000), FMT_INC(1000000000), FMT_INC(1000000000), // 1024M
  1123. FMT_INC(1000000000), FMT_INC(1000000000) // 4B
  1124. };
  1125. auto inc = table[FMT_BUILTIN_CLZ(n | 1) ^ 31];
  1126. return static_cast<int>((n + inc) >> 32);
  1127. }
  1128. #endif
  1129. // Optional version of count_digits for better performance on 32-bit platforms.
  1130. FMT_CONSTEXPR20 inline auto count_digits(uint32_t n) -> int {
  1131. #ifdef FMT_BUILTIN_CLZ
  1132. if (!is_constant_evaluated()) {
  1133. return do_count_digits(n);
  1134. }
  1135. #endif
  1136. return count_digits_fallback(n);
  1137. }
  1138. template <typename Int> constexpr auto digits10() noexcept -> int {
  1139. return std::numeric_limits<Int>::digits10;
  1140. }
  1141. template <> constexpr auto digits10<int128_opt>() noexcept -> int { return 38; }
  1142. template <> constexpr auto digits10<uint128_t>() noexcept -> int { return 38; }
  1143. template <typename Char> struct thousands_sep_result {
  1144. std::string grouping;
  1145. Char thousands_sep;
  1146. };
  1147. template <typename Char>
  1148. FMT_API auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result<Char>;
  1149. template <typename Char>
  1150. inline auto thousands_sep(locale_ref loc) -> thousands_sep_result<Char> {
  1151. auto result = thousands_sep_impl<char>(loc);
  1152. return {result.grouping, Char(result.thousands_sep)};
  1153. }
  1154. template <>
  1155. inline auto thousands_sep(locale_ref loc) -> thousands_sep_result<wchar_t> {
  1156. return thousands_sep_impl<wchar_t>(loc);
  1157. }
  1158. template <typename Char>
  1159. FMT_API auto decimal_point_impl(locale_ref loc) -> Char;
  1160. template <typename Char> inline auto decimal_point(locale_ref loc) -> Char {
  1161. return Char(decimal_point_impl<char>(loc));
  1162. }
  1163. template <> inline auto decimal_point(locale_ref loc) -> wchar_t {
  1164. return decimal_point_impl<wchar_t>(loc);
  1165. }
  1166. // Compares two characters for equality.
  1167. template <typename Char> auto equal2(const Char* lhs, const char* rhs) -> bool {
  1168. return lhs[0] == Char(rhs[0]) && lhs[1] == Char(rhs[1]);
  1169. }
  1170. inline auto equal2(const char* lhs, const char* rhs) -> bool {
  1171. return memcmp(lhs, rhs, 2) == 0;
  1172. }
  1173. // Copies two characters from src to dst.
  1174. template <typename Char>
  1175. FMT_CONSTEXPR20 FMT_INLINE void copy2(Char* dst, const char* src) {
  1176. if (!is_constant_evaluated() && sizeof(Char) == sizeof(char)) {
  1177. memcpy(dst, src, 2);
  1178. return;
  1179. }
  1180. *dst++ = static_cast<Char>(*src++);
  1181. *dst = static_cast<Char>(*src);
  1182. }
  1183. template <typename Iterator> struct format_decimal_result {
  1184. Iterator begin;
  1185. Iterator end;
  1186. };
  1187. // Formats a decimal unsigned integer value writing into out pointing to a
  1188. // buffer of specified size. The caller must ensure that the buffer is large
  1189. // enough.
  1190. template <typename Char, typename UInt>
  1191. FMT_CONSTEXPR20 auto format_decimal(Char* out, UInt value, int size)
  1192. -> format_decimal_result<Char*> {
  1193. FMT_ASSERT(size >= count_digits(value), "invalid digit count");
  1194. out += size;
  1195. Char* end = out;
  1196. while (value >= 100) {
  1197. // Integer division is slow so do it for a group of two digits instead
  1198. // of for every digit. The idea comes from the talk by Alexandrescu
  1199. // "Three Optimization Tips for C++". See speed-test for a comparison.
  1200. out -= 2;
  1201. copy2(out, digits2(static_cast<size_t>(value % 100)));
  1202. value /= 100;
  1203. }
  1204. if (value < 10) {
  1205. *--out = static_cast<Char>('0' + value);
  1206. return {out, end};
  1207. }
  1208. out -= 2;
  1209. copy2(out, digits2(static_cast<size_t>(value)));
  1210. return {out, end};
  1211. }
  1212. template <typename Char, typename UInt, typename Iterator,
  1213. FMT_ENABLE_IF(!std::is_pointer<remove_cvref_t<Iterator>>::value)>
  1214. FMT_CONSTEXPR inline auto format_decimal(Iterator out, UInt value, int size)
  1215. -> format_decimal_result<Iterator> {
  1216. // Buffer is large enough to hold all digits (digits10 + 1).
  1217. Char buffer[digits10<UInt>() + 1] = {};
  1218. auto end = format_decimal(buffer, value, size).end;
  1219. return {out, detail::copy_str_noinline<Char>(buffer, end, out)};
  1220. }
  1221. template <unsigned BASE_BITS, typename Char, typename UInt>
  1222. FMT_CONSTEXPR auto format_uint(Char* buffer, UInt value, int num_digits,
  1223. bool upper = false) -> Char* {
  1224. buffer += num_digits;
  1225. Char* end = buffer;
  1226. do {
  1227. const char* digits = upper ? "0123456789ABCDEF" : "0123456789abcdef";
  1228. unsigned digit = static_cast<unsigned>(value & ((1 << BASE_BITS) - 1));
  1229. *--buffer = static_cast<Char>(BASE_BITS < 4 ? static_cast<char>('0' + digit)
  1230. : digits[digit]);
  1231. } while ((value >>= BASE_BITS) != 0);
  1232. return end;
  1233. }
  1234. template <unsigned BASE_BITS, typename Char, typename It, typename UInt>
  1235. inline auto format_uint(It out, UInt value, int num_digits, bool upper = false)
  1236. -> It {
  1237. if (auto ptr = to_pointer<Char>(out, to_unsigned(num_digits))) {
  1238. format_uint<BASE_BITS>(ptr, value, num_digits, upper);
  1239. return out;
  1240. }
  1241. // Buffer should be large enough to hold all digits (digits / BASE_BITS + 1).
  1242. char buffer[num_bits<UInt>() / BASE_BITS + 1];
  1243. format_uint<BASE_BITS>(buffer, value, num_digits, upper);
  1244. return detail::copy_str_noinline<Char>(buffer, buffer + num_digits, out);
  1245. }
  1246. // A converter from UTF-8 to UTF-16.
  1247. class utf8_to_utf16 {
  1248. private:
  1249. basic_memory_buffer<wchar_t> buffer_;
  1250. public:
  1251. FMT_API explicit utf8_to_utf16(string_view s);
  1252. operator basic_string_view<wchar_t>() const { return {&buffer_[0], size()}; }
  1253. auto size() const -> size_t { return buffer_.size() - 1; }
  1254. auto c_str() const -> const wchar_t* { return &buffer_[0]; }
  1255. auto str() const -> std::wstring { return {&buffer_[0], size()}; }
  1256. };
  1257. // A converter from UTF-16/UTF-32 (host endian) to UTF-8.
  1258. template <typename WChar, typename Buffer = memory_buffer>
  1259. class unicode_to_utf8 {
  1260. private:
  1261. Buffer buffer_;
  1262. public:
  1263. unicode_to_utf8() {}
  1264. explicit unicode_to_utf8(basic_string_view<WChar> s) {
  1265. static_assert(sizeof(WChar) == 2 || sizeof(WChar) == 4,
  1266. "Expect utf16 or utf32");
  1267. if (!convert(s))
  1268. FMT_THROW(std::runtime_error(sizeof(WChar) == 2 ? "invalid utf16"
  1269. : "invalid utf32"));
  1270. }
  1271. operator string_view() const { return string_view(&buffer_[0], size()); }
  1272. size_t size() const { return buffer_.size() - 1; }
  1273. const char* c_str() const { return &buffer_[0]; }
  1274. std::string str() const { return std::string(&buffer_[0], size()); }
  1275. // Performs conversion returning a bool instead of throwing exception on
  1276. // conversion error. This method may still throw in case of memory allocation
  1277. // error.
  1278. bool convert(basic_string_view<WChar> s) {
  1279. if (!convert(buffer_, s)) return false;
  1280. buffer_.push_back(0);
  1281. return true;
  1282. }
  1283. static bool convert(Buffer& buf, basic_string_view<WChar> s) {
  1284. for (auto p = s.begin(); p != s.end(); ++p) {
  1285. uint32_t c = static_cast<uint32_t>(*p);
  1286. if (sizeof(WChar) == 2 && c >= 0xd800 && c <= 0xdfff) {
  1287. // surrogate pair
  1288. ++p;
  1289. if (p == s.end() || (c & 0xfc00) != 0xd800 || (*p & 0xfc00) != 0xdc00) {
  1290. return false;
  1291. }
  1292. c = (c << 10) + static_cast<uint32_t>(*p) - 0x35fdc00;
  1293. }
  1294. if (c < 0x80) {
  1295. buf.push_back(static_cast<char>(c));
  1296. } else if (c < 0x800) {
  1297. buf.push_back(static_cast<char>(0xc0 | (c >> 6)));
  1298. buf.push_back(static_cast<char>(0x80 | (c & 0x3f)));
  1299. } else if ((c >= 0x800 && c <= 0xd7ff) || (c >= 0xe000 && c <= 0xffff)) {
  1300. buf.push_back(static_cast<char>(0xe0 | (c >> 12)));
  1301. buf.push_back(static_cast<char>(0x80 | ((c & 0xfff) >> 6)));
  1302. buf.push_back(static_cast<char>(0x80 | (c & 0x3f)));
  1303. } else if (c >= 0x10000 && c <= 0x10ffff) {
  1304. buf.push_back(static_cast<char>(0xf0 | (c >> 18)));
  1305. buf.push_back(static_cast<char>(0x80 | ((c & 0x3ffff) >> 12)));
  1306. buf.push_back(static_cast<char>(0x80 | ((c & 0xfff) >> 6)));
  1307. buf.push_back(static_cast<char>(0x80 | (c & 0x3f)));
  1308. } else {
  1309. return false;
  1310. }
  1311. }
  1312. return true;
  1313. }
  1314. };
  1315. // Computes 128-bit result of multiplication of two 64-bit unsigned integers.
  1316. inline uint128_fallback umul128(uint64_t x, uint64_t y) noexcept {
  1317. #if FMT_USE_INT128
  1318. auto p = static_cast<uint128_opt>(x) * static_cast<uint128_opt>(y);
  1319. return {static_cast<uint64_t>(p >> 64), static_cast<uint64_t>(p)};
  1320. #elif defined(_MSC_VER) && defined(_M_X64)
  1321. auto result = uint128_fallback();
  1322. result.lo_ = _umul128(x, y, &result.hi_);
  1323. return result;
  1324. #else
  1325. const uint64_t mask = static_cast<uint64_t>(max_value<uint32_t>());
  1326. uint64_t a = x >> 32;
  1327. uint64_t b = x & mask;
  1328. uint64_t c = y >> 32;
  1329. uint64_t d = y & mask;
  1330. uint64_t ac = a * c;
  1331. uint64_t bc = b * c;
  1332. uint64_t ad = a * d;
  1333. uint64_t bd = b * d;
  1334. uint64_t intermediate = (bd >> 32) + (ad & mask) + (bc & mask);
  1335. return {ac + (intermediate >> 32) + (ad >> 32) + (bc >> 32),
  1336. (intermediate << 32) + (bd & mask)};
  1337. #endif
  1338. }
  1339. namespace dragonbox {
  1340. // Computes floor(log10(pow(2, e))) for e in [-2620, 2620] using the method from
  1341. // https://fmt.dev/papers/Dragonbox.pdf#page=28, section 6.1.
  1342. inline int floor_log10_pow2(int e) noexcept {
  1343. FMT_ASSERT(e <= 2620 && e >= -2620, "too large exponent");
  1344. static_assert((-1 >> 1) == -1, "right shift is not arithmetic");
  1345. return (e * 315653) >> 20;
  1346. }
  1347. inline int floor_log2_pow10(int e) noexcept {
  1348. FMT_ASSERT(e <= 1233 && e >= -1233, "too large exponent");
  1349. return (e * 1741647) >> 19;
  1350. }
  1351. // Computes upper 64 bits of multiplication of two 64-bit unsigned integers.
  1352. inline uint64_t umul128_upper64(uint64_t x, uint64_t y) noexcept {
  1353. #if FMT_USE_INT128
  1354. auto p = static_cast<uint128_opt>(x) * static_cast<uint128_opt>(y);
  1355. return static_cast<uint64_t>(p >> 64);
  1356. #elif defined(_MSC_VER) && defined(_M_X64)
  1357. return __umulh(x, y);
  1358. #else
  1359. return umul128(x, y).high();
  1360. #endif
  1361. }
  1362. // Computes upper 128 bits of multiplication of a 64-bit unsigned integer and a
  1363. // 128-bit unsigned integer.
  1364. inline uint128_fallback umul192_upper128(uint64_t x,
  1365. uint128_fallback y) noexcept {
  1366. uint128_fallback r = umul128(x, y.high());
  1367. r += umul128_upper64(x, y.low());
  1368. return r;
  1369. }
  1370. FMT_API uint128_fallback get_cached_power(int k) noexcept;
  1371. // Type-specific information that Dragonbox uses.
  1372. template <typename T, typename Enable = void> struct float_info;
  1373. template <> struct float_info<float> {
  1374. using carrier_uint = uint32_t;
  1375. static const int exponent_bits = 8;
  1376. static const int kappa = 1;
  1377. static const int big_divisor = 100;
  1378. static const int small_divisor = 10;
  1379. static const int min_k = -31;
  1380. static const int max_k = 46;
  1381. static const int shorter_interval_tie_lower_threshold = -35;
  1382. static const int shorter_interval_tie_upper_threshold = -35;
  1383. };
  1384. template <> struct float_info<double> {
  1385. using carrier_uint = uint64_t;
  1386. static const int exponent_bits = 11;
  1387. static const int kappa = 2;
  1388. static const int big_divisor = 1000;
  1389. static const int small_divisor = 100;
  1390. static const int min_k = -292;
  1391. static const int max_k = 341;
  1392. static const int shorter_interval_tie_lower_threshold = -77;
  1393. static const int shorter_interval_tie_upper_threshold = -77;
  1394. };
  1395. // An 80- or 128-bit floating point number.
  1396. template <typename T>
  1397. struct float_info<T, enable_if_t<std::numeric_limits<T>::digits == 64 ||
  1398. std::numeric_limits<T>::digits == 113 ||
  1399. is_float128<T>::value>> {
  1400. using carrier_uint = detail::uint128_t;
  1401. static const int exponent_bits = 15;
  1402. };
  1403. // A double-double floating point number.
  1404. template <typename T>
  1405. struct float_info<T, enable_if_t<is_double_double<T>::value>> {
  1406. using carrier_uint = detail::uint128_t;
  1407. };
  1408. template <typename T> struct decimal_fp {
  1409. using significand_type = typename float_info<T>::carrier_uint;
  1410. significand_type significand;
  1411. int exponent;
  1412. };
  1413. template <typename T> FMT_API auto to_decimal(T x) noexcept -> decimal_fp<T>;
  1414. } // namespace dragonbox
  1415. // Returns true iff Float has the implicit bit which is not stored.
  1416. template <typename Float> constexpr bool has_implicit_bit() {
  1417. // An 80-bit FP number has a 64-bit significand an no implicit bit.
  1418. return std::numeric_limits<Float>::digits != 64;
  1419. }
  1420. // Returns the number of significand bits stored in Float. The implicit bit is
  1421. // not counted since it is not stored.
  1422. template <typename Float> constexpr int num_significand_bits() {
  1423. // std::numeric_limits may not support __float128.
  1424. return is_float128<Float>() ? 112
  1425. : (std::numeric_limits<Float>::digits -
  1426. (has_implicit_bit<Float>() ? 1 : 0));
  1427. }
  1428. template <typename Float>
  1429. constexpr auto exponent_mask() ->
  1430. typename dragonbox::float_info<Float>::carrier_uint {
  1431. using float_uint = typename dragonbox::float_info<Float>::carrier_uint;
  1432. return ((float_uint(1) << dragonbox::float_info<Float>::exponent_bits) - 1)
  1433. << num_significand_bits<Float>();
  1434. }
  1435. template <typename Float> constexpr auto exponent_bias() -> int {
  1436. // std::numeric_limits may not support __float128.
  1437. return is_float128<Float>() ? 16383
  1438. : std::numeric_limits<Float>::max_exponent - 1;
  1439. }
  1440. // Writes the exponent exp in the form "[+-]d{2,3}" to buffer.
  1441. template <typename Char, typename It>
  1442. FMT_CONSTEXPR auto write_exponent(int exp, It it) -> It {
  1443. FMT_ASSERT(-10000 < exp && exp < 10000, "exponent out of range");
  1444. if (exp < 0) {
  1445. *it++ = static_cast<Char>('-');
  1446. exp = -exp;
  1447. } else {
  1448. *it++ = static_cast<Char>('+');
  1449. }
  1450. if (exp >= 100) {
  1451. const char* top = digits2(to_unsigned(exp / 100));
  1452. if (exp >= 1000) *it++ = static_cast<Char>(top[0]);
  1453. *it++ = static_cast<Char>(top[1]);
  1454. exp %= 100;
  1455. }
  1456. const char* d = digits2(to_unsigned(exp));
  1457. *it++ = static_cast<Char>(d[0]);
  1458. *it++ = static_cast<Char>(d[1]);
  1459. return it;
  1460. }
  1461. // A floating-point number f * pow(2, e) where F is an unsigned type.
  1462. template <typename F> struct basic_fp {
  1463. F f;
  1464. int e;
  1465. static constexpr const int num_significand_bits =
  1466. static_cast<int>(sizeof(F) * num_bits<unsigned char>());
  1467. constexpr basic_fp() : f(0), e(0) {}
  1468. constexpr basic_fp(uint64_t f_val, int e_val) : f(f_val), e(e_val) {}
  1469. // Constructs fp from an IEEE754 floating-point number.
  1470. template <typename Float> FMT_CONSTEXPR basic_fp(Float n) { assign(n); }
  1471. // Assigns n to this and return true iff predecessor is closer than successor.
  1472. template <typename Float, FMT_ENABLE_IF(!is_double_double<Float>::value)>
  1473. FMT_CONSTEXPR auto assign(Float n) -> bool {
  1474. static_assert(std::numeric_limits<Float>::digits <= 113, "unsupported FP");
  1475. // Assume Float is in the format [sign][exponent][significand].
  1476. using carrier_uint = typename dragonbox::float_info<Float>::carrier_uint;
  1477. const auto num_float_significand_bits =
  1478. detail::num_significand_bits<Float>();
  1479. const auto implicit_bit = carrier_uint(1) << num_float_significand_bits;
  1480. const auto significand_mask = implicit_bit - 1;
  1481. auto u = bit_cast<carrier_uint>(n);
  1482. f = static_cast<F>(u & significand_mask);
  1483. auto biased_e = static_cast<int>((u & exponent_mask<Float>()) >>
  1484. num_float_significand_bits);
  1485. // The predecessor is closer if n is a normalized power of 2 (f == 0)
  1486. // other than the smallest normalized number (biased_e > 1).
  1487. auto is_predecessor_closer = f == 0 && biased_e > 1;
  1488. if (biased_e == 0)
  1489. biased_e = 1; // Subnormals use biased exponent 1 (min exponent).
  1490. else if (has_implicit_bit<Float>())
  1491. f += static_cast<F>(implicit_bit);
  1492. e = biased_e - exponent_bias<Float>() - num_float_significand_bits;
  1493. if (!has_implicit_bit<Float>()) ++e;
  1494. return is_predecessor_closer;
  1495. }
  1496. template <typename Float, FMT_ENABLE_IF(is_double_double<Float>::value)>
  1497. FMT_CONSTEXPR auto assign(Float n) -> bool {
  1498. static_assert(std::numeric_limits<double>::is_iec559, "unsupported FP");
  1499. return assign(static_cast<double>(n));
  1500. }
  1501. };
  1502. using fp = basic_fp<unsigned long long>;
  1503. // Normalizes the value converted from double and multiplied by (1 << SHIFT).
  1504. template <int SHIFT = 0, typename F>
  1505. FMT_CONSTEXPR basic_fp<F> normalize(basic_fp<F> value) {
  1506. // Handle subnormals.
  1507. const auto implicit_bit = F(1) << num_significand_bits<double>();
  1508. const auto shifted_implicit_bit = implicit_bit << SHIFT;
  1509. while ((value.f & shifted_implicit_bit) == 0) {
  1510. value.f <<= 1;
  1511. --value.e;
  1512. }
  1513. // Subtract 1 to account for hidden bit.
  1514. const auto offset = basic_fp<F>::num_significand_bits -
  1515. num_significand_bits<double>() - SHIFT - 1;
  1516. value.f <<= offset;
  1517. value.e -= offset;
  1518. return value;
  1519. }
  1520. // Computes lhs * rhs / pow(2, 64) rounded to nearest with half-up tie breaking.
  1521. FMT_CONSTEXPR inline uint64_t multiply(uint64_t lhs, uint64_t rhs) {
  1522. #if FMT_USE_INT128
  1523. auto product = static_cast<__uint128_t>(lhs) * rhs;
  1524. auto f = static_cast<uint64_t>(product >> 64);
  1525. return (static_cast<uint64_t>(product) & (1ULL << 63)) != 0 ? f + 1 : f;
  1526. #else
  1527. // Multiply 32-bit parts of significands.
  1528. uint64_t mask = (1ULL << 32) - 1;
  1529. uint64_t a = lhs >> 32, b = lhs & mask;
  1530. uint64_t c = rhs >> 32, d = rhs & mask;
  1531. uint64_t ac = a * c, bc = b * c, ad = a * d, bd = b * d;
  1532. // Compute mid 64-bit of result and round.
  1533. uint64_t mid = (bd >> 32) + (ad & mask) + (bc & mask) + (1U << 31);
  1534. return ac + (ad >> 32) + (bc >> 32) + (mid >> 32);
  1535. #endif
  1536. }
  1537. FMT_CONSTEXPR inline fp operator*(fp x, fp y) {
  1538. return {multiply(x.f, y.f), x.e + y.e + 64};
  1539. }
  1540. template <typename T = void> struct basic_data {
  1541. // Normalized 64-bit significands of pow(10, k), for k = -348, -340, ..., 340.
  1542. // These are generated by support/compute-powers.py.
  1543. static constexpr uint64_t pow10_significands[87] = {
  1544. 0xfa8fd5a0081c0288, 0xbaaee17fa23ebf76, 0x8b16fb203055ac76,
  1545. 0xcf42894a5dce35ea, 0x9a6bb0aa55653b2d, 0xe61acf033d1a45df,
  1546. 0xab70fe17c79ac6ca, 0xff77b1fcbebcdc4f, 0xbe5691ef416bd60c,
  1547. 0x8dd01fad907ffc3c, 0xd3515c2831559a83, 0x9d71ac8fada6c9b5,
  1548. 0xea9c227723ee8bcb, 0xaecc49914078536d, 0x823c12795db6ce57,
  1549. 0xc21094364dfb5637, 0x9096ea6f3848984f, 0xd77485cb25823ac7,
  1550. 0xa086cfcd97bf97f4, 0xef340a98172aace5, 0xb23867fb2a35b28e,
  1551. 0x84c8d4dfd2c63f3b, 0xc5dd44271ad3cdba, 0x936b9fcebb25c996,
  1552. 0xdbac6c247d62a584, 0xa3ab66580d5fdaf6, 0xf3e2f893dec3f126,
  1553. 0xb5b5ada8aaff80b8, 0x87625f056c7c4a8b, 0xc9bcff6034c13053,
  1554. 0x964e858c91ba2655, 0xdff9772470297ebd, 0xa6dfbd9fb8e5b88f,
  1555. 0xf8a95fcf88747d94, 0xb94470938fa89bcf, 0x8a08f0f8bf0f156b,
  1556. 0xcdb02555653131b6, 0x993fe2c6d07b7fac, 0xe45c10c42a2b3b06,
  1557. 0xaa242499697392d3, 0xfd87b5f28300ca0e, 0xbce5086492111aeb,
  1558. 0x8cbccc096f5088cc, 0xd1b71758e219652c, 0x9c40000000000000,
  1559. 0xe8d4a51000000000, 0xad78ebc5ac620000, 0x813f3978f8940984,
  1560. 0xc097ce7bc90715b3, 0x8f7e32ce7bea5c70, 0xd5d238a4abe98068,
  1561. 0x9f4f2726179a2245, 0xed63a231d4c4fb27, 0xb0de65388cc8ada8,
  1562. 0x83c7088e1aab65db, 0xc45d1df942711d9a, 0x924d692ca61be758,
  1563. 0xda01ee641a708dea, 0xa26da3999aef774a, 0xf209787bb47d6b85,
  1564. 0xb454e4a179dd1877, 0x865b86925b9bc5c2, 0xc83553c5c8965d3d,
  1565. 0x952ab45cfa97a0b3, 0xde469fbd99a05fe3, 0xa59bc234db398c25,
  1566. 0xf6c69a72a3989f5c, 0xb7dcbf5354e9bece, 0x88fcf317f22241e2,
  1567. 0xcc20ce9bd35c78a5, 0x98165af37b2153df, 0xe2a0b5dc971f303a,
  1568. 0xa8d9d1535ce3b396, 0xfb9b7cd9a4a7443c, 0xbb764c4ca7a44410,
  1569. 0x8bab8eefb6409c1a, 0xd01fef10a657842c, 0x9b10a4e5e9913129,
  1570. 0xe7109bfba19c0c9d, 0xac2820d9623bf429, 0x80444b5e7aa7cf85,
  1571. 0xbf21e44003acdd2d, 0x8e679c2f5e44ff8f, 0xd433179d9c8cb841,
  1572. 0x9e19db92b4e31ba9, 0xeb96bf6ebadf77d9, 0xaf87023b9bf0ee6b,
  1573. };
  1574. #if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
  1575. # pragma GCC diagnostic push
  1576. # pragma GCC diagnostic ignored "-Wnarrowing"
  1577. #endif
  1578. // Binary exponents of pow(10, k), for k = -348, -340, ..., 340, corresponding
  1579. // to significands above.
  1580. static constexpr int16_t pow10_exponents[87] = {
  1581. -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, -954,
  1582. -927, -901, -874, -847, -821, -794, -768, -741, -715, -688, -661,
  1583. -635, -608, -582, -555, -529, -502, -475, -449, -422, -396, -369,
  1584. -343, -316, -289, -263, -236, -210, -183, -157, -130, -103, -77,
  1585. -50, -24, 3, 30, 56, 83, 109, 136, 162, 189, 216,
  1586. 242, 269, 295, 322, 348, 375, 402, 428, 455, 481, 508,
  1587. 534, 561, 588, 614, 641, 667, 694, 720, 747, 774, 800,
  1588. 827, 853, 880, 907, 933, 960, 986, 1013, 1039, 1066};
  1589. #if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
  1590. # pragma GCC diagnostic pop
  1591. #endif
  1592. static constexpr uint64_t power_of_10_64[20] = {
  1593. 1, FMT_POWERS_OF_10(1ULL), FMT_POWERS_OF_10(1000000000ULL),
  1594. 10000000000000000000ULL};
  1595. // For checking rounding thresholds.
  1596. // The kth entry is chosen to be the smallest integer such that the
  1597. // upper 32-bits of 10^(k+1) times it is strictly bigger than 5 * 10^k.
  1598. static constexpr uint32_t fractional_part_rounding_thresholds[8] = {
  1599. 2576980378, // ceil(2^31 + 2^32/10^1)
  1600. 2190433321, // ceil(2^31 + 2^32/10^2)
  1601. 2151778616, // ceil(2^31 + 2^32/10^3)
  1602. 2147913145, // ceil(2^31 + 2^32/10^4)
  1603. 2147526598, // ceil(2^31 + 2^32/10^5)
  1604. 2147487943, // ceil(2^31 + 2^32/10^6)
  1605. 2147484078, // ceil(2^31 + 2^32/10^7)
  1606. 2147483691 // ceil(2^31 + 2^32/10^8)
  1607. };
  1608. };
  1609. #if FMT_CPLUSPLUS < 201703L
  1610. template <typename T> constexpr uint64_t basic_data<T>::pow10_significands[];
  1611. template <typename T> constexpr int16_t basic_data<T>::pow10_exponents[];
  1612. template <typename T> constexpr uint64_t basic_data<T>::power_of_10_64[];
  1613. template <typename T>
  1614. constexpr uint32_t basic_data<T>::fractional_part_rounding_thresholds[];
  1615. #endif
  1616. // This is a struct rather than an alias to avoid shadowing warnings in gcc.
  1617. struct data : basic_data<> {};
  1618. // Returns a cached power of 10 `c_k = c_k.f * pow(2, c_k.e)` such that its
  1619. // (binary) exponent satisfies `min_exponent <= c_k.e <= min_exponent + 28`.
  1620. FMT_CONSTEXPR inline fp get_cached_power(int min_exponent,
  1621. int& pow10_exponent) {
  1622. const int shift = 32;
  1623. // log10(2) = 0x0.4d104d427de7fbcc...
  1624. const int64_t significand = 0x4d104d427de7fbcc;
  1625. int index = static_cast<int>(
  1626. ((min_exponent + fp::num_significand_bits - 1) * (significand >> shift) +
  1627. ((int64_t(1) << shift) - 1)) // ceil
  1628. >> 32 // arithmetic shift
  1629. );
  1630. // Decimal exponent of the first (smallest) cached power of 10.
  1631. const int first_dec_exp = -348;
  1632. // Difference between 2 consecutive decimal exponents in cached powers of 10.
  1633. const int dec_exp_step = 8;
  1634. index = (index - first_dec_exp - 1) / dec_exp_step + 1;
  1635. pow10_exponent = first_dec_exp + index * dec_exp_step;
  1636. // Using *(x + index) instead of x[index] avoids an issue with some compilers
  1637. // using the EDG frontend (e.g. nvhpc/22.3 in C++17 mode).
  1638. return {*(data::pow10_significands + index),
  1639. *(data::pow10_exponents + index)};
  1640. }
  1641. template <typename T>
  1642. using convert_float_result =
  1643. conditional_t<std::is_same<T, float>::value ||
  1644. std::numeric_limits<T>::digits ==
  1645. std::numeric_limits<double>::digits,
  1646. double, T>;
  1647. template <typename T>
  1648. constexpr auto convert_float(T value) -> convert_float_result<T> {
  1649. return static_cast<convert_float_result<T>>(value);
  1650. }
  1651. template <typename OutputIt, typename Char>
  1652. FMT_NOINLINE FMT_CONSTEXPR auto fill(OutputIt it, size_t n,
  1653. const fill_t<Char>& fill) -> OutputIt {
  1654. auto fill_size = fill.size();
  1655. if (fill_size == 1) return detail::fill_n(it, n, fill[0]);
  1656. auto data = fill.data();
  1657. for (size_t i = 0; i < n; ++i)
  1658. it = copy_str<Char>(data, data + fill_size, it);
  1659. return it;
  1660. }
  1661. // Writes the output of f, padded according to format specifications in specs.
  1662. // size: output size in code units.
  1663. // width: output display width in (terminal) column positions.
  1664. template <align::type align = align::left, typename OutputIt, typename Char,
  1665. typename F>
  1666. FMT_CONSTEXPR auto write_padded(OutputIt out, const format_specs<Char>& specs,
  1667. size_t size, size_t width, F&& f) -> OutputIt {
  1668. static_assert(align == align::left || align == align::right, "");
  1669. unsigned spec_width = to_unsigned(specs.width);
  1670. size_t padding = spec_width > width ? spec_width - width : 0;
  1671. // Shifts are encoded as string literals because static constexpr is not
  1672. // supported in constexpr functions.
  1673. auto* shifts = align == align::left ? "\x1f\x1f\x00\x01" : "\x00\x1f\x00\x01";
  1674. size_t left_padding = padding >> shifts[specs.align];
  1675. size_t right_padding = padding - left_padding;
  1676. auto it = reserve(out, size + padding * specs.fill.size());
  1677. if (left_padding != 0) it = fill(it, left_padding, specs.fill);
  1678. it = f(it);
  1679. if (right_padding != 0) it = fill(it, right_padding, specs.fill);
  1680. return base_iterator(out, it);
  1681. }
  1682. template <align::type align = align::left, typename OutputIt, typename Char,
  1683. typename F>
  1684. constexpr auto write_padded(OutputIt out, const format_specs<Char>& specs,
  1685. size_t size, F&& f) -> OutputIt {
  1686. return write_padded<align>(out, specs, size, size, f);
  1687. }
  1688. template <align::type align = align::left, typename Char, typename OutputIt>
  1689. FMT_CONSTEXPR auto write_bytes(OutputIt out, string_view bytes,
  1690. const format_specs<Char>& specs) -> OutputIt {
  1691. return write_padded<align>(
  1692. out, specs, bytes.size(), [bytes](reserve_iterator<OutputIt> it) {
  1693. const char* data = bytes.data();
  1694. return copy_str<Char>(data, data + bytes.size(), it);
  1695. });
  1696. }
  1697. template <typename Char, typename OutputIt, typename UIntPtr>
  1698. auto write_ptr(OutputIt out, UIntPtr value, const format_specs<Char>* specs)
  1699. -> OutputIt {
  1700. int num_digits = count_digits<4>(value);
  1701. auto size = to_unsigned(num_digits) + size_t(2);
  1702. auto write = [=](reserve_iterator<OutputIt> it) {
  1703. *it++ = static_cast<Char>('0');
  1704. *it++ = static_cast<Char>('x');
  1705. return format_uint<4, Char>(it, value, num_digits);
  1706. };
  1707. return specs ? write_padded<align::right>(out, *specs, size, write)
  1708. : base_iterator(out, write(reserve(out, size)));
  1709. }
  1710. // Returns true iff the code point cp is printable.
  1711. FMT_API auto is_printable(uint32_t cp) -> bool;
  1712. inline auto needs_escape(uint32_t cp) -> bool {
  1713. return cp < 0x20 || cp == 0x7f || cp == '"' || cp == '\\' ||
  1714. !is_printable(cp);
  1715. }
  1716. template <typename Char> struct find_escape_result {
  1717. const Char* begin;
  1718. const Char* end;
  1719. uint32_t cp;
  1720. };
  1721. template <typename Char>
  1722. using make_unsigned_char =
  1723. typename conditional_t<std::is_integral<Char>::value,
  1724. std::make_unsigned<Char>,
  1725. type_identity<uint32_t>>::type;
  1726. template <typename Char>
  1727. auto find_escape(const Char* begin, const Char* end)
  1728. -> find_escape_result<Char> {
  1729. for (; begin != end; ++begin) {
  1730. uint32_t cp = static_cast<make_unsigned_char<Char>>(*begin);
  1731. if (const_check(sizeof(Char) == 1) && cp >= 0x80) continue;
  1732. if (needs_escape(cp)) return {begin, begin + 1, cp};
  1733. }
  1734. return {begin, nullptr, 0};
  1735. }
  1736. inline auto find_escape(const char* begin, const char* end)
  1737. -> find_escape_result<char> {
  1738. if (!is_utf8()) return find_escape<char>(begin, end);
  1739. auto result = find_escape_result<char>{end, nullptr, 0};
  1740. for_each_codepoint(string_view(begin, to_unsigned(end - begin)),
  1741. [&](uint32_t cp, string_view sv) {
  1742. if (needs_escape(cp)) {
  1743. result = {sv.begin(), sv.end(), cp};
  1744. return false;
  1745. }
  1746. return true;
  1747. });
  1748. return result;
  1749. }
  1750. #define FMT_STRING_IMPL(s, base, explicit) \
  1751. [] { \
  1752. /* Use the hidden visibility as a workaround for a GCC bug (#1973). */ \
  1753. /* Use a macro-like name to avoid shadowing warnings. */ \
  1754. struct FMT_GCC_VISIBILITY_HIDDEN FMT_COMPILE_STRING : base { \
  1755. using char_type FMT_MAYBE_UNUSED = fmt::remove_cvref_t<decltype(s[0])>; \
  1756. FMT_MAYBE_UNUSED FMT_CONSTEXPR explicit \
  1757. operator fmt::basic_string_view<char_type>() const { \
  1758. return fmt::detail_exported::compile_string_to_view<char_type>(s); \
  1759. } \
  1760. }; \
  1761. return FMT_COMPILE_STRING(); \
  1762. }()
  1763. /**
  1764. \rst
  1765. Constructs a compile-time format string from a string literal *s*.
  1766. **Example**::
  1767. // A compile-time error because 'd' is an invalid specifier for strings.
  1768. std::string s = fmt::format(FMT_STRING("{:d}"), "foo");
  1769. \endrst
  1770. */
  1771. #define FMT_STRING(s) FMT_STRING_IMPL(s, fmt::detail::compile_string, )
  1772. template <size_t width, typename Char, typename OutputIt>
  1773. auto write_codepoint(OutputIt out, char prefix, uint32_t cp) -> OutputIt {
  1774. *out++ = static_cast<Char>('\\');
  1775. *out++ = static_cast<Char>(prefix);
  1776. Char buf[width];
  1777. fill_n(buf, width, static_cast<Char>('0'));
  1778. format_uint<4>(buf, cp, width);
  1779. return copy_str<Char>(buf, buf + width, out);
  1780. }
  1781. template <typename OutputIt, typename Char>
  1782. auto write_escaped_cp(OutputIt out, const find_escape_result<Char>& escape)
  1783. -> OutputIt {
  1784. auto c = static_cast<Char>(escape.cp);
  1785. switch (escape.cp) {
  1786. case '\n':
  1787. *out++ = static_cast<Char>('\\');
  1788. c = static_cast<Char>('n');
  1789. break;
  1790. case '\r':
  1791. *out++ = static_cast<Char>('\\');
  1792. c = static_cast<Char>('r');
  1793. break;
  1794. case '\t':
  1795. *out++ = static_cast<Char>('\\');
  1796. c = static_cast<Char>('t');
  1797. break;
  1798. case '"':
  1799. FMT_FALLTHROUGH;
  1800. case '\'':
  1801. FMT_FALLTHROUGH;
  1802. case '\\':
  1803. *out++ = static_cast<Char>('\\');
  1804. break;
  1805. default:
  1806. if (escape.cp < 0x100) {
  1807. return write_codepoint<2, Char>(out, 'x', escape.cp);
  1808. }
  1809. if (escape.cp < 0x10000) {
  1810. return write_codepoint<4, Char>(out, 'u', escape.cp);
  1811. }
  1812. if (escape.cp < 0x110000) {
  1813. return write_codepoint<8, Char>(out, 'U', escape.cp);
  1814. }
  1815. for (Char escape_char : basic_string_view<Char>(
  1816. escape.begin, to_unsigned(escape.end - escape.begin))) {
  1817. out = write_codepoint<2, Char>(out, 'x',
  1818. static_cast<uint32_t>(escape_char) & 0xFF);
  1819. }
  1820. return out;
  1821. }
  1822. *out++ = c;
  1823. return out;
  1824. }
  1825. template <typename Char, typename OutputIt>
  1826. auto write_escaped_string(OutputIt out, basic_string_view<Char> str)
  1827. -> OutputIt {
  1828. *out++ = static_cast<Char>('"');
  1829. auto begin = str.begin(), end = str.end();
  1830. do {
  1831. auto escape = find_escape(begin, end);
  1832. out = copy_str<Char>(begin, escape.begin, out);
  1833. begin = escape.end;
  1834. if (!begin) break;
  1835. out = write_escaped_cp<OutputIt, Char>(out, escape);
  1836. } while (begin != end);
  1837. *out++ = static_cast<Char>('"');
  1838. return out;
  1839. }
  1840. template <typename Char, typename OutputIt>
  1841. auto write_escaped_char(OutputIt out, Char v) -> OutputIt {
  1842. *out++ = static_cast<Char>('\'');
  1843. if ((needs_escape(static_cast<uint32_t>(v)) && v != static_cast<Char>('"')) ||
  1844. v == static_cast<Char>('\'')) {
  1845. out = write_escaped_cp(
  1846. out, find_escape_result<Char>{&v, &v + 1, static_cast<uint32_t>(v)});
  1847. } else {
  1848. *out++ = v;
  1849. }
  1850. *out++ = static_cast<Char>('\'');
  1851. return out;
  1852. }
  1853. template <typename Char, typename OutputIt>
  1854. FMT_CONSTEXPR auto write_char(OutputIt out, Char value,
  1855. const format_specs<Char>& specs) -> OutputIt {
  1856. bool is_debug = specs.type == presentation_type::debug;
  1857. return write_padded(out, specs, 1, [=](reserve_iterator<OutputIt> it) {
  1858. if (is_debug) return write_escaped_char(it, value);
  1859. *it++ = value;
  1860. return it;
  1861. });
  1862. }
  1863. template <typename Char, typename OutputIt>
  1864. FMT_CONSTEXPR auto write(OutputIt out, Char value,
  1865. const format_specs<Char>& specs, locale_ref loc = {})
  1866. -> OutputIt {
  1867. // char is formatted as unsigned char for consistency across platforms.
  1868. using unsigned_type =
  1869. conditional_t<std::is_same<Char, char>::value, unsigned char, unsigned>;
  1870. return check_char_specs(specs)
  1871. ? write_char(out, value, specs)
  1872. : write(out, static_cast<unsigned_type>(value), specs, loc);
  1873. }
  1874. // Data for write_int that doesn't depend on output iterator type. It is used to
  1875. // avoid template code bloat.
  1876. template <typename Char> struct write_int_data {
  1877. size_t size;
  1878. size_t padding;
  1879. FMT_CONSTEXPR write_int_data(int num_digits, unsigned prefix,
  1880. const format_specs<Char>& specs)
  1881. : size((prefix >> 24) + to_unsigned(num_digits)), padding(0) {
  1882. if (specs.align == align::numeric) {
  1883. auto width = to_unsigned(specs.width);
  1884. if (width > size) {
  1885. padding = width - size;
  1886. size = width;
  1887. }
  1888. } else if (specs.precision > num_digits) {
  1889. size = (prefix >> 24) + to_unsigned(specs.precision);
  1890. padding = to_unsigned(specs.precision - num_digits);
  1891. }
  1892. }
  1893. };
  1894. // Writes an integer in the format
  1895. // <left-padding><prefix><numeric-padding><digits><right-padding>
  1896. // where <digits> are written by write_digits(it).
  1897. // prefix contains chars in three lower bytes and the size in the fourth byte.
  1898. template <typename OutputIt, typename Char, typename W>
  1899. FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, int num_digits,
  1900. unsigned prefix,
  1901. const format_specs<Char>& specs,
  1902. W write_digits) -> OutputIt {
  1903. // Slightly faster check for specs.width == 0 && specs.precision == -1.
  1904. if ((specs.width | (specs.precision + 1)) == 0) {
  1905. auto it = reserve(out, to_unsigned(num_digits) + (prefix >> 24));
  1906. if (prefix != 0) {
  1907. for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8)
  1908. *it++ = static_cast<Char>(p & 0xff);
  1909. }
  1910. return base_iterator(out, write_digits(it));
  1911. }
  1912. auto data = write_int_data<Char>(num_digits, prefix, specs);
  1913. return write_padded<align::right>(
  1914. out, specs, data.size, [=](reserve_iterator<OutputIt> it) {
  1915. for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8)
  1916. *it++ = static_cast<Char>(p & 0xff);
  1917. it = detail::fill_n(it, data.padding, static_cast<Char>('0'));
  1918. return write_digits(it);
  1919. });
  1920. }
  1921. template <typename Char> class digit_grouping {
  1922. private:
  1923. std::string grouping_;
  1924. std::basic_string<Char> thousands_sep_;
  1925. struct next_state {
  1926. std::string::const_iterator group;
  1927. int pos;
  1928. };
  1929. next_state initial_state() const { return {grouping_.begin(), 0}; }
  1930. // Returns the next digit group separator position.
  1931. int next(next_state& state) const {
  1932. if (thousands_sep_.empty()) return max_value<int>();
  1933. if (state.group == grouping_.end()) return state.pos += grouping_.back();
  1934. if (*state.group <= 0 || *state.group == max_value<char>())
  1935. return max_value<int>();
  1936. state.pos += *state.group++;
  1937. return state.pos;
  1938. }
  1939. public:
  1940. explicit digit_grouping(locale_ref loc, bool localized = true) {
  1941. if (!localized) return;
  1942. auto sep = thousands_sep<Char>(loc);
  1943. grouping_ = sep.grouping;
  1944. if (sep.thousands_sep) thousands_sep_.assign(1, sep.thousands_sep);
  1945. }
  1946. digit_grouping(std::string grouping, std::basic_string<Char> sep)
  1947. : grouping_(std::move(grouping)), thousands_sep_(std::move(sep)) {}
  1948. bool has_separator() const { return !thousands_sep_.empty(); }
  1949. int count_separators(int num_digits) const {
  1950. int count = 0;
  1951. auto state = initial_state();
  1952. while (num_digits > next(state)) ++count;
  1953. return count;
  1954. }
  1955. // Applies grouping to digits and write the output to out.
  1956. template <typename Out, typename C>
  1957. Out apply(Out out, basic_string_view<C> digits) const {
  1958. auto num_digits = static_cast<int>(digits.size());
  1959. auto separators = basic_memory_buffer<int>();
  1960. separators.push_back(0);
  1961. auto state = initial_state();
  1962. while (int i = next(state)) {
  1963. if (i >= num_digits) break;
  1964. separators.push_back(i);
  1965. }
  1966. for (int i = 0, sep_index = static_cast<int>(separators.size() - 1);
  1967. i < num_digits; ++i) {
  1968. if (num_digits - i == separators[sep_index]) {
  1969. out =
  1970. copy_str<Char>(thousands_sep_.data(),
  1971. thousands_sep_.data() + thousands_sep_.size(), out);
  1972. --sep_index;
  1973. }
  1974. *out++ = static_cast<Char>(digits[to_unsigned(i)]);
  1975. }
  1976. return out;
  1977. }
  1978. };
  1979. // Writes a decimal integer with digit grouping.
  1980. template <typename OutputIt, typename UInt, typename Char>
  1981. auto write_int(OutputIt out, UInt value, unsigned prefix,
  1982. const format_specs<Char>& specs,
  1983. const digit_grouping<Char>& grouping) -> OutputIt {
  1984. static_assert(std::is_same<uint64_or_128_t<UInt>, UInt>::value, "");
  1985. int num_digits = count_digits(value);
  1986. char digits[40];
  1987. format_decimal(digits, value, num_digits);
  1988. unsigned size = to_unsigned((prefix != 0 ? 1 : 0) + num_digits +
  1989. grouping.count_separators(num_digits));
  1990. return write_padded<align::right>(
  1991. out, specs, size, size, [&](reserve_iterator<OutputIt> it) {
  1992. if (prefix != 0) {
  1993. char sign = static_cast<char>(prefix);
  1994. *it++ = static_cast<Char>(sign);
  1995. }
  1996. return grouping.apply(it, string_view(digits, to_unsigned(num_digits)));
  1997. });
  1998. }
  1999. // Writes a localized value.
  2000. FMT_API auto write_loc(appender out, loc_value value,
  2001. const format_specs<>& specs, locale_ref loc) -> bool;
  2002. template <typename OutputIt, typename Char>
  2003. inline auto write_loc(OutputIt, loc_value, const format_specs<Char>&,
  2004. locale_ref) -> bool {
  2005. return false;
  2006. }
  2007. FMT_CONSTEXPR inline void prefix_append(unsigned& prefix, unsigned value) {
  2008. prefix |= prefix != 0 ? value << 8 : value;
  2009. prefix += (1u + (value > 0xff ? 1 : 0)) << 24;
  2010. }
  2011. template <typename UInt> struct write_int_arg {
  2012. UInt abs_value;
  2013. unsigned prefix;
  2014. };
  2015. template <typename T>
  2016. FMT_CONSTEXPR auto make_write_int_arg(T value, sign_t sign)
  2017. -> write_int_arg<uint32_or_64_or_128_t<T>> {
  2018. auto prefix = 0u;
  2019. auto abs_value = static_cast<uint32_or_64_or_128_t<T>>(value);
  2020. if (is_negative(value)) {
  2021. prefix = 0x01000000 | '-';
  2022. abs_value = 0 - abs_value;
  2023. } else {
  2024. constexpr const unsigned prefixes[4] = {0, 0, 0x1000000u | '+',
  2025. 0x1000000u | ' '};
  2026. prefix = prefixes[sign];
  2027. }
  2028. return {abs_value, prefix};
  2029. }
  2030. template <typename Char = char> struct loc_writer {
  2031. buffer_appender<Char> out;
  2032. const format_specs<Char>& specs;
  2033. std::basic_string<Char> sep;
  2034. std::string grouping;
  2035. std::basic_string<Char> decimal_point;
  2036. template <typename T, FMT_ENABLE_IF(is_integer<T>::value)>
  2037. auto operator()(T value) -> bool {
  2038. auto arg = make_write_int_arg(value, specs.sign);
  2039. write_int(out, static_cast<uint64_or_128_t<T>>(arg.abs_value), arg.prefix,
  2040. specs, digit_grouping<Char>(grouping, sep));
  2041. return true;
  2042. }
  2043. template <typename T, FMT_ENABLE_IF(!is_integer<T>::value)>
  2044. auto operator()(T) -> bool {
  2045. return false;
  2046. }
  2047. };
  2048. template <typename Char, typename OutputIt, typename T>
  2049. FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg<T> arg,
  2050. const format_specs<Char>& specs,
  2051. locale_ref) -> OutputIt {
  2052. static_assert(std::is_same<T, uint32_or_64_or_128_t<T>>::value, "");
  2053. auto abs_value = arg.abs_value;
  2054. auto prefix = arg.prefix;
  2055. switch (specs.type) {
  2056. case presentation_type::none:
  2057. case presentation_type::dec: {
  2058. auto num_digits = count_digits(abs_value);
  2059. return write_int(
  2060. out, num_digits, prefix, specs, [=](reserve_iterator<OutputIt> it) {
  2061. return format_decimal<Char>(it, abs_value, num_digits).end;
  2062. });
  2063. }
  2064. case presentation_type::hex_lower:
  2065. case presentation_type::hex_upper: {
  2066. bool upper = specs.type == presentation_type::hex_upper;
  2067. if (specs.alt)
  2068. prefix_append(prefix, unsigned(upper ? 'X' : 'x') << 8 | '0');
  2069. int num_digits = count_digits<4>(abs_value);
  2070. return write_int(
  2071. out, num_digits, prefix, specs, [=](reserve_iterator<OutputIt> it) {
  2072. return format_uint<4, Char>(it, abs_value, num_digits, upper);
  2073. });
  2074. }
  2075. case presentation_type::bin_lower:
  2076. case presentation_type::bin_upper: {
  2077. bool upper = specs.type == presentation_type::bin_upper;
  2078. if (specs.alt)
  2079. prefix_append(prefix, unsigned(upper ? 'B' : 'b') << 8 | '0');
  2080. int num_digits = count_digits<1>(abs_value);
  2081. return write_int(out, num_digits, prefix, specs,
  2082. [=](reserve_iterator<OutputIt> it) {
  2083. return format_uint<1, Char>(it, abs_value, num_digits);
  2084. });
  2085. }
  2086. case presentation_type::oct: {
  2087. int num_digits = count_digits<3>(abs_value);
  2088. // Octal prefix '0' is counted as a digit, so only add it if precision
  2089. // is not greater than the number of digits.
  2090. if (specs.alt && specs.precision <= num_digits && abs_value != 0)
  2091. prefix_append(prefix, '0');
  2092. return write_int(out, num_digits, prefix, specs,
  2093. [=](reserve_iterator<OutputIt> it) {
  2094. return format_uint<3, Char>(it, abs_value, num_digits);
  2095. });
  2096. }
  2097. case presentation_type::chr:
  2098. return write_char(out, static_cast<Char>(abs_value), specs);
  2099. default:
  2100. throw_format_error("invalid format specifier");
  2101. }
  2102. return out;
  2103. }
  2104. template <typename Char, typename OutputIt, typename T>
  2105. FMT_CONSTEXPR FMT_NOINLINE auto write_int_noinline(
  2106. OutputIt out, write_int_arg<T> arg, const format_specs<Char>& specs,
  2107. locale_ref loc) -> OutputIt {
  2108. return write_int(out, arg, specs, loc);
  2109. }
  2110. template <typename Char, typename OutputIt, typename T,
  2111. FMT_ENABLE_IF(is_integral<T>::value &&
  2112. !std::is_same<T, bool>::value &&
  2113. std::is_same<OutputIt, buffer_appender<Char>>::value)>
  2114. FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value,
  2115. const format_specs<Char>& specs,
  2116. locale_ref loc) -> OutputIt {
  2117. if (specs.localized && write_loc(out, value, specs, loc)) return out;
  2118. return write_int_noinline(out, make_write_int_arg(value, specs.sign), specs,
  2119. loc);
  2120. }
  2121. // An inlined version of write used in format string compilation.
  2122. template <typename Char, typename OutputIt, typename T,
  2123. FMT_ENABLE_IF(is_integral<T>::value &&
  2124. !std::is_same<T, bool>::value &&
  2125. !std::is_same<OutputIt, buffer_appender<Char>>::value)>
  2126. FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value,
  2127. const format_specs<Char>& specs,
  2128. locale_ref loc) -> OutputIt {
  2129. if (specs.localized && write_loc(out, value, specs, loc)) return out;
  2130. return write_int(out, make_write_int_arg(value, specs.sign), specs, loc);
  2131. }
  2132. // An output iterator that counts the number of objects written to it and
  2133. // discards them.
  2134. class counting_iterator {
  2135. private:
  2136. size_t count_;
  2137. public:
  2138. using iterator_category = std::output_iterator_tag;
  2139. using difference_type = std::ptrdiff_t;
  2140. using pointer = void;
  2141. using reference = void;
  2142. FMT_UNCHECKED_ITERATOR(counting_iterator);
  2143. struct value_type {
  2144. template <typename T> FMT_CONSTEXPR void operator=(const T&) {}
  2145. };
  2146. FMT_CONSTEXPR counting_iterator() : count_(0) {}
  2147. FMT_CONSTEXPR size_t count() const { return count_; }
  2148. FMT_CONSTEXPR counting_iterator& operator++() {
  2149. ++count_;
  2150. return *this;
  2151. }
  2152. FMT_CONSTEXPR counting_iterator operator++(int) {
  2153. auto it = *this;
  2154. ++*this;
  2155. return it;
  2156. }
  2157. FMT_CONSTEXPR friend counting_iterator operator+(counting_iterator it,
  2158. difference_type n) {
  2159. it.count_ += static_cast<size_t>(n);
  2160. return it;
  2161. }
  2162. FMT_CONSTEXPR value_type operator*() const { return {}; }
  2163. };
  2164. template <typename Char, typename OutputIt>
  2165. FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
  2166. const format_specs<Char>& specs) -> OutputIt {
  2167. auto data = s.data();
  2168. auto size = s.size();
  2169. if (specs.precision >= 0 && to_unsigned(specs.precision) < size)
  2170. size = code_point_index(s, to_unsigned(specs.precision));
  2171. bool is_debug = specs.type == presentation_type::debug;
  2172. size_t width = 0;
  2173. if (specs.width != 0) {
  2174. if (is_debug)
  2175. width = write_escaped_string(counting_iterator{}, s).count();
  2176. else
  2177. width = compute_width(basic_string_view<Char>(data, size));
  2178. }
  2179. return write_padded(out, specs, size, width,
  2180. [=](reserve_iterator<OutputIt> it) {
  2181. if (is_debug) return write_escaped_string(it, s);
  2182. return copy_str<Char>(data, data + size, it);
  2183. });
  2184. }
  2185. template <typename Char, typename OutputIt>
  2186. FMT_CONSTEXPR auto write(OutputIt out,
  2187. basic_string_view<type_identity_t<Char>> s,
  2188. const format_specs<Char>& specs, locale_ref)
  2189. -> OutputIt {
  2190. return write(out, s, specs);
  2191. }
  2192. template <typename Char, typename OutputIt>
  2193. FMT_CONSTEXPR auto write(OutputIt out, const Char* s,
  2194. const format_specs<Char>& specs, locale_ref)
  2195. -> OutputIt {
  2196. return specs.type != presentation_type::pointer
  2197. ? write(out, basic_string_view<Char>(s), specs, {})
  2198. : write_ptr<Char>(out, bit_cast<uintptr_t>(s), &specs);
  2199. }
  2200. template <typename Char, typename OutputIt, typename T,
  2201. FMT_ENABLE_IF(is_integral<T>::value &&
  2202. !std::is_same<T, bool>::value &&
  2203. !std::is_same<T, Char>::value)>
  2204. FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt {
  2205. auto abs_value = static_cast<uint32_or_64_or_128_t<T>>(value);
  2206. bool negative = is_negative(value);
  2207. // Don't do -abs_value since it trips unsigned-integer-overflow sanitizer.
  2208. if (negative) abs_value = ~abs_value + 1;
  2209. int num_digits = count_digits(abs_value);
  2210. auto size = (negative ? 1 : 0) + static_cast<size_t>(num_digits);
  2211. auto it = reserve(out, size);
  2212. if (auto ptr = to_pointer<Char>(it, size)) {
  2213. if (negative) *ptr++ = static_cast<Char>('-');
  2214. format_decimal<Char>(ptr, abs_value, num_digits);
  2215. return out;
  2216. }
  2217. if (negative) *it++ = static_cast<Char>('-');
  2218. it = format_decimal<Char>(it, abs_value, num_digits).end;
  2219. return base_iterator(out, it);
  2220. }
  2221. // A floating-point presentation format.
  2222. enum class float_format : unsigned char {
  2223. general, // General: exponent notation or fixed point based on magnitude.
  2224. exp, // Exponent notation with the default precision of 6, e.g. 1.2e-3.
  2225. fixed, // Fixed point with the default precision of 6, e.g. 0.0012.
  2226. hex
  2227. };
  2228. struct float_specs {
  2229. int precision;
  2230. float_format format : 8;
  2231. sign_t sign : 8;
  2232. bool upper : 1;
  2233. bool locale : 1;
  2234. bool binary32 : 1;
  2235. bool showpoint : 1;
  2236. };
  2237. template <typename ErrorHandler = error_handler, typename Char>
  2238. FMT_CONSTEXPR auto parse_float_type_spec(const format_specs<Char>& specs,
  2239. ErrorHandler&& eh = {})
  2240. -> float_specs {
  2241. auto result = float_specs();
  2242. result.showpoint = specs.alt;
  2243. result.locale = specs.localized;
  2244. switch (specs.type) {
  2245. case presentation_type::none:
  2246. result.format = float_format::general;
  2247. break;
  2248. case presentation_type::general_upper:
  2249. result.upper = true;
  2250. FMT_FALLTHROUGH;
  2251. case presentation_type::general_lower:
  2252. result.format = float_format::general;
  2253. break;
  2254. case presentation_type::exp_upper:
  2255. result.upper = true;
  2256. FMT_FALLTHROUGH;
  2257. case presentation_type::exp_lower:
  2258. result.format = float_format::exp;
  2259. result.showpoint |= specs.precision != 0;
  2260. break;
  2261. case presentation_type::fixed_upper:
  2262. result.upper = true;
  2263. FMT_FALLTHROUGH;
  2264. case presentation_type::fixed_lower:
  2265. result.format = float_format::fixed;
  2266. result.showpoint |= specs.precision != 0;
  2267. break;
  2268. case presentation_type::hexfloat_upper:
  2269. result.upper = true;
  2270. FMT_FALLTHROUGH;
  2271. case presentation_type::hexfloat_lower:
  2272. result.format = float_format::hex;
  2273. break;
  2274. default:
  2275. eh.on_error("invalid format specifier");
  2276. break;
  2277. }
  2278. return result;
  2279. }
  2280. template <typename Char, typename OutputIt>
  2281. FMT_CONSTEXPR20 auto write_nonfinite(OutputIt out, bool isnan,
  2282. format_specs<Char> specs,
  2283. const float_specs& fspecs) -> OutputIt {
  2284. auto str =
  2285. isnan ? (fspecs.upper ? "NAN" : "nan") : (fspecs.upper ? "INF" : "inf");
  2286. constexpr size_t str_size = 3;
  2287. auto sign = fspecs.sign;
  2288. auto size = str_size + (sign ? 1 : 0);
  2289. // Replace '0'-padding with space for non-finite values.
  2290. const bool is_zero_fill =
  2291. specs.fill.size() == 1 && *specs.fill.data() == static_cast<Char>('0');
  2292. if (is_zero_fill) specs.fill[0] = static_cast<Char>(' ');
  2293. return write_padded(out, specs, size, [=](reserve_iterator<OutputIt> it) {
  2294. if (sign) *it++ = detail::sign<Char>(sign);
  2295. return copy_str<Char>(str, str + str_size, it);
  2296. });
  2297. }
  2298. // A decimal floating-point number significand * pow(10, exp).
  2299. struct big_decimal_fp {
  2300. const char* significand;
  2301. int significand_size;
  2302. int exponent;
  2303. };
  2304. constexpr auto get_significand_size(const big_decimal_fp& f) -> int {
  2305. return f.significand_size;
  2306. }
  2307. template <typename T>
  2308. inline auto get_significand_size(const dragonbox::decimal_fp<T>& f) -> int {
  2309. return count_digits(f.significand);
  2310. }
  2311. template <typename Char, typename OutputIt>
  2312. constexpr auto write_significand(OutputIt out, const char* significand,
  2313. int significand_size) -> OutputIt {
  2314. return copy_str<Char>(significand, significand + significand_size, out);
  2315. }
  2316. template <typename Char, typename OutputIt, typename UInt>
  2317. inline auto write_significand(OutputIt out, UInt significand,
  2318. int significand_size) -> OutputIt {
  2319. return format_decimal<Char>(out, significand, significand_size).end;
  2320. }
  2321. template <typename Char, typename OutputIt, typename T, typename Grouping>
  2322. FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand,
  2323. int significand_size, int exponent,
  2324. const Grouping& grouping) -> OutputIt {
  2325. if (!grouping.has_separator()) {
  2326. out = write_significand<Char>(out, significand, significand_size);
  2327. return detail::fill_n(out, exponent, static_cast<Char>('0'));
  2328. }
  2329. auto buffer = memory_buffer();
  2330. write_significand<char>(appender(buffer), significand, significand_size);
  2331. detail::fill_n(appender(buffer), exponent, '0');
  2332. return grouping.apply(out, string_view(buffer.data(), buffer.size()));
  2333. }
  2334. template <typename Char, typename UInt,
  2335. FMT_ENABLE_IF(std::is_integral<UInt>::value)>
  2336. inline auto write_significand(Char* out, UInt significand, int significand_size,
  2337. int integral_size, Char decimal_point) -> Char* {
  2338. if (!decimal_point)
  2339. return format_decimal(out, significand, significand_size).end;
  2340. out += significand_size + 1;
  2341. Char* end = out;
  2342. int floating_size = significand_size - integral_size;
  2343. for (int i = floating_size / 2; i > 0; --i) {
  2344. out -= 2;
  2345. copy2(out, digits2(static_cast<std::size_t>(significand % 100)));
  2346. significand /= 100;
  2347. }
  2348. if (floating_size % 2 != 0) {
  2349. *--out = static_cast<Char>('0' + significand % 10);
  2350. significand /= 10;
  2351. }
  2352. *--out = decimal_point;
  2353. format_decimal(out - integral_size, significand, integral_size);
  2354. return end;
  2355. }
  2356. template <typename OutputIt, typename UInt, typename Char,
  2357. FMT_ENABLE_IF(!std::is_pointer<remove_cvref_t<OutputIt>>::value)>
  2358. inline auto write_significand(OutputIt out, UInt significand,
  2359. int significand_size, int integral_size,
  2360. Char decimal_point) -> OutputIt {
  2361. // Buffer is large enough to hold digits (digits10 + 1) and a decimal point.
  2362. Char buffer[digits10<UInt>() + 2];
  2363. auto end = write_significand(buffer, significand, significand_size,
  2364. integral_size, decimal_point);
  2365. return detail::copy_str_noinline<Char>(buffer, end, out);
  2366. }
  2367. template <typename OutputIt, typename Char>
  2368. FMT_CONSTEXPR auto write_significand(OutputIt out, const char* significand,
  2369. int significand_size, int integral_size,
  2370. Char decimal_point) -> OutputIt {
  2371. out = detail::copy_str_noinline<Char>(significand,
  2372. significand + integral_size, out);
  2373. if (!decimal_point) return out;
  2374. *out++ = decimal_point;
  2375. return detail::copy_str_noinline<Char>(significand + integral_size,
  2376. significand + significand_size, out);
  2377. }
  2378. template <typename OutputIt, typename Char, typename T, typename Grouping>
  2379. FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand,
  2380. int significand_size, int integral_size,
  2381. Char decimal_point,
  2382. const Grouping& grouping) -> OutputIt {
  2383. if (!grouping.has_separator()) {
  2384. return write_significand(out, significand, significand_size, integral_size,
  2385. decimal_point);
  2386. }
  2387. auto buffer = basic_memory_buffer<Char>();
  2388. write_significand(buffer_appender<Char>(buffer), significand,
  2389. significand_size, integral_size, decimal_point);
  2390. grouping.apply(
  2391. out, basic_string_view<Char>(buffer.data(), to_unsigned(integral_size)));
  2392. return detail::copy_str_noinline<Char>(buffer.data() + integral_size,
  2393. buffer.end(), out);
  2394. }
  2395. template <typename OutputIt, typename DecimalFP, typename Char,
  2396. typename Grouping = digit_grouping<Char>>
  2397. FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f,
  2398. const format_specs<Char>& specs,
  2399. float_specs fspecs, locale_ref loc)
  2400. -> OutputIt {
  2401. auto significand = f.significand;
  2402. int significand_size = get_significand_size(f);
  2403. const Char zero = static_cast<Char>('0');
  2404. auto sign = fspecs.sign;
  2405. size_t size = to_unsigned(significand_size) + (sign ? 1 : 0);
  2406. using iterator = reserve_iterator<OutputIt>;
  2407. Char decimal_point =
  2408. fspecs.locale ? detail::decimal_point<Char>(loc) : static_cast<Char>('.');
  2409. int output_exp = f.exponent + significand_size - 1;
  2410. auto use_exp_format = [=]() {
  2411. if (fspecs.format == float_format::exp) return true;
  2412. if (fspecs.format != float_format::general) return false;
  2413. // Use the fixed notation if the exponent is in [exp_lower, exp_upper),
  2414. // e.g. 0.0001 instead of 1e-04. Otherwise use the exponent notation.
  2415. const int exp_lower = -4, exp_upper = 16;
  2416. return output_exp < exp_lower ||
  2417. output_exp >= (fspecs.precision > 0 ? fspecs.precision : exp_upper);
  2418. };
  2419. if (use_exp_format()) {
  2420. int num_zeros = 0;
  2421. if (fspecs.showpoint) {
  2422. num_zeros = fspecs.precision - significand_size;
  2423. if (num_zeros < 0) num_zeros = 0;
  2424. size += to_unsigned(num_zeros);
  2425. } else if (significand_size == 1) {
  2426. decimal_point = Char();
  2427. }
  2428. auto abs_output_exp = output_exp >= 0 ? output_exp : -output_exp;
  2429. int exp_digits = 2;
  2430. if (abs_output_exp >= 100) exp_digits = abs_output_exp >= 1000 ? 4 : 3;
  2431. size += to_unsigned((decimal_point ? 1 : 0) + 2 + exp_digits);
  2432. char exp_char = fspecs.upper ? 'E' : 'e';
  2433. auto write = [=](iterator it) {
  2434. if (sign) *it++ = detail::sign<Char>(sign);
  2435. // Insert a decimal point after the first digit and add an exponent.
  2436. it = write_significand(it, significand, significand_size, 1,
  2437. decimal_point);
  2438. if (num_zeros > 0) it = detail::fill_n(it, num_zeros, zero);
  2439. *it++ = static_cast<Char>(exp_char);
  2440. return write_exponent<Char>(output_exp, it);
  2441. };
  2442. return specs.width > 0 ? write_padded<align::right>(out, specs, size, write)
  2443. : base_iterator(out, write(reserve(out, size)));
  2444. }
  2445. int exp = f.exponent + significand_size;
  2446. if (f.exponent >= 0) {
  2447. // 1234e5 -> 123400000[.0+]
  2448. size += to_unsigned(f.exponent);
  2449. int num_zeros = fspecs.precision - exp;
  2450. abort_fuzzing_if(num_zeros > 5000);
  2451. if (fspecs.showpoint) {
  2452. ++size;
  2453. if (num_zeros <= 0 && fspecs.format != float_format::fixed) num_zeros = 0;
  2454. if (num_zeros > 0) size += to_unsigned(num_zeros);
  2455. }
  2456. auto grouping = Grouping(loc, fspecs.locale);
  2457. size += to_unsigned(grouping.count_separators(exp));
  2458. return write_padded<align::right>(out, specs, size, [&](iterator it) {
  2459. if (sign) *it++ = detail::sign<Char>(sign);
  2460. it = write_significand<Char>(it, significand, significand_size,
  2461. f.exponent, grouping);
  2462. if (!fspecs.showpoint) return it;
  2463. *it++ = decimal_point;
  2464. return num_zeros > 0 ? detail::fill_n(it, num_zeros, zero) : it;
  2465. });
  2466. } else if (exp > 0) {
  2467. // 1234e-2 -> 12.34[0+]
  2468. int num_zeros = fspecs.showpoint ? fspecs.precision - significand_size : 0;
  2469. size += 1 + to_unsigned(num_zeros > 0 ? num_zeros : 0);
  2470. auto grouping = Grouping(loc, fspecs.locale);
  2471. size += to_unsigned(grouping.count_separators(exp));
  2472. return write_padded<align::right>(out, specs, size, [&](iterator it) {
  2473. if (sign) *it++ = detail::sign<Char>(sign);
  2474. it = write_significand(it, significand, significand_size, exp,
  2475. decimal_point, grouping);
  2476. return num_zeros > 0 ? detail::fill_n(it, num_zeros, zero) : it;
  2477. });
  2478. }
  2479. // 1234e-6 -> 0.001234
  2480. int num_zeros = -exp;
  2481. if (significand_size == 0 && fspecs.precision >= 0 &&
  2482. fspecs.precision < num_zeros) {
  2483. num_zeros = fspecs.precision;
  2484. }
  2485. bool pointy = num_zeros != 0 || significand_size != 0 || fspecs.showpoint;
  2486. size += 1 + (pointy ? 1 : 0) + to_unsigned(num_zeros);
  2487. return write_padded<align::right>(out, specs, size, [&](iterator it) {
  2488. if (sign) *it++ = detail::sign<Char>(sign);
  2489. *it++ = zero;
  2490. if (!pointy) return it;
  2491. *it++ = decimal_point;
  2492. it = detail::fill_n(it, num_zeros, zero);
  2493. return write_significand<Char>(it, significand, significand_size);
  2494. });
  2495. }
  2496. template <typename Char> class fallback_digit_grouping {
  2497. public:
  2498. constexpr fallback_digit_grouping(locale_ref, bool) {}
  2499. constexpr bool has_separator() const { return false; }
  2500. constexpr int count_separators(int) const { return 0; }
  2501. template <typename Out, typename C>
  2502. constexpr Out apply(Out out, basic_string_view<C>) const {
  2503. return out;
  2504. }
  2505. };
  2506. template <typename OutputIt, typename DecimalFP, typename Char>
  2507. FMT_CONSTEXPR20 auto write_float(OutputIt out, const DecimalFP& f,
  2508. const format_specs<Char>& specs,
  2509. float_specs fspecs, locale_ref loc)
  2510. -> OutputIt {
  2511. if (is_constant_evaluated()) {
  2512. return do_write_float<OutputIt, DecimalFP, Char,
  2513. fallback_digit_grouping<Char>>(out, f, specs, fspecs,
  2514. loc);
  2515. } else {
  2516. return do_write_float(out, f, specs, fspecs, loc);
  2517. }
  2518. }
  2519. template <typename T> constexpr bool isnan(T value) {
  2520. return !(value >= value); // std::isnan doesn't support __float128.
  2521. }
  2522. template <typename T, typename Enable = void>
  2523. struct has_isfinite : std::false_type {};
  2524. template <typename T>
  2525. struct has_isfinite<T, enable_if_t<sizeof(std::isfinite(T())) != 0>>
  2526. : std::true_type {};
  2527. template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value&&
  2528. has_isfinite<T>::value)>
  2529. FMT_CONSTEXPR20 bool isfinite(T value) {
  2530. constexpr T inf = T(std::numeric_limits<double>::infinity());
  2531. if (is_constant_evaluated())
  2532. return !detail::isnan(value) && value < inf && value > -inf;
  2533. return std::isfinite(value);
  2534. }
  2535. template <typename T, FMT_ENABLE_IF(!has_isfinite<T>::value)>
  2536. FMT_CONSTEXPR bool isfinite(T value) {
  2537. T inf = T(std::numeric_limits<double>::infinity());
  2538. // std::isfinite doesn't support __float128.
  2539. return !detail::isnan(value) && value < inf && value > -inf;
  2540. }
  2541. template <typename T, FMT_ENABLE_IF(is_floating_point<T>::value)>
  2542. FMT_INLINE FMT_CONSTEXPR bool signbit(T value) {
  2543. if (is_constant_evaluated()) {
  2544. #ifdef __cpp_if_constexpr
  2545. if constexpr (std::numeric_limits<double>::is_iec559) {
  2546. auto bits = detail::bit_cast<uint64_t>(static_cast<double>(value));
  2547. return (bits >> (num_bits<uint64_t>() - 1)) != 0;
  2548. }
  2549. #endif
  2550. }
  2551. return std::signbit(static_cast<double>(value));
  2552. }
  2553. enum class round_direction { unknown, up, down };
  2554. // Given the divisor (normally a power of 10), the remainder = v % divisor for
  2555. // some number v and the error, returns whether v should be rounded up, down, or
  2556. // whether the rounding direction can't be determined due to error.
  2557. // error should be less than divisor / 2.
  2558. FMT_CONSTEXPR inline round_direction get_round_direction(uint64_t divisor,
  2559. uint64_t remainder,
  2560. uint64_t error) {
  2561. FMT_ASSERT(remainder < divisor, ""); // divisor - remainder won't overflow.
  2562. FMT_ASSERT(error < divisor, ""); // divisor - error won't overflow.
  2563. FMT_ASSERT(error < divisor - error, ""); // error * 2 won't overflow.
  2564. // Round down if (remainder + error) * 2 <= divisor.
  2565. if (remainder <= divisor - remainder && error * 2 <= divisor - remainder * 2)
  2566. return round_direction::down;
  2567. // Round up if (remainder - error) * 2 >= divisor.
  2568. if (remainder >= error &&
  2569. remainder - error >= divisor - (remainder - error)) {
  2570. return round_direction::up;
  2571. }
  2572. return round_direction::unknown;
  2573. }
  2574. namespace digits {
  2575. enum result {
  2576. more, // Generate more digits.
  2577. done, // Done generating digits.
  2578. error // Digit generation cancelled due to an error.
  2579. };
  2580. }
  2581. struct gen_digits_handler {
  2582. char* buf;
  2583. int size;
  2584. int precision;
  2585. int exp10;
  2586. bool fixed;
  2587. FMT_CONSTEXPR digits::result on_digit(char digit, uint64_t divisor,
  2588. uint64_t remainder, uint64_t error,
  2589. bool integral) {
  2590. FMT_ASSERT(remainder < divisor, "");
  2591. buf[size++] = digit;
  2592. if (!integral && error >= remainder) return digits::error;
  2593. if (size < precision) return digits::more;
  2594. if (!integral) {
  2595. // Check if error * 2 < divisor with overflow prevention.
  2596. // The check is not needed for the integral part because error = 1
  2597. // and divisor > (1 << 32) there.
  2598. if (error >= divisor || error >= divisor - error) return digits::error;
  2599. } else {
  2600. FMT_ASSERT(error == 1 && divisor > 2, "");
  2601. }
  2602. auto dir = get_round_direction(divisor, remainder, error);
  2603. if (dir != round_direction::up)
  2604. return dir == round_direction::down ? digits::done : digits::error;
  2605. ++buf[size - 1];
  2606. for (int i = size - 1; i > 0 && buf[i] > '9'; --i) {
  2607. buf[i] = '0';
  2608. ++buf[i - 1];
  2609. }
  2610. if (buf[0] > '9') {
  2611. buf[0] = '1';
  2612. if (fixed)
  2613. buf[size++] = '0';
  2614. else
  2615. ++exp10;
  2616. }
  2617. return digits::done;
  2618. }
  2619. };
  2620. inline FMT_CONSTEXPR20 void adjust_precision(int& precision, int exp10) {
  2621. // Adjust fixed precision by exponent because it is relative to decimal
  2622. // point.
  2623. if (exp10 > 0 && precision > max_value<int>() - exp10)
  2624. FMT_THROW(format_error("number is too big"));
  2625. precision += exp10;
  2626. }
  2627. // Generates output using the Grisu digit-gen algorithm.
  2628. // error: the size of the region (lower, upper) outside of which numbers
  2629. // definitely do not round to value (Delta in Grisu3).
  2630. FMT_INLINE FMT_CONSTEXPR20 auto grisu_gen_digits(fp value, uint64_t error,
  2631. int& exp,
  2632. gen_digits_handler& handler)
  2633. -> digits::result {
  2634. const fp one(1ULL << -value.e, value.e);
  2635. // The integral part of scaled value (p1 in Grisu) = value / one. It cannot be
  2636. // zero because it contains a product of two 64-bit numbers with MSB set (due
  2637. // to normalization) - 1, shifted right by at most 60 bits.
  2638. auto integral = static_cast<uint32_t>(value.f >> -one.e);
  2639. FMT_ASSERT(integral != 0, "");
  2640. FMT_ASSERT(integral == value.f >> -one.e, "");
  2641. // The fractional part of scaled value (p2 in Grisu) c = value % one.
  2642. uint64_t fractional = value.f & (one.f - 1);
  2643. exp = count_digits(integral); // kappa in Grisu.
  2644. // Non-fixed formats require at least one digit and no precision adjustment.
  2645. if (handler.fixed) {
  2646. adjust_precision(handler.precision, exp + handler.exp10);
  2647. // Check if precision is satisfied just by leading zeros, e.g.
  2648. // format("{:.2f}", 0.001) gives "0.00" without generating any digits.
  2649. if (handler.precision <= 0) {
  2650. if (handler.precision < 0) return digits::done;
  2651. // Divide by 10 to prevent overflow.
  2652. uint64_t divisor = data::power_of_10_64[exp - 1] << -one.e;
  2653. auto dir = get_round_direction(divisor, value.f / 10, error * 10);
  2654. if (dir == round_direction::unknown) return digits::error;
  2655. handler.buf[handler.size++] = dir == round_direction::up ? '1' : '0';
  2656. return digits::done;
  2657. }
  2658. }
  2659. // Generate digits for the integral part. This can produce up to 10 digits.
  2660. do {
  2661. uint32_t digit = 0;
  2662. auto divmod_integral = [&](uint32_t divisor) {
  2663. digit = integral / divisor;
  2664. integral %= divisor;
  2665. };
  2666. // This optimization by Milo Yip reduces the number of integer divisions by
  2667. // one per iteration.
  2668. switch (exp) {
  2669. case 10:
  2670. divmod_integral(1000000000);
  2671. break;
  2672. case 9:
  2673. divmod_integral(100000000);
  2674. break;
  2675. case 8:
  2676. divmod_integral(10000000);
  2677. break;
  2678. case 7:
  2679. divmod_integral(1000000);
  2680. break;
  2681. case 6:
  2682. divmod_integral(100000);
  2683. break;
  2684. case 5:
  2685. divmod_integral(10000);
  2686. break;
  2687. case 4:
  2688. divmod_integral(1000);
  2689. break;
  2690. case 3:
  2691. divmod_integral(100);
  2692. break;
  2693. case 2:
  2694. divmod_integral(10);
  2695. break;
  2696. case 1:
  2697. digit = integral;
  2698. integral = 0;
  2699. break;
  2700. default:
  2701. FMT_ASSERT(false, "invalid number of digits");
  2702. }
  2703. --exp;
  2704. auto remainder = (static_cast<uint64_t>(integral) << -one.e) + fractional;
  2705. auto result = handler.on_digit(static_cast<char>('0' + digit),
  2706. data::power_of_10_64[exp] << -one.e,
  2707. remainder, error, true);
  2708. if (result != digits::more) return result;
  2709. } while (exp > 0);
  2710. // Generate digits for the fractional part.
  2711. for (;;) {
  2712. fractional *= 10;
  2713. error *= 10;
  2714. char digit = static_cast<char>('0' + (fractional >> -one.e));
  2715. fractional &= one.f - 1;
  2716. --exp;
  2717. auto result = handler.on_digit(digit, one.f, fractional, error, false);
  2718. if (result != digits::more) return result;
  2719. }
  2720. }
  2721. class bigint {
  2722. private:
  2723. // A bigint is stored as an array of bigits (big digits), with bigit at index
  2724. // 0 being the least significant one.
  2725. using bigit = uint32_t;
  2726. using double_bigit = uint64_t;
  2727. enum { bigits_capacity = 32 };
  2728. basic_memory_buffer<bigit, bigits_capacity> bigits_;
  2729. int exp_;
  2730. FMT_CONSTEXPR20 bigit operator[](int index) const {
  2731. return bigits_[to_unsigned(index)];
  2732. }
  2733. FMT_CONSTEXPR20 bigit& operator[](int index) {
  2734. return bigits_[to_unsigned(index)];
  2735. }
  2736. static constexpr const int bigit_bits = num_bits<bigit>();
  2737. friend struct formatter<bigint>;
  2738. FMT_CONSTEXPR20 void subtract_bigits(int index, bigit other, bigit& borrow) {
  2739. auto result = static_cast<double_bigit>((*this)[index]) - other - borrow;
  2740. (*this)[index] = static_cast<bigit>(result);
  2741. borrow = static_cast<bigit>(result >> (bigit_bits * 2 - 1));
  2742. }
  2743. FMT_CONSTEXPR20 void remove_leading_zeros() {
  2744. int num_bigits = static_cast<int>(bigits_.size()) - 1;
  2745. while (num_bigits > 0 && (*this)[num_bigits] == 0) --num_bigits;
  2746. bigits_.resize(to_unsigned(num_bigits + 1));
  2747. }
  2748. // Computes *this -= other assuming aligned bigints and *this >= other.
  2749. FMT_CONSTEXPR20 void subtract_aligned(const bigint& other) {
  2750. FMT_ASSERT(other.exp_ >= exp_, "unaligned bigints");
  2751. FMT_ASSERT(compare(*this, other) >= 0, "");
  2752. bigit borrow = 0;
  2753. int i = other.exp_ - exp_;
  2754. for (size_t j = 0, n = other.bigits_.size(); j != n; ++i, ++j)
  2755. subtract_bigits(i, other.bigits_[j], borrow);
  2756. while (borrow > 0) subtract_bigits(i, 0, borrow);
  2757. remove_leading_zeros();
  2758. }
  2759. FMT_CONSTEXPR20 void multiply(uint32_t value) {
  2760. const double_bigit wide_value = value;
  2761. bigit carry = 0;
  2762. for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
  2763. double_bigit result = bigits_[i] * wide_value + carry;
  2764. bigits_[i] = static_cast<bigit>(result);
  2765. carry = static_cast<bigit>(result >> bigit_bits);
  2766. }
  2767. if (carry != 0) bigits_.push_back(carry);
  2768. }
  2769. template <typename UInt, FMT_ENABLE_IF(std::is_same<UInt, uint64_t>::value ||
  2770. std::is_same<UInt, uint128_t>::value)>
  2771. FMT_CONSTEXPR20 void multiply(UInt value) {
  2772. using half_uint =
  2773. conditional_t<std::is_same<UInt, uint128_t>::value, uint64_t, uint32_t>;
  2774. const int shift = num_bits<half_uint>() - bigit_bits;
  2775. const UInt lower = static_cast<half_uint>(value);
  2776. const UInt upper = value >> num_bits<half_uint>();
  2777. UInt carry = 0;
  2778. for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
  2779. UInt result = lower * bigits_[i] + static_cast<bigit>(carry);
  2780. carry = (upper * bigits_[i] << shift) + (result >> bigit_bits) +
  2781. (carry >> bigit_bits);
  2782. bigits_[i] = static_cast<bigit>(result);
  2783. }
  2784. while (carry != 0) {
  2785. bigits_.push_back(static_cast<bigit>(carry));
  2786. carry >>= bigit_bits;
  2787. }
  2788. }
  2789. template <typename UInt, FMT_ENABLE_IF(std::is_same<UInt, uint64_t>::value ||
  2790. std::is_same<UInt, uint128_t>::value)>
  2791. FMT_CONSTEXPR20 void assign(UInt n) {
  2792. size_t num_bigits = 0;
  2793. do {
  2794. bigits_[num_bigits++] = static_cast<bigit>(n);
  2795. n >>= bigit_bits;
  2796. } while (n != 0);
  2797. bigits_.resize(num_bigits);
  2798. exp_ = 0;
  2799. }
  2800. public:
  2801. FMT_CONSTEXPR20 bigint() : exp_(0) {}
  2802. explicit bigint(uint64_t n) { assign(n); }
  2803. bigint(const bigint&) = delete;
  2804. void operator=(const bigint&) = delete;
  2805. FMT_CONSTEXPR20 void assign(const bigint& other) {
  2806. auto size = other.bigits_.size();
  2807. bigits_.resize(size);
  2808. auto data = other.bigits_.data();
  2809. std::copy(data, data + size, make_checked(bigits_.data(), size));
  2810. exp_ = other.exp_;
  2811. }
  2812. template <typename Int> FMT_CONSTEXPR20 void operator=(Int n) {
  2813. FMT_ASSERT(n > 0, "");
  2814. assign(uint64_or_128_t<Int>(n));
  2815. }
  2816. FMT_CONSTEXPR20 int num_bigits() const {
  2817. return static_cast<int>(bigits_.size()) + exp_;
  2818. }
  2819. FMT_NOINLINE FMT_CONSTEXPR20 bigint& operator<<=(int shift) {
  2820. FMT_ASSERT(shift >= 0, "");
  2821. exp_ += shift / bigit_bits;
  2822. shift %= bigit_bits;
  2823. if (shift == 0) return *this;
  2824. bigit carry = 0;
  2825. for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
  2826. bigit c = bigits_[i] >> (bigit_bits - shift);
  2827. bigits_[i] = (bigits_[i] << shift) + carry;
  2828. carry = c;
  2829. }
  2830. if (carry != 0) bigits_.push_back(carry);
  2831. return *this;
  2832. }
  2833. template <typename Int> FMT_CONSTEXPR20 bigint& operator*=(Int value) {
  2834. FMT_ASSERT(value > 0, "");
  2835. multiply(uint32_or_64_or_128_t<Int>(value));
  2836. return *this;
  2837. }
  2838. friend FMT_CONSTEXPR20 int compare(const bigint& lhs, const bigint& rhs) {
  2839. int num_lhs_bigits = lhs.num_bigits(), num_rhs_bigits = rhs.num_bigits();
  2840. if (num_lhs_bigits != num_rhs_bigits)
  2841. return num_lhs_bigits > num_rhs_bigits ? 1 : -1;
  2842. int i = static_cast<int>(lhs.bigits_.size()) - 1;
  2843. int j = static_cast<int>(rhs.bigits_.size()) - 1;
  2844. int end = i - j;
  2845. if (end < 0) end = 0;
  2846. for (; i >= end; --i, --j) {
  2847. bigit lhs_bigit = lhs[i], rhs_bigit = rhs[j];
  2848. if (lhs_bigit != rhs_bigit) return lhs_bigit > rhs_bigit ? 1 : -1;
  2849. }
  2850. if (i != j) return i > j ? 1 : -1;
  2851. return 0;
  2852. }
  2853. // Returns compare(lhs1 + lhs2, rhs).
  2854. friend FMT_CONSTEXPR20 int add_compare(const bigint& lhs1, const bigint& lhs2,
  2855. const bigint& rhs) {
  2856. auto minimum = [](int a, int b) { return a < b ? a : b; };
  2857. auto maximum = [](int a, int b) { return a > b ? a : b; };
  2858. int max_lhs_bigits = maximum(lhs1.num_bigits(), lhs2.num_bigits());
  2859. int num_rhs_bigits = rhs.num_bigits();
  2860. if (max_lhs_bigits + 1 < num_rhs_bigits) return -1;
  2861. if (max_lhs_bigits > num_rhs_bigits) return 1;
  2862. auto get_bigit = [](const bigint& n, int i) -> bigit {
  2863. return i >= n.exp_ && i < n.num_bigits() ? n[i - n.exp_] : 0;
  2864. };
  2865. double_bigit borrow = 0;
  2866. int min_exp = minimum(minimum(lhs1.exp_, lhs2.exp_), rhs.exp_);
  2867. for (int i = num_rhs_bigits - 1; i >= min_exp; --i) {
  2868. double_bigit sum =
  2869. static_cast<double_bigit>(get_bigit(lhs1, i)) + get_bigit(lhs2, i);
  2870. bigit rhs_bigit = get_bigit(rhs, i);
  2871. if (sum > rhs_bigit + borrow) return 1;
  2872. borrow = rhs_bigit + borrow - sum;
  2873. if (borrow > 1) return -1;
  2874. borrow <<= bigit_bits;
  2875. }
  2876. return borrow != 0 ? -1 : 0;
  2877. }
  2878. // Assigns pow(10, exp) to this bigint.
  2879. FMT_CONSTEXPR20 void assign_pow10(int exp) {
  2880. FMT_ASSERT(exp >= 0, "");
  2881. if (exp == 0) return *this = 1;
  2882. // Find the top bit.
  2883. int bitmask = 1;
  2884. while (exp >= bitmask) bitmask <<= 1;
  2885. bitmask >>= 1;
  2886. // pow(10, exp) = pow(5, exp) * pow(2, exp). First compute pow(5, exp) by
  2887. // repeated squaring and multiplication.
  2888. *this = 5;
  2889. bitmask >>= 1;
  2890. while (bitmask != 0) {
  2891. square();
  2892. if ((exp & bitmask) != 0) *this *= 5;
  2893. bitmask >>= 1;
  2894. }
  2895. *this <<= exp; // Multiply by pow(2, exp) by shifting.
  2896. }
  2897. FMT_CONSTEXPR20 void square() {
  2898. int num_bigits = static_cast<int>(bigits_.size());
  2899. int num_result_bigits = 2 * num_bigits;
  2900. basic_memory_buffer<bigit, bigits_capacity> n(std::move(bigits_));
  2901. bigits_.resize(to_unsigned(num_result_bigits));
  2902. auto sum = uint128_t();
  2903. for (int bigit_index = 0; bigit_index < num_bigits; ++bigit_index) {
  2904. // Compute bigit at position bigit_index of the result by adding
  2905. // cross-product terms n[i] * n[j] such that i + j == bigit_index.
  2906. for (int i = 0, j = bigit_index; j >= 0; ++i, --j) {
  2907. // Most terms are multiplied twice which can be optimized in the future.
  2908. sum += static_cast<double_bigit>(n[i]) * n[j];
  2909. }
  2910. (*this)[bigit_index] = static_cast<bigit>(sum);
  2911. sum >>= num_bits<bigit>(); // Compute the carry.
  2912. }
  2913. // Do the same for the top half.
  2914. for (int bigit_index = num_bigits; bigit_index < num_result_bigits;
  2915. ++bigit_index) {
  2916. for (int j = num_bigits - 1, i = bigit_index - j; i < num_bigits;)
  2917. sum += static_cast<double_bigit>(n[i++]) * n[j--];
  2918. (*this)[bigit_index] = static_cast<bigit>(sum);
  2919. sum >>= num_bits<bigit>();
  2920. }
  2921. remove_leading_zeros();
  2922. exp_ *= 2;
  2923. }
  2924. // If this bigint has a bigger exponent than other, adds trailing zero to make
  2925. // exponents equal. This simplifies some operations such as subtraction.
  2926. FMT_CONSTEXPR20 void align(const bigint& other) {
  2927. int exp_difference = exp_ - other.exp_;
  2928. if (exp_difference <= 0) return;
  2929. int num_bigits = static_cast<int>(bigits_.size());
  2930. bigits_.resize(to_unsigned(num_bigits + exp_difference));
  2931. for (int i = num_bigits - 1, j = i + exp_difference; i >= 0; --i, --j)
  2932. bigits_[j] = bigits_[i];
  2933. std::uninitialized_fill_n(bigits_.data(), exp_difference, 0);
  2934. exp_ -= exp_difference;
  2935. }
  2936. // Divides this bignum by divisor, assigning the remainder to this and
  2937. // returning the quotient.
  2938. FMT_CONSTEXPR20 int divmod_assign(const bigint& divisor) {
  2939. FMT_ASSERT(this != &divisor, "");
  2940. if (compare(*this, divisor) < 0) return 0;
  2941. FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1u] != 0, "");
  2942. align(divisor);
  2943. int quotient = 0;
  2944. do {
  2945. subtract_aligned(divisor);
  2946. ++quotient;
  2947. } while (compare(*this, divisor) >= 0);
  2948. return quotient;
  2949. }
  2950. };
  2951. // format_dragon flags.
  2952. enum dragon {
  2953. predecessor_closer = 1,
  2954. fixup = 2, // Run fixup to correct exp10 which can be off by one.
  2955. fixed = 4,
  2956. };
  2957. // Formats a floating-point number using a variation of the Fixed-Precision
  2958. // Positive Floating-Point Printout ((FPP)^2) algorithm by Steele & White:
  2959. // https://fmt.dev/papers/p372-steele.pdf.
  2960. FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value,
  2961. unsigned flags, int num_digits,
  2962. buffer<char>& buf, int& exp10) {
  2963. bigint numerator; // 2 * R in (FPP)^2.
  2964. bigint denominator; // 2 * S in (FPP)^2.
  2965. // lower and upper are differences between value and corresponding boundaries.
  2966. bigint lower; // (M^- in (FPP)^2).
  2967. bigint upper_store; // upper's value if different from lower.
  2968. bigint* upper = nullptr; // (M^+ in (FPP)^2).
  2969. // Shift numerator and denominator by an extra bit or two (if lower boundary
  2970. // is closer) to make lower and upper integers. This eliminates multiplication
  2971. // by 2 during later computations.
  2972. bool is_predecessor_closer = (flags & dragon::predecessor_closer) != 0;
  2973. int shift = is_predecessor_closer ? 2 : 1;
  2974. if (value.e >= 0) {
  2975. numerator = value.f;
  2976. numerator <<= value.e + shift;
  2977. lower = 1;
  2978. lower <<= value.e;
  2979. if (is_predecessor_closer) {
  2980. upper_store = 1;
  2981. upper_store <<= value.e + 1;
  2982. upper = &upper_store;
  2983. }
  2984. denominator.assign_pow10(exp10);
  2985. denominator <<= shift;
  2986. } else if (exp10 < 0) {
  2987. numerator.assign_pow10(-exp10);
  2988. lower.assign(numerator);
  2989. if (is_predecessor_closer) {
  2990. upper_store.assign(numerator);
  2991. upper_store <<= 1;
  2992. upper = &upper_store;
  2993. }
  2994. numerator *= value.f;
  2995. numerator <<= shift;
  2996. denominator = 1;
  2997. denominator <<= shift - value.e;
  2998. } else {
  2999. numerator = value.f;
  3000. numerator <<= shift;
  3001. denominator.assign_pow10(exp10);
  3002. denominator <<= shift - value.e;
  3003. lower = 1;
  3004. if (is_predecessor_closer) {
  3005. upper_store = 1ULL << 1;
  3006. upper = &upper_store;
  3007. }
  3008. }
  3009. int even = static_cast<int>((value.f & 1) == 0);
  3010. if (!upper) upper = &lower;
  3011. if ((flags & dragon::fixup) != 0) {
  3012. if (add_compare(numerator, *upper, denominator) + even <= 0) {
  3013. --exp10;
  3014. numerator *= 10;
  3015. if (num_digits < 0) {
  3016. lower *= 10;
  3017. if (upper != &lower) *upper *= 10;
  3018. }
  3019. }
  3020. if ((flags & dragon::fixed) != 0) adjust_precision(num_digits, exp10 + 1);
  3021. }
  3022. // Invariant: value == (numerator / denominator) * pow(10, exp10).
  3023. if (num_digits < 0) {
  3024. // Generate the shortest representation.
  3025. num_digits = 0;
  3026. char* data = buf.data();
  3027. for (;;) {
  3028. int digit = numerator.divmod_assign(denominator);
  3029. bool low = compare(numerator, lower) - even < 0; // numerator <[=] lower.
  3030. // numerator + upper >[=] pow10:
  3031. bool high = add_compare(numerator, *upper, denominator) + even > 0;
  3032. data[num_digits++] = static_cast<char>('0' + digit);
  3033. if (low || high) {
  3034. if (!low) {
  3035. ++data[num_digits - 1];
  3036. } else if (high) {
  3037. int result = add_compare(numerator, numerator, denominator);
  3038. // Round half to even.
  3039. if (result > 0 || (result == 0 && (digit % 2) != 0))
  3040. ++data[num_digits - 1];
  3041. }
  3042. buf.try_resize(to_unsigned(num_digits));
  3043. exp10 -= num_digits - 1;
  3044. return;
  3045. }
  3046. numerator *= 10;
  3047. lower *= 10;
  3048. if (upper != &lower) *upper *= 10;
  3049. }
  3050. }
  3051. // Generate the given number of digits.
  3052. exp10 -= num_digits - 1;
  3053. if (num_digits == 0) {
  3054. denominator *= 10;
  3055. auto digit = add_compare(numerator, numerator, denominator) > 0 ? '1' : '0';
  3056. buf.push_back(digit);
  3057. return;
  3058. }
  3059. buf.try_resize(to_unsigned(num_digits));
  3060. for (int i = 0; i < num_digits - 1; ++i) {
  3061. int digit = numerator.divmod_assign(denominator);
  3062. buf[i] = static_cast<char>('0' + digit);
  3063. numerator *= 10;
  3064. }
  3065. int digit = numerator.divmod_assign(denominator);
  3066. auto result = add_compare(numerator, numerator, denominator);
  3067. if (result > 0 || (result == 0 && (digit % 2) != 0)) {
  3068. if (digit == 9) {
  3069. const auto overflow = '0' + 10;
  3070. buf[num_digits - 1] = overflow;
  3071. // Propagate the carry.
  3072. for (int i = num_digits - 1; i > 0 && buf[i] == overflow; --i) {
  3073. buf[i] = '0';
  3074. ++buf[i - 1];
  3075. }
  3076. if (buf[0] == overflow) {
  3077. buf[0] = '1';
  3078. ++exp10;
  3079. }
  3080. return;
  3081. }
  3082. ++digit;
  3083. }
  3084. buf[num_digits - 1] = static_cast<char>('0' + digit);
  3085. }
  3086. // Formats a floating-point number using the hexfloat format.
  3087. template <typename Float, FMT_ENABLE_IF(!is_double_double<Float>::value)>
  3088. FMT_CONSTEXPR20 void format_hexfloat(Float value, int precision,
  3089. float_specs specs, buffer<char>& buf) {
  3090. // float is passed as double to reduce the number of instantiations and to
  3091. // simplify implementation.
  3092. static_assert(!std::is_same<Float, float>::value, "");
  3093. using info = dragonbox::float_info<Float>;
  3094. // Assume Float is in the format [sign][exponent][significand].
  3095. using carrier_uint = typename info::carrier_uint;
  3096. constexpr auto num_float_significand_bits =
  3097. detail::num_significand_bits<Float>();
  3098. basic_fp<carrier_uint> f(value);
  3099. f.e += num_float_significand_bits;
  3100. if (!has_implicit_bit<Float>()) --f.e;
  3101. constexpr auto num_fraction_bits =
  3102. num_float_significand_bits + (has_implicit_bit<Float>() ? 1 : 0);
  3103. constexpr auto num_xdigits = (num_fraction_bits + 3) / 4;
  3104. constexpr auto leading_shift = ((num_xdigits - 1) * 4);
  3105. const auto leading_mask = carrier_uint(0xF) << leading_shift;
  3106. const auto leading_xdigit =
  3107. static_cast<uint32_t>((f.f & leading_mask) >> leading_shift);
  3108. if (leading_xdigit > 1) f.e -= (32 - countl_zero(leading_xdigit) - 1);
  3109. int print_xdigits = num_xdigits - 1;
  3110. if (precision >= 0 && print_xdigits > precision) {
  3111. const int shift = ((print_xdigits - precision - 1) * 4);
  3112. const auto mask = carrier_uint(0xF) << shift;
  3113. const auto v = static_cast<uint32_t>((f.f & mask) >> shift);
  3114. if (v >= 8) {
  3115. const auto inc = carrier_uint(1) << (shift + 4);
  3116. f.f += inc;
  3117. f.f &= ~(inc - 1);
  3118. }
  3119. // Check long double overflow
  3120. if (!has_implicit_bit<Float>()) {
  3121. const auto implicit_bit = carrier_uint(1) << num_float_significand_bits;
  3122. if ((f.f & implicit_bit) == implicit_bit) {
  3123. f.f >>= 4;
  3124. f.e += 4;
  3125. }
  3126. }
  3127. print_xdigits = precision;
  3128. }
  3129. char xdigits[num_bits<carrier_uint>() / 4];
  3130. detail::fill_n(xdigits, sizeof(xdigits), '0');
  3131. format_uint<4>(xdigits, f.f, num_xdigits, specs.upper);
  3132. // Remove zero tail
  3133. while (print_xdigits > 0 && xdigits[print_xdigits] == '0') --print_xdigits;
  3134. buf.push_back('0');
  3135. buf.push_back(specs.upper ? 'X' : 'x');
  3136. buf.push_back(xdigits[0]);
  3137. if (specs.showpoint || print_xdigits > 0 || print_xdigits < precision)
  3138. buf.push_back('.');
  3139. buf.append(xdigits + 1, xdigits + 1 + print_xdigits);
  3140. for (; print_xdigits < precision; ++print_xdigits) buf.push_back('0');
  3141. buf.push_back(specs.upper ? 'P' : 'p');
  3142. uint32_t abs_e;
  3143. if (f.e < 0) {
  3144. buf.push_back('-');
  3145. abs_e = static_cast<uint32_t>(-f.e);
  3146. } else {
  3147. buf.push_back('+');
  3148. abs_e = static_cast<uint32_t>(f.e);
  3149. }
  3150. format_decimal<char>(appender(buf), abs_e, detail::count_digits(abs_e));
  3151. }
  3152. template <typename Float, FMT_ENABLE_IF(is_double_double<Float>::value)>
  3153. FMT_CONSTEXPR20 void format_hexfloat(Float value, int precision,
  3154. float_specs specs, buffer<char>& buf) {
  3155. format_hexfloat(static_cast<double>(value), precision, specs, buf);
  3156. }
  3157. template <typename Float>
  3158. FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs,
  3159. buffer<char>& buf) -> int {
  3160. // float is passed as double to reduce the number of instantiations.
  3161. static_assert(!std::is_same<Float, float>::value, "");
  3162. FMT_ASSERT(value >= 0, "value is negative");
  3163. auto converted_value = convert_float(value);
  3164. const bool fixed = specs.format == float_format::fixed;
  3165. if (value <= 0) { // <= instead of == to silence a warning.
  3166. if (precision <= 0 || !fixed) {
  3167. buf.push_back('0');
  3168. return 0;
  3169. }
  3170. buf.try_resize(to_unsigned(precision));
  3171. fill_n(buf.data(), precision, '0');
  3172. return -precision;
  3173. }
  3174. int exp = 0;
  3175. bool use_dragon = true;
  3176. unsigned dragon_flags = 0;
  3177. if (!is_fast_float<Float>()) {
  3178. const auto inv_log2_10 = 0.3010299956639812; // 1 / log2(10)
  3179. using info = dragonbox::float_info<decltype(converted_value)>;
  3180. const auto f = basic_fp<typename info::carrier_uint>(converted_value);
  3181. // Compute exp, an approximate power of 10, such that
  3182. // 10^(exp - 1) <= value < 10^exp or 10^exp <= value < 10^(exp + 1).
  3183. // This is based on log10(value) == log2(value) / log2(10) and approximation
  3184. // of log2(value) by e + num_fraction_bits idea from double-conversion.
  3185. exp = static_cast<int>(
  3186. std::ceil((f.e + count_digits<1>(f.f) - 1) * inv_log2_10 - 1e-10));
  3187. dragon_flags = dragon::fixup;
  3188. } else if (!is_constant_evaluated() && precision < 0) {
  3189. // Use Dragonbox for the shortest format.
  3190. if (specs.binary32) {
  3191. auto dec = dragonbox::to_decimal(static_cast<float>(value));
  3192. write<char>(buffer_appender<char>(buf), dec.significand);
  3193. return dec.exponent;
  3194. }
  3195. auto dec = dragonbox::to_decimal(static_cast<double>(value));
  3196. write<char>(buffer_appender<char>(buf), dec.significand);
  3197. return dec.exponent;
  3198. } else if (is_constant_evaluated()) {
  3199. // Use Grisu + Dragon4 for the given precision:
  3200. // https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf.
  3201. const int min_exp = -60; // alpha in Grisu.
  3202. int cached_exp10 = 0; // K in Grisu.
  3203. fp normalized = normalize(fp(converted_value));
  3204. const auto cached_pow = get_cached_power(
  3205. min_exp - (normalized.e + fp::num_significand_bits), cached_exp10);
  3206. normalized = normalized * cached_pow;
  3207. gen_digits_handler handler{buf.data(), 0, precision, -cached_exp10, fixed};
  3208. if (grisu_gen_digits(normalized, 1, exp, handler) != digits::error &&
  3209. !is_constant_evaluated()) {
  3210. exp += handler.exp10;
  3211. buf.try_resize(to_unsigned(handler.size));
  3212. use_dragon = false;
  3213. } else {
  3214. exp += handler.size - cached_exp10 - 1;
  3215. precision = handler.precision;
  3216. }
  3217. } else {
  3218. // Extract significand bits and exponent bits.
  3219. using info = dragonbox::float_info<double>;
  3220. auto br = bit_cast<uint64_t>(static_cast<double>(value));
  3221. const uint64_t significand_mask =
  3222. (static_cast<uint64_t>(1) << num_significand_bits<double>()) - 1;
  3223. uint64_t significand = (br & significand_mask);
  3224. int exponent = static_cast<int>((br & exponent_mask<double>()) >>
  3225. num_significand_bits<double>());
  3226. if (exponent != 0) { // Check if normal.
  3227. exponent -= exponent_bias<double>() + num_significand_bits<double>();
  3228. significand |=
  3229. (static_cast<uint64_t>(1) << num_significand_bits<double>());
  3230. significand <<= 1;
  3231. } else {
  3232. // Normalize subnormal inputs.
  3233. FMT_ASSERT(significand != 0, "zeros should not appear hear");
  3234. int shift = countl_zero(significand);
  3235. FMT_ASSERT(shift >= num_bits<uint64_t>() - num_significand_bits<double>(),
  3236. "");
  3237. shift -= (num_bits<uint64_t>() - num_significand_bits<double>() - 2);
  3238. exponent = (std::numeric_limits<double>::min_exponent -
  3239. num_significand_bits<double>()) -
  3240. shift;
  3241. significand <<= shift;
  3242. }
  3243. // Compute the first several nonzero decimal significand digits.
  3244. // We call the number we get the first segment.
  3245. const int k = info::kappa - dragonbox::floor_log10_pow2(exponent);
  3246. exp = -k;
  3247. const int beta = exponent + dragonbox::floor_log2_pow10(k);
  3248. uint64_t first_segment;
  3249. bool has_more_segments;
  3250. int digits_in_the_first_segment;
  3251. {
  3252. const auto r = dragonbox::umul192_upper128(
  3253. significand << beta, dragonbox::get_cached_power(k));
  3254. first_segment = r.high();
  3255. has_more_segments = r.low() != 0;
  3256. // The first segment can have 18 ~ 19 digits.
  3257. if (first_segment >= 1000000000000000000ULL) {
  3258. digits_in_the_first_segment = 19;
  3259. } else {
  3260. // When it is of 18-digits, we align it to 19-digits by adding a bogus
  3261. // zero at the end.
  3262. digits_in_the_first_segment = 18;
  3263. first_segment *= 10;
  3264. }
  3265. }
  3266. // Compute the actual number of decimal digits to print.
  3267. if (fixed) {
  3268. adjust_precision(precision, exp + digits_in_the_first_segment);
  3269. }
  3270. // Use Dragon4 only when there might be not enough digits in the first
  3271. // segment.
  3272. if (digits_in_the_first_segment > precision) {
  3273. use_dragon = false;
  3274. if (precision <= 0) {
  3275. exp += digits_in_the_first_segment;
  3276. if (precision < 0) {
  3277. // Nothing to do, since all we have are just leading zeros.
  3278. buf.try_resize(0);
  3279. } else {
  3280. // We may need to round-up.
  3281. buf.try_resize(1);
  3282. if ((first_segment | static_cast<uint64_t>(has_more_segments)) >
  3283. 5000000000000000000ULL) {
  3284. buf[0] = '1';
  3285. } else {
  3286. buf[0] = '0';
  3287. }
  3288. }
  3289. } // precision <= 0
  3290. else {
  3291. exp += digits_in_the_first_segment - precision;
  3292. // When precision > 0, we divide the first segment into three
  3293. // subsegments, each with 9, 9, and 0 ~ 1 digits so that each fits
  3294. // in 32-bits which usually allows faster calculation than in
  3295. // 64-bits. Since some compiler (e.g. MSVC) doesn't know how to optimize
  3296. // division-by-constant for large 64-bit divisors, we do it here
  3297. // manually. The magic number 7922816251426433760 below is equal to
  3298. // ceil(2^(64+32) / 10^10).
  3299. const uint32_t first_subsegment = static_cast<uint32_t>(
  3300. dragonbox::umul128_upper64(first_segment, 7922816251426433760ULL) >>
  3301. 32);
  3302. const uint64_t second_third_subsegments =
  3303. first_segment - first_subsegment * 10000000000ULL;
  3304. uint64_t prod;
  3305. uint32_t digits;
  3306. bool should_round_up;
  3307. int number_of_digits_to_print = precision > 9 ? 9 : precision;
  3308. // Print a 9-digits subsegment, either the first or the second.
  3309. auto print_subsegment = [&](uint32_t subsegment, char* buffer) {
  3310. int number_of_digits_printed = 0;
  3311. // If we want to print an odd number of digits from the subsegment,
  3312. if ((number_of_digits_to_print & 1) != 0) {
  3313. // Convert to 64-bit fixed-point fractional form with 1-digit
  3314. // integer part. The magic number 720575941 is a good enough
  3315. // approximation of 2^(32 + 24) / 10^8; see
  3316. // https://jk-jeon.github.io/posts/2022/12/fixed-precision-formatting/#fixed-length-case
  3317. // for details.
  3318. prod = ((subsegment * static_cast<uint64_t>(720575941)) >> 24) + 1;
  3319. digits = static_cast<uint32_t>(prod >> 32);
  3320. *buffer = static_cast<char>('0' + digits);
  3321. number_of_digits_printed++;
  3322. }
  3323. // If we want to print an even number of digits from the
  3324. // first_subsegment,
  3325. else {
  3326. // Convert to 64-bit fixed-point fractional form with 2-digits
  3327. // integer part. The magic number 450359963 is a good enough
  3328. // approximation of 2^(32 + 20) / 10^7; see
  3329. // https://jk-jeon.github.io/posts/2022/12/fixed-precision-formatting/#fixed-length-case
  3330. // for details.
  3331. prod = ((subsegment * static_cast<uint64_t>(450359963)) >> 20) + 1;
  3332. digits = static_cast<uint32_t>(prod >> 32);
  3333. copy2(buffer, digits2(digits));
  3334. number_of_digits_printed += 2;
  3335. }
  3336. // Print all digit pairs.
  3337. while (number_of_digits_printed < number_of_digits_to_print) {
  3338. prod = static_cast<uint32_t>(prod) * static_cast<uint64_t>(100);
  3339. digits = static_cast<uint32_t>(prod >> 32);
  3340. copy2(buffer + number_of_digits_printed, digits2(digits));
  3341. number_of_digits_printed += 2;
  3342. }
  3343. };
  3344. // Print first subsegment.
  3345. print_subsegment(first_subsegment, buf.data());
  3346. // Perform rounding if the first subsegment is the last subsegment to
  3347. // print.
  3348. if (precision <= 9) {
  3349. // Rounding inside the subsegment.
  3350. // We round-up if:
  3351. // - either the fractional part is strictly larger than 1/2, or
  3352. // - the fractional part is exactly 1/2 and the last digit is odd.
  3353. // We rely on the following observations:
  3354. // - If fractional_part >= threshold, then the fractional part is
  3355. // strictly larger than 1/2.
  3356. // - If the MSB of fractional_part is set, then the fractional part
  3357. // must be at least 1/2.
  3358. // - When the MSB of fractional_part is set, either
  3359. // second_third_subsegments being nonzero or has_more_segments
  3360. // being true means there are further digits not printed, so the
  3361. // fractional part is strictly larger than 1/2.
  3362. if (precision < 9) {
  3363. uint32_t fractional_part = static_cast<uint32_t>(prod);
  3364. should_round_up = fractional_part >=
  3365. data::fractional_part_rounding_thresholds
  3366. [8 - number_of_digits_to_print] ||
  3367. ((fractional_part >> 31) &
  3368. ((digits & 1) | (second_third_subsegments != 0) |
  3369. has_more_segments)) != 0;
  3370. }
  3371. // Rounding at the subsegment boundary.
  3372. // In this case, the fractional part is at least 1/2 if and only if
  3373. // second_third_subsegments >= 5000000000ULL, and is strictly larger
  3374. // than 1/2 if we further have either second_third_subsegments >
  3375. // 5000000000ULL or has_more_segments == true.
  3376. else {
  3377. should_round_up = second_third_subsegments > 5000000000ULL ||
  3378. (second_third_subsegments == 5000000000ULL &&
  3379. ((digits & 1) != 0 || has_more_segments));
  3380. }
  3381. }
  3382. // Otherwise, print the second subsegment.
  3383. else {
  3384. // Compilers are not aware of how to leverage the maximum value of
  3385. // second_third_subsegments to find out a better magic number which
  3386. // allows us to eliminate an additional shift. 1844674407370955162 =
  3387. // ceil(2^64/10) < ceil(2^64*(10^9/(10^10 - 1))).
  3388. const uint32_t second_subsegment =
  3389. static_cast<uint32_t>(dragonbox::umul128_upper64(
  3390. second_third_subsegments, 1844674407370955162ULL));
  3391. const uint32_t third_subsegment =
  3392. static_cast<uint32_t>(second_third_subsegments) -
  3393. second_subsegment * 10;
  3394. number_of_digits_to_print = precision - 9;
  3395. print_subsegment(second_subsegment, buf.data() + 9);
  3396. // Rounding inside the subsegment.
  3397. if (precision < 18) {
  3398. // The condition third_subsegment != 0 implies that the segment was
  3399. // of 19 digits, so in this case the third segment should be
  3400. // consisting of a genuine digit from the input.
  3401. uint32_t fractional_part = static_cast<uint32_t>(prod);
  3402. should_round_up = fractional_part >=
  3403. data::fractional_part_rounding_thresholds
  3404. [8 - number_of_digits_to_print] ||
  3405. ((fractional_part >> 31) &
  3406. ((digits & 1) | (third_subsegment != 0) |
  3407. has_more_segments)) != 0;
  3408. }
  3409. // Rounding at the subsegment boundary.
  3410. else {
  3411. // In this case, the segment must be of 19 digits, thus
  3412. // the third subsegment should be consisting of a genuine digit from
  3413. // the input.
  3414. should_round_up = third_subsegment > 5 ||
  3415. (third_subsegment == 5 &&
  3416. ((digits & 1) != 0 || has_more_segments));
  3417. }
  3418. }
  3419. // Round-up if necessary.
  3420. if (should_round_up) {
  3421. ++buf[precision - 1];
  3422. for (int i = precision - 1; i > 0 && buf[i] > '9'; --i) {
  3423. buf[i] = '0';
  3424. ++buf[i - 1];
  3425. }
  3426. if (buf[0] > '9') {
  3427. buf[0] = '1';
  3428. if (fixed)
  3429. buf[precision++] = '0';
  3430. else
  3431. ++exp;
  3432. }
  3433. }
  3434. buf.try_resize(to_unsigned(precision));
  3435. }
  3436. } // if (digits_in_the_first_segment > precision)
  3437. else {
  3438. // Adjust the exponent for its use in Dragon4.
  3439. exp += digits_in_the_first_segment - 1;
  3440. }
  3441. }
  3442. if (use_dragon) {
  3443. auto f = basic_fp<uint128_t>();
  3444. bool is_predecessor_closer = specs.binary32
  3445. ? f.assign(static_cast<float>(value))
  3446. : f.assign(converted_value);
  3447. if (is_predecessor_closer) dragon_flags |= dragon::predecessor_closer;
  3448. if (fixed) dragon_flags |= dragon::fixed;
  3449. // Limit precision to the maximum possible number of significant digits in
  3450. // an IEEE754 double because we don't need to generate zeros.
  3451. const int max_double_digits = 767;
  3452. if (precision > max_double_digits) precision = max_double_digits;
  3453. format_dragon(f, dragon_flags, precision, buf, exp);
  3454. }
  3455. if (!fixed && !specs.showpoint) {
  3456. // Remove trailing zeros.
  3457. auto num_digits = buf.size();
  3458. while (num_digits > 0 && buf[num_digits - 1] == '0') {
  3459. --num_digits;
  3460. ++exp;
  3461. }
  3462. buf.try_resize(num_digits);
  3463. }
  3464. return exp;
  3465. }
  3466. template <typename Char, typename OutputIt, typename T>
  3467. FMT_CONSTEXPR20 auto write_float(OutputIt out, T value,
  3468. format_specs<Char> specs, locale_ref loc)
  3469. -> OutputIt {
  3470. float_specs fspecs = parse_float_type_spec(specs);
  3471. fspecs.sign = specs.sign;
  3472. if (detail::signbit(value)) { // value < 0 is false for NaN so use signbit.
  3473. fspecs.sign = sign::minus;
  3474. value = -value;
  3475. } else if (fspecs.sign == sign::minus) {
  3476. fspecs.sign = sign::none;
  3477. }
  3478. if (!detail::isfinite(value))
  3479. return write_nonfinite(out, detail::isnan(value), specs, fspecs);
  3480. if (specs.align == align::numeric && fspecs.sign) {
  3481. auto it = reserve(out, 1);
  3482. *it++ = detail::sign<Char>(fspecs.sign);
  3483. out = base_iterator(out, it);
  3484. fspecs.sign = sign::none;
  3485. if (specs.width != 0) --specs.width;
  3486. }
  3487. memory_buffer buffer;
  3488. if (fspecs.format == float_format::hex) {
  3489. if (fspecs.sign) buffer.push_back(detail::sign<char>(fspecs.sign));
  3490. format_hexfloat(convert_float(value), specs.precision, fspecs, buffer);
  3491. return write_bytes<align::right>(out, {buffer.data(), buffer.size()},
  3492. specs);
  3493. }
  3494. int precision = specs.precision >= 0 || specs.type == presentation_type::none
  3495. ? specs.precision
  3496. : 6;
  3497. if (fspecs.format == float_format::exp) {
  3498. if (precision == max_value<int>())
  3499. throw_format_error("number is too big");
  3500. else
  3501. ++precision;
  3502. } else if (fspecs.format != float_format::fixed && precision == 0) {
  3503. precision = 1;
  3504. }
  3505. if (const_check(std::is_same<T, float>())) fspecs.binary32 = true;
  3506. int exp = format_float(convert_float(value), precision, fspecs, buffer);
  3507. fspecs.precision = precision;
  3508. auto f = big_decimal_fp{buffer.data(), static_cast<int>(buffer.size()), exp};
  3509. return write_float(out, f, specs, fspecs, loc);
  3510. }
  3511. template <typename Char, typename OutputIt, typename T,
  3512. FMT_ENABLE_IF(is_floating_point<T>::value)>
  3513. FMT_CONSTEXPR20 auto write(OutputIt out, T value, format_specs<Char> specs,
  3514. locale_ref loc = {}) -> OutputIt {
  3515. if (const_check(!is_supported_floating_point(value))) return out;
  3516. return specs.localized && write_loc(out, value, specs, loc)
  3517. ? out
  3518. : write_float(out, value, specs, loc);
  3519. }
  3520. template <typename Char, typename OutputIt, typename T,
  3521. FMT_ENABLE_IF(is_fast_float<T>::value)>
  3522. FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt {
  3523. if (is_constant_evaluated()) return write(out, value, format_specs<Char>());
  3524. if (const_check(!is_supported_floating_point(value))) return out;
  3525. auto fspecs = float_specs();
  3526. if (detail::signbit(value)) {
  3527. fspecs.sign = sign::minus;
  3528. value = -value;
  3529. }
  3530. constexpr auto specs = format_specs<Char>();
  3531. using floaty = conditional_t<std::is_same<T, long double>::value, double, T>;
  3532. using floaty_uint = typename dragonbox::float_info<floaty>::carrier_uint;
  3533. floaty_uint mask = exponent_mask<floaty>();
  3534. if ((bit_cast<floaty_uint>(value) & mask) == mask)
  3535. return write_nonfinite(out, std::isnan(value), specs, fspecs);
  3536. auto dec = dragonbox::to_decimal(static_cast<floaty>(value));
  3537. return write_float(out, dec, specs, fspecs, {});
  3538. }
  3539. template <typename Char, typename OutputIt, typename T,
  3540. FMT_ENABLE_IF(is_floating_point<T>::value &&
  3541. !is_fast_float<T>::value)>
  3542. inline auto write(OutputIt out, T value) -> OutputIt {
  3543. return write(out, value, format_specs<Char>());
  3544. }
  3545. template <typename Char, typename OutputIt>
  3546. auto write(OutputIt out, monostate, format_specs<Char> = {}, locale_ref = {})
  3547. -> OutputIt {
  3548. FMT_ASSERT(false, "");
  3549. return out;
  3550. }
  3551. template <typename Char, typename OutputIt>
  3552. FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> value)
  3553. -> OutputIt {
  3554. auto it = reserve(out, value.size());
  3555. it = copy_str_noinline<Char>(value.begin(), value.end(), it);
  3556. return base_iterator(out, it);
  3557. }
  3558. template <typename Char, typename OutputIt, typename T,
  3559. FMT_ENABLE_IF(is_string<T>::value)>
  3560. constexpr auto write(OutputIt out, const T& value) -> OutputIt {
  3561. return write<Char>(out, to_string_view(value));
  3562. }
  3563. // FMT_ENABLE_IF() condition separated to workaround an MSVC bug.
  3564. template <
  3565. typename Char, typename OutputIt, typename T,
  3566. bool check =
  3567. std::is_enum<T>::value && !std::is_same<T, Char>::value &&
  3568. mapped_type_constant<T, basic_format_context<OutputIt, Char>>::value !=
  3569. type::custom_type,
  3570. FMT_ENABLE_IF(check)>
  3571. FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt {
  3572. return write<Char>(out, static_cast<underlying_t<T>>(value));
  3573. }
  3574. template <typename Char, typename OutputIt, typename T,
  3575. FMT_ENABLE_IF(std::is_same<T, bool>::value)>
  3576. FMT_CONSTEXPR auto write(OutputIt out, T value,
  3577. const format_specs<Char>& specs = {}, locale_ref = {})
  3578. -> OutputIt {
  3579. return specs.type != presentation_type::none &&
  3580. specs.type != presentation_type::string
  3581. ? write(out, value ? 1 : 0, specs, {})
  3582. : write_bytes(out, value ? "true" : "false", specs);
  3583. }
  3584. template <typename Char, typename OutputIt>
  3585. FMT_CONSTEXPR auto write(OutputIt out, Char value) -> OutputIt {
  3586. auto it = reserve(out, 1);
  3587. *it++ = value;
  3588. return base_iterator(out, it);
  3589. }
  3590. template <typename Char, typename OutputIt>
  3591. FMT_CONSTEXPR_CHAR_TRAITS auto write(OutputIt out, const Char* value)
  3592. -> OutputIt {
  3593. if (value) return write(out, basic_string_view<Char>(value));
  3594. throw_format_error("string pointer is null");
  3595. return out;
  3596. }
  3597. template <typename Char, typename OutputIt, typename T,
  3598. FMT_ENABLE_IF(std::is_same<T, void>::value)>
  3599. auto write(OutputIt out, const T* value, const format_specs<Char>& specs = {},
  3600. locale_ref = {}) -> OutputIt {
  3601. return write_ptr<Char>(out, bit_cast<uintptr_t>(value), &specs);
  3602. }
  3603. // A write overload that handles implicit conversions.
  3604. template <typename Char, typename OutputIt, typename T,
  3605. typename Context = basic_format_context<OutputIt, Char>>
  3606. FMT_CONSTEXPR auto write(OutputIt out, const T& value) -> enable_if_t<
  3607. std::is_class<T>::value && !is_string<T>::value &&
  3608. !is_floating_point<T>::value && !std::is_same<T, Char>::value &&
  3609. !std::is_same<T, remove_cvref_t<decltype(arg_mapper<Context>().map(
  3610. value))>>::value,
  3611. OutputIt> {
  3612. return write<Char>(out, arg_mapper<Context>().map(value));
  3613. }
  3614. template <typename Char, typename OutputIt, typename T,
  3615. typename Context = basic_format_context<OutputIt, Char>>
  3616. FMT_CONSTEXPR auto write(OutputIt out, const T& value)
  3617. -> enable_if_t<mapped_type_constant<T, Context>::value == type::custom_type,
  3618. OutputIt> {
  3619. auto ctx = Context(out, {}, {});
  3620. return typename Context::template formatter_type<T>().format(value, ctx);
  3621. }
  3622. // An argument visitor that formats the argument and writes it via the output
  3623. // iterator. It's a class and not a generic lambda for compatibility with C++11.
  3624. template <typename Char> struct default_arg_formatter {
  3625. using iterator = buffer_appender<Char>;
  3626. using context = buffer_context<Char>;
  3627. iterator out;
  3628. basic_format_args<context> args;
  3629. locale_ref loc;
  3630. template <typename T> auto operator()(T value) -> iterator {
  3631. return write<Char>(out, value);
  3632. }
  3633. auto operator()(typename basic_format_arg<context>::handle h) -> iterator {
  3634. basic_format_parse_context<Char> parse_ctx({});
  3635. context format_ctx(out, args, loc);
  3636. h.format(parse_ctx, format_ctx);
  3637. return format_ctx.out();
  3638. }
  3639. };
  3640. template <typename Char> struct arg_formatter {
  3641. using iterator = buffer_appender<Char>;
  3642. using context = buffer_context<Char>;
  3643. iterator out;
  3644. const format_specs<Char>& specs;
  3645. locale_ref locale;
  3646. template <typename T>
  3647. FMT_CONSTEXPR FMT_INLINE auto operator()(T value) -> iterator {
  3648. return detail::write(out, value, specs, locale);
  3649. }
  3650. auto operator()(typename basic_format_arg<context>::handle) -> iterator {
  3651. // User-defined types are handled separately because they require access
  3652. // to the parse context.
  3653. return out;
  3654. }
  3655. };
  3656. template <typename Char> struct custom_formatter {
  3657. basic_format_parse_context<Char>& parse_ctx;
  3658. buffer_context<Char>& ctx;
  3659. void operator()(
  3660. typename basic_format_arg<buffer_context<Char>>::handle h) const {
  3661. h.format(parse_ctx, ctx);
  3662. }
  3663. template <typename T> void operator()(T) const {}
  3664. };
  3665. template <typename ErrorHandler> class width_checker {
  3666. public:
  3667. explicit FMT_CONSTEXPR width_checker(ErrorHandler& eh) : handler_(eh) {}
  3668. template <typename T, FMT_ENABLE_IF(is_integer<T>::value)>
  3669. FMT_CONSTEXPR auto operator()(T value) -> unsigned long long {
  3670. if (is_negative(value)) handler_.on_error("negative width");
  3671. return static_cast<unsigned long long>(value);
  3672. }
  3673. template <typename T, FMT_ENABLE_IF(!is_integer<T>::value)>
  3674. FMT_CONSTEXPR auto operator()(T) -> unsigned long long {
  3675. handler_.on_error("width is not integer");
  3676. return 0;
  3677. }
  3678. private:
  3679. ErrorHandler& handler_;
  3680. };
  3681. template <typename ErrorHandler> class precision_checker {
  3682. public:
  3683. explicit FMT_CONSTEXPR precision_checker(ErrorHandler& eh) : handler_(eh) {}
  3684. template <typename T, FMT_ENABLE_IF(is_integer<T>::value)>
  3685. FMT_CONSTEXPR auto operator()(T value) -> unsigned long long {
  3686. if (is_negative(value)) handler_.on_error("negative precision");
  3687. return static_cast<unsigned long long>(value);
  3688. }
  3689. template <typename T, FMT_ENABLE_IF(!is_integer<T>::value)>
  3690. FMT_CONSTEXPR auto operator()(T) -> unsigned long long {
  3691. handler_.on_error("precision is not integer");
  3692. return 0;
  3693. }
  3694. private:
  3695. ErrorHandler& handler_;
  3696. };
  3697. template <template <typename> class Handler, typename FormatArg,
  3698. typename ErrorHandler>
  3699. FMT_CONSTEXPR auto get_dynamic_spec(FormatArg arg, ErrorHandler eh) -> int {
  3700. unsigned long long value = visit_format_arg(Handler<ErrorHandler>(eh), arg);
  3701. if (value > to_unsigned(max_value<int>())) eh.on_error("number is too big");
  3702. return static_cast<int>(value);
  3703. }
  3704. template <typename Context, typename ID>
  3705. FMT_CONSTEXPR auto get_arg(Context& ctx, ID id) ->
  3706. typename Context::format_arg {
  3707. auto arg = ctx.arg(id);
  3708. if (!arg) ctx.on_error("argument not found");
  3709. return arg;
  3710. }
  3711. template <template <typename> class Handler, typename Context>
  3712. FMT_CONSTEXPR void handle_dynamic_spec(int& value,
  3713. arg_ref<typename Context::char_type> ref,
  3714. Context& ctx) {
  3715. switch (ref.kind) {
  3716. case arg_id_kind::none:
  3717. break;
  3718. case arg_id_kind::index:
  3719. value = detail::get_dynamic_spec<Handler>(get_arg(ctx, ref.val.index),
  3720. ctx.error_handler());
  3721. break;
  3722. case arg_id_kind::name:
  3723. value = detail::get_dynamic_spec<Handler>(get_arg(ctx, ref.val.name),
  3724. ctx.error_handler());
  3725. break;
  3726. }
  3727. }
  3728. #if FMT_USE_USER_DEFINED_LITERALS
  3729. template <typename Char> struct udl_formatter {
  3730. basic_string_view<Char> str;
  3731. template <typename... T>
  3732. auto operator()(T&&... args) const -> std::basic_string<Char> {
  3733. return vformat(str, fmt::make_format_args<buffer_context<Char>>(args...));
  3734. }
  3735. };
  3736. # if FMT_USE_NONTYPE_TEMPLATE_ARGS
  3737. template <typename T, typename Char, size_t N,
  3738. fmt::detail_exported::fixed_string<Char, N> Str>
  3739. struct statically_named_arg : view {
  3740. static constexpr auto name = Str.data;
  3741. const T& value;
  3742. statically_named_arg(const T& v) : value(v) {}
  3743. };
  3744. template <typename T, typename Char, size_t N,
  3745. fmt::detail_exported::fixed_string<Char, N> Str>
  3746. struct is_named_arg<statically_named_arg<T, Char, N, Str>> : std::true_type {};
  3747. template <typename T, typename Char, size_t N,
  3748. fmt::detail_exported::fixed_string<Char, N> Str>
  3749. struct is_statically_named_arg<statically_named_arg<T, Char, N, Str>>
  3750. : std::true_type {};
  3751. template <typename Char, size_t N,
  3752. fmt::detail_exported::fixed_string<Char, N> Str>
  3753. struct udl_arg {
  3754. template <typename T> auto operator=(T&& value) const {
  3755. return statically_named_arg<T, Char, N, Str>(std::forward<T>(value));
  3756. }
  3757. };
  3758. # else
  3759. template <typename Char> struct udl_arg {
  3760. const Char* str;
  3761. template <typename T> auto operator=(T&& value) const -> named_arg<Char, T> {
  3762. return {str, std::forward<T>(value)};
  3763. }
  3764. };
  3765. # endif
  3766. #endif // FMT_USE_USER_DEFINED_LITERALS
  3767. template <typename Locale, typename Char>
  3768. auto vformat(const Locale& loc, basic_string_view<Char> fmt,
  3769. basic_format_args<buffer_context<type_identity_t<Char>>> args)
  3770. -> std::basic_string<Char> {
  3771. auto buf = basic_memory_buffer<Char>();
  3772. detail::vformat_to(buf, fmt, args, detail::locale_ref(loc));
  3773. return {buf.data(), buf.size()};
  3774. }
  3775. using format_func = void (*)(detail::buffer<char>&, int, const char*);
  3776. FMT_API void format_error_code(buffer<char>& out, int error_code,
  3777. string_view message) noexcept;
  3778. FMT_API void report_error(format_func func, int error_code,
  3779. const char* message) noexcept;
  3780. FMT_END_DETAIL_NAMESPACE
  3781. FMT_API auto vsystem_error(int error_code, string_view format_str,
  3782. format_args args) -> std::system_error;
  3783. /**
  3784. \rst
  3785. Constructs :class:`std::system_error` with a message formatted with
  3786. ``fmt::format(fmt, args...)``.
  3787. *error_code* is a system error code as given by ``errno``.
  3788. **Example**::
  3789. // This throws std::system_error with the description
  3790. // cannot open file 'madeup': No such file or directory
  3791. // or similar (system message may vary).
  3792. const char* filename = "madeup";
  3793. std::FILE* file = std::fopen(filename, "r");
  3794. if (!file)
  3795. throw fmt::system_error(errno, "cannot open file '{}'", filename);
  3796. \endrst
  3797. */
  3798. template <typename... T>
  3799. auto system_error(int error_code, format_string<T...> fmt, T&&... args)
  3800. -> std::system_error {
  3801. return vsystem_error(error_code, fmt, fmt::make_format_args(args...));
  3802. }
  3803. /**
  3804. \rst
  3805. Formats an error message for an error returned by an operating system or a
  3806. language runtime, for example a file opening error, and writes it to *out*.
  3807. The format is the same as the one used by ``std::system_error(ec, message)``
  3808. where ``ec`` is ``std::error_code(error_code, std::generic_category()})``.
  3809. It is implementation-defined but normally looks like:
  3810. .. parsed-literal::
  3811. *<message>*: *<system-message>*
  3812. where *<message>* is the passed message and *<system-message>* is the system
  3813. message corresponding to the error code.
  3814. *error_code* is a system error code as given by ``errno``.
  3815. \endrst
  3816. */
  3817. FMT_API void format_system_error(detail::buffer<char>& out, int error_code,
  3818. const char* message) noexcept;
  3819. // Reports a system error without throwing an exception.
  3820. // Can be used to report errors from destructors.
  3821. FMT_API void report_system_error(int error_code, const char* message) noexcept;
  3822. /** Fast integer formatter. */
  3823. class format_int {
  3824. private:
  3825. // Buffer should be large enough to hold all digits (digits10 + 1),
  3826. // a sign and a null character.
  3827. enum { buffer_size = std::numeric_limits<unsigned long long>::digits10 + 3 };
  3828. mutable char buffer_[buffer_size];
  3829. char* str_;
  3830. template <typename UInt> auto format_unsigned(UInt value) -> char* {
  3831. auto n = static_cast<detail::uint32_or_64_or_128_t<UInt>>(value);
  3832. return detail::format_decimal(buffer_, n, buffer_size - 1).begin;
  3833. }
  3834. template <typename Int> auto format_signed(Int value) -> char* {
  3835. auto abs_value = static_cast<detail::uint32_or_64_or_128_t<Int>>(value);
  3836. bool negative = value < 0;
  3837. if (negative) abs_value = 0 - abs_value;
  3838. auto begin = format_unsigned(abs_value);
  3839. if (negative) *--begin = '-';
  3840. return begin;
  3841. }
  3842. public:
  3843. explicit format_int(int value) : str_(format_signed(value)) {}
  3844. explicit format_int(long value) : str_(format_signed(value)) {}
  3845. explicit format_int(long long value) : str_(format_signed(value)) {}
  3846. explicit format_int(unsigned value) : str_(format_unsigned(value)) {}
  3847. explicit format_int(unsigned long value) : str_(format_unsigned(value)) {}
  3848. explicit format_int(unsigned long long value)
  3849. : str_(format_unsigned(value)) {}
  3850. /** Returns the number of characters written to the output buffer. */
  3851. auto size() const -> size_t {
  3852. return detail::to_unsigned(buffer_ - str_ + buffer_size - 1);
  3853. }
  3854. /**
  3855. Returns a pointer to the output buffer content. No terminating null
  3856. character is appended.
  3857. */
  3858. auto data() const -> const char* { return str_; }
  3859. /**
  3860. Returns a pointer to the output buffer content with terminating null
  3861. character appended.
  3862. */
  3863. auto c_str() const -> const char* {
  3864. buffer_[buffer_size - 1] = '\0';
  3865. return str_;
  3866. }
  3867. /**
  3868. \rst
  3869. Returns the content of the output buffer as an ``std::string``.
  3870. \endrst
  3871. */
  3872. auto str() const -> std::string { return std::string(str_, size()); }
  3873. };
  3874. template <typename T, typename Char>
  3875. struct formatter<T, Char, enable_if_t<detail::has_format_as<T>::value>>
  3876. : private formatter<detail::format_as_t<T>> {
  3877. using base = formatter<detail::format_as_t<T>>;
  3878. using base::parse;
  3879. template <typename FormatContext>
  3880. auto format(const T& value, FormatContext& ctx) const -> decltype(ctx.out()) {
  3881. return base::format(format_as(value), ctx);
  3882. }
  3883. };
  3884. template <typename Char>
  3885. struct formatter<void*, Char> : formatter<const void*, Char> {
  3886. template <typename FormatContext>
  3887. auto format(void* val, FormatContext& ctx) const -> decltype(ctx.out()) {
  3888. return formatter<const void*, Char>::format(val, ctx);
  3889. }
  3890. };
  3891. template <typename Char, size_t N>
  3892. struct formatter<Char[N], Char> : formatter<basic_string_view<Char>, Char> {
  3893. template <typename FormatContext>
  3894. FMT_CONSTEXPR auto format(const Char* val, FormatContext& ctx) const
  3895. -> decltype(ctx.out()) {
  3896. return formatter<basic_string_view<Char>, Char>::format(val, ctx);
  3897. }
  3898. };
  3899. /**
  3900. \rst
  3901. Converts ``p`` to ``const void*`` for pointer formatting.
  3902. **Example**::
  3903. auto s = fmt::format("{}", fmt::ptr(p));
  3904. \endrst
  3905. */
  3906. template <typename T> auto ptr(T p) -> const void* {
  3907. static_assert(std::is_pointer<T>::value, "");
  3908. return detail::bit_cast<const void*>(p);
  3909. }
  3910. template <typename T, typename Deleter>
  3911. auto ptr(const std::unique_ptr<T, Deleter>& p) -> const void* {
  3912. return p.get();
  3913. }
  3914. template <typename T> auto ptr(const std::shared_ptr<T>& p) -> const void* {
  3915. return p.get();
  3916. }
  3917. /**
  3918. \rst
  3919. Converts ``e`` to the underlying type.
  3920. **Example**::
  3921. enum class color { red, green, blue };
  3922. auto s = fmt::format("{}", fmt::underlying(color::red));
  3923. \endrst
  3924. */
  3925. template <typename Enum>
  3926. constexpr auto underlying(Enum e) noexcept -> underlying_t<Enum> {
  3927. return static_cast<underlying_t<Enum>>(e);
  3928. }
  3929. namespace enums {
  3930. template <typename Enum, FMT_ENABLE_IF(std::is_enum<Enum>::value)>
  3931. constexpr auto format_as(Enum e) noexcept -> underlying_t<Enum> {
  3932. return static_cast<underlying_t<Enum>>(e);
  3933. }
  3934. } // namespace enums
  3935. class bytes {
  3936. private:
  3937. string_view data_;
  3938. friend struct formatter<bytes>;
  3939. public:
  3940. explicit bytes(string_view data) : data_(data) {}
  3941. };
  3942. template <> struct formatter<bytes> {
  3943. private:
  3944. detail::dynamic_format_specs<> specs_;
  3945. public:
  3946. template <typename ParseContext>
  3947. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const char* {
  3948. return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx,
  3949. detail::type::string_type);
  3950. }
  3951. template <typename FormatContext>
  3952. auto format(bytes b, FormatContext& ctx) -> decltype(ctx.out()) {
  3953. detail::handle_dynamic_spec<detail::width_checker>(specs_.width,
  3954. specs_.width_ref, ctx);
  3955. detail::handle_dynamic_spec<detail::precision_checker>(
  3956. specs_.precision, specs_.precision_ref, ctx);
  3957. return detail::write_bytes(ctx.out(), b.data_, specs_);
  3958. }
  3959. };
  3960. // group_digits_view is not derived from view because it copies the argument.
  3961. template <typename T> struct group_digits_view { T value; };
  3962. /**
  3963. \rst
  3964. Returns a view that formats an integer value using ',' as a locale-independent
  3965. thousands separator.
  3966. **Example**::
  3967. fmt::print("{}", fmt::group_digits(12345));
  3968. // Output: "12,345"
  3969. \endrst
  3970. */
  3971. template <typename T> auto group_digits(T value) -> group_digits_view<T> {
  3972. return {value};
  3973. }
  3974. template <typename T> struct formatter<group_digits_view<T>> : formatter<T> {
  3975. private:
  3976. detail::dynamic_format_specs<> specs_;
  3977. public:
  3978. template <typename ParseContext>
  3979. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const char* {
  3980. return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx,
  3981. detail::type::int_type);
  3982. }
  3983. template <typename FormatContext>
  3984. auto format(group_digits_view<T> t, FormatContext& ctx)
  3985. -> decltype(ctx.out()) {
  3986. detail::handle_dynamic_spec<detail::width_checker>(specs_.width,
  3987. specs_.width_ref, ctx);
  3988. detail::handle_dynamic_spec<detail::precision_checker>(
  3989. specs_.precision, specs_.precision_ref, ctx);
  3990. return detail::write_int(
  3991. ctx.out(), static_cast<detail::uint64_or_128_t<T>>(t.value), 0, specs_,
  3992. detail::digit_grouping<char>("\3", ","));
  3993. }
  3994. };
  3995. // DEPRECATED! join_view will be moved to ranges.h.
  3996. template <typename It, typename Sentinel, typename Char = char>
  3997. struct join_view : detail::view {
  3998. It begin;
  3999. Sentinel end;
  4000. basic_string_view<Char> sep;
  4001. join_view(It b, Sentinel e, basic_string_view<Char> s)
  4002. : begin(b), end(e), sep(s) {}
  4003. };
  4004. template <typename It, typename Sentinel, typename Char>
  4005. struct formatter<join_view<It, Sentinel, Char>, Char> {
  4006. private:
  4007. using value_type =
  4008. #ifdef __cpp_lib_ranges
  4009. std::iter_value_t<It>;
  4010. #else
  4011. typename std::iterator_traits<It>::value_type;
  4012. #endif
  4013. formatter<remove_cvref_t<value_type>, Char> value_formatter_;
  4014. public:
  4015. template <typename ParseContext>
  4016. FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const Char* {
  4017. return value_formatter_.parse(ctx);
  4018. }
  4019. template <typename FormatContext>
  4020. auto format(const join_view<It, Sentinel, Char>& value,
  4021. FormatContext& ctx) const -> decltype(ctx.out()) {
  4022. auto it = value.begin;
  4023. auto out = ctx.out();
  4024. if (it != value.end) {
  4025. out = value_formatter_.format(*it, ctx);
  4026. ++it;
  4027. while (it != value.end) {
  4028. out = detail::copy_str<Char>(value.sep.begin(), value.sep.end(), out);
  4029. ctx.advance_to(out);
  4030. out = value_formatter_.format(*it, ctx);
  4031. ++it;
  4032. }
  4033. }
  4034. return out;
  4035. }
  4036. };
  4037. /**
  4038. Returns a view that formats the iterator range `[begin, end)` with elements
  4039. separated by `sep`.
  4040. */
  4041. template <typename It, typename Sentinel>
  4042. auto join(It begin, Sentinel end, string_view sep) -> join_view<It, Sentinel> {
  4043. return {begin, end, sep};
  4044. }
  4045. /**
  4046. \rst
  4047. Returns a view that formats `range` with elements separated by `sep`.
  4048. **Example**::
  4049. std::vector<int> v = {1, 2, 3};
  4050. fmt::print("{}", fmt::join(v, ", "));
  4051. // Output: "1, 2, 3"
  4052. ``fmt::join`` applies passed format specifiers to the range elements::
  4053. fmt::print("{:02}", fmt::join(v, ", "));
  4054. // Output: "01, 02, 03"
  4055. \endrst
  4056. */
  4057. template <typename Range>
  4058. auto join(Range&& range, string_view sep)
  4059. -> join_view<detail::iterator_t<Range>, detail::sentinel_t<Range>> {
  4060. return join(std::begin(range), std::end(range), sep);
  4061. }
  4062. /**
  4063. \rst
  4064. Converts *value* to ``std::string`` using the default format for type *T*.
  4065. **Example**::
  4066. #include <fmt/format.h>
  4067. std::string answer = fmt::to_string(42);
  4068. \endrst
  4069. */
  4070. template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
  4071. inline auto to_string(const T& value) -> std::string {
  4072. auto buffer = memory_buffer();
  4073. detail::write<char>(appender(buffer), value);
  4074. return {buffer.data(), buffer.size()};
  4075. }
  4076. template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
  4077. FMT_NODISCARD inline auto to_string(T value) -> std::string {
  4078. // The buffer should be large enough to store the number including the sign
  4079. // or "false" for bool.
  4080. constexpr int max_size = detail::digits10<T>() + 2;
  4081. char buffer[max_size > 5 ? static_cast<unsigned>(max_size) : 5];
  4082. char* begin = buffer;
  4083. return std::string(begin, detail::write<char>(begin, value));
  4084. }
  4085. template <typename Char, size_t SIZE>
  4086. FMT_NODISCARD auto to_string(const basic_memory_buffer<Char, SIZE>& buf)
  4087. -> std::basic_string<Char> {
  4088. auto size = buf.size();
  4089. detail::assume(size < std::basic_string<Char>().max_size());
  4090. return std::basic_string<Char>(buf.data(), size);
  4091. }
  4092. FMT_BEGIN_DETAIL_NAMESPACE
  4093. template <typename Char>
  4094. void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
  4095. typename vformat_args<Char>::type args, locale_ref loc) {
  4096. auto out = buffer_appender<Char>(buf);
  4097. if (fmt.size() == 2 && equal2(fmt.data(), "{}")) {
  4098. auto arg = args.get(0);
  4099. if (!arg) error_handler().on_error("argument not found");
  4100. visit_format_arg(default_arg_formatter<Char>{out, args, loc}, arg);
  4101. return;
  4102. }
  4103. struct format_handler : error_handler {
  4104. basic_format_parse_context<Char> parse_context;
  4105. buffer_context<Char> context;
  4106. format_handler(buffer_appender<Char> p_out, basic_string_view<Char> str,
  4107. basic_format_args<buffer_context<Char>> p_args,
  4108. locale_ref p_loc)
  4109. : parse_context(str), context(p_out, p_args, p_loc) {}
  4110. void on_text(const Char* begin, const Char* end) {
  4111. auto text = basic_string_view<Char>(begin, to_unsigned(end - begin));
  4112. context.advance_to(write<Char>(context.out(), text));
  4113. }
  4114. FMT_CONSTEXPR auto on_arg_id() -> int {
  4115. return parse_context.next_arg_id();
  4116. }
  4117. FMT_CONSTEXPR auto on_arg_id(int id) -> int {
  4118. return parse_context.check_arg_id(id), id;
  4119. }
  4120. FMT_CONSTEXPR auto on_arg_id(basic_string_view<Char> id) -> int {
  4121. int arg_id = context.arg_id(id);
  4122. if (arg_id < 0) on_error("argument not found");
  4123. return arg_id;
  4124. }
  4125. FMT_INLINE void on_replacement_field(int id, const Char*) {
  4126. auto arg = get_arg(context, id);
  4127. context.advance_to(visit_format_arg(
  4128. default_arg_formatter<Char>{context.out(), context.args(),
  4129. context.locale()},
  4130. arg));
  4131. }
  4132. auto on_format_specs(int id, const Char* begin, const Char* end)
  4133. -> const Char* {
  4134. auto arg = get_arg(context, id);
  4135. if (arg.type() == type::custom_type) {
  4136. parse_context.advance_to(begin);
  4137. visit_format_arg(custom_formatter<Char>{parse_context, context}, arg);
  4138. return parse_context.begin();
  4139. }
  4140. auto specs = detail::dynamic_format_specs<Char>();
  4141. begin = parse_format_specs(begin, end, specs, parse_context, arg.type());
  4142. detail::handle_dynamic_spec<detail::width_checker>(
  4143. specs.width, specs.width_ref, context);
  4144. detail::handle_dynamic_spec<detail::precision_checker>(
  4145. specs.precision, specs.precision_ref, context);
  4146. if (begin == end || *begin != '}')
  4147. on_error("missing '}' in format string");
  4148. auto f = arg_formatter<Char>{context.out(), specs, context.locale()};
  4149. context.advance_to(visit_format_arg(f, arg));
  4150. return begin;
  4151. }
  4152. };
  4153. detail::parse_format_string<false>(fmt, format_handler(out, fmt, args, loc));
  4154. }
  4155. #ifndef FMT_HEADER_ONLY
  4156. extern template FMT_API void vformat_to(buffer<char>&, string_view,
  4157. typename vformat_args<>::type,
  4158. locale_ref);
  4159. extern template FMT_API auto thousands_sep_impl<char>(locale_ref)
  4160. -> thousands_sep_result<char>;
  4161. extern template FMT_API auto thousands_sep_impl<wchar_t>(locale_ref)
  4162. -> thousands_sep_result<wchar_t>;
  4163. extern template FMT_API auto decimal_point_impl(locale_ref) -> char;
  4164. extern template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t;
  4165. #endif // FMT_HEADER_ONLY
  4166. FMT_END_DETAIL_NAMESPACE
  4167. #if FMT_USE_USER_DEFINED_LITERALS
  4168. inline namespace literals {
  4169. /**
  4170. \rst
  4171. User-defined literal equivalent of :func:`fmt::arg`.
  4172. **Example**::
  4173. using namespace fmt::literals;
  4174. fmt::print("Elapsed time: {s:.2f} seconds", "s"_a=1.23);
  4175. \endrst
  4176. */
  4177. # if FMT_USE_NONTYPE_TEMPLATE_ARGS
  4178. template <detail_exported::fixed_string Str> constexpr auto operator""_a() {
  4179. using char_t = remove_cvref_t<decltype(Str.data[0])>;
  4180. return detail::udl_arg<char_t, sizeof(Str.data) / sizeof(char_t), Str>();
  4181. }
  4182. # else
  4183. constexpr auto operator"" _a(const char* s, size_t) -> detail::udl_arg<char> {
  4184. return {s};
  4185. }
  4186. # endif
  4187. } // namespace literals
  4188. #endif // FMT_USE_USER_DEFINED_LITERALS
  4189. template <typename Locale, FMT_ENABLE_IF(detail::is_locale<Locale>::value)>
  4190. inline auto vformat(const Locale& loc, string_view fmt, format_args args)
  4191. -> std::string {
  4192. return detail::vformat(loc, fmt, args);
  4193. }
  4194. template <typename Locale, typename... T,
  4195. FMT_ENABLE_IF(detail::is_locale<Locale>::value)>
  4196. inline auto format(const Locale& loc, format_string<T...> fmt, T&&... args)
  4197. -> std::string {
  4198. return fmt::vformat(loc, string_view(fmt), fmt::make_format_args(args...));
  4199. }
  4200. template <typename OutputIt, typename Locale,
  4201. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value&&
  4202. detail::is_locale<Locale>::value)>
  4203. auto vformat_to(OutputIt out, const Locale& loc, string_view fmt,
  4204. format_args args) -> OutputIt {
  4205. using detail::get_buffer;
  4206. auto&& buf = get_buffer<char>(out);
  4207. detail::vformat_to(buf, fmt, args, detail::locale_ref(loc));
  4208. return detail::get_iterator(buf, out);
  4209. }
  4210. template <typename OutputIt, typename Locale, typename... T,
  4211. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value&&
  4212. detail::is_locale<Locale>::value)>
  4213. FMT_INLINE auto format_to(OutputIt out, const Locale& loc,
  4214. format_string<T...> fmt, T&&... args) -> OutputIt {
  4215. return vformat_to(out, loc, fmt, fmt::make_format_args(args...));
  4216. }
  4217. template <typename Locale, typename... T,
  4218. FMT_ENABLE_IF(detail::is_locale<Locale>::value)>
  4219. FMT_NODISCARD FMT_INLINE auto formatted_size(const Locale& loc,
  4220. format_string<T...> fmt,
  4221. T&&... args) -> size_t {
  4222. auto buf = detail::counting_buffer<>();
  4223. detail::vformat_to<char>(buf, fmt, fmt::make_format_args(args...),
  4224. detail::locale_ref(loc));
  4225. return buf.count();
  4226. }
  4227. FMT_END_EXPORT
  4228. template <typename T, typename Char>
  4229. template <typename FormatContext>
  4230. FMT_CONSTEXPR FMT_INLINE auto
  4231. formatter<T, Char,
  4232. enable_if_t<detail::type_constant<T, Char>::value !=
  4233. detail::type::custom_type>>::format(const T& val,
  4234. FormatContext& ctx)
  4235. const -> decltype(ctx.out()) {
  4236. if (specs_.width_ref.kind != detail::arg_id_kind::none ||
  4237. specs_.precision_ref.kind != detail::arg_id_kind::none) {
  4238. auto specs = specs_;
  4239. detail::handle_dynamic_spec<detail::width_checker>(specs.width,
  4240. specs.width_ref, ctx);
  4241. detail::handle_dynamic_spec<detail::precision_checker>(
  4242. specs.precision, specs.precision_ref, ctx);
  4243. return detail::write<Char>(ctx.out(), val, specs, ctx.locale());
  4244. }
  4245. return detail::write<Char>(ctx.out(), val, specs_, ctx.locale());
  4246. }
  4247. FMT_END_NAMESPACE
  4248. #ifdef FMT_HEADER_ONLY
  4249. # define FMT_FUNC inline
  4250. # include "format-inl.h"
  4251. #else
  4252. # define FMT_FUNC
  4253. #endif
  4254. #endif // FMT_FORMAT_H_