TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
format.h
Go to the documentation of this file.
1 /*
2  Formatting library for C++
3 
4  Copyright (c) 2012 - 2015, 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 <cmath>
33 #include <cstdio>
34 #include <cstring>
35 #include <limits>
36 #include <memory>
37 #include <stdexcept>
38 #include <string>
39 #include <vector>
40 #include <utility>
41 
42 #ifndef FMT_USE_IOSTREAMS
43 # define FMT_USE_IOSTREAMS 1
44 #endif
45 
46 #if FMT_USE_IOSTREAMS
47 # include <ostream>
48 #endif
49 
50 #ifdef _SECURE_SCL
51 # define FMT_SECURE_SCL _SECURE_SCL
52 #else
53 # define FMT_SECURE_SCL 0
54 #endif
55 
56 #if FMT_SECURE_SCL
57 # include <iterator>
58 #endif
59 
60 #if defined(_MSC_VER) && _MSC_VER <= 1500
61 typedef unsigned __int32 uint32_t;
62 typedef unsigned __int64 uint64_t;
63 typedef __int64 intmax_t;
64 #else
65 #include <stdint.h>
66 #endif
67 
68 #if !defined(FMT_HEADER_ONLY) && defined(_WIN32)
69 # ifdef FMT_EXPORT
70 # define FMT_API __declspec(dllexport)
71 # elif defined(FMT_SHARED)
72 # define FMT_API __declspec(dllimport)
73 # endif
74 #endif
75 #ifndef FMT_API
76 # define FMT_API
77 #endif
78 
79 #ifdef __GNUC__
80 # define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
81 # define FMT_GCC_EXTENSION __extension__
82 # if FMT_GCC_VERSION >= 406
83 # pragma GCC diagnostic push
84 // Disable the warning about "long long" which is sometimes reported even
85 // when using __extension__.
86 # pragma GCC diagnostic ignored "-Wlong-long"
87 // Disable the warning about declaration shadowing because it affects too
88 // many valid cases.
89 # pragma GCC diagnostic ignored "-Wshadow"
90 // Disable the warning about implicit conversions that may change the sign of
91 // an integer; silencing it otherwise would require many explicit casts.
92 # pragma GCC diagnostic ignored "-Wsign-conversion"
93 # endif
94 # if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__
95 # define FMT_HAS_GXX_CXX11 1
96 # endif
97 #else
98 # define FMT_GCC_EXTENSION
99 #endif
100 
101 #if defined(__clang__) && !defined(__INTEL_COMPILER)
102 # pragma clang diagnostic push
103 # pragma clang diagnostic ignored "-Wdocumentation"
104 #endif
105 
106 #ifdef __GNUC_LIBSTD__
107 # define FMT_GNUC_LIBSTD_VERSION (__GNUC_LIBSTD__ * 100 + __GNUC_LIBSTD_MINOR__)
108 #endif
109 
110 #ifdef __has_feature
111 # define FMT_HAS_FEATURE(x) __has_feature(x)
112 #else
113 # define FMT_HAS_FEATURE(x) 0
114 #endif
115 
116 #ifdef __has_builtin
117 # define FMT_HAS_BUILTIN(x) __has_builtin(x)
118 #else
119 # define FMT_HAS_BUILTIN(x) 0
120 #endif
121 
122 #ifdef __has_cpp_attribute
123 # define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
124 #else
125 # define FMT_HAS_CPP_ATTRIBUTE(x) 0
126 #endif
127 
128 #ifndef FMT_USE_VARIADIC_TEMPLATES
129 // Variadic templates are available in GCC since version 4.4
130 // (http://gcc.gnu.org/projects/cxx0x.html) and in Visual C++
131 // since version 2013.
132 # define FMT_USE_VARIADIC_TEMPLATES \
133  (FMT_HAS_FEATURE(cxx_variadic_templates) || \
134  (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1800)
135 #endif
136 
137 #ifndef FMT_USE_RVALUE_REFERENCES
138 // Don't use rvalue references when compiling with clang and an old libstdc++
139 // as the latter doesn't provide std::move.
140 # if defined(FMT_GNUC_LIBSTD_VERSION) && FMT_GNUC_LIBSTD_VERSION <= 402
141 # define FMT_USE_RVALUE_REFERENCES 0
142 # else
143 # define FMT_USE_RVALUE_REFERENCES \
144  (FMT_HAS_FEATURE(cxx_rvalue_references) || \
145  (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600)
146 # endif
147 #endif
148 
149 #if FMT_USE_RVALUE_REFERENCES
150 # include <utility> // for std::move
151 #endif
152 
153 // Check if exceptions are disabled.
154 #if defined(__GNUC__) && !defined(__EXCEPTIONS)
155 # define FMT_EXCEPTIONS 0
156 #endif
157 #if defined(_MSC_VER) && !_HAS_EXCEPTIONS
158 # define FMT_EXCEPTIONS 0
159 #endif
160 #ifndef FMT_EXCEPTIONS
161 # define FMT_EXCEPTIONS 1
162 #endif
163 
164 #ifndef FMT_THROW
165 # if FMT_EXCEPTIONS
166 # define FMT_THROW(x) throw x
167 # else
168 # define FMT_THROW(x) assert(false)
169 # endif
170 #endif
171 
172 // Define FMT_USE_NOEXCEPT to make C++ Format use noexcept (C++11 feature).
173 #ifndef FMT_USE_NOEXCEPT
174 # define FMT_USE_NOEXCEPT 0
175 #endif
176 
177 #ifndef FMT_NOEXCEPT
178 # if FMT_EXCEPTIONS
179 # if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \
180  (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
181  _MSC_VER >= 1900
182 # define FMT_NOEXCEPT noexcept
183 # else
184 # define FMT_NOEXCEPT throw()
185 # endif
186 # else
187 # define FMT_NOEXCEPT
188 # endif
189 #endif
190 
191 // A macro to disallow the copy constructor and operator= functions
192 // This should be used in the private: declarations for a class
193 #ifndef FMT_USE_DELETED_FUNCTIONS
194 # define FMT_USE_DELETED_FUNCTIONS 0
195 #endif
196 
197 #if FMT_USE_DELETED_FUNCTIONS || FMT_HAS_FEATURE(cxx_deleted_functions) || \
198  (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1800
199 # define FMT_DELETED_OR_UNDEFINED = delete
200 # define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
201  TypeName(const TypeName&) = delete; \
202  TypeName& operator=(const TypeName&) = delete
203 #else
204 # define FMT_DELETED_OR_UNDEFINED
205 # define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
206  TypeName(const TypeName&); \
207  TypeName& operator=(const TypeName&)
208 #endif
209 
210 #ifndef FMT_USE_USER_DEFINED_LITERALS
211 // All compilers which support UDLs also support variadic templates. This
212 // makes the fmt::literals implementation easier. However, an explicit check
213 // for variadic templates is added here just in case.
214 # define FMT_USE_USER_DEFINED_LITERALS \
215  FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES && \
216  (FMT_HAS_FEATURE(cxx_user_literals) || \
217  (FMT_GCC_VERSION >= 407 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1900)
218 #endif
219 
220 #ifndef FMT_ASSERT
221 # define FMT_ASSERT(condition, message) assert((condition) && message)
222 #endif
223 
224 
225 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz)
226 # define FMT_BUILTIN_CLZ(n) __builtin_clz(n)
227 #endif
228 
229 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clzll)
230 # define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n)
231 #endif
232 
233 // Some compilers masquerade as both MSVC and GCC-likes or
234 // otherwise support __builtin_clz and __builtin_clzll, so
235 // only define FMT_BUILTIN_CLZ using the MSVC intrinsics
236 // if the clz and clzll builtins are not available.
237 #if defined(_MSC_VER) && !defined(FMT_BUILTIN_CLZLL)
238 # include <intrin.h> // _BitScanReverse, _BitScanReverse64
239 
240 namespace fmt {
241 namespace internal {
242 # pragma intrinsic(_BitScanReverse)
243 inline uint32_t clz(uint32_t x) {
244  unsigned long r = 0;
245  _BitScanReverse(&r, x);
246 
247  assert(x != 0);
248  // Static analysis complains about using uninitialized data
249  // "r", but the only way that can happen is if "x" is 0,
250  // which the callers guarantee to not happen.
251 # pragma warning(suppress: 6102)
252  return 31 - r;
253 }
254 # define FMT_BUILTIN_CLZ(n) fmt::internal::clz(n)
255 
256 # ifdef _WIN64
257 # pragma intrinsic(_BitScanReverse64)
258 # endif
259 
260 inline uint32_t clzll(uint64_t x) {
261  unsigned long r = 0;
262 # ifdef _WIN64
263  _BitScanReverse64(&r, x);
264 # else
265  // Scan the high 32 bits.
266  if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32)))
267  return 63 - (r + 32);
268 
269  // Scan the low 32 bits.
270  _BitScanReverse(&r, static_cast<uint32_t>(x));
271 # endif
272 
273  assert(x != 0);
274  // Static analysis complains about using uninitialized data
275  // "r", but the only way that can happen is if "x" is 0,
276  // which the callers guarantee to not happen.
277 # pragma warning(suppress: 6102)
278  return 63 - r;
279 }
280 # define FMT_BUILTIN_CLZLL(n) fmt::internal::clzll(n)
281 }
282 }
283 #endif
284 
285 namespace fmt {
286 namespace internal {
287 struct DummyInt {
288  int data[2];
289  operator int() const { return 0; }
290 };
292 
293 // Dummy implementations of system functions such as signbit and ecvt called
294 // if the latter are not available.
295 inline DummyInt signbit(...) { return DummyInt(); }
296 inline DummyInt _ecvt_s(...) { return DummyInt(); }
297 inline DummyInt isinf(...) { return DummyInt(); }
298 inline DummyInt _finite(...) { return DummyInt(); }
299 inline DummyInt isnan(...) { return DummyInt(); }
300 inline DummyInt _isnan(...) { return DummyInt(); }
301 
302 // A helper function to suppress bogus "conditional expression is constant"
303 // warnings.
304 template <typename T>
305 inline T check(T value) { return value; }
306 }
307 } // namespace fmt
308 
309 namespace std {
310 // Standard permits specialization of std::numeric_limits. This specialization
311 // is used to resolve ambiguity between isinf and std::isinf in glibc:
312 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48891
313 // and the same for isnan and signbit.
314 template <>
315 class numeric_limits<fmt::internal::DummyInt> :
316  public std::numeric_limits<int> {
317  public:
318  // Portable version of isinf.
319  template <typename T>
320  static bool isinfinity(T x) {
321  using namespace fmt::internal;
322  // The resolution "priority" is:
323  // isinf macro > std::isinf > ::isinf > fmt::internal::isinf
324  if (check(sizeof(isinf(x)) == sizeof(bool) ||
325  sizeof(isinf(x)) == sizeof(int))) {
326  return isinf(x) != 0;
327  }
328  return !_finite(static_cast<double>(x));
329  }
330 
331  // Portable version of isnan.
332  template <typename T>
333  static bool isnotanumber(T x) {
334  using namespace fmt::internal;
335  if (check(sizeof(isnan(x)) == sizeof(bool) ||
336  sizeof(isnan(x)) == sizeof(int))) {
337  return isnan(x) != 0;
338  }
339  return _isnan(static_cast<double>(x)) != 0;
340  }
341 
342  // Portable version of signbit.
343  static bool isnegative(double x) {
344  using namespace fmt::internal;
345  if (check(sizeof(signbit(x)) == sizeof(int)))
346  return signbit(x) != 0;
347  if (x < 0) return true;
348  if (!isnotanumber(x)) return false;
349  int dec = 0, sign = 0;
350  char buffer[2]; // The buffer size must be >= 2 or _ecvt_s will fail.
351  _ecvt_s(buffer, sizeof(buffer), x, 0, &dec, &sign);
352  return sign != 0;
353  }
354 };
355 } // namespace std
356 
357 namespace fmt {
358 
359 // Fix the warning about long long on older versions of GCC
360 // that don't support the diagnostic pragma.
361 FMT_GCC_EXTENSION typedef long long LongLong;
362 FMT_GCC_EXTENSION typedef unsigned long long ULongLong;
363 
364 #if FMT_USE_RVALUE_REFERENCES
365 using std::move;
366 #endif
367 
368 template <typename Char>
370 
371 typedef BasicWriter<char> Writer;
373 
374 template <typename Char>
376 
377 template <typename Char, typename T>
378 void format(BasicFormatter<Char> &f, const Char *&format_str, const T &value);
379 
404 template <typename Char>
406  private:
407  const Char *data_;
408  std::size_t size_;
409 
410  public:
412  BasicStringRef(const Char *s, std::size_t size) : data_(s), size_(size) {}
413 
421  : data_(s), size_(std::char_traits<Char>::length(s)) {}
422 
428  BasicStringRef(const std::basic_string<Char> &s)
429  : data_(s.c_str()), size_(s.size()) {}
430 
436  std::basic_string<Char> to_string() const {
437  return std::basic_string<Char>(data_, size_);
438  }
439 
441  const Char *data() const { return data_; }
442 
444  std::size_t size() const { return size_; }
445 
446  // Lexicographically compare this string reference to other.
447  int compare(BasicStringRef other) const {
448  std::size_t size = size_ < other.size_ ? size_ : other.size_;
449  int result = std::char_traits<Char>::compare(data_, other.data_, size);
450  if (result == 0)
451  result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1);
452  return result;
453  }
454 
455  friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) {
456  return lhs.compare(rhs) == 0;
457  }
458  friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs) {
459  return lhs.compare(rhs) != 0;
460  }
461  friend bool operator<(BasicStringRef lhs, BasicStringRef rhs) {
462  return lhs.compare(rhs) < 0;
463  }
464  friend bool operator<=(BasicStringRef lhs, BasicStringRef rhs) {
465  return lhs.compare(rhs) <= 0;
466  }
467  friend bool operator>(BasicStringRef lhs, BasicStringRef rhs) {
468  return lhs.compare(rhs) > 0;
469  }
470  friend bool operator>=(BasicStringRef lhs, BasicStringRef rhs) {
471  return lhs.compare(rhs) >= 0;
472  }
473 };
474 
477 
503 template <typename Char>
505  private:
506  const Char *data_;
507 
508  public:
510  BasicCStringRef(const Char *s) : data_(s) {}
511 
517  BasicCStringRef(const std::basic_string<Char> &s) : data_(s.c_str()) {}
518 
520  const Char *c_str() const { return data_; }
521 };
522 
525 
529 class FormatError : public std::runtime_error {
530  public:
531  explicit FormatError(CStringRef message)
532  : std::runtime_error(message.c_str()) {}
533 };
534 
535 namespace internal {
536 
537 // MakeUnsigned<T>::Type gives an unsigned type corresponding to integer type T.
538 template <typename T>
539 struct MakeUnsigned { typedef T Type; };
540 
541 #define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U) \
542  template <> \
543  struct MakeUnsigned<T> { typedef U Type; }
544 
545 FMT_SPECIALIZE_MAKE_UNSIGNED(char, unsigned char);
546 FMT_SPECIALIZE_MAKE_UNSIGNED(signed char, unsigned char);
547 FMT_SPECIALIZE_MAKE_UNSIGNED(short, unsigned short);
548 FMT_SPECIALIZE_MAKE_UNSIGNED(int, unsigned);
549 FMT_SPECIALIZE_MAKE_UNSIGNED(long, unsigned long);
550 FMT_SPECIALIZE_MAKE_UNSIGNED(LongLong, ULongLong);
551 
552 // Casts nonnegative integer to unsigned.
553 template <typename Int>
555  FMT_ASSERT(value >= 0, "negative value");
556  return static_cast<typename MakeUnsigned<Int>::Type>(value);
557 }
558 
559 // The number of characters to store in the MemoryBuffer object itself
560 // to avoid dynamic memory allocation.
561 enum { INLINE_BUFFER_SIZE = 500 };
562 
563 #if FMT_SECURE_SCL
564 // Use checked iterator to avoid warnings on MSVC.
565 template <typename T>
566 inline stdext::checked_array_iterator<T*> make_ptr(T *ptr, std::size_t size) {
567  return stdext::checked_array_iterator<T*>(ptr, size);
568 }
569 #else
570 template <typename T>
571 inline T *make_ptr(T *ptr, std::size_t) { return ptr; }
572 #endif
573 } // namespace internal
574 
580 template <typename T>
581 class Buffer {
582  private:
584 
585  protected:
586  T *ptr_;
587  std::size_t size_;
588  std::size_t capacity_;
589 
590  Buffer(T *ptr = 0, std::size_t capacity = 0)
591  : ptr_(ptr), size_(0), capacity_(capacity) {}
592 
599  virtual void grow(std::size_t size) = 0;
600 
601  public:
602  virtual ~Buffer() {}
603 
605  std::size_t size() const { return size_; }
606 
608  std::size_t capacity() const { return capacity_; }
609 
613  void resize(std::size_t new_size) {
614  if (new_size > capacity_)
615  grow(new_size);
616  size_ = new_size;
617  }
618 
624  void reserve(std::size_t capacity) {
625  if (capacity > capacity_)
626  grow(capacity);
627  }
628 
629  void clear() FMT_NOEXCEPT { size_ = 0; }
630 
631  void push_back(const T &value) {
632  if (size_ == capacity_)
633  grow(size_ + 1);
634  ptr_[size_++] = value;
635  }
636 
638  template <typename U>
639  void append(const U *begin, const U *end);
640 
641  T &operator[](std::size_t index) { return ptr_[index]; }
642  const T &operator[](std::size_t index) const { return ptr_[index]; }
643 };
644 
645 template <typename T>
646 template <typename U>
647 void Buffer<T>::append(const U *begin, const U *end) {
648  std::size_t new_size = size_ + internal::to_unsigned(end - begin);
649  if (new_size > capacity_)
650  grow(new_size);
651  std::uninitialized_copy(begin, end,
652  internal::make_ptr(ptr_, capacity_) + size_);
653  size_ = new_size;
654 }
655 
656 namespace internal {
657 
658 // A memory buffer for trivially copyable/constructible types with the first SIZE
659 // elements stored in the object itself.
660 template <typename T, std::size_t SIZE, typename Allocator = std::allocator<T> >
661 class MemoryBuffer : private Allocator, public Buffer<T> {
662  private:
663  T data_[SIZE];
664 
665  // Deallocate memory allocated by the buffer.
666  void deallocate() {
667  if (this->ptr_ != data_) Allocator::deallocate(this->ptr_, this->capacity_);
668  }
669 
670  protected:
671  void grow(std::size_t size);
672 
673  public:
674  explicit MemoryBuffer(const Allocator &alloc = Allocator())
675  : Allocator(alloc), Buffer<T>(data_, SIZE) {}
676  ~MemoryBuffer() { deallocate(); }
677 
678 #if FMT_USE_RVALUE_REFERENCES
679  private:
680  // Move data from other to this buffer.
681  void move(MemoryBuffer &other) {
682  Allocator &this_alloc = *this, &other_alloc = other;
683  this_alloc = std::move(other_alloc);
684  this->size_ = other.size_;
685  this->capacity_ = other.capacity_;
686  if (other.ptr_ == other.data_) {
687  this->ptr_ = data_;
688  std::uninitialized_copy(other.data_, other.data_ + this->size_,
689  make_ptr(data_, this->capacity_));
690  } else {
691  this->ptr_ = other.ptr_;
692  // Set pointer to the inline array so that delete is not called
693  // when deallocating.
694  other.ptr_ = other.data_;
695  }
696  }
697 
698  public:
699  MemoryBuffer(MemoryBuffer &&other) {
700  move(other);
701  }
702 
704  assert(this != &other);
705  deallocate();
706  move(other);
707  return *this;
708  }
709 #endif
710 
711  // Returns a copy of the allocator associated with this buffer.
712  Allocator get_allocator() const { return *this; }
713 };
714 
715 template <typename T, std::size_t SIZE, typename Allocator>
717  std::size_t new_capacity = this->capacity_ + this->capacity_ / 2;
718  if (size > new_capacity)
719  new_capacity = size;
720  T *new_ptr = this->allocate(new_capacity);
721  // The following code doesn't throw, so the raw pointer above doesn't leak.
722  std::uninitialized_copy(this->ptr_, this->ptr_ + this->size_,
723  make_ptr(new_ptr, new_capacity));
724  std::size_t old_capacity = this->capacity_;
725  T *old_ptr = this->ptr_;
726  this->capacity_ = new_capacity;
727  this->ptr_ = new_ptr;
728  // deallocate may throw (at least in principle), but it doesn't matter since
729  // the buffer already uses the new storage and will deallocate it in case
730  // of exception.
731  if (old_ptr != data_)
732  Allocator::deallocate(old_ptr, old_capacity);
733 }
734 
735 // A fixed-size buffer.
736 template <typename Char>
737 class FixedBuffer : public fmt::Buffer<Char> {
738  public:
739  FixedBuffer(Char *array, std::size_t size) : fmt::Buffer<Char>(array, size) {}
740 
741  protected:
742  FMT_API void grow(std::size_t size);
743 };
744 
745 template <typename Char>
747  public:
748 #if FMT_SECURE_SCL
749  typedef stdext::checked_array_iterator<Char*> CharPtr;
750 #else
751  typedef Char *CharPtr;
752 #endif
753  static Char cast(int value) { return static_cast<Char>(value); }
754 };
755 
756 template <typename Char>
758 
759 template <>
760 class CharTraits<char> : public BasicCharTraits<char> {
761  private:
762  // Conversion from wchar_t to char is not allowed.
763  static char convert(wchar_t);
764 
765  public:
766  static char convert(char value) { return value; }
767 
768  // Formats a floating-point number.
769  template <typename T>
770  FMT_API static int format_float(char *buffer, std::size_t size,
771  const char *format, unsigned width, int precision, T value);
772 };
773 
774 template <>
775 class CharTraits<wchar_t> : public BasicCharTraits<wchar_t> {
776  public:
777  static wchar_t convert(char value) { return value; }
778  static wchar_t convert(wchar_t value) { return value; }
779 
780  template <typename T>
781  FMT_API static int format_float(wchar_t *buffer, std::size_t size,
782  const wchar_t *format, unsigned width, int precision, T value);
783 };
784 
785 // Checks if a number is negative - used to avoid warnings.
786 template <bool IsSigned>
787 struct SignChecker {
788  template <typename T>
789  static bool is_negative(T value) { return value < 0; }
790 };
791 
792 template <>
794  template <typename T>
795  static bool is_negative(T) { return false; }
796 };
797 
798 // Returns true if value is negative, false otherwise.
799 // Same as (value < 0) but doesn't produce warnings if T is an unsigned type.
800 template <typename T>
801 inline bool is_negative(T value) {
803 }
804 
805 // Selects uint32_t if FitsIn32Bits is true, uint64_t otherwise.
806 template <bool FitsIn32Bits>
807 struct TypeSelector { typedef uint32_t Type; };
808 
809 template <>
810 struct TypeSelector<false> { typedef uint64_t Type; };
811 
812 template <typename T>
813 struct IntTraits {
814  // Smallest of uint32_t and uint64_t that is large enough to represent
815  // all values of T.
816  typedef typename
818 };
819 
820 FMT_API void report_unknown_type(char code, const char *type);
821 
822 // Static data is placed in this class template to allow header-only
823 // configuration.
824 template <typename T = void>
826  static const uint32_t POWERS_OF_10_32[];
827  static const uint64_t POWERS_OF_10_64[];
828  static const char DIGITS[];
829 };
830 
832 
833 #ifdef FMT_BUILTIN_CLZLL
834 // Returns the number of decimal digits in n. Leading zeros are not counted
835 // except for n == 0 in which case count_digits returns 1.
836 inline unsigned count_digits(uint64_t n) {
837  // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
838  // and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits.
839  int t = (64 - FMT_BUILTIN_CLZLL(n | 1)) * 1233 >> 12;
840  return to_unsigned(t) - (n < Data::POWERS_OF_10_64[t]) + 1;
841 }
842 #else
843 // Fallback version of count_digits used when __builtin_clz is not available.
844 inline unsigned count_digits(uint64_t n) {
845  unsigned count = 1;
846  for (;;) {
847  // Integer division is slow so do it for a group of four digits instead
848  // of for every digit. The idea comes from the talk by Alexandrescu
849  // "Three Optimization Tips for C++". See speed-test for a comparison.
850  if (n < 10) return count;
851  if (n < 100) return count + 1;
852  if (n < 1000) return count + 2;
853  if (n < 10000) return count + 3;
854  n /= 10000u;
855  count += 4;
856  }
857 }
858 #endif
859 
860 #ifdef FMT_BUILTIN_CLZ
861 // Optional version of count_digits for better performance on 32-bit platforms.
862 inline unsigned count_digits(uint32_t n) {
863  int t = (32 - FMT_BUILTIN_CLZ(n | 1)) * 1233 >> 12;
864  return to_unsigned(t) - (n < Data::POWERS_OF_10_32[t]) + 1;
865 }
866 #endif
867 
868 // Formats a decimal unsigned integer value writing into buffer.
869 template <typename UInt, typename Char>
870 inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) {
871  buffer += num_digits;
872  while (value >= 100) {
873  // Integer division is slow so do it for a group of two digits instead
874  // of for every digit. The idea comes from the talk by Alexandrescu
875  // "Three Optimization Tips for C++". See speed-test for a comparison.
876  unsigned index = static_cast<unsigned>((value % 100) * 2);
877  value /= 100;
878  *--buffer = Data::DIGITS[index + 1];
879  *--buffer = Data::DIGITS[index];
880  }
881  if (value < 10) {
882  *--buffer = static_cast<char>('0' + value);
883  return;
884  }
885  unsigned index = static_cast<unsigned>(value * 2);
886  *--buffer = Data::DIGITS[index + 1];
887  *--buffer = Data::DIGITS[index];
888 }
889 
890 #ifndef _WIN32
891 # define FMT_USE_WINDOWS_H 0
892 #elif !defined(FMT_USE_WINDOWS_H)
893 # define FMT_USE_WINDOWS_H 1
894 #endif
895 
896 // Define FMT_USE_WINDOWS_H to 0 to disable use of windows.h.
897 // All the functionality that relies on it will be disabled too.
898 #if FMT_USE_WINDOWS_H
899 // A converter from UTF-8 to UTF-16.
900 // It is only provided for Windows since other systems support UTF-8 natively.
901 class UTF8ToUTF16 {
902  private:
904 
905  public:
906  FMT_API explicit UTF8ToUTF16(StringRef s);
907  operator WStringRef() const { return WStringRef(&buffer_[0], size()); }
908  size_t size() const { return buffer_.size() - 1; }
909  const wchar_t *c_str() const { return &buffer_[0]; }
910  std::wstring str() const { return std::wstring(&buffer_[0], size()); }
911 };
912 
913 // A converter from UTF-16 to UTF-8.
914 // It is only provided for Windows since other systems support UTF-8 natively.
915 class UTF16ToUTF8 {
916  private:
918 
919  public:
920  UTF16ToUTF8() {}
921  FMT_API explicit UTF16ToUTF8(WStringRef s);
922  operator StringRef() const { return StringRef(&buffer_[0], size()); }
923  size_t size() const { return buffer_.size() - 1; }
924  const char *c_str() const { return &buffer_[0]; }
925  std::string str() const { return std::string(&buffer_[0], size()); }
926 
927  // Performs conversion returning a system error code instead of
928  // throwing exception on conversion error. This method may still throw
929  // in case of memory allocation error.
930  FMT_API int convert(WStringRef s);
931 };
932 
933 FMT_API void format_windows_error(fmt::Writer &out, int error_code,
934  fmt::StringRef message) FMT_NOEXCEPT;
935 #endif
936 
937 FMT_API void format_system_error(fmt::Writer &out, int error_code,
938  fmt::StringRef message) FMT_NOEXCEPT;
939 
940 // A formatting argument value.
941 struct Value {
942  template <typename Char>
943  struct StringValue {
944  const Char *value;
945  std::size_t size;
946  };
947 
948  typedef void (*FormatFunc)(
949  void *formatter, const void *arg, void *format_str_ptr);
950 
951  struct CustomValue {
952  const void *value;
953  FormatFunc format;
954  };
955 
956  union {
958  unsigned uint_value;
959  LongLong long_long_value;
960  ULongLong ulong_long_value;
961  double double_value;
962  long double long_double_value;
963  const void *pointer;
969  };
970 
971  enum Type {
972  NONE, NAMED_ARG,
973  // Integer types should go first,
974  INT, UINT, LONG_LONG, ULONG_LONG, BOOL, CHAR, LAST_INTEGER_TYPE = CHAR,
975  // followed by floating-point types.
976  DOUBLE, LONG_DOUBLE, LAST_NUMERIC_TYPE = LONG_DOUBLE,
977  CSTRING, STRING, WSTRING, POINTER, CUSTOM
978  };
979 };
980 
981 // A formatting argument. It is a trivially copyable/constructible type to
982 // allow storage in internal::MemoryBuffer.
983 struct Arg : Value {
985 };
986 
987 template <typename Char>
988 struct NamedArg;
989 
990 template <typename T = void>
991 struct Null {};
992 
993 // A helper class template to enable or disable overloads taking wide
994 // characters and strings in MakeValue.
995 template <typename T, typename Char>
996 struct WCharHelper {
998  typedef T Unsupported;
999 };
1000 
1001 template <typename T>
1002 struct WCharHelper<T, wchar_t> {
1003  typedef T Supported;
1005 };
1006 
1007 typedef char Yes[1];
1008 typedef char No[2];
1009 
1010 // These are non-members to workaround an overload resolution bug in bcc32.
1011 Yes &convert(fmt::ULongLong);
1012 Yes &convert(std::ostream &);
1013 No &convert(...);
1014 
1015 template <typename T>
1016 T &get();
1017 
1018 struct DummyStream : std::ostream {
1019  DummyStream(); // Suppress a bogus warning in MSVC.
1020  // Hide all operator<< overloads from std::ostream.
1021  void operator<<(Null<>);
1022 };
1023 
1024 No &operator<<(std::ostream &, int);
1025 
1026 template<typename T, bool ENABLE_CONVERSION>
1028  enum { value = false };
1029 };
1030 
1031 template<typename T>
1033  // Convert to int only if T doesn't have an overloaded operator<<.
1034  enum {
1035  value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No)
1036  };
1037 };
1038 
1039 template<typename T, bool ENABLE_CONVERSION>
1041  enum { value = false };
1042 };
1043 
1044 template<typename T>
1046  enum {
1047  // Don't convert numeric types.
1049  };
1050 };
1051 
1052 template<typename T>
1054  enum { enable_conversion = sizeof(convert(get<T>())) == sizeof(Yes) };
1056 };
1057 
1058 #define FMT_DISABLE_CONVERSION_TO_INT(Type) \
1059  template <> \
1060  struct ConvertToInt<Type> { enum { value = 0 }; }
1061 
1062 // Silence warnings about convering float to int.
1065 FMT_DISABLE_CONVERSION_TO_INT(long double);
1066 
1067 template<bool B, class T = void>
1068 struct EnableIf {};
1069 
1070 template<class T>
1071 struct EnableIf<true, T> { typedef T type; };
1072 
1073 template<bool B, class T, class F>
1074 struct Conditional { typedef T type; };
1075 
1076 template<class T, class F>
1077 struct Conditional<false, T, F> { typedef F type; };
1078 
1079 // For bcc32 which doesn't understand ! in template arguments.
1080 template<bool>
1081 struct Not { enum { value = 0 }; };
1082 
1083 template<>
1084 struct Not<false> { enum { value = 1 }; };
1085 
1086 // Makes an Arg object from any type.
1087 template <typename Formatter>
1088 class MakeValue : public Arg {
1089  public:
1090  typedef typename Formatter::Char Char;
1091 
1092  private:
1093  // The following two methods are private to disallow formatting of
1094  // arbitrary pointers. If you want to output a pointer cast it to
1095  // "void *" or "const void *". In particular, this forbids formatting
1096  // of "[const] volatile char *" which is printed as bool by iostreams.
1097  // Do not implement!
1098  template <typename T>
1099  MakeValue(const T *value);
1100  template <typename T>
1101  MakeValue(T *value);
1102 
1103  // The following methods are private to disallow formatting of wide
1104  // characters and strings into narrow strings as in
1105  // fmt::format("{}", L"test");
1106  // To fix this, use a wide format string: fmt::format(L"{}", L"test").
1107 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
1109 #endif
1114 
1115  void set_string(StringRef str) {
1116  string.value = str.data();
1117  string.size = str.size();
1118  }
1119 
1120  void set_string(WStringRef str) {
1121  wstring.value = str.data();
1122  wstring.size = str.size();
1123  }
1124 
1125  // Formats an argument of a custom type, such as a user-defined class.
1126  template <typename T>
1127  static void format_custom_arg(
1128  void *formatter, const void *arg, void *format_str_ptr) {
1129  format(*static_cast<Formatter*>(formatter),
1130  *static_cast<const Char**>(format_str_ptr),
1131  *static_cast<const T*>(arg));
1132  }
1133 
1134  public:
1136 
1137 #define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \
1138  MakeValue(Type value) { field = rhs; } \
1139  static uint64_t type(Type) { return Arg::TYPE; }
1140 
1141 #define FMT_MAKE_VALUE(Type, field, TYPE) \
1142  FMT_MAKE_VALUE_(Type, field, TYPE, value)
1143 
1144  FMT_MAKE_VALUE(bool, int_value, BOOL)
1145  FMT_MAKE_VALUE(short, int_value, INT)
1146  FMT_MAKE_VALUE(unsigned short, uint_value, UINT)
1147  FMT_MAKE_VALUE(int, int_value, INT)
1148  FMT_MAKE_VALUE(unsigned, uint_value, UINT)
1149 
1151  // To minimize the number of types we need to deal with, long is
1152  // translated either to int or to long long depending on its size.
1153  if (check(sizeof(long) == sizeof(int)))
1154  int_value = static_cast<int>(value);
1155  else
1156  long_long_value = value;
1157  }
1158  static uint64_t type(long) {
1159  return sizeof(long) == sizeof(int) ? Arg::INT : Arg::LONG_LONG;
1160  }
1161 
1162  MakeValue(unsigned long value) {
1163  if (check(sizeof(unsigned long) == sizeof(unsigned)))
1164  uint_value = static_cast<unsigned>(value);
1165  else
1166  ulong_long_value = value;
1167  }
1168  static uint64_t type(unsigned long) {
1169  return sizeof(unsigned long) == sizeof(unsigned) ?
1170  Arg::UINT : Arg::ULONG_LONG;
1171  }
1172 
1173  FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG)
1174  FMT_MAKE_VALUE(ULongLong, ulong_long_value, ULONG_LONG)
1175  FMT_MAKE_VALUE(float, double_value, DOUBLE)
1176  FMT_MAKE_VALUE(double, double_value, DOUBLE)
1177  FMT_MAKE_VALUE(long double, long_double_value, LONG_DOUBLE)
1178  FMT_MAKE_VALUE(signed char, int_value, INT)
1179  FMT_MAKE_VALUE(unsigned char, uint_value, UINT)
1180  FMT_MAKE_VALUE(char, int_value, CHAR)
1181 
1182 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
1184  int_value = value;
1185  }
1186  static uint64_t type(wchar_t) { return Arg::CHAR; }
1187 #endif
1188 
1189 #define FMT_MAKE_STR_VALUE(Type, TYPE) \
1190  MakeValue(Type value) { set_string(value); } \
1191  static uint64_t type(Type) { return Arg::TYPE; }
1192 
1193  FMT_MAKE_VALUE(char *, string.value, CSTRING)
1194  FMT_MAKE_VALUE(const char *, string.value, CSTRING)
1195  FMT_MAKE_VALUE(const signed char *, sstring.value, CSTRING)
1196  FMT_MAKE_VALUE(const unsigned char *, ustring.value, CSTRING)
1197  FMT_MAKE_STR_VALUE(const std::string &, STRING)
1198  FMT_MAKE_STR_VALUE(StringRef, STRING)
1199  FMT_MAKE_VALUE_(CStringRef, string.value, CSTRING, value.c_str())
1200 
1201 #define FMT_MAKE_WSTR_VALUE(Type, TYPE) \
1202  MakeValue(typename WCharHelper<Type, Char>::Supported value) { \
1203  set_string(value); \
1204  } \
1205  static uint64_t type(Type) { return Arg::TYPE; }
1206 
1207  FMT_MAKE_WSTR_VALUE(wchar_t *, WSTRING)
1208  FMT_MAKE_WSTR_VALUE(const wchar_t *, WSTRING)
1209  FMT_MAKE_WSTR_VALUE(const std::wstring &, WSTRING)
1210  FMT_MAKE_WSTR_VALUE(WStringRef, WSTRING)
1211 
1212  FMT_MAKE_VALUE(void *, pointer, POINTER)
1213  FMT_MAKE_VALUE(const void *, pointer, POINTER)
1214 
1215  template <typename T>
1216  MakeValue(const T &value,
1217  typename EnableIf<Not<
1218  ConvertToInt<T>::value>::value, int>::type = 0) {
1219  custom.value = &value;
1220  custom.format = &format_custom_arg<T>;
1221  }
1222 
1223  template <typename T>
1224  MakeValue(const T &value,
1225  typename EnableIf<ConvertToInt<T>::value, int>::type = 0) {
1226  int_value = value;
1227  }
1228 
1229  template <typename T>
1230  static uint64_t type(const T &) {
1231  return ConvertToInt<T>::value ? Arg::INT : Arg::CUSTOM;
1232  }
1233 
1234  // Additional template param `Char_` is needed here because make_type always
1235  // uses char.
1236  template <typename Char_>
1237  MakeValue(const NamedArg<Char_> &value) { pointer = &value; }
1238 
1239  template <typename Char_>
1240  static uint64_t type(const NamedArg<Char_> &) { return Arg::NAMED_ARG; }
1241 };
1242 
1243 template <typename Formatter>
1244 class MakeArg : public Arg {
1245 public:
1247  type = Arg::NONE;
1248  }
1249 
1250  template <typename T>
1251  MakeArg(const T &value)
1252  : Arg(MakeValue<Formatter>(value)) {
1253  type = static_cast<Arg::Type>(MakeValue<Formatter>::type(value));
1254  }
1255 };
1256 
1257 template <typename Char>
1258 struct NamedArg : Arg {
1260 
1261  template <typename T>
1263  : Arg(MakeArg< BasicFormatter<Char> >(value)), name(argname) {}
1264 };
1265 
1266 #define FMT_DISPATCH(call) static_cast<Impl*>(this)->call
1267 
1268 // An argument visitor.
1269 // To use ArgVisitor define a subclass that implements some or all of the
1270 // visit methods with the same signatures as the methods in ArgVisitor,
1271 // for example, visit_int(int).
1272 // Specify the subclass name as the Impl template parameter. Then calling
1273 // ArgVisitor::visit for some argument will dispatch to a visit method
1274 // specific to the argument type. For example, if the argument type is
1275 // double then visit_double(double) method of a subclass will be called.
1276 // If the subclass doesn't contain a method with this signature, then
1277 // a corresponding method of ArgVisitor will be called.
1278 //
1279 // Example:
1280 // class MyArgVisitor : public ArgVisitor<MyArgVisitor, void> {
1281 // public:
1282 // void visit_int(int value) { print("{}", value); }
1283 // void visit_double(double value) { print("{}", value ); }
1284 // };
1285 //
1286 // ArgVisitor uses the curiously recurring template pattern:
1287 // http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
1288 template <typename Impl, typename Result>
1289 class ArgVisitor {
1290  public:
1292 
1294  FMT_DISPATCH(report_unhandled_arg());
1295  return Result();
1296  }
1297 
1298  Result visit_int(int value) {
1299  return FMT_DISPATCH(visit_any_int(value));
1300  }
1301  Result visit_long_long(LongLong value) {
1302  return FMT_DISPATCH(visit_any_int(value));
1303  }
1304  Result visit_uint(unsigned value) {
1305  return FMT_DISPATCH(visit_any_int(value));
1306  }
1307  Result visit_ulong_long(ULongLong value) {
1308  return FMT_DISPATCH(visit_any_int(value));
1309  }
1310  Result visit_bool(bool value) {
1311  return FMT_DISPATCH(visit_any_int(value));
1312  }
1313  Result visit_char(int value) {
1314  return FMT_DISPATCH(visit_any_int(value));
1315  }
1316  template <typename T>
1317  Result visit_any_int(T) {
1318  return FMT_DISPATCH(visit_unhandled_arg());
1319  }
1320 
1321  Result visit_double(double value) {
1322  return FMT_DISPATCH(visit_any_double(value));
1323  }
1324  Result visit_long_double(long double value) {
1325  return FMT_DISPATCH(visit_any_double(value));
1326  }
1327  template <typename T>
1328  Result visit_any_double(T) {
1329  return FMT_DISPATCH(visit_unhandled_arg());
1330  }
1331 
1332  Result visit_cstring(const char *) {
1333  return FMT_DISPATCH(visit_unhandled_arg());
1334  }
1336  return FMT_DISPATCH(visit_unhandled_arg());
1337  }
1339  return FMT_DISPATCH(visit_unhandled_arg());
1340  }
1341  Result visit_pointer(const void *) {
1342  return FMT_DISPATCH(visit_unhandled_arg());
1343  }
1345  return FMT_DISPATCH(visit_unhandled_arg());
1346  }
1347 
1348  Result visit(const Arg &arg) {
1349  switch (arg.type) {
1350  default:
1351  FMT_ASSERT(false, "invalid argument type");
1352  return Result();
1353  case Arg::INT:
1354  return FMT_DISPATCH(visit_int(arg.int_value));
1355  case Arg::UINT:
1356  return FMT_DISPATCH(visit_uint(arg.uint_value));
1357  case Arg::LONG_LONG:
1358  return FMT_DISPATCH(visit_long_long(arg.long_long_value));
1359  case Arg::ULONG_LONG:
1360  return FMT_DISPATCH(visit_ulong_long(arg.ulong_long_value));
1361  case Arg::BOOL:
1362  return FMT_DISPATCH(visit_bool(arg.int_value != 0));
1363  case Arg::CHAR:
1364  return FMT_DISPATCH(visit_char(arg.int_value));
1365  case Arg::DOUBLE:
1366  return FMT_DISPATCH(visit_double(arg.double_value));
1367  case Arg::LONG_DOUBLE:
1368  return FMT_DISPATCH(visit_long_double(arg.long_double_value));
1369  case Arg::CSTRING:
1370  return FMT_DISPATCH(visit_cstring(arg.string.value));
1371  case Arg::STRING:
1372  return FMT_DISPATCH(visit_string(arg.string));
1373  case Arg::WSTRING:
1374  return FMT_DISPATCH(visit_wstring(arg.wstring));
1375  case Arg::POINTER:
1376  return FMT_DISPATCH(visit_pointer(arg.pointer));
1377  case Arg::CUSTOM:
1378  return FMT_DISPATCH(visit_custom(arg.custom));
1379  }
1380  }
1381 };
1382 
1383 class RuntimeError : public std::runtime_error {
1384  protected:
1385  RuntimeError() : std::runtime_error("") {}
1386 };
1387 
1388 template <typename Char>
1390 
1391 template <typename Char>
1392 class ArgMap;
1393 } // namespace internal
1394 
1396 class ArgList {
1397  private:
1398  // To reduce compiled code size per formatting function call, types of first
1399  // MAX_PACKED_ARGS arguments are passed in the types_ field.
1401  union {
1402  // If the number of arguments is less than MAX_PACKED_ARGS, the argument
1403  // values are stored in values_, otherwise they are stored in args_.
1404  // This is done to reduce compiled code size as storing larger objects
1405  // may require more code (at least on x86-64) even if the same amount of
1406  // data is actually copied to stack. It saves ~10% on the bloat test.
1409  };
1410 
1411  internal::Arg::Type type(unsigned index) const {
1412  unsigned shift = index * 4;
1413  uint64_t mask = 0xf;
1414  return static_cast<internal::Arg::Type>(
1415  (types_ & (mask << shift)) >> shift);
1416  }
1417 
1418  template <typename Char>
1419  friend class internal::ArgMap;
1420 
1421  public:
1422  // Maximum number of arguments with packed types.
1423  enum { MAX_PACKED_ARGS = 16 };
1424 
1425  ArgList() : types_(0) {}
1426 
1427  ArgList(ULongLong types, const internal::Value *values)
1428  : types_(types), values_(values) {}
1429  ArgList(ULongLong types, const internal::Arg *args)
1430  : types_(types), args_(args) {}
1431 
1433  internal::Arg operator[](unsigned index) const {
1434  using internal::Arg;
1435  Arg arg;
1436  bool use_values = type(MAX_PACKED_ARGS - 1) == Arg::NONE;
1437  if (index < MAX_PACKED_ARGS) {
1438  Arg::Type arg_type = type(index);
1439  internal::Value &val = arg;
1440  if (arg_type != Arg::NONE)
1441  val = use_values ? values_[index] : args_[index];
1442  arg.type = arg_type;
1443  return arg;
1444  }
1445  if (use_values) {
1446  // The index is greater than the number of arguments that can be stored
1447  // in values, so return a "none" argument.
1448  arg.type = Arg::NONE;
1449  return arg;
1450  }
1451  for (unsigned i = MAX_PACKED_ARGS; i <= index; ++i) {
1452  if (args_[i].type == Arg::NONE)
1453  return args_[i];
1454  }
1455  return args_[index];
1456  }
1457 };
1458 
1461 };
1462 
1463 // Flags.
1464 enum {
1466  CHAR_FLAG = 0x10 // Argument has char type - used in error reporting.
1467 };
1468 
1469 // An empty format specifier.
1470 struct EmptySpec {};
1471 
1472 // A type specifier.
1473 template <char TYPE>
1475  Alignment align() const { return ALIGN_DEFAULT; }
1476  unsigned width() const { return 0; }
1477  int precision() const { return -1; }
1478  bool flag(unsigned) const { return false; }
1479  char type() const { return TYPE; }
1480  char fill() const { return ' '; }
1481 };
1482 
1483 // A width specifier.
1484 struct WidthSpec {
1485  unsigned width_;
1486  // Fill is always wchar_t and cast to char if necessary to avoid having
1487  // two specialization of WidthSpec and its subclasses.
1488  wchar_t fill_;
1489 
1490  WidthSpec(unsigned width, wchar_t fill) : width_(width), fill_(fill) {}
1491 
1492  unsigned width() const { return width_; }
1493  wchar_t fill() const { return fill_; }
1494 };
1495 
1496 // An alignment specifier.
1499 
1500  AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT)
1501  : WidthSpec(width, fill), align_(align) {}
1502 
1503  Alignment align() const { return align_; }
1504 
1505  int precision() const { return -1; }
1506 };
1507 
1508 // An alignment and type specifier.
1509 template <char TYPE>
1511  AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {}
1512 
1513  bool flag(unsigned) const { return false; }
1514  char type() const { return TYPE; }
1515 };
1516 
1517 // A full format specifier.
1519  unsigned flags_;
1521  char type_;
1522 
1524  unsigned width = 0, char type = 0, wchar_t fill = ' ')
1525  : AlignSpec(width, fill), flags_(0), precision_(-1), type_(type) {}
1526 
1527  bool flag(unsigned f) const { return (flags_ & f) != 0; }
1528  int precision() const { return precision_; }
1529  char type() const { return type_; }
1530 };
1531 
1532 // An integer format specifier.
1533 template <typename T, typename SpecT = TypeSpec<0>, typename Char = char>
1534 class IntFormatSpec : public SpecT {
1535  private:
1537 
1538  public:
1539  IntFormatSpec(T val, const SpecT &spec = SpecT())
1540  : SpecT(spec), value_(val) {}
1541 
1542  T value() const { return value_; }
1543 };
1544 
1545 // A string format specifier.
1546 template <typename Char>
1547 class StrFormatSpec : public AlignSpec {
1548  private:
1549  const Char *str_;
1550 
1551  public:
1552  template <typename FillChar>
1553  StrFormatSpec(const Char *str, unsigned width, FillChar fill)
1554  : AlignSpec(width, fill), str_(str) {
1556  }
1557 
1558  const Char *str() const { return str_; }
1559 };
1560 
1564 IntFormatSpec<int, TypeSpec<'b'> > bin(int value);
1565 
1569 IntFormatSpec<int, TypeSpec<'o'> > oct(int value);
1570 
1575 IntFormatSpec<int, TypeSpec<'x'> > hex(int value);
1576 
1581 IntFormatSpec<int, TypeSpec<'X'> > hexu(int value);
1582 
1597 template <char TYPE_CODE, typename Char>
1598 IntFormatSpec<int, AlignTypeSpec<TYPE_CODE>, Char> pad(
1599  int value, unsigned width, Char fill = ' ');
1600 
1601 #define FMT_DEFINE_INT_FORMATTERS(TYPE) \
1602 inline IntFormatSpec<TYPE, TypeSpec<'b'> > bin(TYPE value) { \
1603  return IntFormatSpec<TYPE, TypeSpec<'b'> >(value, TypeSpec<'b'>()); \
1604 } \
1605  \
1606 inline IntFormatSpec<TYPE, TypeSpec<'o'> > oct(TYPE value) { \
1607  return IntFormatSpec<TYPE, TypeSpec<'o'> >(value, TypeSpec<'o'>()); \
1608 } \
1609  \
1610 inline IntFormatSpec<TYPE, TypeSpec<'x'> > hex(TYPE value) { \
1611  return IntFormatSpec<TYPE, TypeSpec<'x'> >(value, TypeSpec<'x'>()); \
1612 } \
1613  \
1614 inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
1615  return IntFormatSpec<TYPE, TypeSpec<'X'> >(value, TypeSpec<'X'>()); \
1616 } \
1617  \
1618 template <char TYPE_CODE> \
1619 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> > pad( \
1620  IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, unsigned width) { \
1621  return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> >( \
1622  f.value(), AlignTypeSpec<TYPE_CODE>(width, ' ')); \
1623 } \
1624  \
1625 /* For compatibility with older compilers we provide two overloads for pad, */ \
1626 /* one that takes a fill character and one that doesn't. In the future this */ \
1627 /* can be replaced with one overload making the template argument Char */ \
1628 /* default to char (C++11). */ \
1629 template <char TYPE_CODE, typename Char> \
1630 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char> pad( \
1631  IntFormatSpec<TYPE, TypeSpec<TYPE_CODE>, Char> f, \
1632  unsigned width, Char fill) { \
1633  return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char>( \
1634  f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \
1635 } \
1636  \
1637 inline IntFormatSpec<TYPE, AlignTypeSpec<0> > pad( \
1638  TYPE value, unsigned width) { \
1639  return IntFormatSpec<TYPE, AlignTypeSpec<0> >( \
1640  value, AlignTypeSpec<0>(width, ' ')); \
1641 } \
1642  \
1643 template <typename Char> \
1644 inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
1645  TYPE value, unsigned width, Char fill) { \
1646  return IntFormatSpec<TYPE, AlignTypeSpec<0>, Char>( \
1647  value, AlignTypeSpec<0>(width, fill)); \
1648 }
1649 
1652 FMT_DEFINE_INT_FORMATTERS(unsigned)
1653 FMT_DEFINE_INT_FORMATTERS(unsigned long)
1654 FMT_DEFINE_INT_FORMATTERS(LongLong)
1655 FMT_DEFINE_INT_FORMATTERS(ULongLong)
1656 
1669 template <typename Char>
1670 inline StrFormatSpec<Char> pad(
1671  const Char *str, unsigned width, Char fill = ' ') {
1672  return StrFormatSpec<Char>(str, width, fill);
1673 }
1674 
1676  const wchar_t *str, unsigned width, char fill = ' ') {
1677  return StrFormatSpec<wchar_t>(str, width, fill);
1678 }
1679 
1680 namespace internal {
1681 
1682 template <typename Char>
1683 class ArgMap {
1684  private:
1685  typedef std::vector<std::pair<fmt::BasicStringRef<Char>, internal::Arg> > MapType;
1686  typedef typename MapType::value_type Pair;
1687 
1688  MapType map_;
1689 
1690  public:
1691  FMT_API void init(const ArgList &args);
1692 
1693  const internal::Arg* find(const fmt::BasicStringRef<Char> &name) const {
1694  // The list is unsorted, so just return the first matching name.
1695  for (typename MapType::const_iterator it = map_.begin(), end = map_.end();
1696  it != end; ++it) {
1697  if (it->first == name)
1698  return &it->second;
1699  }
1700  return 0;
1701  }
1702 };
1703 
1704 template <typename Impl, typename Char>
1705 class ArgFormatterBase : public ArgVisitor<Impl, void> {
1706  private:
1709 
1711 
1712  void write_pointer(const void *p) {
1713  spec_.flags_ = HASH_FLAG;
1714  spec_.type_ = 'x';
1715  writer_.write_int(reinterpret_cast<uintptr_t>(p), spec_);
1716  }
1717 
1718  protected:
1719  BasicWriter<Char> &writer() { return writer_; }
1720  FormatSpec &spec() { return spec_; }
1721 
1722  void write(bool value) {
1723  const char *str_value = value ? "true" : "false";
1724  Arg::StringValue<char> str = { str_value, std::strlen(str_value) };
1725  writer_.write_str(str, spec_);
1726  }
1727 
1728  void write(const char *value) {
1729  Arg::StringValue<char> str = {value, value != 0 ? std::strlen(value) : 0};
1730  writer_.write_str(str, spec_);
1731  }
1732 
1733  public:
1735  : writer_(w), spec_(s) {}
1736 
1737  template <typename T>
1738  void visit_any_int(T value) { writer_.write_int(value, spec_); }
1739 
1740  template <typename T>
1741  void visit_any_double(T value) { writer_.write_double(value, spec_); }
1742 
1743  void visit_bool(bool value) {
1744  if (spec_.type_)
1745  return visit_any_int(value);
1746  write(value);
1747  }
1748 
1749  void visit_char(int value) {
1750  if (spec_.type_ && spec_.type_ != 'c') {
1751  spec_.flags_ |= CHAR_FLAG;
1752  writer_.write_int(value, spec_);
1753  return;
1754  }
1755  if (spec_.align_ == ALIGN_NUMERIC || spec_.flags_ != 0)
1756  FMT_THROW(FormatError("invalid format specifier for char"));
1757  typedef typename BasicWriter<Char>::CharPtr CharPtr;
1758  Char fill = internal::CharTraits<Char>::cast(spec_.fill());
1759  CharPtr out = CharPtr();
1760  const unsigned CHAR_WIDTH = 1;
1761  if (spec_.width_ > CHAR_WIDTH) {
1762  out = writer_.grow_buffer(spec_.width_);
1763  if (spec_.align_ == ALIGN_RIGHT) {
1764  std::uninitialized_fill_n(out, spec_.width_ - CHAR_WIDTH, fill);
1765  out += spec_.width_ - CHAR_WIDTH;
1766  } else if (spec_.align_ == ALIGN_CENTER) {
1767  out = writer_.fill_padding(out, spec_.width_,
1768  internal::check(CHAR_WIDTH), fill);
1769  } else {
1770  std::uninitialized_fill_n(out + CHAR_WIDTH,
1771  spec_.width_ - CHAR_WIDTH, fill);
1772  }
1773  } else {
1774  out = writer_.grow_buffer(CHAR_WIDTH);
1775  }
1776  *out = internal::CharTraits<Char>::cast(value);
1777  }
1778 
1779  void visit_cstring(const char *value) {
1780  if (spec_.type_ == 'p')
1781  return write_pointer(value);
1782  write(value);
1783  }
1784 
1786  writer_.write_str(value, spec_);
1787  }
1788 
1790 
1792  writer_.write_str(value, spec_);
1793  }
1794 
1795  void visit_pointer(const void *value) {
1796  if (spec_.type_ && spec_.type_ != 'p')
1797  report_unknown_type(spec_.type_, "pointer");
1798  write_pointer(value);
1799  }
1800 };
1801 
1802 // An argument formatter.
1803 template <typename Char>
1805  public ArgFormatterBase<BasicArgFormatter<Char>, Char> {
1806  private:
1808  const Char *format_;
1809 
1810  public:
1812  : ArgFormatterBase<BasicArgFormatter<Char>, Char>(f.writer(), s),
1813  formatter_(f), format_(fmt) {}
1814 
1816  c.format(&formatter_, c.value, &format_);
1817  }
1818 };
1819 
1821  private:
1824 
1825  // Returns the argument with specified index.
1826  FMT_API Arg do_get_arg(unsigned arg_index, const char *&error);
1827 
1828  protected:
1829  const ArgList &args() const { return args_; }
1830 
1831  explicit FormatterBase(const ArgList &args) {
1832  args_ = args;
1833  next_arg_index_ = 0;
1834  }
1835 
1836  // Returns the next argument.
1837  Arg next_arg(const char *&error) {
1838  if (next_arg_index_ >= 0)
1839  return do_get_arg(internal::to_unsigned(next_arg_index_++), error);
1840  error = "cannot switch from manual to automatic argument indexing";
1841  return Arg();
1842  }
1843 
1844  // Checks if manual indexing is used and returns the argument with
1845  // specified index.
1846  Arg get_arg(unsigned arg_index, const char *&error) {
1847  return check_no_auto_index(error) ? do_get_arg(arg_index, error) : Arg();
1848  }
1849 
1850  bool check_no_auto_index(const char *&error) {
1851  if (next_arg_index_ > 0) {
1852  error = "cannot switch from automatic to manual argument indexing";
1853  return false;
1854  }
1855  next_arg_index_ = -1;
1856  return true;
1857  }
1858 
1859  template <typename Char>
1860  void write(BasicWriter<Char> &w, const Char *start, const Char *end) {
1861  if (start != end)
1862  w << BasicStringRef<Char>(start, internal::to_unsigned(end - start));
1863  }
1864 };
1865 
1866 // A printf formatter.
1867 template <typename Char>
1869  private:
1870  void parse_flags(FormatSpec &spec, const Char *&s);
1871 
1872  // Returns the argument with specified index or, if arg_index is equal
1873  // to the maximum unsigned value, the next argument.
1874  Arg get_arg(const Char *s,
1875  unsigned arg_index = (std::numeric_limits<unsigned>::max)());
1876 
1877  // Parses argument index, flags and width and returns the argument index.
1878  unsigned parse_header(const Char *&s, FormatSpec &spec);
1879 
1880  public:
1881  explicit PrintfFormatter(const ArgList &args) : FormatterBase(args) {}
1882  FMT_API void format(BasicWriter<Char> &writer,
1883  BasicCStringRef<Char> format_str);
1884 };
1885 } // namespace internal
1886 
1888 template <typename CharType>
1889 class BasicFormatter : private internal::FormatterBase {
1890  public:
1892  typedef CharType Char;
1893 
1894  private:
1897 
1899 
1900  using internal::FormatterBase::get_arg;
1901 
1902  // Checks if manual indexing is used and returns the argument with
1903  // specified name.
1904  internal::Arg get_arg(BasicStringRef<Char> arg_name, const char *&error);
1905 
1906  // Parses argument index and returns corresponding argument.
1907  internal::Arg parse_arg_index(const Char *&s);
1908 
1909  // Parses argument name and returns corresponding argument.
1910  internal::Arg parse_arg_name(const Char *&s);
1911 
1912  public:
1921  : internal::FormatterBase(args), writer_(w) {}
1922 
1924  BasicWriter<Char> &writer() { return writer_; }
1925 
1927  void format(BasicCStringRef<Char> format_str);
1928 
1929  // Formats a single argument and advances format_str, a format string pointer.
1930  const Char *format(const Char *&format_str, const internal::Arg &arg);
1931 };
1932 
1933 // Generates a comma-separated list with results of applying f to
1934 // numbers 0..n-1.
1935 # define FMT_GEN(n, f) FMT_GEN##n(f)
1936 # define FMT_GEN1(f) f(0)
1937 # define FMT_GEN2(f) FMT_GEN1(f), f(1)
1938 # define FMT_GEN3(f) FMT_GEN2(f), f(2)
1939 # define FMT_GEN4(f) FMT_GEN3(f), f(3)
1940 # define FMT_GEN5(f) FMT_GEN4(f), f(4)
1941 # define FMT_GEN6(f) FMT_GEN5(f), f(5)
1942 # define FMT_GEN7(f) FMT_GEN6(f), f(6)
1943 # define FMT_GEN8(f) FMT_GEN7(f), f(7)
1944 # define FMT_GEN9(f) FMT_GEN8(f), f(8)
1945 # define FMT_GEN10(f) FMT_GEN9(f), f(9)
1946 # define FMT_GEN11(f) FMT_GEN10(f), f(10)
1947 # define FMT_GEN12(f) FMT_GEN11(f), f(11)
1948 # define FMT_GEN13(f) FMT_GEN12(f), f(12)
1949 # define FMT_GEN14(f) FMT_GEN13(f), f(13)
1950 # define FMT_GEN15(f) FMT_GEN14(f), f(14)
1951 
1952 namespace internal {
1953 inline uint64_t make_type() { return 0; }
1954 
1955 template <typename T>
1956 inline uint64_t make_type(const T &arg) {
1957  return MakeValue< BasicFormatter<char> >::type(arg);
1958 }
1959 
1960 template <unsigned N, bool/*IsPacked*/= (N < ArgList::MAX_PACKED_ARGS)>
1961 struct ArgArray;
1962 
1963 template <unsigned N>
1964 struct ArgArray<N, true/*IsPacked*/> {
1965  typedef Value Type[N > 0 ? N : 1];
1966 
1967  template <typename Formatter, typename T>
1968  static Value make(const T &value) {
1969  Value result = MakeValue<Formatter>(value);
1970  // Workaround a bug in Apple LLVM version 4.2 (clang-425.0.28) of clang:
1971  // https://github.com/cppformat/cppformat/issues/276
1972  (void)result.custom.format;
1973  return result;
1974  }
1975 };
1976 
1977 template <unsigned N>
1978 struct ArgArray<N, false/*IsPacked*/> {
1979  typedef Arg Type[N + 1]; // +1 for the list end Arg::NONE
1980 
1981  template <typename Formatter, typename T>
1982  static Arg make(const T &value) { return MakeArg<Formatter>(value); }
1983 };
1984 
1985 #if FMT_USE_VARIADIC_TEMPLATES
1986 template <typename Arg, typename... Args>
1987 inline uint64_t make_type(const Arg &first, const Args & ... tail) {
1988  return make_type(first) | (make_type(tail...) << 4);
1989 }
1990 
1991 #else
1992 
1993 struct ArgType {
1995 
1996  ArgType() : type(0) {}
1997 
1998  template <typename T>
1999  ArgType(const T &arg) : type(make_type(arg)) {}
2000 };
2001 
2002 # define FMT_ARG_TYPE_DEFAULT(n) ArgType t##n = ArgType()
2003 
2005  return t0.type | (t1.type << 4) | (t2.type << 8) | (t3.type << 12) |
2006  (t4.type << 16) | (t5.type << 20) | (t6.type << 24) | (t7.type << 28) |
2007  (t8.type << 32) | (t9.type << 36) | (t10.type << 40) | (t11.type << 44) |
2008  (t12.type << 48) | (t13.type << 52) | (t14.type << 56);
2009 }
2010 #endif
2011 
2012 template <class Char>
2013 class FormatBuf : public std::basic_streambuf<Char> {
2014  private:
2015  typedef typename std::basic_streambuf<Char>::int_type int_type;
2016  typedef typename std::basic_streambuf<Char>::traits_type traits_type;
2017 
2019  Char *start_;
2020 
2021  public:
2022  FormatBuf(Buffer<Char> &buffer) : buffer_(buffer), start_(&buffer[0]) {
2023  this->setp(start_, start_ + buffer_.capacity());
2024  }
2025 
2026  int_type overflow(int_type ch = traits_type::eof()) {
2027  if (!traits_type::eq_int_type(ch, traits_type::eof())) {
2028  size_t size = this->size();
2029  buffer_.resize(size);
2030  buffer_.reserve(size * 2);
2031 
2032  start_ = &buffer_[0];
2033  start_[size] = traits_type::to_char_type(ch);
2034  this->setp(start_+ size + 1, start_ + size * 2);
2035  }
2036  return ch;
2037  }
2038 
2039  size_t size() const {
2040  return to_unsigned(this->pptr() - start_);
2041  }
2042 };
2043 } // namespace internal
2044 
2045 # define FMT_MAKE_TEMPLATE_ARG(n) typename T##n
2046 # define FMT_MAKE_ARG_TYPE(n) T##n
2047 # define FMT_MAKE_ARG(n) const T##n &v##n
2048 # define FMT_ASSIGN_char(n) \
2049  arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter<char> >(v##n)
2050 # define FMT_ASSIGN_wchar_t(n) \
2051  arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter<wchar_t> >(v##n)
2052 
2053 #if FMT_USE_VARIADIC_TEMPLATES
2054 // Defines a variadic function returning void.
2055 # define FMT_VARIADIC_VOID(func, arg_type) \
2056  template <typename... Args> \
2057  void func(arg_type arg0, const Args & ... args) { \
2058  typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
2059  typename ArgArray::Type array{ \
2060  ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
2061  func(arg0, fmt::ArgList(fmt::internal::make_type(args...), array)); \
2062  }
2063 
2064 // Defines a variadic constructor.
2065 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
2066  template <typename... Args> \
2067  ctor(arg0_type arg0, arg1_type arg1, const Args & ... args) { \
2068  typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
2069  typename ArgArray::Type array{ \
2070  ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
2071  func(arg0, arg1, fmt::ArgList(fmt::internal::make_type(args...), array)); \
2072  }
2073 
2074 #else
2075 
2076 # define FMT_MAKE_REF(n) \
2077  fmt::internal::MakeValue< fmt::BasicFormatter<Char> >(v##n)
2078 # define FMT_MAKE_REF2(n) v##n
2079 
2080 // Defines a wrapper for a function taking one argument of type arg_type
2081 // and n additional arguments of arbitrary types.
2082 # define FMT_WRAP1(func, arg_type, n) \
2083  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2084  inline void func(arg_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
2085  const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
2086  func(arg1, fmt::ArgList( \
2087  fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
2088  }
2089 
2090 // Emulates a variadic function returning void on a pre-C++11 compiler.
2091 # define FMT_VARIADIC_VOID(func, arg_type) \
2092  inline void func(arg_type arg) { func(arg, fmt::ArgList()); } \
2093  FMT_WRAP1(func, arg_type, 1) FMT_WRAP1(func, arg_type, 2) \
2094  FMT_WRAP1(func, arg_type, 3) FMT_WRAP1(func, arg_type, 4) \
2095  FMT_WRAP1(func, arg_type, 5) FMT_WRAP1(func, arg_type, 6) \
2096  FMT_WRAP1(func, arg_type, 7) FMT_WRAP1(func, arg_type, 8) \
2097  FMT_WRAP1(func, arg_type, 9) FMT_WRAP1(func, arg_type, 10)
2098 
2099 # define FMT_CTOR(ctor, func, arg0_type, arg1_type, n) \
2100  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2101  ctor(arg0_type arg0, arg1_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
2102  const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
2103  func(arg0, arg1, fmt::ArgList( \
2104  fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
2105  }
2106 
2107 // Emulates a variadic constructor on a pre-C++11 compiler.
2108 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
2109  FMT_CTOR(ctor, func, arg0_type, arg1_type, 1) \
2110  FMT_CTOR(ctor, func, arg0_type, arg1_type, 2) \
2111  FMT_CTOR(ctor, func, arg0_type, arg1_type, 3) \
2112  FMT_CTOR(ctor, func, arg0_type, arg1_type, 4) \
2113  FMT_CTOR(ctor, func, arg0_type, arg1_type, 5) \
2114  FMT_CTOR(ctor, func, arg0_type, arg1_type, 6) \
2115  FMT_CTOR(ctor, func, arg0_type, arg1_type, 7) \
2116  FMT_CTOR(ctor, func, arg0_type, arg1_type, 8) \
2117  FMT_CTOR(ctor, func, arg0_type, arg1_type, 9) \
2118  FMT_CTOR(ctor, func, arg0_type, arg1_type, 10)
2119 #endif
2120 
2121 // Generates a comma-separated list with results of applying f to pairs
2122 // (argument, index).
2123 #define FMT_FOR_EACH1(f, x0) f(x0, 0)
2124 #define FMT_FOR_EACH2(f, x0, x1) \
2125  FMT_FOR_EACH1(f, x0), f(x1, 1)
2126 #define FMT_FOR_EACH3(f, x0, x1, x2) \
2127  FMT_FOR_EACH2(f, x0 ,x1), f(x2, 2)
2128 #define FMT_FOR_EACH4(f, x0, x1, x2, x3) \
2129  FMT_FOR_EACH3(f, x0, x1, x2), f(x3, 3)
2130 #define FMT_FOR_EACH5(f, x0, x1, x2, x3, x4) \
2131  FMT_FOR_EACH4(f, x0, x1, x2, x3), f(x4, 4)
2132 #define FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5) \
2133  FMT_FOR_EACH5(f, x0, x1, x2, x3, x4), f(x5, 5)
2134 #define FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6) \
2135  FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5), f(x6, 6)
2136 #define FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7) \
2137  FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6), f(x7, 7)
2138 #define FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8) \
2139  FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7), f(x8, 8)
2140 #define FMT_FOR_EACH10(f, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) \
2141  FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8), f(x9, 9)
2142 
2148  private:
2149  void init(int err_code, CStringRef format_str, ArgList args);
2150 
2151  protected:
2153 
2154  typedef char Char; // For FMT_VARIADIC_CTOR.
2155 
2157 
2158  public:
2184  SystemError(int error_code, CStringRef message) {
2185  init(error_code, message, ArgList());
2186  }
2187  FMT_VARIADIC_CTOR(SystemError, init, int, CStringRef)
2188 
2189  int error_code() const { return error_code_; }
2190 };
2191 
2210 template <typename Char>
2211 class BasicWriter {
2212  private:
2213  // Output buffer.
2215 
2217 
2219 
2220 #if FMT_SECURE_SCL
2221  // Returns pointer value.
2222  static Char *get(CharPtr p) { return p.base(); }
2223 #else
2224  static Char *get(Char *p) { return p; }
2225 #endif
2226 
2227  // Fills the padding around the content and returns the pointer to the
2228  // content area.
2229  static CharPtr fill_padding(CharPtr buffer,
2230  unsigned total_size, std::size_t content_size, wchar_t fill);
2231 
2232  // Grows the buffer by n characters and returns a pointer to the newly
2233  // allocated area.
2234  CharPtr grow_buffer(std::size_t n) {
2235  std::size_t size = buffer_.size();
2236  buffer_.resize(size + n);
2237  return internal::make_ptr(&buffer_[size], n);
2238  }
2239 
2240  // Writes an unsigned decimal integer.
2241  template <typename UInt>
2242  Char *write_unsigned_decimal(UInt value, unsigned prefix_size = 0) {
2243  unsigned num_digits = internal::count_digits(value);
2244  Char *ptr = get(grow_buffer(prefix_size + num_digits));
2245  internal::format_decimal(ptr + prefix_size, value, num_digits);
2246  return ptr;
2247  }
2248 
2249  // Writes a decimal integer.
2250  template <typename Int>
2251  void write_decimal(Int value) {
2252  typedef typename internal::IntTraits<Int>::MainType MainType;
2253  MainType abs_value = static_cast<MainType>(value);
2254  if (internal::is_negative(value)) {
2255  abs_value = 0 - abs_value;
2256  *write_unsigned_decimal(abs_value, 1) = '-';
2257  } else {
2258  write_unsigned_decimal(abs_value, 0);
2259  }
2260  }
2261 
2262  // Prepare a buffer for integer formatting.
2263  CharPtr prepare_int_buffer(unsigned num_digits,
2264  const EmptySpec &, const char *prefix, unsigned prefix_size) {
2265  unsigned size = prefix_size + num_digits;
2266  CharPtr p = grow_buffer(size);
2267  std::uninitialized_copy(prefix, prefix + prefix_size, p);
2268  return p + size - 1;
2269  }
2270 
2271  template <typename Spec>
2272  CharPtr prepare_int_buffer(unsigned num_digits,
2273  const Spec &spec, const char *prefix, unsigned prefix_size);
2274 
2275  // Formats an integer.
2276  template <typename T, typename Spec>
2277  void write_int(T value, Spec spec);
2278 
2279  // Formats a floating-point number (double or long double).
2280  template <typename T>
2281  void write_double(T value, const FormatSpec &spec);
2282 
2283  // Writes a formatted string.
2284  template <typename StrChar>
2285  CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec);
2286 
2287  template <typename StrChar>
2288  void write_str(const internal::Arg::StringValue<StrChar> &str,
2289  const FormatSpec &spec);
2290 
2291  // This following methods are private to disallow writing wide characters
2292  // and strings to a char stream. If you want to print a wide string as a
2293  // pointer as std::ostream does, cast it to const void*.
2294  // Do not implement!
2295  void operator<<(typename internal::WCharHelper<wchar_t, Char>::Unsupported);
2296  void operator<<(
2298 
2299  // Appends floating-point length specifier to the format string.
2300  // The second argument is only used for overload resolution.
2301  void append_float_length(Char *&format_ptr, long double) {
2302  *format_ptr++ = 'L';
2303  }
2304 
2305  template<typename T>
2306  void append_float_length(Char *&, T) {}
2307 
2308  template <typename Impl, typename Char_>
2310 
2311  friend class internal::PrintfArgFormatter<Char>;
2312 
2313  protected:
2317  explicit BasicWriter(Buffer<Char> &b) : buffer_(b) {}
2318 
2319  public:
2325  virtual ~BasicWriter() {}
2326 
2330  std::size_t size() const { return buffer_.size(); }
2331 
2336  const Char *data() const FMT_NOEXCEPT { return &buffer_[0]; }
2337 
2342  const Char *c_str() const {
2343  std::size_t size = buffer_.size();
2344  buffer_.reserve(size + 1);
2345  buffer_[size] = '\0';
2346  return &buffer_[0];
2347  }
2348 
2354  std::basic_string<Char> str() const {
2355  return std::basic_string<Char>(&buffer_[0], buffer_.size());
2356  }
2357 
2384  BasicFormatter<Char>(args, *this).format(format);
2385  }
2387 
2388  BasicWriter &operator<<(int value) {
2389  write_decimal(value);
2390  return *this;
2391  }
2393  return *this << IntFormatSpec<unsigned>(value);
2394  }
2396  write_decimal(value);
2397  return *this;
2398  }
2399  BasicWriter &operator<<(unsigned long value) {
2400  return *this << IntFormatSpec<unsigned long>(value);
2401  }
2403  write_decimal(value);
2404  return *this;
2405  }
2406 
2413  return *this << IntFormatSpec<ULongLong>(value);
2414  }
2415 
2417  write_double(value, FormatSpec());
2418  return *this;
2419  }
2420 
2427  BasicWriter &operator<<(long double value) {
2428  write_double(value, FormatSpec());
2429  return *this;
2430  }
2431 
2436  buffer_.push_back(value);
2437  return *this;
2438  }
2439 
2442  buffer_.push_back(value);
2443  return *this;
2444  }
2445 
2451  BasicWriter &operator<<(fmt::BasicStringRef<Char> value) {
2452  const Char *str = value.data();
2453  buffer_.append(str, str + value.size());
2454  return *this;
2455  }
2456 
2459  const char *str = value.data();
2460  buffer_.append(str, str + value.size());
2461  return *this;
2462  }
2463 
2464  template <typename T, typename Spec, typename FillChar>
2465  BasicWriter &operator<<(IntFormatSpec<T, Spec, FillChar> spec) {
2467  write_int(spec.value(), spec);
2468  return *this;
2469  }
2470 
2471  template <typename StrChar>
2472  BasicWriter &operator<<(const StrFormatSpec<StrChar> &spec) {
2473  const StrChar *s = spec.str();
2474  write_str(s, std::char_traits<Char>::length(s), spec);
2475  return *this;
2476  }
2477 
2478  void clear() FMT_NOEXCEPT { buffer_.clear(); }
2479 };
2480 
2481 template <typename Char>
2482 template <typename StrChar>
2484  const StrChar *s, std::size_t size, const AlignSpec &spec) {
2485  CharPtr out = CharPtr();
2486  if (spec.width() > size) {
2487  out = grow_buffer(spec.width());
2488  Char fill = internal::CharTraits<Char>::cast(spec.fill());
2489  if (spec.align() == ALIGN_RIGHT) {
2490  std::uninitialized_fill_n(out, spec.width() - size, fill);
2491  out += spec.width() - size;
2492  } else if (spec.align() == ALIGN_CENTER) {
2493  out = fill_padding(out, spec.width(), size, fill);
2494  } else {
2495  std::uninitialized_fill_n(out + size, spec.width() - size, fill);
2496  }
2497  } else {
2498  out = grow_buffer(size);
2499  }
2500  std::uninitialized_copy(s, s + size, out);
2501  return out;
2502 }
2503 
2504 template <typename Char>
2505 template <typename StrChar>
2507  const internal::Arg::StringValue<StrChar> &s, const FormatSpec &spec) {
2508  // Check if StrChar is convertible to Char.
2510  if (spec.type_ && spec.type_ != 's')
2511  internal::report_unknown_type(spec.type_, "string");
2512  const StrChar *str_value = s.value;
2513  std::size_t str_size = s.size;
2514  if (str_size == 0) {
2515  if (!str_value) {
2516  FMT_THROW(FormatError("string pointer is null"));
2517  return;
2518  }
2519  }
2520  std::size_t precision = static_cast<std::size_t>(spec.precision_);
2521  if (spec.precision_ >= 0 && precision < str_size)
2522  str_size = precision;
2523  write_str(str_value, str_size, spec);
2524 }
2525 
2526 template <typename Char>
2529  CharPtr buffer, unsigned total_size,
2530  std::size_t content_size, wchar_t fill) {
2531  std::size_t padding = total_size - content_size;
2532  std::size_t left_padding = padding / 2;
2533  Char fill_char = internal::CharTraits<Char>::cast(fill);
2534  std::uninitialized_fill_n(buffer, left_padding, fill_char);
2535  buffer += left_padding;
2536  CharPtr content = buffer;
2537  std::uninitialized_fill_n(buffer + content_size,
2538  padding - left_padding, fill_char);
2539  return content;
2540 }
2541 
2542 template <typename Char>
2543 template <typename Spec>
2546  unsigned num_digits, const Spec &spec,
2547  const char *prefix, unsigned prefix_size) {
2548  unsigned width = spec.width();
2549  Alignment align = spec.align();
2550  Char fill = internal::CharTraits<Char>::cast(spec.fill());
2551  if (spec.precision() > static_cast<int>(num_digits)) {
2552  // Octal prefix '0' is counted as a digit, so ignore it if precision
2553  // is specified.
2554  if (prefix_size > 0 && prefix[prefix_size - 1] == '0')
2555  --prefix_size;
2556  unsigned number_size =
2557  prefix_size + internal::to_unsigned(spec.precision());
2558  AlignSpec subspec(number_size, '0', ALIGN_NUMERIC);
2559  if (number_size >= width)
2560  return prepare_int_buffer(num_digits, subspec, prefix, prefix_size);
2561  buffer_.reserve(width);
2562  unsigned fill_size = width - number_size;
2563  if (align != ALIGN_LEFT) {
2564  CharPtr p = grow_buffer(fill_size);
2565  std::uninitialized_fill(p, p + fill_size, fill);
2566  }
2567  CharPtr result = prepare_int_buffer(
2568  num_digits, subspec, prefix, prefix_size);
2569  if (align == ALIGN_LEFT) {
2570  CharPtr p = grow_buffer(fill_size);
2571  std::uninitialized_fill(p, p + fill_size, fill);
2572  }
2573  return result;
2574  }
2575  unsigned size = prefix_size + num_digits;
2576  if (width <= size) {
2577  CharPtr p = grow_buffer(size);
2578  std::uninitialized_copy(prefix, prefix + prefix_size, p);
2579  return p + size - 1;
2580  }
2581  CharPtr p = grow_buffer(width);
2582  CharPtr end = p + width;
2583  if (align == ALIGN_LEFT) {
2584  std::uninitialized_copy(prefix, prefix + prefix_size, p);
2585  p += size;
2586  std::uninitialized_fill(p, end, fill);
2587  } else if (align == ALIGN_CENTER) {
2588  p = fill_padding(p, width, size, fill);
2589  std::uninitialized_copy(prefix, prefix + prefix_size, p);
2590  p += size;
2591  } else {
2592  if (align == ALIGN_NUMERIC) {
2593  if (prefix_size != 0) {
2594  p = std::uninitialized_copy(prefix, prefix + prefix_size, p);
2595  size -= prefix_size;
2596  }
2597  } else {
2598  std::uninitialized_copy(prefix, prefix + prefix_size, end - size);
2599  }
2600  std::uninitialized_fill(p, end - size, fill);
2601  p = end;
2602  }
2603  return p - 1;
2604 }
2605 
2606 template <typename Char>
2607 template <typename T, typename Spec>
2609  unsigned prefix_size = 0;
2610  typedef typename internal::IntTraits<T>::MainType UnsignedType;
2611  UnsignedType abs_value = static_cast<UnsignedType>(value);
2612  char prefix[4] = "";
2613  if (internal::is_negative(value)) {
2614  prefix[0] = '-';
2615  ++prefix_size;
2616  abs_value = 0 - abs_value;
2617  } else if (spec.flag(SIGN_FLAG)) {
2618  prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' ';
2619  ++prefix_size;
2620  }
2621  switch (spec.type()) {
2622  case 0: case 'd': {
2623  unsigned num_digits = internal::count_digits(abs_value);
2624  CharPtr p = prepare_int_buffer(
2625  num_digits, spec, prefix, prefix_size) + 1 - num_digits;
2626  internal::format_decimal(get(p), abs_value, num_digits);
2627  break;
2628  }
2629  case 'x': case 'X': {
2630  UnsignedType n = abs_value;
2631  if (spec.flag(HASH_FLAG)) {
2632  prefix[prefix_size++] = '0';
2633  prefix[prefix_size++] = spec.type();
2634  }
2635  unsigned num_digits = 0;
2636  do {
2637  ++num_digits;
2638  } while ((n >>= 4) != 0);
2639  Char *p = get(prepare_int_buffer(
2640  num_digits, spec, prefix, prefix_size));
2641  n = abs_value;
2642  const char *digits = spec.type() == 'x' ?
2643  "0123456789abcdef" : "0123456789ABCDEF";
2644  do {
2645  *p-- = digits[n & 0xf];
2646  } while ((n >>= 4) != 0);
2647  break;
2648  }
2649  case 'b': case 'B': {
2650  UnsignedType n = abs_value;
2651  if (spec.flag(HASH_FLAG)) {
2652  prefix[prefix_size++] = '0';
2653  prefix[prefix_size++] = spec.type();
2654  }
2655  unsigned num_digits = 0;
2656  do {
2657  ++num_digits;
2658  } while ((n >>= 1) != 0);
2659  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
2660  n = abs_value;
2661  do {
2662  *p-- = static_cast<Char>('0' + (n & 1));
2663  } while ((n >>= 1) != 0);
2664  break;
2665  }
2666  case 'o': {
2667  UnsignedType n = abs_value;
2668  if (spec.flag(HASH_FLAG))
2669  prefix[prefix_size++] = '0';
2670  unsigned num_digits = 0;
2671  do {
2672  ++num_digits;
2673  } while ((n >>= 3) != 0);
2674  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
2675  n = abs_value;
2676  do {
2677  *p-- = static_cast<Char>('0' + (n & 7));
2678  } while ((n >>= 3) != 0);
2679  break;
2680  }
2681  default:
2683  spec.type(), spec.flag(CHAR_FLAG) ? "char" : "integer");
2684  break;
2685  }
2686 }
2687 
2688 template <typename Char>
2689 template <typename T>
2691  // Check type.
2692  char type = spec.type();
2693  bool upper = false;
2694  switch (type) {
2695  case 0:
2696  type = 'g';
2697  break;
2698  case 'e': case 'f': case 'g': case 'a':
2699  break;
2700  case 'F':
2701 #ifdef _MSC_VER
2702  // MSVC's printf doesn't support 'F'.
2703  type = 'f';
2704 #endif
2705  // Fall through.
2706  case 'E': case 'G': case 'A':
2707  upper = true;
2708  break;
2709  default:
2710  internal::report_unknown_type(type, "double");
2711  break;
2712  }
2713 
2714  char sign = 0;
2715  // Use isnegative instead of value < 0 because the latter is always
2716  // false for NaN.
2717  if (internal::FPUtil::isnegative(static_cast<double>(value))) {
2718  sign = '-';
2719  value = -value;
2720  } else if (spec.flag(SIGN_FLAG)) {
2721  sign = spec.flag(PLUS_FLAG) ? '+' : ' ';
2722  }
2723 
2724  if (internal::FPUtil::isnotanumber(value)) {
2725  // Format NaN ourselves because sprintf's output is not consistent
2726  // across platforms.
2727  std::size_t nan_size = 4;
2728  const char *nan = upper ? " NAN" : " nan";
2729  if (!sign) {
2730  --nan_size;
2731  ++nan;
2732  }
2733  CharPtr out = write_str(nan, nan_size, spec);
2734  if (sign)
2735  *out = sign;
2736  return;
2737  }
2738 
2739  if (internal::FPUtil::isinfinity(value)) {
2740  // Format infinity ourselves because sprintf's output is not consistent
2741  // across platforms.
2742  std::size_t inf_size = 4;
2743  const char *inf = upper ? " INF" : " inf";
2744  if (!sign) {
2745  --inf_size;
2746  ++inf;
2747  }
2748  CharPtr out = write_str(inf, inf_size, spec);
2749  if (sign)
2750  *out = sign;
2751  return;
2752  }
2753 
2754  std::size_t offset = buffer_.size();
2755  unsigned width = spec.width();
2756  if (sign) {
2757  buffer_.reserve(buffer_.size() + (width > 1u ? width : 1u));
2758  if (width > 0)
2759  --width;
2760  ++offset;
2761  }
2762 
2763  // Build format string.
2764  enum { MAX_FORMAT_SIZE = 10}; // longest format: %#-*.*Lg
2765  Char format[MAX_FORMAT_SIZE];
2766  Char *format_ptr = format;
2767  *format_ptr++ = '%';
2768  unsigned width_for_sprintf = width;
2769  if (spec.flag(HASH_FLAG))
2770  *format_ptr++ = '#';
2771  if (spec.align() == ALIGN_CENTER) {
2772  width_for_sprintf = 0;
2773  } else {
2774  if (spec.align() == ALIGN_LEFT)
2775  *format_ptr++ = '-';
2776  if (width != 0)
2777  *format_ptr++ = '*';
2778  }
2779  if (spec.precision() >= 0) {
2780  *format_ptr++ = '.';
2781  *format_ptr++ = '*';
2782  }
2783 
2784  append_float_length(format_ptr, value);
2785  *format_ptr++ = type;
2786  *format_ptr = '\0';
2787 
2788  // Format using snprintf.
2789  Char fill = internal::CharTraits<Char>::cast(spec.fill());
2790  unsigned n = 0;
2791  Char *start = 0;
2792  for (;;) {
2793  std::size_t buffer_size = buffer_.capacity() - offset;
2794 #ifdef _MSC_VER
2795  // MSVC's vsnprintf_s doesn't work with zero size, so reserve
2796  // space for at least one extra character to make the size non-zero.
2797  // Note that the buffer's capacity will increase by more than 1.
2798  if (buffer_size == 0) {
2799  buffer_.reserve(offset + 1);
2800  buffer_size = buffer_.capacity() - offset;
2801  }
2802 #endif
2803  start = &buffer_[offset];
2805  start, buffer_size, format, width_for_sprintf, spec.precision(), value);
2806  if (result >= 0) {
2807  n = internal::to_unsigned(result);
2808  if (offset + n < buffer_.capacity())
2809  break; // The buffer is large enough - continue with formatting.
2810  buffer_.reserve(offset + n + 1);
2811  } else {
2812  // If result is negative we ask to increase the capacity by at least 1,
2813  // but as std::vector, the buffer grows exponentially.
2814  buffer_.reserve(buffer_.capacity() + 1);
2815  }
2816  }
2817  if (sign) {
2818  if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) ||
2819  *start != ' ') {
2820  *(start - 1) = sign;
2821  sign = 0;
2822  } else {
2823  *(start - 1) = fill;
2824  }
2825  ++n;
2826  }
2827  if (spec.align() == ALIGN_CENTER && spec.width() > n) {
2828  width = spec.width();
2829  CharPtr p = grow_buffer(width);
2830  std::memmove(get(p) + (width - n) / 2, get(p), n * sizeof(Char));
2831  fill_padding(p, spec.width(), n, fill);
2832  return;
2833  }
2834  if (spec.fill() != ' ' || sign) {
2835  while (*start == ' ')
2836  *start++ = fill;
2837  if (sign)
2838  *(start - 1) = sign;
2839  }
2840  grow_buffer(n);
2841 }
2842 
2877 template <typename Char, typename Allocator = std::allocator<Char> >
2878 class BasicMemoryWriter : public BasicWriter<Char> {
2879  private:
2881 
2882  public:
2883  explicit BasicMemoryWriter(const Allocator& alloc = Allocator())
2884  : BasicWriter<Char>(buffer_), buffer_(alloc) {}
2885 
2886 #if FMT_USE_RVALUE_REFERENCES
2887 
2894  : BasicWriter<Char>(buffer_), buffer_(std::move(other.buffer_)) {
2895  }
2896 
2902  BasicMemoryWriter &operator=(BasicMemoryWriter &&other) {
2903  buffer_ = std::move(other.buffer_);
2904  return *this;
2905  }
2906 #endif
2907 };
2908 
2911 
2932 template <typename Char>
2933 class BasicArrayWriter : public BasicWriter<Char> {
2934  private:
2936 
2937  public:
2944  BasicArrayWriter(Char *array, std::size_t size)
2945  : BasicWriter<Char>(buffer_), buffer_(array, size) {}
2946 
2953  template <std::size_t SIZE>
2954  explicit BasicArrayWriter(Char (&array)[SIZE])
2955  : BasicWriter<Char>(buffer_), buffer_(array, SIZE) {}
2956 };
2957 
2960 
2961 // Formats a value.
2962 template <typename Char, typename T>
2963 void format(BasicFormatter<Char> &f, const Char *&format_str, const T &value) {
2965 
2966  internal::FormatBuf<Char> format_buf(buffer);
2967  std::basic_ostream<Char> output(&format_buf);
2968  output << value;
2969 
2970  BasicStringRef<Char> str(&buffer[0], format_buf.size());
2972  format_str = f.format(format_str, MakeArg(str));
2973 }
2974 
2975 // Reports a system error without throwing an exception.
2976 // Can be used to report errors from destructors.
2977 FMT_API void report_system_error(int error_code,
2978  StringRef message) FMT_NOEXCEPT;
2979 
2980 #if FMT_USE_WINDOWS_H
2981 
2983 class WindowsError : public SystemError {
2984  private:
2985  FMT_API void init(int error_code, CStringRef format_str, ArgList args);
2986 
2987  public:
3016  WindowsError(int error_code, CStringRef message) {
3017  init(error_code, message, ArgList());
3018  }
3019  FMT_VARIADIC_CTOR(WindowsError, init, int, CStringRef)
3020 };
3021 
3022 // Reports a Windows error without throwing an exception.
3023 // Can be used to report errors from destructors.
3024 FMT_API void report_windows_error(int error_code,
3025  StringRef message) FMT_NOEXCEPT;
3026 
3027 #endif
3028 
3030 
3037 FMT_API void print_colored(Color c, CStringRef format, ArgList args);
3038 
3048 inline std::string format(CStringRef format_str, ArgList args) {
3049  MemoryWriter w;
3050  w.write(format_str, args);
3051  return w.str();
3052 }
3053 
3054 inline std::wstring format(WCStringRef format_str, ArgList args) {
3055  WMemoryWriter w;
3056  w.write(format_str, args);
3057  return w.str();
3058 }
3059 
3069 FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args);
3070 
3080 FMT_API void print(CStringRef format_str, ArgList args);
3081 
3082 template <typename Char>
3084  internal::PrintfFormatter<Char>(args).format(w, format);
3085 }
3086 
3096 inline std::string sprintf(CStringRef format, ArgList args) {
3097  MemoryWriter w;
3098  printf(w, format, args);
3099  return w.str();
3100 }
3101 
3102 inline std::wstring sprintf(WCStringRef format, ArgList args) {
3103  WMemoryWriter w;
3104  printf(w, format, args);
3105  return w.str();
3106 }
3107 
3117 FMT_API int fprintf(std::FILE *f, CStringRef format, ArgList args);
3118 
3128 inline int printf(CStringRef format, ArgList args) {
3129  return fprintf(stdout, format, args);
3130 }
3131 
3135 class FormatInt {
3136  private:
3137  // Buffer should be large enough to hold all digits (digits10 + 1),
3138  // a sign and a null character.
3139  enum {BUFFER_SIZE = std::numeric_limits<ULongLong>::digits10 + 3};
3140  mutable char buffer_[BUFFER_SIZE];
3141  char *str_;
3142 
3143  // Formats value in reverse and returns the number of digits.
3144  char *format_decimal(ULongLong value) {
3145  char *buffer_end = buffer_ + BUFFER_SIZE - 1;
3146  while (value >= 100) {
3147  // Integer division is slow so do it for a group of two digits instead
3148  // of for every digit. The idea comes from the talk by Alexandrescu
3149  // "Three Optimization Tips for C++". See speed-test for a comparison.
3150  unsigned index = static_cast<unsigned>((value % 100) * 2);
3151  value /= 100;
3152  *--buffer_end = internal::Data::DIGITS[index + 1];
3153  *--buffer_end = internal::Data::DIGITS[index];
3154  }
3155  if (value < 10) {
3156  *--buffer_end = static_cast<char>('0' + value);
3157  return buffer_end;
3158  }
3159  unsigned index = static_cast<unsigned>(value * 2);
3160  *--buffer_end = internal::Data::DIGITS[index + 1];
3161  *--buffer_end = internal::Data::DIGITS[index];
3162  return buffer_end;
3163  }
3164 
3165  void FormatSigned(LongLong value) {
3166  ULongLong abs_value = static_cast<ULongLong>(value);
3167  bool negative = value < 0;
3168  if (negative)
3169  abs_value = 0 - abs_value;
3170  str_ = format_decimal(abs_value);
3171  if (negative)
3172  *--str_ = '-';
3173  }
3174 
3175  public:
3176  explicit FormatInt(int value) { FormatSigned(value); }
3177  explicit FormatInt(long value) { FormatSigned(value); }
3178  explicit FormatInt(LongLong value) { FormatSigned(value); }
3179  explicit FormatInt(unsigned value) : str_(format_decimal(value)) {}
3180  explicit FormatInt(unsigned long value) : str_(format_decimal(value)) {}
3181  explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {}
3182 
3184  std::size_t size() const {
3185  return internal::to_unsigned(buffer_ - str_ + BUFFER_SIZE - 1);
3186  }
3187 
3192  const char *data() const { return str_; }
3193 
3198  const char *c_str() const {
3199  buffer_[BUFFER_SIZE - 1] = '\0';
3200  return str_;
3201  }
3202 
3208  std::string str() const { return std::string(str_, size()); }
3209 };
3210 
3211 // Formats a decimal integer value writing into buffer and returns
3212 // a pointer to the end of the formatted string. This function doesn't
3213 // write a terminating null character.
3214 template <typename T>
3215 inline void format_decimal(char *&buffer, T value) {
3216  typedef typename internal::IntTraits<T>::MainType MainType;
3217  MainType abs_value = static_cast<MainType>(value);
3218  if (internal::is_negative(value)) {
3219  *buffer++ = '-';
3220  abs_value = 0 - abs_value;
3221  }
3222  if (abs_value < 100) {
3223  if (abs_value < 10) {
3224  *buffer++ = static_cast<char>('0' + abs_value);
3225  return;
3226  }
3227  unsigned index = static_cast<unsigned>(abs_value * 2);
3228  *buffer++ = internal::Data::DIGITS[index];
3229  *buffer++ = internal::Data::DIGITS[index + 1];
3230  return;
3231  }
3232  unsigned num_digits = internal::count_digits(abs_value);
3233  internal::format_decimal(buffer, abs_value, num_digits);
3234  buffer += num_digits;
3235 }
3236 
3247 template <typename T>
3248 inline internal::NamedArg<char> arg(StringRef name, const T &arg) {
3249  return internal::NamedArg<char>(name, arg);
3250 }
3251 
3252 template <typename T>
3253 inline internal::NamedArg<wchar_t> arg(WStringRef name, const T &arg) {
3254  return internal::NamedArg<wchar_t>(name, arg);
3255 }
3256 
3257 // The following two functions are deleted intentionally to disable
3258 // nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``.
3259 template <typename Char>
3260 void arg(StringRef, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
3261 template <typename Char>
3262 void arg(WStringRef, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
3263 }
3264 
3265 #if FMT_GCC_VERSION
3266 // Use the system_header pragma to suppress warnings about variadic macros
3267 // because suppressing -Wvariadic-macros with the diagnostic pragma doesn't
3268 // work. It is used at the end because we want to suppress as little warnings
3269 // as possible.
3270 # pragma GCC system_header
3271 #endif
3272 
3273 // This is used to work around VC++ bugs in handling variadic macros.
3274 #define FMT_EXPAND(args) args
3275 
3276 // Returns the number of arguments.
3277 // Based on https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s.
3278 #define FMT_NARG(...) FMT_NARG_(__VA_ARGS__, FMT_RSEQ_N())
3279 #define FMT_NARG_(...) FMT_EXPAND(FMT_ARG_N(__VA_ARGS__))
3280 #define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
3281 #define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
3282 
3283 #define FMT_CONCAT(a, b) a##b
3284 #define FMT_FOR_EACH_(N, f, ...) \
3285  FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__))
3286 #define FMT_FOR_EACH(f, ...) \
3287  FMT_EXPAND(FMT_FOR_EACH_(FMT_NARG(__VA_ARGS__), f, __VA_ARGS__))
3288 
3289 #define FMT_ADD_ARG_NAME(type, index) type arg##index
3290 #define FMT_GET_ARG_NAME(type, index) arg##index
3291 
3292 #if FMT_USE_VARIADIC_TEMPLATES
3293 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
3294  template <typename... Args> \
3295  ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
3296  const Args & ... args) { \
3297  typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
3298  typename ArgArray::Type array{ \
3299  ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
3300  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), \
3301  fmt::ArgList(fmt::internal::make_type(args...), array)); \
3302  }
3303 #else
3304 // Defines a wrapper for a function taking __VA_ARGS__ arguments
3305 // and n additional arguments of arbitrary types.
3306 # define FMT_WRAP(Char, ReturnType, func, call, n, ...) \
3307  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
3308  inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
3309  FMT_GEN(n, FMT_MAKE_ARG)) { \
3310  fmt::internal::ArgArray<n>::Type arr; \
3311  FMT_GEN(n, FMT_ASSIGN_##Char); \
3312  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList( \
3313  fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), arr)); \
3314  }
3315 
3316 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
3317  inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) { \
3318  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList()); \
3319  } \
3320  FMT_WRAP(Char, ReturnType, func, call, 1, __VA_ARGS__) \
3321  FMT_WRAP(Char, ReturnType, func, call, 2, __VA_ARGS__) \
3322  FMT_WRAP(Char, ReturnType, func, call, 3, __VA_ARGS__) \
3323  FMT_WRAP(Char, ReturnType, func, call, 4, __VA_ARGS__) \
3324  FMT_WRAP(Char, ReturnType, func, call, 5, __VA_ARGS__) \
3325  FMT_WRAP(Char, ReturnType, func, call, 6, __VA_ARGS__) \
3326  FMT_WRAP(Char, ReturnType, func, call, 7, __VA_ARGS__) \
3327  FMT_WRAP(Char, ReturnType, func, call, 8, __VA_ARGS__) \
3328  FMT_WRAP(Char, ReturnType, func, call, 9, __VA_ARGS__) \
3329  FMT_WRAP(Char, ReturnType, func, call, 10, __VA_ARGS__) \
3330  FMT_WRAP(Char, ReturnType, func, call, 11, __VA_ARGS__) \
3331  FMT_WRAP(Char, ReturnType, func, call, 12, __VA_ARGS__) \
3332  FMT_WRAP(Char, ReturnType, func, call, 13, __VA_ARGS__) \
3333  FMT_WRAP(Char, ReturnType, func, call, 14, __VA_ARGS__) \
3334  FMT_WRAP(Char, ReturnType, func, call, 15, __VA_ARGS__)
3335 #endif // FMT_USE_VARIADIC_TEMPLATES
3336 
3364 #define FMT_VARIADIC(ReturnType, func, ...) \
3365  FMT_VARIADIC_(char, ReturnType, func, return func, __VA_ARGS__)
3366 
3367 #define FMT_VARIADIC_W(ReturnType, func, ...) \
3368  FMT_VARIADIC_(wchar_t, ReturnType, func, return func, __VA_ARGS__)
3369 
3370 #define FMT_CAPTURE_ARG_(id, index) ::fmt::arg(#id, id)
3371 
3372 #define FMT_CAPTURE_ARG_W_(id, index) ::fmt::arg(L###id, id)
3373 
3388 #define FMT_CAPTURE(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_, __VA_ARGS__)
3389 
3390 #define FMT_CAPTURE_W(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_W_, __VA_ARGS__)
3391 
3392 namespace fmt {
3393 FMT_VARIADIC(std::string, format, CStringRef)
3394 FMT_VARIADIC_W(std::wstring, format, WCStringRef)
3395 FMT_VARIADIC(void, print, CStringRef)
3396 FMT_VARIADIC(void, print, std::FILE *, CStringRef)
3397 
3398 FMT_VARIADIC(void, print_colored, Color, CStringRef)
3399 FMT_VARIADIC(std::string, sprintf, CStringRef)
3400 FMT_VARIADIC_W(std::wstring, sprintf, WCStringRef)
3401 FMT_VARIADIC(int, printf, CStringRef)
3402 FMT_VARIADIC(int, fprintf, std::FILE *, CStringRef)
3403 
3404 #if FMT_USE_IOSTREAMS
3405 
3414 FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args);
3415 FMT_VARIADIC(void, print, std::ostream &, CStringRef)
3416 
3417 
3426 FMT_API int fprintf(std::ostream &os, CStringRef format_str, ArgList args);
3427 FMT_VARIADIC(int, fprintf, std::ostream &, CStringRef)
3428 #endif
3429 
3430 namespace internal {
3431 template <typename Char>
3432 inline bool is_name_start(Char c) {
3433  return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || '_' == c;
3434 }
3435 
3436 // Parses an unsigned integer advancing s to the end of the parsed input.
3437 // This function assumes that the first character of s is a digit.
3438 template <typename Char>
3439 unsigned parse_nonnegative_int(const Char *&s) {
3440  assert('0' <= *s && *s <= '9');
3441  unsigned value = 0;
3442  do {
3443  unsigned new_value = value * 10 + (*s++ - '0');
3444  // Check if value wrapped around.
3445  if (new_value < value) {
3447  break;
3448  }
3449  value = new_value;
3450  } while ('0' <= *s && *s <= '9');
3451  // Convert to unsigned to prevent a warning.
3452  unsigned max_int = (std::numeric_limits<int>::max)();
3453  if (value > max_int)
3454  FMT_THROW(FormatError("number is too big"));
3455  return value;
3456 }
3457 
3458 inline void require_numeric_argument(const Arg &arg, char spec) {
3459  if (arg.type > Arg::LAST_NUMERIC_TYPE) {
3460  std::string message =
3461  fmt::format("format specifier '{}' requires numeric argument", spec);
3462  FMT_THROW(fmt::FormatError(message));
3463  }
3464 }
3465 
3466 template <typename Char>
3467 void check_sign(const Char *&s, const Arg &arg) {
3468  char sign = static_cast<char>(*s);
3469  require_numeric_argument(arg, sign);
3470  if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) {
3472  "format specifier '{}' requires signed argument", sign)));
3473  }
3474  ++s;
3475 }
3476 } // namespace internal
3477 
3478 template <typename Char>
3480  BasicStringRef<Char> arg_name, const char *&error) {
3481  if (check_no_auto_index(error)) {
3482  map_.init(args());
3483  const internal::Arg *arg = map_.find(arg_name);
3484  if (arg)
3485  return *arg;
3486  error = "argument not found";
3487  }
3488  return internal::Arg();
3489 }
3490 
3491 template <typename Char>
3493  const char *error = 0;
3494  internal::Arg arg = *s < '0' || *s > '9' ?
3495  next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error);
3496  if (error) {
3498  *s != '}' && *s != ':' ? "invalid format string" : error));
3499  }
3500  return arg;
3501 }
3502 
3503 template <typename Char>
3505  assert(internal::is_name_start(*s));
3506  const Char *start = s;
3507  Char c;
3508  do {
3509  c = *++s;
3510  } while (internal::is_name_start(c) || ('0' <= c && c <= '9'));
3511  const char *error = 0;
3512  internal::Arg arg = get_arg(BasicStringRef<Char>(start, s - start), error);
3513  if (error)
3514  FMT_THROW(FormatError(error));
3515  return arg;
3516 }
3517 
3518 template <typename Char>
3520  const Char *&format_str, const internal::Arg &arg) {
3521  using internal::Arg;
3522  const Char *s = format_str;
3523  FormatSpec spec;
3524  if (*s == ':') {
3525  if (arg.type == Arg::CUSTOM) {
3526  arg.custom.format(this, arg.custom.value, &s);
3527  return s;
3528  }
3529  ++s;
3530  // Parse fill and alignment.
3531  if (Char c = *s) {
3532  const Char *p = s + 1;
3533  spec.align_ = ALIGN_DEFAULT;
3534  do {
3535  switch (*p) {
3536  case '<':
3537  spec.align_ = ALIGN_LEFT;
3538  break;
3539  case '>':
3540  spec.align_ = ALIGN_RIGHT;
3541  break;
3542  case '=':
3543  spec.align_ = ALIGN_NUMERIC;
3544  break;
3545  case '^':
3546  spec.align_ = ALIGN_CENTER;
3547  break;
3548  }
3549  if (spec.align_ != ALIGN_DEFAULT) {
3550  if (p != s) {
3551  if (c == '}') break;
3552  if (c == '{')
3553  FMT_THROW(FormatError("invalid fill character '{'"));
3554  s += 2;
3555  spec.fill_ = c;
3556  } else ++s;
3557  if (spec.align_ == ALIGN_NUMERIC)
3558  require_numeric_argument(arg, '=');
3559  break;
3560  }
3561  } while (--p >= s);
3562  }
3563 
3564  // Parse sign.
3565  switch (*s) {
3566  case '+':
3567  check_sign(s, arg);
3568  spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
3569  break;
3570  case '-':
3571  check_sign(s, arg);
3572  spec.flags_ |= MINUS_FLAG;
3573  break;
3574  case ' ':
3575  check_sign(s, arg);
3576  spec.flags_ |= SIGN_FLAG;
3577  break;
3578  }
3579 
3580  if (*s == '#') {
3581  require_numeric_argument(arg, '#');
3582  spec.flags_ |= HASH_FLAG;
3583  ++s;
3584  }
3585 
3586  // Parse zero flag.
3587  if (*s == '0') {
3588  require_numeric_argument(arg, '0');
3589  spec.align_ = ALIGN_NUMERIC;
3590  spec.fill_ = '0';
3591  ++s;
3592  }
3593 
3594  // Parse width.
3595  if ('0' <= *s && *s <= '9') {
3597  } else if (*s == '{') {
3598  ++s;
3599  Arg width_arg = internal::is_name_start(*s) ?
3600  parse_arg_name(s) : parse_arg_index(s);
3601  if (*s++ != '}')
3602  FMT_THROW(FormatError("invalid format string"));
3603  ULongLong value = 0;
3604  switch (width_arg.type) {
3605  case Arg::INT:
3606  if (width_arg.int_value < 0)
3607  FMT_THROW(FormatError("negative width"));
3608  value = width_arg.int_value;
3609  break;
3610  case Arg::UINT:
3611  value = width_arg.uint_value;
3612  break;
3613  case Arg::LONG_LONG:
3614  if (width_arg.long_long_value < 0)
3615  FMT_THROW(FormatError("negative width"));
3616  value = width_arg.long_long_value;
3617  break;
3618  case Arg::ULONG_LONG:
3619  value = width_arg.ulong_long_value;
3620  break;
3621  default:
3622  FMT_THROW(FormatError("width is not integer"));
3623  }
3624  if (value > (std::numeric_limits<int>::max)())
3625  FMT_THROW(FormatError("number is too big"));
3626  spec.width_ = static_cast<int>(value);
3627  }
3628 
3629  // Parse precision.
3630  if (*s == '.') {
3631  ++s;
3632  spec.precision_ = 0;
3633  if ('0' <= *s && *s <= '9') {
3635  } else if (*s == '{') {
3636  ++s;
3637  Arg precision_arg = internal::is_name_start(*s) ?
3638  parse_arg_name(s) : parse_arg_index(s);
3639  if (*s++ != '}')
3640  FMT_THROW(FormatError("invalid format string"));
3641  ULongLong value = 0;
3642  switch (precision_arg.type) {
3643  case Arg::INT:
3644  if (precision_arg.int_value < 0)
3645  FMT_THROW(FormatError("negative precision"));
3646  value = precision_arg.int_value;
3647  break;
3648  case Arg::UINT:
3649  value = precision_arg.uint_value;
3650  break;
3651  case Arg::LONG_LONG:
3652  if (precision_arg.long_long_value < 0)
3653  FMT_THROW(FormatError("negative precision"));
3654  value = precision_arg.long_long_value;
3655  break;
3656  case Arg::ULONG_LONG:
3657  value = precision_arg.ulong_long_value;
3658  break;
3659  default:
3660  FMT_THROW(FormatError("precision is not integer"));
3661  }
3662  if (value > (std::numeric_limits<int>::max)())
3663  FMT_THROW(FormatError("number is too big"));
3664  spec.precision_ = static_cast<int>(value);
3665  } else {
3666  FMT_THROW(FormatError("missing precision specifier"));
3667  }
3668  if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) {
3670  fmt::format("precision not allowed in {} format specifier",
3671  arg.type == Arg::POINTER ? "pointer" : "integer")));
3672  }
3673  }
3674 
3675  // Parse type.
3676  if (*s != '}' && *s)
3677  spec.type_ = static_cast<char>(*s++);
3678  }
3679 
3680  if (*s++ != '}')
3681  FMT_THROW(FormatError("missing '}' in format string"));
3682 
3683  // Format argument.
3684  internal::BasicArgFormatter<Char>(*this, spec, s - 1).visit(arg);
3685  return s;
3686 }
3687 
3688 template <typename Char>
3690  const Char *s = format_str.c_str();
3691  const Char *start = s;
3692  while (*s) {
3693  Char c = *s++;
3694  if (c != '{' && c != '}') continue;
3695  if (*s == c) {
3696  write(writer_, start, s);
3697  start = ++s;
3698  continue;
3699  }
3700  if (c == '}')
3701  FMT_THROW(FormatError("unmatched '}' in format string"));
3702  write(writer_, start, s - 1);
3704  parse_arg_name(s) : parse_arg_index(s);
3705  start = s = format(s, arg);
3706  }
3707  write(writer_, start, s);
3708 }
3709 } // namespace fmt
3710 
3711 #if FMT_USE_USER_DEFINED_LITERALS
3712 namespace fmt {
3713 namespace internal {
3714 
3715 template <typename Char>
3716 struct UdlFormat {
3717  const Char *str;
3718 
3719  template <typename... Args>
3720  auto operator()(Args && ... args) const
3721  -> decltype(format(str, std::forward<Args>(args)...)) {
3722  return format(str, std::forward<Args>(args)...);
3723  }
3724 };
3725 
3726 template <typename Char>
3727 struct UdlArg {
3728  const Char *str;
3729 
3730  template <typename T>
3731  NamedArg<Char> operator=(T &&value) const {
3732  return {str, std::forward<T>(value)};
3733  }
3734 };
3735 
3736 } // namespace internal
3737 
3738 inline namespace literals {
3739 
3750 inline internal::UdlFormat<char>
3751 operator"" _format(const char *s, std::size_t) { return {s}; }
3752 inline internal::UdlFormat<wchar_t>
3753 operator"" _format(const wchar_t *s, std::size_t) { return {s}; }
3754 
3765 inline internal::UdlArg<char>
3766 operator"" _a(const char *s, std::size_t) { return {s}; }
3767 inline internal::UdlArg<wchar_t>
3768 operator"" _a(const wchar_t *s, std::size_t) { return {s}; }
3769 
3770 } // inline namespace literals
3771 } // namespace fmt
3772 #endif // FMT_USE_USER_DEFINED_LITERALS
3773 
3774 // Restore warnings.
3775 #if FMT_GCC_VERSION >= 406
3776 # pragma GCC diagnostic pop
3777 #endif
3778 
3779 #if defined(__clang__) && !defined(__INTEL_COMPILER)
3780 # pragma clang diagnostic pop
3781 #endif
3782 
3783 #ifdef FMT_HEADER_ONLY
3784 # include "format.cc"
3785 #endif
3786 
3787 #endif // FMT_FORMAT_H_
internal::ArgMap< Char > map_
Definition: format.h:1896
BasicCStringRef(const Char *s)
Definition: format.h:510
void format(BasicFormatter< Char > &f, const Char *&format_str, const T &value)
Definition: format.h:2963
double double_value
Definition: format.h:961
Definition: format.h:1820
AlignTypeSpec(unsigned width, wchar_t fill)
Definition: format.h:1511
uint64_t Type
Definition: format.h:810
Definition: format.h:951
StrFormatSpec< wchar_t > pad(const wchar_t *str, unsigned width, char fill= ' ')
Definition: format.h:1675
FormatInt(unsigned long value)
Definition: format.h:3180
Definition: AppenderConsole.h:28
BasicWriter & operator<<(typename internal::WCharHelper< wchar_t, Char >::Supported value)
Definition: format.h:2440
MakeValue(typename WCharHelper< wchar_t, Char >::Supported value)
Definition: format.h:1183
Definition: format.h:539
BasicFormatter< Char > & formatter_
Definition: format.h:1807
int printf(CStringRef format, ArgList args)
Definition: format.h:3128
FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args)
Buffer< Char > & buffer_
Definition: format.h:2018
std::size_t size() const
Definition: format.h:2330
BasicStringRef(const Char *s)
Definition: format.h:420
#define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U)
Definition: format.h:541
StrFormatSpec(const Char *str, unsigned width, FillChar fill)
Definition: format.h:1553
BasicWriter< Char > & writer()
Definition: format.h:1719
void format(BasicCStringRef< Char > format_str)
Definition: format.h:3689
Definition: format.h:943
FMT_API void print_colored(Color c, CStringRef format, ArgList args)
const T & operator[](std::size_t index) const
Definition: format.h:642
int precision() const
Definition: format.h:1528
ArgFormatterBase(BasicWriter< Char > &w, FormatSpec &s)
Definition: format.h:1734
ArgType(const T &arg)
Definition: format.h:1999
bool is_negative(T value)
Definition: format.h:801
BasicWriter & operator<<(unsigned long value)
Definition: format.h:2399
Alignment align_
Definition: format.h:1498
const Char * data() const
Definition: format.h:441
Definition: format.h:1460
static void format_custom_arg(void *formatter, const void *arg, void *format_str_ptr)
Definition: format.h:1127
std::size_t size_
Definition: format.h:587
T data_[SIZE]
Definition: format.h:663
Definition: format.h:287
static bool is_negative(T value)
Definition: format.h:789
void resize(std::size_t new_size)
Definition: format.h:613
ArgList args_
Definition: format.h:1822
CharPtr grow_buffer(std::size_t n)
Definition: format.h:2234
Alignment align() const
Definition: format.h:1475
friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:458
const void * value
Definition: format.h:952
FMT_API int fprintf(std::ostream &os, CStringRef format_str, ArgList args)
int precision() const
Definition: format.h:1505
friend bool operator<=(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:464
char Yes[1]
Definition: format.h:1007
Definition: AppenderConsole.h:30
wchar_t fill_
Definition: format.h:1488
IntFormatSpec< int, TypeSpec<'x'> > hex(int value)
Definition: format.h:941
Result visit_wstring(Arg::StringValue< wchar_t >)
Definition: format.h:1338
T Type
Definition: format.h:539
Arg next_arg(const char *&error)
Definition: format.h:1837
IntFormatSpec< int, TypeSpec<'o'> > oct(int value)
Definition: format.h:3135
BasicFormatter(const ArgList &args, BasicWriter< Char > &w)
Definition: format.h:1920
WidthSpec(unsigned width, wchar_t fill)
Definition: format.h:1490
Definition: format.h:813
T * make_ptr(T *ptr, std::size_t)
Definition: format.h:571
friend bool operator<(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:461
void visit_wstring(Arg::StringValue< Char > value)
Definition: format.h:1791
Definition: format.h:983
Definition: format.h:737
void write(const char *value)
Definition: format.h:1728
const char * c_str() const
Definition: format.h:3198
const Char * c_str() const
Definition: format.h:2342
BasicWriter< Char > & writer_
Definition: format.h:1707
int next_arg_index_
Definition: format.h:1823
internal::FixedBuffer< Char > buffer_
Definition: format.h:2935
BasicWriter(Buffer< Char > &b)
Definition: format.h:2317
char Char
Definition: format.h:2154
static wchar_t convert(char value)
Definition: format.h:777
Definition: AppenderConsole.h:27
void visit_custom(Arg::CustomValue c)
Definition: format.h:1815
void visit_bool(bool value)
Definition: format.h:1743
int data[2]
Definition: format.h:288
FMT_API void format_system_error(fmt::Writer &out, int error_code, fmt::StringRef message) FMT_NOEXCEPT
#define isnan
Definition: BoundingIntervalHierarchy.cpp:24
T type
Definition: format.h:1071
FormatInt(ULongLong value)
Definition: format.h:3181
Definition: format.h:974
std::size_t capacity() const
Definition: format.h:608
unsigned width_
Definition: format.h:1485
Definition: format.h:1705
BasicArrayWriter(Char *array, std::size_t size)
Definition: format.h:2944
Definition: format.h:529
const Char * c_str() const
Definition: format.h:520
FMT_GCC_EXTENSION typedef unsigned long long ULongLong
Definition: format.h:362
Result visit_long_long(LongLong value)
Definition: format.h:1301
char * str_
Definition: format.h:3141
Definition: format.h:1053
STL namespace.
Definition: format.h:807
FormatSpec & spec()
Definition: format.h:1720
void visit_string(Arg::StringValue< char > value)
Definition: format.h:1785
Definition: format.h:2933
Alignment align() const
Definition: format.h:1503
Definition: format.h:1460
DummyInt isinf(...)
Definition: format.h:297
Definition: AppenderConsole.h:32
#define false
Definition: CascPort.h:18
static uint64_t type(long)
Definition: format.h:1158
Definition: format.h:1961
#define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: format.h:205
MakeValue(const NamedArg< Char_ > &value)
Definition: format.h:1237
void write_double(T value, const FormatSpec &spec)
Definition: format.h:2690
Definition: format.h:972
void write(BasicCStringRef< Char > format, ArgList args)
Definition: format.h:2383
int_type overflow(int_type ch=traits_type::eof())
Definition: format.h:2026
Definition: inflate.h:32
Result visit_cstring(const char *)
Definition: format.h:1332
bool flag(unsigned) const
Definition: format.h:1478
Definition: format.h:1518
const void * pointer
Definition: format.h:963
NamedArg(BasicStringRef< Char > argname, const T &value)
Definition: format.h:1262
static wchar_t convert(wchar_t value)
Definition: format.h:778
BasicStringRef< Char > name
Definition: format.h:1259
size_t size() const
Definition: format.h:2039
unsigned parse_nonnegative_int(const Char *&s)
Definition: format.h:3439
ArgType()
Definition: format.h:1996
BasicStringRef(const Char *s, std::size_t size)
Definition: format.h:412
uint32_t Type
Definition: format.h:807
StringValue< signed char > sstring
Definition: format.h:965
CustomValue custom
Definition: format.h:968
BasicWriter & operator<<(LongLong value)
Definition: format.h:2402
void write_decimal(Int value)
Definition: format.h:2251
BasicCStringRef(const std::basic_string< Char > &s)
Definition: format.h:517
FormatInt(unsigned value)
Definition: format.h:3179
void visit_pointer(const void *value)
Definition: format.h:1795
Result visit_string(Arg::StringValue< char >)
Definition: format.h:1335
Char * start_
Definition: format.h:2019
CharType Char
Definition: format.h:1892
Result visit_ulong_long(ULongLong value)
Definition: format.h:1307
Definition: format.h:1534
#define F(x, y, z)
Definition: format.h:1547
T & operator[](std::size_t index)
Definition: format.h:641
FormatSpec(unsigned width=0, char type=0, wchar_t fill= ' ')
Definition: format.h:1523
std::size_t size
Definition: format.h:945
friend bool operator>(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:467
bool flag(unsigned f) const
Definition: format.h:1527
void set_string(StringRef str)
Definition: format.h:1115
const Char * str_
Definition: format.h:1549
T max(const T &x, const T &y)
Definition: g3dmath.h:320
static uint64_t type(unsigned long)
Definition: format.h:1168
#define output
Definition: wire_format_lite.h:381
virtual ~BasicWriter()
Definition: format.h:2325
Definition: format.h:1018
void append(const U *begin, const U *end)
Definition: format.h:647
Result visit_uint(unsigned value)
Definition: format.h:1304
Definition: format.h:1460
static uint64_t type(const T &)
Definition: format.h:1230
Result visit_char(int value)
Definition: format.h:1313
FixedBuffer(Char *array, std::size_t size)
Definition: format.h:739
Definition: format.h:581
std::size_t size() const
Definition: format.h:3184
virtual ~Buffer()
Definition: format.h:602
#define true
Definition: CascPort.h:17
PrintfFormatter(const ArgList &args)
Definition: format.h:1881
wchar_t fill() const
Definition: format.h:1493
std::basic_string< Char > to_string() const
Definition: format.h:436
#define FMT_MAKE_VALUE_(Type, field, TYPE, rhs)
Definition: format.h:1137
BasicStringRef< wchar_t > WStringRef
Definition: format.h:476
void arg(WStringRef, const internal::NamedArg< Char > &) FMT_DELETED_OR_UNDEFINED
unsigned width() const
Definition: format.h:1476
Definition: format.h:1510
Definition: format.h:1068
Definition: format.h:1040
static uint64_t type(wchar_t)
Definition: format.h:1186
Definition: format.h:2878
void clear() FMT_NOEXCEPT
Definition: format.h:629
BasicStringRef< char > StringRef
Definition: format.h:475
unsigned int uint32_t
Definition: stdint.h:80
Result visit(const Arg &arg)
Definition: format.h:1348
Alignment
Definition: format.h:1459
Definition: format.h:1392
BasicMemoryWriter< char > MemoryWriter
Definition: format.h:2909
#define FMT_VARIADIC_W(ReturnType, func,...)
Definition: format.h:3367
#define FMT_GEN15(f)
Definition: format.h:1950
Definition: AppenderConsole.h:34
double inf()
Definition: g3dmath.cpp:40
BasicWriter & operator<<(long value)
Definition: format.h:2395
Definition: format.h:1804
uint64_t types_
Definition: format.h:1400
RuntimeError()
Definition: format.h:1385
void clear() FMT_NOEXCEPT
Definition: format.h:2478
Null< T > Unsupported
Definition: format.h:1004
char Char
Definition: bzlib_private.h:41
T value_
Definition: format.h:1536
DummyInt isnan(...)
Definition: format.h:299
Null< T > Supported
Definition: format.h:997
Type
Definition: format.h:971
Definition: format.h:2013
Definition: format.h:1081
MakeValue(unsigned long value)
Definition: format.h:1162
IntFormatSpec< int, TypeSpec<'X'> > hexu(int value)
#define FMT_MAKE_WSTR_VALUE(Type, TYPE)
Definition: format.h:1201
Definition: format.h:1396
SystemError()
Definition: format.h:2156
Definition: format.h:757
Definition: format.h:661
BasicWriter< Char > & writer_
Definition: format.h:1895
BasicCStringRef< char > CStringRef
Definition: format.h:523
MapType map_
Definition: format.h:1688
Result visit_double(double value)
Definition: format.h:1321
unsigned uint_value
Definition: format.h:958
BasicWriter< Char > & writer()
Definition: format.h:1924
unsigned __int64 uint64_t
Definition: stdint.h:90
FormatBuf(Buffer< Char > &buffer)
Definition: format.h:2022
BasicWriter & operator<<(long double value)
Definition: format.h:2427
friend bool operator==(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:455
Definition: format.h:1868
Definition: format.h:1465
bool check_no_auto_index(const char *&error)
Definition: format.h:1850
static bool isinfinity(T x)
Definition: format.h:320
BasicWriter< char > Writer
Definition: format.h:369
internal::CharTraits< Char >::CharPtr CharPtr
Definition: format.h:2218
void write(bool value)
Definition: format.h:1722
char type() const
Definition: format.h:1529
std::size_t size() const
Definition: format.h:605
Definition: format.h:369
Result visit_custom(Arg::CustomValue)
Definition: format.h:1344
void FormatSigned(LongLong value)
Definition: format.h:3165
Definition: AppenderConsole.h:26
Definition: format.h:1027
Definition: format.h:1465
Definition: format.h:977
int int_value
Definition: format.h:957
MakeUnsigned< Int >::Type to_unsigned(Int value)
Definition: format.h:554
CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec)
Definition: format.h:2483
ArgList()
Definition: format.h:1425
friend bool operator>=(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:470
void visit_char(int value)
Definition: format.h:1749
Vector2int16 & operator=(const Any &a)
SystemError(int error_code, CStringRef message)
Definition: format.h:2184
StringValue< char > string
Definition: format.h:964
void append_float_length(Char *&format_ptr, long double)
Definition: format.h:2301
DummyInt signbit(...)
Definition: format.h:295
#define FMT_DEFINE_INT_FORMATTERS(TYPE)
Definition: format.h:1601
void check_sign(const Char *&s, const Arg &arg)
Definition: format.h:3467
const char * data() const
Definition: format.h:3192
Buffer(T *ptr=0, std::size_t capacity=0)
Definition: format.h:590
BasicCStringRef< wchar_t > WCStringRef
Definition: format.h:524
Definition: format.h:787
internal::Arg operator[](unsigned index) const
Definition: format.h:1433
const Char * value
Definition: format.h:944
static bool isnotanumber(T x)
Definition: format.h:333
int compare(BasicStringRef other) const
Definition: format.h:447
ArgList(ULongLong types, const internal::Arg *args)
Definition: format.h:1429
No & operator<<(std::ostream &, int)
FMT_API void report_unknown_type(char code, const char *type)
std::size_t size() const
Definition: format.h:444
std::basic_string< Char > str() const
Definition: format.h:2354
~MemoryBuffer()
Definition: format.h:676
const internal::Arg * args_
Definition: format.h:1408
#define FMT_MAKE_VALUE(Type, field, TYPE)
Definition: format.h:1141
std::string __cdecl format(const char *fmt...) G3D_CHECK_PRINTF_ARGS
Result visit_any_double(T)
Definition: format.h:1328
Definition: format.h:1470
DummyInt _isnan(...)
Definition: format.h:300
Char * CharPtr
Definition: format.h:751
Definition: format.h:1289
BasicWriter< wchar_t > WWriter
Definition: format.h:372
MakeValue(const T &value, typename EnableIf< ConvertToInt< T >::value, int >::type=0)
Definition: format.h:1224
BasicWriter & operator<<(ULongLong value)
Definition: format.h:2412
Formatter::Char Char
Definition: format.h:1090
void push_back(const T &value)
Definition: format.h:631
char No[2]
Definition: format.h:1008
const Char * str() const
Definition: format.h:1558
T Unsupported
Definition: format.h:998
BasicArrayWriter(Char(&array)[SIZE])
Definition: format.h:2954
BasicArrayWriter< char > ArrayWriter
Definition: format.h:2958
BasicMemoryWriter(const Allocator &alloc=Allocator())
Definition: format.h:2883
float length(float v)
Definition: vectorMath.h:208
BasicArgFormatter(BasicFormatter< Char > &f, FormatSpec &s, const Char *fmt)
Definition: format.h:1811
MakeArg()
Definition: format.h:1246
Allocator get_allocator() const
Definition: format.h:712
T type
Definition: format.h:1074
Definition: format.h:1497
const Char * data_
Definition: format.h:506
void write_int(T value, Spec spec)
Definition: format.h:2608
Definition: inftrees.h:24
BasicWriter & operator<<(char value)
Definition: format.h:2435
Definition: format.h:1460
void visit_any_int(T value)
Definition: format.h:1738
Definition: AppenderConsole.h:31
Color
Definition: format.h:3029
uint64_t type
Definition: format.h:1994
FormatInt(long value)
Definition: format.h:3177
Definition: document.h:390
char type() const
Definition: format.h:1479
Definition: format.h:1474
Definition: format.h:1389
static char convert(char value)
Definition: format.h:766
Result visit_long_double(long double value)
Definition: format.h:1324
BasicWriter & operator<<(double value)
Definition: format.h:2416
void append_float_length(Char *&, T)
Definition: format.h:2306
Definition: format.h:286
FormatInt(LongLong value)
Definition: format.h:3178
T * ptr_
Definition: format.h:586
char * format_decimal(ULongLong value)
Definition: format.h:3144
void require_numeric_argument(const Arg &arg, char spec)
Definition: format.h:3458
void set_string(WStringRef str)
Definition: format.h:1120
#define FMT_DISPATCH(call)
Definition: format.h:1266
#define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type)
Definition: format.h:2108
Definition: format.h:375
StringValue< wchar_t > wstring
Definition: format.h:967
unsigned count_digits(uint64_t n)
Definition: format.h:844
T value() const
Definition: format.h:1542
MemoryBuffer(const Allocator &alloc=Allocator())
Definition: format.h:674
int precision() const
Definition: format.h:1477
std::basic_streambuf< Char >::int_type int_type
Definition: format.h:2015
void visit_cstring(const char *value)
Definition: format.h:1779
Result visit_int(int value)
Definition: format.h:1298
std::vector< std::pair< fmt::BasicStringRef< Char >, internal::Arg > > MapType
Definition: format.h:1685
Result visit_any_int(T)
Definition: format.h:1317
#define FMT_VARIADIC(ReturnType, func,...)
Definition: format.h:3364
Char * write_unsigned_decimal(UInt value, unsigned prefix_size=0)
Definition: format.h:2242
#define FMT_MAKE_STR_VALUE(Type, TYPE)
Definition: format.h:1189
void write(BasicWriter< Char > &w, const Char *start, const Char *end)
Definition: format.h:1860
BasicStringRef(const std::basic_string< Char > &s)
Definition: format.h:428
char fill() const
Definition: format.h:1480
IntFormatSpec(T val, const SpecT &spec=SpecT())
Definition: format.h:1539
const Char * format_
Definition: format.h:1808
FormatSpec & spec_
Definition: format.h:1708
Definition: format.h:1465
Definition: format.h:988
FormatterBase(const ArgList &args)
Definition: format.h:1831
IntFormatSpec< int, TypeSpec<'b'> > bin(int value)
Definition: format.h:1993
BasicWriter & operator<<(unsigned value)
Definition: format.h:2392
#define FMT_DISABLE_CONVERSION_TO_INT(Type)
Definition: format.h:1058
Definition: format.h:1074
Definition: format.h:746
std::basic_streambuf< Char >::traits_type traits_type
Definition: format.h:2016
MakeArg(const T &value)
Definition: format.h:1251
Definition: AppenderConsole.h:40
#define FMT_DELETED_OR_UNDEFINED
Definition: format.h:204
const Char * data() const FMT_NOEXCEPT
Definition: format.h:2336
Definition: format.h:405
void reserve(std::size_t capacity)
Definition: format.h:624
BasicData Data
Definition: format.h:831
internal::MemoryBuffer< Char, internal::INLINE_BUFFER_SIZE, Allocator > buffer_
Definition: format.h:2880
#define FMT_VARIADIC_VOID(func, arg_type)
Definition: format.h:2091
std::size_t capacity_
Definition: format.h:588
FormatError(CStringRef message)
Definition: format.h:531
T Supported
Definition: format.h:1003
Result visit_unhandled_arg()
Definition: format.h:1293
static Arg make(const T &value)
Definition: format.h:1982
No & convert(...)
ULongLong ulong_long_value
Definition: format.h:960
Definition: format.h:285
bool flag(unsigned) const
Definition: format.h:1513
unsigned flags_
Definition: format.h:1519
BasicMemoryWriter< wchar_t > WMemoryWriter
Definition: format.h:2910
std::wstring sprintf(WCStringRef format, ArgList args)
Definition: format.h:3102
Definition: format.h:991
Definition: format.h:1466
const FieldDescriptor value
Definition: descriptor.h:1522
std::string str() const
Definition: format.h:3208
Definition: format.h:1484
const internal::Arg * find(const fmt::BasicStringRef< Char > &name) const
Definition: format.h:1693
FormatFunc format
Definition: format.h:953
Arg get_arg(unsigned arg_index, const char *&error)
Definition: format.h:1846
static uint64_t type(const NamedArg< Char_ > &)
Definition: format.h:1240
Type type
Definition: format.h:984
Definition: format.h:2147
Definition: format.h:1244
MakeValue()
Definition: format.h:1135
static CharPtr fill_padding(CharPtr buffer, unsigned total_size, std::size_t content_size, wchar_t fill)
Definition: format.h:2528
#define const
Definition: zconf.h:217
Definition: format.h:561
G3D::int16 x
Definition: Vector2int16.h:37
int precision_
Definition: format.h:1520
AlignSpec(unsigned width, wchar_t fill, Alignment align=ALIGN_DEFAULT)
Definition: format.h:1500
unsigned width() const
Definition: format.h:1492
void report_unhandled_arg()
Definition: format.h:1291
uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT))
Definition: format.h:2004
static bool isnegative(double x)
Definition: format.h:343
LongLong long_long_value
Definition: format.h:959
void visit_any_double(T value)
Definition: format.h:1741
DummyInt _finite(...)
Definition: format.h:298
T check(T value)
Definition: format.h:305
#define FMT_GCC_EXTENSION
Definition: format.h:98
Definition: format.h:1460
static bool is_negative(T)
Definition: format.h:795
long double long_double_value
Definition: format.h:962
Type
Type of JSON value.
Definition: rapidjson.h:642
#define FMT_API
Definition: format.h:76
Buffer< Char > & buffer_
Definition: format.h:2214
char type() const
Definition: format.h:1514
#define FMT_THROW(x)
Definition: format.h:166
Definition: format.h:504
StringValue< unsigned char > ustring
Definition: format.h:966
double sign(double fValue)
Definition: g3dmath.h:669
const internal::Value * values_
Definition: format.h:1407
std::numeric_limits< fmt::internal::DummyInt > FPUtil
Definition: format.h:291
FMT_API void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT
Result visit_pointer(const void *)
Definition: format.h:1341
Definition: format.h:825
static Value make(const T &value)
Definition: format.h:1968
void write_pointer(const void *p)
Definition: format.h:1712
void format_decimal(char *&buffer, T value)
Definition: format.h:3215
static Char cast(int value)
Definition: format.h:753
Definition: format.h:976
const ArgList & args() const
Definition: format.h:1829
FormatInt(int value)
Definition: format.h:3176
ArgList(ULongLong types, const internal::Value *values)
Definition: format.h:1427
Definition: format.h:1088
double nan()
Definition: g3dmath.cpp:77
Definition: format.h:1465
MapType::value_type Pair
Definition: format.h:1686
int error_code_
Definition: format.h:2152
internal::Arg::Type type(unsigned index) const
Definition: format.h:1411
GenericMemoryBuffer MemoryBuffer
Definition: memorybuffer.h:60
BasicArrayWriter< wchar_t > WArrayWriter
Definition: format.h:2959
BasicWriter & operator<<(typename internal::WCharHelper< StringRef, Char >::Supported value)
Definition: format.h:2457
FMT_GCC_EXTENSION typedef long long LongLong
Definition: format.h:361
#define FMT_ARG_TYPE_DEFAULT(n)
Definition: format.h:2002
DummyInt _ecvt_s(...)
Definition: format.h:296
#define FMT_ASSERT(condition, message)
Definition: format.h:221
#define FMT_NOEXCEPT
Definition: format.h:184
void deallocate()
Definition: format.h:666
const Char * data_
Definition: format.h:407
octet_iterator append(uint32_t cp, octet_iterator result)
The library API - functions intended to be called by the users.
Definition: checked.h:73
bool is_name_start(Char c)
Definition: format.h:3432
CharPtr prepare_int_buffer(unsigned num_digits, const EmptySpec &, const char *prefix, unsigned prefix_size)
Definition: format.h:2263
Result visit_bool(bool value)
Definition: format.h:1310
Definition: format.h:996
int64_t intmax_t
Definition: stdint.h:123
char type_
Definition: format.h:1521
Definition: format.h:1383
std::size_t size_
Definition: format.h:408