bitz-server  2.0.1
format.h
1 /*
2  Formatting library for C++
3 
4  Copyright (c) 2012 - 2016, Victor Zverovich
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions are met:
9 
10  1. Redistributions of source code must retain the above copyright notice, this
11  list of conditions and the following disclaimer.
12  2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef FMT_FORMAT_H_
29 #define FMT_FORMAT_H_
30 
31 #define FMT_INCLUDE
32 #include <cassert>
33 #include <clocale>
34 #include <cmath>
35 #include <cstdio>
36 #include <cstring>
37 #include <limits>
38 #include <memory>
39 #include <stdexcept>
40 #include <string>
41 #include <utility> // for std::pair
42 #include <vector>
43 #undef FMT_INCLUDE
44 
45 // The fmt library version in the form major * 10000 + minor * 100 + patch.
46 #define FMT_VERSION 40100
47 
48 #if defined(__has_include)
49 #define FMT_HAS_INCLUDE(x) __has_include(x)
50 #else
51 #define FMT_HAS_INCLUDE(x) 0
52 #endif
53 
54 #if (FMT_HAS_INCLUDE(<string_view>) && __cplusplus > 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
55 #include <string_view>
56 #define FMT_HAS_STRING_VIEW 1
57 #else
58 #define FMT_HAS_STRING_VIEW 0
59 #endif
60 
61 #if defined _SECURE_SCL && _SECURE_SCL
62 #define FMT_SECURE_SCL _SECURE_SCL
63 #else
64 #define FMT_SECURE_SCL 0
65 #endif
66 
67 #if FMT_SECURE_SCL
68 #include <iterator>
69 #endif
70 
71 #ifdef _MSC_VER
72 #define FMT_MSC_VER _MSC_VER
73 #else
74 #define FMT_MSC_VER 0
75 #endif
76 
77 #if FMT_MSC_VER && FMT_MSC_VER <= 1500
78 typedef unsigned __int32 uint32_t;
79 typedef unsigned __int64 uint64_t;
80 typedef __int64 intmax_t;
81 #else
82 #include <stdint.h>
83 #endif
84 
85 #if !defined(FMT_HEADER_ONLY) && defined(_WIN32)
86 #ifdef FMT_EXPORT
87 #define FMT_API __declspec(dllexport)
88 #elif defined(FMT_SHARED)
89 #define FMT_API __declspec(dllimport)
90 #endif
91 #endif
92 #ifndef FMT_API
93 #define FMT_API
94 #endif
95 
96 #ifdef __GNUC__
97 #define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
98 #define FMT_GCC_EXTENSION __extension__
99 #if FMT_GCC_VERSION >= 406
100 #pragma GCC diagnostic push
101 // Disable the warning about "long long" which is sometimes reported even
102 // when using __extension__.
103 #pragma GCC diagnostic ignored "-Wlong-long"
104 // Disable the warning about declaration shadowing because it affects too
105 // many valid cases.
106 #pragma GCC diagnostic ignored "-Wshadow"
107 // Disable the warning about implicit conversions that may change the sign of
108 // an integer; silencing it otherwise would require many explicit casts.
109 #pragma GCC diagnostic ignored "-Wsign-conversion"
110 #endif
111 #if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__
112 #define FMT_HAS_GXX_CXX11 1
113 #endif
114 #else
115 #define FMT_GCC_VERSION 0
116 #define FMT_GCC_EXTENSION
117 #define FMT_HAS_GXX_CXX11 0
118 #endif
119 
120 #if defined(__INTEL_COMPILER)
121 #define FMT_ICC_VERSION __INTEL_COMPILER
122 #elif defined(__ICL)
123 #define FMT_ICC_VERSION __ICL
124 #endif
125 
126 #if defined(__clang__) && !defined(FMT_ICC_VERSION)
127 #define FMT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__)
128 #pragma clang diagnostic push
129 #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
130 #pragma clang diagnostic ignored "-Wpadded"
131 #endif
132 
133 #ifdef __GNUC_LIBSTD__
134 #define FMT_GNUC_LIBSTD_VERSION (__GNUC_LIBSTD__ * 100 + __GNUC_LIBSTD_MINOR__)
135 #endif
136 
137 #ifdef __has_feature
138 #define FMT_HAS_FEATURE(x) __has_feature(x)
139 #else
140 #define FMT_HAS_FEATURE(x) 0
141 #endif
142 
143 #ifdef __has_builtin
144 #define FMT_HAS_BUILTIN(x) __has_builtin(x)
145 #else
146 #define FMT_HAS_BUILTIN(x) 0
147 #endif
148 
149 #ifdef __has_cpp_attribute
150 #define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
151 #else
152 #define FMT_HAS_CPP_ATTRIBUTE(x) 0
153 #endif
154 
155 #if FMT_HAS_CPP_ATTRIBUTE(maybe_unused)
156 #define FMT_HAS_CXX17_ATTRIBUTE_MAYBE_UNUSED
157 // VC++ 1910 support /std: option and that will set _MSVC_LANG macro
158 // Clang with Microsoft CodeGen doesn't define _MSVC_LANG macro
159 #elif defined(_MSVC_LANG) && _MSVC_LANG > 201402 && _MSC_VER >= 1910
160 #define FMT_HAS_CXX17_ATTRIBUTE_MAYBE_UNUSED
161 #endif
162 
163 #ifdef FMT_HAS_CXX17_ATTRIBUTE_MAYBE_UNUSED
164 #define FMT_MAYBE_UNUSED [[maybe_unused]]
165 // g++/clang++ also support [[gnu::unused]]. However, we don't use it.
166 #elif defined(__GNUC__)
167 #define FMT_MAYBE_UNUSED __attribute__((unused))
168 #else
169 #define FMT_MAYBE_UNUSED
170 #endif
171 
172 // Use the compiler's attribute noreturn
173 #if defined(__MINGW32__) || defined(__MINGW64__)
174 #define FMT_NORETURN __attribute__((noreturn))
175 #elif FMT_HAS_CPP_ATTRIBUTE(noreturn) && __cplusplus >= 201103L
176 #define FMT_NORETURN [[noreturn]]
177 #else
178 #define FMT_NORETURN
179 #endif
180 
181 #ifndef FMT_USE_VARIADIC_TEMPLATES
182 // Variadic templates are available in GCC since version 4.4
183 // (http://gcc.gnu.org/projects/cxx0x.html) and in Visual C++
184 // since version 2013.
185 #define FMT_USE_VARIADIC_TEMPLATES \
186  (FMT_HAS_FEATURE(cxx_variadic_templates) || (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800)
187 #endif
188 
189 #ifndef FMT_USE_RVALUE_REFERENCES
190 // Don't use rvalue references when compiling with clang and an old libstdc++
191 // as the latter doesn't provide std::move.
192 #if defined(FMT_GNUC_LIBSTD_VERSION) && FMT_GNUC_LIBSTD_VERSION <= 402
193 #define FMT_USE_RVALUE_REFERENCES 0
194 #else
195 #define FMT_USE_RVALUE_REFERENCES \
196  (FMT_HAS_FEATURE(cxx_rvalue_references) || (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1600)
197 #endif
198 #endif
199 
200 #if __cplusplus >= 201103L || FMT_MSC_VER >= 1700
201 #define FMT_USE_ALLOCATOR_TRAITS 1
202 #else
203 #define FMT_USE_ALLOCATOR_TRAITS 0
204 #endif
205 
206 // Check if exceptions are disabled.
207 #if defined(__GNUC__) && !defined(__EXCEPTIONS)
208 #define FMT_EXCEPTIONS 0
209 #endif
210 #if FMT_MSC_VER && !_HAS_EXCEPTIONS
211 #define FMT_EXCEPTIONS 0
212 #endif
213 #ifndef FMT_EXCEPTIONS
214 #define FMT_EXCEPTIONS 1
215 #endif
216 
217 #ifndef FMT_THROW
218 #if FMT_EXCEPTIONS
219 #define FMT_THROW(x) throw x
220 #else
221 #define FMT_THROW(x) assert(false)
222 #endif
223 #endif
224 
225 // Define FMT_USE_NOEXCEPT to make fmt use noexcept (C++11 feature).
226 #ifndef FMT_USE_NOEXCEPT
227 #define FMT_USE_NOEXCEPT 0
228 #endif
229 
230 #if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900
231 #define FMT_DETECTED_NOEXCEPT noexcept
232 #else
233 #define FMT_DETECTED_NOEXCEPT throw()
234 #endif
235 
236 #ifndef FMT_NOEXCEPT
237 #if FMT_EXCEPTIONS
238 #define FMT_NOEXCEPT FMT_DETECTED_NOEXCEPT
239 #else
240 #define FMT_NOEXCEPT
241 #endif
242 #endif
243 
244 // This is needed because GCC still uses throw() in its headers when exceptions
245 // are disabled.
246 #if FMT_GCC_VERSION
247 #define FMT_DTOR_NOEXCEPT FMT_DETECTED_NOEXCEPT
248 #else
249 #define FMT_DTOR_NOEXCEPT FMT_NOEXCEPT
250 #endif
251 
252 #ifndef FMT_OVERRIDE
253 #if (defined(FMT_USE_OVERRIDE) && FMT_USE_OVERRIDE) || FMT_HAS_FEATURE(cxx_override) || (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
254  FMT_MSC_VER >= 1900
255 #define FMT_OVERRIDE override
256 #else
257 #define FMT_OVERRIDE
258 #endif
259 #endif
260 
261 #ifndef FMT_NULL
262 #if FMT_HAS_FEATURE(cxx_nullptr) || (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1600
263 #define FMT_NULL nullptr
264 #else
265 #define FMT_NULL NULL
266 #endif
267 #endif
268 
269 // A macro to disallow the copy constructor and operator= functions
270 // This should be used in the private: declarations for a class
271 #ifndef FMT_USE_DELETED_FUNCTIONS
272 #define FMT_USE_DELETED_FUNCTIONS 0
273 #endif
274 
275 #if FMT_USE_DELETED_FUNCTIONS || FMT_HAS_FEATURE(cxx_deleted_functions) || (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || \
276  FMT_MSC_VER >= 1800
277 #define FMT_DELETED_OR_UNDEFINED = delete
278 #define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
279  TypeName(const TypeName &) = delete; \
280  TypeName &operator=(const TypeName &) = delete
281 #else
282 #define FMT_DELETED_OR_UNDEFINED
283 #define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
284  TypeName(const TypeName &); \
285  TypeName &operator=(const TypeName &)
286 #endif
287 
288 #ifndef FMT_USE_DEFAULTED_FUNCTIONS
289 #define FMT_USE_DEFAULTED_FUNCTIONS 0
290 #endif
291 
292 #ifndef FMT_DEFAULTED_COPY_CTOR
293 #if FMT_USE_DEFAULTED_FUNCTIONS || FMT_HAS_FEATURE(cxx_defaulted_functions) || (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || \
294  FMT_MSC_VER >= 1800
295 #define FMT_DEFAULTED_COPY_CTOR(TypeName) TypeName(const TypeName &) = default;
296 #else
297 #define FMT_DEFAULTED_COPY_CTOR(TypeName)
298 #endif
299 #endif
300 
301 #ifndef FMT_USE_USER_DEFINED_LITERALS
302 // All compilers which support UDLs also support variadic templates. This
303 // makes the fmt::literals implementation easier. However, an explicit check
304 // for variadic templates is added here just in case.
305 // For Intel's compiler both it and the system gcc/msc must support UDLs.
306 #if FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES && \
307  (FMT_HAS_FEATURE(cxx_user_literals) || (FMT_GCC_VERSION >= 407 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900) && \
308  (!defined(FMT_ICC_VERSION) || FMT_ICC_VERSION >= 1500)
309 #define FMT_USE_USER_DEFINED_LITERALS 1
310 #else
311 #define FMT_USE_USER_DEFINED_LITERALS 0
312 #endif
313 #endif
314 
315 #ifndef FMT_USE_EXTERN_TEMPLATES
316 #define FMT_USE_EXTERN_TEMPLATES (FMT_CLANG_VERSION >= 209 || (FMT_GCC_VERSION >= 303 && FMT_HAS_GXX_CXX11))
317 #endif
318 
319 #ifdef FMT_HEADER_ONLY
320 // If header only do not use extern templates.
321 #undef FMT_USE_EXTERN_TEMPLATES
322 #define FMT_USE_EXTERN_TEMPLATES 0
323 #endif
324 
325 #ifndef FMT_ASSERT
326 #define FMT_ASSERT(condition, message) assert((condition) && message)
327 #endif
328 
329 // __builtin_clz is broken in clang with Microsoft CodeGen:
330 // https://github.com/fmtlib/fmt/issues/519
331 #ifndef _MSC_VER
332 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz)
333 #define FMT_BUILTIN_CLZ(n) __builtin_clz(n)
334 #endif
335 
336 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clzll)
337 #define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n)
338 #endif
339 #endif
340 
341 // Some compilers masquerade as both MSVC and GCC-likes or
342 // otherwise support __builtin_clz and __builtin_clzll, so
343 // only define FMT_BUILTIN_CLZ using the MSVC intrinsics
344 // if the clz and clzll builtins are not available.
345 #if FMT_MSC_VER && !defined(FMT_BUILTIN_CLZLL) && !defined(_MANAGED)
346 #include <intrin.h> // _BitScanReverse, _BitScanReverse64
347 
348 namespace fmt {
349 namespace internal {
350 // avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning
351 #ifndef __clang__
352 #pragma intrinsic(_BitScanReverse)
353 #endif
354 inline uint32_t clz(uint32_t x)
355 {
356  unsigned long r = 0;
357  _BitScanReverse(&r, x);
358 
359  assert(x != 0);
360  // Static analysis complains about using uninitialized data
361  // "r", but the only way that can happen is if "x" is 0,
362  // which the callers guarantee to not happen.
363 #pragma warning(suppress : 6102)
364  return 31 - r;
365 }
366 #define FMT_BUILTIN_CLZ(n) fmt::internal::clz(n)
367 
368 // avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning
369 #if defined(_WIN64) && !defined(__clang__)
370 #pragma intrinsic(_BitScanReverse64)
371 #endif
372 
373 inline uint32_t clzll(uint64_t x)
374 {
375  unsigned long r = 0;
376 #ifdef _WIN64
377  _BitScanReverse64(&r, x);
378 #else
379  // Scan the high 32 bits.
380  if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32)))
381  return 63 - (r + 32);
382 
383  // Scan the low 32 bits.
384  _BitScanReverse(&r, static_cast<uint32_t>(x));
385 #endif
386 
387  assert(x != 0);
388  // Static analysis complains about using uninitialized data
389  // "r", but the only way that can happen is if "x" is 0,
390  // which the callers guarantee to not happen.
391 #pragma warning(suppress : 6102)
392  return 63 - r;
393 }
394 #define FMT_BUILTIN_CLZLL(n) fmt::internal::clzll(n)
395 } // namespace internal
396 } // namespace fmt
397 #endif
398 
399 namespace fmt {
400 namespace internal {
401 struct DummyInt
402 {
403  int data[2];
404  operator int() const
405  {
406  return 0;
407  }
408 };
410 
411 // Dummy implementations of system functions such as signbit and ecvt called
412 // if the latter are not available.
413 inline DummyInt signbit(...)
414 {
415  return DummyInt();
416 }
417 inline DummyInt _ecvt_s(...)
418 {
419  return DummyInt();
420 }
421 inline DummyInt isinf(...)
422 {
423  return DummyInt();
424 }
425 inline DummyInt _finite(...)
426 {
427  return DummyInt();
428 }
429 inline DummyInt isnan(...)
430 {
431  return DummyInt();
432 }
433 inline DummyInt _isnan(...)
434 {
435  return DummyInt();
436 }
437 
438 // A helper function to suppress bogus "conditional expression is constant"
439 // warnings.
440 template<typename T>
441 inline T const_check(T value)
442 {
443  return value;
444 }
445 } // namespace internal
446 } // namespace fmt
447 
448 namespace std {
449 // Standard permits specialization of std::numeric_limits. This specialization
450 // is used to resolve ambiguity between isinf and std::isinf in glibc:
451 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48891
452 // and the same for isnan and signbit.
453 template<>
454 class numeric_limits<fmt::internal::DummyInt> : public std::numeric_limits<int>
455 {
456 public:
457  // Portable version of isinf.
458  template<typename T>
459  static bool isinfinity(T x)
460  {
461  using namespace fmt::internal;
462  // The resolution "priority" is:
463  // isinf macro > std::isinf > ::isinf > fmt::internal::isinf
464  if (const_check(sizeof(isinf(x)) == sizeof(bool) || sizeof(isinf(x)) == sizeof(int)))
465  {
466  return isinf(x) != 0;
467  }
468  return !_finite(static_cast<double>(x));
469  }
470 
471  // Portable version of isnan.
472  template<typename T>
473  static bool isnotanumber(T x)
474  {
475  using namespace fmt::internal;
476  if (const_check(sizeof(isnan(x)) == sizeof(bool) || sizeof(isnan(x)) == sizeof(int)))
477  {
478  return isnan(x) != 0;
479  }
480  return _isnan(static_cast<double>(x)) != 0;
481  }
482 
483  // Portable version of signbit.
484  static bool isnegative(double x)
485  {
486  using namespace fmt::internal;
487  if (const_check(sizeof(signbit(x)) == sizeof(bool) || sizeof(signbit(x)) == sizeof(int)))
488  {
489  return signbit(x) != 0;
490  }
491  if (x < 0)
492  return true;
493  if (!isnotanumber(x))
494  return false;
495  int dec = 0, sign = 0;
496  char buffer[2]; // The buffer size must be >= 2 or _ecvt_s will fail.
497  _ecvt_s(buffer, sizeof(buffer), x, 0, &dec, &sign);
498  return sign != 0;
499  }
500 };
501 } // namespace std
502 
503 namespace fmt {
504 
505 // Fix the warning about long long on older versions of GCC
506 // that don't support the diagnostic pragma.
507 FMT_GCC_EXTENSION typedef long long LongLong;
508 FMT_GCC_EXTENSION typedef unsigned long long ULongLong;
509 
510 #if FMT_USE_RVALUE_REFERENCES
511 using std::move;
512 #endif
513 
514 template<typename Char>
516 
517 typedef BasicWriter<char> Writer;
519 
520 template<typename Char>
522 
523 struct FormatSpec;
524 
525 template<typename Impl, typename Char, typename Spec = fmt::FormatSpec>
527 
528 template<typename CharType, typename ArgFormatter = fmt::ArgFormatter<CharType>>
530 
556 template<typename Char>
558 {
559 private:
560  const Char *data_;
561  std::size_t size_;
562 
563 public:
565  BasicStringRef(const Char *s, std::size_t size)
566  : data_(s)
567  , size_(size)
568  {
569  }
570 
577  BasicStringRef(const Char *s)
578  : data_(s)
579  , size_(std::char_traits<Char>::length(s))
580  {
581  }
582 
588  template<typename Allocator>
589  BasicStringRef(const std::basic_string<Char, std::char_traits<Char>, Allocator> &s)
590  : data_(s.c_str())
591  , size_(s.size())
592  {
593  }
594 
595 #if FMT_HAS_STRING_VIEW
596 
601  BasicStringRef(const std::basic_string_view<Char, std::char_traits<Char>> &s)
602  : data_(s.data())
603  , size_(s.size())
604  {
605  }
606 
612  explicit operator std::basic_string_view<Char>() const FMT_NOEXCEPT
613  {
614  return std::basic_string_view<Char>(data_, size_);
615  }
616 #endif
617 
623  std::basic_string<Char> to_string() const
624  {
625  return std::basic_string<Char>(data_, size_);
626  }
627 
629  const Char *data() const
630  {
631  return data_;
632  }
633 
635  std::size_t size() const
636  {
637  return size_;
638  }
639 
640  // Lexicographically compare this string reference to other.
641  int compare(BasicStringRef other) const
642  {
643  std::size_t size = size_ < other.size_ ? size_ : other.size_;
644  int result = std::char_traits<Char>::compare(data_, other.data_, size);
645  if (result == 0)
646  result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1);
647  return result;
648  }
649 
650  friend bool operator==(BasicStringRef lhs, BasicStringRef rhs)
651  {
652  return lhs.compare(rhs) == 0;
653  }
654  friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs)
655  {
656  return lhs.compare(rhs) != 0;
657  }
658  friend bool operator<(BasicStringRef lhs, BasicStringRef rhs)
659  {
660  return lhs.compare(rhs) < 0;
661  }
662  friend bool operator<=(BasicStringRef lhs, BasicStringRef rhs)
663  {
664  return lhs.compare(rhs) <= 0;
665  }
666  friend bool operator>(BasicStringRef lhs, BasicStringRef rhs)
667  {
668  return lhs.compare(rhs) > 0;
669  }
670  friend bool operator>=(BasicStringRef lhs, BasicStringRef rhs)
671  {
672  return lhs.compare(rhs) >= 0;
673  }
674 };
675 
678 
704 template<typename Char>
706 {
707 private:
708  const Char *data_;
709 
710 public:
712  BasicCStringRef(const Char *s)
713  : data_(s)
714  {
715  }
716 
722  template<typename Allocator>
723  BasicCStringRef(const std::basic_string<Char, std::char_traits<Char>, Allocator> &s)
724  : data_(s.c_str())
725  {
726  }
727 
729  const Char *c_str() const
730  {
731  return data_;
732  }
733 };
734 
737 
739 class FormatError : public std::runtime_error
740 {
741 public:
742  explicit FormatError(CStringRef message)
743  : std::runtime_error(message.c_str())
744  {
745  }
746  FormatError(const FormatError &ferr)
747  : std::runtime_error(ferr)
748  {
749  }
750  FMT_API ~FormatError() FMT_DTOR_NOEXCEPT FMT_OVERRIDE;
751 };
752 
753 namespace internal {
754 
755 // MakeUnsigned<T>::Type gives an unsigned type corresponding to integer type T.
756 template<typename T>
758 {
759  typedef T Type;
760 };
761 
762 #define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U) \
763  template<> \
764  struct MakeUnsigned<T> \
765  { \
766  typedef U Type; \
767  }
768 
769 FMT_SPECIALIZE_MAKE_UNSIGNED(char, unsigned char);
770 FMT_SPECIALIZE_MAKE_UNSIGNED(signed char, unsigned char);
771 FMT_SPECIALIZE_MAKE_UNSIGNED(short, unsigned short);
772 FMT_SPECIALIZE_MAKE_UNSIGNED(int, unsigned);
773 FMT_SPECIALIZE_MAKE_UNSIGNED(long, unsigned long);
774 FMT_SPECIALIZE_MAKE_UNSIGNED(LongLong, ULongLong);
775 
776 // Casts nonnegative integer to unsigned.
777 template<typename Int>
778 inline typename MakeUnsigned<Int>::Type to_unsigned(Int value)
779 {
780  FMT_ASSERT(value >= 0, "negative value");
781  return static_cast<typename MakeUnsigned<Int>::Type>(value);
782 }
783 
784 // The number of characters to store in the MemoryBuffer object itself
785 // to avoid dynamic memory allocation.
786 enum
787 {
788  INLINE_BUFFER_SIZE = 500
789 };
790 
791 #if FMT_SECURE_SCL
792 // Use checked iterator to avoid warnings on MSVC.
793 template<typename T>
794 inline stdext::checked_array_iterator<T *> make_ptr(T *ptr, std::size_t size)
795 {
796  return stdext::checked_array_iterator<T *>(ptr, size);
797 }
798 #else
799 template<typename T>
800 inline T *make_ptr(T *ptr, std::size_t)
801 {
802  return ptr;
803 }
804 #endif
805 } // namespace internal
806 
812 template<typename T>
813 class Buffer
814 {
815 private:
816  FMT_DISALLOW_COPY_AND_ASSIGN(Buffer);
817 
818 protected:
819  T *ptr_;
820  std::size_t size_;
821  std::size_t capacity_;
822 
823  Buffer(T *ptr = FMT_NULL, std::size_t capacity = 0)
824  : ptr_(ptr)
825  , size_(0)
826  , capacity_(capacity)
827  {
828  }
829 
836  virtual void grow(std::size_t size) = 0;
837 
838 public:
839  virtual ~Buffer() {}
840 
842  std::size_t size() const
843  {
844  return size_;
845  }
846 
848  std::size_t capacity() const
849  {
850  return capacity_;
851  }
852 
856  void resize(std::size_t new_size)
857  {
858  if (new_size > capacity_)
859  grow(new_size);
860  size_ = new_size;
861  }
862 
868  void reserve(std::size_t capacity)
869  {
870  if (capacity > capacity_)
871  grow(capacity);
872  }
873 
874  void clear() FMT_NOEXCEPT
875  {
876  size_ = 0;
877  }
878 
879  void push_back(const T &value)
880  {
881  if (size_ == capacity_)
882  grow(size_ + 1);
883  ptr_[size_++] = value;
884  }
885 
887  template<typename U>
888  void append(const U *begin, const U *end);
889 
890  T &operator[](std::size_t index)
891  {
892  return ptr_[index];
893  }
894  const T &operator[](std::size_t index) const
895  {
896  return ptr_[index];
897  }
898 };
899 
900 template<typename T>
901 template<typename U>
902 void Buffer<T>::append(const U *begin, const U *end)
903 {
904  FMT_ASSERT(end >= begin, "negative value");
905  std::size_t new_size = size_ + static_cast<std::size_t>(end - begin);
906  if (new_size > capacity_)
907  grow(new_size);
908  std::uninitialized_copy(begin, end, internal::make_ptr(ptr_, capacity_) + size_);
909  size_ = new_size;
910 }
911 
912 namespace internal {
913 
914 // A memory buffer for trivially copyable/constructible types with the first
915 // SIZE elements stored in the object itself.
916 template<typename T, std::size_t SIZE, typename Allocator = std::allocator<T>>
917 class MemoryBuffer : private Allocator, public Buffer<T>
918 {
919 private:
920  T data_[SIZE];
921 
922  // Deallocate memory allocated by the buffer.
923  void deallocate()
924  {
925  if (this->ptr_ != data_)
926  Allocator::deallocate(this->ptr_, this->capacity_);
927  }
928 
929 protected:
930  void grow(std::size_t size) FMT_OVERRIDE;
931 
932 public:
933  explicit MemoryBuffer(const Allocator &alloc = Allocator())
934  : Allocator(alloc)
935  , Buffer<T>(data_, SIZE)
936  {
937  }
938  ~MemoryBuffer() FMT_OVERRIDE
939  {
940  deallocate();
941  }
942 
943 #if FMT_USE_RVALUE_REFERENCES
944 private:
945  // Move data from other to this buffer.
946  void move(MemoryBuffer &other)
947  {
948  Allocator &this_alloc = *this, &other_alloc = other;
949  this_alloc = std::move(other_alloc);
950  this->size_ = other.size_;
951  this->capacity_ = other.capacity_;
952  if (other.ptr_ == other.data_)
953  {
954  this->ptr_ = data_;
955  std::uninitialized_copy(other.data_, other.data_ + this->size_, make_ptr(data_, this->capacity_));
956  }
957  else
958  {
959  this->ptr_ = other.ptr_;
960  // Set pointer to the inline array so that delete is not called
961  // when deallocating.
962  other.ptr_ = other.data_;
963  }
964  }
965 
966 public:
967  MemoryBuffer(MemoryBuffer &&other)
968  {
969  move(other);
970  }
971 
972  MemoryBuffer &operator=(MemoryBuffer &&other)
973  {
974  assert(this != &other);
975  deallocate();
976  move(other);
977  return *this;
978  }
979 #endif
980 
981  // Returns a copy of the allocator associated with this buffer.
982  Allocator get_allocator() const
983  {
984  return *this;
985  }
986 };
987 
988 template<typename T, std::size_t SIZE, typename Allocator>
990 {
991  std::size_t new_capacity = this->capacity_ + this->capacity_ / 2;
992  if (size > new_capacity)
993  new_capacity = size;
994 #if FMT_USE_ALLOCATOR_TRAITS
995  T *new_ptr = std::allocator_traits<Allocator>::allocate(*this, new_capacity, FMT_NULL);
996 #else
997  T *new_ptr = this->allocate(new_capacity, FMT_NULL);
998 #endif
999  // The following code doesn't throw, so the raw pointer above doesn't leak.
1000  std::uninitialized_copy(this->ptr_, this->ptr_ + this->size_, make_ptr(new_ptr, new_capacity));
1001  std::size_t old_capacity = this->capacity_;
1002  T *old_ptr = this->ptr_;
1003  this->capacity_ = new_capacity;
1004  this->ptr_ = new_ptr;
1005  // deallocate may throw (at least in principle), but it doesn't matter since
1006  // the buffer already uses the new storage and will deallocate it in case
1007  // of exception.
1008  if (old_ptr != data_)
1009  Allocator::deallocate(old_ptr, old_capacity);
1010 }
1011 
1012 // A fixed-size buffer.
1013 template<typename Char>
1014 class FixedBuffer : public fmt::Buffer<Char>
1015 {
1016 public:
1017  FixedBuffer(Char *array, std::size_t size)
1018  : fmt::Buffer<Char>(array, size)
1019  {
1020  }
1021 
1022 protected:
1023  FMT_API void grow(std::size_t size) FMT_OVERRIDE;
1024 };
1025 
1026 template<typename Char>
1028 {
1029 public:
1030 #if FMT_SECURE_SCL
1031  typedef stdext::checked_array_iterator<Char *> CharPtr;
1032 #else
1033  typedef Char *CharPtr;
1034 #endif
1035  static Char cast(int value)
1036  {
1037  return static_cast<Char>(value);
1038  }
1039 };
1040 
1041 template<typename Char>
1043 
1044 template<>
1045 class CharTraits<char> : public BasicCharTraits<char>
1046 {
1047 private:
1048  // Conversion from wchar_t to char is not allowed.
1049  static char convert(wchar_t);
1050 
1051 public:
1052  static char convert(char value)
1053  {
1054  return value;
1055  }
1056 
1057  // Formats a floating-point number.
1058  template<typename T>
1059  FMT_API static int format_float(char *buffer, std::size_t size, const char *format, unsigned width, int precision, T value);
1060 };
1061 
1062 #if FMT_USE_EXTERN_TEMPLATES
1063 extern template int CharTraits<char>::format_float<double>(
1064  char *buffer, std::size_t size, const char *format, unsigned width, int precision, double value);
1065 extern template int CharTraits<char>::format_float<long double>(
1066  char *buffer, std::size_t size, const char *format, unsigned width, int precision, long double value);
1067 #endif
1068 
1069 template<>
1070 class CharTraits<wchar_t> : public BasicCharTraits<wchar_t>
1071 {
1072 public:
1073  static wchar_t convert(char value)
1074  {
1075  return value;
1076  }
1077  static wchar_t convert(wchar_t value)
1078  {
1079  return value;
1080  }
1081 
1082  template<typename T>
1083  FMT_API static int format_float(wchar_t *buffer, std::size_t size, const wchar_t *format, unsigned width, int precision, T value);
1084 };
1085 
1086 #if FMT_USE_EXTERN_TEMPLATES
1087 extern template int CharTraits<wchar_t>::format_float<double>(
1088  wchar_t *buffer, std::size_t size, const wchar_t *format, unsigned width, int precision, double value);
1089 extern template int CharTraits<wchar_t>::format_float<long double>(
1090  wchar_t *buffer, std::size_t size, const wchar_t *format, unsigned width, int precision, long double value);
1091 #endif
1092 
1093 // Checks if a number is negative - used to avoid warnings.
1094 template<bool IsSigned>
1096 {
1097  template<typename T>
1098  static bool is_negative(T value)
1099  {
1100  return value < 0;
1101  }
1102 };
1103 
1104 template<>
1105 struct SignChecker<false>
1106 {
1107  template<typename T>
1108  static bool is_negative(T)
1109  {
1110  return false;
1111  }
1112 };
1113 
1114 // Returns true if value is negative, false otherwise.
1115 // Same as (value < 0) but doesn't produce warnings if T is an unsigned type.
1116 template<typename T>
1117 inline bool is_negative(T value)
1118 {
1119  return SignChecker<std::numeric_limits<T>::is_signed>::is_negative(value);
1120 }
1121 
1122 // Selects uint32_t if FitsIn32Bits is true, uint64_t otherwise.
1123 template<bool FitsIn32Bits>
1125 {
1126  typedef uint32_t Type;
1127 };
1128 
1129 template<>
1130 struct TypeSelector<false>
1131 {
1132  typedef uint64_t Type;
1133 };
1134 
1135 template<typename T>
1137 {
1138  // Smallest of uint32_t and uint64_t that is large enough to represent
1139  // all values of T.
1140  typedef typename TypeSelector<std::numeric_limits<T>::digits <= 32>::Type MainType;
1141 };
1142 
1143 FMT_API FMT_NORETURN void report_unknown_type(char code, const char *type);
1144 
1145 // Static data is placed in this class template to allow header-only
1146 // configuration.
1147 template<typename T = void>
1148 struct FMT_API BasicData
1149 {
1150  static const uint32_t POWERS_OF_10_32[];
1151  static const uint64_t POWERS_OF_10_64[];
1152  static const char DIGITS[];
1153 };
1154 
1155 #if FMT_USE_EXTERN_TEMPLATES
1156 extern template struct BasicData<void>;
1157 #endif
1158 
1159 typedef BasicData<> Data;
1160 
1161 #ifdef FMT_BUILTIN_CLZLL
1162 // Returns the number of decimal digits in n. Leading zeros are not counted
1163 // except for n == 0 in which case count_digits returns 1.
1164 inline unsigned count_digits(uint64_t n)
1165 {
1166  // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
1167  // and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits.
1168  int t = (64 - FMT_BUILTIN_CLZLL(n | 1)) * 1233 >> 12;
1169  return to_unsigned(t) - (n < Data::POWERS_OF_10_64[t]) + 1;
1170 }
1171 #else
1172 // Fallback version of count_digits used when __builtin_clz is not available.
1173 inline unsigned count_digits(uint64_t n)
1174 {
1175  unsigned count = 1;
1176  for (;;)
1177  {
1178  // Integer division is slow so do it for a group of four digits instead
1179  // of for every digit. The idea comes from the talk by Alexandrescu
1180  // "Three Optimization Tips for C++". See speed-test for a comparison.
1181  if (n < 10)
1182  return count;
1183  if (n < 100)
1184  return count + 1;
1185  if (n < 1000)
1186  return count + 2;
1187  if (n < 10000)
1188  return count + 3;
1189  n /= 10000u;
1190  count += 4;
1191  }
1192 }
1193 #endif
1194 
1195 #ifdef FMT_BUILTIN_CLZ
1196 // Optional version of count_digits for better performance on 32-bit platforms.
1197 inline unsigned count_digits(uint32_t n)
1198 {
1199  int t = (32 - FMT_BUILTIN_CLZ(n | 1)) * 1233 >> 12;
1200  return to_unsigned(t) - (n < Data::POWERS_OF_10_32[t]) + 1;
1201 }
1202 #endif
1203 
1204 // A functor that doesn't add a thousands separator.
1206 {
1207  template<typename Char>
1208  void operator()(Char *)
1209  {
1210  }
1211 };
1212 
1213 // A functor that adds a thousands separator.
1215 {
1216 private:
1217  fmt::StringRef sep_;
1218 
1219  // Index of a decimal digit with the least significant digit having index 0.
1220  unsigned digit_index_;
1221 
1222 public:
1223  explicit ThousandsSep(fmt::StringRef sep)
1224  : sep_(sep)
1225  , digit_index_(0)
1226  {
1227  }
1228 
1229  template<typename Char>
1230  void operator()(Char *&buffer)
1231  {
1232  if (++digit_index_ % 3 != 0)
1233  return;
1234  buffer -= sep_.size();
1235  std::uninitialized_copy(sep_.data(), sep_.data() + sep_.size(), internal::make_ptr(buffer, sep_.size()));
1236  }
1237 };
1238 
1239 // Formats a decimal unsigned integer value writing into buffer.
1240 // thousands_sep is a functor that is called after writing each char to
1241 // add a thousands separator if necessary.
1242 template<typename UInt, typename Char, typename ThousandsSep>
1243 inline void format_decimal(Char *buffer, UInt value, unsigned num_digits, ThousandsSep thousands_sep)
1244 {
1245  buffer += num_digits;
1246  while (value >= 100)
1247  {
1248  // Integer division is slow so do it for a group of two digits instead
1249  // of for every digit. The idea comes from the talk by Alexandrescu
1250  // "Three Optimization Tips for C++". See speed-test for a comparison.
1251  unsigned index = static_cast<unsigned>((value % 100) * 2);
1252  value /= 100;
1253  *--buffer = Data::DIGITS[index + 1];
1254  thousands_sep(buffer);
1255  *--buffer = Data::DIGITS[index];
1256  thousands_sep(buffer);
1257  }
1258  if (value < 10)
1259  {
1260  *--buffer = static_cast<char>('0' + value);
1261  return;
1262  }
1263  unsigned index = static_cast<unsigned>(value * 2);
1264  *--buffer = Data::DIGITS[index + 1];
1265  thousands_sep(buffer);
1266  *--buffer = Data::DIGITS[index];
1267 }
1268 
1269 template<typename UInt, typename Char>
1270 inline void format_decimal(Char *buffer, UInt value, unsigned num_digits)
1271 {
1272  format_decimal(buffer, value, num_digits, NoThousandsSep());
1273  return;
1274 }
1275 
1276 #ifndef _WIN32
1277 #define FMT_USE_WINDOWS_H 0
1278 #elif !defined(FMT_USE_WINDOWS_H)
1279 #define FMT_USE_WINDOWS_H 1
1280 #endif
1281 
1282 // Define FMT_USE_WINDOWS_H to 0 to disable use of windows.h.
1283 // All the functionality that relies on it will be disabled too.
1284 #if FMT_USE_WINDOWS_H
1285 // A converter from UTF-8 to UTF-16.
1286 // It is only provided for Windows since other systems support UTF-8 natively.
1287 class UTF8ToUTF16
1288 {
1289 private:
1291 
1292 public:
1293  FMT_API explicit UTF8ToUTF16(StringRef s);
1294  operator WStringRef() const
1295  {
1296  return WStringRef(&buffer_[0], size());
1297  }
1298  size_t size() const
1299  {
1300  return buffer_.size() - 1;
1301  }
1302  const wchar_t *c_str() const
1303  {
1304  return &buffer_[0];
1305  }
1306  std::wstring str() const
1307  {
1308  return std::wstring(&buffer_[0], size());
1309  }
1310 };
1311 
1312 // A converter from UTF-16 to UTF-8.
1313 // It is only provided for Windows since other systems support UTF-8 natively.
1314 class UTF16ToUTF8
1315 {
1316 private:
1318 
1319 public:
1320  UTF16ToUTF8() {}
1321  FMT_API explicit UTF16ToUTF8(WStringRef s);
1322  operator StringRef() const
1323  {
1324  return StringRef(&buffer_[0], size());
1325  }
1326  size_t size() const
1327  {
1328  return buffer_.size() - 1;
1329  }
1330  const char *c_str() const
1331  {
1332  return &buffer_[0];
1333  }
1334  std::string str() const
1335  {
1336  return std::string(&buffer_[0], size());
1337  }
1338 
1339  // Performs conversion returning a system error code instead of
1340  // throwing exception on conversion error. This method may still throw
1341  // in case of memory allocation error.
1342  FMT_API int convert(WStringRef s);
1343 };
1344 
1345 FMT_API void format_windows_error(fmt::Writer &out, int error_code, fmt::StringRef message) FMT_NOEXCEPT;
1346 #endif
1347 
1348 // A formatting argument value.
1349 struct Value
1350 {
1351  template<typename Char>
1353  {
1354  const Char *value;
1355  std::size_t size;
1356  };
1357 
1358  typedef void (*FormatFunc)(void *formatter, const void *arg, void *format_str_ptr);
1359 
1361  {
1362  const void *value;
1363  FormatFunc format;
1364  };
1365 
1366  union
1367  {
1368  int int_value;
1369  unsigned uint_value;
1370  LongLong long_long_value;
1371  ULongLong ulong_long_value;
1372  double double_value;
1373  long double long_double_value;
1374  const void *pointer;
1375  StringValue<char> string;
1376  StringValue<signed char> sstring;
1378  StringValue<wchar_t> wstring;
1379  CustomValue custom;
1380  };
1381 
1382  enum Type
1383  {
1384  NONE,
1385  NAMED_ARG,
1386  // Integer types should go first,
1387  INT,
1388  UINT,
1389  LONG_LONG,
1390  ULONG_LONG,
1391  BOOL,
1392  CHAR,
1393  LAST_INTEGER_TYPE = CHAR,
1394  // followed by floating-point types.
1395  DOUBLE,
1396  LONG_DOUBLE,
1397  LAST_NUMERIC_TYPE = LONG_DOUBLE,
1398  CSTRING,
1399  STRING,
1400  WSTRING,
1401  POINTER,
1402  CUSTOM
1403  };
1404 };
1405 
1406 // A formatting argument. It is a trivially copyable/constructible type to
1407 // allow storage in internal::MemoryBuffer.
1408 struct Arg : Value
1409 {
1410  Type type;
1411 };
1412 
1413 template<typename Char>
1414 struct NamedArg;
1415 template<typename Char, typename T>
1417 
1418 template<typename T = void>
1419 struct Null
1420 {
1421 };
1422 
1423 // A helper class template to enable or disable overloads taking wide
1424 // characters and strings in MakeValue.
1425 template<typename T, typename Char>
1427 {
1428  typedef Null<T> Supported;
1429  typedef T Unsupported;
1430 };
1431 
1432 template<typename T>
1434 {
1435  typedef T Supported;
1436  typedef Null<T> Unsupported;
1437 };
1438 
1439 typedef char Yes[1];
1440 typedef char No[2];
1441 
1442 template<typename T>
1443 T &get();
1444 
1445 // These are non-members to workaround an overload resolution bug in bcc32.
1446 Yes &convert(fmt::ULongLong);
1447 No &convert(...);
1448 
1449 template<typename T, bool ENABLE_CONVERSION>
1451 {
1452  enum
1453  {
1454  value = ENABLE_CONVERSION
1455  };
1456 };
1457 
1458 template<typename T, bool ENABLE_CONVERSION>
1460 {
1461  enum
1462  {
1463  value = false
1464  };
1465 };
1466 
1467 template<typename T>
1469 {
1470  enum
1471  {
1472  // Don't convert numeric types.
1474  };
1475 };
1476 
1477 template<typename T>
1479 {
1480  enum
1481  {
1482  enable_conversion = sizeof(fmt::internal::convert(get<T>())) == sizeof(Yes)
1483  };
1484  enum
1485  {
1487  };
1488 };
1489 
1490 #define FMT_DISABLE_CONVERSION_TO_INT(Type) \
1491  template<> \
1492  struct ConvertToInt<Type> \
1493  { \
1494  enum \
1495  { \
1496  value = 0 \
1497  }; \
1498  }
1499 
1500 // Silence warnings about convering float to int.
1501 FMT_DISABLE_CONVERSION_TO_INT(float);
1502 FMT_DISABLE_CONVERSION_TO_INT(double);
1503 FMT_DISABLE_CONVERSION_TO_INT(long double);
1504 
1505 template<bool B, class T = void>
1506 struct EnableIf
1507 {
1508 };
1509 
1510 template<class T>
1511 struct EnableIf<true, T>
1512 {
1513  typedef T type;
1514 };
1515 
1516 template<bool B, class T, class F>
1518 {
1519  typedef T type;
1520 };
1521 
1522 template<class T, class F>
1523 struct Conditional<false, T, F>
1524 {
1525  typedef F type;
1526 };
1527 
1528 // For bcc32 which doesn't understand ! in template arguments.
1529 template<bool>
1530 struct Not
1531 {
1532  enum
1533  {
1534  value = 0
1535  };
1536 };
1537 
1538 template<>
1539 struct Not<false>
1540 {
1541  enum
1542  {
1543  value = 1
1544  };
1545 };
1546 
1547 template<typename T>
1549 {
1550  enum
1551  {
1552  value = 0
1553  };
1554 };
1555 
1556 template<typename T, T>
1558 {
1559  LConvCheck(int) {}
1560 };
1561 
1562 // Returns the thousands separator for the current locale.
1563 // We check if ``lconv`` contains ``thousands_sep`` because on Android
1564 // ``lconv`` is stubbed as an empty struct.
1565 template<typename LConv>
1566 inline StringRef thousands_sep(LConv *lc, LConvCheck<char * LConv::*, &LConv::thousands_sep> = 0)
1567 {
1568  return lc->thousands_sep;
1569 }
1570 
1571 inline fmt::StringRef thousands_sep(...)
1572 {
1573  return "";
1574 }
1575 
1576 #define FMT_CONCAT(a, b) a##b
1577 
1578 #if FMT_GCC_VERSION >= 303
1579 #define FMT_UNUSED __attribute__((unused))
1580 #else
1581 #define FMT_UNUSED
1582 #endif
1583 
1584 #ifndef FMT_USE_STATIC_ASSERT
1585 #define FMT_USE_STATIC_ASSERT 0
1586 #endif
1587 
1588 #if FMT_USE_STATIC_ASSERT || FMT_HAS_FEATURE(cxx_static_assert) || (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600
1589 #define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message)
1590 #else
1591 #define FMT_CONCAT_(a, b) FMT_CONCAT(a, b)
1592 #define FMT_STATIC_ASSERT(cond, message) typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED
1593 #endif
1594 
1595 template<typename Formatter>
1596 void format_arg(Formatter &, ...)
1597 {
1598  FMT_STATIC_ASSERT(FalseType<Formatter>::value, "Cannot format argument. To enable the use of ostream "
1599  "operator<< include fmt/ostream.h. Otherwise provide "
1600  "an overload of format_arg.");
1601 }
1602 
1603 // Makes an Arg object from any type.
1604 template<typename Formatter>
1605 class MakeValue : public Arg
1606 {
1607 public:
1608  typedef typename Formatter::Char Char;
1609 
1610 private:
1611  // The following two methods are private to disallow formatting of
1612  // arbitrary pointers. If you want to output a pointer cast it to
1613  // "void *" or "const void *". In particular, this forbids formatting
1614  // of "[const] volatile char *" which is printed as bool by iostreams.
1615  // Do not implement!
1616  template<typename T>
1617  MakeValue(const T *value);
1618  template<typename T>
1619  MakeValue(T *value);
1620 
1621  // The following methods are private to disallow formatting of wide
1622  // characters and strings into narrow strings as in
1623  // fmt::format("{}", L"test");
1624  // To fix this, use a wide format string: fmt::format(L"{}", L"test").
1625 #if !FMT_MSC_VER || defined(_NATIVE_WCHAR_T_DEFINED)
1626  MakeValue(typename WCharHelper<wchar_t, Char>::Unsupported);
1627 #endif
1628  MakeValue(typename WCharHelper<wchar_t *, Char>::Unsupported);
1629  MakeValue(typename WCharHelper<const wchar_t *, Char>::Unsupported);
1630  MakeValue(typename WCharHelper<const std::wstring &, Char>::Unsupported);
1631 #if FMT_HAS_STRING_VIEW
1632  MakeValue(typename WCharHelper<const std::wstring_view &, Char>::Unsupported);
1633 #endif
1634  MakeValue(typename WCharHelper<WStringRef, Char>::Unsupported);
1635 
1636  void set_string(StringRef str)
1637  {
1638  string.value = str.data();
1639  string.size = str.size();
1640  }
1641 
1642  void set_string(WStringRef str)
1643  {
1644  wstring.value = str.data();
1645  wstring.size = str.size();
1646  }
1647 
1648  // Formats an argument of a custom type, such as a user-defined class.
1649  template<typename T>
1650  static void format_custom_arg(void *formatter, const void *arg, void *format_str_ptr)
1651  {
1652  format_arg(*static_cast<Formatter *>(formatter), *static_cast<const Char **>(format_str_ptr), *static_cast<const T *>(arg));
1653  }
1654 
1655 public:
1656  MakeValue() {}
1657 
1658 #define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \
1659  MakeValue(Type value) \
1660  { \
1661  field = rhs; \
1662  } \
1663  static uint64_t type(Type) \
1664  { \
1665  return Arg::TYPE; \
1666  }
1667 
1668 #define FMT_MAKE_VALUE(Type, field, TYPE) FMT_MAKE_VALUE_(Type, field, TYPE, value)
1669 
1670  FMT_MAKE_VALUE(bool, int_value, BOOL)
1671  FMT_MAKE_VALUE(short, int_value, INT)
1672  FMT_MAKE_VALUE(unsigned short, uint_value, UINT)
1673  FMT_MAKE_VALUE(int, int_value, INT)
1674  FMT_MAKE_VALUE(unsigned, uint_value, UINT)
1675 
1676  MakeValue(long value)
1677  {
1678  // To minimize the number of types we need to deal with, long is
1679  // translated either to int or to long long depending on its size.
1680  if (const_check(sizeof(long) == sizeof(int)))
1681  int_value = static_cast<int>(value);
1682  else
1683  long_long_value = value;
1684  }
1685  static uint64_t type(long)
1686  {
1687  return sizeof(long) == sizeof(int) ? Arg::INT : Arg::LONG_LONG;
1688  }
1689 
1690  MakeValue(unsigned long value)
1691  {
1692  if (const_check(sizeof(unsigned long) == sizeof(unsigned)))
1693  uint_value = static_cast<unsigned>(value);
1694  else
1695  ulong_long_value = value;
1696  }
1697  static uint64_t type(unsigned long)
1698  {
1699  return sizeof(unsigned long) == sizeof(unsigned) ? Arg::UINT : Arg::ULONG_LONG;
1700  }
1701 
1702  FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG)
1703  FMT_MAKE_VALUE(ULongLong, ulong_long_value, ULONG_LONG)
1704  FMT_MAKE_VALUE(float, double_value, DOUBLE)
1705  FMT_MAKE_VALUE(double, double_value, DOUBLE)
1706  FMT_MAKE_VALUE(long double, long_double_value, LONG_DOUBLE)
1707  FMT_MAKE_VALUE(signed char, int_value, INT)
1708  FMT_MAKE_VALUE(unsigned char, uint_value, UINT)
1709  FMT_MAKE_VALUE(char, int_value, CHAR)
1710 
1711 #if __cplusplus >= 201103L
1712  template<typename T, typename = typename std::enable_if<std::is_enum<T>::value && ConvertToInt<T>::value>::type>
1713  MakeValue(T value)
1714  {
1715  int_value = value;
1716  }
1717 
1718  template<typename T, typename = typename std::enable_if<std::is_enum<T>::value && ConvertToInt<T>::value>::type>
1719  static uint64_t type(T)
1720  {
1721  return Arg::INT;
1722  }
1723 #endif
1724 
1725 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
1727  {
1728  int_value = value;
1729  }
1730  static uint64_t type(wchar_t)
1731  {
1732  return Arg::CHAR;
1733  }
1734 #endif
1735 
1736 #define FMT_MAKE_STR_VALUE(Type, TYPE) \
1737  MakeValue(Type value) \
1738  { \
1739  set_string(value); \
1740  } \
1741  static uint64_t type(Type) \
1742  { \
1743  return Arg::TYPE; \
1744  }
1745 
1746  FMT_MAKE_VALUE(char *, string.value, CSTRING)
1747  FMT_MAKE_VALUE(const char *, string.value, CSTRING)
1748  FMT_MAKE_VALUE(signed char *, sstring.value, CSTRING)
1749  FMT_MAKE_VALUE(const signed char *, sstring.value, CSTRING)
1750  FMT_MAKE_VALUE(unsigned char *, ustring.value, CSTRING)
1751  FMT_MAKE_VALUE(const unsigned char *, ustring.value, CSTRING)
1752  FMT_MAKE_STR_VALUE(const std::string &, STRING)
1753 #if FMT_HAS_STRING_VIEW
1754  FMT_MAKE_STR_VALUE(const std::string_view &, STRING)
1755 #endif
1756  FMT_MAKE_STR_VALUE(StringRef, STRING)
1757  FMT_MAKE_VALUE_(CStringRef, string.value, CSTRING, value.c_str())
1758 
1759 #define FMT_MAKE_WSTR_VALUE(Type, TYPE) \
1760  MakeValue(typename WCharHelper<Type, Char>::Supported value) \
1761  { \
1762  set_string(value); \
1763  } \
1764  static uint64_t type(Type) \
1765  { \
1766  return Arg::TYPE; \
1767  }
1768 
1769  FMT_MAKE_WSTR_VALUE(wchar_t *, WSTRING)
1770  FMT_MAKE_WSTR_VALUE(const wchar_t *, WSTRING)
1771  FMT_MAKE_WSTR_VALUE(const std::wstring &, WSTRING)
1772 #if FMT_HAS_STRING_VIEW
1773  FMT_MAKE_WSTR_VALUE(const std::wstring_view &, WSTRING)
1774 #endif
1775  FMT_MAKE_WSTR_VALUE(WStringRef, WSTRING)
1776 
1777  FMT_MAKE_VALUE(void *, pointer, POINTER)
1778  FMT_MAKE_VALUE(const void *, pointer, POINTER)
1779 
1780  template<typename T>
1781  MakeValue(const T &value, typename EnableIf<Not<ConvertToInt<T>::value>::value, int>::type = 0)
1782  {
1783  custom.value = &value;
1784  custom.format = &format_custom_arg<T>;
1785  }
1786 
1787  template<typename T>
1788  static typename EnableIf<Not<ConvertToInt<T>::value>::value, uint64_t>::type type(const T &)
1789  {
1790  return Arg::CUSTOM;
1791  }
1792 
1793  // Additional template param `Char_` is needed here because make_type always
1794  // uses char.
1795  template<typename Char_>
1796  MakeValue(const NamedArg<Char_> &value)
1797  {
1798  pointer = &value;
1799  }
1800  template<typename Char_, typename T>
1802  {
1803  pointer = &value;
1804  }
1805 
1806  template<typename Char_>
1807  static uint64_t type(const NamedArg<Char_> &)
1808  {
1809  return Arg::NAMED_ARG;
1810  }
1811  template<typename Char_, typename T>
1812  static uint64_t type(const NamedArgWithType<Char_, T> &)
1813  {
1814  return Arg::NAMED_ARG;
1815  }
1816 };
1817 
1818 template<typename Formatter>
1819 class MakeArg : public Arg
1820 {
1821 public:
1822  MakeArg()
1823  {
1824  type = Arg::NONE;
1825  }
1826 
1827  template<typename T>
1828  MakeArg(const T &value)
1829  : Arg(MakeValue<Formatter>(value))
1830  {
1831  type = static_cast<Arg::Type>(MakeValue<Formatter>::type(value));
1832  }
1833 };
1834 
1835 template<typename Char>
1836 struct NamedArg : Arg
1837 {
1838  BasicStringRef<Char> name;
1839 
1840  template<typename T>
1841  NamedArg(BasicStringRef<Char> argname, const T &value)
1842  : Arg(MakeArg<BasicFormatter<Char>>(value))
1843  , name(argname)
1844  {
1845  }
1846 };
1847 
1848 template<typename Char, typename T>
1849 struct NamedArgWithType : NamedArg<Char>
1850 {
1851  NamedArgWithType(BasicStringRef<Char> argname, const T &value)
1852  : NamedArg<Char>(argname, value)
1853  {
1854  }
1855 };
1856 
1857 class RuntimeError : public std::runtime_error
1858 {
1859 protected:
1860  RuntimeError()
1861  : std::runtime_error("")
1862  {
1863  }
1864  RuntimeError(const RuntimeError &rerr)
1865  : std::runtime_error(rerr)
1866  {
1867  }
1868  FMT_API ~RuntimeError() FMT_DTOR_NOEXCEPT FMT_OVERRIDE;
1869 };
1870 
1871 template<typename Char>
1872 class ArgMap;
1873 } // namespace internal
1874 
1876 class ArgList
1877 {
1878 private:
1879  // To reduce compiled code size per formatting function call, types of first
1880  // MAX_PACKED_ARGS arguments are passed in the types_ field.
1881  uint64_t types_;
1882  union
1883  {
1884  // If the number of arguments is less than MAX_PACKED_ARGS, the argument
1885  // values are stored in values_, otherwise they are stored in args_.
1886  // This is done to reduce compiled code size as storing larger objects
1887  // may require more code (at least on x86-64) even if the same amount of
1888  // data is actually copied to stack. It saves ~10% on the bloat test.
1889  const internal::Value *values_;
1890  const internal::Arg *args_;
1891  };
1892 
1893  internal::Arg::Type type(unsigned index) const
1894  {
1895  return type(types_, index);
1896  }
1897 
1898  template<typename Char>
1899  friend class internal::ArgMap;
1900 
1901 public:
1902  // Maximum number of arguments with packed types.
1903  enum
1904  {
1905  MAX_PACKED_ARGS = 16
1906  };
1907 
1908  ArgList()
1909  : types_(0)
1910  {
1911  }
1912 
1913  ArgList(ULongLong types, const internal::Value *values)
1914  : types_(types)
1915  , values_(values)
1916  {
1917  }
1918  ArgList(ULongLong types, const internal::Arg *args)
1919  : types_(types)
1920  , args_(args)
1921  {
1922  }
1923 
1924  uint64_t types() const
1925  {
1926  return types_;
1927  }
1928 
1930  internal::Arg operator[](unsigned index) const
1931  {
1932  using internal::Arg;
1933  Arg arg;
1934  bool use_values = type(MAX_PACKED_ARGS - 1) == Arg::NONE;
1935  if (index < MAX_PACKED_ARGS)
1936  {
1937  Arg::Type arg_type = type(index);
1938  internal::Value &val = arg;
1939  if (arg_type != Arg::NONE)
1940  val = use_values ? values_[index] : args_[index];
1941  arg.type = arg_type;
1942  return arg;
1943  }
1944  if (use_values)
1945  {
1946  // The index is greater than the number of arguments that can be stored
1947  // in values, so return a "none" argument.
1948  arg.type = Arg::NONE;
1949  return arg;
1950  }
1951  for (unsigned i = MAX_PACKED_ARGS; i <= index; ++i)
1952  {
1953  if (args_[i].type == Arg::NONE)
1954  return args_[i];
1955  }
1956  return args_[index];
1957  }
1958 
1959  static internal::Arg::Type type(uint64_t types, unsigned index)
1960  {
1961  unsigned shift = index * 4;
1962  uint64_t mask = 0xf;
1963  return static_cast<internal::Arg::Type>((types & (mask << shift)) >> shift);
1964  }
1965 };
1966 
1967 #define FMT_DISPATCH(call) static_cast<Impl *>(this)->call
1968 
1993 template<typename Impl, typename Result>
1995 {
1996 private:
1997  typedef internal::Arg Arg;
1998 
1999 public:
2000  void report_unhandled_arg() {}
2001 
2002  Result visit_unhandled_arg()
2003  {
2004  FMT_DISPATCH(report_unhandled_arg());
2005  return Result();
2006  }
2007 
2009  Result visit_int(int value)
2010  {
2011  return FMT_DISPATCH(visit_any_int(value));
2012  }
2013 
2015  Result visit_long_long(LongLong value)
2016  {
2017  return FMT_DISPATCH(visit_any_int(value));
2018  }
2019 
2021  Result visit_uint(unsigned value)
2022  {
2023  return FMT_DISPATCH(visit_any_int(value));
2024  }
2025 
2027  Result visit_ulong_long(ULongLong value)
2028  {
2029  return FMT_DISPATCH(visit_any_int(value));
2030  }
2031 
2033  Result visit_bool(bool value)
2034  {
2035  return FMT_DISPATCH(visit_any_int(value));
2036  }
2037 
2039  Result visit_char(int value)
2040  {
2041  return FMT_DISPATCH(visit_any_int(value));
2042  }
2043 
2045  template<typename T>
2046  Result visit_any_int(T)
2047  {
2048  return FMT_DISPATCH(visit_unhandled_arg());
2049  }
2050 
2052  Result visit_double(double value)
2053  {
2054  return FMT_DISPATCH(visit_any_double(value));
2055  }
2056 
2058  Result visit_long_double(long double value)
2059  {
2060  return FMT_DISPATCH(visit_any_double(value));
2061  }
2062 
2064  template<typename T>
2066  {
2067  return FMT_DISPATCH(visit_unhandled_arg());
2068  }
2069 
2071  Result visit_cstring(const char *)
2072  {
2073  return FMT_DISPATCH(visit_unhandled_arg());
2074  }
2075 
2078  {
2079  return FMT_DISPATCH(visit_unhandled_arg());
2080  }
2081 
2084  {
2085  return FMT_DISPATCH(visit_unhandled_arg());
2086  }
2087 
2089  Result visit_pointer(const void *)
2090  {
2091  return FMT_DISPATCH(visit_unhandled_arg());
2092  }
2093 
2096  {
2097  return FMT_DISPATCH(visit_unhandled_arg());
2098  }
2099 
2108  Result visit(const Arg &arg)
2109  {
2110  switch (arg.type)
2111  {
2112  case Arg::NONE:
2113  case Arg::NAMED_ARG:
2114  FMT_ASSERT(false, "invalid argument type");
2115  break;
2116  case Arg::INT:
2117  return FMT_DISPATCH(visit_int(arg.int_value));
2118  case Arg::UINT:
2119  return FMT_DISPATCH(visit_uint(arg.uint_value));
2120  case Arg::LONG_LONG:
2121  return FMT_DISPATCH(visit_long_long(arg.long_long_value));
2122  case Arg::ULONG_LONG:
2123  return FMT_DISPATCH(visit_ulong_long(arg.ulong_long_value));
2124  case Arg::BOOL:
2125  return FMT_DISPATCH(visit_bool(arg.int_value != 0));
2126  case Arg::CHAR:
2127  return FMT_DISPATCH(visit_char(arg.int_value));
2128  case Arg::DOUBLE:
2129  return FMT_DISPATCH(visit_double(arg.double_value));
2130  case Arg::LONG_DOUBLE:
2131  return FMT_DISPATCH(visit_long_double(arg.long_double_value));
2132  case Arg::CSTRING:
2133  return FMT_DISPATCH(visit_cstring(arg.string.value));
2134  case Arg::STRING:
2135  return FMT_DISPATCH(visit_string(arg.string));
2136  case Arg::WSTRING:
2137  return FMT_DISPATCH(visit_wstring(arg.wstring));
2138  case Arg::POINTER:
2139  return FMT_DISPATCH(visit_pointer(arg.pointer));
2140  case Arg::CUSTOM:
2141  return FMT_DISPATCH(visit_custom(arg.custom));
2142  }
2143  return Result();
2144  }
2145 };
2146 
2147 enum Alignment
2148 {
2149  ALIGN_DEFAULT,
2150  ALIGN_LEFT,
2151  ALIGN_RIGHT,
2152  ALIGN_CENTER,
2153  ALIGN_NUMERIC
2154 };
2155 
2156 // Flags.
2157 enum
2158 {
2159  SIGN_FLAG = 1,
2160  PLUS_FLAG = 2,
2161  MINUS_FLAG = 4,
2162  HASH_FLAG = 8,
2163  CHAR_FLAG = 0x10 // Argument has char type - used in error reporting.
2164 };
2165 
2166 // An empty format specifier.
2168 {
2169 };
2170 
2171 // A type specifier.
2172 template<char TYPE>
2174 {
2175  Alignment align() const
2176  {
2177  return ALIGN_DEFAULT;
2178  }
2179  unsigned width() const
2180  {
2181  return 0;
2182  }
2183  int precision() const
2184  {
2185  return -1;
2186  }
2187  bool flag(unsigned) const
2188  {
2189  return false;
2190  }
2191  char type() const
2192  {
2193  return TYPE;
2194  }
2195  char type_prefix() const
2196  {
2197  return TYPE;
2198  }
2199  char fill() const
2200  {
2201  return ' ';
2202  }
2203 };
2204 
2205 // A width specifier.
2207 {
2208  unsigned width_;
2209  // Fill is always wchar_t and cast to char if necessary to avoid having
2210  // two specialization of WidthSpec and its subclasses.
2211  wchar_t fill_;
2212 
2213  WidthSpec(unsigned width, wchar_t fill)
2214  : width_(width)
2215  , fill_(fill)
2216  {
2217  }
2218 
2219  unsigned width() const
2220  {
2221  return width_;
2222  }
2223  wchar_t fill() const
2224  {
2225  return fill_;
2226  }
2227 };
2228 
2229 // An alignment specifier.
2231 {
2232  Alignment align_;
2233 
2234  AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT)
2235  : WidthSpec(width, fill)
2236  , align_(align)
2237  {
2238  }
2239 
2240  Alignment align() const
2241  {
2242  return align_;
2243  }
2244 
2245  int precision() const
2246  {
2247  return -1;
2248  }
2249 };
2250 
2251 // An alignment and type specifier.
2252 template<char TYPE>
2254 {
2255  AlignTypeSpec(unsigned width, wchar_t fill)
2256  : AlignSpec(width, fill)
2257  {
2258  }
2259 
2260  bool flag(unsigned) const
2261  {
2262  return false;
2263  }
2264  char type() const
2265  {
2266  return TYPE;
2267  }
2268  char type_prefix() const
2269  {
2270  return TYPE;
2271  }
2272 };
2273 
2274 // A full format specifier.
2276 {
2277  unsigned flags_;
2278  int precision_;
2279  char type_;
2280 
2281  FormatSpec(unsigned width = 0, char type = 0, wchar_t fill = ' ')
2282  : AlignSpec(width, fill)
2283  , flags_(0)
2284  , precision_(-1)
2285  , type_(type)
2286  {
2287  }
2288 
2289  bool flag(unsigned f) const
2290  {
2291  return (flags_ & f) != 0;
2292  }
2293  int precision() const
2294  {
2295  return precision_;
2296  }
2297  char type() const
2298  {
2299  return type_;
2300  }
2301  char type_prefix() const
2302  {
2303  return type_;
2304  }
2305 };
2306 
2307 // An integer format specifier.
2308 template<typename T, typename SpecT = TypeSpec<0>, typename Char = char>
2309 class IntFormatSpec : public SpecT
2310 {
2311 private:
2312  T value_;
2313 
2314 public:
2315  IntFormatSpec(T val, const SpecT &spec = SpecT())
2316  : SpecT(spec)
2317  , value_(val)
2318  {
2319  }
2320 
2321  T value() const
2322  {
2323  return value_;
2324  }
2325 };
2326 
2327 // A string format specifier.
2328 template<typename Char>
2329 class StrFormatSpec : public AlignSpec
2330 {
2331 private:
2332  const Char *str_;
2333 
2334 public:
2335  template<typename FillChar>
2336  StrFormatSpec(const Char *str, unsigned width, FillChar fill)
2337  : AlignSpec(width, fill)
2338  , str_(str)
2339  {
2341  }
2342 
2343  const Char *str() const
2344  {
2345  return str_;
2346  }
2347 };
2348 
2352 IntFormatSpec<int, TypeSpec<'b'>> bin(int value);
2353 
2357 IntFormatSpec<int, TypeSpec<'o'>> oct(int value);
2358 
2363 IntFormatSpec<int, TypeSpec<'x'>> hex(int value);
2364 
2369 IntFormatSpec<int, TypeSpec<'X'>> hexu(int value);
2370 
2385 template<char TYPE_CODE, typename Char>
2386 IntFormatSpec<int, AlignTypeSpec<TYPE_CODE>, Char> pad(int value, unsigned width, Char fill = ' ');
2387 
2388 #define FMT_DEFINE_INT_FORMATTERS(TYPE) \
2389  inline IntFormatSpec<TYPE, TypeSpec<'b'>> bin(TYPE value) \
2390  { \
2391  return IntFormatSpec<TYPE, TypeSpec<'b'>>(value, TypeSpec<'b'>()); \
2392  } \
2393  \
2394  inline IntFormatSpec<TYPE, TypeSpec<'o'>> oct(TYPE value) \
2395  { \
2396  return IntFormatSpec<TYPE, TypeSpec<'o'>>(value, TypeSpec<'o'>()); \
2397  } \
2398  \
2399  inline IntFormatSpec<TYPE, TypeSpec<'x'>> hex(TYPE value) \
2400  { \
2401  return IntFormatSpec<TYPE, TypeSpec<'x'>>(value, TypeSpec<'x'>()); \
2402  } \
2403  \
2404  inline IntFormatSpec<TYPE, TypeSpec<'X'>> hexu(TYPE value) \
2405  { \
2406  return IntFormatSpec<TYPE, TypeSpec<'X'>>(value, TypeSpec<'X'>()); \
2407  } \
2408  \
2409  template<char TYPE_CODE> \
2410  inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>> pad(IntFormatSpec<TYPE, TypeSpec<TYPE_CODE>> f, unsigned width) \
2411  { \
2412  return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>>(f.value(), AlignTypeSpec<TYPE_CODE>(width, ' ')); \
2413  } \
2414  \
2415  /* For compatibility with older compilers we provide two overloads for pad, */ \
2416  /* one that takes a fill character and one that doesn't. In the future this */ \
2417  /* can be replaced with one overload making the template argument Char */ \
2418  /* default to char (C++11). */ \
2419  template<char TYPE_CODE, typename Char> \
2420  inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char> pad( \
2421  IntFormatSpec<TYPE, TypeSpec<TYPE_CODE>, Char> f, unsigned width, Char fill) \
2422  { \
2423  return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char>(f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \
2424  } \
2425  \
2426  inline IntFormatSpec<TYPE, AlignTypeSpec<0>> pad(TYPE value, unsigned width) \
2427  { \
2428  return IntFormatSpec<TYPE, AlignTypeSpec<0>>(value, AlignTypeSpec<0>(width, ' ')); \
2429  } \
2430  \
2431  template<typename Char> \
2432  inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad(TYPE value, unsigned width, Char fill) \
2433  { \
2434  return IntFormatSpec<TYPE, AlignTypeSpec<0>, Char>(value, AlignTypeSpec<0>(width, fill)); \
2435  }
2436 
2437 FMT_DEFINE_INT_FORMATTERS(int)
2438 FMT_DEFINE_INT_FORMATTERS(long)
2439 FMT_DEFINE_INT_FORMATTERS(unsigned)
2440 FMT_DEFINE_INT_FORMATTERS(unsigned long)
2441 FMT_DEFINE_INT_FORMATTERS(LongLong)
2442 FMT_DEFINE_INT_FORMATTERS(ULongLong)
2443 
2444 
2456 template<typename Char>
2457 inline StrFormatSpec<Char> pad(const Char *str, unsigned width, Char fill = ' ')
2458 {
2459  return StrFormatSpec<Char>(str, width, fill);
2460 }
2461 
2462 inline StrFormatSpec<wchar_t> pad(const wchar_t *str, unsigned width, char fill = ' ')
2463 {
2464  return StrFormatSpec<wchar_t>(str, width, fill);
2465 }
2466 
2467 namespace internal {
2468 
2469 template<typename Char>
2470 class ArgMap
2471 {
2472 private:
2473  typedef std::vector<std::pair<fmt::BasicStringRef<Char>, internal::Arg>> MapType;
2474  typedef typename MapType::value_type Pair;
2475 
2476  MapType map_;
2477 
2478 public:
2479  void init(const ArgList &args);
2480 
2481  const internal::Arg *find(const fmt::BasicStringRef<Char> &name) const
2482  {
2483  // The list is unsorted, so just return the first matching name.
2484  for (typename MapType::const_iterator it = map_.begin(), end = map_.end(); it != end; ++it)
2485  {
2486  if (it->first == name)
2487  return &it->second;
2488  }
2489  return FMT_NULL;
2490  }
2491 };
2492 
2493 template<typename Char>
2494 void ArgMap<Char>::init(const ArgList &args)
2495 {
2496  if (!map_.empty())
2497  return;
2499  const NamedArg *named_arg = FMT_NULL;
2500  bool use_values = args.type(ArgList::MAX_PACKED_ARGS - 1) == internal::Arg::NONE;
2501  if (use_values)
2502  {
2503  for (unsigned i = 0; /*nothing*/; ++i)
2504  {
2505  internal::Arg::Type arg_type = args.type(i);
2506  switch (arg_type)
2507  {
2508  case internal::Arg::NONE:
2509  return;
2510  case internal::Arg::NAMED_ARG:
2511  named_arg = static_cast<const NamedArg *>(args.values_[i].pointer);
2512  map_.push_back(Pair(named_arg->name, *named_arg));
2513  break;
2514  default:
2515  /*nothing*/
2516  ;
2517  }
2518  }
2519  return;
2520  }
2521  for (unsigned i = 0; i != ArgList::MAX_PACKED_ARGS; ++i)
2522  {
2523  internal::Arg::Type arg_type = args.type(i);
2524  if (arg_type == internal::Arg::NAMED_ARG)
2525  {
2526  named_arg = static_cast<const NamedArg *>(args.args_[i].pointer);
2527  map_.push_back(Pair(named_arg->name, *named_arg));
2528  }
2529  }
2530  for (unsigned i = ArgList::MAX_PACKED_ARGS; /*nothing*/; ++i)
2531  {
2532  switch (args.args_[i].type)
2533  {
2534  case internal::Arg::NONE:
2535  return;
2536  case internal::Arg::NAMED_ARG:
2537  named_arg = static_cast<const NamedArg *>(args.args_[i].pointer);
2538  map_.push_back(Pair(named_arg->name, *named_arg));
2539  break;
2540  default:
2541  /*nothing*/
2542  ;
2543  }
2544  }
2545 }
2546 
2547 template<typename Impl, typename Char, typename Spec = fmt::FormatSpec>
2548 class ArgFormatterBase : public ArgVisitor<Impl, void>
2549 {
2550 private:
2551  BasicWriter<Char> &writer_;
2552  Spec &spec_;
2553 
2554  FMT_DISALLOW_COPY_AND_ASSIGN(ArgFormatterBase);
2555 
2556  void write_pointer(const void *p)
2557  {
2558  spec_.flags_ = HASH_FLAG;
2559  spec_.type_ = 'x';
2560  writer_.write_int(reinterpret_cast<uintptr_t>(p), spec_);
2561  }
2562 
2563  // workaround MSVC two-phase lookup issue
2564  typedef internal::Arg Arg;
2565 
2566 protected:
2567  BasicWriter<Char> &writer()
2568  {
2569  return writer_;
2570  }
2571  Spec &spec()
2572  {
2573  return spec_;
2574  }
2575 
2576  void write(bool value)
2577  {
2578  const char *str_value = value ? "true" : "false";
2579  Arg::StringValue<char> str = {str_value, std::strlen(str_value)};
2580  writer_.write_str(str, spec_);
2581  }
2582 
2583  void write(const char *value)
2584  {
2585  Arg::StringValue<char> str = {value, value ? std::strlen(value) : 0};
2586  writer_.write_str(str, spec_);
2587  }
2588 
2589 public:
2590  typedef Spec SpecType;
2591 
2592  ArgFormatterBase(BasicWriter<Char> &w, Spec &s)
2593  : writer_(w)
2594  , spec_(s)
2595  {
2596  }
2597 
2598  template<typename T>
2599  void visit_any_int(T value)
2600  {
2601  writer_.write_int(value, spec_);
2602  }
2603 
2604  template<typename T>
2605  void visit_any_double(T value)
2606  {
2607  writer_.write_double(value, spec_);
2608  }
2609 
2610  void visit_bool(bool value)
2611  {
2612  if (spec_.type_)
2613  {
2614  visit_any_int(value);
2615  return;
2616  }
2617  write(value);
2618  }
2619 
2620  void visit_char(int value)
2621  {
2622  if (spec_.type_ && spec_.type_ != 'c')
2623  {
2624  spec_.flags_ |= CHAR_FLAG;
2625  writer_.write_int(value, spec_);
2626  return;
2627  }
2628  if (spec_.align_ == ALIGN_NUMERIC || spec_.flags_ != 0)
2629  FMT_THROW(FormatError("invalid format specifier for char"));
2630  typedef typename BasicWriter<Char>::CharPtr CharPtr;
2631  Char fill = internal::CharTraits<Char>::cast(spec_.fill());
2632  CharPtr out = CharPtr();
2633  const unsigned CHAR_SIZE = 1;
2634  if (spec_.width_ > CHAR_SIZE)
2635  {
2636  out = writer_.grow_buffer(spec_.width_);
2637  if (spec_.align_ == ALIGN_RIGHT)
2638  {
2639  std::uninitialized_fill_n(out, spec_.width_ - CHAR_SIZE, fill);
2640  out += spec_.width_ - CHAR_SIZE;
2641  }
2642  else if (spec_.align_ == ALIGN_CENTER)
2643  {
2644  out = writer_.fill_padding(out, spec_.width_, internal::const_check(CHAR_SIZE), fill);
2645  }
2646  else
2647  {
2648  std::uninitialized_fill_n(out + CHAR_SIZE, spec_.width_ - CHAR_SIZE, fill);
2649  }
2650  }
2651  else
2652  {
2653  out = writer_.grow_buffer(CHAR_SIZE);
2654  }
2655  *out = internal::CharTraits<Char>::cast(value);
2656  }
2657 
2658  void visit_cstring(const char *value)
2659  {
2660  if (spec_.type_ == 'p')
2661  return write_pointer(value);
2662  write(value);
2663  }
2664 
2665  // Qualification with "internal" here and below is a workaround for nvcc.
2666  void visit_string(internal::Arg::StringValue<char> value)
2667  {
2668  writer_.write_str(value, spec_);
2669  }
2670 
2672 
2673  void visit_wstring(internal::Arg::StringValue<Char> value)
2674  {
2675  writer_.write_str(value, spec_);
2676  }
2677 
2678  void visit_pointer(const void *value)
2679  {
2680  if (spec_.type_ && spec_.type_ != 'p')
2681  report_unknown_type(spec_.type_, "pointer");
2682  write_pointer(value);
2683  }
2684 };
2685 
2687 {
2688 private:
2689  ArgList args_;
2690  int next_arg_index_;
2691 
2692  // Returns the argument with specified index.
2693  FMT_API Arg do_get_arg(unsigned arg_index, const char *&error);
2694 
2695 protected:
2696  const ArgList &args() const
2697  {
2698  return args_;
2699  }
2700 
2701  explicit FormatterBase(const ArgList &args)
2702  {
2703  args_ = args;
2704  next_arg_index_ = 0;
2705  }
2706 
2707  // Returns the next argument.
2708  Arg next_arg(const char *&error)
2709  {
2710  if (next_arg_index_ >= 0)
2711  return do_get_arg(internal::to_unsigned(next_arg_index_++), error);
2712  error = "cannot switch from manual to automatic argument indexing";
2713  return Arg();
2714  }
2715 
2716  // Checks if manual indexing is used and returns the argument with
2717  // specified index.
2718  Arg get_arg(unsigned arg_index, const char *&error)
2719  {
2720  return check_no_auto_index(error) ? do_get_arg(arg_index, error) : Arg();
2721  }
2722 
2723  bool check_no_auto_index(const char *&error)
2724  {
2725  if (next_arg_index_ > 0)
2726  {
2727  error = "cannot switch from automatic to manual argument indexing";
2728  return false;
2729  }
2730  next_arg_index_ = -1;
2731  return true;
2732  }
2733 
2734  template<typename Char>
2735  void write(BasicWriter<Char> &w, const Char *start, const Char *end)
2736  {
2737  if (start != end)
2738  w << BasicStringRef<Char>(start, internal::to_unsigned(end - start));
2739  }
2740 };
2741 } // namespace internal
2742 
2760 template<typename Impl, typename Char, typename Spec = fmt::FormatSpec>
2761 class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char, Spec>
2762 {
2763 private:
2764  BasicFormatter<Char, Impl> &formatter_;
2765  const Char *format_;
2766 
2767 public:
2776  BasicArgFormatter(BasicFormatter<Char, Impl> &formatter, Spec &spec, const Char *fmt)
2777  : internal::ArgFormatterBase<Impl, Char, Spec>(formatter.writer(), spec)
2778  , formatter_(formatter)
2779  , format_(fmt)
2780  {
2781  }
2782 
2785  {
2786  c.format(&formatter_, c.value, &format_);
2787  }
2788 };
2789 
2791 template<typename Char>
2792 class ArgFormatter : public BasicArgFormatter<ArgFormatter<Char>, Char, FormatSpec>
2793 {
2794 public:
2796  ArgFormatter(BasicFormatter<Char> &formatter, FormatSpec &spec, const Char *fmt)
2797  : BasicArgFormatter<ArgFormatter<Char>, Char, FormatSpec>(formatter, spec, fmt)
2798  {
2799  }
2800 };
2801 
2803 template<typename CharType, typename ArgFormatter>
2805 {
2806 public:
2808  typedef CharType Char;
2809 
2810 private:
2811  BasicWriter<Char> &writer_;
2813 
2814  FMT_DISALLOW_COPY_AND_ASSIGN(BasicFormatter);
2815 
2816  using internal::FormatterBase::get_arg;
2817 
2818  // Checks if manual indexing is used and returns the argument with
2819  // specified name.
2820  internal::Arg get_arg(BasicStringRef<Char> arg_name, const char *&error);
2821 
2822  // Parses argument index and returns corresponding argument.
2823  internal::Arg parse_arg_index(const Char *&s);
2824 
2825  // Parses argument name and returns corresponding argument.
2826  internal::Arg parse_arg_name(const Char *&s);
2827 
2828 public:
2837  : internal::FormatterBase(args)
2838  , writer_(w)
2839  {
2840  }
2841 
2844  {
2845  return writer_;
2846  }
2847 
2849  void format(BasicCStringRef<Char> format_str);
2850 
2851  // Formats a single argument and advances format_str, a format string pointer.
2852  const Char *format(const Char *&format_str, const internal::Arg &arg);
2853 };
2854 
2855 // Generates a comma-separated list with results of applying f to
2856 // numbers 0..n-1.
2857 #define FMT_GEN(n, f) FMT_GEN##n(f)
2858 #define FMT_GEN1(f) f(0)
2859 #define FMT_GEN2(f) FMT_GEN1(f), f(1)
2860 #define FMT_GEN3(f) FMT_GEN2(f), f(2)
2861 #define FMT_GEN4(f) FMT_GEN3(f), f(3)
2862 #define FMT_GEN5(f) FMT_GEN4(f), f(4)
2863 #define FMT_GEN6(f) FMT_GEN5(f), f(5)
2864 #define FMT_GEN7(f) FMT_GEN6(f), f(6)
2865 #define FMT_GEN8(f) FMT_GEN7(f), f(7)
2866 #define FMT_GEN9(f) FMT_GEN8(f), f(8)
2867 #define FMT_GEN10(f) FMT_GEN9(f), f(9)
2868 #define FMT_GEN11(f) FMT_GEN10(f), f(10)
2869 #define FMT_GEN12(f) FMT_GEN11(f), f(11)
2870 #define FMT_GEN13(f) FMT_GEN12(f), f(12)
2871 #define FMT_GEN14(f) FMT_GEN13(f), f(13)
2872 #define FMT_GEN15(f) FMT_GEN14(f), f(14)
2873 
2874 namespace internal {
2875 inline uint64_t make_type()
2876 {
2877  return 0;
2878 }
2879 
2880 template<typename T>
2881 inline uint64_t make_type(const T &arg)
2882 {
2883  return MakeValue<BasicFormatter<char>>::type(arg);
2884 }
2885 
2886 template<std::size_t N, bool /*IsPacked*/ = (N < ArgList::MAX_PACKED_ARGS)>
2887 struct ArgArray;
2888 
2889 template<std::size_t N>
2890 struct ArgArray<N, true /*IsPacked*/>
2891 {
2892  // '+' is used to silence GCC -Wduplicated-branches warning.
2893  typedef Value Type[N > 0 ? N : +1];
2894 
2895  template<typename Formatter, typename T>
2896  static Value make(const T &value)
2897  {
2898 #ifdef __clang__
2899  Value result = MakeValue<Formatter>(value);
2900  // Workaround a bug in Apple LLVM version 4.2 (clang-425.0.28) of clang:
2901  // https://github.com/fmtlib/fmt/issues/276
2902  (void)result.custom.format;
2903  return result;
2904 #else
2905  return MakeValue<Formatter>(value);
2906 #endif
2907  }
2908 };
2909 
2910 template<std::size_t N>
2911 struct ArgArray<N, false /*IsPacked*/>
2912 {
2913  typedef Arg Type[N + 1]; // +1 for the list end Arg::NONE
2914 
2915  template<typename Formatter, typename T>
2916  static Arg make(const T &value)
2917  {
2918  return MakeArg<Formatter>(value);
2919  }
2920 };
2921 
2922 #if FMT_USE_VARIADIC_TEMPLATES
2923 template<typename Arg, typename... Args>
2924 inline uint64_t make_type(const Arg &first, const Args &... tail)
2925 {
2926  return make_type(first) | (make_type(tail...) << 4);
2927 }
2928 
2929 #else
2930 
2931 struct ArgType
2932 {
2933  uint64_t type;
2934 
2935  ArgType()
2936  : type(0)
2937  {
2938  }
2939 
2940  template<typename T>
2941  ArgType(const T &arg)
2942  : type(make_type(arg))
2943  {
2944  }
2945 };
2946 
2947 #define FMT_ARG_TYPE_DEFAULT(n) ArgType t##n = ArgType()
2948 
2949 inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT))
2950 {
2951  return t0.type | (t1.type << 4) | (t2.type << 8) | (t3.type << 12) | (t4.type << 16) | (t5.type << 20) | (t6.type << 24) |
2952  (t7.type << 28) | (t8.type << 32) | (t9.type << 36) | (t10.type << 40) | (t11.type << 44) | (t12.type << 48) | (t13.type << 52) |
2953  (t14.type << 56);
2954 }
2955 #endif
2956 } // namespace internal
2957 
2958 #define FMT_MAKE_TEMPLATE_ARG(n) typename T##n
2959 #define FMT_MAKE_ARG_TYPE(n) T##n
2960 #define FMT_MAKE_ARG(n) const T##n &v##n
2961 #define FMT_ASSIGN_char(n) arr[n] = fmt::internal::MakeValue<fmt::BasicFormatter<char>>(v##n)
2962 #define FMT_ASSIGN_wchar_t(n) arr[n] = fmt::internal::MakeValue<fmt::BasicFormatter<wchar_t>>(v##n)
2963 
2964 #if FMT_USE_VARIADIC_TEMPLATES
2965 // Defines a variadic function returning void.
2966 #define FMT_VARIADIC_VOID(func, arg_type) \
2967  template<typename... Args> \
2968  void func(arg_type arg0, const Args &... args) \
2969  { \
2970  typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
2971  typename ArgArray::Type array{ArgArray::template make<fmt::BasicFormatter<Char>>(args)...}; \
2972  func(arg0, fmt::ArgList(fmt::internal::make_type(args...), array)); \
2973  }
2974 
2975 // Defines a variadic constructor.
2976 #define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
2977  template<typename... Args> \
2978  ctor(arg0_type arg0, arg1_type arg1, const Args &... args) \
2979  { \
2980  typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
2981  typename ArgArray::Type array{ArgArray::template make<fmt::BasicFormatter<Char>>(args)...}; \
2982  func(arg0, arg1, fmt::ArgList(fmt::internal::make_type(args...), array)); \
2983  }
2984 
2985 #else
2986 
2987 #define FMT_MAKE_REF(n) fmt::internal::MakeValue<fmt::BasicFormatter<Char>>(v##n)
2988 #define FMT_MAKE_REF2(n) v##n
2989 
2990 // Defines a wrapper for a function taking one argument of type arg_type
2991 // and n additional arguments of arbitrary types.
2992 #define FMT_WRAP1(func, arg_type, n) \
2993  template<FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2994  inline void func(arg_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) \
2995  { \
2996  const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
2997  func(arg1, fmt::ArgList(fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
2998  }
2999 
3000 // Emulates a variadic function returning void on a pre-C++11 compiler.
3001 #define FMT_VARIADIC_VOID(func, arg_type) \
3002  inline void func(arg_type arg) \
3003  { \
3004  func(arg, fmt::ArgList()); \
3005  } \
3006  FMT_WRAP1(func, arg_type, 1) \
3007  FMT_WRAP1(func, arg_type, 2) \
3008  FMT_WRAP1(func, arg_type, 3) \
3009  FMT_WRAP1(func, arg_type, 4) \
3010  FMT_WRAP1(func, arg_type, 5) \
3011  FMT_WRAP1(func, arg_type, 6) \
3012  FMT_WRAP1(func, arg_type, 7) FMT_WRAP1(func, arg_type, 8) FMT_WRAP1(func, arg_type, 9) FMT_WRAP1(func, arg_type, 10)
3013 
3014 #define FMT_CTOR(ctor, func, arg0_type, arg1_type, n) \
3015  template<FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
3016  ctor(arg0_type arg0, arg1_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) \
3017  { \
3018  const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
3019  func(arg0, arg1, fmt::ArgList(fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
3020  }
3021 
3022 // Emulates a variadic constructor on a pre-C++11 compiler.
3023 #define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
3024  FMT_CTOR(ctor, func, arg0_type, arg1_type, 1) \
3025  FMT_CTOR(ctor, func, arg0_type, arg1_type, 2) \
3026  FMT_CTOR(ctor, func, arg0_type, arg1_type, 3) \
3027  FMT_CTOR(ctor, func, arg0_type, arg1_type, 4) \
3028  FMT_CTOR(ctor, func, arg0_type, arg1_type, 5) \
3029  FMT_CTOR(ctor, func, arg0_type, arg1_type, 6) \
3030  FMT_CTOR(ctor, func, arg0_type, arg1_type, 7) \
3031  FMT_CTOR(ctor, func, arg0_type, arg1_type, 8) \
3032  FMT_CTOR(ctor, func, arg0_type, arg1_type, 9) \
3033  FMT_CTOR(ctor, func, arg0_type, arg1_type, 10)
3034 #endif
3035 
3036 // Generates a comma-separated list with results of applying f to pairs
3037 // (argument, index).
3038 #define FMT_FOR_EACH1(f, x0) f(x0, 0)
3039 #define FMT_FOR_EACH2(f, x0, x1) FMT_FOR_EACH1(f, x0), f(x1, 1)
3040 #define FMT_FOR_EACH3(f, x0, x1, x2) FMT_FOR_EACH2(f, x0, x1), f(x2, 2)
3041 #define FMT_FOR_EACH4(f, x0, x1, x2, x3) FMT_FOR_EACH3(f, x0, x1, x2), f(x3, 3)
3042 #define FMT_FOR_EACH5(f, x0, x1, x2, x3, x4) FMT_FOR_EACH4(f, x0, x1, x2, x3), f(x4, 4)
3043 #define FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5) FMT_FOR_EACH5(f, x0, x1, x2, x3, x4), f(x5, 5)
3044 #define FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6) FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5), f(x6, 6)
3045 #define FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7) FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6), f(x7, 7)
3046 #define FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8) FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7), f(x8, 8)
3047 #define FMT_FOR_EACH10(f, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8), f(x9, 9)
3048 
3054 {
3055 private:
3056  FMT_API void init(int err_code, CStringRef format_str, ArgList args);
3057 
3058 protected:
3059  int error_code_;
3060 
3061  typedef char Char; // For FMT_VARIADIC_CTOR.
3062 
3063  SystemError() {}
3064 
3065 public:
3084  SystemError(int error_code, CStringRef message)
3085  {
3086  init(error_code, message, ArgList());
3087  }
3088  FMT_DEFAULTED_COPY_CTOR(SystemError)
3089  FMT_VARIADIC_CTOR(SystemError, init, int, CStringRef)
3090 
3091  FMT_API ~SystemError() FMT_DTOR_NOEXCEPT FMT_OVERRIDE;
3092 
3093  int error_code() const
3094  {
3095  return error_code_;
3096  }
3097 };
3098 
3115 FMT_API void format_system_error(fmt::Writer &out, int error_code, fmt::StringRef message) FMT_NOEXCEPT;
3116 
3135 template<typename Char>
3136 class BasicWriter
3137 {
3138 private:
3139  // Output buffer.
3140  Buffer<Char> &buffer_;
3141 
3142  FMT_DISALLOW_COPY_AND_ASSIGN(BasicWriter);
3143 
3144  typedef typename internal::CharTraits<Char>::CharPtr CharPtr;
3145 
3146 #if FMT_SECURE_SCL
3147  // Returns pointer value.
3148  static Char *get(CharPtr p)
3149  {
3150  return p.base();
3151  }
3152 #else
3153  static Char *get(Char *p)
3154  {
3155  return p;
3156  }
3157 #endif
3158 
3159  // Fills the padding around the content and returns the pointer to the
3160  // content area.
3161  static CharPtr fill_padding(CharPtr buffer, unsigned total_size, std::size_t content_size, wchar_t fill);
3162 
3163  // Grows the buffer by n characters and returns a pointer to the newly
3164  // allocated area.
3165  CharPtr grow_buffer(std::size_t n)
3166  {
3167  std::size_t size = buffer_.size();
3168  buffer_.resize(size + n);
3169  return internal::make_ptr(&buffer_[size], n);
3170  }
3171 
3172  // Writes an unsigned decimal integer.
3173  template<typename UInt>
3174  Char *write_unsigned_decimal(UInt value, unsigned prefix_size = 0)
3175  {
3176  unsigned num_digits = internal::count_digits(value);
3177  Char *ptr = get(grow_buffer(prefix_size + num_digits));
3178  internal::format_decimal(ptr + prefix_size, value, num_digits);
3179  return ptr;
3180  }
3181 
3182  // Writes a decimal integer.
3183  template<typename Int>
3184  void write_decimal(Int value)
3185  {
3186  typedef typename internal::IntTraits<Int>::MainType MainType;
3187  MainType abs_value = static_cast<MainType>(value);
3188  if (internal::is_negative(value))
3189  {
3190  abs_value = 0 - abs_value;
3191  *write_unsigned_decimal(abs_value, 1) = '-';
3192  }
3193  else
3194  {
3195  write_unsigned_decimal(abs_value, 0);
3196  }
3197  }
3198 
3199  // Prepare a buffer for integer formatting.
3200  CharPtr prepare_int_buffer(unsigned num_digits, const EmptySpec &, const char *prefix, unsigned prefix_size)
3201  {
3202  unsigned size = prefix_size + num_digits;
3203  CharPtr p = grow_buffer(size);
3204  std::uninitialized_copy(prefix, prefix + prefix_size, p);
3205  return p + size - 1;
3206  }
3207 
3208  template<typename Spec>
3209  CharPtr prepare_int_buffer(unsigned num_digits, const Spec &spec, const char *prefix, unsigned prefix_size);
3210 
3211  // Formats an integer.
3212  template<typename T, typename Spec>
3213  void write_int(T value, Spec spec);
3214 
3215  // Formats a floating-point number (double or long double).
3216  template<typename T, typename Spec>
3217  void write_double(T value, const Spec &spec);
3218 
3219  // Writes a formatted string.
3220  template<typename StrChar>
3221  CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec);
3222 
3223  template<typename StrChar, typename Spec>
3224  void write_str(const internal::Arg::StringValue<StrChar> &str, const Spec &spec);
3225 
3226  // This following methods are private to disallow writing wide characters
3227  // and strings to a char stream. If you want to print a wide string as a
3228  // pointer as std::ostream does, cast it to const void*.
3229  // Do not implement!
3230  void operator<<(typename internal::WCharHelper<wchar_t, Char>::Unsupported);
3231  void operator<<(typename internal::WCharHelper<const wchar_t *, Char>::Unsupported);
3232 
3233  // Appends floating-point length specifier to the format string.
3234  // The second argument is only used for overload resolution.
3235  void append_float_length(Char *&format_ptr, long double)
3236  {
3237  *format_ptr++ = 'L';
3238  }
3239 
3240  template<typename T>
3241  void append_float_length(Char *&, T)
3242  {
3243  }
3244 
3245  template<typename Impl, typename Char_, typename Spec_>
3246  friend class internal::ArgFormatterBase;
3247 
3248  template<typename Impl, typename Char_, typename Spec_>
3249  friend class BasicPrintfArgFormatter;
3250 
3251 protected:
3256  : buffer_(b)
3257  {
3258  }
3259 
3260 public:
3266  virtual ~BasicWriter() {}
3267 
3271  std::size_t size() const
3272  {
3273  return buffer_.size();
3274  }
3275 
3280  const Char *data() const FMT_NOEXCEPT
3281  {
3282  return &buffer_[0];
3283  }
3284 
3289  const Char *c_str() const
3290  {
3291  std::size_t size = buffer_.size();
3292  buffer_.reserve(size + 1);
3293  buffer_[size] = '\0';
3294  return &buffer_[0];
3295  }
3296 
3302  std::basic_string<Char> str() const
3303  {
3304  return std::basic_string<Char>(&buffer_[0], buffer_.size());
3305  }
3306 
3333  {
3334  BasicFormatter<Char>(args, *this).format(format);
3335  }
3336  FMT_VARIADIC_VOID(write, BasicCStringRef<Char>)
3337 
3338  BasicWriter &operator<<(int value)
3339  {
3340  write_decimal(value);
3341  return *this;
3342  }
3343  BasicWriter &operator<<(unsigned value)
3344  {
3345  return *this << IntFormatSpec<unsigned>(value);
3346  }
3347  BasicWriter &operator<<(long value)
3348  {
3349  write_decimal(value);
3350  return *this;
3351  }
3352  BasicWriter &operator<<(unsigned long value)
3353  {
3354  return *this << IntFormatSpec<unsigned long>(value);
3355  }
3356  BasicWriter &operator<<(LongLong value)
3357  {
3358  write_decimal(value);
3359  return *this;
3360  }
3361 
3367  BasicWriter &operator<<(ULongLong value)
3368  {
3369  return *this << IntFormatSpec<ULongLong>(value);
3370  }
3371 
3372  BasicWriter &operator<<(double value)
3373  {
3374  write_double(value, FormatSpec());
3375  return *this;
3376  }
3377 
3384  BasicWriter &operator<<(long double value)
3385  {
3386  write_double(value, FormatSpec());
3387  return *this;
3388  }
3389 
3394  {
3395  buffer_.push_back(value);
3396  return *this;
3397  }
3398 
3399  BasicWriter &operator<<(typename internal::WCharHelper<wchar_t, Char>::Supported value)
3400  {
3401  buffer_.push_back(value);
3402  return *this;
3403  }
3404 
3410  BasicWriter &operator<<(fmt::BasicStringRef<Char> value)
3411  {
3412  const Char *str = value.data();
3413  buffer_.append(str, str + value.size());
3414  return *this;
3415  }
3416 
3417  BasicWriter &operator<<(typename internal::WCharHelper<StringRef, Char>::Supported value)
3418  {
3419  const char *str = value.data();
3420  buffer_.append(str, str + value.size());
3421  return *this;
3422  }
3423 
3424  template<typename T, typename Spec, typename FillChar>
3425  BasicWriter &operator<<(IntFormatSpec<T, Spec, FillChar> spec)
3426  {
3428  write_int(spec.value(), spec);
3429  return *this;
3430  }
3431 
3432  template<typename StrChar>
3433  BasicWriter &operator<<(const StrFormatSpec<StrChar> &spec)
3434  {
3435  const StrChar *s = spec.str();
3436  write_str(s, std::char_traits<Char>::length(s), spec);
3437  return *this;
3438  }
3439 
3440  void clear() FMT_NOEXCEPT
3441  {
3442  buffer_.clear();
3443  }
3444 
3445  Buffer<Char> &buffer() FMT_NOEXCEPT
3446  {
3447  return buffer_;
3448  }
3449 };
3450 
3451 template<typename Char>
3452 template<typename StrChar>
3453 typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(const StrChar *s, std::size_t size, const AlignSpec &spec)
3454 {
3455  CharPtr out = CharPtr();
3456  if (spec.width() > size)
3457  {
3458  out = grow_buffer(spec.width());
3459  Char fill = internal::CharTraits<Char>::cast(spec.fill());
3460  if (spec.align() == ALIGN_RIGHT)
3461  {
3462  std::uninitialized_fill_n(out, spec.width() - size, fill);
3463  out += spec.width() - size;
3464  }
3465  else if (spec.align() == ALIGN_CENTER)
3466  {
3467  out = fill_padding(out, spec.width(), size, fill);
3468  }
3469  else
3470  {
3471  std::uninitialized_fill_n(out + size, spec.width() - size, fill);
3472  }
3473  }
3474  else
3475  {
3476  out = grow_buffer(size);
3477  }
3478  std::uninitialized_copy(s, s + size, out);
3479  return out;
3480 }
3481 
3482 template<typename Char>
3483 template<typename StrChar, typename Spec>
3485 {
3486  // Check if StrChar is convertible to Char.
3488  if (spec.type_ && spec.type_ != 's')
3489  internal::report_unknown_type(spec.type_, "string");
3490  const StrChar *str_value = s.value;
3491  std::size_t str_size = s.size;
3492  if (str_size == 0)
3493  {
3494  if (!str_value)
3495  {
3496  FMT_THROW(FormatError("string pointer is null"));
3497  }
3498  }
3499  std::size_t precision = static_cast<std::size_t>(spec.precision_);
3500  if (spec.precision_ >= 0 && precision < str_size)
3501  str_size = precision;
3502  write_str(str_value, str_size, spec);
3503 }
3504 
3505 template<typename Char>
3506 typename BasicWriter<Char>::CharPtr BasicWriter<Char>::fill_padding(
3507  CharPtr buffer, unsigned total_size, std::size_t content_size, wchar_t fill)
3508 {
3509  std::size_t padding = total_size - content_size;
3510  std::size_t left_padding = padding / 2;
3511  Char fill_char = internal::CharTraits<Char>::cast(fill);
3512  std::uninitialized_fill_n(buffer, left_padding, fill_char);
3513  buffer += left_padding;
3514  CharPtr content = buffer;
3515  std::uninitialized_fill_n(buffer + content_size, padding - left_padding, fill_char);
3516  return content;
3517 }
3518 
3519 template<typename Char>
3520 template<typename Spec>
3521 typename BasicWriter<Char>::CharPtr BasicWriter<Char>::prepare_int_buffer(
3522  unsigned num_digits, const Spec &spec, const char *prefix, unsigned prefix_size)
3523 {
3524  unsigned width = spec.width();
3525  Alignment align = spec.align();
3526  Char fill = internal::CharTraits<Char>::cast(spec.fill());
3527  if (spec.precision() > static_cast<int>(num_digits))
3528  {
3529  // Octal prefix '0' is counted as a digit, so ignore it if precision
3530  // is specified.
3531  if (prefix_size > 0 && prefix[prefix_size - 1] == '0')
3532  --prefix_size;
3533  unsigned number_size = prefix_size + internal::to_unsigned(spec.precision());
3534  AlignSpec subspec(number_size, '0', ALIGN_NUMERIC);
3535  if (number_size >= width)
3536  return prepare_int_buffer(num_digits, subspec, prefix, prefix_size);
3537  buffer_.reserve(width);
3538  unsigned fill_size = width - number_size;
3539  if (align != ALIGN_LEFT)
3540  {
3541  CharPtr p = grow_buffer(fill_size);
3542  std::uninitialized_fill(p, p + fill_size, fill);
3543  }
3544  CharPtr result = prepare_int_buffer(num_digits, subspec, prefix, prefix_size);
3545  if (align == ALIGN_LEFT)
3546  {
3547  CharPtr p = grow_buffer(fill_size);
3548  std::uninitialized_fill(p, p + fill_size, fill);
3549  }
3550  return result;
3551  }
3552  unsigned size = prefix_size + num_digits;
3553  if (width <= size)
3554  {
3555  CharPtr p = grow_buffer(size);
3556  std::uninitialized_copy(prefix, prefix + prefix_size, p);
3557  return p + size - 1;
3558  }
3559  CharPtr p = grow_buffer(width);
3560  CharPtr end = p + width;
3561  if (align == ALIGN_LEFT)
3562  {
3563  std::uninitialized_copy(prefix, prefix + prefix_size, p);
3564  p += size;
3565  std::uninitialized_fill(p, end, fill);
3566  }
3567  else if (align == ALIGN_CENTER)
3568  {
3569  p = fill_padding(p, width, size, fill);
3570  std::uninitialized_copy(prefix, prefix + prefix_size, p);
3571  p += size;
3572  }
3573  else
3574  {
3575  if (align == ALIGN_NUMERIC)
3576  {
3577  if (prefix_size != 0)
3578  {
3579  p = std::uninitialized_copy(prefix, prefix + prefix_size, p);
3580  size -= prefix_size;
3581  }
3582  }
3583  else
3584  {
3585  std::uninitialized_copy(prefix, prefix + prefix_size, end - size);
3586  }
3587  std::uninitialized_fill(p, end - size, fill);
3588  p = end;
3589  }
3590  return p - 1;
3591 }
3592 
3593 template<typename Char>
3594 template<typename T, typename Spec>
3595 void BasicWriter<Char>::write_int(T value, Spec spec)
3596 {
3597  unsigned prefix_size = 0;
3598  typedef typename internal::IntTraits<T>::MainType UnsignedType;
3599  UnsignedType abs_value = static_cast<UnsignedType>(value);
3600  char prefix[4] = "";
3601  if (internal::is_negative(value))
3602  {
3603  prefix[0] = '-';
3604  ++prefix_size;
3605  abs_value = 0 - abs_value;
3606  }
3607  else if (spec.flag(SIGN_FLAG))
3608  {
3609  prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' ';
3610  ++prefix_size;
3611  }
3612  switch (spec.type())
3613  {
3614  case 0:
3615  case 'd':
3616  {
3617  unsigned num_digits = internal::count_digits(abs_value);
3618  CharPtr p = prepare_int_buffer(num_digits, spec, prefix, prefix_size) + 1;
3619  internal::format_decimal(get(p), abs_value, 0);
3620  break;
3621  }
3622  case 'x':
3623  case 'X':
3624  {
3625  UnsignedType n = abs_value;
3626  if (spec.flag(HASH_FLAG))
3627  {
3628  prefix[prefix_size++] = '0';
3629  prefix[prefix_size++] = spec.type_prefix();
3630  }
3631  unsigned num_digits = 0;
3632  do
3633  {
3634  ++num_digits;
3635  } while ((n >>= 4) != 0);
3636  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
3637  n = abs_value;
3638  const char *digits = spec.type() == 'x' ? "0123456789abcdef" : "0123456789ABCDEF";
3639  do
3640  {
3641  *p-- = digits[n & 0xf];
3642  } while ((n >>= 4) != 0);
3643  break;
3644  }
3645  case 'b':
3646  case 'B':
3647  {
3648  UnsignedType n = abs_value;
3649  if (spec.flag(HASH_FLAG))
3650  {
3651  prefix[prefix_size++] = '0';
3652  prefix[prefix_size++] = spec.type_prefix();
3653  }
3654  unsigned num_digits = 0;
3655  do
3656  {
3657  ++num_digits;
3658  } while ((n >>= 1) != 0);
3659  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
3660  n = abs_value;
3661  do
3662  {
3663  *p-- = static_cast<Char>('0' + (n & 1));
3664  } while ((n >>= 1) != 0);
3665  break;
3666  }
3667  case 'o':
3668  {
3669  UnsignedType n = abs_value;
3670  if (spec.flag(HASH_FLAG))
3671  prefix[prefix_size++] = '0';
3672  unsigned num_digits = 0;
3673  do
3674  {
3675  ++num_digits;
3676  } while ((n >>= 3) != 0);
3677  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
3678  n = abs_value;
3679  do
3680  {
3681  *p-- = static_cast<Char>('0' + (n & 7));
3682  } while ((n >>= 3) != 0);
3683  break;
3684  }
3685  case 'n':
3686  {
3687  unsigned num_digits = internal::count_digits(abs_value);
3688  fmt::StringRef sep = "";
3689 #if !(defined(ANDROID) || defined(__ANDROID__))
3690  sep = internal::thousands_sep(std::localeconv());
3691 #endif
3692  unsigned size = static_cast<unsigned>(num_digits + sep.size() * ((num_digits - 1) / 3));
3693  CharPtr p = prepare_int_buffer(size, spec, prefix, prefix_size) + 1;
3694  internal::format_decimal(get(p), abs_value, 0, internal::ThousandsSep(sep));
3695  break;
3696  }
3697  default:
3698  internal::report_unknown_type(spec.type(), spec.flag(CHAR_FLAG) ? "char" : "integer");
3699  break;
3700  }
3701 }
3702 
3703 template<typename Char>
3704 template<typename T, typename Spec>
3705 void BasicWriter<Char>::write_double(T value, const Spec &spec)
3706 {
3707  // Check type.
3708  char type = spec.type();
3709  bool upper = false;
3710  switch (type)
3711  {
3712  case 0:
3713  type = 'g';
3714  break;
3715  case 'e':
3716  case 'f':
3717  case 'g':
3718  case 'a':
3719  break;
3720  case 'F':
3721 #if FMT_MSC_VER
3722  // MSVC's printf doesn't support 'F'.
3723  type = 'f';
3724 #endif
3725  // Fall through.
3726  case 'E':
3727  case 'G':
3728  case 'A':
3729  upper = true;
3730  break;
3731  default:
3732  internal::report_unknown_type(type, "double");
3733  break;
3734  }
3735 
3736  char sign = 0;
3737  // Use isnegative instead of value < 0 because the latter is always
3738  // false for NaN.
3739  if (internal::FPUtil::isnegative(static_cast<double>(value)))
3740  {
3741  sign = '-';
3742  value = -value;
3743  }
3744  else if (spec.flag(SIGN_FLAG))
3745  {
3746  sign = spec.flag(PLUS_FLAG) ? '+' : ' ';
3747  }
3748 
3749  if (internal::FPUtil::isnotanumber(value))
3750  {
3751  // Format NaN ourselves because sprintf's output is not consistent
3752  // across platforms.
3753  std::size_t nan_size = 4;
3754  const char *nan = upper ? " NAN" : " nan";
3755  if (!sign)
3756  {
3757  --nan_size;
3758  ++nan;
3759  }
3760  CharPtr out = write_str(nan, nan_size, spec);
3761  if (sign)
3762  *out = sign;
3763  return;
3764  }
3765 
3766  if (internal::FPUtil::isinfinity(value))
3767  {
3768  // Format infinity ourselves because sprintf's output is not consistent
3769  // across platforms.
3770  std::size_t inf_size = 4;
3771  const char *inf = upper ? " INF" : " inf";
3772  if (!sign)
3773  {
3774  --inf_size;
3775  ++inf;
3776  }
3777  CharPtr out = write_str(inf, inf_size, spec);
3778  if (sign)
3779  *out = sign;
3780  return;
3781  }
3782 
3783  std::size_t offset = buffer_.size();
3784  unsigned width = spec.width();
3785  if (sign)
3786  {
3787  buffer_.reserve(buffer_.size() + (width > 1u ? width : 1u));
3788  if (width > 0)
3789  --width;
3790  ++offset;
3791  }
3792 
3793  // Build format string.
3794  enum
3795  {
3796  MAX_FORMAT_SIZE = 10
3797  }; // longest format: %#-*.*Lg
3798  Char format[MAX_FORMAT_SIZE];
3799  Char *format_ptr = format;
3800  *format_ptr++ = '%';
3801  unsigned width_for_sprintf = width;
3802  if (spec.flag(HASH_FLAG))
3803  *format_ptr++ = '#';
3804  if (spec.align() == ALIGN_CENTER)
3805  {
3806  width_for_sprintf = 0;
3807  }
3808  else
3809  {
3810  if (spec.align() == ALIGN_LEFT)
3811  *format_ptr++ = '-';
3812  if (width != 0)
3813  *format_ptr++ = '*';
3814  }
3815  if (spec.precision() >= 0)
3816  {
3817  *format_ptr++ = '.';
3818  *format_ptr++ = '*';
3819  }
3820 
3821  append_float_length(format_ptr, value);
3822  *format_ptr++ = type;
3823  *format_ptr = '\0';
3824 
3825  // Format using snprintf.
3826  Char fill = internal::CharTraits<Char>::cast(spec.fill());
3827  unsigned n = 0;
3828  Char *start = FMT_NULL;
3829  for (;;)
3830  {
3831  std::size_t buffer_size = buffer_.capacity() - offset;
3832 #if FMT_MSC_VER
3833  // MSVC's vsnprintf_s doesn't work with zero size, so reserve
3834  // space for at least one extra character to make the size non-zero.
3835  // Note that the buffer's capacity will increase by more than 1.
3836  if (buffer_size == 0)
3837  {
3838  buffer_.reserve(offset + 1);
3839  buffer_size = buffer_.capacity() - offset;
3840  }
3841 #endif
3842  start = &buffer_[offset];
3843  int result = internal::CharTraits<Char>::format_float(start, buffer_size, format, width_for_sprintf, spec.precision(), value);
3844  if (result >= 0)
3845  {
3846  n = internal::to_unsigned(result);
3847  if (offset + n < buffer_.capacity())
3848  break; // The buffer is large enough - continue with formatting.
3849  buffer_.reserve(offset + n + 1);
3850  }
3851  else
3852  {
3853  // If result is negative we ask to increase the capacity by at least 1,
3854  // but as std::vector, the buffer grows exponentially.
3855  buffer_.reserve(buffer_.capacity() + 1);
3856  }
3857  }
3858  if (sign)
3859  {
3860  if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) || *start != ' ')
3861  {
3862  *(start - 1) = sign;
3863  sign = 0;
3864  }
3865  else
3866  {
3867  *(start - 1) = fill;
3868  }
3869  ++n;
3870  }
3871  if (spec.align() == ALIGN_CENTER && spec.width() > n)
3872  {
3873  width = spec.width();
3874  CharPtr p = grow_buffer(width);
3875  std::memmove(get(p) + (width - n) / 2, get(p), n * sizeof(Char));
3876  fill_padding(p, spec.width(), n, fill);
3877  return;
3878  }
3879  if (spec.fill() != ' ' || sign)
3880  {
3881  while (*start == ' ')
3882  *start++ = fill;
3883  if (sign)
3884  *(start - 1) = sign;
3885  }
3886  grow_buffer(n);
3887 }
3888 
3923 template<typename Char, typename Allocator = std::allocator<Char>>
3924 class BasicMemoryWriter : public BasicWriter<Char>
3925 {
3926 private:
3928 
3929 public:
3930  explicit BasicMemoryWriter(const Allocator &alloc = Allocator())
3931  : BasicWriter<Char>(buffer_)
3932  , buffer_(alloc)
3933  {
3934  }
3935 
3936 #if FMT_USE_RVALUE_REFERENCES
3937 
3944  : BasicWriter<Char>(buffer_)
3945  , buffer_(std::move(other.buffer_))
3946  {
3947  }
3948 
3954  BasicMemoryWriter &operator=(BasicMemoryWriter &&other)
3955  {
3956  buffer_ = std::move(other.buffer_);
3957  return *this;
3958  }
3959 #endif
3960 };
3961 
3964 
3985 template<typename Char>
3986 class BasicArrayWriter : public BasicWriter<Char>
3987 {
3988 private:
3990 
3991 public:
3998  BasicArrayWriter(Char *array, std::size_t size)
3999  : BasicWriter<Char>(buffer_)
4000  , buffer_(array, size)
4001  {
4002  }
4003 
4010  template<std::size_t SIZE>
4011  explicit BasicArrayWriter(Char (&array)[SIZE])
4012  : BasicWriter<Char>(buffer_)
4013  , buffer_(array, SIZE)
4014  {
4015  }
4016 };
4017 
4020 
4021 // Reports a system error without throwing an exception.
4022 // Can be used to report errors from destructors.
4023 FMT_API void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT;
4024 
4025 #if FMT_USE_WINDOWS_H
4026 
4028 class WindowsError : public SystemError
4029 {
4030 private:
4031  FMT_API void init(int error_code, CStringRef format_str, ArgList args);
4032 
4033 public:
4062  WindowsError(int error_code, CStringRef message)
4063  {
4064  init(error_code, message, ArgList());
4065  }
4066  FMT_VARIADIC_CTOR(WindowsError, init, int, CStringRef)
4067 };
4068 
4069 // Reports a Windows error without throwing an exception.
4070 // Can be used to report errors from destructors.
4071 FMT_API void report_windows_error(int error_code, StringRef message) FMT_NOEXCEPT;
4072 
4073 #endif
4074 
4075 enum Color
4076 {
4077  BLACK,
4078  RED,
4079  GREEN,
4080  YELLOW,
4081  BLUE,
4082  MAGENTA,
4083  CYAN,
4084  WHITE
4085 };
4086 
4093 FMT_API void print_colored(Color c, CStringRef format, ArgList args);
4094 
4104 inline std::string format(CStringRef format_str, ArgList args)
4105 {
4106  MemoryWriter w;
4107  w.write(format_str, args);
4108  return w.str();
4109 }
4110 
4111 inline std::wstring format(WCStringRef format_str, ArgList args)
4112 {
4113  WMemoryWriter w;
4114  w.write(format_str, args);
4115  return w.str();
4116 }
4117 
4127 FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args);
4128 
4138 FMT_API void print(CStringRef format_str, ArgList args);
4139 
4144 {
4145 private:
4146  // Buffer should be large enough to hold all digits (digits10 + 1),
4147  // a sign and a null character.
4148  enum
4149  {
4150  BUFFER_SIZE = std::numeric_limits<ULongLong>::digits10 + 3
4151  };
4152  mutable char buffer_[BUFFER_SIZE];
4153  char *str_;
4154 
4155  // Formats value in reverse and returns the number of digits.
4156  char *format_decimal(ULongLong value)
4157  {
4158  char *buffer_end = buffer_ + BUFFER_SIZE - 1;
4159  while (value >= 100)
4160  {
4161  // Integer division is slow so do it for a group of two digits instead
4162  // of for every digit. The idea comes from the talk by Alexandrescu
4163  // "Three Optimization Tips for C++". See speed-test for a comparison.
4164  unsigned index = static_cast<unsigned>((value % 100) * 2);
4165  value /= 100;
4166  *--buffer_end = internal::Data::DIGITS[index + 1];
4167  *--buffer_end = internal::Data::DIGITS[index];
4168  }
4169  if (value < 10)
4170  {
4171  *--buffer_end = static_cast<char>('0' + value);
4172  return buffer_end;
4173  }
4174  unsigned index = static_cast<unsigned>(value * 2);
4175  *--buffer_end = internal::Data::DIGITS[index + 1];
4176  *--buffer_end = internal::Data::DIGITS[index];
4177  return buffer_end;
4178  }
4179 
4180  void FormatSigned(LongLong value)
4181  {
4182  ULongLong abs_value = static_cast<ULongLong>(value);
4183  bool negative = value < 0;
4184  if (negative)
4185  abs_value = 0 - abs_value;
4186  str_ = format_decimal(abs_value);
4187  if (negative)
4188  *--str_ = '-';
4189  }
4190 
4191 public:
4192  explicit FormatInt(int value)
4193  {
4194  FormatSigned(value);
4195  }
4196  explicit FormatInt(long value)
4197  {
4198  FormatSigned(value);
4199  }
4200  explicit FormatInt(LongLong value)
4201  {
4202  FormatSigned(value);
4203  }
4204  explicit FormatInt(unsigned value)
4205  : str_(format_decimal(value))
4206  {
4207  }
4208  explicit FormatInt(unsigned long value)
4209  : str_(format_decimal(value))
4210  {
4211  }
4212  explicit FormatInt(ULongLong value)
4213  : str_(format_decimal(value))
4214  {
4215  }
4216 
4218  std::size_t size() const
4219  {
4220  return internal::to_unsigned(buffer_ - str_ + BUFFER_SIZE - 1);
4221  }
4222 
4227  const char *data() const
4228  {
4229  return str_;
4230  }
4231 
4236  const char *c_str() const
4237  {
4238  buffer_[BUFFER_SIZE - 1] = '\0';
4239  return str_;
4240  }
4241 
4247  std::string str() const
4248  {
4249  return std::string(str_, size());
4250  }
4251 };
4252 
4253 // Formats a decimal integer value writing into buffer and returns
4254 // a pointer to the end of the formatted string. This function doesn't
4255 // write a terminating null character.
4256 template<typename T>
4257 inline void format_decimal(char *&buffer, T value)
4258 {
4259  typedef typename internal::IntTraits<T>::MainType MainType;
4260  MainType abs_value = static_cast<MainType>(value);
4261  if (internal::is_negative(value))
4262  {
4263  *buffer++ = '-';
4264  abs_value = 0 - abs_value;
4265  }
4266  if (abs_value < 100)
4267  {
4268  if (abs_value < 10)
4269  {
4270  *buffer++ = static_cast<char>('0' + abs_value);
4271  return;
4272  }
4273  unsigned index = static_cast<unsigned>(abs_value * 2);
4274  *buffer++ = internal::Data::DIGITS[index];
4275  *buffer++ = internal::Data::DIGITS[index + 1];
4276  return;
4277  }
4278  unsigned num_digits = internal::count_digits(abs_value);
4279  internal::format_decimal(buffer, abs_value, num_digits);
4280  buffer += num_digits;
4281 }
4282 
4293 template<typename T>
4294 inline internal::NamedArgWithType<char, T> arg(StringRef name, const T &arg)
4295 {
4296  return internal::NamedArgWithType<char, T>(name, arg);
4297 }
4298 
4299 template<typename T>
4300 inline internal::NamedArgWithType<wchar_t, T> arg(WStringRef name, const T &arg)
4301 {
4302  return internal::NamedArgWithType<wchar_t, T>(name, arg);
4303 }
4304 
4305 // The following two functions are deleted intentionally to disable
4306 // nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``.
4307 template<typename Char>
4308 void arg(StringRef, const internal::NamedArg<Char> &) FMT_DELETED_OR_UNDEFINED;
4309 template<typename Char>
4310 void arg(WStringRef, const internal::NamedArg<Char> &) FMT_DELETED_OR_UNDEFINED;
4311 } // namespace fmt
4312 
4313 #if FMT_GCC_VERSION
4314 // Use the system_header pragma to suppress warnings about variadic macros
4315 // because suppressing -Wvariadic-macros with the diagnostic pragma doesn't
4316 // work. It is used at the end because we want to suppress as little warnings
4317 // as possible.
4318 #pragma GCC system_header
4319 #endif
4320 
4321 // This is used to work around VC++ bugs in handling variadic macros.
4322 #define FMT_EXPAND(args) args
4323 
4324 // Returns the number of arguments.
4325 // Based on https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s.
4326 #define FMT_NARG(...) FMT_NARG_(__VA_ARGS__, FMT_RSEQ_N())
4327 #define FMT_NARG_(...) FMT_EXPAND(FMT_ARG_N(__VA_ARGS__))
4328 #define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
4329 #define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
4330 
4331 #define FMT_FOR_EACH_(N, f, ...) FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__))
4332 #define FMT_FOR_EACH(f, ...) FMT_EXPAND(FMT_FOR_EACH_(FMT_NARG(__VA_ARGS__), f, __VA_ARGS__))
4333 
4334 #define FMT_ADD_ARG_NAME(type, index) type arg##index
4335 #define FMT_GET_ARG_NAME(type, index) arg##index
4336 
4337 #if FMT_USE_VARIADIC_TEMPLATES
4338 #define FMT_VARIADIC_(Const, Char, ReturnType, func, call, ...) \
4339  template<typename... Args> \
4340  ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), const Args &... args) Const \
4341  { \
4342  typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
4343  typename ArgArray::Type array{ArgArray::template make<fmt::BasicFormatter<Char>>(args)...}; \
4344  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList(fmt::internal::make_type(args...), array)); \
4345  }
4346 #else
4347 // Defines a wrapper for a function taking __VA_ARGS__ arguments
4348 // and n additional arguments of arbitrary types.
4349 #define FMT_WRAP(Const, Char, ReturnType, func, call, n, ...) \
4350  template<FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
4351  inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), FMT_GEN(n, FMT_MAKE_ARG)) Const \
4352  { \
4353  fmt::internal::ArgArray<n>::Type arr; \
4354  FMT_GEN(n, FMT_ASSIGN_##Char); \
4355  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList(fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), arr)); \
4356  }
4357 
4358 #define FMT_VARIADIC_(Const, Char, ReturnType, func, call, ...) \
4359  inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) Const \
4360  { \
4361  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList()); \
4362  } \
4363  FMT_WRAP(Const, Char, ReturnType, func, call, 1, __VA_ARGS__) \
4364  FMT_WRAP(Const, Char, ReturnType, func, call, 2, __VA_ARGS__) \
4365  FMT_WRAP(Const, Char, ReturnType, func, call, 3, __VA_ARGS__) \
4366  FMT_WRAP(Const, Char, ReturnType, func, call, 4, __VA_ARGS__) \
4367  FMT_WRAP(Const, Char, ReturnType, func, call, 5, __VA_ARGS__) \
4368  FMT_WRAP(Const, Char, ReturnType, func, call, 6, __VA_ARGS__) \
4369  FMT_WRAP(Const, Char, ReturnType, func, call, 7, __VA_ARGS__) \
4370  FMT_WRAP(Const, Char, ReturnType, func, call, 8, __VA_ARGS__) \
4371  FMT_WRAP(Const, Char, ReturnType, func, call, 9, __VA_ARGS__) \
4372  FMT_WRAP(Const, Char, ReturnType, func, call, 10, __VA_ARGS__) \
4373  FMT_WRAP(Const, Char, ReturnType, func, call, 11, __VA_ARGS__) \
4374  FMT_WRAP(Const, Char, ReturnType, func, call, 12, __VA_ARGS__) \
4375  FMT_WRAP(Const, Char, ReturnType, func, call, 13, __VA_ARGS__) \
4376  FMT_WRAP(Const, Char, ReturnType, func, call, 14, __VA_ARGS__) \
4377  FMT_WRAP(Const, Char, ReturnType, func, call, 15, __VA_ARGS__)
4378 #endif // FMT_USE_VARIADIC_TEMPLATES
4379 
4407 #define FMT_VARIADIC(ReturnType, func, ...) FMT_VARIADIC_(, char, ReturnType, func, return func, __VA_ARGS__)
4408 
4409 #define FMT_VARIADIC_CONST(ReturnType, func, ...) FMT_VARIADIC_(const, char, ReturnType, func, return func, __VA_ARGS__)
4410 
4411 #define FMT_VARIADIC_W(ReturnType, func, ...) FMT_VARIADIC_(, wchar_t, ReturnType, func, return func, __VA_ARGS__)
4412 
4413 #define FMT_VARIADIC_CONST_W(ReturnType, func, ...) FMT_VARIADIC_(const, wchar_t, ReturnType, func, return func, __VA_ARGS__)
4414 
4415 #define FMT_CAPTURE_ARG_(id, index) ::fmt::arg(#id, id)
4416 
4417 #define FMT_CAPTURE_ARG_W_(id, index) ::fmt::arg(L## #id, id)
4418 
4433 #define FMT_CAPTURE(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_, __VA_ARGS__)
4434 
4435 #define FMT_CAPTURE_W(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_W_, __VA_ARGS__)
4436 
4437 namespace fmt {
4438 FMT_VARIADIC(std::string, format, CStringRef)
4439 FMT_VARIADIC_W(std::wstring, format, WCStringRef)
4440 FMT_VARIADIC(void, print, CStringRef)
4441 FMT_VARIADIC(void, print, std::FILE *, CStringRef)
4442 FMT_VARIADIC(void, print_colored, Color, CStringRef)
4443 
4444 namespace internal {
4445 template<typename Char>
4446 inline bool is_name_start(Char c)
4447 {
4448  return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || '_' == c;
4449 }
4450 
4451 // Parses an unsigned integer advancing s to the end of the parsed input.
4452 // This function assumes that the first character of s is a digit.
4453 template<typename Char>
4454 unsigned parse_nonnegative_int(const Char *&s)
4455 {
4456  assert('0' <= *s && *s <= '9');
4457  unsigned value = 0;
4458  // Convert to unsigned to prevent a warning.
4459  unsigned max_int = (std::numeric_limits<int>::max)();
4460  unsigned big = max_int / 10;
4461  do
4462  {
4463  // Check for overflow.
4464  if (value > big)
4465  {
4466  value = max_int + 1;
4467  break;
4468  }
4469  value = value * 10 + (*s - '0');
4470  ++s;
4471  } while ('0' <= *s && *s <= '9');
4472  // Convert to unsigned to prevent a warning.
4473  if (value > max_int)
4474  FMT_THROW(FormatError("number is too big"));
4475  return value;
4476 }
4477 
4478 inline void require_numeric_argument(const Arg &arg, char spec)
4479 {
4480  if (arg.type > Arg::LAST_NUMERIC_TYPE)
4481  {
4482  std::string message = fmt::format("format specifier '{}' requires numeric argument", spec);
4483  FMT_THROW(fmt::FormatError(message));
4484  }
4485 }
4486 
4487 template<typename Char>
4488 void check_sign(const Char *&s, const Arg &arg)
4489 {
4490  char sign = static_cast<char>(*s);
4491  require_numeric_argument(arg, sign);
4492  if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG)
4493  {
4494  FMT_THROW(FormatError(fmt::format("format specifier '{}' requires signed argument", sign)));
4495  }
4496  ++s;
4497 }
4498 } // namespace internal
4499 
4500 template<typename Char, typename AF>
4501 inline internal::Arg BasicFormatter<Char, AF>::get_arg(BasicStringRef<Char> arg_name, const char *&error)
4502 {
4503  if (check_no_auto_index(error))
4504  {
4505  map_.init(args());
4506  const internal::Arg *arg = map_.find(arg_name);
4507  if (arg)
4508  return *arg;
4509  error = "argument not found";
4510  }
4511  return internal::Arg();
4512 }
4513 
4514 template<typename Char, typename AF>
4516 {
4517  const char *error = FMT_NULL;
4518  internal::Arg arg = *s < '0' || *s > '9' ? next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error);
4519  if (error)
4520  {
4521  FMT_THROW(FormatError(*s != '}' && *s != ':' ? "invalid format string" : error));
4522  }
4523  return arg;
4524 }
4525 
4526 template<typename Char, typename AF>
4528 {
4529  assert(internal::is_name_start(*s));
4530  const Char *start = s;
4531  Char c;
4532  do
4533  {
4534  c = *++s;
4535  } while (internal::is_name_start(c) || ('0' <= c && c <= '9'));
4536  const char *error = FMT_NULL;
4537  internal::Arg arg = get_arg(BasicStringRef<Char>(start, s - start), error);
4538  if (error)
4539  FMT_THROW(FormatError(error));
4540  return arg;
4541 }
4542 
4543 template<typename Char, typename ArgFormatter>
4544 const Char *BasicFormatter<Char, ArgFormatter>::format(const Char *&format_str, const internal::Arg &arg)
4545 {
4546  using internal::Arg;
4547  const Char *s = format_str;
4548  typename ArgFormatter::SpecType spec;
4549  if (*s == ':')
4550  {
4551  if (arg.type == Arg::CUSTOM)
4552  {
4553  arg.custom.format(this, arg.custom.value, &s);
4554  return s;
4555  }
4556  ++s;
4557  // Parse fill and alignment.
4558  if (Char c = *s)
4559  {
4560  const Char *p = s + 1;
4561  spec.align_ = ALIGN_DEFAULT;
4562  do
4563  {
4564  switch (*p)
4565  {
4566  case '<':
4567  spec.align_ = ALIGN_LEFT;
4568  break;
4569  case '>':
4570  spec.align_ = ALIGN_RIGHT;
4571  break;
4572  case '=':
4573  spec.align_ = ALIGN_NUMERIC;
4574  break;
4575  case '^':
4576  spec.align_ = ALIGN_CENTER;
4577  break;
4578  }
4579  if (spec.align_ != ALIGN_DEFAULT)
4580  {
4581  if (p != s)
4582  {
4583  if (c == '}')
4584  break;
4585  if (c == '{')
4586  FMT_THROW(FormatError("invalid fill character '{'"));
4587  s += 2;
4588  spec.fill_ = c;
4589  }
4590  else
4591  ++s;
4592  if (spec.align_ == ALIGN_NUMERIC)
4593  require_numeric_argument(arg, '=');
4594  break;
4595  }
4596  } while (--p >= s);
4597  }
4598 
4599  // Parse sign.
4600  switch (*s)
4601  {
4602  case '+':
4603  check_sign(s, arg);
4604  spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
4605  break;
4606  case '-':
4607  check_sign(s, arg);
4608  spec.flags_ |= MINUS_FLAG;
4609  break;
4610  case ' ':
4611  check_sign(s, arg);
4612  spec.flags_ |= SIGN_FLAG;
4613  break;
4614  }
4615 
4616  if (*s == '#')
4617  {
4618  require_numeric_argument(arg, '#');
4619  spec.flags_ |= HASH_FLAG;
4620  ++s;
4621  }
4622 
4623  // Parse zero flag.
4624  if (*s == '0')
4625  {
4626  require_numeric_argument(arg, '0');
4627  spec.align_ = ALIGN_NUMERIC;
4628  spec.fill_ = '0';
4629  ++s;
4630  }
4631 
4632  // Parse width.
4633  if ('0' <= *s && *s <= '9')
4634  {
4635  spec.width_ = internal::parse_nonnegative_int(s);
4636  }
4637  else if (*s == '{')
4638  {
4639  ++s;
4640  Arg width_arg = internal::is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s);
4641  if (*s++ != '}')
4642  FMT_THROW(FormatError("invalid format string"));
4643  ULongLong value = 0;
4644  switch (width_arg.type)
4645  {
4646  case Arg::INT:
4647  if (width_arg.int_value < 0)
4648  FMT_THROW(FormatError("negative width"));
4649  value = width_arg.int_value;
4650  break;
4651  case Arg::UINT:
4652  value = width_arg.uint_value;
4653  break;
4654  case Arg::LONG_LONG:
4655  if (width_arg.long_long_value < 0)
4656  FMT_THROW(FormatError("negative width"));
4657  value = width_arg.long_long_value;
4658  break;
4659  case Arg::ULONG_LONG:
4660  value = width_arg.ulong_long_value;
4661  break;
4662  default:
4663  FMT_THROW(FormatError("width is not integer"));
4664  }
4665  unsigned max_int = (std::numeric_limits<int>::max)();
4666  if (value > max_int)
4667  FMT_THROW(FormatError("number is too big"));
4668  spec.width_ = static_cast<int>(value);
4669  }
4670 
4671  // Parse precision.
4672  if (*s == '.')
4673  {
4674  ++s;
4675  spec.precision_ = 0;
4676  if ('0' <= *s && *s <= '9')
4677  {
4678  spec.precision_ = internal::parse_nonnegative_int(s);
4679  }
4680  else if (*s == '{')
4681  {
4682  ++s;
4683  Arg precision_arg = internal::is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s);
4684  if (*s++ != '}')
4685  FMT_THROW(FormatError("invalid format string"));
4686  ULongLong value = 0;
4687  switch (precision_arg.type)
4688  {
4689  case Arg::INT:
4690  if (precision_arg.int_value < 0)
4691  FMT_THROW(FormatError("negative precision"));
4692  value = precision_arg.int_value;
4693  break;
4694  case Arg::UINT:
4695  value = precision_arg.uint_value;
4696  break;
4697  case Arg::LONG_LONG:
4698  if (precision_arg.long_long_value < 0)
4699  FMT_THROW(FormatError("negative precision"));
4700  value = precision_arg.long_long_value;
4701  break;
4702  case Arg::ULONG_LONG:
4703  value = precision_arg.ulong_long_value;
4704  break;
4705  default:
4706  FMT_THROW(FormatError("precision is not integer"));
4707  }
4708  unsigned max_int = (std::numeric_limits<int>::max)();
4709  if (value > max_int)
4710  FMT_THROW(FormatError("number is too big"));
4711  spec.precision_ = static_cast<int>(value);
4712  }
4713  else
4714  {
4715  FMT_THROW(FormatError("missing precision specifier"));
4716  }
4717  if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER)
4718  {
4719  FMT_THROW(FormatError(
4720  fmt::format("precision not allowed in {} format specifier", arg.type == Arg::POINTER ? "pointer" : "integer")));
4721  }
4722  }
4723 
4724  // Parse type.
4725  if (*s != '}' && *s)
4726  spec.type_ = static_cast<char>(*s++);
4727  }
4728 
4729  if (*s++ != '}')
4730  FMT_THROW(FormatError("missing '}' in format string"));
4731 
4732  // Format argument.
4733  ArgFormatter(*this, spec, s - 1).visit(arg);
4734  return s;
4735 }
4736 
4737 template<typename Char, typename AF>
4739 {
4740  const Char *s = format_str.c_str();
4741  const Char *start = s;
4742  while (*s)
4743  {
4744  Char c = *s++;
4745  if (c != '{' && c != '}')
4746  continue;
4747  if (*s == c)
4748  {
4749  write(writer_, start, s);
4750  start = ++s;
4751  continue;
4752  }
4753  if (c == '}')
4754  FMT_THROW(FormatError("unmatched '}' in format string"));
4755  write(writer_, start, s - 1);
4756  internal::Arg arg = internal::is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s);
4757  start = s = format(s, arg);
4758  }
4759  write(writer_, start, s);
4760 }
4761 
4762 template<typename Char, typename It>
4763 struct ArgJoin
4764 {
4765  It first;
4766  It last;
4768 
4769  ArgJoin(It first, It last, const BasicCStringRef<Char> &sep)
4770  : first(first)
4771  , last(last)
4772  , sep(sep)
4773  {
4774  }
4775 };
4776 
4777 template<typename It>
4778 ArgJoin<char, It> join(It first, It last, const BasicCStringRef<char> &sep)
4779 {
4780  return ArgJoin<char, It>(first, last, sep);
4781 }
4782 
4783 template<typename It>
4784 ArgJoin<wchar_t, It> join(It first, It last, const BasicCStringRef<wchar_t> &sep)
4785 {
4786  return ArgJoin<wchar_t, It>(first, last, sep);
4787 }
4788 
4789 #if FMT_HAS_GXX_CXX11
4790 template<typename Range>
4791 auto join(const Range &range, const BasicCStringRef<char> &sep) -> ArgJoin<char, decltype(std::begin(range))>
4792 {
4793  return join(std::begin(range), std::end(range), sep);
4794 }
4795 
4796 template<typename Range>
4797 auto join(const Range &range, const BasicCStringRef<wchar_t> &sep) -> ArgJoin<wchar_t, decltype(std::begin(range))>
4798 {
4799  return join(std::begin(range), std::end(range), sep);
4800 }
4801 #endif
4802 
4803 template<typename ArgFormatter, typename Char, typename It>
4804 void format_arg(fmt::BasicFormatter<Char, ArgFormatter> &f, const Char *&format_str, const ArgJoin<Char, It> &e)
4805 {
4806  const Char *end = format_str;
4807  if (*end == ':')
4808  ++end;
4809  while (*end && *end != '}')
4810  ++end;
4811  if (*end != '}')
4812  FMT_THROW(FormatError("missing '}' in format string"));
4813 
4814  It it = e.first;
4815  if (it != e.last)
4816  {
4817  const Char *save = format_str;
4819  while (it != e.last)
4820  {
4821  f.writer().write(e.sep);
4822  format_str = save;
4824  }
4825  }
4826  format_str = end + 1;
4827 }
4828 } // namespace fmt
4829 
4830 #if FMT_USE_USER_DEFINED_LITERALS
4831 namespace fmt {
4832 namespace internal {
4833 
4834 template<typename Char>
4835 struct UdlFormat
4836 {
4837  const Char *str;
4838 
4839  template<typename... Args>
4840  auto operator()(Args &&... args) const -> decltype(format(str, std::forward<Args>(args)...))
4841  {
4842  return format(str, std::forward<Args>(args)...);
4843  }
4844 };
4845 
4846 template<typename Char>
4847 struct UdlArg
4848 {
4849  const Char *str;
4850 
4851  template<typename T>
4852  NamedArgWithType<Char, T> operator=(T &&value) const
4853  {
4854  return {str, std::forward<T>(value)};
4855  }
4856 };
4857 
4858 } // namespace internal
4859 
4860 inline namespace literals {
4861 
4872 inline internal::UdlFormat<char> operator"" _format(const char *s, std::size_t)
4873 {
4874  return {s};
4875 }
4876 inline internal::UdlFormat<wchar_t> operator"" _format(const wchar_t *s, std::size_t)
4877 {
4878  return {s};
4879 }
4880 
4891 inline internal::UdlArg<char> operator"" _a(const char *s, std::size_t)
4892 {
4893  return {s};
4894 }
4895 inline internal::UdlArg<wchar_t> operator"" _a(const wchar_t *s, std::size_t)
4896 {
4897  return {s};
4898 }
4899 
4900 } // namespace literals
4901 } // namespace fmt
4902 #endif // FMT_USE_USER_DEFINED_LITERALS
4903 
4904 // Restore warnings.
4905 #if FMT_GCC_VERSION >= 406
4906 #pragma GCC diagnostic pop
4907 #endif
4908 
4909 #if defined(__clang__) && !defined(FMT_ICC_VERSION)
4910 #pragma clang diagnostic pop
4911 #endif
4912 
4913 #ifdef FMT_HEADER_ONLY
4914 #define FMT_FUNC inline
4915 #include "format.cc"
4916 #else
4917 #define FMT_FUNC
4918 #endif
4919 
4920 #endif // FMT_FORMAT_H_
Definition: format.h:1349
BasicWriter< Char > & writer()
Definition: format.h:2843
Definition: format.h:2275
Result visit_custom(Arg::CustomValue)
Definition: format.h:2095
Definition: format.h:1450
BasicCStringRef(const Char *s)
Definition: format.h:712
Result visit_string(Arg::StringValue< char >)
Definition: format.h:2077
Result visit_pointer(const void *)
Definition: format.h:2089
Definition: format.h:1426
BasicWriter & operator<<(long double value)
Definition: format.h:3384
Definition: format.h:2309
Definition: format.h:1876
Definition: format.h:2887
Result visit_uint(unsigned value)
Definition: format.h:2021
const Char * c_str() const
Definition: format.h:3289
Definition: format.h:705
Definition: format.h:4143
Result visit_bool(bool value)
Definition: format.h:2033
Definition: format.h:1136
Definition: format.h:1819
Definition: format.h:401
Definition: format.h:1095
const Char * data() const FMT_NOEXCEPT
Definition: format.h:3280
Result visit_ulong_long(ULongLong value)
Definition: format.h:2027
Result visit_any_int(T)
Definition: format.h:2046
SystemError(int error_code, CStringRef message)
Definition: format.h:3084
const Char * data() const
Definition: format.h:629
Result visit_long_long(LongLong value)
Definition: format.h:2015
Definition: format.h:448
void format(BasicCStringRef< Char > format_str)
Definition: format.h:4738
Definition: format.h:1360
CharType Char
Definition: format.h:2808
Definition: format.h:1408
Result visit_any_double(T)
Definition: format.h:2065
Definition: format.h:2253
Definition: format.h:1557
Definition: format.h:1994
std::size_t size() const
Definition: format.h:4218
Definition: format.h:813
Definition: format.h:3986
Definition: format.h:1205
BasicStringRef(const Char *s)
Definition: format.h:577
std::size_t size() const
Definition: format.h:3271
BasicCStringRef(const std::basic_string< Char, std::char_traits< Char >, Allocator > &s)
Definition: format.h:723
BasicStringRef(const Char *s, std::size_t size)
Definition: format.h:565
Result visit_wstring(Arg::StringValue< wchar_t >)
Definition: format.h:2083
Result visit_double(double value)
Definition: format.h:2052
Result visit_long_double(long double value)
Definition: format.h:2058
void write(BasicCStringRef< Char > format, ArgList args)
Definition: format.h:3332
Result visit_char(int value)
Definition: format.h:2039
BasicStringRef(const std::basic_string< Char, std::char_traits< Char >, Allocator > &s)
Definition: format.h:589
Definition: format.h:1352
BasicArrayWriter(Char(&array)[SIZE])
Definition: format.h:4011
BasicFormatter(const ArgList &args, BasicWriter< Char > &w)
Definition: format.h:2836
const Char * c_str() const
Definition: format.h:729
Definition: format.h:2686
std::basic_string< Char > str() const
Definition: format.h:3302
Definition: format.h:529
void reserve(std::size_t capacity)
Definition: format.h:868
Definition: format.h:1459
Definition: format.h:1506
Definition: format.h:1416
std::size_t size() const
Definition: format.h:842
std::size_t capacity() const
Definition: format.h:848
Definition: format.h:1014
Definition: format.h:1857
Definition: format.h:1027
Definition: format.h:2230
Definition: format.h:2548
Definition: format.h:917
BasicWriter & operator<<(char value)
Definition: format.h:3393
Definition: format.h:2173
Definition: format.h:1214
Definition: format.h:1414
ArgFormatter(BasicFormatter< Char > &formatter, FormatSpec &spec, const Char *fmt)
Definition: format.h:2796
Result visit_int(int value)
Definition: format.h:2009
Definition: format.h:3924
Definition: format.h:2167
Definition: format.h:1872
Definition: format.h:1042
const char * c_str() const
Definition: format.h:4236
Definition: format.h:400
Result visit_cstring(const char *)
Definition: format.h:2071
void resize(std::size_t new_size)
Definition: format.h:856
BasicWriter & operator<<(ULongLong value)
Definition: format.h:3367
Definition: format.h:739
Definition: format.h:1148
void grow(std::size_t size) FMT_OVERRIDE
Definition: format.h:989
void append(const U *begin, const U *end)
Definition: format.h:902
Result visit(const Arg &arg)
Definition: format.h:2108
Definition: format.h:557
Definition: format.h:1530
Definition: format.h:1124
Definition: format.h:526
BasicWriter(Buffer< Char > &b)
Definition: format.h:3255
Definition: format.cc:84
BasicArgFormatter(BasicFormatter< Char, Impl > &formatter, Spec &spec, const Char *fmt)
Definition: format.h:2776
Definition: format.h:521
std::size_t size() const
Definition: format.h:635
BasicArrayWriter(Char *array, std::size_t size)
Definition: format.h:3998
Definition: format.h:2931
Definition: format.h:3053
Definition: format.h:757
Definition: format.h:1517
Definition: format.h:2761
std::basic_string< Char > to_string() const
Definition: format.h:623
Definition: format.h:1419
virtual ~BasicWriter()
Definition: format.h:3266
Definition: format.h:1478
Definition: format.h:515
Definition: format.h:1548
Definition: format.h:4763
std::string str() const
Definition: format.h:4247
Definition: format.h:1605
internal::Arg operator[](unsigned index) const
Definition: format.h:1930
const char * data() const
Definition: format.h:4227
Definition: format.h:2206
Definition: format.h:2329
void visit_custom(internal::Arg::CustomValue c)
Definition: format.h:2784