Electroneum
format.h
Go to the documentation of this file.
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 #include <cassert>
32 #include <clocale>
33 #include <cmath>
34 #include <cstdio>
35 #include <cstring>
36 #include <limits>
37 #include <memory>
38 #include <stdexcept>
39 #include <string>
40 #include <vector>
41 #include <utility> // for std::pair
42 
43 // The fmt library version in the form major * 10000 + minor * 100 + patch.
44 #define FMT_VERSION 30002
45 
46 #ifdef _SECURE_SCL
47 # define FMT_SECURE_SCL _SECURE_SCL
48 #else
49 # define FMT_SECURE_SCL 0
50 #endif
51 
52 #if FMT_SECURE_SCL
53 # include <iterator>
54 #endif
55 
56 #ifdef _MSC_VER
57 # define FMT_MSC_VER _MSC_VER
58 #else
59 # define FMT_MSC_VER 0
60 #endif
61 
62 #if FMT_MSC_VER && FMT_MSC_VER <= 1500
63 typedef unsigned __int32 uint32_t;
64 typedef unsigned __int64 uint64_t;
65 typedef __int64 intmax_t;
66 #else
67 #include <stdint.h>
68 #endif
69 
70 #if !defined(FMT_HEADER_ONLY) && defined(_WIN32)
71 # ifdef FMT_EXPORT
72 # define FMT_API __declspec(dllexport)
73 # elif defined(FMT_SHARED)
74 # define FMT_API __declspec(dllimport)
75 # endif
76 #endif
77 #ifndef FMT_API
78 # define FMT_API
79 #endif
80 
81 #ifdef __GNUC__
82 # define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
83 # define FMT_GCC_EXTENSION __extension__
84 # if FMT_GCC_VERSION >= 406
85 # pragma GCC diagnostic push
86 // Disable the warning about "long long" which is sometimes reported even
87 // when using __extension__.
88 # pragma GCC diagnostic ignored "-Wlong-long"
89 // Disable the warning about declaration shadowing because it affects too
90 // many valid cases.
91 # pragma GCC diagnostic ignored "-Wshadow"
92 // Disable the warning about implicit conversions that may change the sign of
93 // an integer; silencing it otherwise would require many explicit casts.
94 # pragma GCC diagnostic ignored "-Wsign-conversion"
95 # endif
96 # if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__
97 # define FMT_HAS_GXX_CXX11 1
98 # endif
99 #else
100 # define FMT_GCC_EXTENSION
101 #endif
102 
103 #if defined(__INTEL_COMPILER)
104 # define FMT_ICC_VERSION __INTEL_COMPILER
105 #elif defined(__ICL)
106 # define FMT_ICC_VERSION __ICL
107 #endif
108 
109 #if defined(__clang__) && !defined(FMT_ICC_VERSION)
110 # pragma clang diagnostic push
111 # pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
112 # pragma clang diagnostic ignored "-Wpadded"
113 #endif
114 
115 #ifdef __GNUC_LIBSTD__
116 # define FMT_GNUC_LIBSTD_VERSION (__GNUC_LIBSTD__ * 100 + __GNUC_LIBSTD_MINOR__)
117 #endif
118 
119 #ifdef __has_feature
120 # define FMT_HAS_FEATURE(x) __has_feature(x)
121 #else
122 # define FMT_HAS_FEATURE(x) 0
123 #endif
124 
125 #ifdef __has_builtin
126 # define FMT_HAS_BUILTIN(x) __has_builtin(x)
127 #else
128 # define FMT_HAS_BUILTIN(x) 0
129 #endif
130 
131 #ifdef __has_cpp_attribute
132 # define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
133 #else
134 # define FMT_HAS_CPP_ATTRIBUTE(x) 0
135 #endif
136 
137 #ifndef FMT_USE_VARIADIC_TEMPLATES
138 // Variadic templates are available in GCC since version 4.4
139 // (http://gcc.gnu.org/projects/cxx0x.html) and in Visual C++
140 // since version 2013.
141 # define FMT_USE_VARIADIC_TEMPLATES \
142  (FMT_HAS_FEATURE(cxx_variadic_templates) || \
143  (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800)
144 #endif
145 
146 #ifndef FMT_USE_RVALUE_REFERENCES
147 // Don't use rvalue references when compiling with clang and an old libstdc++
148 // as the latter doesn't provide std::move.
149 # if defined(FMT_GNUC_LIBSTD_VERSION) && FMT_GNUC_LIBSTD_VERSION <= 402
150 # define FMT_USE_RVALUE_REFERENCES 0
151 # else
152 # define FMT_USE_RVALUE_REFERENCES \
153  (FMT_HAS_FEATURE(cxx_rvalue_references) || \
154  (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1600)
155 # endif
156 #endif
157 
158 // Check if exceptions are disabled.
159 #if defined(__GNUC__) && !defined(__EXCEPTIONS)
160 # define FMT_EXCEPTIONS 0
161 #endif
162 #if FMT_MSC_VER && !_HAS_EXCEPTIONS
163 # define FMT_EXCEPTIONS 0
164 #endif
165 #ifndef FMT_EXCEPTIONS
166 # define FMT_EXCEPTIONS 1
167 #endif
168 
169 #ifndef FMT_THROW
170 # if FMT_EXCEPTIONS
171 # define FMT_THROW(x) throw x
172 # else
173 # define FMT_THROW(x) assert(false)
174 # endif
175 #endif
176 
177 // Define FMT_USE_NOEXCEPT to make fmt use noexcept (C++11 feature).
178 #ifndef FMT_USE_NOEXCEPT
179 # define FMT_USE_NOEXCEPT 0
180 #endif
181 
182 #if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \
183  (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
184  FMT_MSC_VER >= 1900
185 # define FMT_DETECTED_NOEXCEPT noexcept
186 #else
187 # define FMT_DETECTED_NOEXCEPT throw()
188 #endif
189 
190 #ifndef FMT_NOEXCEPT
191 # if FMT_EXCEPTIONS
192 # define FMT_NOEXCEPT FMT_DETECTED_NOEXCEPT
193 # else
194 # define FMT_NOEXCEPT
195 # endif
196 #endif
197 
198 // This is needed because GCC still uses throw() in its headers when exceptions
199 // are disabled.
200 #if FMT_GCC_VERSION
201 # define FMT_DTOR_NOEXCEPT FMT_DETECTED_NOEXCEPT
202 #else
203 # define FMT_DTOR_NOEXCEPT FMT_NOEXCEPT
204 #endif
205 
206 #ifndef FMT_OVERRIDE
207 # if (defined(FMT_USE_OVERRIDE) && FMT_USE_OVERRIDE) || FMT_HAS_FEATURE(cxx_override) || \
208  (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
209  FMT_MSC_VER >= 1900
210 # define FMT_OVERRIDE override
211 # else
212 # define FMT_OVERRIDE
213 # endif
214 #endif
215 
216 #ifndef FMT_NULL
217 # if FMT_HAS_FEATURE(cxx_nullptr) || \
218  (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
219  FMT_MSC_VER >= 1600
220 # define FMT_NULL nullptr
221 # else
222 # define FMT_NULL NULL
223 # endif
224 #endif
225 
226 // A macro to disallow the copy constructor and operator= functions
227 // This should be used in the private: declarations for a class
228 #ifndef FMT_USE_DELETED_FUNCTIONS
229 # define FMT_USE_DELETED_FUNCTIONS 0
230 #endif
231 
232 #if FMT_USE_DELETED_FUNCTIONS || FMT_HAS_FEATURE(cxx_deleted_functions) || \
233  (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800
234 # define FMT_DELETED_OR_UNDEFINED = delete
235 # define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
236  TypeName(const TypeName&) = delete; \
237  TypeName& operator=(const TypeName&) = delete
238 #else
239 # define FMT_DELETED_OR_UNDEFINED
240 # define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
241  TypeName(const TypeName&); \
242  TypeName& operator=(const TypeName&)
243 #endif
244 
245 #ifndef FMT_USE_DEFAULTED_FUNCTIONS
246 # define FMT_USE_DEFAULTED_FUNCTIONS 0
247 #endif
248 
249 #ifndef FMT_DEFAULTED_COPY_CTOR
250 # if FMT_USE_DEFAULTED_FUNCTIONS || FMT_HAS_FEATURE(cxx_defaulted_functions) || \
251  (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800
252 # define FMT_DEFAULTED_COPY_CTOR(TypeName) \
253  TypeName(const TypeName&) = default;
254 # else
255 # define FMT_DEFAULTED_COPY_CTOR(TypeName)
256 # endif
257 #endif
258 
259 #ifndef FMT_USE_USER_DEFINED_LITERALS
260 // All compilers which support UDLs also support variadic templates. This
261 // makes the fmt::literals implementation easier. However, an explicit check
262 // for variadic templates is added here just in case.
263 // For Intel's compiler both it and the system gcc/msc must support UDLs.
264 # define FMT_USE_USER_DEFINED_LITERALS \
265  FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES && \
266  (FMT_HAS_FEATURE(cxx_user_literals) || \
267  (FMT_GCC_VERSION >= 407 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900) && \
268  (!defined(FMT_ICC_VERSION) || FMT_ICC_VERSION >= 1500)
269 #endif
270 
271 #ifndef FMT_USE_EXTERN_TEMPLATES
272 // Clang doesn't have a feature check for extern templates so we check
273 // for variadic templates which were introduced in the same version.
274 // For GCC according to cppreference.com they were introduced in 3.3.
275 # define FMT_USE_EXTERN_TEMPLATES \
276  ((__clang__ && FMT_USE_VARIADIC_TEMPLATES) || \
277  (FMT_GCC_VERSION >= 303 && FMT_HAS_GXX_CXX11))
278 #endif
279 
280 // Checks if decltype v1.1 is supported
281 // http://en.cppreference.com/w/cpp/compiler_support
282 #define FMT_HAS_DECLTYPE_INCOMPLETE_RETURN_TYPES \
283  (FMT_HAS_FEATURE(cxx_decltype_incomplete_return_types) || \
284  (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
285  FMT_MSC_VER >= 1900 || \
286  FMT_ICC_VERSION >= 1200)
287 
288 #ifdef FMT_HEADER_ONLY
289 // If header only do not use extern templates.
290 # undef FMT_USE_EXTERN_TEMPLATES
291 # define FMT_USE_EXTERN_TEMPLATES 0
292 #endif
293 
294 #ifndef FMT_ASSERT
295 # define FMT_ASSERT(condition, message) assert((condition) && message)
296 #endif
297 
298 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz)
299 # define FMT_BUILTIN_CLZ(n) __builtin_clz(n)
300 #endif
301 
302 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clzll)
303 # define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n)
304 #endif
305 
306 // Some compilers masquerade as both MSVC and GCC-likes or
307 // otherwise support __builtin_clz and __builtin_clzll, so
308 // only define FMT_BUILTIN_CLZ using the MSVC intrinsics
309 // if the clz and clzll builtins are not available.
310 #if FMT_MSC_VER && !defined(FMT_BUILTIN_CLZLL) && !defined(_MANAGED)
311 # include <intrin.h> // _BitScanReverse, _BitScanReverse64
312 
313 namespace fmt {
314 namespace internal {
315 # pragma intrinsic(_BitScanReverse)
316 inline uint32_t clz(uint32_t x) {
317  unsigned long r = 0;
318  _BitScanReverse(&r, x);
319 
320  assert(x != 0);
321  // Static analysis complains about using uninitialized data
322  // "r", but the only way that can happen is if "x" is 0,
323  // which the callers guarantee to not happen.
324 # pragma warning(suppress: 6102)
325  return 31 - r;
326 }
327 # define FMT_BUILTIN_CLZ(n) fmt::internal::clz(n)
328 
329 # ifdef _WIN64
330 # pragma intrinsic(_BitScanReverse64)
331 # endif
332 
333 inline uint32_t clzll(uint64_t x) {
334  unsigned long r = 0;
335 # ifdef _WIN64
336  _BitScanReverse64(&r, x);
337 # else
338  // Scan the high 32 bits.
339  if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32)))
340  return 63 - (r + 32);
341 
342  // Scan the low 32 bits.
343  _BitScanReverse(&r, static_cast<uint32_t>(x));
344 # endif
345 
346  assert(x != 0);
347  // Static analysis complains about using uninitialized data
348  // "r", but the only way that can happen is if "x" is 0,
349  // which the callers guarantee to not happen.
350 # pragma warning(suppress: 6102)
351  return 63 - r;
352 }
353 # define FMT_BUILTIN_CLZLL(n) fmt::internal::clzll(n)
354 }
355 }
356 #endif
357 
358 namespace fmt {
359 namespace internal {
360 struct DummyInt {
361  int data[2];
362  operator int() const { return 0; }
363 };
365 
366 // Dummy implementations of system functions such as signbit and ecvt called
367 // if the latter are not available.
368 inline DummyInt signbit(...) { return DummyInt(); }
369 inline DummyInt _ecvt_s(...) { return DummyInt(); }
370 inline DummyInt isinf(...) { return DummyInt(); }
371 inline DummyInt _finite(...) { return DummyInt(); }
372 inline DummyInt isnan(...) { return DummyInt(); }
373 inline DummyInt _isnan(...) { return DummyInt(); }
374 
375 // A helper function to suppress bogus "conditional expression is constant"
376 // warnings.
377 template <typename T>
378 inline T const_check(T value) { return value; }
379 }
380 } // namespace fmt
381 
382 namespace std {
383 // Standard permits specialization of std::numeric_limits. This specialization
384 // is used to resolve ambiguity between isinf and std::isinf in glibc:
385 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48891
386 // and the same for isnan and signbit.
387 template <>
388 class numeric_limits<fmt::internal::DummyInt> :
389  public std::numeric_limits<int> {
390  public:
391  // Portable version of isinf.
392  template <typename T>
393  static bool isinfinity(T x) {
394  using namespace fmt::internal;
395  // The resolution "priority" is:
396  // isinf macro > std::isinf > ::isinf > fmt::internal::isinf
397  if (const_check(sizeof(isinf(x)) == sizeof(bool) ||
398  sizeof(isinf(x)) == sizeof(int))) {
399  return isinf(x) != 0;
400  }
401  return !_finite(static_cast<double>(x));
402  }
403 
404  // Portable version of isnan.
405  template <typename T>
406  static bool isnotanumber(T x) {
407  using namespace fmt::internal;
408  if (const_check(sizeof(isnan(x)) == sizeof(bool) ||
409  sizeof(isnan(x)) == sizeof(int))) {
410  return isnan(x) != 0;
411  }
412  return _isnan(static_cast<double>(x)) != 0;
413  }
414 
415  // Portable version of signbit.
416  static bool isnegative(double x) {
417  using namespace fmt::internal;
418  if (const_check(sizeof(signbit(x)) == sizeof(bool) ||
419  sizeof(signbit(x)) == sizeof(int))) {
420  return signbit(x) != 0;
421  }
422  if (x < 0) return true;
423  if (!isnotanumber(x)) return false;
424  int dec = 0, sign = 0;
425  char buffer[2]; // The buffer size must be >= 2 or _ecvt_s will fail.
426  _ecvt_s(buffer, sizeof(buffer), x, 0, &dec, &sign);
427  return sign != 0;
428  }
429 };
430 } // namespace std
431 
432 namespace fmt {
433 
434 // Fix the warning about long long on older versions of GCC
435 // that don't support the diagnostic pragma.
436 FMT_GCC_EXTENSION typedef long long LongLong;
437 FMT_GCC_EXTENSION typedef unsigned long long ULongLong;
438 
439 #if FMT_USE_RVALUE_REFERENCES
440 using std::move;
441 #endif
442 
443 template <typename Char>
445 
446 typedef BasicWriter<char> Writer;
448 
449 template <typename Char>
451 
452 struct FormatSpec;
453 
454 template <typename Impl, typename Char, typename Spec = fmt::FormatSpec>
456 
457 template <typename CharType,
460 
486 template <typename Char>
488  private:
489  const Char *data_;
490  std::size_t size_;
491 
492  public:
494  BasicStringRef(const Char *s, std::size_t size) : data_(s), size_(size) {}
495 
502  BasicStringRef(const Char *s)
503  : data_(s), size_(std::char_traits<Char>::length(s)) {}
504 
510  template <typename Allocator>
512  const std::basic_string<Char, std::char_traits<Char>, Allocator> &s)
513  : data_(s.c_str()), size_(s.size()) {}
514 
520  std::basic_string<Char> to_string() const {
521  return std::basic_string<Char>(data_, size_);
522  }
523 
525  const Char *data() const { return data_; }
526 
528  std::size_t size() const { return size_; }
529 
530  // Lexicographically compare this string reference to other.
531  int compare(BasicStringRef other) const {
532  std::size_t size = size_ < other.size_ ? size_ : other.size_;
533  int result = std::char_traits<Char>::compare(data_, other.data_, size);
534  if (result == 0)
535  result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1);
536  return result;
537  }
538 
539  friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) {
540  return lhs.compare(rhs) == 0;
541  }
542  friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs) {
543  return lhs.compare(rhs) != 0;
544  }
545  friend bool operator<(BasicStringRef lhs, BasicStringRef rhs) {
546  return lhs.compare(rhs) < 0;
547  }
548  friend bool operator<=(BasicStringRef lhs, BasicStringRef rhs) {
549  return lhs.compare(rhs) <= 0;
550  }
551  friend bool operator>(BasicStringRef lhs, BasicStringRef rhs) {
552  return lhs.compare(rhs) > 0;
553  }
554  friend bool operator>=(BasicStringRef lhs, BasicStringRef rhs) {
555  return lhs.compare(rhs) >= 0;
556  }
557 };
558 
561 
587 template <typename Char>
589  private:
590  const Char *data_;
591 
592  public:
594  BasicCStringRef(const Char *s) : data_(s) {}
595 
601  template <typename Allocator>
603  const std::basic_string<Char, std::char_traits<Char>, Allocator> &s)
604  : data_(s.c_str()) {}
605 
607  const Char *c_str() const { return data_; }
608 };
609 
612 
614 class FormatError : public std::runtime_error {
615  public:
616  explicit FormatError(CStringRef message)
617  : std::runtime_error(message.c_str()) {}
618  FormatError(const FormatError &ferr) : std::runtime_error(ferr) {}
620 };
621 
622 namespace internal {
623 
624 // MakeUnsigned<T>::Type gives an unsigned type corresponding to integer type T.
625 template <typename T>
626 struct MakeUnsigned { typedef T Type; };
627 
628 #define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U) \
629  template <> \
630  struct MakeUnsigned<T> { typedef U Type; }
631 
632 FMT_SPECIALIZE_MAKE_UNSIGNED(char, unsigned char);
633 FMT_SPECIALIZE_MAKE_UNSIGNED(signed char, unsigned char);
634 FMT_SPECIALIZE_MAKE_UNSIGNED(short, unsigned short);
635 FMT_SPECIALIZE_MAKE_UNSIGNED(int, unsigned);
636 FMT_SPECIALIZE_MAKE_UNSIGNED(long, unsigned long);
638 
639 // Casts nonnegative integer to unsigned.
640 template <typename Int>
641 inline typename MakeUnsigned<Int>::Type to_unsigned(Int value) {
642  FMT_ASSERT(value >= 0, "negative value");
643  return static_cast<typename MakeUnsigned<Int>::Type>(value);
644 }
645 
646 // The number of characters to store in the MemoryBuffer object itself
647 // to avoid dynamic memory allocation.
648 enum { INLINE_BUFFER_SIZE = 500 };
649 
650 #if FMT_SECURE_SCL
651 // Use checked iterator to avoid warnings on MSVC.
652 template <typename T>
653 inline stdext::checked_array_iterator<T*> make_ptr(T *ptr, std::size_t size) {
654  return stdext::checked_array_iterator<T*>(ptr, size);
655 }
656 #else
657 template <typename T>
658 inline T *make_ptr(T *ptr, std::size_t) { return ptr; }
659 #endif
660 } // namespace internal
661 
667 template <typename T>
668 class Buffer {
669  private:
671 
672  protected:
673  T *ptr_;
674  std::size_t size_;
675  std::size_t capacity_;
676 
677  Buffer(T *ptr = FMT_NULL, std::size_t capacity = 0)
678  : ptr_(ptr), size_(0), capacity_(capacity) {}
679 
686  virtual void grow(std::size_t size) = 0;
687 
688  public:
689  virtual ~Buffer() {}
690 
692  std::size_t size() const { return size_; }
693 
695  std::size_t capacity() const { return capacity_; }
696 
700  void resize(std::size_t new_size) {
701  if (new_size > capacity_)
702  grow(new_size);
703  size_ = new_size;
704  }
705 
711  void reserve(std::size_t capacity) {
712  if (capacity > capacity_)
713  grow(capacity);
714  }
715 
716  void clear() FMT_NOEXCEPT { size_ = 0; }
717 
718  void push_back(const T &value) {
719  if (size_ == capacity_)
720  grow(size_ + 1);
721  ptr_[size_++] = value;
722  }
723 
725  template <typename U>
726  void append(const U *begin, const U *end);
727 
728  T &operator[](std::size_t index) { return ptr_[index]; }
729  const T &operator[](std::size_t index) const { return ptr_[index]; }
730 };
731 
732 template <typename T>
733 template <typename U>
734 void Buffer<T>::append(const U *begin, const U *end) {
735  std::size_t new_size = size_ + internal::to_unsigned(end - begin);
736  if (new_size > capacity_)
737  grow(new_size);
738  std::uninitialized_copy(begin, end,
739  internal::make_ptr(ptr_, capacity_) + size_);
740  size_ = new_size;
741 }
742 
743 namespace internal {
744 
745 // A memory buffer for trivially copyable/constructible types with the first
746 // SIZE elements stored in the object itself.
747 template <typename T, std::size_t SIZE, typename Allocator = std::allocator<T> >
748 class MemoryBuffer : private Allocator, public Buffer<T> {
749  private:
750  T data_[SIZE];
751 
752  // Deallocate memory allocated by the buffer.
753  void deallocate() {
754  if (this->ptr_ != data_) Allocator::deallocate(this->ptr_, this->capacity_);
755  }
756 
757  protected:
758  void grow(std::size_t size) FMT_OVERRIDE;
759 
760  public:
761  explicit MemoryBuffer(const Allocator &alloc = Allocator())
762  : Allocator(alloc), Buffer<T>(data_, SIZE) {}
763  ~MemoryBuffer() { deallocate(); }
764 
765 #if FMT_USE_RVALUE_REFERENCES
766  private:
767  // Move data from other to this buffer.
768  void move(MemoryBuffer &other) {
769  Allocator &this_alloc = *this, &other_alloc = other;
770  this_alloc = std::move(other_alloc);
771  this->size_ = other.size_;
772  this->capacity_ = other.capacity_;
773  if (other.ptr_ == other.data_) {
774  this->ptr_ = data_;
775  std::uninitialized_copy(other.data_, other.data_ + this->size_,
776  make_ptr(data_, this->capacity_));
777  } else {
778  this->ptr_ = other.ptr_;
779  // Set pointer to the inline array so that delete is not called
780  // when deallocating.
781  other.ptr_ = other.data_;
782  }
783  }
784 
785  public:
786  MemoryBuffer(MemoryBuffer &&other) {
787  move(other);
788  }
789 
790  MemoryBuffer &operator=(MemoryBuffer &&other) {
791  assert(this != &other);
792  deallocate();
793  move(other);
794  return *this;
795  }
796 #endif
797 
798  // Returns a copy of the allocator associated with this buffer.
799  Allocator get_allocator() const { return *this; }
800 };
801 
802 template <typename T, std::size_t SIZE, typename Allocator>
804  std::size_t new_capacity = this->capacity_ + this->capacity_ / 2;
805  if (size > new_capacity)
806  new_capacity = size;
807  T *new_ptr = this->allocate(new_capacity, FMT_NULL);
808  // The following code doesn't throw, so the raw pointer above doesn't leak.
809  std::uninitialized_copy(this->ptr_, this->ptr_ + this->size_,
810  make_ptr(new_ptr, new_capacity));
811  std::size_t old_capacity = this->capacity_;
812  T *old_ptr = this->ptr_;
813  this->capacity_ = new_capacity;
814  this->ptr_ = new_ptr;
815  // deallocate may throw (at least in principle), but it doesn't matter since
816  // the buffer already uses the new storage and will deallocate it in case
817  // of exception.
818  if (old_ptr != data_)
819  Allocator::deallocate(old_ptr, old_capacity);
820 }
821 
822 // A fixed-size buffer.
823 template <typename Char>
824 class FixedBuffer : public fmt::Buffer<Char> {
825  public:
826  FixedBuffer(Char *array, std::size_t size) : fmt::Buffer<Char>(array, size) {}
827 
828  protected:
829  FMT_API void grow(std::size_t size) FMT_OVERRIDE;
830 };
831 
832 template <typename Char>
834  public:
835 #if FMT_SECURE_SCL
836  typedef stdext::checked_array_iterator<Char*> CharPtr;
837 #else
838  typedef Char *CharPtr;
839 #endif
840  static Char cast(int value) { return static_cast<Char>(value); }
841 };
842 
843 template <typename Char>
845 
846 template <>
847 class CharTraits<char> : public BasicCharTraits<char> {
848  private:
849  // Conversion from wchar_t to char is not allowed.
850  static char convert(wchar_t);
851 
852  public:
853  static char convert(char value) { return value; }
854 
855  // Formats a floating-point number.
856  template <typename T>
857  FMT_API static int format_float(char *buffer, std::size_t size,
858  const char *format, unsigned width, int precision, T value);
859 };
860 
861 #if FMT_USE_EXTERN_TEMPLATES
862 extern template int CharTraits<char>::format_float<double>
863  (char *buffer, std::size_t size,
864  const char* format, unsigned width, int precision, double value);
865 extern template int CharTraits<char>::format_float<long double>
866  (char *buffer, std::size_t size,
867  const char* format, unsigned width, int precision, long double value);
868 #endif
869 
870 template <>
871 class CharTraits<wchar_t> : public BasicCharTraits<wchar_t> {
872  public:
873  static wchar_t convert(char value) { return value; }
874  static wchar_t convert(wchar_t value) { return value; }
875 
876  template <typename T>
877  FMT_API static int format_float(wchar_t *buffer, std::size_t size,
878  const wchar_t *format, unsigned width, int precision, T value);
879 };
880 
881 #if FMT_USE_EXTERN_TEMPLATES
882 extern template int CharTraits<wchar_t>::format_float<double>
883  (wchar_t *buffer, std::size_t size,
884  const wchar_t* format, unsigned width, int precision, double value);
885 extern template int CharTraits<wchar_t>::format_float<long double>
886  (wchar_t *buffer, std::size_t size,
887  const wchar_t* format, unsigned width, int precision, long double value);
888 #endif
889 
890 // Checks if a number is negative - used to avoid warnings.
891 template <bool IsSigned>
892 struct SignChecker {
893  template <typename T>
894  static bool is_negative(T value) { return value < 0; }
895 };
896 
897 template <>
899  template <typename T>
900  static bool is_negative(T) { return false; }
901 };
902 
903 // Returns true if value is negative, false otherwise.
904 // Same as (value < 0) but doesn't produce warnings if T is an unsigned type.
905 template <typename T>
906 inline bool is_negative(T value) {
908 }
909 
910 // Selects uint32_t if FitsIn32Bits is true, uint64_t otherwise.
911 template <bool FitsIn32Bits>
912 struct TypeSelector { typedef uint32_t Type; };
913 
914 template <>
915 struct TypeSelector<false> { typedef uint64_t Type; };
916 
917 template <typename T>
918 struct IntTraits {
919  // Smallest of uint32_t and uint64_t that is large enough to represent
920  // all values of T.
921  typedef typename
922  TypeSelector<std::numeric_limits<T>::digits <= 32>::Type MainType;
923 };
924 
925 FMT_API void report_unknown_type(char code, const char *type);
926 
927 // Static data is placed in this class template to allow header-only
928 // configuration.
929 template <typename T = void>
931  static const uint32_t POWERS_OF_10_32[];
932  static const uint64_t POWERS_OF_10_64[];
933  static const char DIGITS[];
934 };
935 
936 #if FMT_USE_EXTERN_TEMPLATES
937 extern template struct BasicData<void>;
938 #endif
939 
941 
942 #ifdef FMT_BUILTIN_CLZLL
943 // Returns the number of decimal digits in n. Leading zeros are not counted
944 // except for n == 0 in which case count_digits returns 1.
945 inline unsigned count_digits(uint64_t n) {
946  // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
947  // and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits.
948  int t = (64 - FMT_BUILTIN_CLZLL(n | 1)) * 1233 >> 12;
949  return to_unsigned(t) - (n < Data::POWERS_OF_10_64[t]) + 1;
950 }
951 #else
952 // Fallback version of count_digits used when __builtin_clz is not available.
953 inline unsigned count_digits(uint64_t n) {
954  unsigned count = 1;
955  for (;;) {
956  // Integer division is slow so do it for a group of four digits instead
957  // of for every digit. The idea comes from the talk by Alexandrescu
958  // "Three Optimization Tips for C++". See speed-test for a comparison.
959  if (n < 10) return count;
960  if (n < 100) return count + 1;
961  if (n < 1000) return count + 2;
962  if (n < 10000) return count + 3;
963  n /= 10000u;
964  count += 4;
965  }
966 }
967 #endif
968 
969 #ifdef FMT_BUILTIN_CLZ
970 // Optional version of count_digits for better performance on 32-bit platforms.
971 inline unsigned count_digits(uint32_t n) {
972  int t = (32 - FMT_BUILTIN_CLZ(n | 1)) * 1233 >> 12;
973  return to_unsigned(t) - (n < Data::POWERS_OF_10_32[t]) + 1;
974 }
975 #endif
976 
977 // A functor that doesn't add a thousands separator.
979  template <typename Char>
980  void operator()(Char *) {}
981 };
982 
983 // A functor that adds a thousands separator.
985  private:
987 
988  // Index of a decimal digit with the least significant digit having index 0.
989  unsigned digit_index_;
990 
991  public:
992  explicit ThousandsSep(fmt::StringRef sep) : sep_(sep), digit_index_(0) {}
993 
994  template <typename Char>
995  void operator()(Char *&buffer) {
996  if (++digit_index_ % 3 != 0)
997  return;
998  buffer -= sep_.size();
999  std::uninitialized_copy(sep_.data(), sep_.data() + sep_.size(),
1000  internal::make_ptr(buffer, sep_.size()));
1001  }
1002 };
1003 
1004 // Formats a decimal unsigned integer value writing into buffer.
1005 // thousands_sep is a functor that is called after writing each char to
1006 // add a thousands separator if necessary.
1007 template <typename UInt, typename Char, typename ThousandsSep>
1008 inline void format_decimal(Char *buffer, UInt value, unsigned num_digits,
1010  buffer += num_digits;
1011  while (value >= 100) {
1012  // Integer division is slow so do it for a group of two digits instead
1013  // of for every digit. The idea comes from the talk by Alexandrescu
1014  // "Three Optimization Tips for C++". See speed-test for a comparison.
1015  unsigned index = static_cast<unsigned>((value % 100) * 2);
1016  value /= 100;
1017  *--buffer = Data::DIGITS[index + 1];
1018  thousands_sep(buffer);
1019  *--buffer = Data::DIGITS[index];
1020  thousands_sep(buffer);
1021  }
1022  if (value < 10) {
1023  *--buffer = static_cast<char>('0' + value);
1024  return;
1025  }
1026  unsigned index = static_cast<unsigned>(value * 2);
1027  *--buffer = Data::DIGITS[index + 1];
1028  thousands_sep(buffer);
1029  *--buffer = Data::DIGITS[index];
1030 }
1031 
1032 template <typename UInt, typename Char>
1033 inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) {
1034  format_decimal(buffer, value, num_digits, NoThousandsSep());
1035  return;
1036 }
1037 
1038 #ifndef _WIN32
1039 # define FMT_USE_WINDOWS_H 0
1040 #elif !defined(FMT_USE_WINDOWS_H)
1041 # define FMT_USE_WINDOWS_H 1
1042 #endif
1043 
1044 // Define FMT_USE_WINDOWS_H to 0 to disable use of windows.h.
1045 // All the functionality that relies on it will be disabled too.
1046 #if FMT_USE_WINDOWS_H
1047 // A converter from UTF-8 to UTF-16.
1048 // It is only provided for Windows since other systems support UTF-8 natively.
1049 class UTF8ToUTF16 {
1050  private:
1052 
1053  public:
1054  FMT_API explicit UTF8ToUTF16(StringRef s);
1055  operator WStringRef() const { return WStringRef(&buffer_[0], size()); }
1056  size_t size() const { return buffer_.size() - 1; }
1057  const wchar_t *c_str() const { return &buffer_[0]; }
1058  std::wstring str() const { return std::wstring(&buffer_[0], size()); }
1059 };
1060 
1061 // A converter from UTF-16 to UTF-8.
1062 // It is only provided for Windows since other systems support UTF-8 natively.
1063 class UTF16ToUTF8 {
1064  private:
1066 
1067  public:
1068  UTF16ToUTF8() {}
1069  FMT_API explicit UTF16ToUTF8(WStringRef s);
1070  operator StringRef() const { return StringRef(&buffer_[0], size()); }
1071  size_t size() const { return buffer_.size() - 1; }
1072  const char *c_str() const { return &buffer_[0]; }
1073  std::string str() const { return std::string(&buffer_[0], size()); }
1074 
1075  // Performs conversion returning a system error code instead of
1076  // throwing exception on conversion error. This method may still throw
1077  // in case of memory allocation error.
1078  FMT_API int convert(WStringRef s);
1079 };
1080 
1081 FMT_API void format_windows_error(fmt::Writer &out, int error_code,
1082  fmt::StringRef message) FMT_NOEXCEPT;
1083 #endif
1084 
1085 // A formatting argument value.
1086 struct Value {
1087  template <typename Char>
1088  struct StringValue {
1089  const Char *value;
1090  std::size_t size;
1091  };
1092 
1093  typedef void (*FormatFunc)(
1094  void *formatter, const void *arg, void *format_str_ptr);
1095 
1096  struct CustomValue {
1097  const void *value;
1099  };
1100 
1101  union {
1103  unsigned uint_value;
1107  long double long_double_value;
1108  const void *pointer;
1114  };
1115 
1116  enum Type {
1117  NONE, NAMED_ARG,
1118  // Integer types should go first,
1119  INT, UINT, LONG_LONG, ULONG_LONG, BOOL, CHAR, LAST_INTEGER_TYPE = CHAR,
1120  // followed by floating-point types.
1121  DOUBLE, LONG_DOUBLE, LAST_NUMERIC_TYPE = LONG_DOUBLE,
1122  CSTRING, STRING, WSTRING, POINTER, CUSTOM
1123  };
1124 };
1125 
1126 // A formatting argument. It is a trivially copyable/constructible type to
1127 // allow storage in internal::MemoryBuffer.
1128 struct Arg : Value {
1130 };
1131 
1132 template <typename Char>
1133 struct NamedArg;
1134 template <typename Char, typename T>
1136 
1137 template <typename T = void>
1138 struct Null {};
1139 
1140 // A helper class template to enable or disable overloads taking wide
1141 // characters and strings in MakeValue.
1142 template <typename T, typename Char>
1143 struct WCharHelper {
1145  typedef T Unsupported;
1146 };
1147 
1148 template <typename T>
1150  typedef T Supported;
1152 };
1153 
1154 typedef char Yes[1];
1155 typedef char No[2];
1156 
1157 template <typename T>
1158 T &get();
1159 
1160 // These are non-members to workaround an overload resolution bug in bcc32.
1162 No &convert(...);
1163 
1164 template<typename T, bool ENABLE_CONVERSION>
1166  enum { value = ENABLE_CONVERSION };
1167 };
1168 
1169 template<typename T, bool ENABLE_CONVERSION>
1171  enum { value = false };
1172 };
1173 
1174 template<typename T>
1176  enum {
1177  // Don't convert numeric types.
1179  };
1180 };
1181 
1182 template<typename T>
1184  enum {
1185  enable_conversion = sizeof(fmt::internal::convert(get<T>())) == sizeof(Yes)
1186  };
1188 };
1189 
1190 #define FMT_DISABLE_CONVERSION_TO_INT(Type) \
1191  template <> \
1192  struct ConvertToInt<Type> { enum { value = 0 }; }
1193 
1194 // Silence warnings about convering float to int.
1197 FMT_DISABLE_CONVERSION_TO_INT(long double);
1198 
1199 template<bool B, class T = void>
1200 struct EnableIf {};
1201 
1202 template<class T>
1203 struct EnableIf<true, T> { typedef T type; };
1204 
1205 template<bool B, class T, class F>
1206 struct Conditional { typedef T type; };
1207 
1208 template<class T, class F>
1209 struct Conditional<false, T, F> { typedef F type; };
1210 
1211 // For bcc32 which doesn't understand ! in template arguments.
1212 template <bool>
1213 struct Not { enum { value = 0 }; };
1214 
1215 template <>
1216 struct Not<false> { enum { value = 1 }; };
1217 
1218 template <typename T>
1219 struct FalseType { enum { value = 0 }; };
1220 
1221 template <typename T, T> struct LConvCheck {
1222  LConvCheck(int) {}
1223 };
1224 
1225 // Returns the thousands separator for the current locale.
1226 // We check if ``lconv`` contains ``thousands_sep`` because on Android
1227 // ``lconv`` is stubbed as an empty struct.
1228 template <typename LConv>
1231  return lc->thousands_sep;
1232 }
1233 
1234 inline fmt::StringRef thousands_sep(...) { return ""; }
1235 
1236 #define FMT_CONCAT(a, b) a##b
1237 
1238 #if FMT_GCC_VERSION >= 303
1239 # define FMT_UNUSED __attribute__((unused))
1240 #else
1241 # define FMT_UNUSED
1242 #endif
1243 
1244 #ifndef FMT_USE_STATIC_ASSERT
1245 # define FMT_USE_STATIC_ASSERT 0
1246 #endif
1247 
1248 #if FMT_USE_STATIC_ASSERT || FMT_HAS_FEATURE(cxx_static_assert) || \
1249  (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600
1250 # define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message)
1251 #else
1252 # define FMT_CONCAT_(a, b) FMT_CONCAT(a, b)
1253 # define FMT_STATIC_ASSERT(cond, message) \
1254  typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED
1255 #endif
1256 
1257 template <typename Formatter, typename Char, typename T>
1258 void format_arg(Formatter &, const Char *, const T &) {
1260  "Cannot format argument. To enable the use of ostream "
1261  "operator<< include fmt/ostream.h. Otherwise provide "
1262  "an overload of format_arg.");
1263 }
1264 
1265 // Makes an Arg object from any type.
1266 template <typename Formatter>
1267 class MakeValue : public Arg {
1268  public:
1269  typedef typename Formatter::Char Char;
1270 
1271  private:
1272  // The following two methods are private to disallow formatting of
1273  // arbitrary pointers. If you want to output a pointer cast it to
1274  // "void *" or "const void *". In particular, this forbids formatting
1275  // of "[const] volatile char *" which is printed as bool by iostreams.
1276  // Do not implement!
1277  template <typename T>
1278  MakeValue(const T *value);
1279  template <typename T>
1280  MakeValue(T *value);
1281 
1282  // The following methods are private to disallow formatting of wide
1283  // characters and strings into narrow strings as in
1284  // fmt::format("{}", L"test");
1285  // To fix this, use a wide format string: fmt::format(L"{}", L"test").
1286 #if !FMT_MSC_VER || defined(_NATIVE_WCHAR_T_DEFINED)
1288 #endif
1293 
1294  void set_string(StringRef str) {
1295  string.value = str.data();
1296  string.size = str.size();
1297  }
1298 
1300  wstring.value = str.data();
1301  wstring.size = str.size();
1302  }
1303 
1304  // Formats an argument of a custom type, such as a user-defined class.
1305  template <typename T>
1306  static void format_custom_arg(
1307  void *formatter, const void *arg, void *format_str_ptr) {
1308  format_arg(*static_cast<Formatter*>(formatter),
1309  *static_cast<const Char**>(format_str_ptr),
1310  *static_cast<const T*>(arg));
1311  }
1312 
1313  public:
1315 
1316 #define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \
1317  MakeValue(Type value) { field = rhs; } \
1318  static uint64_t type(Type) { return Arg::TYPE; }
1319 
1320 #define FMT_MAKE_VALUE(Type, field, TYPE) \
1321  FMT_MAKE_VALUE_(Type, field, TYPE, value)
1322 
1323  FMT_MAKE_VALUE(bool, int_value, BOOL)
1324  FMT_MAKE_VALUE(short, int_value, INT)
1325  FMT_MAKE_VALUE(unsigned short, uint_value, UINT)
1326  FMT_MAKE_VALUE(int, int_value, INT)
1327  FMT_MAKE_VALUE(unsigned, uint_value, UINT)
1328 
1329  MakeValue(long value) {
1330  // To minimize the number of types we need to deal with, long is
1331  // translated either to int or to long long depending on its size.
1332  if (const_check(sizeof(long) == sizeof(int)))
1333  int_value = static_cast<int>(value);
1334  else
1335  long_long_value = value;
1336  }
1337  static uint64_t type(long) {
1338  return sizeof(long) == sizeof(int) ? Arg::INT : Arg::LONG_LONG;
1339  }
1340 
1341  MakeValue(unsigned long value) {
1342  if (const_check(sizeof(unsigned long) == sizeof(unsigned)))
1343  uint_value = static_cast<unsigned>(value);
1344  else
1345  ulong_long_value = value;
1346  }
1347  static uint64_t type(unsigned long) {
1348  return sizeof(unsigned long) == sizeof(unsigned) ?
1349  Arg::UINT : Arg::ULONG_LONG;
1350  }
1351 
1352  FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG)
1353  FMT_MAKE_VALUE(ULongLong, ulong_long_value, ULONG_LONG)
1354  FMT_MAKE_VALUE(float, double_value, DOUBLE)
1355  FMT_MAKE_VALUE(double, double_value, DOUBLE)
1356  FMT_MAKE_VALUE(long double, long_double_value, LONG_DOUBLE)
1357  FMT_MAKE_VALUE(signed char, int_value, INT)
1358  FMT_MAKE_VALUE(unsigned char, uint_value, UINT)
1359  FMT_MAKE_VALUE(char, int_value, CHAR)
1360 
1361 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
1363  int_value = value;
1364  }
1365  static uint64_t type(wchar_t) { return Arg::CHAR; }
1366 #endif
1367 
1368 #define FMT_MAKE_STR_VALUE(Type, TYPE) \
1369  MakeValue(Type value) { set_string(value); } \
1370  static uint64_t type(Type) { return Arg::TYPE; }
1371 
1372  FMT_MAKE_VALUE(char *, string.value, CSTRING)
1373  FMT_MAKE_VALUE(const char *, string.value, CSTRING)
1374  FMT_MAKE_VALUE(signed char *, sstring.value, CSTRING)
1375  FMT_MAKE_VALUE(const signed char *, sstring.value, CSTRING)
1376  FMT_MAKE_VALUE(unsigned char *, ustring.value, CSTRING)
1377  FMT_MAKE_VALUE(const unsigned char *, ustring.value, CSTRING)
1378  FMT_MAKE_STR_VALUE(const std::string &, STRING)
1379  FMT_MAKE_STR_VALUE(StringRef, STRING)
1380  FMT_MAKE_VALUE_(CStringRef, string.value, CSTRING, value.c_str())
1381 
1382 #define FMT_MAKE_WSTR_VALUE(Type, TYPE) \
1383  MakeValue(typename WCharHelper<Type, Char>::Supported value) { \
1384  set_string(value); \
1385  } \
1386  static uint64_t type(Type) { return Arg::TYPE; }
1387 
1388  FMT_MAKE_WSTR_VALUE(wchar_t *, WSTRING)
1389  FMT_MAKE_WSTR_VALUE(const wchar_t *, WSTRING)
1390  FMT_MAKE_WSTR_VALUE(const std::wstring &, WSTRING)
1392 
1393  FMT_MAKE_VALUE(void *, pointer, POINTER)
1394  FMT_MAKE_VALUE(const void *, pointer, POINTER)
1395 
1396  template <typename T>
1397  MakeValue(const T &value,
1398  typename EnableIf<Not<
1399  ConvertToInt<T>::value>::value, int>::type = 0) {
1400  custom.value = &value;
1401  custom.format = &format_custom_arg<T>;
1402  }
1403 
1404  template <typename T>
1405  MakeValue(const T &value,
1406  typename EnableIf<ConvertToInt<T>::value, int>::type = 0) {
1407  int_value = value;
1408  }
1409 
1410  template <typename T>
1411  static uint64_t type(const T &) {
1412  return ConvertToInt<T>::value ? Arg::INT : Arg::CUSTOM;
1413  }
1414 
1415  // Additional template param `Char_` is needed here because make_type always
1416  // uses char.
1417  template <typename Char_>
1418  MakeValue(const NamedArg<Char_> &value) { pointer = &value; }
1419  template <typename Char_, typename T>
1420  MakeValue(const NamedArgWithType<Char_, T> &value) { pointer = &value; }
1421 
1422  template <typename Char_>
1423  static uint64_t type(const NamedArg<Char_> &) { return Arg::NAMED_ARG; }
1424  template <typename Char_, typename T>
1425  static uint64_t type(const NamedArgWithType<Char_, T> &) { return Arg::NAMED_ARG; }
1426 };
1427 
1428 template <typename Formatter>
1429 class MakeArg : public Arg {
1430 public:
1432  type = Arg::NONE;
1433  }
1434 
1435  template <typename T>
1436  MakeArg(const T &value)
1437  : Arg(MakeValue<Formatter>(value)) {
1438  type = static_cast<Arg::Type>(MakeValue<Formatter>::type(value));
1439  }
1440 };
1441 
1442 template <typename Char>
1443 struct NamedArg : Arg {
1445 
1446  template <typename T>
1447  NamedArg(BasicStringRef<Char> argname, const T &value)
1448  : Arg(MakeArg< BasicFormatter<Char> >(value)), name(argname) {}
1449 };
1450 
1451 template <typename Char, typename T>
1452 struct NamedArgWithType : NamedArg<Char> {
1453  NamedArgWithType(BasicStringRef<Char> argname, const T &value)
1454  : NamedArg<Char>(argname, value) {}
1455 };
1456 
1457 class RuntimeError : public std::runtime_error {
1458  protected:
1459  RuntimeError() : std::runtime_error("") {}
1460  RuntimeError(const RuntimeError &rerr) : std::runtime_error(rerr) {}
1462 };
1463 
1464 template <typename Char>
1465 class ArgMap;
1466 } // namespace internal
1467 
1469 class ArgList {
1470  private:
1471  // To reduce compiled code size per formatting function call, types of first
1472  // MAX_PACKED_ARGS arguments are passed in the types_ field.
1473  uint64_t types_;
1474  union {
1475  // If the number of arguments is less than MAX_PACKED_ARGS, the argument
1476  // values are stored in values_, otherwise they are stored in args_.
1477  // This is done to reduce compiled code size as storing larger objects
1478  // may require more code (at least on x86-64) even if the same amount of
1479  // data is actually copied to stack. It saves ~10% on the bloat test.
1482  };
1483 
1484  internal::Arg::Type type(unsigned index) const {
1485  return type(types_, index);
1486  }
1487 
1488  template <typename Char>
1489  friend class internal::ArgMap;
1490 
1491  public:
1492  // Maximum number of arguments with packed types.
1493  enum { MAX_PACKED_ARGS = 16 };
1494 
1495  ArgList() : types_(0) {}
1496 
1497  ArgList(ULongLong types, const internal::Value *values)
1498  : types_(types), values_(values) {}
1499  ArgList(ULongLong types, const internal::Arg *args)
1500  : types_(types), args_(args) {}
1501 
1502  uint64_t types() const { return types_; }
1503 
1505  internal::Arg operator[](unsigned index) const {
1506  using internal::Arg;
1507  Arg arg;
1508  bool use_values = type(MAX_PACKED_ARGS - 1) == Arg::NONE;
1509  if (index < MAX_PACKED_ARGS) {
1510  Arg::Type arg_type = type(index);
1511  internal::Value &val = arg;
1512  if (arg_type != Arg::NONE)
1513  val = use_values ? values_[index] : args_[index];
1514  arg.type = arg_type;
1515  return arg;
1516  }
1517  if (use_values) {
1518  // The index is greater than the number of arguments that can be stored
1519  // in values, so return a "none" argument.
1520  arg.type = Arg::NONE;
1521  return arg;
1522  }
1523  for (unsigned i = MAX_PACKED_ARGS; i <= index; ++i) {
1524  if (args_[i].type == Arg::NONE)
1525  return args_[i];
1526  }
1527  return args_[index];
1528  }
1529 
1530  static internal::Arg::Type type(uint64_t types, unsigned index) {
1531  unsigned shift = index * 4;
1532  uint64_t mask = 0xf;
1533  return static_cast<internal::Arg::Type>(
1534  (types & (mask << shift)) >> shift);
1535  }
1536 };
1537 
1538 #define FMT_DISPATCH(call) static_cast<Impl*>(this)->call
1539 
1564 template <typename Impl, typename Result>
1565 class ArgVisitor {
1566  private:
1568 
1569  public:
1571 
1573  FMT_DISPATCH(report_unhandled_arg());
1574  return Result();
1575  }
1576 
1578  Result visit_int(int value) {
1579  return FMT_DISPATCH(visit_any_int(value));
1580  }
1581 
1583  Result visit_long_long(LongLong value) {
1584  return FMT_DISPATCH(visit_any_int(value));
1585  }
1586 
1588  Result visit_uint(unsigned value) {
1589  return FMT_DISPATCH(visit_any_int(value));
1590  }
1591 
1593  Result visit_ulong_long(ULongLong value) {
1594  return FMT_DISPATCH(visit_any_int(value));
1595  }
1596 
1598  Result visit_bool(bool value) {
1599  return FMT_DISPATCH(visit_any_int(value));
1600  }
1601 
1603  Result visit_char(int value) {
1604  return FMT_DISPATCH(visit_any_int(value));
1605  }
1606 
1608  template <typename T>
1609  Result visit_any_int(T) {
1610  return FMT_DISPATCH(visit_unhandled_arg());
1611  }
1612 
1614  Result visit_double(double value) {
1615  return FMT_DISPATCH(visit_any_double(value));
1616  }
1617 
1619  Result visit_long_double(long double value) {
1620  return FMT_DISPATCH(visit_any_double(value));
1621  }
1622 
1624  template <typename T>
1626  return FMT_DISPATCH(visit_unhandled_arg());
1627  }
1628 
1630  Result visit_cstring(const char *) {
1631  return FMT_DISPATCH(visit_unhandled_arg());
1632  }
1633 
1636  return FMT_DISPATCH(visit_unhandled_arg());
1637  }
1638 
1641  return FMT_DISPATCH(visit_unhandled_arg());
1642  }
1643 
1645  Result visit_pointer(const void *) {
1646  return FMT_DISPATCH(visit_unhandled_arg());
1647  }
1648 
1651  return FMT_DISPATCH(visit_unhandled_arg());
1652  }
1653 
1662  Result visit(const Arg &arg) {
1663  switch (arg.type) {
1664  case Arg::NONE:
1665  case Arg::NAMED_ARG:
1666  FMT_ASSERT(false, "invalid argument type");
1667  break;
1668  case Arg::INT:
1669  return FMT_DISPATCH(visit_int(arg.int_value));
1670  case Arg::UINT:
1671  return FMT_DISPATCH(visit_uint(arg.uint_value));
1672  case Arg::LONG_LONG:
1673  return FMT_DISPATCH(visit_long_long(arg.long_long_value));
1674  case Arg::ULONG_LONG:
1675  return FMT_DISPATCH(visit_ulong_long(arg.ulong_long_value));
1676  case Arg::BOOL:
1677  return FMT_DISPATCH(visit_bool(arg.int_value != 0));
1678  case Arg::CHAR:
1679  return FMT_DISPATCH(visit_char(arg.int_value));
1680  case Arg::DOUBLE:
1681  return FMT_DISPATCH(visit_double(arg.double_value));
1682  case Arg::LONG_DOUBLE:
1683  return FMT_DISPATCH(visit_long_double(arg.long_double_value));
1684  case Arg::CSTRING:
1685  return FMT_DISPATCH(visit_cstring(arg.string.value));
1686  case Arg::STRING:
1687  return FMT_DISPATCH(visit_string(arg.string));
1688  case Arg::WSTRING:
1689  return FMT_DISPATCH(visit_wstring(arg.wstring));
1690  case Arg::POINTER:
1691  return FMT_DISPATCH(visit_pointer(arg.pointer));
1692  case Arg::CUSTOM:
1693  return FMT_DISPATCH(visit_custom(arg.custom));
1694  }
1695  return Result();
1696  }
1697 };
1698 
1701 };
1702 
1703 // Flags.
1704 enum {
1706  CHAR_FLAG = 0x10 // Argument has char type - used in error reporting.
1707 };
1708 
1709 // An empty format specifier.
1710 struct EmptySpec {};
1711 
1712 // A type specifier.
1713 template <char TYPE>
1715  Alignment align() const { return ALIGN_DEFAULT; }
1716  unsigned width() const { return 0; }
1717  int precision() const { return -1; }
1718  bool flag(unsigned) const { return false; }
1719  char type() const { return TYPE; }
1720  char type_prefix() const { return TYPE; }
1721  char fill() const { return ' '; }
1722 };
1723 
1724 // A width specifier.
1725 struct WidthSpec {
1726  unsigned width_;
1727  // Fill is always wchar_t and cast to char if necessary to avoid having
1728  // two specialization of WidthSpec and its subclasses.
1729  wchar_t fill_;
1730 
1731  WidthSpec(unsigned width, wchar_t fill) : width_(width), fill_(fill) {}
1732 
1733  unsigned width() const { return width_; }
1734  wchar_t fill() const { return fill_; }
1735 };
1736 
1737 // An alignment specifier.
1740 
1741  AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT)
1742  : WidthSpec(width, fill), align_(align) {}
1743 
1744  Alignment align() const { return align_; }
1745 
1746  int precision() const { return -1; }
1747 };
1748 
1749 // An alignment and type specifier.
1750 template <char TYPE>
1752  AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {}
1753 
1754  bool flag(unsigned) const { return false; }
1755  char type() const { return TYPE; }
1756  char type_prefix() const { return TYPE; }
1757 };
1758 
1759 // A full format specifier.
1761  unsigned flags_;
1763  char type_;
1764 
1766  unsigned width = 0, char type = 0, wchar_t fill = ' ')
1767  : AlignSpec(width, fill), flags_(0), precision_(-1), type_(type) {}
1768 
1769  bool flag(unsigned f) const { return (flags_ & f) != 0; }
1770  int precision() const { return precision_; }
1771  char type() const { return type_; }
1772  char type_prefix() const { return type_; }
1773 };
1774 
1775 // An integer format specifier.
1776 template <typename T, typename SpecT = TypeSpec<0>, typename Char = char>
1777 class IntFormatSpec : public SpecT {
1778  private:
1780 
1781  public:
1782  IntFormatSpec(T val, const SpecT &spec = SpecT())
1783  : SpecT(spec), value_(val) {}
1784 
1785  T value() const { return value_; }
1786 };
1787 
1788 // A string format specifier.
1789 template <typename Char>
1790 class StrFormatSpec : public AlignSpec {
1791  private:
1792  const Char *str_;
1793 
1794  public:
1795  template <typename FillChar>
1796  StrFormatSpec(const Char *str, unsigned width, FillChar fill)
1797  : AlignSpec(width, fill), str_(str) {
1799  }
1800 
1801  const Char *str() const { return str_; }
1802 };
1803 
1807 IntFormatSpec<int, TypeSpec<'b'> > bin(int value);
1808 
1812 IntFormatSpec<int, TypeSpec<'o'> > oct(int value);
1813 
1818 IntFormatSpec<int, TypeSpec<'x'> > hex(int value);
1819 
1824 IntFormatSpec<int, TypeSpec<'X'> > hexu(int value);
1825 
1840 template <char TYPE_CODE, typename Char>
1841 IntFormatSpec<int, AlignTypeSpec<TYPE_CODE>, Char> pad(
1842  int value, unsigned width, Char fill = ' ');
1843 
1844 #define FMT_DEFINE_INT_FORMATTERS(TYPE) \
1845 inline IntFormatSpec<TYPE, TypeSpec<'b'> > bin(TYPE value) { \
1846  return IntFormatSpec<TYPE, TypeSpec<'b'> >(value, TypeSpec<'b'>()); \
1847 } \
1848  \
1849 inline IntFormatSpec<TYPE, TypeSpec<'o'> > oct(TYPE value) { \
1850  return IntFormatSpec<TYPE, TypeSpec<'o'> >(value, TypeSpec<'o'>()); \
1851 } \
1852  \
1853 inline IntFormatSpec<TYPE, TypeSpec<'x'> > hex(TYPE value) { \
1854  return IntFormatSpec<TYPE, TypeSpec<'x'> >(value, TypeSpec<'x'>()); \
1855 } \
1856  \
1857 inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
1858  return IntFormatSpec<TYPE, TypeSpec<'X'> >(value, TypeSpec<'X'>()); \
1859 } \
1860  \
1861 template <char TYPE_CODE> \
1862 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> > pad( \
1863  IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, unsigned width) { \
1864  return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> >( \
1865  f.value(), AlignTypeSpec<TYPE_CODE>(width, ' ')); \
1866 } \
1867  \
1868 /* For compatibility with older compilers we provide two overloads for pad, */ \
1869 /* one that takes a fill character and one that doesn't. In the future this */ \
1870 /* can be replaced with one overload making the template argument Char */ \
1871 /* default to char (C++11). */ \
1872 template <char TYPE_CODE, typename Char> \
1873 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char> pad( \
1874  IntFormatSpec<TYPE, TypeSpec<TYPE_CODE>, Char> f, \
1875  unsigned width, Char fill) { \
1876  return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char>( \
1877  f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \
1878 } \
1879  \
1880 inline IntFormatSpec<TYPE, AlignTypeSpec<0> > pad( \
1881  TYPE value, unsigned width) { \
1882  return IntFormatSpec<TYPE, AlignTypeSpec<0> >( \
1883  value, AlignTypeSpec<0>(width, ' ')); \
1884 } \
1885  \
1886 template <typename Char> \
1887 inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
1888  TYPE value, unsigned width, Char fill) { \
1889  return IntFormatSpec<TYPE, AlignTypeSpec<0>, Char>( \
1890  value, AlignTypeSpec<0>(width, fill)); \
1891 }
1892 
1895 FMT_DEFINE_INT_FORMATTERS(unsigned)
1896 FMT_DEFINE_INT_FORMATTERS(unsigned long)
1899 
1900 
1912 template <typename Char>
1914  const Char *str, unsigned width, Char fill = ' ') {
1915  return StrFormatSpec<Char>(str, width, fill);
1916 }
1917 
1919  const wchar_t *str, unsigned width, char fill = ' ') {
1920  return StrFormatSpec<wchar_t>(str, width, fill);
1921 }
1922 
1923 namespace internal {
1924 
1925 template <typename Char>
1926 class ArgMap {
1927  private:
1928  typedef std::vector<
1929  std::pair<fmt::BasicStringRef<Char>, internal::Arg> > MapType;
1930  typedef typename MapType::value_type Pair;
1931 
1933 
1934  public:
1935  FMT_API void init(const ArgList &args);
1936 
1938  // The list is unsorted, so just return the first matching name.
1939  for (typename MapType::const_iterator it = map_.begin(), end = map_.end();
1940  it != end; ++it) {
1941  if (it->first == name)
1942  return &it->second;
1943  }
1944  return FMT_NULL;
1945  }
1946 };
1947 
1948 template <typename Impl, typename Char, typename Spec = fmt::FormatSpec>
1949 class ArgFormatterBase : public ArgVisitor<Impl, void> {
1950  private:
1952  Spec &spec_;
1953 
1955 
1956  void write_pointer(const void *p) {
1957  spec_.flags_ = HASH_FLAG;
1958  spec_.type_ = 'x';
1959  writer_.write_int(reinterpret_cast<uintptr_t>(p), spec_);
1960  }
1961 
1962  protected:
1963  BasicWriter<Char> &writer() { return writer_; }
1964  Spec &spec() { return spec_; }
1965 
1966  void write(bool value) {
1967  const char *str_value = value ? "true" : "false";
1968  Arg::StringValue<char> str = { str_value, std::strlen(str_value) };
1969  writer_.write_str(str, spec_);
1970  }
1971 
1972  void write(const char *value) {
1973  Arg::StringValue<char> str = {value, value ? std::strlen(value) : 0};
1974  writer_.write_str(str, spec_);
1975  }
1976 
1977  public:
1978  typedef Spec SpecType;
1979 
1981  : writer_(w), spec_(s) {}
1982 
1983  template <typename T>
1984  void visit_any_int(T value) { writer_.write_int(value, spec_); }
1985 
1986  template <typename T>
1987  void visit_any_double(T value) { writer_.write_double(value, spec_); }
1988 
1989  void visit_bool(bool value) {
1990  if (spec_.type_) {
1991  visit_any_int(value);
1992  return;
1993  }
1994  write(value);
1995  }
1996 
1997  void visit_char(int value) {
1998  if (spec_.type_ && spec_.type_ != 'c') {
1999  spec_.flags_ |= CHAR_FLAG;
2000  writer_.write_int(value, spec_);
2001  return;
2002  }
2003  if (spec_.align_ == ALIGN_NUMERIC || spec_.flags_ != 0)
2004  FMT_THROW(FormatError("invalid format specifier for char"));
2005  typedef typename BasicWriter<Char>::CharPtr CharPtr;
2006  Char fill = internal::CharTraits<Char>::cast(spec_.fill());
2007  CharPtr out = CharPtr();
2008  const unsigned CHAR_SIZE = 1;
2009  if (spec_.width_ > CHAR_SIZE) {
2010  out = writer_.grow_buffer(spec_.width_);
2011  if (spec_.align_ == ALIGN_RIGHT) {
2012  std::uninitialized_fill_n(out, spec_.width_ - CHAR_SIZE, fill);
2013  out += spec_.width_ - CHAR_SIZE;
2014  } else if (spec_.align_ == ALIGN_CENTER) {
2015  out = writer_.fill_padding(out, spec_.width_,
2016  internal::const_check(CHAR_SIZE), fill);
2017  } else {
2018  std::uninitialized_fill_n(out + CHAR_SIZE,
2019  spec_.width_ - CHAR_SIZE, fill);
2020  }
2021  } else {
2022  out = writer_.grow_buffer(CHAR_SIZE);
2023  }
2024  *out = internal::CharTraits<Char>::cast(value);
2025  }
2026 
2027  void visit_cstring(const char *value) {
2028  if (spec_.type_ == 'p')
2029  return write_pointer(value);
2030  write(value);
2031  }
2032 
2033  // Qualification with "internal" here and below is a workaround for nvcc.
2035  writer_.write_str(value, spec_);
2036  }
2037 
2039 
2041  writer_.write_str(value, spec_);
2042  }
2043 
2044  void visit_pointer(const void *value) {
2045  if (spec_.type_ && spec_.type_ != 'p')
2046  report_unknown_type(spec_.type_, "pointer");
2047  write_pointer(value);
2048  }
2049 };
2050 
2052  private:
2055 
2056  // Returns the argument with specified index.
2057  FMT_API Arg do_get_arg(unsigned arg_index, const char *&error);
2058 
2059  protected:
2060  const ArgList &args() const { return args_; }
2061 
2062  explicit FormatterBase(const ArgList &args) {
2063  args_ = args;
2064  next_arg_index_ = 0;
2065  }
2066 
2067  // Returns the next argument.
2068  Arg next_arg(const char *&error) {
2069  if (next_arg_index_ >= 0)
2070  return do_get_arg(internal::to_unsigned(next_arg_index_++), error);
2071  error = "cannot switch from manual to automatic argument indexing";
2072  return Arg();
2073  }
2074 
2075  // Checks if manual indexing is used and returns the argument with
2076  // specified index.
2077  Arg get_arg(unsigned arg_index, const char *&error) {
2078  return check_no_auto_index(error) ? do_get_arg(arg_index, error) : Arg();
2079  }
2080 
2081  bool check_no_auto_index(const char *&error) {
2082  if (next_arg_index_ > 0) {
2083  error = "cannot switch from automatic to manual argument indexing";
2084  return false;
2085  }
2086  next_arg_index_ = -1;
2087  return true;
2088  }
2089 
2090  template <typename Char>
2091  void write(BasicWriter<Char> &w, const Char *start, const Char *end) {
2092  if (start != end)
2093  w << BasicStringRef<Char>(start, internal::to_unsigned(end - start));
2094  }
2095 };
2096 } // namespace internal
2097 
2115 template <typename Impl, typename Char, typename Spec = fmt::FormatSpec>
2116 class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char, Spec> {
2117  private:
2119  const Char *format_;
2120 
2121  public:
2131  Spec &spec, const Char *fmt)
2132  : internal::ArgFormatterBase<Impl, Char, Spec>(formatter.writer(), spec),
2133  formatter_(formatter), format_(fmt) {}
2134 
2137  c.format(&formatter_, c.value, &format_);
2138  }
2139 };
2140 
2142 template <typename Char>
2143 class ArgFormatter :
2144  public BasicArgFormatter<ArgFormatter<Char>, Char, FormatSpec> {
2145  public:
2148  FormatSpec &spec, const Char *fmt)
2150  Char, FormatSpec>(formatter, spec, fmt) {}
2151 };
2152 
2154 template <typename CharType, typename ArgFormatter>
2155 class BasicFormatter : private internal::FormatterBase {
2156  public:
2158  typedef CharType Char;
2159 
2160  private:
2163 
2165 
2167 
2168  // Checks if manual indexing is used and returns the argument with
2169  // specified name.
2170  internal::Arg get_arg(BasicStringRef<Char> arg_name, const char *&error);
2171 
2172  // Parses argument index and returns corresponding argument.
2173  internal::Arg parse_arg_index(const Char *&s);
2174 
2175  // Parses argument name and returns corresponding argument.
2176  internal::Arg parse_arg_name(const Char *&s);
2177 
2178  public:
2187  : internal::FormatterBase(args), writer_(w) {}
2188 
2190  BasicWriter<Char> &writer() { return writer_; }
2191 
2193  void format(BasicCStringRef<Char> format_str);
2194 
2195  // Formats a single argument and advances format_str, a format string pointer.
2196  const Char *format(const Char *&format_str, const internal::Arg &arg);
2197 };
2198 
2199 // Generates a comma-separated list with results of applying f to
2200 // numbers 0..n-1.
2201 # define FMT_GEN(n, f) FMT_GEN##n(f)
2202 # define FMT_GEN1(f) f(0)
2203 # define FMT_GEN2(f) FMT_GEN1(f), f(1)
2204 # define FMT_GEN3(f) FMT_GEN2(f), f(2)
2205 # define FMT_GEN4(f) FMT_GEN3(f), f(3)
2206 # define FMT_GEN5(f) FMT_GEN4(f), f(4)
2207 # define FMT_GEN6(f) FMT_GEN5(f), f(5)
2208 # define FMT_GEN7(f) FMT_GEN6(f), f(6)
2209 # define FMT_GEN8(f) FMT_GEN7(f), f(7)
2210 # define FMT_GEN9(f) FMT_GEN8(f), f(8)
2211 # define FMT_GEN10(f) FMT_GEN9(f), f(9)
2212 # define FMT_GEN11(f) FMT_GEN10(f), f(10)
2213 # define FMT_GEN12(f) FMT_GEN11(f), f(11)
2214 # define FMT_GEN13(f) FMT_GEN12(f), f(12)
2215 # define FMT_GEN14(f) FMT_GEN13(f), f(13)
2216 # define FMT_GEN15(f) FMT_GEN14(f), f(14)
2217 
2218 namespace internal {
2219 inline uint64_t make_type() { return 0; }
2220 
2221 template <typename T>
2222 inline uint64_t make_type(const T &arg) {
2224 }
2225 
2226 template <unsigned N, bool/*IsPacked*/= (N < ArgList::MAX_PACKED_ARGS)>
2227 struct ArgArray;
2228 
2229 template <unsigned N>
2230 struct ArgArray<N, true/*IsPacked*/> {
2231  typedef Value Type[N > 0 ? N : 1];
2232 
2233  template <typename Formatter, typename T>
2234  static Value make(const T &value) {
2235 #ifdef __clang__
2236  Value result = MakeValue<Formatter>(value);
2237  // Workaround a bug in Apple LLVM version 4.2 (clang-425.0.28) of clang:
2238  // https://github.com/fmtlib/fmt/issues/276
2239  (void)result.custom.format;
2240  return result;
2241 #else
2242  return MakeValue<Formatter>(value);
2243 #endif
2244  }
2245 };
2246 
2247 template <unsigned N>
2248 struct ArgArray<N, false/*IsPacked*/> {
2249  typedef Arg Type[N + 1]; // +1 for the list end Arg::NONE
2250 
2251  template <typename Formatter, typename T>
2252  static Arg make(const T &value) { return MakeArg<Formatter>(value); }
2253 };
2254 
2255 #if FMT_USE_VARIADIC_TEMPLATES
2256 template <typename Arg, typename... Args>
2257 inline uint64_t make_type(const Arg &first, const Args & ... tail) {
2258  return make_type(first) | (make_type(tail...) << 4);
2259 }
2260 
2261 #else
2262 
2263 struct ArgType {
2264  uint64_t type;
2265 
2266  ArgType() : type(0) {}
2267 
2268  template <typename T>
2269  ArgType(const T &arg) : type(make_type(arg)) {}
2270 };
2271 
2272 # define FMT_ARG_TYPE_DEFAULT(n) ArgType t##n = ArgType()
2273 
2275  return t0.type | (t1.type << 4) | (t2.type << 8) | (t3.type << 12) |
2276  (t4.type << 16) | (t5.type << 20) | (t6.type << 24) | (t7.type << 28) |
2277  (t8.type << 32) | (t9.type << 36) | (t10.type << 40) | (t11.type << 44) |
2278  (t12.type << 48) | (t13.type << 52) | (t14.type << 56);
2279 }
2280 #endif
2281 } // namespace internal
2282 
2283 # define FMT_MAKE_TEMPLATE_ARG(n) typename T##n
2284 # define FMT_MAKE_ARG_TYPE(n) T##n
2285 # define FMT_MAKE_ARG(n) const T##n &v##n
2286 # define FMT_ASSIGN_char(n) \
2287  arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter<char> >(v##n)
2288 # define FMT_ASSIGN_wchar_t(n) \
2289  arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter<wchar_t> >(v##n)
2290 
2291 #if FMT_USE_VARIADIC_TEMPLATES
2292 // Defines a variadic function returning void.
2293 # define FMT_VARIADIC_VOID(func, arg_type) \
2294  template <typename... Args> \
2295  void func(arg_type arg0, const Args & ... args) { \
2296  typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
2297  typename ArgArray::Type array{ \
2298  ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
2299  func(arg0, fmt::ArgList(fmt::internal::make_type(args...), array)); \
2300  }
2301 
2302 // Defines a variadic constructor.
2303 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
2304  template <typename... Args> \
2305  ctor(arg0_type arg0, arg1_type arg1, const Args & ... args) { \
2306  typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
2307  typename ArgArray::Type array{ \
2308  ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
2309  func(arg0, arg1, fmt::ArgList(fmt::internal::make_type(args...), array)); \
2310  }
2311 
2312 #else
2313 
2314 # define FMT_MAKE_REF(n) \
2315  fmt::internal::MakeValue< fmt::BasicFormatter<Char> >(v##n)
2316 # define FMT_MAKE_REF2(n) v##n
2317 
2318 // Defines a wrapper for a function taking one argument of type arg_type
2319 // and n additional arguments of arbitrary types.
2320 # define FMT_WRAP1(func, arg_type, n) \
2321  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2322  inline void func(arg_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
2323  const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
2324  func(arg1, fmt::ArgList( \
2325  fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
2326  }
2327 
2328 // Emulates a variadic function returning void on a pre-C++11 compiler.
2329 # define FMT_VARIADIC_VOID(func, arg_type) \
2330  inline void func(arg_type arg) { func(arg, fmt::ArgList()); } \
2331  FMT_WRAP1(func, arg_type, 1) FMT_WRAP1(func, arg_type, 2) \
2332  FMT_WRAP1(func, arg_type, 3) FMT_WRAP1(func, arg_type, 4) \
2333  FMT_WRAP1(func, arg_type, 5) FMT_WRAP1(func, arg_type, 6) \
2334  FMT_WRAP1(func, arg_type, 7) FMT_WRAP1(func, arg_type, 8) \
2335  FMT_WRAP1(func, arg_type, 9) FMT_WRAP1(func, arg_type, 10)
2336 
2337 # define FMT_CTOR(ctor, func, arg0_type, arg1_type, n) \
2338  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2339  ctor(arg0_type arg0, arg1_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
2340  const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
2341  func(arg0, arg1, fmt::ArgList( \
2342  fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
2343  }
2344 
2345 // Emulates a variadic constructor on a pre-C++11 compiler.
2346 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
2347  FMT_CTOR(ctor, func, arg0_type, arg1_type, 1) \
2348  FMT_CTOR(ctor, func, arg0_type, arg1_type, 2) \
2349  FMT_CTOR(ctor, func, arg0_type, arg1_type, 3) \
2350  FMT_CTOR(ctor, func, arg0_type, arg1_type, 4) \
2351  FMT_CTOR(ctor, func, arg0_type, arg1_type, 5) \
2352  FMT_CTOR(ctor, func, arg0_type, arg1_type, 6) \
2353  FMT_CTOR(ctor, func, arg0_type, arg1_type, 7) \
2354  FMT_CTOR(ctor, func, arg0_type, arg1_type, 8) \
2355  FMT_CTOR(ctor, func, arg0_type, arg1_type, 9) \
2356  FMT_CTOR(ctor, func, arg0_type, arg1_type, 10)
2357 #endif
2358 
2359 // Generates a comma-separated list with results of applying f to pairs
2360 // (argument, index).
2361 #define FMT_FOR_EACH1(f, x0) f(x0, 0)
2362 #define FMT_FOR_EACH2(f, x0, x1) \
2363  FMT_FOR_EACH1(f, x0), f(x1, 1)
2364 #define FMT_FOR_EACH3(f, x0, x1, x2) \
2365  FMT_FOR_EACH2(f, x0 ,x1), f(x2, 2)
2366 #define FMT_FOR_EACH4(f, x0, x1, x2, x3) \
2367  FMT_FOR_EACH3(f, x0, x1, x2), f(x3, 3)
2368 #define FMT_FOR_EACH5(f, x0, x1, x2, x3, x4) \
2369  FMT_FOR_EACH4(f, x0, x1, x2, x3), f(x4, 4)
2370 #define FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5) \
2371  FMT_FOR_EACH5(f, x0, x1, x2, x3, x4), f(x5, 5)
2372 #define FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6) \
2373  FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5), f(x6, 6)
2374 #define FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7) \
2375  FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6), f(x7, 7)
2376 #define FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8) \
2377  FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7), f(x8, 8)
2378 #define FMT_FOR_EACH10(f, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) \
2379  FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8), f(x9, 9)
2380 
2386  private:
2387  FMT_API void init(int err_code, CStringRef format_str, ArgList args);
2388 
2389  protected:
2391 
2392  typedef char Char; // For FMT_VARIADIC_CTOR.
2393 
2395 
2396  public:
2415  SystemError(int error_code, CStringRef message) {
2416  init(error_code, message, ArgList());
2417  }
2420 
2422 
2423  int error_code() const { return error_code_; }
2424 };
2425 
2442 FMT_API void format_system_error(fmt::Writer &out, int error_code,
2443  fmt::StringRef message) FMT_NOEXCEPT;
2444 
2463 template <typename Char>
2464 class BasicWriter {
2465  private:
2466  // Output buffer.
2468 
2470 
2472 
2473 #if FMT_SECURE_SCL
2474  // Returns pointer value.
2475  static Char *get(CharPtr p) { return p.base(); }
2476 #else
2477  static Char *get(Char *p) { return p; }
2478 #endif
2479 
2480  // Fills the padding around the content and returns the pointer to the
2481  // content area.
2482  static CharPtr fill_padding(CharPtr buffer,
2483  unsigned total_size, std::size_t content_size, wchar_t fill);
2484 
2485  // Grows the buffer by n characters and returns a pointer to the newly
2486  // allocated area.
2487  CharPtr grow_buffer(std::size_t n) {
2488  std::size_t size = buffer_.size();
2489  buffer_.resize(size + n);
2490  return internal::make_ptr(&buffer_[size], n);
2491  }
2492 
2493  // Writes an unsigned decimal integer.
2494  template <typename UInt>
2495  Char *write_unsigned_decimal(UInt value, unsigned prefix_size = 0) {
2496  unsigned num_digits = internal::count_digits(value);
2497  Char *ptr = get(grow_buffer(prefix_size + num_digits));
2498  internal::format_decimal(ptr + prefix_size, value, num_digits);
2499  return ptr;
2500  }
2501 
2502  // Writes a decimal integer.
2503  template <typename Int>
2504  void write_decimal(Int value) {
2505  typedef typename internal::IntTraits<Int>::MainType MainType;
2506  MainType abs_value = static_cast<MainType>(value);
2507  if (internal::is_negative(value)) {
2508  abs_value = 0 - abs_value;
2509  *write_unsigned_decimal(abs_value, 1) = '-';
2510  } else {
2511  write_unsigned_decimal(abs_value, 0);
2512  }
2513  }
2514 
2515  // Prepare a buffer for integer formatting.
2516  CharPtr prepare_int_buffer(unsigned num_digits,
2517  const EmptySpec &, const char *prefix, unsigned prefix_size) {
2518  unsigned size = prefix_size + num_digits;
2519  CharPtr p = grow_buffer(size);
2520  std::uninitialized_copy(prefix, prefix + prefix_size, p);
2521  return p + size - 1;
2522  }
2523 
2524  template <typename Spec>
2525  CharPtr prepare_int_buffer(unsigned num_digits,
2526  const Spec &spec, const char *prefix, unsigned prefix_size);
2527 
2528  // Formats an integer.
2529  template <typename T, typename Spec>
2530  void write_int(T value, Spec spec);
2531 
2532  // Formats a floating-point number (double or long double).
2533  template <typename T, typename Spec>
2534  void write_double(T value, const Spec &spec);
2535 
2536  // Writes a formatted string.
2537  template <typename StrChar>
2538  CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec);
2539 
2540  template <typename StrChar, typename Spec>
2541  void write_str(const internal::Arg::StringValue<StrChar> &str,
2542  const Spec &spec);
2543 
2544  // This following methods are private to disallow writing wide characters
2545  // and strings to a char stream. If you want to print a wide string as a
2546  // pointer as std::ostream does, cast it to const void*.
2547  // Do not implement!
2548  void operator<<(typename internal::WCharHelper<wchar_t, Char>::Unsupported);
2549  void operator<<(
2551 
2552  // Appends floating-point length specifier to the format string.
2553  // The second argument is only used for overload resolution.
2554  void append_float_length(Char *&format_ptr, long double) {
2555  *format_ptr++ = 'L';
2556  }
2557 
2558  template<typename T>
2559  void append_float_length(Char *&, T) {}
2560 
2561  template <typename Impl, typename Char_, typename Spec_>
2563 
2564  template <typename Impl, typename Char_, typename Spec_>
2566 
2567  protected:
2571  explicit BasicWriter(Buffer<Char> &b) : buffer_(b) {}
2572 
2573  public:
2579  virtual ~BasicWriter() {}
2580 
2584  std::size_t size() const { return buffer_.size(); }
2585 
2590  const Char *data() const FMT_NOEXCEPT { return &buffer_[0]; }
2591 
2596  const Char *c_str() const {
2597  std::size_t size = buffer_.size();
2598  buffer_.reserve(size + 1);
2599  buffer_[size] = '\0';
2600  return &buffer_[0];
2601  }
2602 
2608  std::basic_string<Char> str() const {
2609  return std::basic_string<Char>(&buffer_[0], buffer_.size());
2610  }
2611 
2638  BasicFormatter<Char>(args, *this).format(format);
2639  }
2641 
2642  BasicWriter &operator<<(int value) {
2643  write_decimal(value);
2644  return *this;
2645  }
2646  BasicWriter &operator<<(unsigned value) {
2647  return *this << IntFormatSpec<unsigned>(value);
2648  }
2649  BasicWriter &operator<<(long value) {
2650  write_decimal(value);
2651  return *this;
2652  }
2653  BasicWriter &operator<<(unsigned long value) {
2654  return *this << IntFormatSpec<unsigned long>(value);
2655  }
2657  write_decimal(value);
2658  return *this;
2659  }
2660 
2667  return *this << IntFormatSpec<ULongLong>(value);
2668  }
2669 
2670  BasicWriter &operator<<(double value) {
2671  write_double(value, FormatSpec());
2672  return *this;
2673  }
2674 
2681  BasicWriter &operator<<(long double value) {
2682  write_double(value, FormatSpec());
2683  return *this;
2684  }
2685 
2689  BasicWriter &operator<<(char value) {
2690  buffer_.push_back(value);
2691  return *this;
2692  }
2693 
2696  buffer_.push_back(value);
2697  return *this;
2698  }
2699 
2705  BasicWriter &operator<<(fmt::BasicStringRef<Char> value) {
2706  const Char *str = value.data();
2707  buffer_.append(str, str + value.size());
2708  return *this;
2709  }
2710 
2713  const char *str = value.data();
2714  buffer_.append(str, str + value.size());
2715  return *this;
2716  }
2717 
2718  template <typename T, typename Spec, typename FillChar>
2719  BasicWriter &operator<<(IntFormatSpec<T, Spec, FillChar> spec) {
2721  write_int(spec.value(), spec);
2722  return *this;
2723  }
2724 
2725  template <typename StrChar>
2726  BasicWriter &operator<<(const StrFormatSpec<StrChar> &spec) {
2727  const StrChar *s = spec.str();
2728  write_str(s, std::char_traits<Char>::length(s), spec);
2729  return *this;
2730  }
2731 
2732  void clear() FMT_NOEXCEPT { buffer_.clear(); }
2733 
2734  Buffer<Char> &buffer() FMT_NOEXCEPT { return buffer_; }
2735 };
2736 
2737 template <typename Char>
2738 template <typename StrChar>
2740  const StrChar *s, std::size_t size, const AlignSpec &spec) {
2741  CharPtr out = CharPtr();
2742  if (spec.width() > size) {
2743  out = grow_buffer(spec.width());
2744  Char fill = internal::CharTraits<Char>::cast(spec.fill());
2745  if (spec.align() == ALIGN_RIGHT) {
2746  std::uninitialized_fill_n(out, spec.width() - size, fill);
2747  out += spec.width() - size;
2748  } else if (spec.align() == ALIGN_CENTER) {
2749  out = fill_padding(out, spec.width(), size, fill);
2750  } else {
2751  std::uninitialized_fill_n(out + size, spec.width() - size, fill);
2752  }
2753  } else {
2754  out = grow_buffer(size);
2755  }
2756  std::uninitialized_copy(s, s + size, out);
2757  return out;
2758 }
2759 
2760 template <typename Char>
2761 template <typename StrChar, typename Spec>
2763  const internal::Arg::StringValue<StrChar> &s, const Spec &spec) {
2764  // Check if StrChar is convertible to Char.
2766  if (spec.type_ && spec.type_ != 's')
2767  internal::report_unknown_type(spec.type_, "string");
2768  const StrChar *str_value = s.value;
2769  std::size_t str_size = s.size;
2770  if (str_size == 0) {
2771  if (!str_value) {
2772  FMT_THROW(FormatError("string pointer is null"));
2773  }
2774  }
2775  std::size_t precision = static_cast<std::size_t>(spec.precision_);
2776  if (spec.precision_ >= 0 && precision < str_size)
2777  str_size = precision;
2778  write_str(str_value, str_size, spec);
2779 }
2780 
2781 template <typename Char>
2784  CharPtr buffer, unsigned total_size,
2785  std::size_t content_size, wchar_t fill) {
2786  std::size_t padding = total_size - content_size;
2787  std::size_t left_padding = padding / 2;
2788  Char fill_char = internal::CharTraits<Char>::cast(fill);
2789  std::uninitialized_fill_n(buffer, left_padding, fill_char);
2790  buffer += left_padding;
2791  CharPtr content = buffer;
2792  std::uninitialized_fill_n(buffer + content_size,
2793  padding - left_padding, fill_char);
2794  return content;
2795 }
2796 
2797 template <typename Char>
2798 template <typename Spec>
2801  unsigned num_digits, const Spec &spec,
2802  const char *prefix, unsigned prefix_size) {
2803  unsigned width = spec.width();
2804  Alignment align = spec.align();
2805  Char fill = internal::CharTraits<Char>::cast(spec.fill());
2806  if (spec.precision() > static_cast<int>(num_digits)) {
2807  // Octal prefix '0' is counted as a digit, so ignore it if precision
2808  // is specified.
2809  if (prefix_size > 0 && prefix[prefix_size - 1] == '0')
2810  --prefix_size;
2811  unsigned number_size =
2812  prefix_size + internal::to_unsigned(spec.precision());
2813  AlignSpec subspec(number_size, '0', ALIGN_NUMERIC);
2814  if (number_size >= width)
2815  return prepare_int_buffer(num_digits, subspec, prefix, prefix_size);
2816  buffer_.reserve(width);
2817  unsigned fill_size = width - number_size;
2818  if (align != ALIGN_LEFT) {
2819  CharPtr p = grow_buffer(fill_size);
2820  std::uninitialized_fill(p, p + fill_size, fill);
2821  }
2822  CharPtr result = prepare_int_buffer(
2823  num_digits, subspec, prefix, prefix_size);
2824  if (align == ALIGN_LEFT) {
2825  CharPtr p = grow_buffer(fill_size);
2826  std::uninitialized_fill(p, p + fill_size, fill);
2827  }
2828  return result;
2829  }
2830  unsigned size = prefix_size + num_digits;
2831  if (width <= size) {
2832  CharPtr p = grow_buffer(size);
2833  std::uninitialized_copy(prefix, prefix + prefix_size, p);
2834  return p + size - 1;
2835  }
2836  CharPtr p = grow_buffer(width);
2837  CharPtr end = p + width;
2838  if (align == ALIGN_LEFT) {
2839  std::uninitialized_copy(prefix, prefix + prefix_size, p);
2840  p += size;
2841  std::uninitialized_fill(p, end, fill);
2842  } else if (align == ALIGN_CENTER) {
2843  p = fill_padding(p, width, size, fill);
2844  std::uninitialized_copy(prefix, prefix + prefix_size, p);
2845  p += size;
2846  } else {
2847  if (align == ALIGN_NUMERIC) {
2848  if (prefix_size != 0) {
2849  p = std::uninitialized_copy(prefix, prefix + prefix_size, p);
2850  size -= prefix_size;
2851  }
2852  } else {
2853  std::uninitialized_copy(prefix, prefix + prefix_size, end - size);
2854  }
2855  std::uninitialized_fill(p, end - size, fill);
2856  p = end;
2857  }
2858  return p - 1;
2859 }
2860 
2861 template <typename Char>
2862 template <typename T, typename Spec>
2863 void BasicWriter<Char>::write_int(T value, Spec spec) {
2864  unsigned prefix_size = 0;
2865  typedef typename internal::IntTraits<T>::MainType UnsignedType;
2866  UnsignedType abs_value = static_cast<UnsignedType>(value);
2867  char prefix[4] = "";
2868  if (internal::is_negative(value)) {
2869  prefix[0] = '-';
2870  ++prefix_size;
2871  abs_value = 0 - abs_value;
2872  } else if (spec.flag(SIGN_FLAG)) {
2873  prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' ';
2874  ++prefix_size;
2875  }
2876  switch (spec.type()) {
2877  case 0: case 'd': {
2878  unsigned num_digits = internal::count_digits(abs_value);
2879  CharPtr p = prepare_int_buffer(num_digits, spec, prefix, prefix_size) + 1;
2880  internal::format_decimal(get(p), abs_value, 0);
2881  break;
2882  }
2883  case 'x': case 'X': {
2884  UnsignedType n = abs_value;
2885  if (spec.flag(HASH_FLAG)) {
2886  prefix[prefix_size++] = '0';
2887  prefix[prefix_size++] = spec.type_prefix();
2888  }
2889  unsigned num_digits = 0;
2890  do {
2891  ++num_digits;
2892  } while ((n >>= 4) != 0);
2893  Char *p = get(prepare_int_buffer(
2894  num_digits, spec, prefix, prefix_size));
2895  n = abs_value;
2896  const char *digits = spec.type() == 'x' ?
2897  "0123456789abcdef" : "0123456789ABCDEF";
2898  do {
2899  *p-- = digits[n & 0xf];
2900  } while ((n >>= 4) != 0);
2901  break;
2902  }
2903  case 'b': case 'B': {
2904  UnsignedType n = abs_value;
2905  if (spec.flag(HASH_FLAG)) {
2906  prefix[prefix_size++] = '0';
2907  prefix[prefix_size++] = spec.type_prefix();
2908  }
2909  unsigned num_digits = 0;
2910  do {
2911  ++num_digits;
2912  } while ((n >>= 1) != 0);
2913  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
2914  n = abs_value;
2915  do {
2916  *p-- = static_cast<Char>('0' + (n & 1));
2917  } while ((n >>= 1) != 0);
2918  break;
2919  }
2920  case 'o': {
2921  UnsignedType n = abs_value;
2922  if (spec.flag(HASH_FLAG))
2923  prefix[prefix_size++] = '0';
2924  unsigned num_digits = 0;
2925  do {
2926  ++num_digits;
2927  } while ((n >>= 3) != 0);
2928  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
2929  n = abs_value;
2930  do {
2931  *p-- = static_cast<Char>('0' + (n & 7));
2932  } while ((n >>= 3) != 0);
2933  break;
2934  }
2935  case 'n': {
2936  unsigned num_digits = internal::count_digits(abs_value);
2937  fmt::StringRef sep = "";
2938 #if !(defined(ANDROID) || defined(__ANDROID__))
2939  sep = internal::thousands_sep(std::localeconv());
2940 #endif
2941  unsigned size = static_cast<unsigned>(
2942  num_digits + sep.size() * ((num_digits - 1) / 3));
2943  CharPtr p = prepare_int_buffer(size, spec, prefix, prefix_size) + 1;
2944  internal::format_decimal(get(p), abs_value, 0, internal::ThousandsSep(sep));
2945  break;
2946  }
2947  default:
2949  spec.type(), spec.flag(CHAR_FLAG) ? "char" : "integer");
2950  break;
2951  }
2952 }
2953 
2954 template <typename Char>
2955 template <typename T, typename Spec>
2956 void BasicWriter<Char>::write_double(T value, const Spec &spec) {
2957  // Check type.
2958  char type = spec.type();
2959  bool upper = false;
2960  switch (type) {
2961  case 0:
2962  type = 'g';
2963  break;
2964  case 'e': case 'f': case 'g': case 'a':
2965  break;
2966  case 'F':
2967 #if FMT_MSC_VER
2968  // MSVC's printf doesn't support 'F'.
2969  type = 'f';
2970 #endif
2971  // Fall through.
2972  case 'E': case 'G': case 'A':
2973  upper = true;
2974  break;
2975  default:
2977  break;
2978  }
2979 
2980  char sign = 0;
2981  // Use isnegative instead of value < 0 because the latter is always
2982  // false for NaN.
2983  if (internal::FPUtil::isnegative(static_cast<double>(value))) {
2984  sign = '-';
2985  value = -value;
2986  } else if (spec.flag(SIGN_FLAG)) {
2987  sign = spec.flag(PLUS_FLAG) ? '+' : ' ';
2988  }
2989 
2990  if (internal::FPUtil::isnotanumber(value)) {
2991  // Format NaN ourselves because sprintf's output is not consistent
2992  // across platforms.
2993  std::size_t nan_size = 4;
2994  const char *nan = upper ? " NAN" : " nan";
2995  if (!sign) {
2996  --nan_size;
2997  ++nan;
2998  }
2999  CharPtr out = write_str(nan, nan_size, spec);
3000  if (sign)
3001  *out = sign;
3002  return;
3003  }
3004 
3005  if (internal::FPUtil::isinfinity(value)) {
3006  // Format infinity ourselves because sprintf's output is not consistent
3007  // across platforms.
3008  std::size_t inf_size = 4;
3009  const char *inf = upper ? " INF" : " inf";
3010  if (!sign) {
3011  --inf_size;
3012  ++inf;
3013  }
3014  CharPtr out = write_str(inf, inf_size, spec);
3015  if (sign)
3016  *out = sign;
3017  return;
3018  }
3019 
3020  std::size_t offset = buffer_.size();
3021  unsigned width = spec.width();
3022  if (sign) {
3023  buffer_.reserve(buffer_.size() + (width > 1u ? width : 1u));
3024  if (width > 0)
3025  --width;
3026  ++offset;
3027  }
3028 
3029  // Build format string.
3030  enum { MAX_FORMAT_SIZE = 10}; // longest format: %#-*.*Lg
3031  Char format[MAX_FORMAT_SIZE];
3032  Char *format_ptr = format;
3033  *format_ptr++ = '%';
3034  unsigned width_for_sprintf = width;
3035  if (spec.flag(HASH_FLAG))
3036  *format_ptr++ = '#';
3037  if (spec.align() == ALIGN_CENTER) {
3038  width_for_sprintf = 0;
3039  } else {
3040  if (spec.align() == ALIGN_LEFT)
3041  *format_ptr++ = '-';
3042  if (width != 0)
3043  *format_ptr++ = '*';
3044  }
3045  if (spec.precision() >= 0) {
3046  *format_ptr++ = '.';
3047  *format_ptr++ = '*';
3048  }
3049 
3050  append_float_length(format_ptr, value);
3051  *format_ptr++ = type;
3052  *format_ptr = '\0';
3053 
3054  // Format using snprintf.
3055  Char fill = internal::CharTraits<Char>::cast(spec.fill());
3056  unsigned n = 0;
3057  Char *start = FMT_NULL;
3058  for (;;) {
3059  std::size_t buffer_size = buffer_.capacity() - offset;
3060 #if FMT_MSC_VER
3061  // MSVC's vsnprintf_s doesn't work with zero size, so reserve
3062  // space for at least one extra character to make the size non-zero.
3063  // Note that the buffer's capacity will increase by more than 1.
3064  if (buffer_size == 0) {
3065  buffer_.reserve(offset + 1);
3066  buffer_size = buffer_.capacity() - offset;
3067  }
3068 #endif
3069  start = &buffer_[offset];
3071  start, buffer_size, format, width_for_sprintf, spec.precision(), value);
3072  if (result >= 0) {
3073  n = internal::to_unsigned(result);
3074  if (offset + n < buffer_.capacity())
3075  break; // The buffer is large enough - continue with formatting.
3076  buffer_.reserve(offset + n + 1);
3077  } else {
3078  // If result is negative we ask to increase the capacity by at least 1,
3079  // but as std::vector, the buffer grows exponentially.
3080  buffer_.reserve(buffer_.capacity() + 1);
3081  }
3082  }
3083  if (sign) {
3084  if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) ||
3085  *start != ' ') {
3086  *(start - 1) = sign;
3087  sign = 0;
3088  } else {
3089  *(start - 1) = fill;
3090  }
3091  ++n;
3092  }
3093  if (spec.align() == ALIGN_CENTER && spec.width() > n) {
3094  width = spec.width();
3095  CharPtr p = grow_buffer(width);
3096  std::memmove(get(p) + (width - n) / 2, get(p), n * sizeof(Char));
3097  fill_padding(p, spec.width(), n, fill);
3098  return;
3099  }
3100  if (spec.fill() != ' ' || sign) {
3101  while (*start == ' ')
3102  *start++ = fill;
3103  if (sign)
3104  *(start - 1) = sign;
3105  }
3106  grow_buffer(n);
3107 }
3108 
3143 template <typename Char, typename Allocator = std::allocator<Char> >
3144 class BasicMemoryWriter : public BasicWriter<Char> {
3145  private:
3147 
3148  public:
3149  explicit BasicMemoryWriter(const Allocator& alloc = Allocator())
3150  : BasicWriter<Char>(buffer_), buffer_(alloc) {}
3151 
3152 #if FMT_USE_RVALUE_REFERENCES
3153 
3160  : BasicWriter<Char>(buffer_), buffer_(std::move(other.buffer_)) {
3161  }
3162 
3168  BasicMemoryWriter &operator=(BasicMemoryWriter &&other) {
3169  buffer_ = std::move(other.buffer_);
3170  return *this;
3171  }
3172 #endif
3173 };
3174 
3177 
3198 template <typename Char>
3199 class BasicArrayWriter : public BasicWriter<Char> {
3200  private:
3202 
3203  public:
3210  BasicArrayWriter(Char *array, std::size_t size)
3211  : BasicWriter<Char>(buffer_), buffer_(array, size) {}
3212 
3219  template <std::size_t SIZE>
3220  explicit BasicArrayWriter(Char (&array)[SIZE])
3221  : BasicWriter<Char>(buffer_), buffer_(array, SIZE) {}
3222 };
3223 
3226 
3227 // Reports a system error without throwing an exception.
3228 // Can be used to report errors from destructors.
3229 FMT_API void report_system_error(int error_code,
3230  StringRef message) FMT_NOEXCEPT;
3231 
3232 #if FMT_USE_WINDOWS_H
3233 
3235 class WindowsError : public SystemError {
3236  private:
3237  FMT_API void init(int error_code, CStringRef format_str, ArgList args);
3238 
3239  public:
3268  WindowsError(int error_code, CStringRef message) {
3269  init(error_code, message, ArgList());
3270  }
3271  FMT_VARIADIC_CTOR(WindowsError, init, int, CStringRef)
3272 };
3273 
3274 // Reports a Windows error without throwing an exception.
3275 // Can be used to report errors from destructors.
3276 FMT_API void report_windows_error(int error_code,
3277  StringRef message) FMT_NOEXCEPT;
3278 
3279 #endif
3280 
3282 
3289 FMT_API void print_colored(Color c, CStringRef format, ArgList args);
3290 
3300 inline std::string format(CStringRef format_str, ArgList args) {
3301  MemoryWriter w;
3302  w.write(format_str, args);
3303  return w.str();
3304 }
3305 
3306 inline std::wstring format(WCStringRef format_str, ArgList args) {
3307  WMemoryWriter w;
3308  w.write(format_str, args);
3309  return w.str();
3310 }
3311 
3321 FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args);
3322 
3332 FMT_API void print(CStringRef format_str, ArgList args);
3333 
3337 class FormatInt {
3338  private:
3339  // Buffer should be large enough to hold all digits (digits10 + 1),
3340  // a sign and a null character.
3341  enum {BUFFER_SIZE = std::numeric_limits<ULongLong>::digits10 + 3};
3342  mutable char buffer_[BUFFER_SIZE];
3343  char *str_;
3344 
3345  // Formats value in reverse and returns the number of digits.
3346  char *format_decimal(ULongLong value) {
3347  char *buffer_end = buffer_ + BUFFER_SIZE - 1;
3348  while (value >= 100) {
3349  // Integer division is slow so do it for a group of two digits instead
3350  // of for every digit. The idea comes from the talk by Alexandrescu
3351  // "Three Optimization Tips for C++". See speed-test for a comparison.
3352  unsigned index = static_cast<unsigned>((value % 100) * 2);
3353  value /= 100;
3354  *--buffer_end = internal::Data::DIGITS[index + 1];
3355  *--buffer_end = internal::Data::DIGITS[index];
3356  }
3357  if (value < 10) {
3358  *--buffer_end = static_cast<char>('0' + value);
3359  return buffer_end;
3360  }
3361  unsigned index = static_cast<unsigned>(value * 2);
3362  *--buffer_end = internal::Data::DIGITS[index + 1];
3363  *--buffer_end = internal::Data::DIGITS[index];
3364  return buffer_end;
3365  }
3366 
3367  void FormatSigned(LongLong value) {
3368  ULongLong abs_value = static_cast<ULongLong>(value);
3369  bool negative = value < 0;
3370  if (negative)
3371  abs_value = 0 - abs_value;
3372  str_ = format_decimal(abs_value);
3373  if (negative)
3374  *--str_ = '-';
3375  }
3376 
3377  public:
3378  explicit FormatInt(int value) { FormatSigned(value); }
3379  explicit FormatInt(long value) { FormatSigned(value); }
3380  explicit FormatInt(LongLong value) { FormatSigned(value); }
3381  explicit FormatInt(unsigned value) : str_(format_decimal(value)) {}
3382  explicit FormatInt(unsigned long value) : str_(format_decimal(value)) {}
3383  explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {}
3384 
3386  std::size_t size() const {
3387  return internal::to_unsigned(buffer_ - str_ + BUFFER_SIZE - 1);
3388  }
3389 
3394  const char *data() const { return str_; }
3395 
3400  const char *c_str() const {
3401  buffer_[BUFFER_SIZE - 1] = '\0';
3402  return str_;
3403  }
3404 
3410  std::string str() const { return std::string(str_, size()); }
3411 };
3412 
3413 // Formats a decimal integer value writing into buffer and returns
3414 // a pointer to the end of the formatted string. This function doesn't
3415 // write a terminating null character.
3416 template <typename T>
3417 inline void format_decimal(char *&buffer, T value) {
3418  typedef typename internal::IntTraits<T>::MainType MainType;
3419  MainType abs_value = static_cast<MainType>(value);
3420  if (internal::is_negative(value)) {
3421  *buffer++ = '-';
3422  abs_value = 0 - abs_value;
3423  }
3424  if (abs_value < 100) {
3425  if (abs_value < 10) {
3426  *buffer++ = static_cast<char>('0' + abs_value);
3427  return;
3428  }
3429  unsigned index = static_cast<unsigned>(abs_value * 2);
3430  *buffer++ = internal::Data::DIGITS[index];
3431  *buffer++ = internal::Data::DIGITS[index + 1];
3432  return;
3433  }
3434  unsigned num_digits = internal::count_digits(abs_value);
3435  internal::format_decimal(buffer, abs_value, num_digits);
3436  buffer += num_digits;
3437 }
3438 
3449 template <typename T>
3452 }
3453 
3454 template <typename T>
3457 }
3458 
3459 // The following two functions are deleted intentionally to disable
3460 // nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``.
3461 template <typename Char>
3462 void arg(StringRef, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
3463 template <typename Char>
3464 void arg(WStringRef, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
3465 }
3466 
3467 #if FMT_GCC_VERSION
3468 // Use the system_header pragma to suppress warnings about variadic macros
3469 // because suppressing -Wvariadic-macros with the diagnostic pragma doesn't
3470 // work. It is used at the end because we want to suppress as little warnings
3471 // as possible.
3472 # pragma GCC system_header
3473 #endif
3474 
3475 // This is used to work around VC++ bugs in handling variadic macros.
3476 #define FMT_EXPAND(args) args
3477 
3478 // Returns the number of arguments.
3479 // Based on https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s.
3480 #define FMT_NARG(...) FMT_NARG_(__VA_ARGS__, FMT_RSEQ_N())
3481 #define FMT_NARG_(...) FMT_EXPAND(FMT_ARG_N(__VA_ARGS__))
3482 #define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
3483 #define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
3484 
3485 #define FMT_FOR_EACH_(N, f, ...) \
3486  FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__))
3487 #define FMT_FOR_EACH(f, ...) \
3488  FMT_EXPAND(FMT_FOR_EACH_(FMT_NARG(__VA_ARGS__), f, __VA_ARGS__))
3489 
3490 #define FMT_ADD_ARG_NAME(type, index) type arg##index
3491 #define FMT_GET_ARG_NAME(type, index) arg##index
3492 
3493 #if FMT_USE_VARIADIC_TEMPLATES
3494 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
3495  template <typename... Args> \
3496  ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
3497  const Args & ... args) { \
3498  typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
3499  typename ArgArray::Type array{ \
3500  ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
3501  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), \
3502  fmt::ArgList(fmt::internal::make_type(args...), array)); \
3503  }
3504 #else
3505 // Defines a wrapper for a function taking __VA_ARGS__ arguments
3506 // and n additional arguments of arbitrary types.
3507 # define FMT_WRAP(Char, ReturnType, func, call, n, ...) \
3508  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
3509  inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
3510  FMT_GEN(n, FMT_MAKE_ARG)) { \
3511  fmt::internal::ArgArray<n>::Type arr; \
3512  FMT_GEN(n, FMT_ASSIGN_##Char); \
3513  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList( \
3514  fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), arr)); \
3515  }
3516 
3517 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
3518  inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) { \
3519  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList()); \
3520  } \
3521  FMT_WRAP(Char, ReturnType, func, call, 1, __VA_ARGS__) \
3522  FMT_WRAP(Char, ReturnType, func, call, 2, __VA_ARGS__) \
3523  FMT_WRAP(Char, ReturnType, func, call, 3, __VA_ARGS__) \
3524  FMT_WRAP(Char, ReturnType, func, call, 4, __VA_ARGS__) \
3525  FMT_WRAP(Char, ReturnType, func, call, 5, __VA_ARGS__) \
3526  FMT_WRAP(Char, ReturnType, func, call, 6, __VA_ARGS__) \
3527  FMT_WRAP(Char, ReturnType, func, call, 7, __VA_ARGS__) \
3528  FMT_WRAP(Char, ReturnType, func, call, 8, __VA_ARGS__) \
3529  FMT_WRAP(Char, ReturnType, func, call, 9, __VA_ARGS__) \
3530  FMT_WRAP(Char, ReturnType, func, call, 10, __VA_ARGS__) \
3531  FMT_WRAP(Char, ReturnType, func, call, 11, __VA_ARGS__) \
3532  FMT_WRAP(Char, ReturnType, func, call, 12, __VA_ARGS__) \
3533  FMT_WRAP(Char, ReturnType, func, call, 13, __VA_ARGS__) \
3534  FMT_WRAP(Char, ReturnType, func, call, 14, __VA_ARGS__) \
3535  FMT_WRAP(Char, ReturnType, func, call, 15, __VA_ARGS__)
3536 #endif // FMT_USE_VARIADIC_TEMPLATES
3537 
3565 #define FMT_VARIADIC(ReturnType, func, ...) \
3566  FMT_VARIADIC_(char, ReturnType, func, return func, __VA_ARGS__)
3567 
3568 #define FMT_VARIADIC_W(ReturnType, func, ...) \
3569  FMT_VARIADIC_(wchar_t, ReturnType, func, return func, __VA_ARGS__)
3570 
3571 #define FMT_CAPTURE_ARG_(id, index) ::fmt::arg(#id, id)
3572 
3573 #define FMT_CAPTURE_ARG_W_(id, index) ::fmt::arg(L###id, id)
3574 
3589 #define FMT_CAPTURE(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_, __VA_ARGS__)
3590 
3591 #define FMT_CAPTURE_W(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_W_, __VA_ARGS__)
3592 
3593 namespace fmt {
3594 FMT_VARIADIC(std::string, format, CStringRef)
3595 FMT_VARIADIC_W(std::wstring, format, WCStringRef)
3597 FMT_VARIADIC(void, print, std::FILE *, CStringRef)
3599 
3600 namespace internal {
3601 template <typename Char>
3602 inline bool is_name_start(Char c) {
3603  return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || '_' == c;
3604 }
3605 
3606 // Parses an unsigned integer advancing s to the end of the parsed input.
3607 // This function assumes that the first character of s is a digit.
3608 template <typename Char>
3609 unsigned parse_nonnegative_int(const Char *&s) {
3610  assert('0' <= *s && *s <= '9');
3611  unsigned value = 0;
3612  do {
3613  unsigned new_value = value * 10 + (*s++ - '0');
3614  // Check if value wrapped around.
3615  if (new_value < value) {
3616  value = (std::numeric_limits<unsigned>::max)();
3617  break;
3618  }
3619  value = new_value;
3620  } while ('0' <= *s && *s <= '9');
3621  // Convert to unsigned to prevent a warning.
3622  unsigned max_int = (std::numeric_limits<int>::max)();
3623  if (value > max_int)
3624  FMT_THROW(FormatError("number is too big"));
3625  return value;
3626 }
3627 
3628 inline void require_numeric_argument(const Arg &arg, char spec) {
3629  if (arg.type > Arg::LAST_NUMERIC_TYPE) {
3630  std::string message =
3631  fmt::format("format specifier '{}' requires numeric argument", spec);
3632  FMT_THROW(fmt::FormatError(message));
3633  }
3634 }
3635 
3636 template <typename Char>
3637 void check_sign(const Char *&s, const Arg &arg) {
3638  char sign = static_cast<char>(*s);
3640  if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) {
3642  "format specifier '{}' requires signed argument", sign)));
3643  }
3644  ++s;
3645 }
3646 } // namespace internal
3647 
3648 template <typename Char, typename AF>
3650  BasicStringRef<Char> arg_name, const char *&error) {
3651  if (check_no_auto_index(error)) {
3652  map_.init(args());
3653  const internal::Arg *arg = map_.find(arg_name);
3654  if (arg)
3655  return *arg;
3656  error = "argument not found";
3657  }
3658  return internal::Arg();
3659 }
3660 
3661 template <typename Char, typename AF>
3663  const char *error = FMT_NULL;
3664  internal::Arg arg = *s < '0' || *s > '9' ?
3665  next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error);
3666  if (error) {
3668  *s != '}' && *s != ':' ? "invalid format string" : error));
3669  }
3670  return arg;
3671 }
3672 
3673 template <typename Char, typename AF>
3675  assert(internal::is_name_start(*s));
3676  const Char *start = s;
3677  Char c;
3678  do {
3679  c = *++s;
3680  } while (internal::is_name_start(c) || ('0' <= c && c <= '9'));
3681  const char *error = FMT_NULL;
3682  internal::Arg arg = get_arg(BasicStringRef<Char>(start, s - start), error);
3683  if (error)
3684  FMT_THROW(FormatError(error));
3685  return arg;
3686 }
3687 
3688 template <typename Char, typename ArgFormatter>
3690  const Char *&format_str, const internal::Arg &arg) {
3691  using internal::Arg;
3692  const Char *s = format_str;
3693  typename ArgFormatter::SpecType spec;
3694  if (*s == ':') {
3695  if (arg.type == Arg::CUSTOM) {
3696  arg.custom.format(this, arg.custom.value, &s);
3697  return s;
3698  }
3699  ++s;
3700  // Parse fill and alignment.
3701  if (Char c = *s) {
3702  const Char *p = s + 1;
3703  spec.align_ = ALIGN_DEFAULT;
3704  do {
3705  switch (*p) {
3706  case '<':
3707  spec.align_ = ALIGN_LEFT;
3708  break;
3709  case '>':
3710  spec.align_ = ALIGN_RIGHT;
3711  break;
3712  case '=':
3713  spec.align_ = ALIGN_NUMERIC;
3714  break;
3715  case '^':
3716  spec.align_ = ALIGN_CENTER;
3717  break;
3718  }
3719  if (spec.align_ != ALIGN_DEFAULT) {
3720  if (p != s) {
3721  if (c == '}') break;
3722  if (c == '{')
3723  FMT_THROW(FormatError("invalid fill character '{'"));
3724  s += 2;
3725  spec.fill_ = c;
3726  } else ++s;
3727  if (spec.align_ == ALIGN_NUMERIC)
3729  break;
3730  }
3731  } while (--p >= s);
3732  }
3733 
3734  // Parse sign.
3735  switch (*s) {
3736  case '+':
3737  check_sign(s, arg);
3738  spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
3739  break;
3740  case '-':
3741  check_sign(s, arg);
3742  spec.flags_ |= MINUS_FLAG;
3743  break;
3744  case ' ':
3745  check_sign(s, arg);
3746  spec.flags_ |= SIGN_FLAG;
3747  break;
3748  }
3749 
3750  if (*s == '#') {
3752  spec.flags_ |= HASH_FLAG;
3753  ++s;
3754  }
3755 
3756  // Parse zero flag.
3757  if (*s == '0') {
3759  spec.align_ = ALIGN_NUMERIC;
3760  spec.fill_ = '0';
3761  ++s;
3762  }
3763 
3764  // Parse width.
3765  if ('0' <= *s && *s <= '9') {
3767  } else if (*s == '{') {
3768  ++s;
3769  Arg width_arg = internal::is_name_start(*s) ?
3770  parse_arg_name(s) : parse_arg_index(s);
3771  if (*s++ != '}')
3772  FMT_THROW(FormatError("invalid format string"));
3773  ULongLong value = 0;
3774  switch (width_arg.type) {
3775  case Arg::INT:
3776  if (width_arg.int_value < 0)
3777  FMT_THROW(FormatError("negative width"));
3778  value = width_arg.int_value;
3779  break;
3780  case Arg::UINT:
3781  value = width_arg.uint_value;
3782  break;
3783  case Arg::LONG_LONG:
3784  if (width_arg.long_long_value < 0)
3785  FMT_THROW(FormatError("negative width"));
3786  value = width_arg.long_long_value;
3787  break;
3788  case Arg::ULONG_LONG:
3789  value = width_arg.ulong_long_value;
3790  break;
3791  default:
3792  FMT_THROW(FormatError("width is not integer"));
3793  }
3794  if (value > (std::numeric_limits<int>::max)())
3795  FMT_THROW(FormatError("number is too big"));
3796  spec.width_ = static_cast<int>(value);
3797  }
3798 
3799  // Parse precision.
3800  if (*s == '.') {
3801  ++s;
3802  spec.precision_ = 0;
3803  if ('0' <= *s && *s <= '9') {
3805  } else if (*s == '{') {
3806  ++s;
3807  Arg precision_arg = internal::is_name_start(*s) ?
3808  parse_arg_name(s) : parse_arg_index(s);
3809  if (*s++ != '}')
3810  FMT_THROW(FormatError("invalid format string"));
3811  ULongLong value = 0;
3812  switch (precision_arg.type) {
3813  case Arg::INT:
3814  if (precision_arg.int_value < 0)
3815  FMT_THROW(FormatError("negative precision"));
3816  value = precision_arg.int_value;
3817  break;
3818  case Arg::UINT:
3819  value = precision_arg.uint_value;
3820  break;
3821  case Arg::LONG_LONG:
3822  if (precision_arg.long_long_value < 0)
3823  FMT_THROW(FormatError("negative precision"));
3824  value = precision_arg.long_long_value;
3825  break;
3826  case Arg::ULONG_LONG:
3827  value = precision_arg.ulong_long_value;
3828  break;
3829  default:
3830  FMT_THROW(FormatError("precision is not integer"));
3831  }
3832  if (value > (std::numeric_limits<int>::max)())
3833  FMT_THROW(FormatError("number is too big"));
3834  spec.precision_ = static_cast<int>(value);
3835  } else {
3836  FMT_THROW(FormatError("missing precision specifier"));
3837  }
3838  if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) {
3840  fmt::format("precision not allowed in {} format specifier",
3841  arg.type == Arg::POINTER ? "pointer" : "integer")));
3842  }
3843  }
3844 
3845  // Parse type.
3846  if (*s != '}' && *s)
3847  spec.type_ = static_cast<char>(*s++);
3848  }
3849 
3850  if (*s++ != '}')
3851  FMT_THROW(FormatError("missing '}' in format string"));
3852 
3853  // Format argument.
3854  ArgFormatter(*this, spec, s - 1).visit(arg);
3855  return s;
3856 }
3857 
3858 template <typename Char, typename AF>
3860  const Char *s = format_str.c_str();
3861  const Char *start = s;
3862  while (*s) {
3863  Char c = *s++;
3864  if (c != '{' && c != '}') continue;
3865  if (*s == c) {
3866  write(writer_, start, s);
3867  start = ++s;
3868  continue;
3869  }
3870  if (c == '}')
3871  FMT_THROW(FormatError("unmatched '}' in format string"));
3872  write(writer_, start, s - 1);
3874  parse_arg_name(s) : parse_arg_index(s);
3875  start = s = format(s, arg);
3876  }
3877  write(writer_, start, s);
3878 }
3879 
3880 template <typename Char, typename It>
3881 struct ArgJoin {
3882  It first;
3883  It last;
3885 
3886  ArgJoin(It first, It last, const BasicCStringRef<Char>& sep) :
3887  first(first),
3888  last(last),
3889  sep(sep) {}
3890 };
3891 
3892 template <typename It>
3893 ArgJoin<char, It> join(It first, It last, const BasicCStringRef<char>& sep) {
3894  return ArgJoin<char, It>(first, last, sep);
3895 }
3896 
3897 template <typename It>
3898 ArgJoin<wchar_t, It> join(It first, It last, const BasicCStringRef<wchar_t>& sep) {
3899  return ArgJoin<wchar_t, It>(first, last, sep);
3900 }
3901 
3902 #if FMT_HAS_GXX_CXX11
3903 template <typename Range>
3904 auto join(const Range& range, const BasicCStringRef<char>& sep)
3905  -> ArgJoin<char, decltype(std::begin(range))> {
3906  return join(std::begin(range), std::end(range), sep);
3907 }
3908 
3909 template <typename Range>
3910 auto join(const Range& range, const BasicCStringRef<wchar_t>& sep)
3911  -> ArgJoin<wchar_t, decltype(std::begin(range))> {
3912  return join(std::begin(range), std::end(range), sep);
3913 }
3914 #endif
3915 
3916 template <typename ArgFormatter, typename Char, typename It>
3918  const Char *&format_str, const ArgJoin<Char, It>& e) {
3919  const Char* end = format_str;
3920  if (*end == ':')
3921  ++end;
3922  while (*end && *end != '}')
3923  ++end;
3924  if (*end != '}')
3925  FMT_THROW(FormatError("missing '}' in format string"));
3926 
3927  It it = e.first;
3928  if (it != e.last) {
3929  const Char* save = format_str;
3931  while (it != e.last) {
3932  f.writer().write(e.sep);
3933  format_str = save;
3935  }
3936  }
3937  format_str = end + 1;
3938 }
3939 } // namespace fmt
3940 
3941 #if FMT_USE_USER_DEFINED_LITERALS
3942 namespace fmt {
3943 namespace internal {
3944 
3945 template <typename Char>
3946 struct UdlFormat {
3947  const Char *str;
3948 
3949  template <typename... Args>
3950  auto operator()(Args && ... args) const
3951  -> decltype(format(str, std::forward<Args>(args)...)) {
3952  return format(str, std::forward<Args>(args)...);
3953  }
3954 };
3955 
3956 template <typename Char>
3957 struct UdlArg {
3958  const Char *str;
3959 
3960  template <typename T>
3961  NamedArgWithType<Char, T> operator=(T &&value) const {
3962  return {str, std::forward<T>(value)};
3963  }
3964 };
3965 
3966 } // namespace internal
3967 
3968 inline namespace literals {
3969 
3980 inline internal::UdlFormat<char>
3981 operator"" _format(const char *s, std::size_t) { return {s}; }
3982 inline internal::UdlFormat<wchar_t>
3983 operator"" _format(const wchar_t *s, std::size_t) { return {s}; }
3984 
3995 inline internal::UdlArg<char>
3996 operator"" _a(const char *s, std::size_t) { return {s}; }
3997 inline internal::UdlArg<wchar_t>
3998 operator"" _a(const wchar_t *s, std::size_t) { return {s}; }
3999 
4000 } // inline namespace literals
4001 } // namespace fmt
4002 #endif // FMT_USE_USER_DEFINED_LITERALS
4003 
4004 // Restore warnings.
4005 #if FMT_GCC_VERSION >= 406
4006 # pragma GCC diagnostic pop
4007 #endif
4008 
4009 #if defined(__clang__) && !defined(FMT_ICC_VERSION)
4010 # pragma clang diagnostic pop
4011 #endif
4012 
4013 #ifdef FMT_HEADER_ONLY
4014 # define FMT_FUNC inline
4015 # include "format.cc"
4016 #else
4017 # define FMT_FUNC
4018 #endif
4019 
4020 #endif // FMT_FORMAT_H_
CharPtr prepare_int_buffer(unsigned num_digits, const EmptySpec &, const char *prefix, unsigned prefix_size)
Definition: format.h:2516
BasicCStringRef(const Char *s)
Definition: format.h:594
double double_value
Definition: format.h:1106
Definition: format.h:2051
std::basic_string< Char > str() const
Definition: format.h:2608
Result visit_bool(bool value)
Definition: format.h:1598
AlignTypeSpec(unsigned width, wchar_t fill)
Definition: format.h:1752
uint64_t Type
Definition: format.h:915
bool flag(unsigned f) const
Definition: format.h:1769
#define FMT_GCC_EXTENSION
Definition: format.h:100
#define FMT_MAKE_VALUE_(Type, field, TYPE, rhs)
Definition: format.h:1316
Definition: format.h:1096
FormatInt(unsigned long value)
Definition: format.h:3382
Alignment align() const
Definition: format.h:1744
std::size_t size() const
Definition: format.h:3386
const char * c_str() const
Definition: format.h:3400
MakeValue(typename WCharHelper< wchar_t, Char >::Supported value)
Definition: format.h:1362
Definition: format.h:626
const uint32_t T[512]
Definition: groestl_tables.h:34
static const uint8_t padding[]
Definition: blake256.c:77
BasicStringRef(const Char *s)
Definition: format.h:502
MapType::value_type Pair
Definition: format.h:1930
StrFormatSpec(const Char *str, unsigned width, FillChar fill)
Definition: format.h:1796
Definition: format.h:1706
Definition: format.h:1088
FMT_FUNC void print_colored(Color c, CStringRef format, ArgList args)
Definition: format.cc:496
Buffer< Char > & buffer_
Definition: format.h:2467
#define FMT_NOEXCEPT
Definition: format.h:192
static internal::Arg::Type type(uint64_t types, unsigned index)
Definition: format.h:1530
void visit_string(internal::Arg::StringValue< char > value)
Definition: format.h:2034
Definition: format.h:2116
ArgType(const T &arg)
Definition: format.h:2269
bool is_negative(T value)
Definition: format.h:906
#define FMT_ASSERT(condition, message)
Definition: format.h:295
Alignment align_
Definition: format.h:1739
Definition: format.h:1700
void operator()(Char *)
Definition: format.h:980
static void format_custom_arg(void *formatter, const void *arg, void *format_str_ptr)
Definition: format.h:1306
std::size_t size_
Definition: format.h:674
void save(Archive &a, const std::unordered_map< h_key, hval > &x, const boost::serialization::version_type ver)
Definition: unordered_containers_boost_serialization.h:43
T data_[SIZE]
Definition: format.h:750
Definition: format.h:360
static bool is_negative(T value)
Definition: format.h:894
#define FMT_VARIADIC(ReturnType, func,...)
Definition: format.h:3565
void resize(std::size_t new_size)
Definition: format.h:700
void format_arg(fmt::BasicFormatter< Char, ArgFormatter > &f, const Char *&format_str, const ArgJoin< Char, It > &e)
Definition: format.h:3917
ArgList args_
Definition: format.h:2053
friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:542
const void * value
Definition: format.h:1097
friend bool operator<=(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:548
Definition: format.h:1705
char Yes[1]
Definition: format.h:1154
CharType Char
Definition: format.h:2158
Result visit_ulong_long(ULongLong value)
Definition: format.h:1593
char type() const
Definition: format.h:1719
wchar_t fill_
Definition: format.h:1729
IntFormatSpec< int, TypeSpec< 'x'> > hex(int value)
Definition: format.h:1086
BasicWriter(Buffer< Char > &b)
Definition: format.h:2571
Result visit_pointer(const void *)
Definition: format.h:1645
Definition: format.h:3281
#define FMT_MAKE_WSTR_VALUE(Type, TYPE)
Definition: format.h:1382
StrFormatSpec< wchar_t > pad(const wchar_t *str, unsigned width, char fill=' ')
Definition: format.h:1918
T Type
Definition: format.h:626
Arg next_arg(const char *&error)
Definition: format.h:2068
#define FMT_MAKE_VALUE(Type, field, TYPE)
Definition: format.h:1320
static unsigned char negative(signed char b)
Definition: crypto-ops.c:1504
IntFormatSpec< int, TypeSpec< 'o'> > oct(int value)
Definition: format.h:3337
WidthSpec(unsigned width, wchar_t fill)
Definition: format.h:1731
#define F(w, k)
Definition: sha512-blocks.c:61
Spec & spec_
Definition: format.h:1952
Definition: format.h:918
T * make_ptr(T *ptr, std::size_t)
Definition: format.h:658
FMT_API void report_unknown_type(char code, const char *type)
Definition: format.cc:294
friend bool operator<(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:545
Definition: format.h:1128
MakeValue(const NamedArgWithType< Char_, T > &value)
Definition: format.h:1420
Definition: format.h:824
int next_arg_index_
Definition: format.h:2054
internal::FixedBuffer< Char > buffer_
Definition: format.h:3201
BasicFormatter< Char, Impl > & formatter_
Definition: format.h:2118
MakeValue(const T &value, typename EnableIf< Not< ConvertToInt< T >::value >::value, int >::type=0)
Definition: format.h:1397
Result visit_any_double(T)
Definition: format.h:1625
FMT_FUNC void write(std::ostream &os, Writer &w)
Definition: ostream.cc:15
void operator()(Char *&buffer)
Definition: format.h:995
LConvCheck(int)
Definition: format.h:1222
void write(BasicCStringRef< Char > format, ArgList args)
Definition: format.h:2637
char Char
Definition: format.h:2392
Definition: format.h:3281
static wchar_t convert(char value)
Definition: format.h:873
void write(const char *value)
Definition: format.h:1972
#define FMT_NULL
Definition: format.h:222
int data[2]
Definition: format.h:361
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3300
BasicWriter & operator<<(unsigned long value)
Definition: format.h:2653
T type
Definition: format.h:1203
#define FMT_DISPATCH(call)
Definition: format.h:1538
BasicCStringRef(const std::basic_string< Char, std::char_traits< Char >, Allocator > &s)
Definition: format.h:602
FormatInt(ULongLong value)
Definition: format.h:3383
T const_check(T value)
Definition: format.h:378
#define BUFFER_SIZE
Definition: blockchain_utilities.h:37
Definition: format.h:1119
unsigned width_
Definition: format.h:1726
fmt::StringRef sep_
Definition: format.h:986
Definition: format.h:1949
internal::Arg::Type type(unsigned index) const
Definition: format.h:1484
BasicArrayWriter(Char *array, std::size_t size)
Definition: format.h:3210
Definition: format.h:614
int precision() const
Definition: format.h:1746
FMT_GCC_EXTENSION typedef unsigned long long ULongLong
Definition: format.h:437
Definition: format.h:3881
Definition: format.h:455
char * str_
Definition: format.h:3343
BasicArgFormatter(BasicFormatter< Char, Impl > &formatter, Spec &spec, const Char *fmt)
Definition: format.h:2130
Definition: format.h:1183
Definition: format.h:3281
BasicWriter< Char > & writer()
Definition: format.h:2190
Definition: block_queue.cpp:41
const T & operator[](std::size_t index) const
Definition: format.h:729
static uint64_t type(const NamedArgWithType< Char_, T > &)
Definition: format.h:1425
Definition: format.h:912
Definition: format.h:3199
Definition: format.h:1700
DummyInt isinf(...)
Definition: format.h:370
static uint64_t type(long)
Definition: format.h:1337
#define FMT_STATIC_ASSERT(cond, message)
Definition: format.h:1253
char fill() const
Definition: format.h:1721
Definition: format.h:2227
Result visit_custom(Arg::CustomValue)
Definition: format.h:1650
MakeValue(const NamedArg< Char_ > &value)
Definition: format.h:1418
Definition: format.h:1117
Char * write_unsigned_decimal(UInt value, unsigned prefix_size=0)
Definition: format.h:2495
BasicWriter & operator<<(ULongLong value)
Definition: format.h:2666
Definition: format.h:1760
const void * pointer
Definition: format.h:1108
FMT_FUNC void format_system_error(Writer &out, int error_code, StringRef message) FMT_NOEXCEPT
Definition: format.cc:217
BasicWriter & operator<<(double value)
Definition: format.h:2670
NamedArg(BasicStringRef< Char > argname, const T &value)
Definition: format.h:1447
static wchar_t convert(wchar_t value)
Definition: format.h:874
BasicStringRef< Char > name
Definition: format.h:1444
unsigned parse_nonnegative_int(const Char *&s)
Definition: format.h:3609
ArgType()
Definition: format.h:2266
BasicStringRef(const Char *s, std::size_t size)
Definition: format.h:494
const Char * data() const FMT_NOEXCEPT
Definition: format.h:2590
uint32_t Type
Definition: format.h:912
#define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U)
Definition: format.h:628
void format(BasicCStringRef< Char > format_str)
Definition: format.h:3859
StringValue< signed char > sstring
Definition: format.h:1110
virtual ~BasicWriter()
Definition: format.h:2579
std::size_t size() const
Definition: format.h:528
ThousandsSep(fmt::StringRef sep)
Definition: format.h:992
CustomValue custom
Definition: format.h:1113
void(* FormatFunc)(Writer &, int, StringRef)
Definition: format.cc:108
const ArgList & args() const
Definition: format.h:2060
FormatInt(unsigned value)
Definition: format.h:3381
void visit_any_double(T value)
Definition: format.h:1987
#define FMT_THROW(x)
Definition: format.h:171
ArgJoin< wchar_t, It > join(It first, It last, const BasicCStringRef< wchar_t > &sep)
Definition: format.h:3898
Spec & spec()
Definition: format.h:1964
bool flag(unsigned) const
Definition: format.h:1754
Definition: format.h:1777
Definition: format.h:1790
Yes & convert(fmt::ULongLong)
T & operator[](std::size_t index)
Definition: format.h:728
FMT_FUNC void report_system_error(int error_code, fmt::StringRef message) FMT_NOEXCEPT
Definition: format.cc:472
std::size_t size
Definition: format.h:1090
friend bool operator>(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:551
ArgFormatter(BasicFormatter< Char > &formatter, FormatSpec &spec, const Char *fmt)
Definition: format.h:2147
void set_string(StringRef str)
Definition: format.h:1294
const Char * str_
Definition: format.h:1792
Allocator get_allocator() const
Definition: format.h:799
Definition: format.h:459
static uint64_t type(unsigned long)
Definition: format.h:1347
void append(const U *begin, const U *end)
Definition: format.h:734
const Char * data() const
Definition: format.h:525
Definition: format.h:1700
static uint64_t type(const T &)
Definition: format.h:1411
FixedBuffer(Char *array, std::size_t size)
Definition: format.h:826
Definition: format.h:668
virtual ~Buffer()
Definition: format.h:689
#define FMT_API
Definition: format.h:78
FormatError(const FormatError &ferr)
Definition: format.h:618
#define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type)
Definition: format.h:2346
BasicStringRef< wchar_t > WStringRef
Definition: format.h:560
void visit_pointer(const void *value)
Definition: format.h:2044
void arg(WStringRef, const internal::NamedArg< Char > &) FMT_DELETED_OR_UNDEFINED
void append_float_length(Char *&, T)
Definition: format.h:2559
Result visit_any_int(T)
Definition: format.h:1609
internal::Arg operator[](unsigned index) const
Definition: format.h:1505
Definition: format.h:1751
Definition: format.h:1200
Definition: format.h:1170
t0
Definition: pow22523.h:53
static uint64_t type(wchar_t)
Definition: format.h:1365
Definition: format.h:844
Definition: format.h:3144
void clear() FMT_NOEXCEPT
Definition: format.h:716
BasicStringRef< char > StringRef
Definition: format.h:559
std::wstring format(WCStringRef format_str, ArgList args)
Definition: format.h:3306
#define FMT_MAKE_STR_VALUE(Type, TYPE)
Definition: format.h:1368
fmt::StringRef thousands_sep(...)
Definition: format.h:1234
BasicWriter & operator<<(long value)
Definition: format.h:2649
void clear() FMT_NOEXCEPT
Definition: format.h:2732
Definition: format.h:984
Alignment
Definition: format.h:1699
void write_decimal(Int value)
Definition: format.h:2504
BasicMemoryWriter< char > MemoryWriter
Definition: format.h:3175
#define FMT_DEFAULTED_COPY_CTOR(TypeName)
Definition: format.h:255
uint64_t types() const
Definition: format.h:1502
Definition: format.h:3281
uint64_t types_
Definition: format.h:1473
RuntimeError()
Definition: format.h:1459
CharPtr grow_buffer(std::size_t n)
Definition: format.h:2487
#define FMT_ARG_TYPE_DEFAULT(n)
Definition: format.h:2272
Null< T > Unsupported
Definition: format.h:1151
BasicCStringRef< Char > sep
Definition: format.h:3884
CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec)
Definition: format.h:2739
T value_
Definition: format.h:1779
DummyInt isnan(...)
Definition: format.h:372
Null< T > Supported
Definition: format.h:1144
Type
Definition: format.h:1116
Definition: format.h:1213
char type_prefix() const
Definition: format.h:1772
Result visit_long_long(LongLong value)
Definition: format.h:1583
MakeValue(unsigned long value)
Definition: format.h:1341
#define FMT_OVERRIDE
Definition: format.h:212
Result visit_double(double value)
Definition: format.h:1614
char type_prefix() const
Definition: format.h:1720
IntFormatSpec< int, TypeSpec< 'X'> > hexu(int value)
BasicWriter & operator<<(LongLong value)
Definition: format.h:2656
Definition: format.h:1469
SystemError()
Definition: format.h:2394
Definition: format.h:1705
BasicWriter & operator<<(typename internal::WCharHelper< StringRef, Char >::Supported value)
Definition: format.h:2711
Definition: format.h:748
BasicCStringRef< char > CStringRef
Definition: format.h:610
unsigned uint_value
Definition: format.h:1103
friend bool operator==(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:539
bool check_no_auto_index(const char *&error)
Definition: format.h:2081
Result visit_uint(unsigned value)
Definition: format.h:1588
void write(bool value)
Definition: format.h:1966
t3
Definition: pow225521.h:103
MapType map_
Definition: format.h:1932
static bool isinfinity(T x)
Definition: format.h:393
BasicWriter< char > Writer
Definition: format.h:444
int compare(BasicStringRef other) const
Definition: format.h:531
char type_prefix() const
Definition: format.h:1756
Definition: format.h:1465
void append_float_length(Char *&format_ptr, long double)
Definition: format.h:2554
Result visit_string(Arg::StringValue< char >)
Definition: format.h:1635
void FormatSigned(LongLong value)
Definition: format.h:3367
int b
Definition: base.py:1
Definition: format.h:1165
BasicWriter< Char > & writer_
Definition: format.h:2161
std::vector< std::pair< fmt::BasicStringRef< Char >, internal::Arg > > MapType
Definition: format.h:1929
Definition: format.h:1122
int int_value
Definition: format.h:1102
MakeUnsigned< Int >::Type to_unsigned(Int value)
Definition: format.h:641
void visit_bool(bool value)
Definition: format.h:1989
std::size_t capacity() const
Definition: format.h:695
ArgList()
Definition: format.h:1495
friend bool operator>=(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:554
Result visit_cstring(const char *)
Definition: format.h:1630
SystemError(int error_code, CStringRef message)
Definition: format.h:2415
StringValue< char > string
Definition: format.h:1109
BasicWriter & operator<<(char value)
Definition: format.h:2689
DummyInt signbit(...)
Definition: format.h:368
const internal::Arg * find(const fmt::BasicStringRef< Char > &name) const
Definition: format.h:1937
void check_sign(const Char *&s, const Arg &arg)
Definition: format.h:3637
BasicCStringRef< wchar_t > WCStringRef
Definition: format.h:611
Definition: format.h:892
type
Definition: json.h:74
const Char * value
Definition: format.h:1089
#define false
Definition: stdbool.h:38
unsigned digit_index_
Definition: format.h:989
BasicWriter & operator<<(typename internal::WCharHelper< wchar_t, Char >::Supported value)
Definition: format.h:2694
static bool isnotanumber(T x)
Definition: format.h:406
RuntimeError(const RuntimeError &rerr)
Definition: format.h:1460
T value() const
Definition: format.h:1785
ArgList(ULongLong types, const internal::Arg *args)
Definition: format.h:1499
Definition: format.h:450
Buffer(T *ptr=FMT_NULL, std::size_t capacity=0)
Definition: format.h:677
Definition: format.h:1565
~MemoryBuffer()
Definition: format.h:763
const internal::Arg * args_
Definition: format.h:1481
#define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: format.h:240
Spec SpecType
Definition: format.h:1978
void write_int(T value, Spec spec)
Definition: format.h:2863
Definition: format.h:3281
Definition: format.h:1710
DummyInt _isnan(...)
Definition: format.h:373
Definition: format.h:1705
Char * CharPtr
Definition: format.h:838
It last
Definition: format.h:3883
BasicWriter< wchar_t > WWriter
Definition: format.h:447
ArgFormatterBase(BasicWriter< Char > &w, Spec &s)
Definition: format.h:1980
MakeValue(const T &value, typename EnableIf< ConvertToInt< T >::value, int >::type=0)
Definition: format.h:1405
Formatter::Char Char
Definition: format.h:1269
void push_back(const T &value)
Definition: format.h:718
BasicFormatter(const ArgList &args, BasicWriter< Char > &w)
Definition: format.h:2186
BasicWriter & operator<<(long double value)
Definition: format.h:2681
char No[2]
Definition: format.h:1155
internal::CharTraits< Char >::CharPtr CharPtr
Definition: format.h:2471
T Unsupported
Definition: format.h:1145
BasicArrayWriter(Char(&array)[SIZE])
Definition: format.h:3220
Definition: format.h:1219
BasicArrayWriter< char > ArrayWriter
Definition: format.h:3224
BasicMemoryWriter(const Allocator &alloc=Allocator())
Definition: format.h:3149
Definition: format.h:3281
MakeArg()
Definition: format.h:1431
t2
Definition: pow22523.h:103
T type
Definition: format.h:1206
int precision() const
Definition: format.h:1717
Definition: format.h:1738
const Char * data_
Definition: format.h:590
Definition: format.h:1700
char type() const
Definition: format.h:1771
Color
Definition: format.h:3281
bool flag(unsigned) const
Definition: format.h:1718
char type() const
Definition: format.h:1755
uint64_t type
Definition: format.h:2264
FormatInt(long value)
Definition: format.h:3379
Definition: format.h:648
BasicWriter< Char > & writer_
Definition: format.h:1951
Definition: format.h:1714
static char convert(char value)
Definition: format.h:853
Definition: format.cc:213
FormatInt(LongLong value)
Definition: format.h:3380
void visit_cstring(const char *value)
Definition: format.h:2027
T * ptr_
Definition: format.h:673
BasicWriter & operator<<(unsigned value)
Definition: format.h:2646
char * format_decimal(ULongLong value)
Definition: format.h:3346
BasicWriter< Char > & writer()
Definition: format.h:1963
void require_numeric_argument(const Arg &arg, char spec)
Definition: format.h:3628
void set_string(WStringRef str)
Definition: format.h:1299
FMT_FUNC void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:486
Result visit_unhandled_arg()
Definition: format.h:1572
StringValue< wchar_t > wstring
Definition: format.h:1112
unsigned count_digits(uint64_t n)
Definition: format.h:953
MemoryBuffer(const Allocator &alloc=Allocator())
Definition: format.h:761
Definition: format.h:444
Buffer< Char > & buffer() FMT_NOEXCEPT
Definition: format.h:2734
void write(BasicWriter< Char > &w, const Char *start, const Char *end)
Definition: format.h:2091
t1
Definition: pow22523.h:58
Definition: format.h:3281
Definition: format.h:1705
Result visit_char(int value)
Definition: format.h:1603
IntFormatSpec(T val, const SpecT &spec=SpecT())
Definition: format.h:1782
Definition: format.h:3281
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, required > &arg)
Definition: command_line.h:195
Definition: format.h:1133
Definition: format.h:978
FormatterBase(const ArgList &args)
Definition: format.h:2062
IntFormatSpec< int, TypeSpec< 'b'> > bin(int value)
Definition: format.h:2263
Definition: format.h:1206
Definition: format.h:833
MakeArg(const T &value)
Definition: format.h:1436
void visit_char(int value)
Definition: format.h:1997
#define FMT_DEFINE_INT_FORMATTERS(TYPE)
Definition: format.h:1844
Definition: format.h:487
void visit_custom(internal::Arg::CustomValue c)
Definition: format.h:2136
void reserve(std::size_t capacity)
Definition: format.h:711
BasicData Data
Definition: format.h:940
internal::MemoryBuffer< Char, internal::INLINE_BUFFER_SIZE, Allocator > buffer_
Definition: format.h:3146
std::size_t capacity_
Definition: format.h:675
FormatError(CStringRef message)
Definition: format.h:616
void write_pointer(const void *p)
Definition: format.h:1956
T Supported
Definition: format.h:1150
const char * data() const
Definition: format.h:3394
std::size_t size() const
Definition: format.h:692
Result visit_int(int value)
Definition: format.h:1578
static Arg make(const T &value)
Definition: format.h:2252
wchar_t fill() const
Definition: format.h:1734
Definition: format.h:1221
internal::Arg Arg
Definition: format.h:1567
No & convert(...)
ULongLong ulong_long_value
Definition: format.h:1105
Definition: format.cc:79
unsigned flags_
Definition: format.h:1761
It first
Definition: format.h:3882
BasicMemoryWriter< wchar_t > WMemoryWriter
Definition: format.h:3176
Definition: format.h:1138
#define FMT_DTOR_NOEXCEPT
Definition: format.h:203
std::ostream & operator<<(std::ostream &o, const crypto::public_key &v)
Definition: cryptonote_basic_impl.h:141
Definition: format.h:1725
FormatFunc format
Definition: format.h:1098
Arg get_arg(unsigned arg_index, const char *&error)
Definition: format.h:2077
#define FMT_GEN15(f)
Definition: format.h:2216
Result visit_wstring(Arg::StringValue< wchar_t >)
Definition: format.h:1640
static uint64_t type(const NamedArg< Char_ > &)
Definition: format.h:1423
Type type
Definition: format.h:1129
const char * name
Definition: simplewallet.cpp:180
Definition: format.h:2385
Definition: format.h:1429
MakeValue()
Definition: format.h:1314
unsigned width() const
Definition: format.h:1716
std::string str() const
Definition: format.h:3410
Definition: format.h:1135
int precision_
Definition: format.h:1762
AlignSpec(unsigned width, wchar_t fill, Alignment align=ALIGN_DEFAULT)
Definition: format.h:1741
void visit_any_int(T value)
Definition: format.h:1984
uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT))
Definition: format.h:2274
static bool isnegative(double x)
Definition: format.h:416
LongLong long_long_value
Definition: format.h:1104
DummyInt _finite(...)
Definition: format.h:371
const Char * c_str() const
Definition: format.h:607
void visit_wstring(internal::Arg::StringValue< Char > value)
Definition: format.h:2040
Alignment align() const
Definition: format.h:1715
Definition: format.h:1700
static bool is_negative(T)
Definition: format.h:900
#define FMT_VARIADIC_VOID(func, arg_type)
Definition: format.h:2329
long double long_double_value
Definition: format.h:1107
const Char * c_str() const
Definition: format.h:2596
ArgJoin(It first, It last, const BasicCStringRef< Char > &sep)
Definition: format.h:3886
void report_unhandled_arg()
Definition: format.h:1570
Definition: format.h:588
StringValue< unsigned char > ustring
Definition: format.h:1111
const internal::Value * values_
Definition: format.h:1480
std::numeric_limits< fmt::internal::DummyInt > FPUtil
Definition: format.h:364
Definition: format.h:930
#define FMT_DISABLE_CONVERSION_TO_INT(Type)
Definition: format.h:1190
const Char * str() const
Definition: format.h:1801
#define FMT_VARIADIC_W(ReturnType, func,...)
Definition: format.h:3568
unsigned width() const
Definition: format.h:1733
internal::ArgMap< Char > map_
Definition: format.h:2162
static Value make(const T &value)
Definition: format.h:2234
#define true
Definition: stdbool.h:37
#define s(x, c)
Definition: aesb.c:46
std::size_t size() const
Definition: format.h:2584
void write_double(T value, const Spec &spec)
Definition: format.h:2956
void format_decimal(char *&buffer, T value)
Definition: format.h:3417
static Char cast(int value)
Definition: format.h:840
Definition: format.h:1121
FormatInt(int value)
Definition: format.h:3378
static CharPtr fill_padding(CharPtr buffer, unsigned total_size, std::size_t content_size, wchar_t fill)
Definition: format.h:2783
std::basic_string< Char > to_string() const
Definition: format.h:520
ArgList(ULongLong types, const internal::Value *values)
Definition: format.h:1497
Definition: format.h:1267
int error_code_
Definition: format.h:2390
Result visit_long_double(long double value)
Definition: format.h:1619
int precision() const
Definition: format.h:1770
BasicArrayWriter< wchar_t > WArrayWriter
Definition: format.h:3225
FMT_GCC_EXTENSION typedef long long LongLong
Definition: format.h:436
DummyInt _ecvt_s(...)
Definition: format.h:369
#define FMT_DELETED_OR_UNDEFINED
Definition: format.h:239
NamedArgWithType(BasicStringRef< Char > argname, const T &value)
Definition: format.h:1453
void deallocate()
Definition: format.h:753
FormatSpec(unsigned width=0, char type=0, wchar_t fill=' ')
Definition: format.h:1765
const Char * data_
Definition: format.h:489
bool is_name_start(Char c)
Definition: format.h:3602
const Char * format_
Definition: format.h:2119
Result visit(const Arg &arg)
Definition: format.h:1662
Definition: format.h:1143
char type_
Definition: format.h:1763
Definition: format.h:1457
BasicStringRef(const std::basic_string< Char, std::char_traits< Char >, Allocator > &s)
Definition: format.h:511
std::size_t size_
Definition: format.h:490