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