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