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