Debian Codegraph @

podofo/3rdparty/fmt/core.h

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