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