| 1 |
// Formatting library for C++ - the base API for char/UTF-8 |
| 2 |
// |
| 3 |
// Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors |
| 4 |
// All rights reserved. |
| 5 |
// |
| 6 |
// For the license information refer to format.h. |
| 7 |
|
| 8 |
#ifndef FMT_BASE_H_ |
| 9 |
#define FMT_BASE_H_ |
| 10 |
|
| 11 |
#if defined(FMT_IMPORT_STD) && !defined(FMT_MODULE) |
| 12 |
# define FMT_MODULE |
| 13 |
#endif |
| 14 |
|
| 15 |
#ifndef FMT_MODULE |
| 16 |
# include <limits.h> // CHAR_BIT |
| 17 |
# include <stdio.h> // FILE |
| 18 |
# include <string.h> // memcmp |
| 19 |
|
| 20 |
# include <type_traits> // std::enable_if |
| 21 |
#endif |
| 22 |
|
| 23 |
// The fmt library version in the form major * 10000 + minor * 100 + patch. |
| 24 |
#define FMT_VERSION 120200 |
| 25 |
|
| 26 |
// Detect compiler versions. |
| 27 |
#if defined(__clang__) && !defined(__ibmxl__) |
| 28 |
# define FMT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__) |
| 29 |
#else |
| 30 |
# define FMT_CLANG_VERSION 0 |
| 31 |
#endif |
| 32 |
#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) |
| 33 |
# define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) |
| 34 |
#else |
| 35 |
# define FMT_GCC_VERSION 0 |
| 36 |
#endif |
| 37 |
#if defined(__ICL) |
| 38 |
# define FMT_ICC_VERSION __ICL |
| 39 |
#elif defined(__INTEL_COMPILER) |
| 40 |
# define FMT_ICC_VERSION __INTEL_COMPILER |
| 41 |
#else |
| 42 |
# define FMT_ICC_VERSION 0 |
| 43 |
#endif |
| 44 |
#if defined(_MSC_VER) |
| 45 |
# define FMT_MSC_VERSION _MSC_VER |
| 46 |
#else |
| 47 |
# define FMT_MSC_VERSION 0 |
| 48 |
#endif |
| 49 |
|
| 50 |
// Detect standard library versions. |
| 51 |
#ifdef _GLIBCXX_RELEASE |
| 52 |
# define FMT_GLIBCXX_RELEASE _GLIBCXX_RELEASE |
| 53 |
#else |
| 54 |
# define FMT_GLIBCXX_RELEASE 0 |
| 55 |
#endif |
| 56 |
#ifdef _LIBCPP_VERSION |
| 57 |
# define FMT_LIBCPP_VERSION _LIBCPP_VERSION |
| 58 |
#else |
| 59 |
# define FMT_LIBCPP_VERSION 0 |
| 60 |
#endif |
| 61 |
|
| 62 |
#ifdef _MSVC_LANG |
| 63 |
# define FMT_CPLUSPLUS _MSVC_LANG |
| 64 |
#else |
| 65 |
# define FMT_CPLUSPLUS __cplusplus |
| 66 |
#endif |
| 67 |
|
| 68 |
// Detect __has_*. |
| 69 |
#ifdef __has_feature |
| 70 |
# define FMT_HAS_FEATURE(x) __has_feature(x) |
| 71 |
#else |
| 72 |
# define FMT_HAS_FEATURE(x) 0 |
| 73 |
#endif |
| 74 |
#ifdef __has_include |
| 75 |
# define FMT_HAS_INCLUDE(x) __has_include(x) |
| 76 |
#else |
| 77 |
# define FMT_HAS_INCLUDE(x) 0 |
| 78 |
#endif |
| 79 |
#ifdef __has_builtin |
| 80 |
# define FMT_HAS_BUILTIN(x) __has_builtin(x) |
| 81 |
#else |
| 82 |
# define FMT_HAS_BUILTIN(x) 0 |
| 83 |
#endif |
| 84 |
#ifdef __has_cpp_attribute |
| 85 |
# define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) |
| 86 |
#else |
| 87 |
# define FMT_HAS_CPP_ATTRIBUTE(x) 0 |
| 88 |
#endif |
| 89 |
|
| 90 |
#define FMT_HAS_CPP14_ATTRIBUTE(attribute) \ |
| 91 |
(FMT_CPLUSPLUS >= 201402L && FMT_HAS_CPP_ATTRIBUTE(attribute)) |
| 92 |
|
| 93 |
#define FMT_HAS_CPP17_ATTRIBUTE(attribute) \ |
| 94 |
(FMT_CPLUSPLUS >= 201703L && FMT_HAS_CPP_ATTRIBUTE(attribute)) |
| 95 |
|
| 96 |
// Detect C++14 relaxed constexpr. |
| 97 |
#ifdef FMT_USE_CONSTEXPR |
| 98 |
// Use the provided definition. |
| 99 |
#elif FMT_GCC_VERSION >= 702 && FMT_CPLUSPLUS >= 201402L |
| 100 |
// GCC only allows constexpr member functions in non-literal types since 7.2: |
| 101 |
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66297. |
| 102 |
# define FMT_USE_CONSTEXPR 1 |
| 103 |
#elif FMT_ICC_VERSION |
| 104 |
# define FMT_USE_CONSTEXPR 0 // https://github.com/fmtlib/fmt/issues/1628 |
| 105 |
#elif FMT_HAS_FEATURE(cxx_relaxed_constexpr) || FMT_MSC_VERSION >= 1912 |
| 106 |
# define FMT_USE_CONSTEXPR 1 |
| 107 |
#else |
| 108 |
# define FMT_USE_CONSTEXPR 0 |
| 109 |
#endif |
| 110 |
#if FMT_USE_CONSTEXPR |
| 111 |
# define FMT_CONSTEXPR constexpr |
| 112 |
#else |
| 113 |
# define FMT_CONSTEXPR |
| 114 |
#endif |
| 115 |
|
| 116 |
// Detect consteval, C++20 constexpr extensions and std::is_constant_evaluated. |
| 117 |
#ifdef FMT_USE_CONSTEVAL |
| 118 |
// Use the provided definition. |
| 119 |
#elif !defined(__cpp_lib_is_constant_evaluated) |
| 120 |
# define FMT_USE_CONSTEVAL 0 |
| 121 |
#elif FMT_CPLUSPLUS < 201709L |
| 122 |
# define FMT_USE_CONSTEVAL 0 |
| 123 |
#elif FMT_GLIBCXX_RELEASE && FMT_GLIBCXX_RELEASE < 10 |
| 124 |
# define FMT_USE_CONSTEVAL 0 |
| 125 |
#elif FMT_LIBCPP_VERSION && FMT_LIBCPP_VERSION < 10000 |
| 126 |
# define FMT_USE_CONSTEVAL 0 |
| 127 |
#elif defined(__apple_build_version__) && __apple_build_version__ < 14000029L |
| 128 |
# define FMT_USE_CONSTEVAL 0 // consteval is broken in Apple clang < 14. |
| 129 |
#elif FMT_MSC_VERSION && FMT_MSC_VERSION < 1940 |
| 130 |
# define FMT_USE_CONSTEVAL 0 // consteval is broken in some MSVC2022 versions. |
| 131 |
#elif defined(__cpp_consteval) |
| 132 |
# define FMT_USE_CONSTEVAL 1 |
| 133 |
#elif FMT_GCC_VERSION >= 1002 || FMT_CLANG_VERSION >= 1101 |
| 134 |
# define FMT_USE_CONSTEVAL 1 |
| 135 |
#else |
| 136 |
# define FMT_USE_CONSTEVAL 0 |
| 137 |
#endif |
| 138 |
#if FMT_USE_CONSTEVAL |
| 139 |
# define FMT_CONSTEVAL consteval |
| 140 |
# define FMT_CONSTEXPR20 constexpr |
| 141 |
#else |
| 142 |
# define FMT_CONSTEVAL |
| 143 |
# define FMT_CONSTEXPR20 |
| 144 |
#endif |
| 145 |
|
| 146 |
// Check if exceptions are disabled. |
| 147 |
#ifdef FMT_USE_EXCEPTIONS |
| 148 |
// Use the provided definition. |
| 149 |
#elif defined(__GNUC__) && !defined(__EXCEPTIONS) |
| 150 |
# define FMT_USE_EXCEPTIONS 0 |
| 151 |
#elif defined(__clang__) && !defined(__cpp_exceptions) |
| 152 |
# define FMT_USE_EXCEPTIONS 0 |
| 153 |
#elif FMT_MSC_VERSION && !_HAS_EXCEPTIONS |
| 154 |
# define FMT_USE_EXCEPTIONS 0 |
| 155 |
#else |
| 156 |
# define FMT_USE_EXCEPTIONS 1 |
| 157 |
#endif |
| 158 |
#if FMT_USE_EXCEPTIONS |
| 159 |
# define FMT_TRY try |
| 160 |
# define FMT_CATCH(x) catch (x) |
| 161 |
#else |
| 162 |
# define FMT_TRY if (true) |
| 163 |
# define FMT_CATCH(x) if (false) |
| 164 |
#endif |
| 165 |
|
| 166 |
#ifdef FMT_NO_UNIQUE_ADDRESS |
| 167 |
// Use the provided definition. |
| 168 |
#elif FMT_CPLUSPLUS < 202002L |
| 169 |
// Not supported. |
| 170 |
#elif FMT_HAS_CPP_ATTRIBUTE(no_unique_address) |
| 171 |
# define FMT_NO_UNIQUE_ADDRESS [[no_unique_address]] |
| 172 |
// VS2019 v16.10 and later except clang-cl (https://reviews.llvm.org/D110485). |
| 173 |
#elif FMT_MSC_VERSION >= 1929 && !FMT_CLANG_VERSION |
| 174 |
# define FMT_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]] |
| 175 |
#endif |
| 176 |
#ifndef FMT_NO_UNIQUE_ADDRESS |
| 177 |
# define FMT_NO_UNIQUE_ADDRESS |
| 178 |
#endif |
| 179 |
|
| 180 |
#if FMT_HAS_CPP17_ATTRIBUTE(fallthrough) |
| 181 |
# define FMT_FALLTHROUGH [[fallthrough]] |
| 182 |
#elif defined(__clang__) |
| 183 |
# define FMT_FALLTHROUGH [[clang::fallthrough]] |
| 184 |
#elif FMT_GCC_VERSION >= 700 && \ |
| 185 |
(!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 520) |
| 186 |
# define FMT_FALLTHROUGH [[gnu::fallthrough]] |
| 187 |
#else |
| 188 |
# define FMT_FALLTHROUGH |
| 189 |
#endif |
| 190 |
|
| 191 |
// Disable [[noreturn]] on MSVC/NVCC because of bogus unreachable code warnings. |
| 192 |
#if FMT_HAS_CPP_ATTRIBUTE(noreturn) && !FMT_MSC_VERSION && !defined(__NVCC__) |
| 193 |
# define FMT_NORETURN [[noreturn]] |
| 194 |
#else |
| 195 |
# define FMT_NORETURN |
| 196 |
#endif |
| 197 |
|
| 198 |
#ifdef FMT_NODISCARD |
| 199 |
// Use the provided definition. |
| 200 |
#elif FMT_HAS_CPP17_ATTRIBUTE(nodiscard) |
| 201 |
# define FMT_NODISCARD [[nodiscard]] |
| 202 |
#else |
| 203 |
# define FMT_NODISCARD |
| 204 |
#endif |
| 205 |
|
| 206 |
#if FMT_GCC_VERSION || FMT_CLANG_VERSION |
| 207 |
# define FMT_VISIBILITY(value) __attribute__((visibility(value))) |
| 208 |
#else |
| 209 |
# define FMT_VISIBILITY(value) |
| 210 |
#endif |
| 211 |
|
| 212 |
// Detect pragmas. |
| 213 |
#define FMT_PRAGMA_IMPL(x) _Pragma(#x) |
| 214 |
#if FMT_GCC_VERSION >= 504 && !defined(__NVCOMPILER) |
| 215 |
// Workaround a _Pragma bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59884 |
| 216 |
// and an nvhpc warning: https://github.com/fmtlib/fmt/pull/2582. |
| 217 |
# define FMT_PRAGMA_GCC(x) FMT_PRAGMA_IMPL(GCC x) |
| 218 |
#else |
| 219 |
# define FMT_PRAGMA_GCC(x) |
| 220 |
#endif |
| 221 |
#if FMT_CLANG_VERSION |
| 222 |
# define FMT_PRAGMA_CLANG(x) FMT_PRAGMA_IMPL(clang x) |
| 223 |
#else |
| 224 |
# define FMT_PRAGMA_CLANG(x) |
| 225 |
#endif |
| 226 |
|
| 227 |
#ifndef FMT_USE_OPTIMIZE_PRAGMA |
| 228 |
# define FMT_USE_OPTIMIZE_PRAGMA 1 |
| 229 |
#endif |
| 230 |
|
| 231 |
// Enable minimal optimizations for more compact code in debug mode. |
| 232 |
FMT_PRAGMA_GCC(push_options) |
| 233 |
#if FMT_USE_OPTIMIZE_PRAGMA && !defined(__OPTIMIZE__) && \ |
| 234 |
!defined(__CUDACC__) && !defined(FMT_MODULE) |
| 235 |
FMT_PRAGMA_GCC(optimize("Og")) |
| 236 |
#endif |
| 237 |
|
| 238 |
#ifdef FMT_DEPRECATED |
| 239 |
// Use the provided definition. |
| 240 |
#elif FMT_HAS_CPP14_ATTRIBUTE(deprecated) |
| 241 |
# define FMT_DEPRECATED [[deprecated]] |
| 242 |
#else |
| 243 |
# define FMT_DEPRECATED /* deprecated */ |
| 244 |
#endif |
| 245 |
|
| 246 |
#ifdef FMT_ALWAYS_INLINE |
| 247 |
// Use the provided definition. |
| 248 |
#elif FMT_GCC_VERSION || FMT_CLANG_VERSION |
| 249 |
# define FMT_ALWAYS_INLINE inline __attribute__((always_inline)) |
| 250 |
#else |
| 251 |
# define FMT_ALWAYS_INLINE inline |
| 252 |
#endif |
| 253 |
// A version of FMT_ALWAYS_INLINE to prevent code bloat in debug mode. |
| 254 |
#ifdef NDEBUG |
| 255 |
# define FMT_INLINE FMT_ALWAYS_INLINE |
| 256 |
#else |
| 257 |
# define FMT_INLINE inline |
| 258 |
#endif |
| 259 |
|
| 260 |
#ifndef FMT_BEGIN_NAMESPACE |
| 261 |
# define FMT_BEGIN_NAMESPACE \ |
| 262 |
namespace fmt { \ |
| 263 |
inline namespace v12 { |
| 264 |
# define FMT_END_NAMESPACE \ |
| 265 |
} \ |
| 266 |
} |
| 267 |
#endif |
| 268 |
|
| 269 |
#ifndef FMT_EXPORT |
| 270 |
# define FMT_EXPORT |
| 271 |
# define FMT_BEGIN_EXPORT |
| 272 |
# define FMT_END_EXPORT |
| 273 |
#endif |
| 274 |
|
| 275 |
#ifdef _WIN32 |
| 276 |
# define FMT_WIN32 1 |
| 277 |
#else |
| 278 |
# define FMT_WIN32 0 |
| 279 |
#endif |
| 280 |
|
| 281 |
#if !defined(FMT_HEADER_ONLY) && FMT_WIN32 |
| 282 |
# if defined(FMT_LIB_EXPORT) |
| 283 |
# define FMT_API __declspec(dllexport) |
| 284 |
# elif defined(FMT_SHARED) |
| 285 |
# define FMT_API __declspec(dllimport) |
| 286 |
# endif |
| 287 |
#elif defined(FMT_LIB_EXPORT) || defined(FMT_SHARED) |
| 288 |
# define FMT_API FMT_VISIBILITY("default") |
| 289 |
#endif |
| 290 |
#ifndef FMT_API |
| 291 |
# define FMT_API |
| 292 |
#endif |
| 293 |
|
| 294 |
#ifndef FMT_OPTIMIZE_SIZE |
| 295 |
# define FMT_OPTIMIZE_SIZE 0 |
| 296 |
#endif |
| 297 |
|
| 298 |
// FMT_BUILTIN_TYPE=0 may result in smaller library size at the cost of higher |
| 299 |
// per-call binary size by passing built-in types through the extension API. |
| 300 |
#ifndef FMT_BUILTIN_TYPES |
| 301 |
# define FMT_BUILTIN_TYPES 1 |
| 302 |
#endif |
| 303 |
|
| 304 |
#define FMT_APPLY_VARIADIC(expr) \ |
| 305 |
using unused = int[]; \ |
| 306 |
(void)unused { 0, (expr, 0)... } |
| 307 |
|
| 308 |
FMT_BEGIN_NAMESPACE |
| 309 |
|
| 310 |
// Implementations of enable_if_t and other metafunctions for older systems. |
| 311 |
template <bool B, typename T = void> |
| 312 |
using enable_if_t = typename std::enable_if<B, T>::type; |
| 313 |
template <bool B, typename T, typename F> |
| 314 |
using conditional_t = typename std::conditional<B, T, F>::type; |
| 315 |
template <bool B> using bool_constant = std::integral_constant<bool, B>; |
| 316 |
template <typename T> |
| 317 |
using remove_reference_t = typename std::remove_reference<T>::type; |
| 318 |
template <typename T> |
| 319 |
using remove_const_t = typename std::remove_const<T>::type; |
| 320 |
template <typename T> |
| 321 |
using remove_cvref_t = typename std::remove_cv<remove_reference_t<T>>::type; |
| 322 |
template <typename T> |
| 323 |
using make_unsigned_t = typename std::make_unsigned<T>::type; |
| 324 |
template <typename T> |
| 325 |
using underlying_t = typename std::underlying_type<T>::type; |
| 326 |
template <typename T> using decay_t = typename std::decay<T>::type; |
| 327 |
using nullptr_t = decltype(nullptr); |
| 328 |
|
| 329 |
using ullong = unsigned long long; |
| 330 |
|
| 331 |
#if (FMT_GCC_VERSION && FMT_GCC_VERSION < 500) || FMT_MSC_VERSION |
| 332 |
// A workaround for gcc 4.9 & MSVC v141 to make void_t work in a SFINAE context. |
| 333 |
template <typename...> struct void_t_impl { |
| 334 |
using type = void; |
| 335 |
}; |
| 336 |
template <typename... T> using void_t = typename void_t_impl<T...>::type; |
| 337 |
#else |
| 338 |
template <typename...> using void_t = void; |
| 339 |
#endif |
| 340 |
|
| 341 |
struct monostate { |
| 342 |
constexpr monostate() {} |
| 343 |
}; |
| 344 |
|
| 345 |
// An enable_if helper to be used in template parameters which results in much |
| 346 |
// shorter symbols: https://godbolt.org/z/sWw4vP. Extra parentheses are needed |
| 347 |
// to workaround a bug in MSVC 2019 (see #1140 and #1186). |
| 348 |
#ifdef FMT_DOC |
| 349 |
# define FMT_ENABLE_IF(...) |
| 350 |
#else |
| 351 |
# define FMT_ENABLE_IF(...) fmt::enable_if_t<(__VA_ARGS__), int> = 0 |
| 352 |
#endif |
| 353 |
|
| 354 |
template <typename T> constexpr auto min_of(T a, T b) -> T { |
| 355 |
return a < b ? a : b; |
| 356 |
} |
| 357 |
template <typename T> constexpr auto max_of(T a, T b) -> T { |
| 358 |
return a > b ? a : b; |
| 359 |
} |
| 360 |
|
| 361 |
FMT_NORETURN FMT_API void assert_fail(const char* file, int line, |
| 362 |
const char* message); |
| 363 |
|
| 364 |
namespace detail { |
| 365 |
// Suppresses "unused variable" warnings with the method described in |
| 366 |
// https://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/. |
| 367 |
// (void)var does not work on many Intel compilers. |
| 368 |
template <typename... T> FMT_CONSTEXPR void ignore_unused(const T&...) {} |
| 369 |
|
| 370 |
constexpr auto is_constant_evaluated(bool default_value = false) noexcept |
| 371 |
-> bool { |
| 372 |
// Workaround for incompatibility between clang 14 and libstdc++ consteval-based |
| 373 |
// std::is_constant_evaluated: https://github.com/fmtlib/fmt/issues/3247. |
| 374 |
#if FMT_CPLUSPLUS >= 202002L && FMT_GLIBCXX_RELEASE >= 12 && \ |
| 375 |
(FMT_CLANG_VERSION >= 1400 && FMT_CLANG_VERSION < 1500) |
| 376 |
ignore_unused(default_value); |
| 377 |
return __builtin_is_constant_evaluated(); |
| 378 |
#elif defined(__cpp_lib_is_constant_evaluated) |
| 379 |
ignore_unused(default_value); |
| 380 |
return std::is_constant_evaluated(); |
| 381 |
#else |
| 382 |
return default_value; |
| 383 |
#endif |
| 384 |
} |
| 385 |
|
| 386 |
#ifdef FMT_ASSERT |
| 387 |
// Use the provided definition. |
| 388 |
#elif defined(NDEBUG) |
| 389 |
// FMT_ASSERT is not empty to avoid -Wempty-body. |
| 390 |
# define FMT_ASSERT(condition, message) \ |
| 391 |
fmt::detail::ignore_unused((condition), (message)) |
| 392 |
#else |
| 393 |
# define FMT_ASSERT(condition, message) \ |
| 394 |
((condition) /* void() fails with -Winvalid-constexpr on clang 4.0.1 */ \ |
| 395 |
? (void)0 \ |
| 396 |
: ::fmt::assert_fail(__FILE__, __LINE__, (message))) |
| 397 |
#endif |
| 398 |
|
| 399 |
#ifdef FMT_USE_INT128 |
| 400 |
// Use the provided definition. |
| 401 |
#elif defined(__SIZEOF_INT128__) && !defined(__NVCC__) && \ |
| 402 |
!(FMT_CLANG_VERSION && FMT_MSC_VERSION) |
| 403 |
# define FMT_USE_INT128 1 |
| 404 |
using native_int128 = __int128_t; |
| 405 |
using native_uint128 = __uint128_t; |
| 406 |
inline auto map(native_int128 x) -> native_int128 { return x; } |
| 407 |
inline auto map(native_uint128 x) -> native_uint128 { return x; } |
| 408 |
#else |
| 409 |
# define FMT_USE_INT128 0 |
| 410 |
#endif |
| 411 |
#if !FMT_USE_INT128 |
| 412 |
// Fallbacks to reduce conditional compilation and SFINAE. |
| 413 |
enum class native_int128 {}; |
| 414 |
enum class native_uint128 {}; |
| 415 |
inline auto map(native_int128) -> monostate { return {}; } |
| 416 |
inline auto map(native_uint128) -> monostate { return {}; } |
| 417 |
#endif |
| 418 |
|
| 419 |
// Casts a nonnegative integer to unsigned. |
| 420 |
template <typename Int> |
| 421 |
FMT_CONSTEXPR auto to_unsigned(Int value) -> make_unsigned_t<Int> { |
| 422 |
FMT_ASSERT(std::is_unsigned<Int>::value || value >= 0, "negative value"); |
| 423 |
return static_cast<make_unsigned_t<Int>>(value); |
| 424 |
} |
| 425 |
|
| 426 |
template <typename Char> |
| 427 |
using unsigned_char = conditional_t<sizeof(Char) == 1, unsigned char, unsigned>; |
| 428 |
|
| 429 |
// A heuristic to detect std::string and std::[experimental::]string_view. |
| 430 |
// It is mainly used to avoid dependency on <[experimental/]string_view>. |
| 431 |
template <typename T, typename Enable = void> |
| 432 |
struct is_std_string_like : std::false_type {}; |
| 433 |
template <typename T> |
| 434 |
struct is_std_string_like<T, void_t<decltype(std::declval<T>().find_first_of( |
| 435 |
typename T::value_type(), 0))>> |
| 436 |
: std::is_convertible<decltype(std::declval<T>().data()), |
| 437 |
const typename T::value_type*> {}; |
| 438 |
|
| 439 |
// Check if the literal encoding is UTF-8. |
| 440 |
enum { is_utf8_enabled = "\u00A7"[1] == '\xA7' }; |
| 441 |
enum { use_utf8 = !FMT_WIN32 || is_utf8_enabled }; |
| 442 |
|
| 443 |
#ifndef FMT_UNICODE |
| 444 |
# define FMT_UNICODE 1 |
| 445 |
#endif |
| 446 |
|
| 447 |
static_assert(!FMT_UNICODE || use_utf8, |
| 448 |
"Unicode support requires compiling with /utf-8"); |
| 449 |
|
| 450 |
template <typename T> constexpr auto narrow(T*) -> char* { return nullptr; } |
| 451 |
constexpr FMT_ALWAYS_INLINE auto narrow(const char* s) -> const char* { |
| 452 |
return s; |
| 453 |
} |
| 454 |
|
| 455 |
template <typename Char> |
| 456 |
FMT_CONSTEXPR auto compare(const Char* s1, const Char* s2, size_t n) -> int { |
| 457 |
if (!is_constant_evaluated() && sizeof(Char) == 1) return memcmp(s1, s2, n); |
| 458 |
for (; n != 0; ++s1, ++s2, --n) { |
| 459 |
if (*s1 < *s2) return -1; |
| 460 |
if (*s1 > *s2) return 1; |
| 461 |
} |
| 462 |
return 0; |
| 463 |
} |
| 464 |
|
| 465 |
namespace adl { |
| 466 |
using namespace std; |
| 467 |
|
| 468 |
template <typename Container> |
| 469 |
auto invoke_back_inserter() |
| 470 |
-> decltype(back_inserter(std::declval<Container&>())); |
| 471 |
} // namespace adl |
| 472 |
|
| 473 |
template <typename It, typename Enable = std::true_type> |
| 474 |
struct is_back_insert_iterator : std::false_type {}; |
| 475 |
|
| 476 |
template <typename It> |
| 477 |
struct is_back_insert_iterator< |
| 478 |
It, bool_constant<std::is_same< |
| 479 |
decltype(adl::invoke_back_inserter<typename It::container_type>()), |
| 480 |
It>::value>> : std::true_type {}; |
| 481 |
|
| 482 |
// Extracts a reference to the container from *insert_iterator. |
| 483 |
template <typename OutputIt> |
| 484 |
inline FMT_CONSTEXPR auto get_container(OutputIt it) -> |
| 485 |
typename OutputIt::container_type& { |
| 486 |
struct accessor : OutputIt { |
| 487 |
constexpr accessor(OutputIt base) : OutputIt(base) {} |
| 488 |
using OutputIt::container; |
| 489 |
}; |
| 490 |
return *accessor(it).container; |
| 491 |
} |
| 492 |
|
| 493 |
template <typename T, typename Enable = void> |
| 494 |
struct is_contiguous : std::false_type {}; |
| 495 |
template <typename T> |
| 496 |
struct is_contiguous<T, void_t<decltype(std::declval<T&>().data()), |
| 497 |
decltype(std::declval<T&>().size()), |
| 498 |
decltype(std::declval<T&>()[size_t()])>> |
| 499 |
: std::true_type {}; |
| 500 |
} // namespace detail |
| 501 |
|
| 502 |
// Parsing-related public API and forward declarations. |
| 503 |
FMT_BEGIN_EXPORT |
| 504 |
|
| 505 |
/** |
| 506 |
* An implementation of `std::basic_string_view` for pre-C++17 providing a |
| 507 |
* subset of the API. `fmt::basic_string_view` is used in the public API even |
| 508 |
* if `std::basic_string_view` is available to prevent issues when a library is |
| 509 |
* compiled with a different `-std` option than the client code (which is not |
| 510 |
* recommended). |
| 511 |
*/ |
| 512 |
template <typename Char> class basic_string_view { |
| 513 |
private: |
| 514 |
const Char* data_; |
| 515 |
size_t size_; |
| 516 |
|
| 517 |
public: |
| 518 |
using value_type = Char; |
| 519 |
using iterator = const Char*; |
| 520 |
|
| 521 |
constexpr basic_string_view() noexcept : data_(nullptr), size_(0) {} |
| 522 |
constexpr basic_string_view(const Char* s, size_t count) noexcept |
| 523 |
: data_(s), size_(count) {} |
| 524 |
|
| 525 |
#if FMT_GCC_VERSION |
| 526 |
FMT_ALWAYS_INLINE |
| 527 |
#endif |
| 528 |
FMT_CONSTEXPR basic_string_view(const Char* s) : data_(s) { |
| 529 |
#if FMT_HAS_BUILTIN(__builtin_strlen) || FMT_GCC_VERSION || FMT_CLANG_VERSION |
| 530 |
if (std::is_same<Char, char>::value && !detail::is_constant_evaluated()) { |
| 531 |
size_ = __builtin_strlen(detail::narrow(s)); // strlen is not constexpr. |
| 532 |
return; |
| 533 |
} |
| 534 |
#endif |
| 535 |
size_t len = 0; |
| 536 |
while (*s++) ++len; |
| 537 |
size_ = len; |
| 538 |
} |
| 539 |
|
| 540 |
template < |
| 541 |
typename S, |
| 542 |
FMT_ENABLE_IF(detail::is_std_string_like<S>::value&& // |
| 543 |
std::is_same<typename S::value_type, Char>::value)> |
| 544 |
constexpr basic_string_view(const S& s) noexcept |
| 545 |
: data_(s.data()), size_(s.size()) {} |
| 546 |
|
| 547 |
constexpr auto data() const noexcept -> const Char* { return data_; } |
| 548 |
constexpr auto size() const noexcept -> size_t { return size_; } |
| 549 |
|
| 550 |
constexpr auto begin() const noexcept -> iterator { return data_; } |
| 551 |
constexpr auto end() const noexcept -> iterator { return data_ + size_; } |
| 552 |
|
| 553 |
constexpr auto operator[](size_t pos) const noexcept -> const Char& { |
| 554 |
return data_[pos]; |
| 555 |
} |
| 556 |
|
| 557 |
FMT_CONSTEXPR void remove_prefix(size_t n) noexcept { |
| 558 |
data_ += n; |
| 559 |
size_ -= n; |
| 560 |
} |
| 561 |
|
| 562 |
FMT_CONSTEXPR auto starts_with(basic_string_view sv) const noexcept -> bool { |
| 563 |
return size_ >= sv.size_ && detail::compare(data_, sv.data_, sv.size_) == 0; |
| 564 |
} |
| 565 |
FMT_CONSTEXPR auto starts_with(Char c) const noexcept -> bool { |
| 566 |
return size_ >= 1 && *data_ == c; |
| 567 |
} |
| 568 |
FMT_CONSTEXPR auto starts_with(const Char* s) const -> bool { |
| 569 |
return starts_with(basic_string_view(s)); |
| 570 |
} |
| 571 |
|
| 572 |
FMT_CONSTEXPR auto compare(basic_string_view other) const -> int { |
| 573 |
int cmp = detail::compare(data_, other.data_, min_of(size_, other.size_)); |
| 574 |
if (cmp != 0) return cmp; |
| 575 |
return size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1); |
| 576 |
} |
| 577 |
|
| 578 |
FMT_CONSTEXPR friend auto operator==(basic_string_view lhs, |
| 579 |
basic_string_view rhs) -> bool { |
| 580 |
return lhs.compare(rhs) == 0; |
| 581 |
} |
| 582 |
friend auto operator!=(basic_string_view lhs, basic_string_view rhs) -> bool { |
| 583 |
return lhs.compare(rhs) != 0; |
| 584 |
} |
| 585 |
friend auto operator<(basic_string_view lhs, basic_string_view rhs) -> bool { |
| 586 |
return lhs.compare(rhs) < 0; |
| 587 |
} |
| 588 |
friend auto operator<=(basic_string_view lhs, basic_string_view rhs) -> bool { |
| 589 |
return lhs.compare(rhs) <= 0; |
| 590 |
} |
| 591 |
friend auto operator>(basic_string_view lhs, basic_string_view rhs) -> bool { |
| 592 |
return lhs.compare(rhs) > 0; |
| 593 |
} |
| 594 |
friend auto operator>=(basic_string_view lhs, basic_string_view rhs) -> bool { |
| 595 |
return lhs.compare(rhs) >= 0; |
| 596 |
} |
| 597 |
}; |
| 598 |
using string_view = basic_string_view<char>; |
| 599 |
|
| 600 |
template <typename T> class basic_appender; |
| 601 |
using appender = basic_appender<char>; |
| 602 |
|
| 603 |
class context; |
| 604 |
template <typename OutputIt, typename Char> class generic_context; |
| 605 |
template <typename Char> class parse_context; |
| 606 |
|
| 607 |
// Longer aliases for C++20 compatibility. |
| 608 |
template <typename Char> using basic_format_parse_context = parse_context<Char>; |
| 609 |
using format_parse_context = parse_context<char>; |
| 610 |
template <typename OutputIt, typename Char> |
| 611 |
using basic_format_context = |
| 612 |
conditional_t<std::is_same<OutputIt, appender>::value, context, |
| 613 |
generic_context<OutputIt, Char>>; |
| 614 |
using format_context = context; |
| 615 |
|
| 616 |
template <typename Char> |
| 617 |
using buffered_context = |
| 618 |
conditional_t<std::is_same<Char, char>::value, context, |
| 619 |
generic_context<basic_appender<Char>, Char>>; |
| 620 |
|
| 621 |
template <typename T> struct is_contiguous : detail::is_contiguous<T> {}; |
| 622 |
|
| 623 |
template <typename Context> class basic_format_arg; |
| 624 |
template <typename Context> class basic_format_args; |
| 625 |
|
| 626 |
// A separate type would result in shorter symbols but break ABI compatibility |
| 627 |
// between clang and gcc on ARM (#1919). |
| 628 |
using format_args = basic_format_args<context>; |
| 629 |
|
| 630 |
// A formatter for objects of type T. |
| 631 |
template <typename T, typename Char = char, typename Enable = void> |
| 632 |
struct formatter { |
| 633 |
// A deleted default constructor indicates a disabled formatter. |
| 634 |
formatter() = delete; |
| 635 |
}; |
| 636 |
|
| 637 |
template <typename T, typename Enable = void> |
| 638 |
struct locking : std::false_type {}; |
| 639 |
|
| 640 |
/// Reports a format error at compile time or, via a `format_error` exception, |
| 641 |
/// at runtime. |
| 642 |
// This function is intentionally not constexpr to give a compile-time error. |
| 643 |
FMT_NORETURN FMT_API void report_error(const char* message); |
| 644 |
|
| 645 |
enum class presentation_type : unsigned char { |
| 646 |
// Common specifiers: |
| 647 |
none = 0, |
| 648 |
debug = 1, // '?' |
| 649 |
string = 2, // 's' (string, bool) |
| 650 |
|
| 651 |
// Integral, bool and character specifiers: |
| 652 |
dec = 3, // 'd' |
| 653 |
hex, // 'x' or 'X' |
| 654 |
oct, // 'o' |
| 655 |
bin, // 'b' or 'B' |
| 656 |
chr, // 'c' |
| 657 |
|
| 658 |
// String and pointer specifiers: |
| 659 |
pointer = 3, // 'p' |
| 660 |
|
| 661 |
// Floating-point specifiers: |
| 662 |
exp = 1, // 'e' or 'E' (1 since there is no FP debug presentation) |
| 663 |
fixed, // 'f' or 'F' |
| 664 |
general, // 'g' or 'G' |
| 665 |
hexfloat // 'a' or 'A' |
| 666 |
}; |
| 667 |
|
| 668 |
enum class align { none, left, right, center, numeric }; |
| 669 |
enum class sign { none, minus, plus, space }; |
| 670 |
enum class arg_id_kind { none, index, name }; |
| 671 |
|
| 672 |
// Basic format specifiers for built-in and string types. |
| 673 |
class basic_specs { |
| 674 |
private: |
| 675 |
// Data is arranged as follows: |
| 676 |
// |
| 677 |
// 0 1 2 3 |
| 678 |
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 679 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 680 |
// |type |align| w | p | s |u|#|L| f | unused | |
| 681 |
// +-----+-----+---+---+---+-+-+-+-----+---------------------------+ |
| 682 |
// |
| 683 |
// w - dynamic width info |
| 684 |
// p - dynamic precision info |
| 685 |
// s - sign |
| 686 |
// u - uppercase (e.g. 'X' for 'x') |
| 687 |
// # - alternate form ('#') |
| 688 |
// L - localized |
| 689 |
// f - fill size |
| 690 |
// |
| 691 |
// Bitfields are not used because of compiler bugs such as gcc bug 61414. |
| 692 |
enum : unsigned { |
| 693 |
type_mask = 0x00007, |
| 694 |
align_mask = 0x00038, |
| 695 |
width_mask = 0x000C0, |
| 696 |
precision_mask = 0x00300, |
| 697 |
sign_mask = 0x00C00, |
| 698 |
uppercase_mask = 0x01000, |
| 699 |
alternate_mask = 0x02000, |
| 700 |
localized_mask = 0x04000, |
| 701 |
fill_size_mask = 0x38000, |
| 702 |
|
| 703 |
align_shift = 3, |
| 704 |
width_shift = 6, |
| 705 |
precision_shift = 8, |
| 706 |
sign_shift = 10, |
| 707 |
fill_size_shift = 15, |
| 708 |
|
| 709 |
max_fill_size = 4 |
| 710 |
}; |
| 711 |
|
| 712 |
unsigned data_ = 1 << fill_size_shift; |
| 713 |
static_assert(sizeof(basic_specs::data_) * CHAR_BIT >= 18, ""); |
| 714 |
|
| 715 |
// Character (code unit) type is erased to prevent template bloat. |
| 716 |
char fill_data_[max_fill_size] = {' '}; |
| 717 |
|
| 718 |
FMT_CONSTEXPR void set_fill_size(size_t size) { |
| 719 |
data_ = (data_ & ~fill_size_mask) | (unsigned(size) << fill_size_shift); |
| 720 |
} |
| 721 |
|
| 722 |
public: |
| 723 |
constexpr auto type() const -> presentation_type { |
| 724 |
return static_cast<presentation_type>(data_ & type_mask); |
| 725 |
} |
| 726 |
FMT_CONSTEXPR void set_type(presentation_type t) { |
| 727 |
data_ = (data_ & ~type_mask) | unsigned(t); |
| 728 |
} |
| 729 |
|
| 730 |
constexpr auto align() const -> align { |
| 731 |
return static_cast<fmt::align>((data_ & align_mask) >> align_shift); |
| 732 |
} |
| 733 |
FMT_CONSTEXPR void set_align(fmt::align a) { |
| 734 |
data_ = (data_ & ~align_mask) | (unsigned(a) << align_shift); |
| 735 |
} |
| 736 |
|
| 737 |
constexpr auto dynamic_width() const -> arg_id_kind { |
| 738 |
return static_cast<arg_id_kind>((data_ & width_mask) >> width_shift); |
| 739 |
} |
| 740 |
FMT_CONSTEXPR void set_dynamic_width(arg_id_kind w) { |
| 741 |
data_ = (data_ & ~width_mask) | (unsigned(w) << width_shift); |
| 742 |
} |
| 743 |
|
| 744 |
FMT_CONSTEXPR auto dynamic_precision() const -> arg_id_kind { |
| 745 |
return static_cast<arg_id_kind>((data_ & precision_mask) >> |
| 746 |
precision_shift); |
| 747 |
} |
| 748 |
FMT_CONSTEXPR void set_dynamic_precision(arg_id_kind p) { |
| 749 |
data_ = (data_ & ~precision_mask) | (unsigned(p) << precision_shift); |
| 750 |
} |
| 751 |
|
| 752 |
constexpr auto dynamic() const -> bool { |
| 753 |
return (data_ & (width_mask | precision_mask)) != 0; |
| 754 |
} |
| 755 |
|
| 756 |
constexpr auto sign() const -> sign { |
| 757 |
return static_cast<fmt::sign>((data_ & sign_mask) >> sign_shift); |
| 758 |
} |
| 759 |
FMT_CONSTEXPR void set_sign(fmt::sign s) { |
| 760 |
data_ = (data_ & ~sign_mask) | (unsigned(s) << sign_shift); |
| 761 |
} |
| 762 |
|
| 763 |
constexpr auto upper() const -> bool { return (data_ & uppercase_mask) != 0; } |
| 764 |
FMT_CONSTEXPR void set_upper() { data_ |= uppercase_mask; } |
| 765 |
|
| 766 |
constexpr auto alt() const -> bool { return (data_ & alternate_mask) != 0; } |
| 767 |
FMT_CONSTEXPR void set_alt() { data_ |= alternate_mask; } |
| 768 |
FMT_CONSTEXPR void clear_alt() { data_ &= ~alternate_mask; } |
| 769 |
|
| 770 |
constexpr auto localized() const -> bool { |
| 771 |
return (data_ & localized_mask) != 0; |
| 772 |
} |
| 773 |
FMT_CONSTEXPR void set_localized() { data_ |= localized_mask; } |
| 774 |
|
| 775 |
constexpr auto fill_size() const -> size_t { |
| 776 |
return (data_ & fill_size_mask) >> fill_size_shift; |
| 777 |
} |
| 778 |
|
| 779 |
template <typename Char, FMT_ENABLE_IF(std::is_same<Char, char>::value)> |
| 780 |
constexpr auto fill() const -> const Char* { |
| 781 |
return fill_data_; |
| 782 |
} |
| 783 |
template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)> |
| 784 |
constexpr auto fill() const -> const Char* { |
| 785 |
return nullptr; |
| 786 |
} |
| 787 |
|
| 788 |
template <typename Char> constexpr auto fill_unit() const -> Char { |
| 789 |
using uchar = unsigned char; |
| 790 |
return Char(uchar(fill_data_[0]) | uchar(fill_data_[1]) << 8 | |
| 791 |
uchar(fill_data_[2]) << 16); |
| 792 |
} |
| 793 |
|
| 794 |
FMT_CONSTEXPR void set_fill(char c) { |
| 795 |
fill_data_[0] = c; |
| 796 |
set_fill_size(1); |
| 797 |
} |
| 798 |
|
| 799 |
template <typename Char> |
| 800 |
FMT_CONSTEXPR void set_fill(basic_string_view<Char> s) { |
| 801 |
auto size = s.size(); |
| 802 |
set_fill_size(size); |
| 803 |
if (size == 1) { |
| 804 |
unsigned uchar = static_cast<detail::unsigned_char<Char>>(s[0]); |
| 805 |
fill_data_[0] = char(uchar); |
| 806 |
fill_data_[1] = char(uchar >> 8); |
| 807 |
fill_data_[2] = char(uchar >> 16); |
| 808 |
return; |
| 809 |
} |
| 810 |
FMT_ASSERT(size <= max_fill_size, "invalid fill"); |
| 811 |
for (size_t i = 0; i < size; ++i) fill_data_[i & 3] = char(s[i]); |
| 812 |
} |
| 813 |
|
| 814 |
FMT_CONSTEXPR void copy_fill_from(const basic_specs& specs) { |
| 815 |
set_fill_size(specs.fill_size()); |
| 816 |
for (size_t i = 0; i < max_fill_size; ++i) |
| 817 |
fill_data_[i] = specs.fill_data_[i]; |
| 818 |
} |
| 819 |
}; |
| 820 |
|
| 821 |
// Format specifiers for built-in and string types. |
| 822 |
struct format_specs : basic_specs { |
| 823 |
int width; |
| 824 |
int precision; |
| 825 |
|
| 826 |
constexpr format_specs() : width(0), precision(-1) {} |
| 827 |
}; |
| 828 |
|
| 829 |
/** |
| 830 |
* Parsing context consisting of a format string range being parsed and an |
| 831 |
* argument counter for automatic indexing. |
| 832 |
*/ |
| 833 |
template <typename Char = char> class parse_context { |
| 834 |
private: |
| 835 |
basic_string_view<Char> fmt_; |
| 836 |
int next_arg_id_; |
| 837 |
|
| 838 |
enum { use_constexpr_cast = !FMT_GCC_VERSION || FMT_GCC_VERSION >= 1200 }; |
| 839 |
|
| 840 |
FMT_CONSTEXPR void do_check_arg_id(int arg_id); |
| 841 |
|
| 842 |
public: |
| 843 |
using char_type = Char; |
| 844 |
using iterator = const Char*; |
| 845 |
|
| 846 |
constexpr explicit parse_context(basic_string_view<Char> fmt, |
| 847 |
int next_arg_id = 0) |
| 848 |
: fmt_(fmt), next_arg_id_(next_arg_id) {} |
| 849 |
|
| 850 |
/// Returns an iterator to the beginning of the format string range being |
| 851 |
/// parsed. |
| 852 |
constexpr auto begin() const noexcept -> iterator { return fmt_.begin(); } |
| 853 |
|
| 854 |
/// Returns an iterator past the end of the format string range being parsed. |
| 855 |
constexpr auto end() const noexcept -> iterator { return fmt_.end(); } |
| 856 |
|
| 857 |
/// Advances the begin iterator to `it`. |
| 858 |
FMT_CONSTEXPR void advance_to(iterator it) { |
| 859 |
fmt_.remove_prefix(detail::to_unsigned(it - begin())); |
| 860 |
} |
| 861 |
|
| 862 |
/// Reports an error if using the manual argument indexing; otherwise returns |
| 863 |
/// the next argument index and switches to the automatic indexing. |
| 864 |
FMT_CONSTEXPR auto next_arg_id() -> int { |
| 865 |
if (next_arg_id_ < 0) { |
| 866 |
report_error("cannot switch from manual to automatic argument indexing"); |
| 867 |
return 0; |
| 868 |
} |
| 869 |
int id = next_arg_id_++; |
| 870 |
do_check_arg_id(id); |
| 871 |
return id; |
| 872 |
} |
| 873 |
|
| 874 |
/// Reports an error if using the automatic argument indexing; otherwise |
| 875 |
/// switches to the manual indexing. |
| 876 |
FMT_CONSTEXPR void check_arg_id(int id) { |
| 877 |
if (next_arg_id_ > 0) { |
| 878 |
report_error("cannot switch from automatic to manual argument indexing"); |
| 879 |
return; |
| 880 |
} |
| 881 |
next_arg_id_ = -1; |
| 882 |
do_check_arg_id(id); |
| 883 |
} |
| 884 |
FMT_CONSTEXPR void check_arg_id(basic_string_view<Char>) { |
| 885 |
next_arg_id_ = -1; |
| 886 |
} |
| 887 |
FMT_CONSTEXPR void check_dynamic_spec(int arg_id); |
| 888 |
}; |
| 889 |
|
| 890 |
#ifndef FMT_USE_LOCALE |
| 891 |
# define FMT_USE_LOCALE (FMT_OPTIMIZE_SIZE <= 1) |
| 892 |
#endif |
| 893 |
|
| 894 |
// A type-erased reference to std::locale to avoid the heavy <locale> include. |
| 895 |
class locale_ref { |
| 896 |
#if FMT_USE_LOCALE |
| 897 |
private: |
| 898 |
const void* locale_; // A type-erased pointer to std::locale. |
| 899 |
|
| 900 |
public: |
| 901 |
constexpr locale_ref() : locale_(nullptr) {} |
| 902 |
|
| 903 |
template <typename Locale, FMT_ENABLE_IF(sizeof(Locale::collate) != 0)> |
| 904 |
locale_ref(const Locale& loc) : locale_(&loc) { |
| 905 |
// Check if std::isalpha is found via ADL to reduce the chance of misuse. |
| 906 |
detail::ignore_unused(sizeof(isalpha('x', loc))); |
| 907 |
} |
| 908 |
|
| 909 |
inline explicit operator bool() const noexcept { return locale_ != nullptr; } |
| 910 |
#else |
| 911 |
public: |
| 912 |
inline explicit operator bool() const noexcept { return false; } |
| 913 |
#endif // FMT_USE_LOCALE |
| 914 |
|
| 915 |
public: |
| 916 |
template <typename Locale> auto get() const -> Locale; |
| 917 |
}; |
| 918 |
|
| 919 |
FMT_END_EXPORT |
| 920 |
|
| 921 |
namespace detail { |
| 922 |
|
| 923 |
// Specifies if `T` is a code unit type. |
| 924 |
template <typename T> struct is_code_unit : std::false_type {}; |
| 925 |
template <> struct is_code_unit<char> : std::true_type {}; |
| 926 |
template <> struct is_code_unit<wchar_t> : std::true_type {}; |
| 927 |
template <> struct is_code_unit<char16_t> : std::true_type {}; |
| 928 |
template <> struct is_code_unit<char32_t> : std::true_type {}; |
| 929 |
#ifdef __cpp_char8_t |
| 930 |
template <> struct is_code_unit<char8_t> : bool_constant<is_utf8_enabled> {}; |
| 931 |
#endif |
| 932 |
|
| 933 |
// Constructs fmt::basic_string_view<Char> from types implicitly convertible |
| 934 |
// to it, deducing Char. Explicitly convertible types such as the ones returned |
| 935 |
// from FMT_STRING are intentionally excluded. |
| 936 |
template <typename Char, FMT_ENABLE_IF(is_code_unit<Char>::value)> |
| 937 |
constexpr auto to_string_view(const Char* s) -> basic_string_view<Char> { |
| 938 |
return s; |
| 939 |
} |
| 940 |
template <typename T, FMT_ENABLE_IF(is_std_string_like<T>::value)> |
| 941 |
constexpr auto to_string_view(const T& s) |
| 942 |
-> basic_string_view<typename T::value_type> { |
| 943 |
return s; |
| 944 |
} |
| 945 |
template <typename Char> |
| 946 |
constexpr auto to_string_view(basic_string_view<Char> s) |
| 947 |
-> basic_string_view<Char> { |
| 948 |
return s; |
| 949 |
} |
| 950 |
|
| 951 |
template <typename T, typename Enable = void> |
| 952 |
struct has_to_string_view : std::false_type {}; |
| 953 |
// detail:: is intentional since to_string_view is not an extension point. |
| 954 |
template <typename T> |
| 955 |
struct has_to_string_view< |
| 956 |
T, void_t<decltype(detail::to_string_view(std::declval<T>()))>> |
| 957 |
: std::true_type {}; |
| 958 |
|
| 959 |
/// String's character (code unit) type. detail:: is intentional to prevent ADL. |
| 960 |
template <typename S, |
| 961 |
typename V = decltype(detail::to_string_view(std::declval<S>()))> |
| 962 |
using char_t = typename V::value_type; |
| 963 |
|
| 964 |
enum class type { |
| 965 |
none_type, |
| 966 |
// Integer types should go first, |
| 967 |
int_type, |
| 968 |
uint_type, |
| 969 |
long_long_type, |
| 970 |
ulong_long_type, |
| 971 |
int128_type, |
| 972 |
uint128_type, |
| 973 |
bool_type, |
| 974 |
char_type, |
| 975 |
last_integer_type = char_type, |
| 976 |
// followed by floating-point types. |
| 977 |
float_type, |
| 978 |
double_type, |
| 979 |
long_double_type, |
| 980 |
last_numeric_type = long_double_type, |
| 981 |
cstring_type, |
| 982 |
string_type, |
| 983 |
pointer_type, |
| 984 |
custom_type |
| 985 |
}; |
| 986 |
|
| 987 |
// Maps core type T to the corresponding type enum constant. |
| 988 |
template <typename T, typename Char> |
| 989 |
struct type_constant : std::integral_constant<type, type::custom_type> {}; |
| 990 |
|
| 991 |
#define FMT_TYPE_CONSTANT(Type, constant) \ |
| 992 |
template <typename Char> \ |
| 993 |
struct type_constant<Type, Char> \ |
| 994 |
: std::integral_constant<type, type::constant> {} |
| 995 |
|
| 996 |
FMT_TYPE_CONSTANT(int, int_type); |
| 997 |
FMT_TYPE_CONSTANT(unsigned, uint_type); |
| 998 |
FMT_TYPE_CONSTANT(long long, long_long_type); |
| 999 |
FMT_TYPE_CONSTANT(ullong, ulong_long_type); |
| 1000 |
FMT_TYPE_CONSTANT(native_int128, int128_type); |
| 1001 |
FMT_TYPE_CONSTANT(native_uint128, uint128_type); |
| 1002 |
FMT_TYPE_CONSTANT(bool, bool_type); |
| 1003 |
FMT_TYPE_CONSTANT(Char, char_type); |
| 1004 |
FMT_TYPE_CONSTANT(float, float_type); |
| 1005 |
FMT_TYPE_CONSTANT(double, double_type); |
| 1006 |
FMT_TYPE_CONSTANT(long double, long_double_type); |
| 1007 |
FMT_TYPE_CONSTANT(const Char*, cstring_type); |
| 1008 |
FMT_TYPE_CONSTANT(basic_string_view<Char>, string_type); |
| 1009 |
FMT_TYPE_CONSTANT(const void*, pointer_type); |
| 1010 |
|
| 1011 |
constexpr auto is_integral_type(type t) -> bool { |
| 1012 |
return t > type::none_type && t <= type::last_integer_type; |
| 1013 |
} |
| 1014 |
constexpr auto is_arithmetic_type(type t) -> bool { |
| 1015 |
return t > type::none_type && t <= type::last_numeric_type; |
| 1016 |
} |
| 1017 |
|
| 1018 |
constexpr auto set(type rhs) -> int { return 1 << int(rhs); } |
| 1019 |
constexpr auto in(type t, int set) -> bool { |
| 1020 |
return ((set >> int(t)) & 1) != 0; |
| 1021 |
} |
| 1022 |
|
| 1023 |
// Bitsets of types. |
| 1024 |
enum { |
| 1025 |
sint_set = |
| 1026 |
set(type::int_type) | set(type::long_long_type) | set(type::int128_type), |
| 1027 |
uint_set = set(type::uint_type) | set(type::ulong_long_type) | |
| 1028 |
set(type::uint128_type), |
| 1029 |
bool_set = set(type::bool_type), |
| 1030 |
char_set = set(type::char_type), |
| 1031 |
float_set = set(type::float_type) | set(type::double_type) | |
| 1032 |
set(type::long_double_type), |
| 1033 |
string_set = set(type::string_type), |
| 1034 |
cstring_set = set(type::cstring_type), |
| 1035 |
pointer_set = set(type::pointer_type) |
| 1036 |
}; |
| 1037 |
|
| 1038 |
struct view {}; |
| 1039 |
|
| 1040 |
template <typename T, typename Enable = std::true_type> |
| 1041 |
struct is_view : std::false_type {}; |
| 1042 |
template <typename T> |
| 1043 |
struct is_view<T, bool_constant<sizeof(T) != 0>> : std::is_base_of<view, T> {}; |
| 1044 |
|
| 1045 |
// DEPRECATED! named_arg will be moved to the fmt namespace. |
| 1046 |
template <typename T, typename Char> struct named_arg; |
| 1047 |
template <typename T> struct is_named_arg : std::false_type {}; |
| 1048 |
template <typename T> struct is_static_named_arg : std::false_type {}; |
| 1049 |
|
| 1050 |
template <typename T, typename Char> |
| 1051 |
struct is_named_arg<named_arg<T, Char>> : std::true_type {}; |
| 1052 |
|
| 1053 |
template <typename T, typename Char = char> struct named_arg : view { |
| 1054 |
const Char* name; |
| 1055 |
const T& value; |
| 1056 |
|
| 1057 |
named_arg(const Char* n, const T& v) : name(n), value(v) {} |
| 1058 |
static_assert(!is_named_arg<T>::value, "nested named arguments"); |
| 1059 |
}; |
| 1060 |
|
| 1061 |
template <bool B = false> constexpr auto count() -> int { return B ? 1 : 0; } |
| 1062 |
template <bool B1, bool B2, bool... Tail> constexpr auto count() -> int { |
| 1063 |
return (B1 ? 1 : 0) + count<B2, Tail...>(); |
| 1064 |
} |
| 1065 |
|
| 1066 |
template <typename... T> constexpr auto count_named_args() -> int { |
| 1067 |
return count<is_named_arg<T>::value...>(); |
| 1068 |
} |
| 1069 |
template <typename... T> constexpr auto count_static_named_args() -> int { |
| 1070 |
return count<is_static_named_arg<T>::value...>(); |
| 1071 |
} |
| 1072 |
|
| 1073 |
template <typename Char> struct named_arg_info { |
| 1074 |
const Char* name; |
| 1075 |
int id; |
| 1076 |
}; |
| 1077 |
|
| 1078 |
// named_args is non-const to suppress a bogus -Wmaybe-uninitialized in gcc 13. |
| 1079 |
template <typename Char> |
| 1080 |
FMT_CONSTEXPR void check_for_duplicate(named_arg_info<Char>* named_args, |
| 1081 |
int named_arg_index, |
| 1082 |
basic_string_view<Char> arg_name) { |
| 1083 |
for (int i = 0; i < named_arg_index; ++i) { |
| 1084 |
if (named_args[i].name == arg_name) report_error("duplicate named arg"); |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
template <typename Char, typename T, FMT_ENABLE_IF(!is_named_arg<T>::value)> |
| 1089 |
void init_named_arg(named_arg_info<Char>*, int& arg_index, int&, const T&) { |
| 1090 |
++arg_index; |
| 1091 |
} |
| 1092 |
template <typename Char, typename T, FMT_ENABLE_IF(is_named_arg<T>::value)> |
| 1093 |
void init_named_arg(named_arg_info<Char>* named_args, int& arg_index, |
| 1094 |
int& named_arg_index, const T& arg) { |
| 1095 |
check_for_duplicate<Char>(named_args, named_arg_index, arg.name); |
| 1096 |
named_args[named_arg_index++] = {arg.name, arg_index++}; |
| 1097 |
} |
| 1098 |
|
| 1099 |
template <typename T, typename Char, |
| 1100 |
FMT_ENABLE_IF(!is_static_named_arg<T>::value)> |
| 1101 |
FMT_CONSTEXPR void init_static_named_arg(named_arg_info<Char>*, int& arg_index, |
| 1102 |
int&) { |
| 1103 |
++arg_index; |
| 1104 |
} |
| 1105 |
template <typename T, typename Char, |
| 1106 |
FMT_ENABLE_IF(is_static_named_arg<T>::value)> |
| 1107 |
FMT_CONSTEXPR void init_static_named_arg(named_arg_info<Char>* named_args, |
| 1108 |
int& arg_index, int& named_arg_index) { |
| 1109 |
check_for_duplicate<Char>(named_args, named_arg_index, T::name); |
| 1110 |
named_args[named_arg_index++] = {T::name, arg_index++}; |
| 1111 |
} |
| 1112 |
|
| 1113 |
// To minimize the number of types we need to deal with, long is translated |
| 1114 |
// either to int or to long long depending on its size. |
| 1115 |
enum { long_short = sizeof(long) == sizeof(int) && FMT_BUILTIN_TYPES }; |
| 1116 |
using long_type = conditional_t<long_short, int, long long>; |
| 1117 |
using ulong_type = conditional_t<long_short, unsigned, ullong>; |
| 1118 |
|
| 1119 |
template <typename T> |
| 1120 |
using format_as_result = |
| 1121 |
remove_cvref_t<decltype(format_as(std::declval<const T&>()))>; |
| 1122 |
template <typename T> |
| 1123 |
using format_as_member_result = |
| 1124 |
remove_cvref_t<decltype(formatter<T>::format_as(std::declval<const T&>()))>; |
| 1125 |
|
| 1126 |
template <typename T, typename Enable = std::true_type> |
| 1127 |
struct use_format_as : std::false_type {}; |
| 1128 |
// format_as member is only used to avoid injection into the std namespace. |
| 1129 |
template <typename T, typename Enable = std::true_type> |
| 1130 |
struct use_format_as_member : std::false_type {}; |
| 1131 |
|
| 1132 |
// Only map owning types because mapping views can be unsafe. |
| 1133 |
template <typename T> |
| 1134 |
struct use_format_as< |
| 1135 |
T, bool_constant<std::is_arithmetic<format_as_result<T>>::value>> |
| 1136 |
: std::true_type {}; |
| 1137 |
template <typename T> |
| 1138 |
struct use_format_as_member< |
| 1139 |
T, bool_constant<std::is_arithmetic<format_as_member_result<T>>::value>> |
| 1140 |
: std::true_type {}; |
| 1141 |
|
| 1142 |
template <typename T, typename U = remove_const_t<T>> |
| 1143 |
using use_formatter = |
| 1144 |
bool_constant<(std::is_class<T>::value || std::is_enum<T>::value || |
| 1145 |
std::is_union<T>::value || std::is_array<T>::value) && |
| 1146 |
!has_to_string_view<T>::value && !is_named_arg<T>::value && |
| 1147 |
!use_format_as<T>::value && !use_format_as_member<U>::value>; |
| 1148 |
|
| 1149 |
template <typename Char, typename T, typename U = remove_const_t<T>> |
| 1150 |
auto has_formatter_impl(T* p, buffered_context<Char>* ctx = nullptr) |
| 1151 |
-> decltype(formatter<U, Char>().format(*p, *ctx), std::true_type()); |
| 1152 |
template <typename Char> auto has_formatter_impl(...) -> std::false_type; |
| 1153 |
|
| 1154 |
// T can be const-qualified to check if it is const-formattable. |
| 1155 |
template <typename T, typename Char> constexpr auto has_formatter() -> bool { |
| 1156 |
return decltype(has_formatter_impl<Char>(static_cast<T*>(nullptr)))::value; |
| 1157 |
} |
| 1158 |
|
| 1159 |
// Maps formatting argument types to natively supported types or user-defined |
| 1160 |
// types with formatters. Returns void on errors to be SFINAE-friendly. |
| 1161 |
template <typename Char> struct type_mapper { |
| 1162 |
static auto map(signed char) -> int; |
| 1163 |
static auto map(unsigned char) -> unsigned; |
| 1164 |
static auto map(short) -> int; |
| 1165 |
static auto map(unsigned short) -> unsigned; |
| 1166 |
static auto map(int) -> int; |
| 1167 |
static auto map(unsigned) -> unsigned; |
| 1168 |
static auto map(long) -> long_type; |
| 1169 |
static auto map(unsigned long) -> ulong_type; |
| 1170 |
static auto map(long long) -> long long; |
| 1171 |
static auto map(ullong) -> ullong; |
| 1172 |
static auto map(native_int128) -> native_int128; |
| 1173 |
static auto map(native_uint128) -> native_uint128; |
| 1174 |
static auto map(bool) -> bool; |
| 1175 |
|
| 1176 |
template <typename T, FMT_ENABLE_IF(is_code_unit<T>::value)> |
| 1177 |
static auto map(T) -> conditional_t< |
| 1178 |
std::is_same<T, char>::value || std::is_same<T, Char>::value, Char, void>; |
| 1179 |
|
| 1180 |
static auto map(float) -> float; |
| 1181 |
static auto map(double) -> double; |
| 1182 |
static auto map(long double) -> long double; |
| 1183 |
|
| 1184 |
static auto map(Char*) -> const Char*; |
| 1185 |
static auto map(const Char*) -> const Char*; |
| 1186 |
template <typename T, typename C = char_t<T>, |
| 1187 |
FMT_ENABLE_IF(!std::is_pointer<T>::value)> |
| 1188 |
static auto map(const T&) -> conditional_t<std::is_same<C, Char>::value, |
| 1189 |
basic_string_view<C>, void>; |
| 1190 |
|
| 1191 |
static auto map(void*) -> const void*; |
| 1192 |
static auto map(const void*) -> const void*; |
| 1193 |
static auto map(volatile void*) -> const void*; |
| 1194 |
static auto map(const volatile void*) -> const void*; |
| 1195 |
static auto map(nullptr_t) -> const void*; |
| 1196 |
template <typename T, FMT_ENABLE_IF(std::is_pointer<T>::value || |
| 1197 |
std::is_member_pointer<T>::value)> |
| 1198 |
static auto map(const T&) -> void; |
| 1199 |
|
| 1200 |
template <typename T, FMT_ENABLE_IF(use_format_as<T>::value)> |
| 1201 |
static auto map(const T& x) -> decltype(map(format_as(x))); |
| 1202 |
template <typename T, FMT_ENABLE_IF(use_format_as_member<T>::value)> |
| 1203 |
static auto map(const T& x) -> decltype(map(formatter<T>::format_as(x))); |
| 1204 |
|
| 1205 |
template <typename T, FMT_ENABLE_IF(use_formatter<T>::value)> |
| 1206 |
static auto map(T&) -> conditional_t<has_formatter<T, Char>(), T&, void>; |
| 1207 |
|
| 1208 |
template <typename T, FMT_ENABLE_IF(is_named_arg<T>::value)> |
| 1209 |
static auto map(const T& named_arg) -> decltype(map(named_arg.value)); |
| 1210 |
}; |
| 1211 |
|
| 1212 |
// detail:: is used to workaround a bug in MSVC 2017. |
| 1213 |
template <typename T, typename Char> |
| 1214 |
using mapped_t = decltype(detail::type_mapper<Char>::map(std::declval<T&>())); |
| 1215 |
|
| 1216 |
// A type constant after applying type_mapper. |
| 1217 |
template <typename T, typename Char = char> |
| 1218 |
using mapped_type_constant = type_constant<mapped_t<T, Char>, Char>; |
| 1219 |
|
| 1220 |
template <typename T, typename Context, |
| 1221 |
type TYPE = |
| 1222 |
mapped_type_constant<T, typename Context::char_type>::value> |
| 1223 |
using stored_type_constant = std::integral_constant< |
| 1224 |
type, Context::builtin_types || TYPE == type::int_type ? TYPE |
| 1225 |
: type::custom_type>; |
| 1226 |
// A parse context with extra data used only in compile-time checks. |
| 1227 |
template <typename Char> |
| 1228 |
class compile_parse_context : public parse_context<Char> { |
| 1229 |
private: |
| 1230 |
int num_args_; |
| 1231 |
const type* types_; |
| 1232 |
using base = parse_context<Char>; |
| 1233 |
|
| 1234 |
public: |
| 1235 |
constexpr explicit compile_parse_context(basic_string_view<Char> fmt, |
| 1236 |
int num_args, const type* types, |
| 1237 |
int next_arg_id = 0) |
| 1238 |
: base(fmt, next_arg_id), num_args_(num_args), types_(types) {} |
| 1239 |
|
| 1240 |
constexpr auto num_args() const -> int { return num_args_; } |
| 1241 |
constexpr auto arg_type(int id) const -> type { return types_[id]; } |
| 1242 |
|
| 1243 |
FMT_CONSTEXPR auto next_arg_id() -> int { |
| 1244 |
int id = base::next_arg_id(); |
| 1245 |
if (id >= num_args_) report_error("argument not found"); |
| 1246 |
return id; |
| 1247 |
} |
| 1248 |
|
| 1249 |
FMT_CONSTEXPR void check_arg_id(int id) { |
| 1250 |
base::check_arg_id(id); |
| 1251 |
if (id >= num_args_) report_error("argument not found"); |
| 1252 |
} |
| 1253 |
using base::check_arg_id; |
| 1254 |
|
| 1255 |
FMT_CONSTEXPR void check_dynamic_spec(int arg_id) { |
| 1256 |
if (arg_id < num_args_ && types_ && !is_integral_type(types_[arg_id])) |
| 1257 |
report_error("width/precision is not integer"); |
| 1258 |
} |
| 1259 |
}; |
| 1260 |
|
| 1261 |
// An argument reference. |
| 1262 |
template <typename Char> union arg_ref { |
| 1263 |
FMT_CONSTEXPR arg_ref(int idx = 0) : index(idx) {} |
| 1264 |
FMT_CONSTEXPR arg_ref(basic_string_view<Char> n) : name(n) {} |
| 1265 |
|
| 1266 |
int index; |
| 1267 |
basic_string_view<Char> name; |
| 1268 |
}; |
| 1269 |
|
| 1270 |
// Format specifiers with width and precision resolved at formatting rather |
| 1271 |
// than parsing time to allow reusing the same parsed specifiers with |
| 1272 |
// different sets of arguments (precompilation of format strings). |
| 1273 |
template <typename Char = char> struct dynamic_format_specs : format_specs { |
| 1274 |
arg_ref<Char> width_ref; |
| 1275 |
arg_ref<Char> precision_ref; |
| 1276 |
}; |
| 1277 |
|
| 1278 |
// Converts a character to ASCII. Returns '\0' on conversion failure. |
| 1279 |
template <typename Char, FMT_ENABLE_IF(std::is_integral<Char>::value)> |
| 1280 |
constexpr auto to_ascii(Char c) -> char { |
| 1281 |
return c <= 0xff ? char(c) : '\0'; |
| 1282 |
} |
| 1283 |
|
| 1284 |
// Returns the number of code units in a code point or 1 on error. |
| 1285 |
template <typename Char> |
| 1286 |
FMT_CONSTEXPR auto code_point_length(const Char* begin) -> int { |
| 1287 |
if FMT_CONSTEXPR20 (sizeof(Char) != 1) return 1; |
| 1288 |
auto c = static_cast<unsigned char>(*begin); |
| 1289 |
return static_cast<int>((0x3a55000000000000ull >> (2 * (c >> 3))) & 3) + 1; |
| 1290 |
} |
| 1291 |
|
| 1292 |
// Parses the range [begin, end) as an unsigned integer. This function assumes |
| 1293 |
// that the range is non-empty and the first character is a digit. |
| 1294 |
template <typename Char> |
| 1295 |
FMT_CONSTEXPR auto parse_nonnegative_int(const Char*& begin, const Char* end, |
| 1296 |
int error_value) noexcept -> int { |
| 1297 |
FMT_ASSERT(begin != end && '0' <= *begin && *begin <= '9', ""); |
| 1298 |
unsigned value = 0, prev = 0; |
| 1299 |
auto p = begin; |
| 1300 |
do { |
| 1301 |
prev = value; |
| 1302 |
value = value * 10 + unsigned(*p - '0'); |
| 1303 |
++p; |
| 1304 |
} while (p != end && '0' <= *p && *p <= '9'); |
| 1305 |
auto num_digits = p - begin; |
| 1306 |
begin = p; |
| 1307 |
int digits10 = int(sizeof(int) * CHAR_BIT * 3 / 10); |
| 1308 |
if (num_digits <= digits10) return int(value); |
| 1309 |
// Check for overflow. |
| 1310 |
unsigned max = INT_MAX; |
| 1311 |
return num_digits == digits10 + 1 && |
| 1312 |
prev * 10ull + unsigned(p[-1] - '0') <= max |
| 1313 |
? int(value) |
| 1314 |
: error_value; |
| 1315 |
} |
| 1316 |
|
| 1317 |
FMT_CONSTEXPR inline auto parse_align(char c) -> align { |
| 1318 |
switch (c) { |
| 1319 |
case '<': return align::left; |
| 1320 |
case '>': return align::right; |
| 1321 |
case '^': return align::center; |
| 1322 |
} |
| 1323 |
return align::none; |
| 1324 |
} |
| 1325 |
|
| 1326 |
template <typename Char> constexpr auto is_name_start(Char c) -> bool { |
| 1327 |
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_'; |
| 1328 |
} |
| 1329 |
|
| 1330 |
template <typename Char, typename Handler> |
| 1331 |
FMT_CONSTEXPR auto parse_arg_id(const Char* begin, const Char* end, |
| 1332 |
Handler&& handler) -> const Char* { |
| 1333 |
Char c = *begin; |
| 1334 |
if (c >= '0' && c <= '9') { |
| 1335 |
int index = 0; |
| 1336 |
if (c != '0') |
| 1337 |
index = parse_nonnegative_int(begin, end, INT_MAX); |
| 1338 |
else |
| 1339 |
++begin; |
| 1340 |
if (begin == end || (*begin != '}' && *begin != ':')) |
| 1341 |
report_error("invalid format string"); |
| 1342 |
else |
| 1343 |
handler.on_index(index); |
| 1344 |
return begin; |
| 1345 |
} |
| 1346 |
if (FMT_OPTIMIZE_SIZE > 1 || !is_name_start(c)) { |
| 1347 |
report_error("invalid format string"); |
| 1348 |
return begin; |
| 1349 |
} |
| 1350 |
auto it = begin; |
| 1351 |
do { |
| 1352 |
++it; |
| 1353 |
} while (it != end && (is_name_start(*it) || ('0' <= *it && *it <= '9'))); |
| 1354 |
handler.on_name({begin, to_unsigned(it - begin)}); |
| 1355 |
return it; |
| 1356 |
} |
| 1357 |
|
| 1358 |
template <typename Char> struct dynamic_spec_handler { |
| 1359 |
parse_context<Char>& ctx; |
| 1360 |
arg_ref<Char>& ref; |
| 1361 |
arg_id_kind& kind; |
| 1362 |
|
| 1363 |
FMT_CONSTEXPR void on_index(int id) { |
| 1364 |
ref = id; |
| 1365 |
kind = arg_id_kind::index; |
| 1366 |
ctx.check_arg_id(id); |
| 1367 |
ctx.check_dynamic_spec(id); |
| 1368 |
} |
| 1369 |
FMT_CONSTEXPR void on_name(basic_string_view<Char> id) { |
| 1370 |
ref = id; |
| 1371 |
kind = arg_id_kind::name; |
| 1372 |
ctx.check_arg_id(id); |
| 1373 |
} |
| 1374 |
}; |
| 1375 |
|
| 1376 |
template <typename Char> struct parse_dynamic_spec_result { |
| 1377 |
const Char* end; |
| 1378 |
arg_id_kind kind; |
| 1379 |
}; |
| 1380 |
|
| 1381 |
// Parses integer | "{" [arg_id] "}". |
| 1382 |
template <typename Char> |
| 1383 |
FMT_CONSTEXPR auto parse_dynamic_spec(const Char* begin, const Char* end, |
| 1384 |
int& value, arg_ref<Char>& ref, |
| 1385 |
parse_context<Char>& ctx) |
| 1386 |
-> parse_dynamic_spec_result<Char> { |
| 1387 |
FMT_ASSERT(begin != end, ""); |
| 1388 |
auto kind = arg_id_kind::none; |
| 1389 |
if ('0' <= *begin && *begin <= '9') { |
| 1390 |
int val = parse_nonnegative_int(begin, end, -1); |
| 1391 |
if (val == -1) report_error("number is too big"); |
| 1392 |
value = val; |
| 1393 |
} else { |
| 1394 |
if (*begin == '{') { |
| 1395 |
++begin; |
| 1396 |
if (begin != end) { |
| 1397 |
Char c = *begin; |
| 1398 |
if (c == '}' || c == ':') { |
| 1399 |
int id = ctx.next_arg_id(); |
| 1400 |
ref = id; |
| 1401 |
kind = arg_id_kind::index; |
| 1402 |
ctx.check_dynamic_spec(id); |
| 1403 |
} else { |
| 1404 |
begin = parse_arg_id(begin, end, |
| 1405 |
dynamic_spec_handler<Char>{ctx, ref, kind}); |
| 1406 |
} |
| 1407 |
} |
| 1408 |
if (begin != end && *begin == '}') return {++begin, kind}; |
| 1409 |
} |
| 1410 |
report_error("invalid format string"); |
| 1411 |
} |
| 1412 |
return {begin, kind}; |
| 1413 |
} |
| 1414 |
|
| 1415 |
template <typename Char> |
| 1416 |
FMT_CONSTEXPR auto parse_width(const Char* begin, const Char* end, |
| 1417 |
format_specs& specs, arg_ref<Char>& width_ref, |
| 1418 |
parse_context<Char>& ctx) -> const Char* { |
| 1419 |
auto result = parse_dynamic_spec(begin, end, specs.width, width_ref, ctx); |
| 1420 |
specs.set_dynamic_width(result.kind); |
| 1421 |
return result.end; |
| 1422 |
} |
| 1423 |
|
| 1424 |
template <typename Char> |
| 1425 |
FMT_CONSTEXPR auto parse_precision(const Char* begin, const Char* end, |
| 1426 |
format_specs& specs, |
| 1427 |
arg_ref<Char>& precision_ref, |
| 1428 |
parse_context<Char>& ctx) -> const Char* { |
| 1429 |
++begin; |
| 1430 |
if (begin == end) { |
| 1431 |
report_error("invalid precision"); |
| 1432 |
return begin; |
| 1433 |
} |
| 1434 |
auto result = |
| 1435 |
parse_dynamic_spec(begin, end, specs.precision, precision_ref, ctx); |
| 1436 |
specs.set_dynamic_precision(result.kind); |
| 1437 |
return result.end; |
| 1438 |
} |
| 1439 |
|
| 1440 |
enum class state { start, align, sign, hash, zero, width, precision, locale }; |
| 1441 |
|
| 1442 |
// Parses standard format specifiers. |
| 1443 |
template <typename Char> |
| 1444 |
FMT_CONSTEXPR auto parse_format_specs(const Char* begin, const Char* end, |
| 1445 |
dynamic_format_specs<Char>& specs, |
| 1446 |
parse_context<Char>& ctx, type arg_type) |
| 1447 |
-> const Char* { |
| 1448 |
auto c = '\0'; |
| 1449 |
if (end - begin > 1) { |
| 1450 |
auto next = to_ascii(begin[1]); |
| 1451 |
c = parse_align(next) == align::none ? to_ascii(*begin) : '\0'; |
| 1452 |
} else { |
| 1453 |
if (begin == end) return begin; |
| 1454 |
c = to_ascii(*begin); |
| 1455 |
} |
| 1456 |
|
| 1457 |
struct { |
| 1458 |
state current_state = state::start; |
| 1459 |
FMT_CONSTEXPR void operator()(state s, bool valid = true) { |
| 1460 |
if (current_state >= s || !valid) |
| 1461 |
report_error("invalid format specifier"); |
| 1462 |
current_state = s; |
| 1463 |
} |
| 1464 |
} enter_state; |
| 1465 |
|
| 1466 |
using pres = presentation_type; |
| 1467 |
constexpr auto integral_set = sint_set | uint_set | bool_set | char_set; |
| 1468 |
struct { |
| 1469 |
const Char*& begin; |
| 1470 |
format_specs& specs; |
| 1471 |
type arg_type; |
| 1472 |
|
| 1473 |
FMT_CONSTEXPR auto operator()(pres pres_type, int set) -> const Char* { |
| 1474 |
if (!in(arg_type, set)) report_error("invalid format specifier"); |
| 1475 |
specs.set_type(pres_type); |
| 1476 |
return begin + 1; |
| 1477 |
} |
| 1478 |
} parse_presentation_type{begin, specs, arg_type}; |
| 1479 |
|
| 1480 |
for (;;) { |
| 1481 |
switch (c) { |
| 1482 |
case '<': |
| 1483 |
case '>': |
| 1484 |
case '^': |
| 1485 |
enter_state(state::align); |
| 1486 |
specs.set_align(parse_align(c)); |
| 1487 |
++begin; |
| 1488 |
break; |
| 1489 |
case '+': |
| 1490 |
case ' ': |
| 1491 |
specs.set_sign(c == ' ' ? sign::space : sign::plus); |
| 1492 |
FMT_FALLTHROUGH; |
| 1493 |
case '-': |
| 1494 |
enter_state(state::sign, in(arg_type, sint_set | float_set)); |
| 1495 |
++begin; |
| 1496 |
break; |
| 1497 |
case '#': |
| 1498 |
enter_state(state::hash, is_arithmetic_type(arg_type)); |
| 1499 |
specs.set_alt(); |
| 1500 |
++begin; |
| 1501 |
break; |
| 1502 |
case '0': |
| 1503 |
enter_state(state::zero); |
| 1504 |
if (!is_arithmetic_type(arg_type)) |
| 1505 |
report_error("format specifier requires numeric argument"); |
| 1506 |
if (specs.align() == align::none) { |
| 1507 |
// Ignore 0 if align is specified for compatibility with std::format. |
| 1508 |
specs.set_align(align::numeric); |
| 1509 |
specs.set_fill('0'); |
| 1510 |
} |
| 1511 |
++begin; |
| 1512 |
break; |
| 1513 |
// clang-format off |
| 1514 |
case '1': case '2': case '3': case '4': case '5': |
| 1515 |
case '6': case '7': case '8': case '9': case '{': |
| 1516 |
// clang-format on |
| 1517 |
enter_state(state::width); |
| 1518 |
begin = parse_width(begin, end, specs, specs.width_ref, ctx); |
| 1519 |
break; |
| 1520 |
case '.': |
| 1521 |
enter_state(state::precision, |
| 1522 |
in(arg_type, float_set | string_set | cstring_set)); |
| 1523 |
begin = parse_precision(begin, end, specs, specs.precision_ref, ctx); |
| 1524 |
break; |
| 1525 |
case 'L': |
| 1526 |
enter_state(state::locale, is_arithmetic_type(arg_type)); |
| 1527 |
specs.set_localized(); |
| 1528 |
++begin; |
| 1529 |
break; |
| 1530 |
case 'd': return parse_presentation_type(pres::dec, integral_set); |
| 1531 |
case 'X': specs.set_upper(); FMT_FALLTHROUGH; |
| 1532 |
case 'x': return parse_presentation_type(pres::hex, integral_set); |
| 1533 |
case 'o': return parse_presentation_type(pres::oct, integral_set); |
| 1534 |
case 'B': specs.set_upper(); FMT_FALLTHROUGH; |
| 1535 |
case 'b': return parse_presentation_type(pres::bin, integral_set); |
| 1536 |
case 'E': specs.set_upper(); FMT_FALLTHROUGH; |
| 1537 |
case 'e': return parse_presentation_type(pres::exp, float_set); |
| 1538 |
case 'F': specs.set_upper(); FMT_FALLTHROUGH; |
| 1539 |
case 'f': return parse_presentation_type(pres::fixed, float_set); |
| 1540 |
case 'G': specs.set_upper(); FMT_FALLTHROUGH; |
| 1541 |
case 'g': return parse_presentation_type(pres::general, float_set); |
| 1542 |
case 'A': specs.set_upper(); FMT_FALLTHROUGH; |
| 1543 |
case 'a': return parse_presentation_type(pres::hexfloat, float_set); |
| 1544 |
case 'c': |
| 1545 |
if (arg_type == type::bool_type) report_error("invalid format specifier"); |
| 1546 |
return parse_presentation_type(pres::chr, integral_set); |
| 1547 |
case 's': |
| 1548 |
return parse_presentation_type(pres::string, |
| 1549 |
bool_set | string_set | cstring_set); |
| 1550 |
case 'p': |
| 1551 |
return parse_presentation_type(pres::pointer, pointer_set | cstring_set); |
| 1552 |
case '?': |
| 1553 |
return parse_presentation_type(pres::debug, |
| 1554 |
char_set | string_set | cstring_set); |
| 1555 |
case '}': return begin; |
| 1556 |
default: { |
| 1557 |
if (*begin == '}') return begin; |
| 1558 |
// Parse fill and alignment. |
| 1559 |
auto fill_end = begin + code_point_length(begin); |
| 1560 |
if (end - fill_end <= 0) { |
| 1561 |
report_error("invalid format specifier"); |
| 1562 |
return begin; |
| 1563 |
} |
| 1564 |
if (*begin == '{') { |
| 1565 |
report_error("invalid fill character '{'"); |
| 1566 |
return begin; |
| 1567 |
} |
| 1568 |
auto alignment = parse_align(to_ascii(*fill_end)); |
| 1569 |
enter_state(state::align, alignment != align::none); |
| 1570 |
specs.set_fill( |
| 1571 |
basic_string_view<Char>(begin, to_unsigned(fill_end - begin))); |
| 1572 |
specs.set_align(alignment); |
| 1573 |
begin = fill_end + 1; |
| 1574 |
} |
| 1575 |
} |
| 1576 |
if (begin == end) return begin; |
| 1577 |
c = to_ascii(*begin); |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
template <typename Char, typename Handler> |
| 1582 |
FMT_CONSTEXPR FMT_INLINE auto parse_replacement_field(const Char* begin, |
| 1583 |
const Char* end, |
| 1584 |
Handler&& handler) |
| 1585 |
-> const Char* { |
| 1586 |
++begin; |
| 1587 |
if (begin == end) { |
| 1588 |
handler.on_error("invalid format string"); |
| 1589 |
return end; |
| 1590 |
} |
| 1591 |
int arg_id = 0; |
| 1592 |
switch (*begin) { |
| 1593 |
case '}': |
| 1594 |
handler.on_replacement_field(handler.on_arg_id(), begin); |
| 1595 |
return begin + 1; |
| 1596 |
case '{': handler.on_text(begin, begin + 1); return begin + 1; |
| 1597 |
case ':': arg_id = handler.on_arg_id(); break; |
| 1598 |
default: { |
| 1599 |
struct id_adapter { |
| 1600 |
Handler& handler; |
| 1601 |
int arg_id; |
| 1602 |
|
| 1603 |
FMT_CONSTEXPR void on_index(int id) { arg_id = handler.on_arg_id(id); } |
| 1604 |
FMT_CONSTEXPR void on_name(basic_string_view<Char> id) { |
| 1605 |
arg_id = handler.on_arg_id(id); |
| 1606 |
} |
| 1607 |
} adapter = {handler, 0}; |
| 1608 |
begin = parse_arg_id(begin, end, adapter); |
| 1609 |
arg_id = adapter.arg_id; |
| 1610 |
Char c = begin != end ? *begin : Char(); |
| 1611 |
if (c == '}') { |
| 1612 |
handler.on_replacement_field(arg_id, begin); |
| 1613 |
return begin + 1; |
| 1614 |
} |
| 1615 |
if (c != ':') { |
| 1616 |
handler.on_error("missing '}' in format string"); |
| 1617 |
return end; |
| 1618 |
} |
| 1619 |
break; |
| 1620 |
} |
| 1621 |
} |
| 1622 |
begin = handler.on_format_specs(arg_id, begin + 1, end); |
| 1623 |
if (begin == end || *begin != '}') |
| 1624 |
return handler.on_error("unknown format specifier"), end; |
| 1625 |
return begin + 1; |
| 1626 |
} |
| 1627 |
|
| 1628 |
template <typename Char, typename Handler> |
| 1629 |
FMT_CONSTEXPR void parse_format_string(basic_string_view<Char> fmt, |
| 1630 |
Handler&& handler) { |
| 1631 |
auto begin = fmt.data(), end = begin + fmt.size(); |
| 1632 |
auto p = begin; |
| 1633 |
while (p != end) { |
| 1634 |
auto c = *p++; |
| 1635 |
if (c == '{') { |
| 1636 |
handler.on_text(begin, p - 1); |
| 1637 |
begin = p = parse_replacement_field(p - 1, end, handler); |
| 1638 |
} else if (c == '}') { |
| 1639 |
if (p == end || *p != '}') |
| 1640 |
return handler.on_error("unmatched '}' in format string"); |
| 1641 |
handler.on_text(begin, p); |
| 1642 |
begin = ++p; |
| 1643 |
} |
| 1644 |
} |
| 1645 |
handler.on_text(begin, end); |
| 1646 |
} |
| 1647 |
|
| 1648 |
// Checks char specs and returns true iff the presentation type is char-like. |
| 1649 |
FMT_CONSTEXPR inline auto check_char_specs(const format_specs& specs) -> bool { |
| 1650 |
auto type = specs.type(); |
| 1651 |
if (type != presentation_type::none && type != presentation_type::chr && |
| 1652 |
type != presentation_type::debug) { |
| 1653 |
return false; |
| 1654 |
} |
| 1655 |
if (specs.align() == align::numeric || specs.sign() != sign::none || |
| 1656 |
specs.alt()) { |
| 1657 |
report_error("invalid format specifier for char"); |
| 1658 |
} |
| 1659 |
return true; |
| 1660 |
} |
| 1661 |
|
| 1662 |
// A base class for compile-time strings. |
| 1663 |
struct compile_string {}; |
| 1664 |
|
| 1665 |
template <typename T, typename Char> |
| 1666 |
FMT_VISIBILITY("hidden") // Suppress an ld warning on macOS (#3769). |
| 1667 |
FMT_CONSTEXPR auto invoke_parse(parse_context<Char>& ctx) -> const Char* { |
| 1668 |
using mapped_type = remove_cvref_t<mapped_t<T, Char>>; |
| 1669 |
constexpr bool formattable = |
| 1670 |
std::is_constructible<formatter<mapped_type, Char>>::value; |
| 1671 |
if (!formattable) return ctx.begin(); // Error is reported in the value ctor. |
| 1672 |
using formatted_type = conditional_t<formattable, mapped_type, int>; |
| 1673 |
return formatter<formatted_type, Char>().parse(ctx); |
| 1674 |
} |
| 1675 |
|
| 1676 |
template <typename... T> struct arg_pack {}; |
| 1677 |
|
| 1678 |
template <typename Char, int NUM_ARGS, int NUM_NAMED_ARGS, bool DYNAMIC_NAMES> |
| 1679 |
class format_string_checker { |
| 1680 |
private: |
| 1681 |
type types_[max_of<size_t>(1, NUM_ARGS)]; |
| 1682 |
named_arg_info<Char> named_args_[max_of<size_t>(1, NUM_NAMED_ARGS)]; |
| 1683 |
compile_parse_context<Char> context_; |
| 1684 |
|
| 1685 |
using parse_func = auto (*)(parse_context<Char>&) -> const Char*; |
| 1686 |
parse_func parse_funcs_[max_of<size_t>(1, NUM_ARGS)]; |
| 1687 |
|
| 1688 |
public: |
| 1689 |
template <typename... T> |
| 1690 |
FMT_CONSTEXPR explicit format_string_checker(basic_string_view<Char> fmt, |
| 1691 |
arg_pack<T...>) |
| 1692 |
: types_{mapped_type_constant<T, Char>::value...}, |
| 1693 |
named_args_{}, |
| 1694 |
context_(fmt, NUM_ARGS, types_), |
| 1695 |
parse_funcs_{&invoke_parse<T, Char>...} { |
| 1696 |
int arg_index = 0, named_arg_index = 0; |
| 1697 |
FMT_APPLY_VARIADIC( |
| 1698 |
init_static_named_arg<T>(named_args_, arg_index, named_arg_index)); |
| 1699 |
ignore_unused(arg_index, named_arg_index); |
| 1700 |
} |
| 1701 |
|
| 1702 |
FMT_CONSTEXPR void on_text(const Char*, const Char*) {} |
| 1703 |
|
| 1704 |
FMT_CONSTEXPR auto on_arg_id() -> int { return context_.next_arg_id(); } |
| 1705 |
FMT_CONSTEXPR auto on_arg_id(int id) -> int { |
| 1706 |
context_.check_arg_id(id); |
| 1707 |
return id; |
| 1708 |
} |
| 1709 |
FMT_CONSTEXPR auto on_arg_id(basic_string_view<Char> id) -> int { |
| 1710 |
for (int i = 0; i < NUM_NAMED_ARGS; ++i) { |
| 1711 |
if (named_args_[i].name == id) return named_args_[i].id; |
| 1712 |
} |
| 1713 |
if (!DYNAMIC_NAMES) on_error("argument not found"); |
| 1714 |
return -1; |
| 1715 |
} |
| 1716 |
|
| 1717 |
FMT_CONSTEXPR void on_replacement_field(int id, const Char* begin) { |
| 1718 |
on_format_specs(id, begin, begin); // Call parse() on empty specs. |
| 1719 |
} |
| 1720 |
|
| 1721 |
FMT_CONSTEXPR auto on_format_specs(int id, const Char* begin, const Char* end) |
| 1722 |
-> const Char* { |
| 1723 |
context_.advance_to(begin); |
| 1724 |
if (id >= 0 && id < NUM_ARGS) return parse_funcs_[id](context_); |
| 1725 |
|
| 1726 |
// If id is out of range, it means we do not know the type and cannot parse |
| 1727 |
// the format at compile time. Instead, skip over content until we finish |
| 1728 |
// the format spec, accounting for any nested replacements. |
| 1729 |
for (int bracket_count = 0; |
| 1730 |
begin != end && (bracket_count > 0 || *begin != '}'); ++begin) { |
| 1731 |
if (*begin == '{') |
| 1732 |
++bracket_count; |
| 1733 |
else if (*begin == '}') |
| 1734 |
--bracket_count; |
| 1735 |
} |
| 1736 |
return begin; |
| 1737 |
} |
| 1738 |
|
| 1739 |
FMT_NORETURN FMT_CONSTEXPR void on_error(const char* message) { |
| 1740 |
report_error(message); |
| 1741 |
} |
| 1742 |
}; |
| 1743 |
|
| 1744 |
/// A contiguous memory buffer with an optional growing ability. It is an |
| 1745 |
/// internal class and shouldn't be used directly, only via `memory_buffer`. |
| 1746 |
template <typename T> class buffer { |
| 1747 |
private: |
| 1748 |
T* ptr_; |
| 1749 |
size_t size_; |
| 1750 |
size_t capacity_; |
| 1751 |
|
| 1752 |
using grow_fun = void (*)(buffer& buf, size_t capacity); |
| 1753 |
grow_fun grow_; |
| 1754 |
|
| 1755 |
protected: |
| 1756 |
// Don't initialize ptr_ since it is not accessed to save a few cycles. |
| 1757 |
FMT_CONSTEXPR buffer(grow_fun grow, size_t sz) noexcept |
| 1758 |
: size_(sz), capacity_(sz), grow_(grow) { |
| 1759 |
if (FMT_MSC_VERSION != 0) ptr_ = nullptr; // Suppress warning 26495. |
| 1760 |
} |
| 1761 |
|
| 1762 |
constexpr buffer(grow_fun grow, T* p = nullptr, size_t sz = 0, |
| 1763 |
size_t cap = 0) noexcept |
| 1764 |
: ptr_(p), size_(sz), capacity_(cap), grow_(grow) {} |
| 1765 |
|
| 1766 |
FMT_CONSTEXPR20 ~buffer() = default; |
| 1767 |
buffer(buffer&&) = default; |
| 1768 |
|
| 1769 |
/// Sets the buffer data and capacity. |
| 1770 |
FMT_CONSTEXPR void set(T* buf_data, size_t buf_capacity) noexcept { |
| 1771 |
ptr_ = buf_data; |
| 1772 |
capacity_ = buf_capacity; |
| 1773 |
} |
| 1774 |
|
| 1775 |
public: |
| 1776 |
using value_type = T; |
| 1777 |
using const_reference = const T&; |
| 1778 |
|
| 1779 |
buffer(const buffer&) = delete; |
| 1780 |
void operator=(const buffer&) = delete; |
| 1781 |
|
| 1782 |
auto begin() noexcept -> T* { return ptr_; } |
| 1783 |
auto end() noexcept -> T* { return ptr_ + size_; } |
| 1784 |
|
| 1785 |
auto begin() const noexcept -> const T* { return ptr_; } |
| 1786 |
auto end() const noexcept -> const T* { return ptr_ + size_; } |
| 1787 |
|
| 1788 |
/// Returns the size of this buffer. |
| 1789 |
constexpr auto size() const noexcept -> size_t { return size_; } |
| 1790 |
|
| 1791 |
/// Returns the capacity of this buffer. |
| 1792 |
constexpr auto capacity() const noexcept -> size_t { return capacity_; } |
| 1793 |
|
| 1794 |
/// Returns a pointer to the buffer data (not null-terminated). |
| 1795 |
FMT_CONSTEXPR auto data() noexcept -> T* { return ptr_; } |
| 1796 |
FMT_CONSTEXPR auto data() const noexcept -> const T* { return ptr_; } |
| 1797 |
|
| 1798 |
/// Clears this buffer. |
| 1799 |
FMT_CONSTEXPR void clear() { size_ = 0; } |
| 1800 |
|
| 1801 |
// Tries resizing the buffer to contain `count` elements. If T is a POD type |
| 1802 |
// the new elements may not be initialized. |
| 1803 |
FMT_CONSTEXPR void try_resize(size_t count) { |
| 1804 |
try_reserve(count); |
| 1805 |
size_ = min_of(count, capacity_); |
| 1806 |
} |
| 1807 |
|
| 1808 |
// Tries increasing the buffer capacity to `new_capacity`. It can increase the |
| 1809 |
// capacity by a smaller amount than requested but guarantees there is space |
| 1810 |
// for at least one additional element either by increasing the capacity or by |
| 1811 |
// flushing the buffer if it is full. |
| 1812 |
FMT_CONSTEXPR void try_reserve(size_t new_capacity) { |
| 1813 |
if (new_capacity > capacity_) grow_(*this, new_capacity); |
| 1814 |
} |
| 1815 |
|
| 1816 |
FMT_CONSTEXPR void push_back(const T& value) { |
| 1817 |
try_reserve(size_ + 1); |
| 1818 |
ptr_[size_++] = value; |
| 1819 |
} |
| 1820 |
|
| 1821 |
/// Appends data to the end of the buffer. |
| 1822 |
template <typename U> |
| 1823 |
FMT_CONSTEXPR20 void append(const U* begin, const U* end) { |
| 1824 |
static_assert(std::is_same<T, U>() || std::is_same<U, char>(), ""); |
| 1825 |
while (begin != end) { |
| 1826 |
auto size = size_; |
| 1827 |
auto free_cap = capacity_ - size; |
| 1828 |
auto count = to_unsigned(end - begin); |
| 1829 |
if (free_cap < count) { |
| 1830 |
grow_(*this, size + count); |
| 1831 |
size = size_; |
| 1832 |
free_cap = capacity_ - size; |
| 1833 |
count = count < free_cap ? count : free_cap; |
| 1834 |
} |
| 1835 |
// A loop is faster than memcpy on small sizes. |
| 1836 |
T* out = ptr_ + size; |
| 1837 |
for (size_t i = 0; i < count; ++i) out[i] = static_cast<T>(begin[i]); |
| 1838 |
size_ += count; |
| 1839 |
begin += count; |
| 1840 |
} |
| 1841 |
} |
| 1842 |
|
| 1843 |
template <typename Idx> FMT_CONSTEXPR auto operator[](Idx index) -> T& { |
| 1844 |
return ptr_[index]; |
| 1845 |
} |
| 1846 |
template <typename Idx> |
| 1847 |
constexpr auto operator[](Idx index) const -> const T& { |
| 1848 |
return ptr_[index]; |
| 1849 |
} |
| 1850 |
}; |
| 1851 |
|
| 1852 |
struct buffer_traits { |
| 1853 |
constexpr explicit buffer_traits(size_t) {} |
| 1854 |
constexpr auto count() const -> size_t { return 0; } |
| 1855 |
constexpr auto limit(size_t size) const -> size_t { return size; } |
| 1856 |
}; |
| 1857 |
|
| 1858 |
class fixed_buffer_traits { |
| 1859 |
private: |
| 1860 |
size_t count_ = 0; |
| 1861 |
size_t limit_; |
| 1862 |
|
| 1863 |
public: |
| 1864 |
constexpr explicit fixed_buffer_traits(size_t limit) : limit_(limit) {} |
| 1865 |
constexpr auto count() const -> size_t { return count_; } |
| 1866 |
FMT_CONSTEXPR auto limit(size_t size) -> size_t { |
| 1867 |
size_t n = limit_ > count_ ? limit_ - count_ : 0; |
| 1868 |
count_ += size; |
| 1869 |
return min_of(size, n); |
| 1870 |
} |
| 1871 |
}; |
| 1872 |
|
| 1873 |
template <typename OutputIt, typename InputIt, typename = void> |
| 1874 |
struct has_append : std::false_type {}; |
| 1875 |
|
| 1876 |
template <typename OutputIt, typename InputIt> |
| 1877 |
struct has_append<OutputIt, InputIt, |
| 1878 |
void_t<decltype(get_container(std::declval<OutputIt>()) |
| 1879 |
.append(std::declval<InputIt>(), |
| 1880 |
std::declval<InputIt>()))>> |
| 1881 |
: std::true_type {}; |
| 1882 |
|
| 1883 |
template <typename OutputIt, typename T, typename = void> |
| 1884 |
struct has_insert : std::false_type {}; |
| 1885 |
|
| 1886 |
template <typename OutputIt, typename T> |
| 1887 |
struct has_insert< |
| 1888 |
OutputIt, T, |
| 1889 |
void_t<decltype(get_container(std::declval<OutputIt>()) |
| 1890 |
.insert({}, std::declval<T>(), std::declval<T>()))>> |
| 1891 |
: std::true_type {}; |
| 1892 |
|
| 1893 |
// An optimized version of std::copy with the output value type (T). |
| 1894 |
template <typename T, typename InputIt, typename OutputIt, |
| 1895 |
FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>() && |
| 1896 |
has_append<OutputIt, InputIt>())> |
| 1897 |
FMT_CONSTEXPR auto copy(InputIt begin, InputIt end, OutputIt out) -> OutputIt { |
| 1898 |
get_container(out).append(begin, end); |
| 1899 |
return out; |
| 1900 |
} |
| 1901 |
|
| 1902 |
template <typename T, typename InputIt, typename OutputIt, |
| 1903 |
FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>() && |
| 1904 |
!has_append<OutputIt, InputIt>() && |
| 1905 |
has_insert<OutputIt, InputIt>())> |
| 1906 |
FMT_CONSTEXPR auto copy(InputIt begin, InputIt end, OutputIt out) -> OutputIt { |
| 1907 |
auto& c = get_container(out); |
| 1908 |
c.insert(c.end(), begin, end); |
| 1909 |
return out; |
| 1910 |
} |
| 1911 |
|
| 1912 |
template <typename T, typename InputIt, typename OutputIt, |
| 1913 |
FMT_ENABLE_IF(!is_back_insert_iterator<OutputIt>() || |
| 1914 |
!(has_append<OutputIt, InputIt>() || |
| 1915 |
has_insert<OutputIt, InputIt>()))> |
| 1916 |
FMT_CONSTEXPR auto copy(InputIt begin, InputIt end, OutputIt out) -> OutputIt { |
| 1917 |
while (begin != end) *out++ = static_cast<T>(*begin++); |
| 1918 |
return out; |
| 1919 |
} |
| 1920 |
|
| 1921 |
// A buffer that writes to an output iterator when flushed. |
| 1922 |
template <typename OutputIt, typename T, typename Traits = buffer_traits> |
| 1923 |
class iterator_buffer : public Traits, public buffer<T> { |
| 1924 |
private: |
| 1925 |
OutputIt out_; |
| 1926 |
enum { buffer_size = 256 }; |
| 1927 |
T data_[buffer_size]; |
| 1928 |
|
| 1929 |
static FMT_CONSTEXPR void grow(buffer<T>& buf, size_t) { |
| 1930 |
if (buf.size() == buffer_size) static_cast<iterator_buffer&>(buf).flush(); |
| 1931 |
} |
| 1932 |
|
| 1933 |
void flush() { |
| 1934 |
auto size = this->size(); |
| 1935 |
this->clear(); |
| 1936 |
const T* begin = data_; |
| 1937 |
const T* end = begin + this->limit(size); |
| 1938 |
out_ = copy<T>(begin, end, out_); |
| 1939 |
} |
| 1940 |
|
| 1941 |
public: |
| 1942 |
explicit iterator_buffer(OutputIt out, size_t n = buffer_size) |
| 1943 |
: Traits(n), buffer<T>(grow, data_, 0, buffer_size), out_(out) {} |
| 1944 |
iterator_buffer(iterator_buffer&& other) noexcept |
| 1945 |
: Traits(other), |
| 1946 |
buffer<T>(grow, data_, 0, buffer_size), |
| 1947 |
out_(other.out_) {} |
| 1948 |
~iterator_buffer() { |
| 1949 |
// Don't crash if flush fails during unwinding. |
| 1950 |
FMT_TRY { flush(); } |
| 1951 |
FMT_CATCH(...) {} |
| 1952 |
} |
| 1953 |
|
| 1954 |
auto out() -> OutputIt { |
| 1955 |
flush(); |
| 1956 |
return out_; |
| 1957 |
} |
| 1958 |
auto count() const -> size_t { return Traits::count() + this->size(); } |
| 1959 |
}; |
| 1960 |
|
| 1961 |
template <typename T> |
| 1962 |
class iterator_buffer<T*, T, fixed_buffer_traits> : public fixed_buffer_traits, |
| 1963 |
public buffer<T> { |
| 1964 |
private: |
| 1965 |
T* out_; |
| 1966 |
enum { buffer_size = 256 }; |
| 1967 |
T data_[buffer_size]; |
| 1968 |
|
| 1969 |
static FMT_CONSTEXPR void grow(buffer<T>& buf, size_t) { |
| 1970 |
if (buf.size() == buf.capacity()) |
| 1971 |
static_cast<iterator_buffer&>(buf).flush(); |
| 1972 |
} |
| 1973 |
|
| 1974 |
void flush() { |
| 1975 |
size_t n = this->limit(this->size()); |
| 1976 |
if (this->data() == out_) { |
| 1977 |
out_ += n; |
| 1978 |
this->set(data_, buffer_size); |
| 1979 |
} |
| 1980 |
this->clear(); |
| 1981 |
} |
| 1982 |
|
| 1983 |
public: |
| 1984 |
explicit iterator_buffer(T* out, size_t n = buffer_size) |
| 1985 |
: fixed_buffer_traits(n), buffer<T>(grow, out, 0, n), out_(out) {} |
| 1986 |
iterator_buffer(iterator_buffer&& other) noexcept |
| 1987 |
: fixed_buffer_traits(other), |
| 1988 |
buffer<T>(static_cast<iterator_buffer&&>(other)), |
| 1989 |
out_(other.out_) { |
| 1990 |
if (this->data() != out_) { |
| 1991 |
this->set(data_, buffer_size); |
| 1992 |
this->clear(); |
| 1993 |
} |
| 1994 |
} |
| 1995 |
~iterator_buffer() { flush(); } |
| 1996 |
|
| 1997 |
auto out() -> T* { |
| 1998 |
flush(); |
| 1999 |
return out_; |
| 2000 |
} |
| 2001 |
auto count() const -> size_t { |
| 2002 |
return fixed_buffer_traits::count() + this->size(); |
| 2003 |
} |
| 2004 |
}; |
| 2005 |
|
| 2006 |
template <typename T> class iterator_buffer<T*, T> : public buffer<T> { |
| 2007 |
public: |
| 2008 |
explicit iterator_buffer(T* out, size_t = 0) |
| 2009 |
: buffer<T>([](buffer<T>&, size_t) {}, out, 0, ~size_t()) {} |
| 2010 |
|
| 2011 |
auto out() -> T* { return &*this->end(); } |
| 2012 |
}; |
| 2013 |
|
| 2014 |
template <typename Container> |
| 2015 |
class container_buffer : public buffer<typename Container::value_type> { |
| 2016 |
private: |
| 2017 |
using value_type = typename Container::value_type; |
| 2018 |
|
| 2019 |
static FMT_CONSTEXPR void grow(buffer<value_type>& buf, size_t capacity) { |
| 2020 |
auto& self = static_cast<container_buffer&>(buf); |
| 2021 |
self.container.resize(capacity); |
| 2022 |
self.set(&self.container[0], capacity); |
| 2023 |
} |
| 2024 |
|
| 2025 |
public: |
| 2026 |
Container& container; |
| 2027 |
|
| 2028 |
explicit container_buffer(Container& c) |
| 2029 |
: buffer<value_type>(grow, c.size()), container(c) {} |
| 2030 |
}; |
| 2031 |
|
| 2032 |
// A buffer that writes to a container with the contiguous storage. |
| 2033 |
template <typename OutputIt> |
| 2034 |
class iterator_buffer< |
| 2035 |
OutputIt, |
| 2036 |
enable_if_t<is_back_insert_iterator<OutputIt>::value && |
| 2037 |
is_contiguous<typename OutputIt::container_type>::value, |
| 2038 |
typename OutputIt::container_type::value_type>> |
| 2039 |
: public container_buffer<typename OutputIt::container_type> { |
| 2040 |
private: |
| 2041 |
using base = container_buffer<typename OutputIt::container_type>; |
| 2042 |
|
| 2043 |
public: |
| 2044 |
explicit iterator_buffer(typename OutputIt::container_type& c) : base(c) {} |
| 2045 |
explicit iterator_buffer(OutputIt out, size_t = 0) |
| 2046 |
: base(get_container(out)) {} |
| 2047 |
|
| 2048 |
auto out() -> OutputIt { return OutputIt(this->container); } |
| 2049 |
}; |
| 2050 |
|
| 2051 |
// A buffer that counts the number of code units written discarding the output. |
| 2052 |
template <typename T = char> class counting_buffer : public buffer<T> { |
| 2053 |
private: |
| 2054 |
enum { buffer_size = 256 }; |
| 2055 |
T data_[buffer_size]; |
| 2056 |
size_t count_ = 0; |
| 2057 |
|
| 2058 |
static FMT_CONSTEXPR void grow(buffer<T>& buf, size_t) { |
| 2059 |
if (buf.size() != buffer_size) return; |
| 2060 |
static_cast<counting_buffer&>(buf).count_ += buf.size(); |
| 2061 |
buf.clear(); |
| 2062 |
} |
| 2063 |
|
| 2064 |
public: |
| 2065 |
constexpr counting_buffer() : buffer<T>(grow, data_, 0, buffer_size) {} |
| 2066 |
|
| 2067 |
constexpr auto count() const noexcept -> size_t { |
| 2068 |
return count_ + this->size(); |
| 2069 |
} |
| 2070 |
}; |
| 2071 |
|
| 2072 |
template <typename T> |
| 2073 |
struct is_back_insert_iterator<basic_appender<T>> : std::true_type {}; |
| 2074 |
|
| 2075 |
template <typename It, typename Enable = std::true_type> |
| 2076 |
struct is_buffer_appender : std::false_type {}; |
| 2077 |
template <typename It> |
| 2078 |
struct is_buffer_appender< |
| 2079 |
It, bool_constant< |
| 2080 |
is_back_insert_iterator<It>::value && |
| 2081 |
std::is_base_of<buffer<typename It::container_type::value_type>, |
| 2082 |
typename It::container_type>::value>> |
| 2083 |
: std::true_type {}; |
| 2084 |
|
| 2085 |
// Maps an output iterator to a buffer. |
| 2086 |
template <typename T, typename OutputIt, |
| 2087 |
FMT_ENABLE_IF(!is_buffer_appender<OutputIt>::value)> |
| 2088 |
auto get_buffer(OutputIt out) -> iterator_buffer<OutputIt, T> { |
| 2089 |
return iterator_buffer<OutputIt, T>(out); |
| 2090 |
} |
| 2091 |
template <typename T, typename OutputIt, |
| 2092 |
FMT_ENABLE_IF(is_buffer_appender<OutputIt>::value)> |
| 2093 |
auto get_buffer(OutputIt out) -> buffer<T>& { |
| 2094 |
return get_container(out); |
| 2095 |
} |
| 2096 |
|
| 2097 |
template <typename Buf, typename OutputIt> |
| 2098 |
auto get_iterator(Buf& buf, OutputIt) -> decltype(buf.out()) { |
| 2099 |
return buf.out(); |
| 2100 |
} |
| 2101 |
template <typename T, typename OutputIt> |
| 2102 |
auto get_iterator(buffer<T>&, OutputIt out) -> OutputIt { |
| 2103 |
return out; |
| 2104 |
} |
| 2105 |
|
| 2106 |
// This type is intentionally undefined, only used for errors. |
| 2107 |
template <typename T, typename Char> struct type_is_unformattable_for; |
| 2108 |
|
| 2109 |
template <typename Char> struct string_value { |
| 2110 |
const Char* data; |
| 2111 |
size_t size; |
| 2112 |
auto str() const -> basic_string_view<Char> { return {data, size}; } |
| 2113 |
}; |
| 2114 |
|
| 2115 |
template <typename Context> struct custom_value { |
| 2116 |
using char_type = typename Context::char_type; |
| 2117 |
void* value; |
| 2118 |
void (*format)(void* arg, parse_context<char_type>& parse_ctx, Context& ctx); |
| 2119 |
}; |
| 2120 |
|
| 2121 |
template <typename Char> struct named_arg_value { |
| 2122 |
const named_arg_info<Char>* data; |
| 2123 |
size_t size; |
| 2124 |
}; |
| 2125 |
|
| 2126 |
struct custom_tag {}; |
| 2127 |
|
| 2128 |
#if !FMT_BUILTIN_TYPES |
| 2129 |
# define FMT_BUILTIN , monostate |
| 2130 |
#else |
| 2131 |
# define FMT_BUILTIN |
| 2132 |
#endif |
| 2133 |
|
| 2134 |
// A formatting argument value. |
| 2135 |
template <typename Context> class value { |
| 2136 |
public: |
| 2137 |
using char_type = typename Context::char_type; |
| 2138 |
|
| 2139 |
union { |
| 2140 |
monostate no_value; |
| 2141 |
int int_value; |
| 2142 |
unsigned uint_value; |
| 2143 |
long long long_long_value; |
| 2144 |
ullong ulong_long_value; |
| 2145 |
native_int128 int128_value; |
| 2146 |
native_uint128 uint128_value; |
| 2147 |
bool bool_value; |
| 2148 |
char_type char_value; |
| 2149 |
float float_value; |
| 2150 |
double double_value; |
| 2151 |
long double long_double_value; |
| 2152 |
const void* pointer; |
| 2153 |
string_value<char_type> string; |
| 2154 |
custom_value<Context> custom; |
| 2155 |
named_arg_value<char_type> named_args; |
| 2156 |
}; |
| 2157 |
|
| 2158 |
constexpr FMT_INLINE value() : no_value() {} |
| 2159 |
constexpr FMT_INLINE value(signed char x) : int_value(x) {} |
| 2160 |
constexpr FMT_INLINE value(unsigned char x FMT_BUILTIN) : uint_value(x) {} |
| 2161 |
constexpr FMT_INLINE value(signed short x) : int_value(x) {} |
| 2162 |
constexpr FMT_INLINE value(unsigned short x FMT_BUILTIN) : uint_value(x) {} |
| 2163 |
constexpr FMT_INLINE value(int x) : int_value(x) {} |
| 2164 |
constexpr FMT_INLINE value(unsigned x FMT_BUILTIN) : uint_value(x) {} |
| 2165 |
constexpr FMT_INLINE value(long x FMT_BUILTIN) : value(long_type(x)) {} |
| 2166 |
constexpr FMT_INLINE value(unsigned long x FMT_BUILTIN) |
| 2167 |
: value(ulong_type(x)) {} |
| 2168 |
constexpr FMT_INLINE value(long long x FMT_BUILTIN) : long_long_value(x) {} |
| 2169 |
constexpr FMT_INLINE value(ullong x FMT_BUILTIN) : ulong_long_value(x) {} |
| 2170 |
FMT_INLINE value(native_int128 x FMT_BUILTIN) : int128_value(x) {} |
| 2171 |
FMT_INLINE value(native_uint128 x FMT_BUILTIN) : uint128_value(x) {} |
| 2172 |
constexpr FMT_INLINE value(bool x FMT_BUILTIN) : bool_value(x) {} |
| 2173 |
|
| 2174 |
template <typename T, FMT_ENABLE_IF(is_code_unit<T>::value)> |
| 2175 |
constexpr FMT_INLINE value(T x FMT_BUILTIN) : char_value(x) { |
| 2176 |
static_assert( |
| 2177 |
std::is_same<T, char>::value || std::is_same<T, char_type>::value, |
| 2178 |
"mixing character types is disallowed"); |
| 2179 |
} |
| 2180 |
|
| 2181 |
constexpr FMT_INLINE value(float x FMT_BUILTIN) : float_value(x) {} |
| 2182 |
constexpr FMT_INLINE value(double x FMT_BUILTIN) : double_value(x) {} |
| 2183 |
FMT_INLINE value(long double x FMT_BUILTIN) : long_double_value(x) {} |
| 2184 |
|
| 2185 |
FMT_CONSTEXPR FMT_INLINE value(char_type* x FMT_BUILTIN) { |
| 2186 |
string.data = x; |
| 2187 |
if (is_constant_evaluated()) string.size = 0; |
| 2188 |
} |
| 2189 |
FMT_CONSTEXPR FMT_INLINE value(const char_type* x FMT_BUILTIN) { |
| 2190 |
string.data = x; |
| 2191 |
if (is_constant_evaluated()) string.size = 0; |
| 2192 |
} |
| 2193 |
template <typename T, typename C = char_t<T>, |
| 2194 |
FMT_ENABLE_IF(!std::is_pointer<T>::value)> |
| 2195 |
FMT_CONSTEXPR value(const T& x FMT_BUILTIN) { |
| 2196 |
static_assert(std::is_same<C, char_type>::value, |
| 2197 |
"mixing character types is disallowed"); |
| 2198 |
auto sv = to_string_view(x); |
| 2199 |
string.data = sv.data(); |
| 2200 |
string.size = sv.size(); |
| 2201 |
} |
| 2202 |
constexpr FMT_INLINE value(void* x FMT_BUILTIN) : pointer(x) {} |
| 2203 |
constexpr FMT_INLINE value(const void* x FMT_BUILTIN) : pointer(x) {} |
| 2204 |
constexpr FMT_INLINE value(volatile void* x FMT_BUILTIN) |
| 2205 |
: pointer(const_cast<const void*>(x)) {} |
| 2206 |
constexpr FMT_INLINE value(const volatile void* x FMT_BUILTIN) |
| 2207 |
: pointer(const_cast<const void*>(x)) {} |
| 2208 |
constexpr FMT_INLINE value(nullptr_t) : pointer(nullptr) {} |
| 2209 |
|
| 2210 |
template <typename T, |
| 2211 |
FMT_ENABLE_IF( |
| 2212 |
(std::is_pointer<T>::value || |
| 2213 |
std::is_member_pointer<T>::value) && |
| 2214 |
!std::is_void<typename std::remove_pointer<T>::type>::value)> |
| 2215 |
constexpr value(const T&) { |
| 2216 |
// Formatting of arbitrary pointers is disallowed. If you want to format a |
| 2217 |
// pointer cast it to `void*` or `const void*`. In particular, this forbids |
| 2218 |
// formatting of `[const] volatile char*` printed as bool by iostreams. |
| 2219 |
static_assert(sizeof(T) == 0, |
| 2220 |
"formatting of non-void pointers is disallowed"); |
| 2221 |
} |
| 2222 |
|
| 2223 |
template <typename T, FMT_ENABLE_IF(use_format_as<T>::value)> |
| 2224 |
constexpr value(const T& x) : value(format_as(x)) {} |
| 2225 |
template <typename T, FMT_ENABLE_IF(use_format_as_member<T>::value)> |
| 2226 |
constexpr value(const T& x) : value(formatter<T>::format_as(x)) {} |
| 2227 |
|
| 2228 |
template <typename T, FMT_ENABLE_IF(is_named_arg<T>::value)> |
| 2229 |
constexpr value(const T& named_arg) : value(named_arg.value) {} |
| 2230 |
|
| 2231 |
template <typename T, |
| 2232 |
FMT_ENABLE_IF(use_formatter<T>::value || !FMT_BUILTIN_TYPES)> |
| 2233 |
FMT_CONSTEXPR FMT_INLINE value(T& x) : value(x, custom_tag()) {} |
| 2234 |
|
| 2235 |
FMT_ALWAYS_INLINE value(const named_arg_info<char_type>* args, size_t size) |
| 2236 |
: named_args{args, size} {} |
| 2237 |
|
| 2238 |
private: |
| 2239 |
template <typename T, FMT_ENABLE_IF(has_formatter<T, char_type>())> |
| 2240 |
FMT_CONSTEXPR value(T& x, custom_tag) { |
| 2241 |
using value_type = remove_const_t<T>; |
| 2242 |
// T may overload operator& e.g. std::vector<bool>::reference in libc++. |
| 2243 |
if (!is_constant_evaluated()) { |
| 2244 |
custom.value = |
| 2245 |
const_cast<char*>(&reinterpret_cast<const volatile char&>(x)); |
| 2246 |
} else { |
| 2247 |
custom.value = nullptr; |
| 2248 |
#if defined(__cpp_if_constexpr) |
| 2249 |
if constexpr (std::is_same<decltype(&x), remove_reference_t<T>*>::value) |
| 2250 |
custom.value = const_cast<value_type*>(&x); |
| 2251 |
#endif |
| 2252 |
} |
| 2253 |
custom.format = format_custom<value_type>; |
| 2254 |
} |
| 2255 |
|
| 2256 |
template <typename T, FMT_ENABLE_IF(!has_formatter<T, char_type>())> |
| 2257 |
FMT_CONSTEXPR value(const T&, custom_tag) { |
| 2258 |
// Cannot format an argument; to make type T formattable provide a |
| 2259 |
// formatter<T> specialization: https://fmt.dev/latest/api#udt. |
| 2260 |
type_is_unformattable_for<T, char_type> _; |
| 2261 |
} |
| 2262 |
|
| 2263 |
// Formats an argument of a custom type, such as a user-defined class. |
| 2264 |
template <typename T> |
| 2265 |
static void format_custom(void* arg, parse_context<char_type>& parse_ctx, |
| 2266 |
Context& ctx) { |
| 2267 |
auto f = formatter<T, char_type>(); |
| 2268 |
parse_ctx.advance_to(f.parse(parse_ctx)); |
| 2269 |
using qualified_type = |
| 2270 |
conditional_t<has_formatter<const T, char_type>(), const T, T>; |
| 2271 |
// format must be const for compatibility with std::format and compilation. |
| 2272 |
const auto& cf = f; |
| 2273 |
ctx.advance_to(cf.format(*static_cast<qualified_type*>(arg), ctx)); |
| 2274 |
} |
| 2275 |
}; |
| 2276 |
|
| 2277 |
enum { packed_arg_bits = 4 }; |
| 2278 |
// Maximum number of arguments with packed types. |
| 2279 |
enum { max_packed_args = 62 / packed_arg_bits }; |
| 2280 |
enum : ullong { is_unpacked_bit = 1ULL << 63 }; |
| 2281 |
enum : ullong { has_named_args_bit = 1ULL << 62 }; |
| 2282 |
|
| 2283 |
template <typename It, typename T, typename Enable = void> |
| 2284 |
struct is_output_iterator : std::false_type {}; |
| 2285 |
|
| 2286 |
template <> struct is_output_iterator<appender, char> : std::true_type {}; |
| 2287 |
|
| 2288 |
template <typename It, typename T> |
| 2289 |
struct is_output_iterator< |
| 2290 |
It, T, |
| 2291 |
enable_if_t<std::is_assignable<decltype(*std::declval<decay_t<It>&>()++), |
| 2292 |
T>::value>> : std::true_type {}; |
| 2293 |
|
| 2294 |
template <typename> constexpr auto encode_types() -> ullong { return 0; } |
| 2295 |
|
| 2296 |
template <typename Context, typename First, typename... T> |
| 2297 |
constexpr auto encode_types() -> ullong { |
| 2298 |
return unsigned(stored_type_constant<First, Context>::value) | |
| 2299 |
(encode_types<Context, T...>() << packed_arg_bits); |
| 2300 |
} |
| 2301 |
|
| 2302 |
template <typename Context, typename... T, size_t NUM_ARGS = sizeof...(T)> |
| 2303 |
constexpr auto make_descriptor() -> ullong { |
| 2304 |
return NUM_ARGS <= max_packed_args ? encode_types<Context, T...>() |
| 2305 |
: is_unpacked_bit | NUM_ARGS; |
| 2306 |
} |
| 2307 |
|
| 2308 |
template <typename Context, int NUM_ARGS> |
| 2309 |
using arg_t = conditional_t<NUM_ARGS <= max_packed_args, value<Context>, |
| 2310 |
basic_format_arg<Context>>; |
| 2311 |
|
| 2312 |
template <typename Context, int NUM_ARGS, int NUM_NAMED_ARGS, ullong DESC> |
| 2313 |
struct named_arg_store { |
| 2314 |
// args_[0].named_args points to named_args to avoid bloating format_args. |
| 2315 |
arg_t<Context, NUM_ARGS> args[NUM_ARGS + 1u]; |
| 2316 |
named_arg_info<typename Context::char_type> named_args[NUM_NAMED_ARGS + 0u]; |
| 2317 |
|
| 2318 |
template <typename... T> |
| 2319 |
FMT_CONSTEXPR FMT_ALWAYS_INLINE named_arg_store(T&... values) |
| 2320 |
: args{{named_args, NUM_NAMED_ARGS}, values...} { |
| 2321 |
int arg_index = 0, named_arg_index = 0; |
| 2322 |
FMT_APPLY_VARIADIC( |
| 2323 |
init_named_arg(named_args, arg_index, named_arg_index, values)); |
| 2324 |
} |
| 2325 |
|
| 2326 |
named_arg_store(named_arg_store&& rhs) { |
| 2327 |
args[0] = {named_args, NUM_NAMED_ARGS}; |
| 2328 |
for (size_t i = 1; i < sizeof(args) / sizeof(*args); ++i) |
| 2329 |
args[i] = rhs.args[i]; |
| 2330 |
for (size_t i = 0; i < NUM_NAMED_ARGS; ++i) |
| 2331 |
named_args[i] = rhs.named_args[i]; |
| 2332 |
} |
| 2333 |
|
| 2334 |
named_arg_store(const named_arg_store& rhs) = delete; |
| 2335 |
auto operator=(const named_arg_store& rhs) -> named_arg_store& = delete; |
| 2336 |
auto operator=(named_arg_store&& rhs) -> named_arg_store& = delete; |
| 2337 |
operator const arg_t<Context, NUM_ARGS>*() const { return args + 1; } |
| 2338 |
}; |
| 2339 |
|
| 2340 |
// An array of references to arguments. It can be implicitly converted to |
| 2341 |
// `basic_format_args` for passing into type-erased formatting functions |
| 2342 |
// such as `vformat`. It is a plain struct to reduce binary size in debug mode. |
| 2343 |
template <typename Context, int NUM_ARGS, int NUM_NAMED_ARGS, ullong DESC> |
| 2344 |
struct format_arg_store { |
| 2345 |
// +1 to workaround a bug in gcc 7.5 that causes duplicated-branches warning. |
| 2346 |
using type = |
| 2347 |
conditional_t<NUM_NAMED_ARGS == 0, |
| 2348 |
arg_t<Context, NUM_ARGS>[max_of<size_t>(1, NUM_ARGS)], |
| 2349 |
named_arg_store<Context, NUM_ARGS, NUM_NAMED_ARGS, DESC>>; |
| 2350 |
type args; |
| 2351 |
}; |
| 2352 |
|
| 2353 |
// TYPE can be different from type_constant<T>, e.g. for __float128. |
| 2354 |
template <typename T, typename Char, type TYPE> struct native_formatter { |
| 2355 |
private: |
| 2356 |
dynamic_format_specs<Char> specs_; |
| 2357 |
|
| 2358 |
public: |
| 2359 |
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* { |
| 2360 |
if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin(); |
| 2361 |
auto end = parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, TYPE); |
| 2362 |
if FMT_CONSTEXPR20 (TYPE == type::char_type) check_char_specs(specs_); |
| 2363 |
return end; |
| 2364 |
} |
| 2365 |
|
| 2366 |
template <type U = TYPE, |
| 2367 |
FMT_ENABLE_IF(U == type::string_type || U == type::cstring_type || |
| 2368 |
U == type::char_type)> |
| 2369 |
FMT_CONSTEXPR void set_debug_format(bool set = true) { |
| 2370 |
specs_.set_type(set ? presentation_type::debug : presentation_type::none); |
| 2371 |
} |
| 2372 |
|
| 2373 |
template <typename FormatContext> |
| 2374 |
FMT_CONSTEXPR auto format(const T& val, FormatContext& ctx) const |
| 2375 |
-> decltype(ctx.out()); |
| 2376 |
}; |
| 2377 |
|
| 2378 |
template <bool B> constexpr bool enforce_compile_checks() { |
| 2379 |
#ifdef FMT_ENFORCE_COMPILE_STRING |
| 2380 |
static_assert( |
| 2381 |
FMT_USE_CONSTEVAL && B, |
| 2382 |
"FMT_ENFORCE_COMPILE_STRING requires format strings to use FMT_STRING"); |
| 2383 |
#endif |
| 2384 |
return true; |
| 2385 |
} |
| 2386 |
|
| 2387 |
template <typename T = int> constexpr auto is_locking() -> bool { |
| 2388 |
return locking<remove_cvref_t<T>>::value; |
| 2389 |
} |
| 2390 |
template <typename T1, typename T2, typename... Tail> |
| 2391 |
constexpr auto is_locking() -> bool { |
| 2392 |
return locking<remove_cvref_t<T1>>::value || is_locking<T2, Tail...>(); |
| 2393 |
} |
| 2394 |
|
| 2395 |
FMT_API void vformat_to(buffer<char>& buf, string_view fmt, format_args args, |
| 2396 |
locale_ref loc = {}); |
| 2397 |
|
| 2398 |
#if FMT_WIN32 |
| 2399 |
FMT_API void vprint_mojibake(FILE*, string_view, format_args, bool); |
| 2400 |
#else // format_args is passed by reference since it is defined later. |
| 2401 |
inline void vprint_mojibake(FILE*, string_view, const format_args&, bool) {} |
| 2402 |
#endif |
| 2403 |
} // namespace detail |
| 2404 |
|
| 2405 |
// The main public API. |
| 2406 |
|
| 2407 |
template <typename T, typename Char = char> |
| 2408 |
using named_arg = detail::named_arg<T, Char>; |
| 2409 |
|
| 2410 |
template <typename Char> |
| 2411 |
FMT_CONSTEXPR void parse_context<Char>::do_check_arg_id(int arg_id) { |
| 2412 |
// Argument id is only checked at compile time during parsing because |
| 2413 |
// formatting has its own validation. |
| 2414 |
if (detail::is_constant_evaluated() && use_constexpr_cast) { |
| 2415 |
auto ctx = static_cast<detail::compile_parse_context<Char>*>(this); |
| 2416 |
if (arg_id >= ctx->num_args()) report_error("argument not found"); |
| 2417 |
} |
| 2418 |
} |
| 2419 |
|
| 2420 |
template <typename Char> |
| 2421 |
FMT_CONSTEXPR void parse_context<Char>::check_dynamic_spec(int arg_id) { |
| 2422 |
using detail::compile_parse_context; |
| 2423 |
if (detail::is_constant_evaluated() && use_constexpr_cast) |
| 2424 |
static_cast<compile_parse_context<Char>*>(this)->check_dynamic_spec(arg_id); |
| 2425 |
} |
| 2426 |
|
| 2427 |
FMT_BEGIN_EXPORT |
| 2428 |
|
| 2429 |
// An output iterator that appends to a buffer. It is used instead of |
| 2430 |
// back_insert_iterator to reduce symbol sizes and avoid <iterator> dependency. |
| 2431 |
template <typename T> class basic_appender { |
| 2432 |
protected: |
| 2433 |
detail::buffer<T>* container; |
| 2434 |
|
| 2435 |
public: |
| 2436 |
using container_type = detail::buffer<T>; |
| 2437 |
|
| 2438 |
constexpr basic_appender(detail::buffer<T>& buf) : container(&buf) {} |
| 2439 |
|
| 2440 |
FMT_CONSTEXPR auto operator=(T c) -> basic_appender& { |
| 2441 |
container->push_back(c); |
| 2442 |
return *this; |
| 2443 |
} |
| 2444 |
FMT_CONSTEXPR auto operator*() -> basic_appender& { return *this; } |
| 2445 |
FMT_CONSTEXPR auto operator++() -> basic_appender& { return *this; } |
| 2446 |
FMT_CONSTEXPR auto operator++(int) -> basic_appender { return *this; } |
| 2447 |
}; |
| 2448 |
|
| 2449 |
// A formatting argument. Context is a template parameter for the compiled API |
| 2450 |
// where output can be unbuffered. |
| 2451 |
template <typename Context> class basic_format_arg { |
| 2452 |
private: |
| 2453 |
detail::value<Context> value_; |
| 2454 |
detail::type type_; |
| 2455 |
|
| 2456 |
friend class basic_format_args<Context>; |
| 2457 |
|
| 2458 |
using char_type = typename Context::char_type; |
| 2459 |
|
| 2460 |
public: |
| 2461 |
class handle { |
| 2462 |
private: |
| 2463 |
detail::custom_value<Context> custom_; |
| 2464 |
|
| 2465 |
public: |
| 2466 |
explicit handle(detail::custom_value<Context> custom) : custom_(custom) {} |
| 2467 |
|
| 2468 |
void format(parse_context<char_type>& parse_ctx, Context& ctx) const { |
| 2469 |
custom_.format(custom_.value, parse_ctx, ctx); |
| 2470 |
} |
| 2471 |
}; |
| 2472 |
|
| 2473 |
constexpr basic_format_arg() : type_(detail::type::none_type) {} |
| 2474 |
basic_format_arg(const detail::named_arg_info<char_type>* args, size_t size) |
| 2475 |
: value_(args, size) {} |
| 2476 |
template <typename T> |
| 2477 |
basic_format_arg(T&& val) |
| 2478 |
: value_(val), type_(detail::stored_type_constant<T, Context>::value) {} |
| 2479 |
|
| 2480 |
constexpr explicit operator bool() const noexcept { |
| 2481 |
return type_ != detail::type::none_type; |
| 2482 |
} |
| 2483 |
auto type() const -> detail::type { return type_; } |
| 2484 |
|
| 2485 |
/** |
| 2486 |
* Visits an argument dispatching to the appropriate visit method based on |
| 2487 |
* the argument type. For example, if the argument type is `double` then |
| 2488 |
* `vis(value)` will be called with the value of type `double`. |
| 2489 |
*/ |
| 2490 |
template <typename Visitor> |
| 2491 |
FMT_CONSTEXPR FMT_INLINE auto visit(Visitor&& vis) const -> decltype(vis(0)) { |
| 2492 |
using detail::map; |
| 2493 |
switch (type_) { |
| 2494 |
case detail::type::none_type: break; |
| 2495 |
case detail::type::int_type: return vis(value_.int_value); |
| 2496 |
case detail::type::uint_type: return vis(value_.uint_value); |
| 2497 |
case detail::type::long_long_type: return vis(value_.long_long_value); |
| 2498 |
case detail::type::ulong_long_type: return vis(value_.ulong_long_value); |
| 2499 |
case detail::type::int128_type: return vis(map(value_.int128_value)); |
| 2500 |
case detail::type::uint128_type: return vis(map(value_.uint128_value)); |
| 2501 |
case detail::type::bool_type: return vis(value_.bool_value); |
| 2502 |
case detail::type::char_type: return vis(value_.char_value); |
| 2503 |
case detail::type::float_type: return vis(value_.float_value); |
| 2504 |
case detail::type::double_type: return vis(value_.double_value); |
| 2505 |
case detail::type::long_double_type: return vis(value_.long_double_value); |
| 2506 |
case detail::type::cstring_type: return vis(value_.string.data); |
| 2507 |
case detail::type::string_type: return vis(value_.string.str()); |
| 2508 |
case detail::type::pointer_type: return vis(value_.pointer); |
| 2509 |
case detail::type::custom_type: return vis(handle(value_.custom)); |
| 2510 |
} |
| 2511 |
return vis(monostate()); |
| 2512 |
} |
| 2513 |
|
| 2514 |
auto format_custom(const char_type* parse_begin, |
| 2515 |
parse_context<char_type>& parse_ctx, Context& ctx) |
| 2516 |
-> bool { |
| 2517 |
if (type_ != detail::type::custom_type) return false; |
| 2518 |
parse_ctx.advance_to(parse_begin); |
| 2519 |
value_.custom.format(value_.custom.value, parse_ctx, ctx); |
| 2520 |
return true; |
| 2521 |
} |
| 2522 |
}; |
| 2523 |
|
| 2524 |
/** |
| 2525 |
* A view of a collection of formatting arguments. To avoid lifetime issues it |
| 2526 |
* should only be used as a parameter type in type-erased functions such as |
| 2527 |
* `vformat`: |
| 2528 |
* |
| 2529 |
* void vlog(fmt::string_view fmt, fmt::format_args args); // OK |
| 2530 |
* fmt::format_args args = fmt::make_format_args(); // Dangling reference |
| 2531 |
*/ |
| 2532 |
template <typename Context> class basic_format_args { |
| 2533 |
private: |
| 2534 |
// A descriptor that contains information about formatting arguments. |
| 2535 |
// If the number of arguments is less or equal to max_packed_args then |
| 2536 |
// argument types are passed in the descriptor. This reduces binary code size |
| 2537 |
// per formatting function call. |
| 2538 |
ullong desc_; |
| 2539 |
union { |
| 2540 |
// If is_packed() returns true then argument values are stored in values_; |
| 2541 |
// otherwise they are stored in args_. This is done to improve cache |
| 2542 |
// locality and reduce compiled code size since storing larger objects |
| 2543 |
// may require more code (at least on x86-64) even if the same amount of |
| 2544 |
// data is actually copied to stack. It saves ~10% on the bloat test. |
| 2545 |
const detail::value<Context>* values_; |
| 2546 |
const basic_format_arg<Context>* args_; |
| 2547 |
}; |
| 2548 |
|
| 2549 |
constexpr auto is_packed() const -> bool { |
| 2550 |
return (desc_ & detail::is_unpacked_bit) == 0; |
| 2551 |
} |
| 2552 |
constexpr auto has_named_args() const -> bool { |
| 2553 |
return (desc_ & detail::has_named_args_bit) != 0; |
| 2554 |
} |
| 2555 |
|
| 2556 |
FMT_CONSTEXPR auto type(int index) const -> detail::type { |
| 2557 |
int shift = index * detail::packed_arg_bits; |
| 2558 |
unsigned mask = (1 << detail::packed_arg_bits) - 1; |
| 2559 |
return static_cast<detail::type>((desc_ >> shift) & mask); |
| 2560 |
} |
| 2561 |
|
| 2562 |
template <int NUM_ARGS, int NUM_NAMED_ARGS, ullong DESC> |
| 2563 |
using store = |
| 2564 |
detail::format_arg_store<Context, NUM_ARGS, NUM_NAMED_ARGS, DESC>; |
| 2565 |
|
| 2566 |
public: |
| 2567 |
using format_arg = basic_format_arg<Context>; |
| 2568 |
|
| 2569 |
constexpr basic_format_args() : desc_(0), args_(nullptr) {} |
| 2570 |
|
| 2571 |
/// Constructs a `basic_format_args` object from `format_arg_store`. |
| 2572 |
template <int NUM_ARGS, int NUM_NAMED_ARGS, ullong DESC, |
| 2573 |
FMT_ENABLE_IF(NUM_ARGS <= detail::max_packed_args)> |
| 2574 |
constexpr FMT_ALWAYS_INLINE basic_format_args( |
| 2575 |
const store<NUM_ARGS, NUM_NAMED_ARGS, DESC>& s) |
| 2576 |
: desc_(DESC | (NUM_NAMED_ARGS != 0 ? +detail::has_named_args_bit : 0)), |
| 2577 |
values_(s.args) {} |
| 2578 |
|
| 2579 |
template <int NUM_ARGS, int NUM_NAMED_ARGS, ullong DESC, |
| 2580 |
FMT_ENABLE_IF(NUM_ARGS > detail::max_packed_args)> |
| 2581 |
constexpr basic_format_args(const store<NUM_ARGS, NUM_NAMED_ARGS, DESC>& s) |
| 2582 |
: desc_(DESC | (NUM_NAMED_ARGS != 0 ? +detail::has_named_args_bit : 0)), |
| 2583 |
args_(s.args) {} |
| 2584 |
|
| 2585 |
/// Constructs a `basic_format_args` object from a dynamic list of arguments. |
| 2586 |
constexpr basic_format_args(const format_arg* args, int count, |
| 2587 |
bool has_named = false) |
| 2588 |
: desc_(detail::is_unpacked_bit | detail::to_unsigned(count) | |
| 2589 |
(has_named ? +detail::has_named_args_bit : 0)), |
| 2590 |
args_(args) {} |
| 2591 |
|
| 2592 |
/// Returns the argument with the specified id. |
| 2593 |
FMT_CONSTEXPR auto get(int id) const -> format_arg { |
| 2594 |
auto arg = format_arg(); |
| 2595 |
if (!is_packed()) { |
| 2596 |
if (unsigned(id) < unsigned(max_size())) arg = args_[id]; |
| 2597 |
return arg; |
| 2598 |
} |
| 2599 |
if (unsigned(id) >= detail::max_packed_args) return arg; |
| 2600 |
arg.type_ = type(id); |
| 2601 |
if (arg.type_ != detail::type::none_type) arg.value_ = values_[id]; |
| 2602 |
return arg; |
| 2603 |
} |
| 2604 |
|
| 2605 |
template <typename Char> |
| 2606 |
auto get(basic_string_view<Char> name) const -> format_arg { |
| 2607 |
int id = get_id(name); |
| 2608 |
return id >= 0 ? get(id) : format_arg(); |
| 2609 |
} |
| 2610 |
|
| 2611 |
template <typename Char> |
| 2612 |
FMT_CONSTEXPR auto get_id(basic_string_view<Char> name) const -> int { |
| 2613 |
if (!has_named_args()) return -1; |
| 2614 |
const auto& named_args = |
| 2615 |
(is_packed() ? values_[-1] : args_[-1].value_).named_args; |
| 2616 |
for (size_t i = 0; i < named_args.size; ++i) { |
| 2617 |
if (named_args.data[i].name == name) return named_args.data[i].id; |
| 2618 |
} |
| 2619 |
return -1; |
| 2620 |
} |
| 2621 |
|
| 2622 |
auto max_size() const -> int { |
| 2623 |
return int(is_packed() ? ullong(detail::max_packed_args) |
| 2624 |
: desc_ & ~detail::is_unpacked_bit); |
| 2625 |
} |
| 2626 |
}; |
| 2627 |
|
| 2628 |
// A formatting context. |
| 2629 |
class context { |
| 2630 |
private: |
| 2631 |
appender out_; |
| 2632 |
format_args args_; |
| 2633 |
FMT_NO_UNIQUE_ADDRESS locale_ref loc_; |
| 2634 |
|
| 2635 |
public: |
| 2636 |
using char_type = char; ///< The character type for the output. |
| 2637 |
using iterator = appender; |
| 2638 |
using format_arg = basic_format_arg<context>; |
| 2639 |
enum { builtin_types = FMT_BUILTIN_TYPES }; |
| 2640 |
|
| 2641 |
/// Constructs a `context` object. References to the arguments are stored |
| 2642 |
/// in the object so make sure they have appropriate lifetimes. |
| 2643 |
constexpr context(iterator out, format_args args, locale_ref loc = {}) |
| 2644 |
: out_(out), args_(args), loc_(loc) {} |
| 2645 |
context(context&&) = default; |
| 2646 |
context(const context&) = delete; |
| 2647 |
void operator=(const context&) = delete; |
| 2648 |
|
| 2649 |
FMT_CONSTEXPR auto arg(int id) const -> format_arg { return args_.get(id); } |
| 2650 |
inline auto arg(string_view name) const -> format_arg { |
| 2651 |
return args_.get(name); |
| 2652 |
} |
| 2653 |
FMT_CONSTEXPR auto arg_id(string_view name) const -> int { |
| 2654 |
return args_.get_id(name); |
| 2655 |
} |
| 2656 |
auto args() const -> const format_args& { return args_; } |
| 2657 |
|
| 2658 |
// Returns an iterator to the beginning of the output range. |
| 2659 |
constexpr auto out() const -> iterator { return out_; } |
| 2660 |
|
| 2661 |
// Advances the begin iterator to `it`. |
| 2662 |
FMT_CONSTEXPR void advance_to(iterator) {} |
| 2663 |
|
| 2664 |
constexpr auto locale() const -> locale_ref { return loc_; } |
| 2665 |
}; |
| 2666 |
|
| 2667 |
template <typename Char = char> struct runtime_format_string { |
| 2668 |
basic_string_view<Char> str; |
| 2669 |
}; |
| 2670 |
|
| 2671 |
/** |
| 2672 |
* Creates a runtime format string. |
| 2673 |
* |
| 2674 |
* **Example**: |
| 2675 |
* |
| 2676 |
* // Check format string at runtime instead of compile-time. |
| 2677 |
* fmt::print(fmt::runtime("{:d}"), "I am not a number"); |
| 2678 |
*/ |
| 2679 |
inline auto runtime(string_view s) -> runtime_format_string<> { return {{s}}; } |
| 2680 |
|
| 2681 |
/// A compile-time format string. Use `format_string` in the public API to |
| 2682 |
/// prevent type deduction. |
| 2683 |
template <typename... T> struct fstring { |
| 2684 |
private: |
| 2685 |
static constexpr int num_static_named_args = |
| 2686 |
detail::count_static_named_args<T...>(); |
| 2687 |
|
| 2688 |
using checker = detail::format_string_checker< |
| 2689 |
char, int(sizeof...(T)), num_static_named_args, |
| 2690 |
num_static_named_args != detail::count_named_args<T...>()>; |
| 2691 |
|
| 2692 |
using arg_pack = detail::arg_pack<T...>; |
| 2693 |
|
| 2694 |
public: |
| 2695 |
string_view str; |
| 2696 |
using t = fstring; |
| 2697 |
|
| 2698 |
// Reports a compile-time error if S is not a valid format string for T. |
| 2699 |
template <size_t N> |
| 2700 |
FMT_CONSTEVAL FMT_ALWAYS_INLINE fstring(const char (&s)[N]) : str(s, N - 1) { |
| 2701 |
using namespace detail; |
| 2702 |
static_assert(count<(is_view<remove_cvref_t<T>>::value && |
| 2703 |
std::is_reference<T>::value)...>() == 0, |
| 2704 |
"passing views as lvalues is disallowed"); |
| 2705 |
if (FMT_USE_CONSTEVAL) |
| 2706 |
parse_format_string<char>(str, checker(str, arg_pack())); |
| 2707 |
constexpr bool unused = detail::enforce_compile_checks<sizeof(s) != 0>(); |
| 2708 |
(void)unused; |
| 2709 |
} |
| 2710 |
template <typename S, |
| 2711 |
FMT_ENABLE_IF(std::is_convertible<const S&, string_view>::value)> |
| 2712 |
FMT_CONSTEVAL FMT_ALWAYS_INLINE fstring(const S& s) : str(s) { |
| 2713 |
if (FMT_USE_CONSTEVAL) |
| 2714 |
detail::parse_format_string<char>(str, checker(str, arg_pack())); |
| 2715 |
constexpr bool unused = detail::enforce_compile_checks<sizeof(s) != 0>(); |
| 2716 |
(void)unused; |
| 2717 |
} |
| 2718 |
template <typename S, |
| 2719 |
FMT_ENABLE_IF(std::is_base_of<detail::compile_string, S>::value&& |
| 2720 |
std::is_same<typename S::char_type, char>::value)> |
| 2721 |
FMT_ALWAYS_INLINE fstring(const S&) : str(S()) { |
| 2722 |
FMT_CONSTEXPR auto sv = string_view(S()); |
| 2723 |
FMT_CONSTEXPR int x = (parse_format_string(sv, checker(sv, arg_pack())), 0); |
| 2724 |
detail::ignore_unused(x); |
| 2725 |
} |
| 2726 |
fstring(runtime_format_string<> fmt) : str(fmt.str) {} |
| 2727 |
|
| 2728 |
FMT_DEPRECATED operator const string_view&() const { return str; } |
| 2729 |
auto get() const -> string_view { return str; } |
| 2730 |
}; |
| 2731 |
|
| 2732 |
template <typename... T> using format_string = typename fstring<T...>::t; |
| 2733 |
|
| 2734 |
template <typename T, typename Char = char> |
| 2735 |
using is_formattable = bool_constant<!std::is_same< |
| 2736 |
detail::mapped_t<conditional_t<std::is_void<T>::value, int*, T>, Char>, |
| 2737 |
void>::value>; |
| 2738 |
#if defined(__cpp_concepts) && __cpp_concepts >= 201907L |
| 2739 |
template <typename T, typename Char = char> |
| 2740 |
concept formattable = is_formattable<remove_reference_t<T>, Char>::value; |
| 2741 |
#endif |
| 2742 |
|
| 2743 |
// A formatter specialization for natively supported types. |
| 2744 |
template <typename T, typename Char> |
| 2745 |
struct formatter<T, Char, |
| 2746 |
enable_if_t<detail::type_constant<T, Char>::value != |
| 2747 |
detail::type::custom_type>> |
| 2748 |
: detail::native_formatter<T, Char, detail::type_constant<T, Char>::value> { |
| 2749 |
}; |
| 2750 |
|
| 2751 |
/** |
| 2752 |
* Constructs an object that stores references to arguments and can be |
| 2753 |
* implicitly converted to `format_args`. `Context` can be omitted in which case |
| 2754 |
* it defaults to `context`. See `arg` for lifetime considerations. |
| 2755 |
*/ |
| 2756 |
// Take arguments by lvalue references to avoid some lifetime issues, e.g. |
| 2757 |
// auto args = make_format_args(std::string()); |
| 2758 |
template <typename Context = context, typename... T, |
| 2759 |
int NUM_ARGS = int(sizeof...(T)), |
| 2760 |
int NUM_NAMED_ARGS = detail::count_named_args<T...>(), |
| 2761 |
ullong DESC = detail::make_descriptor<Context, T...>()> |
| 2762 |
constexpr FMT_ALWAYS_INLINE auto make_format_args(T&... args) |
| 2763 |
-> detail::format_arg_store<Context, NUM_ARGS, NUM_NAMED_ARGS, DESC> { |
| 2764 |
return {{args...}}; |
| 2765 |
} |
| 2766 |
|
| 2767 |
template <typename... T> |
| 2768 |
using vargs = |
| 2769 |
detail::format_arg_store<context, int(sizeof...(T)), |
| 2770 |
detail::count_named_args<T...>(), |
| 2771 |
detail::make_descriptor<context, T...>()>; |
| 2772 |
|
| 2773 |
/** |
| 2774 |
* Returns a named argument to be used in a formatting function. |
| 2775 |
* It should only be used in a call to a formatting function. |
| 2776 |
* |
| 2777 |
* **Example**: |
| 2778 |
* |
| 2779 |
* fmt::print("The answer is {answer}.", fmt::arg("answer", 42)); |
| 2780 |
* |
| 2781 |
* Named arguments passed with `fmt::arg` are not supported |
| 2782 |
* in compile-time checks, but `"answer"_a=42` are compile-time checked in |
| 2783 |
* sufficiently new compilers. See `operator""_a()`. |
| 2784 |
*/ |
| 2785 |
template <typename T> |
| 2786 |
inline auto arg(const char* name, const T& arg) -> named_arg<T> { |
| 2787 |
return {name, arg}; |
| 2788 |
} |
| 2789 |
|
| 2790 |
/// Formats a string and writes the output to `out`. |
| 2791 |
template <typename OutputIt, |
| 2792 |
FMT_ENABLE_IF(detail::is_output_iterator<remove_cvref_t<OutputIt>, |
| 2793 |
char>::value)> |
| 2794 |
// DEPRECATED! Passing out as a forwarding reference. |
| 2795 |
auto vformat_to(OutputIt&& out, string_view fmt, format_args args) |
| 2796 |
-> remove_cvref_t<OutputIt> { |
| 2797 |
auto&& buf = detail::get_buffer<char>(out); |
| 2798 |
detail::vformat_to(buf, fmt, args, {}); |
| 2799 |
return detail::get_iterator(buf, out); |
| 2800 |
} |
| 2801 |
|
| 2802 |
/** |
| 2803 |
* Formats `args` according to specifications in `fmt`, writes the result to |
| 2804 |
* the output iterator `out` and returns the iterator past the end of the output |
| 2805 |
* range. `format_to` does not append a terminating null character. |
| 2806 |
* |
| 2807 |
* **Example**: |
| 2808 |
* |
| 2809 |
* auto out = std::vector<char>(); |
| 2810 |
* fmt::format_to(std::back_inserter(out), "{}", 42); |
| 2811 |
*/ |
| 2812 |
template <typename OutputIt, typename... T, |
| 2813 |
FMT_ENABLE_IF(detail::is_output_iterator<remove_cvref_t<OutputIt>, |
| 2814 |
char>::value)> |
| 2815 |
FMT_INLINE auto format_to(OutputIt&& out, format_string<T...> fmt, T&&... args) |
| 2816 |
-> remove_cvref_t<OutputIt> { |
| 2817 |
return vformat_to(out, fmt.str, vargs<T...>{{args...}}); |
| 2818 |
} |
| 2819 |
|
| 2820 |
template <typename OutputIt> struct format_to_n_result { |
| 2821 |
OutputIt out; ///< Iterator past the end of the output range. |
| 2822 |
size_t size; ///< Total (not truncated) output size. |
| 2823 |
}; |
| 2824 |
|
| 2825 |
template <typename OutputIt, typename... T, |
| 2826 |
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)> |
| 2827 |
auto vformat_to_n(OutputIt out, size_t n, string_view fmt, format_args args) |
| 2828 |
-> format_to_n_result<OutputIt> { |
| 2829 |
using traits = detail::fixed_buffer_traits; |
| 2830 |
auto buf = detail::iterator_buffer<OutputIt, char, traits>(out, n); |
| 2831 |
detail::vformat_to(buf, fmt, args, {}); |
| 2832 |
return {buf.out(), buf.count()}; |
| 2833 |
} |
| 2834 |
|
| 2835 |
/** |
| 2836 |
* Formats `args` according to specifications in `fmt`, writes up to `n` |
| 2837 |
* characters of the result to the output iterator `out` and returns the total |
| 2838 |
* (not truncated) output size and the iterator past the end of the output |
| 2839 |
* range. `format_to_n` does not append a terminating null character. |
| 2840 |
*/ |
| 2841 |
template <typename OutputIt, typename... T, |
| 2842 |
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)> |
| 2843 |
FMT_INLINE auto format_to_n(OutputIt out, size_t n, format_string<T...> fmt, |
| 2844 |
T&&... args) -> format_to_n_result<OutputIt> { |
| 2845 |
return vformat_to_n(out, n, fmt.str, vargs<T...>{{args...}}); |
| 2846 |
} |
| 2847 |
|
| 2848 |
struct format_to_result { |
| 2849 |
char* out; ///< Pointer to just after the last successful write. |
| 2850 |
bool truncated; ///< Specifies if the output was truncated. |
| 2851 |
|
| 2852 |
FMT_CONSTEXPR operator char*() const { |
| 2853 |
// Report truncation to prevent silent data loss. |
| 2854 |
if (truncated) report_error("output is truncated"); |
| 2855 |
return out; |
| 2856 |
} |
| 2857 |
}; |
| 2858 |
|
| 2859 |
template <size_t N> |
| 2860 |
FMT_DEPRECATED auto vformat_to(char (&out)[N], string_view fmt, |
| 2861 |
format_args args) -> format_to_result { |
| 2862 |
auto result = vformat_to_n(out, N, fmt, args); |
| 2863 |
return {result.out, result.size > N}; |
| 2864 |
} |
| 2865 |
|
| 2866 |
template <size_t N, typename... T> |
| 2867 |
FMT_INLINE auto format_to(char (&out)[N], format_string<T...> fmt, T&&... args) |
| 2868 |
-> format_to_result { |
| 2869 |
auto result = vformat_to_n(out, N, fmt.str, vargs<T...>{{args...}}); |
| 2870 |
return {result.out, result.size > N}; |
| 2871 |
} |
| 2872 |
|
| 2873 |
/// Returns the number of chars in the output of `format(fmt, args...)`. |
| 2874 |
template <typename... T> |
| 2875 |
FMT_NODISCARD FMT_INLINE auto formatted_size(format_string<T...> fmt, |
| 2876 |
T&&... args) -> size_t { |
| 2877 |
auto buf = detail::counting_buffer<>(); |
| 2878 |
detail::vformat_to(buf, fmt.str, vargs<T...>{{args...}}, {}); |
| 2879 |
return buf.count(); |
| 2880 |
} |
| 2881 |
|
| 2882 |
FMT_API void vprint(string_view fmt, format_args args); |
| 2883 |
FMT_API void vprint(FILE* f, string_view fmt, format_args args); |
| 2884 |
FMT_API void vprintln(FILE* f, string_view fmt, format_args args); |
| 2885 |
FMT_API void vprint_buffered(FILE* f, string_view fmt, format_args args); |
| 2886 |
|
| 2887 |
/** |
| 2888 |
* Formats `args` according to specifications in `fmt` and writes the output |
| 2889 |
* to `stdout`. |
| 2890 |
* |
| 2891 |
* **Example**: |
| 2892 |
* |
| 2893 |
* fmt::print("The answer is {}.", 42); |
| 2894 |
*/ |
| 2895 |
template <typename... T> |
| 2896 |
FMT_INLINE void print(format_string<T...> fmt, T&&... args) { |
| 2897 |
vargs<T...> va = {{args...}}; |
| 2898 |
if FMT_CONSTEXPR20 (!detail::use_utf8) |
| 2899 |
return detail::vprint_mojibake(stdout, fmt.str, va, false); |
| 2900 |
detail::is_locking<T...>() ? vprint_buffered(stdout, fmt.str, va) |
| 2901 |
: vprint(fmt.str, va); |
| 2902 |
} |
| 2903 |
|
| 2904 |
/** |
| 2905 |
* Formats `args` according to specifications in `fmt` and writes the |
| 2906 |
* output to the file `f`. |
| 2907 |
* |
| 2908 |
* **Example**: |
| 2909 |
* |
| 2910 |
* fmt::print(stderr, "Don't {}!", "panic"); |
| 2911 |
*/ |
| 2912 |
template <typename... T> |
| 2913 |
FMT_INLINE void print(FILE* f, format_string<T...> fmt, T&&... args) { |
| 2914 |
vargs<T...> va = {{args...}}; |
| 2915 |
if FMT_CONSTEXPR20 (!detail::use_utf8) |
| 2916 |
return detail::vprint_mojibake(f, fmt.str, va, false); |
| 2917 |
detail::is_locking<T...>() ? vprint_buffered(f, fmt.str, va) |
| 2918 |
: vprint(f, fmt.str, va); |
| 2919 |
} |
| 2920 |
|
| 2921 |
/// Formats `args` according to specifications in `fmt` and writes the output |
| 2922 |
/// to the file `f` followed by a newline. |
| 2923 |
template <typename... T> |
| 2924 |
FMT_INLINE void println(FILE* f, format_string<T...> fmt, T&&... args) { |
| 2925 |
vargs<T...> va = {{args...}}; |
| 2926 |
if FMT_CONSTEXPR20 (detail::use_utf8) return vprintln(f, fmt.str, va); |
| 2927 |
detail::vprint_mojibake(f, fmt.str, va, true); |
| 2928 |
} |
| 2929 |
|
| 2930 |
/// Formats `args` according to specifications in `fmt` and writes the output |
| 2931 |
/// to `stdout` followed by a newline. |
| 2932 |
template <typename... T> |
| 2933 |
FMT_INLINE void println(format_string<T...> fmt, T&&... args) { |
| 2934 |
fmt::println(stdout, fmt, static_cast<T&&>(args)...); |
| 2935 |
} |
| 2936 |
|
| 2937 |
FMT_PRAGMA_GCC(pop_options) |
| 2938 |
FMT_END_EXPORT |
| 2939 |
FMT_END_NAMESPACE |
| 2940 |
|
| 2941 |
#ifdef FMT_HEADER_ONLY |
| 2942 |
# include "format.h" |
| 2943 |
#endif |
| 2944 |
#endif // FMT_BASE_H_ |