Roboruka
Knihovna pro obsluhu RoboRuky.
core.h
1// Formatting library for C++ - the core API
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 <functional>
14#include <iterator>
15#include <memory>
16#include <string>
17#include <type_traits>
18#include <vector>
19
20// The fmt library version in the form major * 10000 + minor * 100 + patch.
21#define FMT_VERSION 60201
22
23#ifdef __has_feature
24# define FMT_HAS_FEATURE(x) __has_feature(x)
25#else
26# define FMT_HAS_FEATURE(x) 0
27#endif
28
29#if defined(__has_include) && !defined(__INTELLISENSE__) && \
30 !(defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1600)
31# define FMT_HAS_INCLUDE(x) __has_include(x)
32#else
33# define FMT_HAS_INCLUDE(x) 0
34#endif
35
36#ifdef __has_cpp_attribute
37# define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
38#else
39# define FMT_HAS_CPP_ATTRIBUTE(x) 0
40#endif
41
42#define FMT_HAS_CPP14_ATTRIBUTE(attribute) \
43 (__cplusplus >= 201402L && FMT_HAS_CPP_ATTRIBUTE(attribute))
44
45#define FMT_HAS_CPP17_ATTRIBUTE(attribute) \
46 (__cplusplus >= 201703L && FMT_HAS_CPP_ATTRIBUTE(attribute))
47
48#ifdef __clang__
49# define FMT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__)
50#else
51# define FMT_CLANG_VERSION 0
52#endif
53
54#if defined(__GNUC__) && !defined(__clang__)
55# define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
56#else
57# define FMT_GCC_VERSION 0
58#endif
59
60#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
61# define FMT_HAS_GXX_CXX11 FMT_GCC_VERSION
62#else
63# define FMT_HAS_GXX_CXX11 0
64#endif
65
66#ifdef __NVCC__
67# define FMT_NVCC __NVCC__
68#else
69# define FMT_NVCC 0
70#endif
71
72#ifdef _MSC_VER
73# define FMT_MSC_VER _MSC_VER
74#else
75# define FMT_MSC_VER 0
76#endif
77
78// Check if relaxed C++14 constexpr is supported.
79// GCC doesn't allow throw in constexpr until version 6 (bug 67371).
80#ifndef FMT_USE_CONSTEXPR
81# define FMT_USE_CONSTEXPR \
82 (FMT_HAS_FEATURE(cxx_relaxed_constexpr) || FMT_MSC_VER >= 1910 || \
83 (FMT_GCC_VERSION >= 600 && __cplusplus >= 201402L)) && \
84 !FMT_NVCC
85#endif
86#if FMT_USE_CONSTEXPR
87# define FMT_CONSTEXPR constexpr
88# define FMT_CONSTEXPR_DECL constexpr
89#else
90# define FMT_CONSTEXPR inline
91# define FMT_CONSTEXPR_DECL
92#endif
93
94#ifndef FMT_OVERRIDE
95# if FMT_HAS_FEATURE(cxx_override) || \
96 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900
97# define FMT_OVERRIDE override
98# else
99# define FMT_OVERRIDE
100# endif
101#endif
102
103// Check if exceptions are disabled.
104#ifndef FMT_EXCEPTIONS
105# if (defined(__GNUC__) && !defined(__EXCEPTIONS)) || \
106 FMT_MSC_VER && !_HAS_EXCEPTIONS
107# define FMT_EXCEPTIONS 0
108# else
109# define FMT_EXCEPTIONS 1
110# endif
111#endif
112
113// Define FMT_USE_NOEXCEPT to make fmt use noexcept (C++11 feature).
114#ifndef FMT_USE_NOEXCEPT
115# define FMT_USE_NOEXCEPT 0
116#endif
117
118#if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \
119 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900
120# define FMT_DETECTED_NOEXCEPT noexcept
121# define FMT_HAS_CXX11_NOEXCEPT 1
122#else
123# define FMT_DETECTED_NOEXCEPT throw()
124# define FMT_HAS_CXX11_NOEXCEPT 0
125#endif
126
127#ifndef FMT_NOEXCEPT
128# if FMT_EXCEPTIONS || FMT_HAS_CXX11_NOEXCEPT
129# define FMT_NOEXCEPT FMT_DETECTED_NOEXCEPT
130# else
131# define FMT_NOEXCEPT
132# endif
133#endif
134
135// [[noreturn]] is disabled on MSVC and NVCC because of bogus unreachable code
136// warnings.
137#if FMT_EXCEPTIONS && FMT_HAS_CPP_ATTRIBUTE(noreturn) && !FMT_MSC_VER && \
138 !FMT_NVCC
139# define FMT_NORETURN [[noreturn]]
140#else
141# define FMT_NORETURN
142#endif
143
144#ifndef FMT_MAYBE_UNUSED
145# if FMT_HAS_CPP17_ATTRIBUTE(maybe_unused)
146# define FMT_MAYBE_UNUSED [[maybe_unused]]
147# else
148# define FMT_MAYBE_UNUSED
149# endif
150#endif
151
152#ifndef FMT_DEPRECATED
153# if FMT_HAS_CPP14_ATTRIBUTE(deprecated) || FMT_MSC_VER >= 1900
154# define FMT_DEPRECATED [[deprecated]]
155# else
156# if defined(__GNUC__) || defined(__clang__)
157# define FMT_DEPRECATED __attribute__((deprecated))
158# elif FMT_MSC_VER
159# define FMT_DEPRECATED __declspec(deprecated)
160# else
161# define FMT_DEPRECATED /* deprecated */
162# endif
163# endif
164#endif
165
166// Workaround broken [[deprecated]] in the Intel, PGI and NVCC compilers.
167#if defined(__INTEL_COMPILER) || defined(__PGI) || FMT_NVCC
168# define FMT_DEPRECATED_ALIAS
169#else
170# define FMT_DEPRECATED_ALIAS FMT_DEPRECATED
171#endif
172
173#ifndef FMT_BEGIN_NAMESPACE
174# if FMT_HAS_FEATURE(cxx_inline_namespaces) || FMT_GCC_VERSION >= 404 || \
175 FMT_MSC_VER >= 1900
176# define FMT_INLINE_NAMESPACE inline namespace
177# define FMT_END_NAMESPACE \
178 } \
179 }
180# else
181# define FMT_INLINE_NAMESPACE namespace
182# define FMT_END_NAMESPACE \
183 } \
184 using namespace v6; \
185 }
186# endif
187# define FMT_BEGIN_NAMESPACE \
188 namespace fmt { \
189 FMT_INLINE_NAMESPACE v6 {
190#endif
191
192#if !defined(FMT_HEADER_ONLY) && defined(_WIN32)
193# if FMT_MSC_VER
194# define FMT_NO_W4275 __pragma(warning(suppress : 4275))
195# else
196# define FMT_NO_W4275
197# endif
198# define FMT_CLASS_API FMT_NO_W4275
199# ifdef FMT_EXPORT
200# define FMT_API __declspec(dllexport)
201# elif defined(FMT_SHARED)
202# define FMT_API __declspec(dllimport)
203# define FMT_EXTERN_TEMPLATE_API FMT_API
204# endif
205#endif
206#ifndef FMT_CLASS_API
207# define FMT_CLASS_API
208#endif
209#ifndef FMT_API
210# if FMT_GCC_VERSION || FMT_CLANG_VERSION
211# define FMT_API __attribute__((visibility("default")))
212# define FMT_EXTERN_TEMPLATE_API FMT_API
213# define FMT_INSTANTIATION_DEF_API
214# else
215# define FMT_API
216# endif
217#endif
218#ifndef FMT_EXTERN_TEMPLATE_API
219# define FMT_EXTERN_TEMPLATE_API
220#endif
221#ifndef FMT_INSTANTIATION_DEF_API
222# define FMT_INSTANTIATION_DEF_API FMT_API
223#endif
224
225#ifndef FMT_HEADER_ONLY
226# define FMT_EXTERN extern
227#else
228# define FMT_EXTERN
229#endif
230
231// libc++ supports string_view in pre-c++17.
232#if (FMT_HAS_INCLUDE(<string_view>) && \
233 (__cplusplus > 201402L || defined(_LIBCPP_VERSION))) || \
234 (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
235# include <string_view>
236# define FMT_USE_STRING_VIEW
237#elif FMT_HAS_INCLUDE("experimental/string_view") && __cplusplus >= 201402L
238# include <experimental/string_view>
239# define FMT_USE_EXPERIMENTAL_STRING_VIEW
240#endif
241
242#ifndef FMT_UNICODE
243# define FMT_UNICODE !FMT_MSC_VER
244#endif
245#if FMT_UNICODE && FMT_MSC_VER
246# pragma execution_character_set("utf-8")
247#endif
248
249FMT_BEGIN_NAMESPACE
250
251// Implementations of enable_if_t and other metafunctions for older systems.
252template <bool B, class T = void>
253using enable_if_t = typename std::enable_if<B, T>::type;
254template <bool B, class T, class F>
255using conditional_t = typename std::conditional<B, T, F>::type;
256template <bool B> using bool_constant = std::integral_constant<bool, B>;
257template <typename T>
258using remove_reference_t = typename std::remove_reference<T>::type;
259template <typename T>
260using remove_const_t = typename std::remove_const<T>::type;
261template <typename T>
262using remove_cvref_t = typename std::remove_cv<remove_reference_t<T>>::type;
263template <typename T> struct type_identity { using type = T; };
264template <typename T> using type_identity_t = typename type_identity<T>::type;
265
266struct monostate {};
267
268// An enable_if helper to be used in template parameters which results in much
269// shorter symbols: https://godbolt.org/z/sWw4vP. Extra parentheses are needed
270// to workaround a bug in MSVC 2019 (see #1140 and #1186).
271#define FMT_ENABLE_IF(...) enable_if_t<(__VA_ARGS__), int> = 0
272
273namespace internal {
274
275// A helper function to suppress bogus "conditional expression is constant"
276// warnings.
277template <typename T> FMT_CONSTEXPR T const_check(T value) { return value; }
278
279// A workaround for gcc 4.8 to make void_t work in a SFINAE context.
280template <typename... Ts> struct void_t_impl { using type = void; };
281
282FMT_NORETURN FMT_API void assert_fail(const char* file, int line,
283 const char* message);
284
285#ifndef FMT_ASSERT
286# ifdef NDEBUG
287// FMT_ASSERT is not empty to avoid -Werror=empty-body.
288# define FMT_ASSERT(condition, message) ((void)0)
289# else
290# define FMT_ASSERT(condition, message) \
291 ((condition) /* void() fails with -Winvalid-constexpr on clang 4.0.1 */ \
292 ? (void)0 \
293 : ::fmt::internal::assert_fail(__FILE__, __LINE__, (message)))
294# endif
295#endif
296
297#if defined(FMT_USE_STRING_VIEW)
298template <typename Char> using std_string_view = std::basic_string_view<Char>;
299#elif defined(FMT_USE_EXPERIMENTAL_STRING_VIEW)
300template <typename Char>
301using std_string_view = std::experimental::basic_string_view<Char>;
302#else
303template <typename T> struct std_string_view {};
304#endif
305
306#ifdef FMT_USE_INT128
307// Do nothing.
308#elif defined(__SIZEOF_INT128__) && !FMT_NVCC
309# define FMT_USE_INT128 1
310using int128_t = __int128_t;
311using uint128_t = __uint128_t;
312#else
313# define FMT_USE_INT128 0
314#endif
315#if !FMT_USE_INT128
316struct int128_t {};
317struct uint128_t {};
318#endif
319
320// Casts a nonnegative integer to unsigned.
321template <typename Int>
322FMT_CONSTEXPR typename std::make_unsigned<Int>::type to_unsigned(Int value) {
323 FMT_ASSERT(value >= 0, "negative value");
324 return static_cast<typename std::make_unsigned<Int>::type>(value);
325}
326
327constexpr unsigned char micro[] = "\u00B5";
328
329template <typename Char> constexpr bool is_unicode() {
330 return FMT_UNICODE || sizeof(Char) != 1 ||
331 (sizeof(micro) == 3 && micro[0] == 0xC2 && micro[1] == 0xB5);
332}
333
334#ifdef __cpp_char8_t
335using char8_type = char8_t;
336#else
337enum char8_type : unsigned char {};
338#endif
339} // namespace internal
340
341template <typename... Ts>
342using void_t = typename internal::void_t_impl<Ts...>::type;
343
351template <typename Char> class basic_string_view {
352 private:
353 const Char* data_;
354 size_t size_;
355
356 public:
357 using char_type FMT_DEPRECATED_ALIAS = Char;
358 using value_type = Char;
359 using iterator = const Char*;
360
361 FMT_CONSTEXPR basic_string_view() FMT_NOEXCEPT : data_(nullptr), size_(0) {}
362
364 FMT_CONSTEXPR basic_string_view(const Char* s, size_t count) FMT_NOEXCEPT
365 : data_(s),
366 size_(count) {}
367
374#if __cplusplus >= 201703L // C++17's char_traits::length() is constexpr.
375 FMT_CONSTEXPR
376#endif
377 basic_string_view(const Char* s)
378 : data_(s), size_(std::char_traits<Char>::length(s)) {}
379
381 template <typename Traits, typename Alloc>
382 FMT_CONSTEXPR basic_string_view(
383 const std::basic_string<Char, Traits, Alloc>& s) FMT_NOEXCEPT
384 : data_(s.data()),
385 size_(s.size()) {}
386
387 template <
388 typename S,
389 FMT_ENABLE_IF(std::is_same<S, internal::std_string_view<Char>>::value)>
390 FMT_CONSTEXPR basic_string_view(S s) FMT_NOEXCEPT : data_(s.data()),
391 size_(s.size()) {}
392
394 FMT_CONSTEXPR const Char* data() const { return data_; }
395
397 FMT_CONSTEXPR size_t size() const { return size_; }
398
399 FMT_CONSTEXPR iterator begin() const { return data_; }
400 FMT_CONSTEXPR iterator end() const { return data_ + size_; }
401
402 FMT_CONSTEXPR const Char& operator[](size_t pos) const { return data_[pos]; }
403
404 FMT_CONSTEXPR void remove_prefix(size_t n) {
405 data_ += n;
406 size_ -= n;
407 }
408
409 // Lexicographically compare this string reference to other.
410 int compare(basic_string_view other) const {
411 size_t str_size = size_ < other.size_ ? size_ : other.size_;
412 int result = std::char_traits<Char>::compare(data_, other.data_, str_size);
413 if (result == 0)
414 result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1);
415 return result;
416 }
417
418 friend bool operator==(basic_string_view lhs, basic_string_view rhs) {
419 return lhs.compare(rhs) == 0;
420 }
421 friend bool operator!=(basic_string_view lhs, basic_string_view rhs) {
422 return lhs.compare(rhs) != 0;
423 }
424 friend bool operator<(basic_string_view lhs, basic_string_view rhs) {
425 return lhs.compare(rhs) < 0;
426 }
427 friend bool operator<=(basic_string_view lhs, basic_string_view rhs) {
428 return lhs.compare(rhs) <= 0;
429 }
430 friend bool operator>(basic_string_view lhs, basic_string_view rhs) {
431 return lhs.compare(rhs) > 0;
432 }
433 friend bool operator>=(basic_string_view lhs, basic_string_view rhs) {
434 return lhs.compare(rhs) >= 0;
435 }
436};
437
440
441#ifndef __cpp_char8_t
442// char8_t is deprecated; use char instead.
443using char8_t FMT_DEPRECATED_ALIAS = internal::char8_type;
444#endif
445
447template <typename T> struct is_char : std::false_type {};
448template <> struct is_char<char> : std::true_type {};
449template <> struct is_char<wchar_t> : std::true_type {};
450template <> struct is_char<internal::char8_type> : std::true_type {};
451template <> struct is_char<char16_t> : std::true_type {};
452template <> struct is_char<char32_t> : std::true_type {};
453
470template <typename Char, FMT_ENABLE_IF(is_char<Char>::value)>
471inline basic_string_view<Char> to_string_view(const Char* s) {
472 return s;
473}
474
475template <typename Char, typename Traits, typename Alloc>
476inline basic_string_view<Char> to_string_view(
477 const std::basic_string<Char, Traits, Alloc>& s) {
478 return s;
479}
480
481template <typename Char>
482inline basic_string_view<Char> to_string_view(basic_string_view<Char> s) {
483 return s;
484}
485
486template <typename Char,
487 FMT_ENABLE_IF(!std::is_empty<internal::std_string_view<Char>>::value)>
488inline basic_string_view<Char> to_string_view(
489 internal::std_string_view<Char> s) {
490 return s;
491}
492
493// A base class for compile-time strings. It is defined in the fmt namespace to
494// make formatting functions visible via ADL, e.g. format(fmt("{}"), 42).
495struct compile_string {};
496
497template <typename S>
498struct is_compile_string : std::is_base_of<compile_string, S> {};
499
500template <typename S, FMT_ENABLE_IF(is_compile_string<S>::value)>
501constexpr basic_string_view<typename S::char_type> to_string_view(const S& s) {
502 return s;
503}
504
505namespace internal {
506void to_string_view(...);
507using fmt::v6::to_string_view;
508
509// Specifies whether S is a string type convertible to fmt::basic_string_view.
510// It should be a constexpr function but MSVC 2017 fails to compile it in
511// enable_if and MSVC 2015 fails to compile it as an alias template.
512template <typename S>
513struct is_string : std::is_class<decltype(to_string_view(std::declval<S>()))> {
514};
515
516template <typename S, typename = void> struct char_t_impl {};
517template <typename S> struct char_t_impl<S, enable_if_t<is_string<S>::value>> {
518 using result = decltype(to_string_view(std::declval<S>()));
519 using type = typename result::value_type;
520};
521
522struct error_handler {
523 FMT_CONSTEXPR error_handler() = default;
524 FMT_CONSTEXPR error_handler(const error_handler&) = default;
525
526 // This function is intentionally not constexpr to give a compile-time error.
527 FMT_NORETURN FMT_API void on_error(const char* message);
528};
529} // namespace internal
530
532template <typename S> using char_t = typename internal::char_t_impl<S>::type;
533
550template <typename Char, typename ErrorHandler = internal::error_handler>
551class basic_format_parse_context : private ErrorHandler {
552 private:
553 basic_string_view<Char> format_str_;
554 int next_arg_id_;
555
556 public:
557 using char_type = Char;
558 using iterator = typename basic_string_view<Char>::iterator;
559
560 explicit FMT_CONSTEXPR basic_format_parse_context(
561 basic_string_view<Char> format_str, ErrorHandler eh = ErrorHandler())
562 : ErrorHandler(eh), format_str_(format_str), next_arg_id_(0) {}
563
568 FMT_CONSTEXPR iterator begin() const FMT_NOEXCEPT {
569 return format_str_.begin();
570 }
571
575 FMT_CONSTEXPR iterator end() const FMT_NOEXCEPT { return format_str_.end(); }
576
578 FMT_CONSTEXPR void advance_to(iterator it) {
579 format_str_.remove_prefix(internal::to_unsigned(it - begin()));
580 }
581
586 FMT_CONSTEXPR int next_arg_id() {
587 if (next_arg_id_ >= 0) return next_arg_id_++;
588 on_error("cannot switch from manual to automatic argument indexing");
589 return 0;
590 }
591
596 FMT_CONSTEXPR void check_arg_id(int) {
597 if (next_arg_id_ > 0)
598 on_error("cannot switch from automatic to manual argument indexing");
599 else
600 next_arg_id_ = -1;
601 }
602
603 FMT_CONSTEXPR void check_arg_id(basic_string_view<Char>) {}
604
605 FMT_CONSTEXPR void on_error(const char* message) {
606 ErrorHandler::on_error(message);
607 }
608
609 FMT_CONSTEXPR ErrorHandler error_handler() const { return *this; }
610};
611
614
615template <typename Char, typename ErrorHandler = internal::error_handler>
616using basic_parse_context FMT_DEPRECATED_ALIAS =
618using parse_context FMT_DEPRECATED_ALIAS = basic_format_parse_context<char>;
619using wparse_context FMT_DEPRECATED_ALIAS = basic_format_parse_context<wchar_t>;
620
621template <typename Context> class basic_format_arg;
622template <typename Context> class basic_format_args;
623
624// A formatter for objects of type T.
625template <typename T, typename Char = char, typename Enable = void>
626struct formatter {
627 // A deleted default constructor indicates a disabled formatter.
628 formatter() = delete;
629};
630
631template <typename T, typename Char, typename Enable = void>
632struct FMT_DEPRECATED convert_to_int
633 : bool_constant<!std::is_arithmetic<T>::value &&
634 std::is_convertible<T, int>::value> {};
635
636// Specifies if T has an enabled formatter specialization. A type can be
637// formattable even if it doesn't have a formatter e.g. via a conversion.
638template <typename T, typename Context>
639using has_formatter =
640 std::is_constructible<typename Context::template formatter_type<T>>;
641
642namespace internal {
643
645template <typename T> class buffer {
646 private:
647 T* ptr_;
648 std::size_t size_;
649 std::size_t capacity_;
650
651 protected:
652 // Don't initialize ptr_ since it is not accessed to save a few cycles.
653 buffer(std::size_t sz) FMT_NOEXCEPT : size_(sz), capacity_(sz) {}
654
655 buffer(T* p = nullptr, std::size_t sz = 0, std::size_t cap = 0) FMT_NOEXCEPT
656 : ptr_(p),
657 size_(sz),
658 capacity_(cap) {}
659
661 void set(T* buf_data, std::size_t buf_capacity) FMT_NOEXCEPT {
662 ptr_ = buf_data;
663 capacity_ = buf_capacity;
664 }
665
667 virtual void grow(std::size_t capacity) = 0;
668
669 public:
670 using value_type = T;
671 using const_reference = const T&;
672
673 buffer(const buffer&) = delete;
674 void operator=(const buffer&) = delete;
675 virtual ~buffer() = default;
676
677 T* begin() FMT_NOEXCEPT { return ptr_; }
678 T* end() FMT_NOEXCEPT { return ptr_ + size_; }
679
680 const T* begin() const FMT_NOEXCEPT { return ptr_; }
681 const T* end() const FMT_NOEXCEPT { return ptr_ + size_; }
682
684 std::size_t size() const FMT_NOEXCEPT { return size_; }
685
687 std::size_t capacity() const FMT_NOEXCEPT { return capacity_; }
688
690 T* data() FMT_NOEXCEPT { return ptr_; }
691
693 const T* data() const FMT_NOEXCEPT { return ptr_; }
694
698 void resize(std::size_t new_size) {
699 reserve(new_size);
700 size_ = new_size;
701 }
702
704 void clear() { size_ = 0; }
705
707 void reserve(std::size_t new_capacity) {
708 if (new_capacity > capacity_) grow(new_capacity);
709 }
710
711 void push_back(const T& value) {
712 reserve(size_ + 1);
713 ptr_[size_++] = value;
714 }
715
717 template <typename U> void append(const U* begin, const U* end);
718
719 template <typename I> T& operator[](I index) { return ptr_[index]; }
720 template <typename I> const T& operator[](I index) const {
721 return ptr_[index];
722 }
723};
724
725// A container-backed buffer.
726template <typename Container>
727class container_buffer : public buffer<typename Container::value_type> {
728 private:
729 Container& container_;
730
731 protected:
732 void grow(std::size_t capacity) FMT_OVERRIDE {
733 container_.resize(capacity);
734 this->set(&container_[0], capacity);
735 }
736
737 public:
738 explicit container_buffer(Container& c)
739 : buffer<typename Container::value_type>(c.size()), container_(c) {}
740};
741
742// Extracts a reference to the container from back_insert_iterator.
743template <typename Container>
744inline Container& get_container(std::back_insert_iterator<Container> it) {
745 using bi_iterator = std::back_insert_iterator<Container>;
746 struct accessor : bi_iterator {
747 accessor(bi_iterator iter) : bi_iterator(iter) {}
748 using bi_iterator::container;
749 };
750 return *accessor(it).container;
751}
752
753template <typename T, typename Char = char, typename Enable = void>
754struct fallback_formatter {
755 fallback_formatter() = delete;
756};
757
758// Specifies if T has an enabled fallback_formatter specialization.
759template <typename T, typename Context>
760using has_fallback_formatter =
761 std::is_constructible<fallback_formatter<T, typename Context::char_type>>;
762
763template <typename Char> struct named_arg_base;
764template <typename T, typename Char> struct named_arg;
765
766enum class type {
767 none_type,
768 named_arg_type,
769 // Integer types should go first,
770 int_type,
771 uint_type,
772 long_long_type,
773 ulong_long_type,
774 int128_type,
775 uint128_type,
776 bool_type,
777 char_type,
778 last_integer_type = char_type,
779 // followed by floating-point types.
780 float_type,
781 double_type,
782 long_double_type,
783 last_numeric_type = long_double_type,
784 cstring_type,
785 string_type,
786 pointer_type,
787 custom_type
788};
789
790// Maps core type T to the corresponding type enum constant.
791template <typename T, typename Char>
792struct type_constant : std::integral_constant<type, type::custom_type> {};
793
794#define FMT_TYPE_CONSTANT(Type, constant) \
795 template <typename Char> \
796 struct type_constant<Type, Char> \
797 : std::integral_constant<type, type::constant> {}
798
799FMT_TYPE_CONSTANT(const named_arg_base<Char>&, named_arg_type);
800FMT_TYPE_CONSTANT(int, int_type);
801FMT_TYPE_CONSTANT(unsigned, uint_type);
802FMT_TYPE_CONSTANT(long long, long_long_type);
803FMT_TYPE_CONSTANT(unsigned long long, ulong_long_type);
804FMT_TYPE_CONSTANT(int128_t, int128_type);
805FMT_TYPE_CONSTANT(uint128_t, uint128_type);
806FMT_TYPE_CONSTANT(bool, bool_type);
807FMT_TYPE_CONSTANT(Char, char_type);
808FMT_TYPE_CONSTANT(float, float_type);
809FMT_TYPE_CONSTANT(double, double_type);
810FMT_TYPE_CONSTANT(long double, long_double_type);
811FMT_TYPE_CONSTANT(const Char*, cstring_type);
812FMT_TYPE_CONSTANT(basic_string_view<Char>, string_type);
813FMT_TYPE_CONSTANT(const void*, pointer_type);
814
815FMT_CONSTEXPR bool is_integral_type(type t) {
816 FMT_ASSERT(t != type::named_arg_type, "invalid argument type");
817 return t > type::none_type && t <= type::last_integer_type;
818}
819
820FMT_CONSTEXPR bool is_arithmetic_type(type t) {
821 FMT_ASSERT(t != type::named_arg_type, "invalid argument type");
822 return t > type::none_type && t <= type::last_numeric_type;
823}
824
825template <typename Char> struct string_value {
826 const Char* data;
827 std::size_t size;
828};
829
830template <typename Context> struct custom_value {
832 const void* value;
833 void (*format)(const void* arg,
834 typename Context::parse_context_type& parse_ctx, Context& ctx);
835};
836
837// A formatting argument value.
838template <typename Context> class value {
839 public:
840 using char_type = typename Context::char_type;
841
842 union {
843 int int_value;
844 unsigned uint_value;
845 long long long_long_value;
846 unsigned long long ulong_long_value;
847 int128_t int128_value;
848 uint128_t uint128_value;
849 bool bool_value;
850 char_type char_value;
851 float float_value;
852 double double_value;
853 long double long_double_value;
854 const void* pointer;
855 string_value<char_type> string;
856 custom_value<Context> custom;
857 const named_arg_base<char_type>* named_arg;
858 };
859
860 FMT_CONSTEXPR value(int val = 0) : int_value(val) {}
861 FMT_CONSTEXPR value(unsigned val) : uint_value(val) {}
862 value(long long val) : long_long_value(val) {}
863 value(unsigned long long val) : ulong_long_value(val) {}
864 value(int128_t val) : int128_value(val) {}
865 value(uint128_t val) : uint128_value(val) {}
866 value(float val) : float_value(val) {}
867 value(double val) : double_value(val) {}
868 value(long double val) : long_double_value(val) {}
869 value(bool val) : bool_value(val) {}
870 value(char_type val) : char_value(val) {}
871 value(const char_type* val) { string.data = val; }
873 string.data = val.data();
874 string.size = val.size();
875 }
876 value(const void* val) : pointer(val) {}
877
878 template <typename T> value(const T& val) {
879 custom.value = &val;
880 // Get the formatter type through the context to allow different contexts
881 // have different extension points, e.g. `formatter<T>` for `format` and
882 // `printf_formatter<T>` for `printf`.
883 custom.format = format_custom_arg<
884 T, conditional_t<has_formatter<T, Context>::value,
885 typename Context::template formatter_type<T>,
886 fallback_formatter<T, char_type>>>;
887 }
888
889 value(const named_arg_base<char_type>& val) { named_arg = &val; }
890
891 private:
892 // Formats an argument of a custom type, such as a user-defined class.
893 template <typename T, typename Formatter>
894 static void format_custom_arg(const void* arg,
895 typename Context::parse_context_type& parse_ctx,
896 Context& ctx) {
897 Formatter f;
898 parse_ctx.advance_to(f.parse(parse_ctx));
899 ctx.advance_to(f.format(*static_cast<const T*>(arg), ctx));
900 }
901};
902
903template <typename Context, typename T>
904FMT_CONSTEXPR basic_format_arg<Context> make_arg(const T& value);
905
906// To minimize the number of types we need to deal with, long is translated
907// either to int or to long long depending on its size.
908enum { long_short = sizeof(long) == sizeof(int) };
909using long_type = conditional_t<long_short, int, long long>;
910using ulong_type = conditional_t<long_short, unsigned, unsigned long long>;
911
912// Maps formatting arguments to core types.
913template <typename Context> struct arg_mapper {
914 using char_type = typename Context::char_type;
915
916 FMT_CONSTEXPR int map(signed char val) { return val; }
917 FMT_CONSTEXPR unsigned map(unsigned char val) { return val; }
918 FMT_CONSTEXPR int map(short val) { return val; }
919 FMT_CONSTEXPR unsigned map(unsigned short val) { return val; }
920 FMT_CONSTEXPR int map(int val) { return val; }
921 FMT_CONSTEXPR unsigned map(unsigned val) { return val; }
922 FMT_CONSTEXPR long_type map(long val) { return val; }
923 FMT_CONSTEXPR ulong_type map(unsigned long val) { return val; }
924 FMT_CONSTEXPR long long map(long long val) { return val; }
925 FMT_CONSTEXPR unsigned long long map(unsigned long long val) { return val; }
926 FMT_CONSTEXPR int128_t map(int128_t val) { return val; }
927 FMT_CONSTEXPR uint128_t map(uint128_t val) { return val; }
928 FMT_CONSTEXPR bool map(bool val) { return val; }
929
930 template <typename T, FMT_ENABLE_IF(is_char<T>::value)>
931 FMT_CONSTEXPR char_type map(T val) {
932 static_assert(
933 std::is_same<T, char>::value || std::is_same<T, char_type>::value,
934 "mixing character types is disallowed");
935 return val;
936 }
937
938 FMT_CONSTEXPR float map(float val) { return val; }
939 FMT_CONSTEXPR double map(double val) { return val; }
940 FMT_CONSTEXPR long double map(long double val) { return val; }
941
942 FMT_CONSTEXPR const char_type* map(char_type* val) { return val; }
943 FMT_CONSTEXPR const char_type* map(const char_type* val) { return val; }
944 template <typename T, FMT_ENABLE_IF(is_string<T>::value)>
945 FMT_CONSTEXPR basic_string_view<char_type> map(const T& val) {
946 static_assert(std::is_same<char_type, char_t<T>>::value,
947 "mixing character types is disallowed");
948 return to_string_view(val);
949 }
950 template <typename T,
951 FMT_ENABLE_IF(
952 std::is_constructible<basic_string_view<char_type>, T>::value &&
953 !is_string<T>::value && !has_formatter<T, Context>::value &&
954 !has_fallback_formatter<T, Context>::value)>
955 FMT_CONSTEXPR basic_string_view<char_type> map(const T& val) {
957 }
958 template <
959 typename T,
960 FMT_ENABLE_IF(
961 std::is_constructible<std_string_view<char_type>, T>::value &&
962 !std::is_constructible<basic_string_view<char_type>, T>::value &&
963 !is_string<T>::value && !has_formatter<T, Context>::value &&
964 !has_fallback_formatter<T, Context>::value)>
965 FMT_CONSTEXPR basic_string_view<char_type> map(const T& val) {
966 return std_string_view<char_type>(val);
967 }
968 FMT_CONSTEXPR const char* map(const signed char* val) {
969 static_assert(std::is_same<char_type, char>::value, "invalid string type");
970 return reinterpret_cast<const char*>(val);
971 }
972 FMT_CONSTEXPR const char* map(const unsigned char* val) {
973 static_assert(std::is_same<char_type, char>::value, "invalid string type");
974 return reinterpret_cast<const char*>(val);
975 }
976
977 FMT_CONSTEXPR const void* map(void* val) { return val; }
978 FMT_CONSTEXPR const void* map(const void* val) { return val; }
979 FMT_CONSTEXPR const void* map(std::nullptr_t val) { return val; }
980 template <typename T> FMT_CONSTEXPR int map(const T*) {
981 // Formatting of arbitrary pointers is disallowed. If you want to output
982 // a pointer cast it to "void *" or "const void *". In particular, this
983 // forbids formatting of "[const] volatile char *" which is printed as bool
984 // by iostreams.
985 static_assert(!sizeof(T), "formatting of non-void pointers is disallowed");
986 return 0;
987 }
988
989 template <typename T,
990 FMT_ENABLE_IF(std::is_enum<T>::value &&
991 !has_formatter<T, Context>::value &&
992 !has_fallback_formatter<T, Context>::value)>
993 FMT_CONSTEXPR auto map(const T& val)
994 -> decltype(std::declval<arg_mapper>().map(
995 static_cast<typename std::underlying_type<T>::type>(val))) {
996 return map(static_cast<typename std::underlying_type<T>::type>(val));
997 }
998 template <typename T,
999 FMT_ENABLE_IF(!is_string<T>::value && !is_char<T>::value &&
1000 (has_formatter<T, Context>::value ||
1001 has_fallback_formatter<T, Context>::value))>
1002 FMT_CONSTEXPR const T& map(const T& val) {
1003 return val;
1004 }
1005
1006 template <typename T>
1007 FMT_CONSTEXPR const named_arg_base<char_type>& map(
1008 const named_arg<T, char_type>& val) {
1009 auto arg = make_arg<Context>(val.value);
1010 std::memcpy(val.data, &arg, sizeof(arg));
1011 return val;
1012 }
1013
1014 int map(...) {
1015 constexpr bool formattable = sizeof(Context) == 0;
1016 static_assert(
1017 formattable,
1018 "Cannot format argument. To make type T formattable provide a "
1019 "formatter<T> specialization: "
1020 "https://fmt.dev/latest/api.html#formatting-user-defined-types");
1021 return 0;
1022 }
1023};
1024
1025// A type constant after applying arg_mapper<Context>.
1026template <typename T, typename Context>
1027using mapped_type_constant =
1028 type_constant<decltype(arg_mapper<Context>().map(std::declval<const T&>())),
1029 typename Context::char_type>;
1030
1031enum { packed_arg_bits = 5 };
1032// Maximum number of arguments with packed types.
1033enum { max_packed_args = 63 / packed_arg_bits };
1034enum : unsigned long long { is_unpacked_bit = 1ULL << 63 };
1035
1036template <typename Context> class arg_map;
1037} // namespace internal
1038
1039// A formatting argument. It is a trivially copyable/constructible type to
1040// allow storage in basic_memory_buffer.
1041template <typename Context> class basic_format_arg {
1042 private:
1043 internal::value<Context> value_;
1044 internal::type type_;
1045
1046 template <typename ContextType, typename T>
1047 friend FMT_CONSTEXPR basic_format_arg<ContextType> internal::make_arg(
1048 const T& value);
1049
1050 template <typename Visitor, typename Ctx>
1051 friend FMT_CONSTEXPR auto visit_format_arg(Visitor&& vis,
1052 const basic_format_arg<Ctx>& arg)
1053 -> decltype(vis(0));
1054
1055 friend class basic_format_args<Context>;
1056 friend class internal::arg_map<Context>;
1057
1058 using char_type = typename Context::char_type;
1059
1060 public:
1061 class handle {
1062 public:
1063 explicit handle(internal::custom_value<Context> custom) : custom_(custom) {}
1064
1065 void format(typename Context::parse_context_type& parse_ctx,
1066 Context& ctx) const {
1067 custom_.format(custom_.value, parse_ctx, ctx);
1068 }
1069
1070 private:
1071 internal::custom_value<Context> custom_;
1072 };
1073
1074 FMT_CONSTEXPR basic_format_arg() : type_(internal::type::none_type) {}
1075
1076 FMT_CONSTEXPR explicit operator bool() const FMT_NOEXCEPT {
1077 return type_ != internal::type::none_type;
1078 }
1079
1080 internal::type type() const { return type_; }
1081
1082 bool is_integral() const { return internal::is_integral_type(type_); }
1083 bool is_arithmetic() const { return internal::is_arithmetic_type(type_); }
1084};
1085
1093template <typename Visitor, typename Context>
1094FMT_CONSTEXPR auto visit_format_arg(Visitor&& vis,
1095 const basic_format_arg<Context>& arg)
1096 -> decltype(vis(0)) {
1097 using char_type = typename Context::char_type;
1098 switch (arg.type_) {
1099 case internal::type::none_type:
1100 break;
1101 case internal::type::named_arg_type:
1102 FMT_ASSERT(false, "invalid argument type");
1103 break;
1104 case internal::type::int_type:
1105 return vis(arg.value_.int_value);
1106 case internal::type::uint_type:
1107 return vis(arg.value_.uint_value);
1108 case internal::type::long_long_type:
1109 return vis(arg.value_.long_long_value);
1110 case internal::type::ulong_long_type:
1111 return vis(arg.value_.ulong_long_value);
1112#if FMT_USE_INT128
1113 case internal::type::int128_type:
1114 return vis(arg.value_.int128_value);
1115 case internal::type::uint128_type:
1116 return vis(arg.value_.uint128_value);
1117#else
1118 case internal::type::int128_type:
1119 case internal::type::uint128_type:
1120 break;
1121#endif
1122 case internal::type::bool_type:
1123 return vis(arg.value_.bool_value);
1124 case internal::type::char_type:
1125 return vis(arg.value_.char_value);
1126 case internal::type::float_type:
1127 return vis(arg.value_.float_value);
1128 case internal::type::double_type:
1129 return vis(arg.value_.double_value);
1130 case internal::type::long_double_type:
1131 return vis(arg.value_.long_double_value);
1132 case internal::type::cstring_type:
1133 return vis(arg.value_.string.data);
1134 case internal::type::string_type:
1135 return vis(basic_string_view<char_type>(arg.value_.string.data,
1136 arg.value_.string.size));
1137 case internal::type::pointer_type:
1138 return vis(arg.value_.pointer);
1139 case internal::type::custom_type:
1140 return vis(typename basic_format_arg<Context>::handle(arg.value_.custom));
1141 }
1142 return vis(monostate());
1143}
1144
1145namespace internal {
1146// A map from argument names to their values for named arguments.
1147template <typename Context> class arg_map {
1148 private:
1149 using char_type = typename Context::char_type;
1150
1151 struct entry {
1153 basic_format_arg<Context> arg;
1154 };
1155
1156 entry* map_;
1157 unsigned size_;
1158
1159 void push_back(value<Context> val) {
1160 const auto& named = *val.named_arg;
1161 map_[size_] = {named.name, named.template deserialize<Context>()};
1162 ++size_;
1163 }
1164
1165 public:
1166 arg_map(const arg_map&) = delete;
1167 void operator=(const arg_map&) = delete;
1168 arg_map() : map_(nullptr), size_(0) {}
1169 void init(const basic_format_args<Context>& args);
1170 ~arg_map() { delete[] map_; }
1171
1172 basic_format_arg<Context> find(basic_string_view<char_type> name) const {
1173 // The list is unsorted, so just return the first matching name.
1174 for (entry *it = map_, *end = map_ + size_; it != end; ++it) {
1175 if (it->name == name) return it->arg;
1176 }
1177 return {};
1178 }
1179};
1180
1181// A type-erased reference to an std::locale to avoid heavy <locale> include.
1182class locale_ref {
1183 private:
1184 const void* locale_; // A type-erased pointer to std::locale.
1185
1186 public:
1187 locale_ref() : locale_(nullptr) {}
1188 template <typename Locale> explicit locale_ref(const Locale& loc);
1189
1190 explicit operator bool() const FMT_NOEXCEPT { return locale_ != nullptr; }
1191
1192 template <typename Locale> Locale get() const;
1193};
1194
1195template <typename> constexpr unsigned long long encode_types() { return 0; }
1196
1197template <typename Context, typename Arg, typename... Args>
1198constexpr unsigned long long encode_types() {
1199 return static_cast<unsigned>(mapped_type_constant<Arg, Context>::value) |
1200 (encode_types<Context, Args...>() << packed_arg_bits);
1201}
1202
1203template <typename Context, typename T>
1204FMT_CONSTEXPR basic_format_arg<Context> make_arg(const T& value) {
1205 basic_format_arg<Context> arg;
1206 arg.type_ = mapped_type_constant<T, Context>::value;
1207 arg.value_ = arg_mapper<Context>().map(value);
1208 return arg;
1209}
1210
1211// The type template parameter is there to avoid an ODR violation when using
1212// a fallback formatter in one translation unit and an implicit conversion in
1213// another (not recommended).
1214template <bool IS_PACKED, typename Context, type, typename T,
1215 FMT_ENABLE_IF(IS_PACKED)>
1216inline value<Context> make_arg(const T& val) {
1217 return arg_mapper<Context>().map(val);
1218}
1219
1220template <bool IS_PACKED, typename Context, type, typename T,
1221 FMT_ENABLE_IF(!IS_PACKED)>
1222inline basic_format_arg<Context> make_arg(const T& value) {
1223 return make_arg<Context>(value);
1224}
1225
1226template <typename T> struct is_reference_wrapper : std::false_type {};
1227
1228template <typename T>
1229struct is_reference_wrapper<std::reference_wrapper<T>> : std::true_type {};
1230
1231class dynamic_arg_list {
1232 // Workaround for clang's -Wweak-vtables. Unlike for regular classes, for
1233 // templates it doesn't complain about inability to deduce single translation
1234 // unit for placing vtable. So storage_node_base is made a fake template.
1235 template <typename = void> struct node {
1236 virtual ~node() = default;
1237 std::unique_ptr<node<>> next;
1238 };
1239
1240 template <typename T> struct typed_node : node<> {
1241 T value;
1242
1243 template <typename Arg>
1244 FMT_CONSTEXPR typed_node(const Arg& arg) : value(arg) {}
1245
1246 template <typename Char>
1247 FMT_CONSTEXPR typed_node(const basic_string_view<Char>& arg)
1248 : value(arg.data(), arg.size()) {}
1249 };
1250
1251 std::unique_ptr<node<>> head_;
1252
1253 public:
1254 template <typename T, typename Arg> const T& push(const Arg& arg) {
1255 auto node = std::unique_ptr<typed_node<T>>(new typed_node<T>(arg));
1256 auto& value = node->value;
1257 node->next = std::move(head_);
1258 head_ = std::move(node);
1259 return value;
1260 }
1261};
1262} // namespace internal
1263
1264// Formatting context.
1265template <typename OutputIt, typename Char> class basic_format_context {
1266 public:
1268 using char_type = Char;
1269
1270 private:
1271 OutputIt out_;
1273 internal::arg_map<basic_format_context> map_;
1274 internal::locale_ref loc_;
1275
1276 public:
1277 using iterator = OutputIt;
1278 using format_arg = basic_format_arg<basic_format_context>;
1279 using parse_context_type = basic_format_parse_context<Char>;
1280 template <typename T> using formatter_type = formatter<T, char_type>;
1281
1282 basic_format_context(const basic_format_context&) = delete;
1283 void operator=(const basic_format_context&) = delete;
1288 basic_format_context(OutputIt out,
1290 internal::locale_ref loc = internal::locale_ref())
1291 : out_(out), args_(ctx_args), loc_(loc) {}
1292
1293 format_arg arg(int id) const { return args_.get(id); }
1294
1295 // Checks if manual indexing is used and returns the argument with the
1296 // specified name.
1297 format_arg arg(basic_string_view<char_type> name);
1298
1299 internal::error_handler error_handler() { return {}; }
1300 void on_error(const char* message) { error_handler().on_error(message); }
1301
1302 // Returns an iterator to the beginning of the output range.
1303 iterator out() { return out_; }
1304
1305 // Advances the begin iterator to ``it``.
1306 void advance_to(iterator it) { out_ = it; }
1307
1308 internal::locale_ref locale() { return loc_; }
1309};
1310
1311template <typename Char>
1312using buffer_context =
1313 basic_format_context<std::back_insert_iterator<internal::buffer<Char>>,
1314 Char>;
1315using format_context = buffer_context<char>;
1316using wformat_context = buffer_context<wchar_t>;
1317
1325template <typename Context, typename... Args>
1327#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
1328 // Workaround a GCC template argument substitution bug.
1330#endif
1331{
1332 private:
1333 static const size_t num_args = sizeof...(Args);
1334 static const bool is_packed = num_args < internal::max_packed_args;
1335
1336 using value_type = conditional_t<is_packed, internal::value<Context>,
1337 basic_format_arg<Context>>;
1338
1339 // If the arguments are not packed, add one more element to mark the end.
1340 value_type data_[num_args + (num_args == 0 ? 1 : 0)];
1341
1342 friend class basic_format_args<Context>;
1343
1344 public:
1345 static constexpr unsigned long long types =
1346 is_packed ? internal::encode_types<Context, Args...>()
1347 : internal::is_unpacked_bit | num_args;
1348
1349 format_arg_store(const Args&... args)
1350 :
1351#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
1353#endif
1354 data_{internal::make_arg<
1355 is_packed, Context,
1356 internal::mapped_type_constant<Args, Context>::value>(args)...} {
1357 }
1358};
1359
1368template <typename Context = format_context, typename... Args>
1369inline format_arg_store<Context, Args...> make_format_args(
1370 const Args&... args) {
1371 return {args...};
1372}
1373
1384template <typename Context>
1386#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
1387 // Workaround a GCC template argument substitution bug.
1389#endif
1390{
1391 private:
1392 using char_type = typename Context::char_type;
1393
1394 template <typename T> struct need_copy {
1395 static constexpr internal::type mapped_type =
1396 internal::mapped_type_constant<T, Context>::value;
1397
1398 enum {
1399 value = !(internal::is_reference_wrapper<T>::value ||
1400 std::is_same<T, basic_string_view<char_type>>::value ||
1401 std::is_same<T, internal::std_string_view<char_type>>::value ||
1402 (mapped_type != internal::type::cstring_type &&
1403 mapped_type != internal::type::string_type &&
1404 mapped_type != internal::type::custom_type &&
1405 mapped_type != internal::type::named_arg_type))
1406 };
1407 };
1408
1409 template <typename T>
1410 using stored_type = conditional_t<internal::is_string<T>::value,
1411 std::basic_string<char_type>, T>;
1412
1413 // Storage of basic_format_arg must be contiguous.
1414 std::vector<basic_format_arg<Context>> data_;
1415
1416 // Storage of arguments not fitting into basic_format_arg must grow
1417 // without relocation because items in data_ refer to it.
1418 internal::dynamic_arg_list dynamic_args_;
1419
1420 friend class basic_format_args<Context>;
1421
1422 unsigned long long get_types() const {
1423 return internal::is_unpacked_bit | data_.size();
1424 }
1425
1426 template <typename T> void emplace_arg(const T& arg) {
1427 data_.emplace_back(internal::make_arg<Context>(arg));
1428 }
1429
1430 public:
1448 template <typename T> void push_back(const T& arg) {
1449 static_assert(
1450 !std::is_base_of<internal::named_arg_base<char_type>, T>::value,
1451 "named arguments are not supported yet");
1452 if (internal::const_check(need_copy<T>::value))
1453 emplace_arg(dynamic_args_.push<stored_type<T>>(arg));
1454 else
1455 emplace_arg(arg);
1456 }
1457
1462 template <typename T> void push_back(std::reference_wrapper<T> arg) {
1463 static_assert(
1464 need_copy<T>::value,
1465 "objects of built-in types and string views are always copied");
1466 emplace_arg(arg.get());
1467 }
1468};
1469
1480template <typename Context> class basic_format_args {
1481 public:
1482 using size_type = int;
1483 using format_arg = basic_format_arg<Context>;
1484
1485 private:
1486 // To reduce compiled code size per formatting function call, types of first
1487 // max_packed_args arguments are passed in the types_ field.
1488 unsigned long long types_;
1489 union {
1490 // If the number of arguments is less than max_packed_args, the argument
1491 // values are stored in values_, otherwise they are stored in args_.
1492 // This is done to reduce compiled code size as storing larger objects
1493 // may require more code (at least on x86-64) even if the same amount of
1494 // data is actually copied to stack. It saves ~10% on the bloat test.
1495 const internal::value<Context>* values_;
1496 const format_arg* args_;
1497 };
1498
1499 bool is_packed() const { return (types_ & internal::is_unpacked_bit) == 0; }
1500
1501 internal::type type(int index) const {
1502 int shift = index * internal::packed_arg_bits;
1503 unsigned int mask = (1 << internal::packed_arg_bits) - 1;
1504 return static_cast<internal::type>((types_ >> shift) & mask);
1505 }
1506
1507 friend class internal::arg_map<Context>;
1508
1509 void set_data(const internal::value<Context>* values) { values_ = values; }
1510 void set_data(const format_arg* args) { args_ = args; }
1511
1512 format_arg do_get(int index) const {
1513 format_arg arg;
1514 if (!is_packed()) {
1515 auto num_args = max_size();
1516 if (index < num_args) arg = args_[index];
1517 return arg;
1518 }
1519 if (index > internal::max_packed_args) return arg;
1520 arg.type_ = type(index);
1521 if (arg.type_ == internal::type::none_type) return arg;
1522 internal::value<Context>& val = arg.value_;
1523 val = values_[index];
1524 return arg;
1525 }
1526
1527 public:
1528 basic_format_args() : types_(0) {}
1529
1535 template <typename... Args>
1537 : types_(store.types) {
1538 set_data(store.data_);
1539 }
1540
1548 : types_(store.get_types()) {
1549 set_data(store.data_.data());
1550 }
1551
1557 basic_format_args(const format_arg* args, int count)
1558 : types_(internal::is_unpacked_bit | internal::to_unsigned(count)) {
1559 set_data(args);
1560 }
1561
1563 format_arg get(int index) const {
1564 format_arg arg = do_get(index);
1565 if (arg.type_ == internal::type::named_arg_type)
1566 arg = arg.value_.named_arg->template deserialize<Context>();
1567 return arg;
1568 }
1569
1570 int max_size() const {
1571 unsigned long long max_packed = internal::max_packed_args;
1572 return static_cast<int>(is_packed() ? max_packed
1573 : types_ & ~internal::is_unpacked_bit);
1574 }
1575};
1576
1578// It is a separate type rather than an alias to make symbols readable.
1579struct format_args : basic_format_args<format_context> {
1580 template <typename... Args>
1581 format_args(Args&&... args)
1582 : basic_format_args<format_context>(static_cast<Args&&>(args)...) {}
1583};
1584struct wformat_args : basic_format_args<wformat_context> {
1585 template <typename... Args>
1586 wformat_args(Args&&... args)
1587 : basic_format_args<wformat_context>(static_cast<Args&&>(args)...) {}
1588};
1589
1590template <typename Container> struct is_contiguous : std::false_type {};
1591
1592template <typename Char>
1593struct is_contiguous<std::basic_string<Char>> : std::true_type {};
1594
1595template <typename Char>
1596struct is_contiguous<internal::buffer<Char>> : std::true_type {};
1597
1598namespace internal {
1599
1600template <typename OutputIt>
1601struct is_contiguous_back_insert_iterator : std::false_type {};
1602template <typename Container>
1603struct is_contiguous_back_insert_iterator<std::back_insert_iterator<Container>>
1604 : is_contiguous<Container> {};
1605
1606template <typename Char> struct named_arg_base {
1608
1609 // Serialized value<context>.
1610 mutable char data[sizeof(basic_format_arg<buffer_context<Char>>)];
1611
1612 named_arg_base(basic_string_view<Char> nm) : name(nm) {}
1613
1614 template <typename Context> basic_format_arg<Context> deserialize() const {
1615 basic_format_arg<Context> arg;
1616 std::memcpy(&arg, data, sizeof(basic_format_arg<Context>));
1617 return arg;
1618 }
1619};
1620
1621struct view {};
1622
1623template <typename T, typename Char>
1624struct named_arg : view, named_arg_base<Char> {
1625 const T& value;
1626
1627 named_arg(basic_string_view<Char> name, const T& val)
1628 : named_arg_base<Char>(name), value(val) {}
1629};
1630
1631template <typename..., typename S, FMT_ENABLE_IF(!is_compile_string<S>::value)>
1632inline void check_format_string(const S&) {
1633#if defined(FMT_ENFORCE_COMPILE_STRING)
1634 static_assert(is_compile_string<S>::value,
1635 "FMT_ENFORCE_COMPILE_STRING requires all format strings to "
1636 "utilize FMT_STRING() or fmt().");
1637#endif
1638}
1639template <typename..., typename S, FMT_ENABLE_IF(is_compile_string<S>::value)>
1640void check_format_string(S);
1641
1642template <bool...> struct bool_pack;
1643template <bool... Args>
1644using all_true =
1645 std::is_same<bool_pack<Args..., true>, bool_pack<true, Args...>>;
1646
1647template <typename... Args, typename S, typename Char = char_t<S>>
1648inline format_arg_store<buffer_context<Char>, remove_reference_t<Args>...>
1649make_args_checked(const S& format_str,
1650 const remove_reference_t<Args>&... args) {
1651 static_assert(
1652 all_true<(!std::is_base_of<view, remove_reference_t<Args>>::value ||
1653 !std::is_reference<Args>::value)...>::value,
1654 "passing views as lvalues is disallowed");
1655 check_format_string<Args...>(format_str);
1656 return {args...};
1657}
1658
1659template <typename Char>
1660std::basic_string<Char> vformat(
1661 basic_string_view<Char> format_str,
1662 basic_format_args<buffer_context<type_identity_t<Char>>> args);
1663
1664template <typename Char>
1665typename buffer_context<Char>::iterator vformat_to(
1666 buffer<Char>& buf, basic_string_view<Char> format_str,
1667 basic_format_args<buffer_context<type_identity_t<Char>>> args);
1668
1669template <typename Char, typename Args,
1670 FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
1671inline void vprint_mojibake(std::FILE*, basic_string_view<Char>, const Args&) {}
1672
1673FMT_API void vprint_mojibake(std::FILE*, string_view, format_args);
1674#ifndef _WIN32
1675inline void vprint_mojibake(std::FILE*, string_view, format_args) {}
1676#endif
1677} // namespace internal
1678
1689template <typename S, typename T, typename Char = char_t<S>>
1690inline internal::named_arg<T, Char> arg(const S& name, const T& arg) {
1691 static_assert(internal::is_string<S>::value, "");
1692 return {name, arg};
1693}
1694
1695// Disable nested named arguments, e.g. ``arg("a", arg("b", 42))``.
1696template <typename S, typename T, typename Char>
1697void arg(S, internal::named_arg<T, Char>) = delete;
1698
1700// GCC 8 and earlier cannot handle std::back_insert_iterator<Container> with
1701// vformat_to<ArgFormatter>(...) overload, so SFINAE on iterator type instead.
1702template <typename OutputIt, typename S, typename Char = char_t<S>,
1703 FMT_ENABLE_IF(
1704 internal::is_contiguous_back_insert_iterator<OutputIt>::value)>
1705OutputIt vformat_to(
1706 OutputIt out, const S& format_str,
1707 basic_format_args<buffer_context<type_identity_t<Char>>> args) {
1708 using container = remove_reference_t<decltype(internal::get_container(out))>;
1709 internal::container_buffer<container> buf((internal::get_container(out)));
1710 internal::vformat_to(buf, to_string_view(format_str), args);
1711 return out;
1712}
1713
1714template <typename Container, typename S, typename... Args,
1715 FMT_ENABLE_IF(
1716 is_contiguous<Container>::value&& internal::is_string<S>::value)>
1717inline std::back_insert_iterator<Container> format_to(
1718 std::back_insert_iterator<Container> out, const S& format_str,
1719 Args&&... args) {
1720 return vformat_to(out, to_string_view(format_str),
1721 internal::make_args_checked<Args...>(format_str, args...));
1722}
1723
1724template <typename S, typename Char = char_t<S>>
1725inline std::basic_string<Char> vformat(
1726 const S& format_str,
1727 basic_format_args<buffer_context<type_identity_t<Char>>> args) {
1728 return internal::vformat(to_string_view(format_str), args);
1729}
1730
1741// Pass char_t as a default template parameter instead of using
1742// std::basic_string<char_t<S>> to reduce the symbol size.
1743template <typename S, typename... Args, typename Char = char_t<S>>
1744inline std::basic_string<Char> format(const S& format_str, Args&&... args) {
1745 return internal::vformat(
1746 to_string_view(format_str),
1747 internal::make_args_checked<Args...>(format_str, args...));
1748}
1749
1750FMT_API void vprint(string_view, format_args);
1751FMT_API void vprint(std::FILE*, string_view, format_args);
1752
1764template <typename S, typename... Args, typename Char = char_t<S>>
1765inline void print(std::FILE* f, const S& format_str, Args&&... args) {
1766 return internal::is_unicode<Char>()
1767 ? vprint(f, to_string_view(format_str),
1768 internal::make_args_checked<Args...>(format_str, args...))
1769 : internal::vprint_mojibake(
1770 f, to_string_view(format_str),
1771 internal::make_args_checked<Args...>(format_str, args...));
1772}
1773
1785template <typename S, typename... Args, typename Char = char_t<S>>
1786inline void print(const S& format_str, Args&&... args) {
1787 return internal::is_unicode<Char>()
1788 ? vprint(to_string_view(format_str),
1789 internal::make_args_checked<Args...>(format_str, args...))
1790 : internal::vprint_mojibake(
1791 stdout, to_string_view(format_str),
1792 internal::make_args_checked<Args...>(format_str, args...));
1793}
1794FMT_END_NAMESPACE
1795
1796#endif // FMT_CORE_H_
Definition: core.h:1480
basic_format_args(const format_arg *args, int count)
Definition: core.h:1557
basic_format_args(const dynamic_format_arg_store< Context > &store)
Definition: core.h:1547
format_arg get(int index) const
Definition: core.h:1563
basic_format_args(const format_arg_store< Context, Args... > &store)
Definition: core.h:1536
Definition: core.h:551
FMT_CONSTEXPR iterator end() const FMT_NOEXCEPT
Definition: core.h:575
FMT_CONSTEXPR iterator begin() const FMT_NOEXCEPT
Definition: core.h:568
FMT_CONSTEXPR int next_arg_id()
Definition: core.h:586
FMT_CONSTEXPR void check_arg_id(int)
Definition: core.h:596
FMT_CONSTEXPR void advance_to(iterator it)
Definition: core.h:578
Definition: core.h:351
FMT_CONSTEXPR basic_string_view(const std::basic_string< Char, Traits, Alloc > &s) FMT_NOEXCEPT
Definition: core.h:382
FMT_CONSTEXPR basic_string_view(const Char *s, size_t count) FMT_NOEXCEPT
Definition: core.h:364
FMT_CONSTEXPR size_t size() const
Definition: core.h:397
basic_string_view(const Char *s)
Definition: core.h:377
FMT_CONSTEXPR const Char * data() const
Definition: core.h:394
Definition: core.h:1390
void push_back(const T &arg)
Definition: core.h:1448
void push_back(std::reference_wrapper< T > arg)
Definition: core.h:1462
Definition: core.h:1331
Definition: core.h:645
void append(const U *begin, const U *end)
Definition: format.h:557
void clear()
Definition: core.h:704
void set(T *buf_data, std::size_t buf_capacity) FMT_NOEXCEPT
Definition: core.h:661
virtual void grow(std::size_t capacity)=0
void resize(std::size_t new_size)
Definition: core.h:698
void reserve(std::size_t new_capacity)
Definition: core.h:707
std::size_t capacity() const FMT_NOEXCEPT
Definition: core.h:687
std::size_t size() const FMT_NOEXCEPT
Definition: core.h:684
const T * data() const FMT_NOEXCEPT
Definition: core.h:693
T * data() FMT_NOEXCEPT
Definition: core.h:690
Definition: core.h:1579
Definition: core.h:447