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