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