NGL  6.5
The NCCA Graphics Library
format.h
Go to the documentation of this file.
1 /*
2  Formatting library for C++
3 
4  Copyright (c) 2012 - 2016, Victor Zverovich
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions are met:
9 
10  1. Redistributions of source code must retain the above copyright notice, this
11  list of conditions and the following disclaimer.
12  2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef FMT_FORMAT_H_
29 #define FMT_FORMAT_H_
30 
31 #include <cassert>
32 #include <clocale>
33 #include <cmath>
34 #include <cstdio>
35 #include <cstring>
36 #include <limits>
37 #include <memory>
38 #include <stdexcept>
39 #include <string>
40 #include <vector>
41 #include <utility>
42 
43 #ifdef _SECURE_SCL
44 # define FMT_SECURE_SCL _SECURE_SCL
45 #else
46 # define FMT_SECURE_SCL 0
47 #endif
48 
49 #if FMT_SECURE_SCL
50 # include <iterator>
51 #endif
52 
53 #ifdef _MSC_VER
54 # define FMT_MSC_VER _MSC_VER
55 #else
56 # define FMT_MSC_VER 0
57 #endif
58 
59 #if FMT_MSC_VER && FMT_MSC_VER <= 1500
60 typedef unsigned __int32 uint32_t;
61 typedef unsigned __int64 uint64_t;
62 typedef __int64 intmax_t;
63 #else
64 #include <stdint.h>
65 #endif
66 
67 #if !defined(FMT_HEADER_ONLY) && defined(_WIN32)
68 # ifdef FMT_EXPORT
69 # define FMT_API __declspec(dllexport)
70 # elif defined(FMT_SHARED)
71 # define FMT_API __declspec(dllimport)
72 # endif
73 #endif
74 #ifndef FMT_API
75 # define FMT_API
76 #endif
77 
78 #ifdef __GNUC__
79 # define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
80 # define FMT_GCC_EXTENSION __extension__
81 # if FMT_GCC_VERSION >= 406
82 # pragma GCC diagnostic push
83 // Disable the warning about "long long" which is sometimes reported even
84 // when using __extension__.
85 # pragma GCC diagnostic ignored "-Wlong-long"
86 // Disable the warning about declaration shadowing because it affects too
87 // many valid cases.
88 # pragma GCC diagnostic ignored "-Wshadow"
89 // Disable the warning about implicit conversions that may change the sign of
90 // an integer; silencing it otherwise would require many explicit casts.
91 # pragma GCC diagnostic ignored "-Wsign-conversion"
92 # endif
93 # if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__
94 # define FMT_HAS_GXX_CXX11 1
95 # endif
96 #else
97 # define FMT_GCC_EXTENSION
98 #endif
99 
100 #if defined(__INTEL_COMPILER)
101 # define FMT_ICC_VERSION __INTEL_COMPILER
102 #elif defined(__ICL)
103 # define FMT_ICC_VERSION __ICL
104 #endif
105 
106 #if defined(__clang__) && !defined(FMT_ICC_VERSION)
107 # pragma clang diagnostic push
108 # pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
109 # pragma clang diagnostic ignored "-Wpadded"
110 #endif
111 
112 #ifdef __GNUC_LIBSTD__
113 # define FMT_GNUC_LIBSTD_VERSION (__GNUC_LIBSTD__ * 100 + __GNUC_LIBSTD_MINOR__)
114 #endif
115 
116 #ifdef __has_feature
117 # define FMT_HAS_FEATURE(x) __has_feature(x)
118 #else
119 # define FMT_HAS_FEATURE(x) 0
120 #endif
121 
122 #ifdef __has_builtin
123 # define FMT_HAS_BUILTIN(x) __has_builtin(x)
124 #else
125 # define FMT_HAS_BUILTIN(x) 0
126 #endif
127 
128 #ifdef __has_cpp_attribute
129 # define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
130 #else
131 # define FMT_HAS_CPP_ATTRIBUTE(x) 0
132 #endif
133 
134 #ifndef FMT_USE_VARIADIC_TEMPLATES
135 // Variadic templates are available in GCC since version 4.4
136 // (http://gcc.gnu.org/projects/cxx0x.html) and in Visual C++
137 // since version 2013.
138 # define FMT_USE_VARIADIC_TEMPLATES \
139  (FMT_HAS_FEATURE(cxx_variadic_templates) || \
140  (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800)
141 #endif
142 
143 #ifndef FMT_USE_RVALUE_REFERENCES
144 // Don't use rvalue references when compiling with clang and an old libstdc++
145 // as the latter doesn't provide std::move.
146 # if defined(FMT_GNUC_LIBSTD_VERSION) && FMT_GNUC_LIBSTD_VERSION <= 402
147 # define FMT_USE_RVALUE_REFERENCES 0
148 # else
149 # define FMT_USE_RVALUE_REFERENCES \
150  (FMT_HAS_FEATURE(cxx_rvalue_references) || \
151  (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1600)
152 # endif
153 #endif
154 
155 #if FMT_USE_RVALUE_REFERENCES
156 # include <utility> // for std::move
157 #endif
158 
159 // Check if exceptions are disabled.
160 #if defined(__GNUC__) && !defined(__EXCEPTIONS)
161 # define FMT_EXCEPTIONS 0
162 #endif
163 #if FMT_MSC_VER && !_HAS_EXCEPTIONS
164 # define FMT_EXCEPTIONS 0
165 #endif
166 #ifndef FMT_EXCEPTIONS
167 # define FMT_EXCEPTIONS 1
168 #endif
169 
170 #ifndef FMT_THROW
171 # if FMT_EXCEPTIONS
172 # define FMT_THROW(x) throw x
173 # else
174 # define FMT_THROW(x) assert(false)
175 # endif
176 #endif
177 
178 // Define FMT_USE_NOEXCEPT to make fmt use noexcept (C++11 feature).
179 #ifndef FMT_USE_NOEXCEPT
180 # define FMT_USE_NOEXCEPT 0
181 #endif
182 
183 #ifndef FMT_NOEXCEPT
184 # if FMT_EXCEPTIONS
185 # if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \
186  (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
187  FMT_MSC_VER >= 1900
188 # define FMT_NOEXCEPT noexcept
189 # else
190 # define FMT_NOEXCEPT throw()
191 # endif
192 # else
193 # define FMT_NOEXCEPT
194 # endif
195 #endif
196 
197 // A macro to disallow the copy constructor and operator= functions
198 // This should be used in the private: declarations for a class
199 #ifndef FMT_USE_DELETED_FUNCTIONS
200 # define FMT_USE_DELETED_FUNCTIONS 0
201 #endif
202 
203 #if FMT_USE_DELETED_FUNCTIONS || FMT_HAS_FEATURE(cxx_deleted_functions) || \
204  (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800
205 # define FMT_DELETED_OR_UNDEFINED = delete
206 # define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
207  TypeName(const TypeName&) = delete; \
208  TypeName& operator=(const TypeName&) = delete
209 #else
210 # define FMT_DELETED_OR_UNDEFINED
211 # define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
212  TypeName(const TypeName&); \
213  TypeName& operator=(const TypeName&)
214 #endif
215 
216 #ifndef FMT_USE_USER_DEFINED_LITERALS
217 // All compilers which support UDLs also support variadic templates. This
218 // makes the fmt::literals implementation easier. However, an explicit check
219 // for variadic templates is added here just in case.
220 // For Intel's compiler both it and the system gcc/msc must support UDLs.
221 # define FMT_USE_USER_DEFINED_LITERALS \
222  FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES && \
223  (FMT_HAS_FEATURE(cxx_user_literals) || \
224  (FMT_GCC_VERSION >= 407 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900) && \
225  (!defined(FMT_ICC_VERSION) || FMT_ICC_VERSION >= 1500)
226 #endif
227 
228 #ifndef FMT_ASSERT
229 # define FMT_ASSERT(condition, message) assert((condition) && message)
230 #endif
231 
232 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz)
233 # define FMT_BUILTIN_CLZ(n) __builtin_clz(n)
234 #endif
235 
236 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clzll)
237 # define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n)
238 #endif
239 
240 // Some compilers masquerade as both MSVC and GCC-likes or
241 // otherwise support __builtin_clz and __builtin_clzll, so
242 // only define FMT_BUILTIN_CLZ using the MSVC intrinsics
243 // if the clz and clzll builtins are not available.
244 #if FMT_MSC_VER && !defined(FMT_BUILTIN_CLZLL)
245 # include <intrin.h> // _BitScanReverse, _BitScanReverse64
246 
247 namespace fmt {
248 namespace internal {
249 # pragma intrinsic(_BitScanReverse)
250 inline uint32_t clz(uint32_t x) {
251  unsigned long r = 0;
252  _BitScanReverse(&r, x);
253 
254  assert(x != 0);
255  // Static analysis complains about using uninitialized data
256  // "r", but the only way that can happen is if "x" is 0,
257  // which the callers guarantee to not happen.
258 # pragma warning(suppress: 6102)
259  return 31 - r;
260 }
261 # define FMT_BUILTIN_CLZ(n) fmt::internal::clz(n)
262 
263 # ifdef _WIN64
264 # pragma intrinsic(_BitScanReverse64)
265 # endif
266 
267 inline uint32_t clzll(uint64_t x) {
268  unsigned long r = 0;
269 # ifdef _WIN64
270  _BitScanReverse64(&r, x);
271 # else
272  // Scan the high 32 bits.
273  if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32)))
274  return 63 - (r + 32);
275 
276  // Scan the low 32 bits.
277  _BitScanReverse(&r, static_cast<uint32_t>(x));
278 # endif
279 
280  assert(x != 0);
281  // Static analysis complains about using uninitialized data
282  // "r", but the only way that can happen is if "x" is 0,
283  // which the callers guarantee to not happen.
284 # pragma warning(suppress: 6102)
285  return 63 - r;
286 }
287 # define FMT_BUILTIN_CLZLL(n) fmt::internal::clzll(n)
288 }
289 }
290 #endif
291 
292 namespace fmt {
293 namespace internal {
294 struct DummyInt {
295  int data[2];
296  operator int() const { return 0; }
297 };
299 
300 // Dummy implementations of system functions such as signbit and ecvt called
301 // if the latter are not available.
302 inline DummyInt signbit(...) { return DummyInt(); }
303 inline DummyInt _ecvt_s(...) { return DummyInt(); }
304 inline DummyInt isinf(...) { return DummyInt(); }
305 inline DummyInt _finite(...) { return DummyInt(); }
306 inline DummyInt isnan(...) { return DummyInt(); }
307 inline DummyInt _isnan(...) { return DummyInt(); }
308 
309 // A helper function to suppress bogus "conditional expression is constant"
310 // warnings.
311 template <typename T>
312 inline T const_check(T value) { return value; }
313 }
314 } // namespace fmt
315 
316 namespace std {
317 // Standard permits specialization of std::numeric_limits. This specialization
318 // is used to resolve ambiguity between isinf and std::isinf in glibc:
319 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48891
320 // and the same for isnan and signbit.
321 template <>
322 class numeric_limits<fmt::internal::DummyInt> :
323  public std::numeric_limits<int> {
324  public:
325  // Portable version of isinf.
326  template <typename T>
327  static bool isinfinity(T x) {
328  using namespace fmt::internal;
329  // The resolution "priority" is:
330  // isinf macro > std::isinf > ::isinf > fmt::internal::isinf
331  if (const_check(sizeof(isinf(x)) == sizeof(bool) ||
332  sizeof(isinf(x)) == sizeof(int))) {
333  return isinf(x) != 0;
334  }
335  return !_finite(static_cast<double>(x));
336  }
337 
338  // Portable version of isnan.
339  template <typename T>
340  static bool isnotanumber(T x) {
341  using namespace fmt::internal;
342  if (const_check(sizeof(isnan(x)) == sizeof(bool) ||
343  sizeof(isnan(x)) == sizeof(int))) {
344  return isnan(x) != 0;
345  }
346  return _isnan(static_cast<double>(x)) != 0;
347  }
348 
349  // Portable version of signbit.
350  static bool isnegative(double x) {
351  using namespace fmt::internal;
352  if (const_check(sizeof(signbit(x)) == sizeof(int)))
353  return signbit(x) != 0;
354  if (x < 0) return true;
355  if (!isnotanumber(x)) return false;
356  int dec = 0, sign = 0;
357  char buffer[2]; // The buffer size must be >= 2 or _ecvt_s will fail.
358  _ecvt_s(buffer, sizeof(buffer), x, 0, &dec, &sign);
359  return sign != 0;
360  }
361 };
362 } // namespace std
363 
364 namespace fmt {
365 
366 // Fix the warning about long long on older versions of GCC
367 // that don't support the diagnostic pragma.
368 FMT_GCC_EXTENSION typedef long long LongLong;
369 FMT_GCC_EXTENSION typedef unsigned long long ULongLong;
370 
371 #if FMT_USE_RVALUE_REFERENCES
372 using std::move;
373 #endif
374 
375 template <typename Char>
377 
378 typedef BasicWriter<char> Writer;
380 
381 template <typename Char>
383 
384 template <typename Impl, typename Char>
386 
387 template <typename CharType,
390 
415 template <typename Char>
417  private:
418  const Char *data_;
419  std::size_t size_;
420 
421  public:
423  BasicStringRef(const Char *s, std::size_t size) : data_(s), size_(size) {}
424 
431  BasicStringRef(const Char *s)
432  : data_(s), size_(std::char_traits<Char>::length(s)) {}
433 
439  BasicStringRef(const std::basic_string<Char> &s)
440  : data_(s.c_str()), size_(s.size()) {}
441 
447  std::basic_string<Char> to_string() const {
448  return std::basic_string<Char>(data_, size_);
449  }
450 
452  const Char *data() const { return data_; }
453 
455  std::size_t size() const { return size_; }
456 
457  // Lexicographically compare this string reference to other.
458  int compare(BasicStringRef other) const {
459  std::size_t size = size_ < other.size_ ? size_ : other.size_;
460  int result = std::char_traits<Char>::compare(data_, other.data_, size);
461  if (result == 0)
462  result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1);
463  return result;
464  }
465 
466  friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) {
467  return lhs.compare(rhs) == 0;
468  }
469  friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs) {
470  return lhs.compare(rhs) != 0;
471  }
472  friend bool operator<(BasicStringRef lhs, BasicStringRef rhs) {
473  return lhs.compare(rhs) < 0;
474  }
475  friend bool operator<=(BasicStringRef lhs, BasicStringRef rhs) {
476  return lhs.compare(rhs) <= 0;
477  }
478  friend bool operator>(BasicStringRef lhs, BasicStringRef rhs) {
479  return lhs.compare(rhs) > 0;
480  }
481  friend bool operator>=(BasicStringRef lhs, BasicStringRef rhs) {
482  return lhs.compare(rhs) >= 0;
483  }
484 };
485 
488 
514 template <typename Char>
516  private:
517  const Char *data_;
518 
519  public:
521  BasicCStringRef(const Char *s) : data_(s) {}
522 
528  BasicCStringRef(const std::basic_string<Char> &s) : data_(s.c_str()) {}
529 
531  const Char *c_str() const { return data_; }
532 };
533 
536 
538 class FormatError : public std::runtime_error {
539  public:
540  explicit FormatError(CStringRef message)
541  : std::runtime_error(message.c_str()) {}
542  ~FormatError() throw();
543 };
544 
545 namespace internal {
546 
547 // MakeUnsigned<T>::Type gives an unsigned type corresponding to integer type T.
548 template <typename T>
549 struct MakeUnsigned { typedef T Type; };
550 
551 #define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U) \
552  template <> \
553  struct MakeUnsigned<T> { typedef U Type; }
554 
555 FMT_SPECIALIZE_MAKE_UNSIGNED(char, unsigned char);
556 FMT_SPECIALIZE_MAKE_UNSIGNED(signed char, unsigned char);
557 FMT_SPECIALIZE_MAKE_UNSIGNED(short, unsigned short);
558 FMT_SPECIALIZE_MAKE_UNSIGNED(int, unsigned);
559 FMT_SPECIALIZE_MAKE_UNSIGNED(long, unsigned long);
560 FMT_SPECIALIZE_MAKE_UNSIGNED(LongLong, ULongLong);
561 
562 // Casts nonnegative integer to unsigned.
563 template <typename Int>
565  FMT_ASSERT(value >= 0, "negative value");
566  return static_cast<typename MakeUnsigned<Int>::Type>(value);
567 }
568 
569 // The number of characters to store in the MemoryBuffer object itself
570 // to avoid dynamic memory allocation.
571 enum { INLINE_BUFFER_SIZE = 500 };
572 
573 #if FMT_SECURE_SCL
574 // Use checked iterator to avoid warnings on MSVC.
575 template <typename T>
576 inline stdext::checked_array_iterator<T*> make_ptr(T *ptr, std::size_t size) {
577  return stdext::checked_array_iterator<T*>(ptr, size);
578 }
579 #else
580 template <typename T>
581 inline T *make_ptr(T *ptr, std::size_t) { return ptr; }
582 #endif
583 } // namespace internal
584 
590 template <typename T>
591 class Buffer {
592  private:
594 
595  protected:
596  T *ptr_;
597  std::size_t size_;
598  std::size_t capacity_;
599 
600  Buffer(T *ptr = 0, std::size_t capacity = 0)
601  : ptr_(ptr), size_(0), capacity_(capacity) {}
602 
609  virtual void grow(std::size_t size) = 0;
610 
611  public:
612  virtual ~Buffer() {}
613 
615  std::size_t size() const { return size_; }
616 
618  std::size_t capacity() const { return capacity_; }
619 
623  void resize(std::size_t new_size) {
624  if (new_size > capacity_)
625  grow(new_size);
626  size_ = new_size;
627  }
628 
634  void reserve(std::size_t capacity) {
635  if (capacity > capacity_)
636  grow(capacity);
637  }
638 
639  void clear() FMT_NOEXCEPT { size_ = 0; }
640 
641  void push_back(const T &value) {
642  if (size_ == capacity_)
643  grow(size_ + 1);
644  ptr_[size_++] = value;
645  }
646 
648  template <typename U>
649  void append(const U *begin, const U *end);
650 
651  T &operator[](std::size_t index) { return ptr_[index]; }
652  const T &operator[](std::size_t index) const { return ptr_[index]; }
653 };
654 
655 template <typename T>
656 template <typename U>
657 void Buffer<T>::append(const U *begin, const U *end) {
658  std::size_t new_size = size_ + internal::to_unsigned(end - begin);
659  if (new_size > capacity_)
660  grow(new_size);
661  std::uninitialized_copy(begin, end,
662  internal::make_ptr(ptr_, capacity_) + size_);
663  size_ = new_size;
664 }
665 
666 namespace internal {
667 
668 // A memory buffer for trivially copyable/constructible types with the first
669 // SIZE elements stored in the object itself.
670 template <typename T, std::size_t SIZE, typename Allocator = std::allocator<T> >
671 class MemoryBuffer : private Allocator, public Buffer<T> {
672  private:
673  T data_[SIZE];
674 
675  // Deallocate memory allocated by the buffer.
676  void deallocate() {
677  if (this->ptr_ != data_) Allocator::deallocate(this->ptr_, this->capacity_);
678  }
679 
680  protected:
681  void grow(std::size_t size);
682 
683  public:
684  explicit MemoryBuffer(const Allocator &alloc = Allocator())
685  : Allocator(alloc), Buffer<T>(data_, SIZE) {}
686  ~MemoryBuffer() { deallocate(); }
687 
688 #if FMT_USE_RVALUE_REFERENCES
689  private:
690  // Move data from other to this buffer.
691  void move(MemoryBuffer &other) {
692  Allocator &this_alloc = *this, &other_alloc = other;
693  this_alloc = std::move(other_alloc);
694  this->size_ = other.size_;
695  this->capacity_ = other.capacity_;
696  if (other.ptr_ == other.data_) {
697  this->ptr_ = data_;
698  std::uninitialized_copy(other.data_, other.data_ + this->size_,
699  make_ptr(data_, this->capacity_));
700  } else {
701  this->ptr_ = other.ptr_;
702  // Set pointer to the inline array so that delete is not called
703  // when deallocating.
704  other.ptr_ = other.data_;
705  }
706  }
707 
708  public:
709  MemoryBuffer(MemoryBuffer &&other) {
710  move(other);
711  }
712 
713  MemoryBuffer &operator=(MemoryBuffer &&other) {
714  assert(this != &other);
715  deallocate();
716  move(other);
717  return *this;
718  }
719 #endif
720 
721  // Returns a copy of the allocator associated with this buffer.
722  Allocator get_allocator() const { return *this; }
723 };
724 
725 template <typename T, std::size_t SIZE, typename Allocator>
727  std::size_t new_capacity = this->capacity_ + this->capacity_ / 2;
728  if (size > new_capacity)
729  new_capacity = size;
730  T *new_ptr = this->allocate(new_capacity);
731  // The following code doesn't throw, so the raw pointer above doesn't leak.
732  std::uninitialized_copy(this->ptr_, this->ptr_ + this->size_,
733  make_ptr(new_ptr, new_capacity));
734  std::size_t old_capacity = this->capacity_;
735  T *old_ptr = this->ptr_;
736  this->capacity_ = new_capacity;
737  this->ptr_ = new_ptr;
738  // deallocate may throw (at least in principle), but it doesn't matter since
739  // the buffer already uses the new storage and will deallocate it in case
740  // of exception.
741  if (old_ptr != data_)
742  Allocator::deallocate(old_ptr, old_capacity);
743 }
744 
745 // A fixed-size buffer.
746 template <typename Char>
747 class FixedBuffer : public fmt::Buffer<Char> {
748  public:
749  FixedBuffer(Char *array, std::size_t size) : fmt::Buffer<Char>(array, size) {}
750 
751  protected:
752  FMT_API void grow(std::size_t size);
753 };
754 
755 template <typename Char>
757  public:
758 #if FMT_SECURE_SCL
759  typedef stdext::checked_array_iterator<Char*> CharPtr;
760 #else
761  typedef Char *CharPtr;
762 #endif
763  static Char cast(int value) { return static_cast<Char>(value); }
764 };
765 
766 template <typename Char>
768 
769 template <>
770 class CharTraits<char> : public BasicCharTraits<char> {
771  private:
772  // Conversion from wchar_t to char is not allowed.
773  static char convert(wchar_t);
774 
775  public:
776  static char convert(char value) { return value; }
777 
778  // Formats a floating-point number.
779  template <typename T>
780  FMT_API static int format_float(char *buffer, std::size_t size,
781  const char *format, unsigned width, int precision, T value);
782 };
783 
784 template <>
785 class CharTraits<wchar_t> : public BasicCharTraits<wchar_t> {
786  public:
787  static wchar_t convert(char value) { return value; }
788  static wchar_t convert(wchar_t value) { return value; }
789 
790  template <typename T>
791  FMT_API static int format_float(wchar_t *buffer, std::size_t size,
792  const wchar_t *format, unsigned width, int precision, T value);
793 };
794 
795 // Checks if a number is negative - used to avoid warnings.
796 template <bool IsSigned>
797 struct SignChecker {
798  template <typename T>
799  static bool is_negative(T value) { return value < 0; }
800 };
801 
802 template <>
803 struct SignChecker<false> {
804  template <typename T>
805  static bool is_negative(T) { return false; }
806 };
807 
808 // Returns true if value is negative, false otherwise.
809 // Same as (value < 0) but doesn't produce warnings if T is an unsigned type.
810 template <typename T>
811 inline bool is_negative(T value) {
813 }
814 
815 // Selects uint32_t if FitsIn32Bits is true, uint64_t otherwise.
816 template <bool FitsIn32Bits>
817 struct TypeSelector { typedef uint32_t Type; };
818 
819 template <>
820 struct TypeSelector<false> { typedef uint64_t Type; };
821 
822 template <typename T>
823 struct IntTraits {
824  // Smallest of uint32_t and uint64_t that is large enough to represent
825  // all values of T.
826  typedef typename
828 };
829 
830 FMT_API void report_unknown_type(char code, const char *type);
831 
832 // Static data is placed in this class template to allow header-only
833 // configuration.
834 template <typename T = void>
836  static const uint32_t POWERS_OF_10_32[];
837  static const uint64_t POWERS_OF_10_64[];
838  static const char DIGITS[];
839 };
840 
841 #ifndef FMT_USE_EXTERN_TEMPLATES
842 // Clang doesn't have a feature check for extern templates so we check
843 // for variadic templates which were introduced in the same version.
844 # define FMT_USE_EXTERN_TEMPLATES (__clang__ && FMT_USE_VARIADIC_TEMPLATES)
845 #endif
846 
847 #if FMT_USE_EXTERN_TEMPLATES && !defined(FMT_HEADER_ONLY)
848 extern template struct BasicData<void>;
849 #endif
850 
852 
853 #ifdef FMT_BUILTIN_CLZLL
854 // Returns the number of decimal digits in n. Leading zeros are not counted
855 // except for n == 0 in which case count_digits returns 1.
856 inline unsigned count_digits(uint64_t n) {
857  // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
858  // and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits.
859  int t = (64 - FMT_BUILTIN_CLZLL(n | 1)) * 1233 >> 12;
860  return to_unsigned(t) - (n < Data::POWERS_OF_10_64[t]) + 1;
861 }
862 #else
863 // Fallback version of count_digits used when __builtin_clz is not available.
864 inline unsigned count_digits(uint64_t n) {
865  unsigned count = 1;
866  for (;;) {
867  // Integer division is slow so do it for a group of four digits instead
868  // of for every digit. The idea comes from the talk by Alexandrescu
869  // "Three Optimization Tips for C++". See speed-test for a comparison.
870  if (n < 10) return count;
871  if (n < 100) return count + 1;
872  if (n < 1000) return count + 2;
873  if (n < 10000) return count + 3;
874  n /= 10000u;
875  count += 4;
876  }
877 }
878 #endif
879 
880 #ifdef FMT_BUILTIN_CLZ
881 // Optional version of count_digits for better performance on 32-bit platforms.
882 inline unsigned count_digits(uint32_t n) {
883  int t = (32 - FMT_BUILTIN_CLZ(n | 1)) * 1233 >> 12;
884  return to_unsigned(t) - (n < Data::POWERS_OF_10_32[t]) + 1;
885 }
886 #endif
887 
888 // A functor that doesn't add a thousands separator.
890  template <typename Char>
891  void operator()(Char *) {}
892 };
893 
894 // A functor that adds a thousands separator.
896  private:
898 
899  // Index of a decimal digit with the least significant digit having index 0.
900  unsigned digit_index_;
901 
902  public:
903  explicit ThousandsSep(fmt::StringRef sep) : sep_(sep), digit_index_(0) {}
904 
905  template <typename Char>
906  void operator()(Char *&buffer) {
907  if (++digit_index_ % 3 != 0)
908  return;
909  buffer -= sep_.size();
910  std::uninitialized_copy(sep_.data(), sep_.data() + sep_.size(),
911  internal::make_ptr(buffer, sep_.size()));
912  }
913 };
914 
915 // Formats a decimal unsigned integer value writing into buffer.
916 // thousands_sep is a functor that is called after writing each char to
917 // add a thousands separator if necessary.
918 template <typename UInt, typename Char, typename ThousandsSep>
919 inline void format_decimal(Char *buffer, UInt value, unsigned num_digits,
921  buffer += num_digits;
922  while (value >= 100) {
923  // Integer division is slow so do it for a group of two digits instead
924  // of for every digit. The idea comes from the talk by Alexandrescu
925  // "Three Optimization Tips for C++". See speed-test for a comparison.
926  unsigned index = static_cast<unsigned>((value % 100) * 2);
927  value /= 100;
928  *--buffer = Data::DIGITS[index + 1];
929  thousands_sep(buffer);
930  *--buffer = Data::DIGITS[index];
931  thousands_sep(buffer);
932  }
933  if (value < 10) {
934  *--buffer = static_cast<char>('0' + value);
935  return;
936  }
937  unsigned index = static_cast<unsigned>(value * 2);
938  *--buffer = Data::DIGITS[index + 1];
939  thousands_sep(buffer);
940  *--buffer = Data::DIGITS[index];
941 }
942 
943 template <typename UInt, typename Char>
944 inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) {
945  return format_decimal(buffer, value, num_digits, NoThousandsSep());
946 }
947 
948 #ifndef _WIN32
949 # define FMT_USE_WINDOWS_H 0
950 #elif !defined(FMT_USE_WINDOWS_H)
951 # define FMT_USE_WINDOWS_H 1
952 #endif
953 
954 // Define FMT_USE_WINDOWS_H to 0 to disable use of windows.h.
955 // All the functionality that relies on it will be disabled too.
956 #if FMT_USE_WINDOWS_H
957 // A converter from UTF-8 to UTF-16.
958 // It is only provided for Windows since other systems support UTF-8 natively.
959 class UTF8ToUTF16 {
960  private:
962 
963  public:
964  FMT_API explicit UTF8ToUTF16(StringRef s);
965  operator WStringRef() const { return WStringRef(&buffer_[0], size()); }
966  size_t size() const { return buffer_.size() - 1; }
967  const wchar_t *c_str() const { return &buffer_[0]; }
968  std::wstring str() const { return std::wstring(&buffer_[0], size()); }
969 };
970 
971 // A converter from UTF-16 to UTF-8.
972 // It is only provided for Windows since other systems support UTF-8 natively.
973 class UTF16ToUTF8 {
974  private:
976 
977  public:
978  UTF16ToUTF8() {}
979  FMT_API explicit UTF16ToUTF8(WStringRef s);
980  operator StringRef() const { return StringRef(&buffer_[0], size()); }
981  size_t size() const { return buffer_.size() - 1; }
982  const char *c_str() const { return &buffer_[0]; }
983  std::string str() const { return std::string(&buffer_[0], size()); }
984 
985  // Performs conversion returning a system error code instead of
986  // throwing exception on conversion error. This method may still throw
987  // in case of memory allocation error.
988  FMT_API int convert(WStringRef s);
989 };
990 
991 FMT_API void format_windows_error(fmt::Writer &out, int error_code,
993 #endif
994 
995 // A formatting argument value.
996 struct Value {
997  template <typename Char>
998  struct StringValue {
999  const Char *value;
1000  std::size_t size;
1001  };
1002 
1003  typedef void (*FormatFunc)(
1004  void *formatter, const void *arg, void *format_str_ptr);
1005 
1006  struct CustomValue {
1007  const void *value;
1008  FormatFunc format;
1009  };
1010 
1011  union {
1013  unsigned uint_value;
1015  ULongLong ulong_long_value;
1017  long double long_double_value;
1018  const void *pointer;
1024  };
1025 
1026  enum Type {
1027  NONE, NAMED_ARG,
1028  // Integer types should go first,
1029  INT, UINT, LONG_LONG, ULONG_LONG, BOOL, CHAR, LAST_INTEGER_TYPE = CHAR,
1030  // followed by floating-point types.
1031  DOUBLE, LONG_DOUBLE, LAST_NUMERIC_TYPE = LONG_DOUBLE,
1032  CSTRING, STRING, WSTRING, POINTER, CUSTOM
1033  };
1034 };
1035 
1036 // A formatting argument. It is a trivially copyable/constructible type to
1037 // allow storage in internal::MemoryBuffer.
1038 struct Arg : Value {
1040 };
1041 
1042 template <typename Char>
1043 struct NamedArg;
1044 
1045 template <typename T = void>
1046 struct Null {};
1047 
1048 // A helper class template to enable or disable overloads taking wide
1049 // characters and strings in MakeValue.
1050 template <typename T, typename Char>
1051 struct WCharHelper {
1053  typedef T Unsupported;
1054 };
1055 
1056 template <typename T>
1058  typedef T Supported;
1060 };
1061 
1062 typedef char Yes[1];
1063 typedef char No[2];
1064 
1065 template <typename T>
1066 T &get();
1067 
1068 // These are non-members to workaround an overload resolution bug in bcc32.
1069 Yes &convert(fmt::ULongLong);
1070 No &convert(...);
1071 
1072 template<typename T, bool ENABLE_CONVERSION>
1074  enum { value = ENABLE_CONVERSION };
1075 };
1076 
1077 template<typename T, bool ENABLE_CONVERSION>
1079  enum { value = false };
1080 };
1081 
1082 template<typename T>
1084  enum {
1085  // Don't convert numeric types.
1087  };
1088 };
1089 
1090 template<typename T>
1092  enum { enable_conversion = sizeof(convert(get<T>())) == sizeof(Yes) };
1094 };
1095 
1096 #define FMT_DISABLE_CONVERSION_TO_INT(Type) \
1097  template <> \
1098  struct ConvertToInt<Type> { enum { value = 0 }; }
1099 
1100 // Silence warnings about convering float to int.
1103 FMT_DISABLE_CONVERSION_TO_INT(long double);
1104 
1105 template<bool B, class T = void>
1106 struct EnableIf {};
1107 
1108 template<class T>
1109 struct EnableIf<true, T> { typedef T type; };
1110 
1111 template<bool B, class T, class F>
1112 struct Conditional { typedef T type; };
1113 
1114 template<class T, class F>
1115 struct Conditional<false, T, F> { typedef F type; };
1116 
1117 // For bcc32 which doesn't understand ! in template arguments.
1118 template <bool>
1119 struct Not { enum { value = 0 }; };
1120 
1121 template <>
1122 struct Not<false> { enum { value = 1 }; };
1123 
1124 template <typename T>
1125 struct False { enum { value = 0 }; };
1126 
1127 template <typename T, T> struct LConvCheck {
1128  LConvCheck(int) {}
1129 };
1130 
1131 // Returns the thousands separator for the current locale.
1132 // We check if ``lconv`` contains ``thousands_sep`` because on Android
1133 // ``lconv`` is stubbed as an empty struct.
1134 template <typename LConv>
1135 inline StringRef thousands_sep(
1137  return lc->thousands_sep;
1138 }
1139 
1140 inline fmt::StringRef thousands_sep(...) { return ""; }
1141 
1142 #define FMT_CONCAT(a, b) a##b
1143 
1144 #if FMT_GCC_VERSION >= 407
1145 # define FMT_UNUSED __attribute__((unused))
1146 #else
1147 # define FMT_UNUSED
1148 #endif
1149 
1150 #ifndef FMT_USE_STATIC_ASSERT
1151 # define FMT_USE_STATIC_ASSERT 0
1152 #endif
1153 
1154 #if FMT_USE_STATIC_ASSERT || FMT_HAS_FEATURE(cxx_static_assert) || \
1155  (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600
1156 # define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message)
1157 #else
1158 # define FMT_CONCAT_(a, b) FMT_CONCAT(a, b)
1159 # define FMT_STATIC_ASSERT(cond, message) \
1160  typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED
1161 #endif
1162 
1163 template <typename Formatter, typename Char, typename T>
1164 void format_arg(Formatter &, const Char *, const T &) {
1166  "Cannot format argument. To enable the use of ostream "
1167  "operator<< include fmt/ostream.h. Otherwise provide "
1168  "an overload of format_arg.");
1169 }
1170 
1171 // Makes an Arg object from any type.
1172 template <typename Formatter>
1173 class MakeValue : public Arg {
1174  public:
1175  typedef typename Formatter::Char Char;
1176 
1177  private:
1178  // The following two methods are private to disallow formatting of
1179  // arbitrary pointers. If you want to output a pointer cast it to
1180  // "void *" or "const void *". In particular, this forbids formatting
1181  // of "[const] volatile char *" which is printed as bool by iostreams.
1182  // Do not implement!
1183  template <typename T>
1184  MakeValue(const T *value);
1185  template <typename T>
1186  MakeValue(T *value);
1187 
1188  // The following methods are private to disallow formatting of wide
1189  // characters and strings into narrow strings as in
1190  // fmt::format("{}", L"test");
1191  // To fix this, use a wide format string: fmt::format(L"{}", L"test").
1192 #if !FMT_MSC_VER || defined(_NATIVE_WCHAR_T_DEFINED)
1194 #endif
1199 
1200  void set_string(StringRef str) {
1201  string.value = str.data();
1202  string.size = str.size();
1203  }
1204 
1205  void set_string(WStringRef str) {
1206  wstring.value = str.data();
1207  wstring.size = str.size();
1208  }
1209 
1210  // Formats an argument of a custom type, such as a user-defined class.
1211  template <typename T>
1212  static void format_custom_arg(
1213  void *formatter, const void *arg, void *format_str_ptr) {
1214  format_arg(*static_cast<Formatter*>(formatter),
1215  *static_cast<const Char**>(format_str_ptr),
1216  *static_cast<const T*>(arg));
1217  }
1218 
1219  public:
1221 
1222 #define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \
1223  MakeValue(Type value) { field = rhs; } \
1224  static uint64_t type(Type) { return Arg::TYPE; }
1225 
1226 #define FMT_MAKE_VALUE(Type, field, TYPE) \
1227  FMT_MAKE_VALUE_(Type, field, TYPE, value)
1228 
1229  FMT_MAKE_VALUE(bool, int_value, BOOL)
1230  FMT_MAKE_VALUE(short, int_value, INT)
1231  FMT_MAKE_VALUE(unsigned short, uint_value, UINT)
1232  FMT_MAKE_VALUE(int, int_value, INT)
1233  FMT_MAKE_VALUE(unsigned, uint_value, UINT)
1234 
1236  // To minimize the number of types we need to deal with, long is
1237  // translated either to int or to long long depending on its size.
1238  if (const_check(sizeof(long) == sizeof(int)))
1239  int_value = static_cast<int>(value);
1240  else
1241  long_long_value = value;
1242  }
1243  static uint64_t type(long) {
1244  return sizeof(long) == sizeof(int) ? Arg::INT : Arg::LONG_LONG;
1245  }
1246 
1247  MakeValue(unsigned long value) {
1248  if (const_check(sizeof(unsigned long) == sizeof(unsigned)))
1249  uint_value = static_cast<unsigned>(value);
1250  else
1251  ulong_long_value = value;
1252  }
1253  static uint64_t type(unsigned long) {
1254  return sizeof(unsigned long) == sizeof(unsigned) ?
1256  }
1257 
1258  FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG)
1259  FMT_MAKE_VALUE(ULongLong, ulong_long_value, ULONG_LONG)
1260  FMT_MAKE_VALUE(float, double_value, DOUBLE)
1261  FMT_MAKE_VALUE(double, double_value, DOUBLE)
1262  FMT_MAKE_VALUE(long double, long_double_value, LONG_DOUBLE)
1263  FMT_MAKE_VALUE(signed char, int_value, INT)
1264  FMT_MAKE_VALUE(unsigned char, uint_value, UINT)
1265  FMT_MAKE_VALUE(char, int_value, CHAR)
1266 
1267 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
1269  int_value = value;
1270  }
1271  static uint64_t type(wchar_t) { return Arg::CHAR; }
1272 #endif
1273 
1274 #define FMT_MAKE_STR_VALUE(Type, TYPE) \
1275  MakeValue(Type value) { set_string(value); } \
1276  static uint64_t type(Type) { return Arg::TYPE; }
1277 
1278  FMT_MAKE_VALUE(char *, string.value, CSTRING)
1279  FMT_MAKE_VALUE(const char *, string.value, CSTRING)
1280  FMT_MAKE_VALUE(signed char *, sstring.value, CSTRING)
1281  FMT_MAKE_VALUE(const signed char *, sstring.value, CSTRING)
1282  FMT_MAKE_VALUE(unsigned char *, ustring.value, CSTRING)
1283  FMT_MAKE_VALUE(const unsigned char *, ustring.value, CSTRING)
1284  FMT_MAKE_STR_VALUE(const std::string &, STRING)
1285  FMT_MAKE_STR_VALUE(StringRef, STRING)
1286  FMT_MAKE_VALUE_(CStringRef, string.value, CSTRING, value.c_str())
1287 
1288 #define FMT_MAKE_WSTR_VALUE(Type, TYPE) \
1289  MakeValue(typename WCharHelper<Type, Char>::Supported value) { \
1290  set_string(value); \
1291  } \
1292  static uint64_t type(Type) { return Arg::TYPE; }
1293 
1294  FMT_MAKE_WSTR_VALUE(wchar_t *, WSTRING)
1295  FMT_MAKE_WSTR_VALUE(const wchar_t *, WSTRING)
1296  FMT_MAKE_WSTR_VALUE(const std::wstring &, WSTRING)
1297  FMT_MAKE_WSTR_VALUE(WStringRef, WSTRING)
1298 
1299  FMT_MAKE_VALUE(void *, pointer, POINTER)
1300  FMT_MAKE_VALUE(const void *, pointer, POINTER)
1301 
1302  template <typename T>
1303  MakeValue(const T &value,
1304  typename EnableIf<Not<
1305  ConvertToInt<T>::value>::value, int>::type = 0) {
1306  custom.value = &value;
1307  custom.format = &format_custom_arg<T>;
1308  }
1309 
1310  template <typename T>
1311  MakeValue(const T &value,
1312  typename EnableIf<ConvertToInt<T>::value, int>::type = 0) {
1313  int_value = value;
1314  }
1315 
1316  template <typename T>
1317  static uint64_t type(const T &) {
1319  }
1320 
1321  // Additional template param `Char_` is needed here because make_type always
1322  // uses char.
1323  template <typename Char_>
1324  MakeValue(const NamedArg<Char_> &value) { pointer = &value; }
1325 
1326  template <typename Char_>
1327  static uint64_t type(const NamedArg<Char_> &) { return Arg::NAMED_ARG; }
1328 };
1329 
1330 template <typename Formatter>
1331 class MakeArg : public Arg {
1332 public:
1334  type = Arg::NONE;
1335  }
1336 
1337  template <typename T>
1338  MakeArg(const T &value)
1339  : Arg(MakeValue<Formatter>(value)) {
1340  type = static_cast<Arg::Type>(MakeValue<Formatter>::type(value));
1341  }
1342 };
1343 
1344 template <typename Char>
1345 struct NamedArg : Arg {
1347 
1348  template <typename T>
1350  : Arg(MakeArg< BasicFormatter<Char> >(value)), name(argname) {}
1351 };
1352 
1353 class RuntimeError : public std::runtime_error {
1354  protected:
1355  RuntimeError() : std::runtime_error("") {}
1356  ~RuntimeError() throw();
1357 };
1358 
1359 template <typename Char>
1360 class ArgMap;
1361 } // namespace internal
1362 
1364 class ArgList {
1365  private:
1366  // To reduce compiled code size per formatting function call, types of first
1367  // MAX_PACKED_ARGS arguments are passed in the types_ field.
1369  union {
1370  // If the number of arguments is less than MAX_PACKED_ARGS, the argument
1371  // values are stored in values_, otherwise they are stored in args_.
1372  // This is done to reduce compiled code size as storing larger objects
1373  // may require more code (at least on x86-64) even if the same amount of
1374  // data is actually copied to stack. It saves ~10% on the bloat test.
1377  };
1378 
1379  internal::Arg::Type type(unsigned index) const {
1380  unsigned shift = index * 4;
1381  uint64_t mask = 0xf;
1382  return static_cast<internal::Arg::Type>(
1383  (types_ & (mask << shift)) >> shift);
1384  }
1385 
1386  template <typename Char>
1387  friend class internal::ArgMap;
1388 
1389  public:
1390  // Maximum number of arguments with packed types.
1391  enum { MAX_PACKED_ARGS = 16 };
1392 
1393  ArgList() : types_(0) {}
1394 
1396  : types_(types), values_(values) {}
1397  ArgList(ULongLong types, const internal::Arg *args)
1398  : types_(types), args_(args) {}
1399 
1401  internal::Arg operator[](unsigned index) const {
1402  using internal::Arg;
1403  Arg arg;
1404  bool use_values = type(MAX_PACKED_ARGS - 1) == Arg::NONE;
1405  if (index < MAX_PACKED_ARGS) {
1406  Arg::Type arg_type = type(index);
1407  internal::Value &val = arg;
1408  if (arg_type != Arg::NONE)
1409  val = use_values ? values_[index] : args_[index];
1410  arg.type = arg_type;
1411  return arg;
1412  }
1413  if (use_values) {
1414  // The index is greater than the number of arguments that can be stored
1415  // in values, so return a "none" argument.
1416  arg.type = Arg::NONE;
1417  return arg;
1418  }
1419  for (unsigned i = MAX_PACKED_ARGS; i <= index; ++i) {
1420  if (args_[i].type == Arg::NONE)
1421  return args_[i];
1422  }
1423  return args_[index];
1424  }
1425 };
1426 
1427 #define FMT_DISPATCH(call) static_cast<Impl*>(this)->call
1428 
1453 template <typename Impl, typename Result>
1454 class ArgVisitor {
1455  private:
1457 
1458  public:
1460 
1462  FMT_DISPATCH(report_unhandled_arg());
1463  return Result();
1464  }
1465 
1467  Result visit_int(int value) {
1468  return FMT_DISPATCH(visit_any_int(value));
1469  }
1470 
1472  Result visit_long_long(LongLong value) {
1473  return FMT_DISPATCH(visit_any_int(value));
1474  }
1475 
1477  Result visit_uint(unsigned value) {
1478  return FMT_DISPATCH(visit_any_int(value));
1479  }
1480 
1482  Result visit_ulong_long(ULongLong value) {
1483  return FMT_DISPATCH(visit_any_int(value));
1484  }
1485 
1487  Result visit_bool(bool value) {
1488  return FMT_DISPATCH(visit_any_int(value));
1489  }
1490 
1492  Result visit_char(int value) {
1493  return FMT_DISPATCH(visit_any_int(value));
1494  }
1495 
1497  template <typename T>
1498  Result visit_any_int(T) {
1499  return FMT_DISPATCH(visit_unhandled_arg());
1500  }
1501 
1503  Result visit_double(double value) {
1504  return FMT_DISPATCH(visit_any_double(value));
1505  }
1506 
1508  Result visit_long_double(long double value) {
1509  return FMT_DISPATCH(visit_any_double(value));
1510  }
1511 
1513  template <typename T>
1514  Result visit_any_double(T) {
1515  return FMT_DISPATCH(visit_unhandled_arg());
1516  }
1517 
1519  Result visit_cstring(const char *) {
1520  return FMT_DISPATCH(visit_unhandled_arg());
1521  }
1522 
1525  return FMT_DISPATCH(visit_unhandled_arg());
1526  }
1527 
1530  return FMT_DISPATCH(visit_unhandled_arg());
1531  }
1532 
1534  Result visit_pointer(const void *) {
1535  return FMT_DISPATCH(visit_unhandled_arg());
1536  }
1537 
1540  return FMT_DISPATCH(visit_unhandled_arg());
1541  }
1542 
1551  Result visit(const Arg &arg) {
1552  switch (arg.type) {
1553  case Arg::NONE:
1554  case Arg::NAMED_ARG:
1555  FMT_ASSERT(false, "invalid argument type");
1556  break;
1557  case Arg::INT:
1558  return FMT_DISPATCH(visit_int(arg.int_value));
1559  case Arg::UINT:
1560  return FMT_DISPATCH(visit_uint(arg.uint_value));
1561  case Arg::LONG_LONG:
1562  return FMT_DISPATCH(visit_long_long(arg.long_long_value));
1563  case Arg::ULONG_LONG:
1564  return FMT_DISPATCH(visit_ulong_long(arg.ulong_long_value));
1565  case Arg::BOOL:
1566  return FMT_DISPATCH(visit_bool(arg.int_value != 0));
1567  case Arg::CHAR:
1568  return FMT_DISPATCH(visit_char(arg.int_value));
1569  case Arg::DOUBLE:
1570  return FMT_DISPATCH(visit_double(arg.double_value));
1571  case Arg::LONG_DOUBLE:
1572  return FMT_DISPATCH(visit_long_double(arg.long_double_value));
1573  case Arg::CSTRING:
1574  return FMT_DISPATCH(visit_cstring(arg.string.value));
1575  case Arg::STRING:
1576  return FMT_DISPATCH(visit_string(arg.string));
1577  case Arg::WSTRING:
1578  return FMT_DISPATCH(visit_wstring(arg.wstring));
1579  case Arg::POINTER:
1580  return FMT_DISPATCH(visit_pointer(arg.pointer));
1581  case Arg::CUSTOM:
1582  return FMT_DISPATCH(visit_custom(arg.custom));
1583  }
1584  return Result();
1585  }
1586 };
1587 
1590 };
1591 
1592 // Flags.
1593 enum {
1595  CHAR_FLAG = 0x10 // Argument has char type - used in error reporting.
1596 };
1597 
1598 // An empty format specifier.
1599 struct EmptySpec {};
1600 
1601 // A type specifier.
1602 template <char TYPE>
1604  Alignment align() const { return ALIGN_DEFAULT; }
1605  unsigned width() const { return 0; }
1606  int precision() const { return -1; }
1607  bool flag(unsigned) const { return false; }
1608  char type() const { return TYPE; }
1609  char fill() const { return ' '; }
1610 };
1611 
1612 // A width specifier.
1613 struct WidthSpec {
1614  unsigned width_;
1615  // Fill is always wchar_t and cast to char if necessary to avoid having
1616  // two specialization of WidthSpec and its subclasses.
1617  wchar_t fill_;
1618 
1619  WidthSpec(unsigned width, wchar_t fill) : width_(width), fill_(fill) {}
1620 
1621  unsigned width() const { return width_; }
1622  wchar_t fill() const { return fill_; }
1623 };
1624 
1625 // An alignment specifier.
1628 
1629  AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT)
1630  : WidthSpec(width, fill), align_(align) {}
1631 
1632  Alignment align() const { return align_; }
1633 
1634  int precision() const { return -1; }
1635 };
1636 
1637 // An alignment and type specifier.
1638 template <char TYPE>
1640  AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {}
1641 
1642  bool flag(unsigned) const { return false; }
1643  char type() const { return TYPE; }
1644 };
1645 
1646 // A full format specifier.
1648  unsigned flags_;
1650  char type_;
1651 
1653  unsigned width = 0, char type = 0, wchar_t fill = ' ')
1654  : AlignSpec(width, fill), flags_(0), precision_(-1), type_(type) {}
1655 
1656  bool flag(unsigned f) const { return (flags_ & f) != 0; }
1657  int precision() const { return precision_; }
1658  char type() const { return type_; }
1659 };
1660 
1661 // An integer format specifier.
1662 template <typename T, typename SpecT = TypeSpec<0>, typename Char = char>
1663 class IntFormatSpec : public SpecT {
1664  private:
1666 
1667  public:
1668  IntFormatSpec(T val, const SpecT &spec = SpecT())
1669  : SpecT(spec), value_(val) {}
1670 
1671  T value() const { return value_; }
1672 };
1673 
1674 // A string format specifier.
1675 template <typename Char>
1676 class StrFormatSpec : public AlignSpec {
1677  private:
1678  const Char *str_;
1679 
1680  public:
1681  template <typename FillChar>
1682  StrFormatSpec(const Char *str, unsigned width, FillChar fill)
1683  : AlignSpec(width, fill), str_(str) {
1685  }
1686 
1687  const Char *str() const { return str_; }
1688 };
1689 
1694 
1699 
1705 
1711 
1726 template <char TYPE_CODE, typename Char>
1728  int value, unsigned width, Char fill = ' ');
1729 
1730 #define FMT_DEFINE_INT_FORMATTERS(TYPE) \
1731 inline IntFormatSpec<TYPE, TypeSpec<'b'> > bin(TYPE value) { \
1732  return IntFormatSpec<TYPE, TypeSpec<'b'> >(value, TypeSpec<'b'>()); \
1733 } \
1734  \
1735 inline IntFormatSpec<TYPE, TypeSpec<'o'> > oct(TYPE value) { \
1736  return IntFormatSpec<TYPE, TypeSpec<'o'> >(value, TypeSpec<'o'>()); \
1737 } \
1738  \
1739 inline IntFormatSpec<TYPE, TypeSpec<'x'> > hex(TYPE value) { \
1740  return IntFormatSpec<TYPE, TypeSpec<'x'> >(value, TypeSpec<'x'>()); \
1741 } \
1742  \
1743 inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
1744  return IntFormatSpec<TYPE, TypeSpec<'X'> >(value, TypeSpec<'X'>()); \
1745 } \
1746  \
1747 template <char TYPE_CODE> \
1748 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> > pad( \
1749  IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, unsigned width) { \
1750  return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> >( \
1751  f.value(), AlignTypeSpec<TYPE_CODE>(width, ' ')); \
1752 } \
1753  \
1754 /* For compatibility with older compilers we provide two overloads for pad, */ \
1755 /* one that takes a fill character and one that doesn't. In the future this */ \
1756 /* can be replaced with one overload making the template argument Char */ \
1757 /* default to char (C++11). */ \
1758 template <char TYPE_CODE, typename Char> \
1759 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char> pad( \
1760  IntFormatSpec<TYPE, TypeSpec<TYPE_CODE>, Char> f, \
1761  unsigned width, Char fill) { \
1762  return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char>( \
1763  f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \
1764 } \
1765  \
1766 inline IntFormatSpec<TYPE, AlignTypeSpec<0> > pad( \
1767  TYPE value, unsigned width) { \
1768  return IntFormatSpec<TYPE, AlignTypeSpec<0> >( \
1769  value, AlignTypeSpec<0>(width, ' ')); \
1770 } \
1771  \
1772 template <typename Char> \
1773 inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
1774  TYPE value, unsigned width, Char fill) { \
1775  return IntFormatSpec<TYPE, AlignTypeSpec<0>, Char>( \
1776  value, AlignTypeSpec<0>(width, fill)); \
1777 }
1778 
1781 FMT_DEFINE_INT_FORMATTERS(unsigned)
1782 FMT_DEFINE_INT_FORMATTERS(unsigned long)
1783 FMT_DEFINE_INT_FORMATTERS(LongLong)
1784 FMT_DEFINE_INT_FORMATTERS(ULongLong)
1785 
1798 template <typename Char>
1799 inline StrFormatSpec<Char> pad(
1800  const Char *str, unsigned width, Char fill = ' ') {
1801  return StrFormatSpec<Char>(str, width, fill);
1802 }
1803 
1805  const wchar_t *str, unsigned width, char fill = ' ') {
1806  return StrFormatSpec<wchar_t>(str, width, fill);
1807 }
1808 
1809 namespace internal {
1810 
1811 template <typename Char>
1812 class ArgMap {
1813  private:
1814  typedef std::vector<
1815  std::pair<fmt::BasicStringRef<Char>, internal::Arg> > MapType;
1816  typedef typename MapType::value_type Pair;
1817 
1818  MapType map_;
1819 
1820  public:
1821  FMT_API void init(const ArgList &args);
1822 
1824  // The list is unsorted, so just return the first matching name.
1825  for (typename MapType::const_iterator it = map_.begin(), end = map_.end();
1826  it != end; ++it) {
1827  if (it->first == name)
1828  return &it->second;
1829  }
1830  return 0;
1831  }
1832 };
1833 
1834 template <typename Impl, typename Char>
1835 class ArgFormatterBase : public ArgVisitor<Impl, void> {
1836  private:
1839 
1841 
1842  void write_pointer(const void *p) {
1843  spec_.flags_ = HASH_FLAG;
1844  spec_.type_ = 'x';
1845  writer_.write_int(reinterpret_cast<uintptr_t>(p), spec_);
1846  }
1847 
1848  protected:
1849  BasicWriter<Char> &writer() { return writer_; }
1850  FormatSpec &spec() { return spec_; }
1851 
1852  void write(bool value) {
1853  const char *str_value = value ? "true" : "false";
1854  Arg::StringValue<char> str = { str_value, std::strlen(str_value) };
1855  writer_.write_str(str, spec_);
1856  }
1857 
1858  void write(const char *value) {
1859  Arg::StringValue<char> str = {value, value != 0 ? std::strlen(value) : 0};
1860  writer_.write_str(str, spec_);
1861  }
1862 
1863  public:
1865  : writer_(w), spec_(s) {}
1866 
1867  template <typename T>
1868  void visit_any_int(T value) { writer_.write_int(value, spec_); }
1869 
1870  template <typename T>
1871  void visit_any_double(T value) { writer_.write_double(value, spec_); }
1872 
1873  void visit_bool(bool value) {
1874  if (spec_.type_)
1875  return visit_any_int(value);
1876  write(value);
1877  }
1878 
1879  void visit_char(int value) {
1880  if (spec_.type_ && spec_.type_ != 'c') {
1881  spec_.flags_ |= CHAR_FLAG;
1882  writer_.write_int(value, spec_);
1883  return;
1884  }
1885  if (spec_.align_ == ALIGN_NUMERIC || spec_.flags_ != 0)
1886  FMT_THROW(FormatError("invalid format specifier for char"));
1887  typedef typename BasicWriter<Char>::CharPtr CharPtr;
1888  Char fill = internal::CharTraits<Char>::cast(spec_.fill());
1889  CharPtr out = CharPtr();
1890  const unsigned CHAR_WIDTH = 1;
1891  if (spec_.width_ > CHAR_WIDTH) {
1892  out = writer_.grow_buffer(spec_.width_);
1893  if (spec_.align_ == ALIGN_RIGHT) {
1894  std::uninitialized_fill_n(out, spec_.width_ - CHAR_WIDTH, fill);
1895  out += spec_.width_ - CHAR_WIDTH;
1896  } else if (spec_.align_ == ALIGN_CENTER) {
1897  out = writer_.fill_padding(out, spec_.width_,
1898  internal::const_check(CHAR_WIDTH), fill);
1899  } else {
1900  std::uninitialized_fill_n(out + CHAR_WIDTH,
1901  spec_.width_ - CHAR_WIDTH, fill);
1902  }
1903  } else {
1904  out = writer_.grow_buffer(CHAR_WIDTH);
1905  }
1906  *out = internal::CharTraits<Char>::cast(value);
1907  }
1908 
1909  void visit_cstring(const char *value) {
1910  if (spec_.type_ == 'p')
1911  return write_pointer(value);
1912  write(value);
1913  }
1914 
1916  writer_.write_str(value, spec_);
1917  }
1918 
1920 
1922  writer_.write_str(value, spec_);
1923  }
1924 
1925  void visit_pointer(const void *value) {
1926  if (spec_.type_ && spec_.type_ != 'p')
1927  report_unknown_type(spec_.type_, "pointer");
1928  write_pointer(value);
1929  }
1930 };
1931 
1933  private:
1936 
1937  // Returns the argument with specified index.
1938  FMT_API Arg do_get_arg(unsigned arg_index, const char *&error);
1939 
1940  protected:
1941  const ArgList &args() const { return args_; }
1942 
1943  explicit FormatterBase(const ArgList &args) {
1944  args_ = args;
1945  next_arg_index_ = 0;
1946  }
1947 
1948  // Returns the next argument.
1949  Arg next_arg(const char *&error) {
1950  if (next_arg_index_ >= 0)
1951  return do_get_arg(internal::to_unsigned(next_arg_index_++), error);
1952  error = "cannot switch from manual to automatic argument indexing";
1953  return Arg();
1954  }
1955 
1956  // Checks if manual indexing is used and returns the argument with
1957  // specified index.
1958  Arg get_arg(unsigned arg_index, const char *&error) {
1959  return check_no_auto_index(error) ? do_get_arg(arg_index, error) : Arg();
1960  }
1961 
1962  bool check_no_auto_index(const char *&error) {
1963  if (next_arg_index_ > 0) {
1964  error = "cannot switch from automatic to manual argument indexing";
1965  return false;
1966  }
1967  next_arg_index_ = -1;
1968  return true;
1969  }
1970 
1971  template <typename Char>
1972  void write(BasicWriter<Char> &w, const Char *start, const Char *end) {
1973  if (start != end)
1975  }
1976 };
1977 } // namespace internal
1978 
1996 template <typename Impl, typename Char>
1997 class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
1998  private:
2000  const Char *format_;
2001 
2002  public:
2012  FormatSpec &spec, const Char *fmt)
2013  : internal::ArgFormatterBase<Impl, Char>(formatter.writer(), spec),
2014  formatter_(formatter), format_(fmt) {}
2015 
2018  c.format(&formatter_, c.value, &format_);
2019  }
2020 };
2021 
2023 template <typename Char>
2024 class ArgFormatter : public BasicArgFormatter<ArgFormatter<Char>, Char> {
2025  public:
2028  FormatSpec &spec, const Char *fmt)
2029  : BasicArgFormatter<ArgFormatter<Char>, Char>(formatter, spec, fmt) {}
2030 };
2031 
2033 template <typename CharType, typename ArgFormatter>
2034 class BasicFormatter : private internal::FormatterBase {
2035  public:
2037  typedef CharType Char;
2038 
2039  private:
2042 
2044 
2046 
2047  // Checks if manual indexing is used and returns the argument with
2048  // specified name.
2049  internal::Arg get_arg(BasicStringRef<Char> arg_name, const char *&error);
2050 
2051  // Parses argument index and returns corresponding argument.
2052  internal::Arg parse_arg_index(const Char *&s);
2053 
2054  // Parses argument name and returns corresponding argument.
2055  internal::Arg parse_arg_name(const Char *&s);
2056 
2057  public:
2066  : internal::FormatterBase(args), writer_(w) {}
2067 
2069  BasicWriter<Char> &writer() { return writer_; }
2070 
2072  void format(BasicCStringRef<Char> format_str);
2073 
2074  // Formats a single argument and advances format_str, a format string pointer.
2075  const Char *format(const Char *&format_str, const internal::Arg &arg);
2076 };
2077 
2078 // Generates a comma-separated list with results of applying f to
2079 // numbers 0..n-1.
2080 # define FMT_GEN(n, f) FMT_GEN##n(f)
2081 # define FMT_GEN1(f) f(0)
2082 # define FMT_GEN2(f) FMT_GEN1(f), f(1)
2083 # define FMT_GEN3(f) FMT_GEN2(f), f(2)
2084 # define FMT_GEN4(f) FMT_GEN3(f), f(3)
2085 # define FMT_GEN5(f) FMT_GEN4(f), f(4)
2086 # define FMT_GEN6(f) FMT_GEN5(f), f(5)
2087 # define FMT_GEN7(f) FMT_GEN6(f), f(6)
2088 # define FMT_GEN8(f) FMT_GEN7(f), f(7)
2089 # define FMT_GEN9(f) FMT_GEN8(f), f(8)
2090 # define FMT_GEN10(f) FMT_GEN9(f), f(9)
2091 # define FMT_GEN11(f) FMT_GEN10(f), f(10)
2092 # define FMT_GEN12(f) FMT_GEN11(f), f(11)
2093 # define FMT_GEN13(f) FMT_GEN12(f), f(12)
2094 # define FMT_GEN14(f) FMT_GEN13(f), f(13)
2095 # define FMT_GEN15(f) FMT_GEN14(f), f(14)
2096 
2097 namespace internal {
2098 inline uint64_t make_type() { return 0; }
2099 
2100 template <typename T>
2101 inline uint64_t make_type(const T &arg) {
2103 }
2104 
2105 template <unsigned N, bool/*IsPacked*/= (N < ArgList::MAX_PACKED_ARGS)>
2106 struct ArgArray;
2107 
2108 template <unsigned N>
2109 struct ArgArray<N, true/*IsPacked*/> {
2110  typedef Value Type[N > 0 ? N : 1];
2111 
2112  template <typename Formatter, typename T>
2113  static Value make(const T &value) {
2114 #ifdef __clang__
2116  // Workaround a bug in Apple LLVM version 4.2 (clang-425.0.28) of clang:
2117  // https://github.com/fmtlib/fmt/issues/276
2118  (void)result.custom.format;
2119  return result;
2120 #else
2121  return MakeValue<Formatter>(value);
2122 #endif
2123  }
2124 };
2125 
2126 template <unsigned N>
2127 struct ArgArray<N, false/*IsPacked*/> {
2128  typedef Arg Type[N + 1]; // +1 for the list end Arg::NONE
2129 
2130  template <typename Formatter, typename T>
2131  static Arg make(const T &value) { return MakeArg<Formatter>(value); }
2132 };
2133 
2134 #if FMT_USE_VARIADIC_TEMPLATES
2135 template <typename Arg, typename... Args>
2136 inline uint64_t make_type(const Arg &first, const Args & ... tail) {
2137  return make_type(first) | (make_type(tail...) << 4);
2138 }
2139 
2140 #else
2141 
2142 struct ArgType {
2144 
2145  ArgType() : type(0) {}
2146 
2147  template <typename T>
2148  ArgType(const T &arg) : type(make_type(arg)) {}
2149 };
2150 
2151 # define FMT_ARG_TYPE_DEFAULT(n) ArgType t##n = ArgType()
2152 
2154  return t0.type | (t1.type << 4) | (t2.type << 8) | (t3.type << 12) |
2155  (t4.type << 16) | (t5.type << 20) | (t6.type << 24) | (t7.type << 28) |
2156  (t8.type << 32) | (t9.type << 36) | (t10.type << 40) | (t11.type << 44) |
2157  (t12.type << 48) | (t13.type << 52) | (t14.type << 56);
2158 }
2159 #endif
2160 } // namespace internal
2161 
2162 # define FMT_MAKE_TEMPLATE_ARG(n) typename T##n
2163 # define FMT_MAKE_ARG_TYPE(n) T##n
2164 # define FMT_MAKE_ARG(n) const T##n &v##n
2165 # define FMT_ASSIGN_char(n) \
2166  arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter<char> >(v##n)
2167 # define FMT_ASSIGN_wchar_t(n) \
2168  arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter<wchar_t> >(v##n)
2169 
2170 #if FMT_USE_VARIADIC_TEMPLATES
2171 // Defines a variadic function returning void.
2172 # define FMT_VARIADIC_VOID(func, arg_type) \
2173  template <typename... Args> \
2174  void func(arg_type arg0, const Args & ... args) { \
2175  typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
2176  typename ArgArray::Type array{ \
2177  ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
2178  func(arg0, fmt::ArgList(fmt::internal::make_type(args...), array)); \
2179  }
2180 
2181 // Defines a variadic constructor.
2182 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
2183  template <typename... Args> \
2184  ctor(arg0_type arg0, arg1_type arg1, const Args & ... args) { \
2185  typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
2186  typename ArgArray::Type array{ \
2187  ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
2188  func(arg0, arg1, fmt::ArgList(fmt::internal::make_type(args...), array)); \
2189  }
2190 
2191 #else
2192 
2193 # define FMT_MAKE_REF(n) \
2194  fmt::internal::MakeValue< fmt::BasicFormatter<Char> >(v##n)
2195 # define FMT_MAKE_REF2(n) v##n
2196 
2197 // Defines a wrapper for a function taking one argument of type arg_type
2198 // and n additional arguments of arbitrary types.
2199 # define FMT_WRAP1(func, arg_type, n) \
2200  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2201  inline void func(arg_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
2202  const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
2203  func(arg1, fmt::ArgList( \
2204  fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
2205  }
2206 
2207 // Emulates a variadic function returning void on a pre-C++11 compiler.
2208 # define FMT_VARIADIC_VOID(func, arg_type) \
2209  inline void func(arg_type arg) { func(arg, fmt::ArgList()); } \
2210  FMT_WRAP1(func, arg_type, 1) FMT_WRAP1(func, arg_type, 2) \
2211  FMT_WRAP1(func, arg_type, 3) FMT_WRAP1(func, arg_type, 4) \
2212  FMT_WRAP1(func, arg_type, 5) FMT_WRAP1(func, arg_type, 6) \
2213  FMT_WRAP1(func, arg_type, 7) FMT_WRAP1(func, arg_type, 8) \
2214  FMT_WRAP1(func, arg_type, 9) FMT_WRAP1(func, arg_type, 10)
2215 
2216 # define FMT_CTOR(ctor, func, arg0_type, arg1_type, n) \
2217  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2218  ctor(arg0_type arg0, arg1_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
2219  const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
2220  func(arg0, arg1, fmt::ArgList( \
2221  fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
2222  }
2223 
2224 // Emulates a variadic constructor on a pre-C++11 compiler.
2225 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
2226  FMT_CTOR(ctor, func, arg0_type, arg1_type, 1) \
2227  FMT_CTOR(ctor, func, arg0_type, arg1_type, 2) \
2228  FMT_CTOR(ctor, func, arg0_type, arg1_type, 3) \
2229  FMT_CTOR(ctor, func, arg0_type, arg1_type, 4) \
2230  FMT_CTOR(ctor, func, arg0_type, arg1_type, 5) \
2231  FMT_CTOR(ctor, func, arg0_type, arg1_type, 6) \
2232  FMT_CTOR(ctor, func, arg0_type, arg1_type, 7) \
2233  FMT_CTOR(ctor, func, arg0_type, arg1_type, 8) \
2234  FMT_CTOR(ctor, func, arg0_type, arg1_type, 9) \
2235  FMT_CTOR(ctor, func, arg0_type, arg1_type, 10)
2236 #endif
2237 
2238 // Generates a comma-separated list with results of applying f to pairs
2239 // (argument, index).
2240 #define FMT_FOR_EACH1(f, x0) f(x0, 0)
2241 #define FMT_FOR_EACH2(f, x0, x1) \
2242  FMT_FOR_EACH1(f, x0), f(x1, 1)
2243 #define FMT_FOR_EACH3(f, x0, x1, x2) \
2244  FMT_FOR_EACH2(f, x0 ,x1), f(x2, 2)
2245 #define FMT_FOR_EACH4(f, x0, x1, x2, x3) \
2246  FMT_FOR_EACH3(f, x0, x1, x2), f(x3, 3)
2247 #define FMT_FOR_EACH5(f, x0, x1, x2, x3, x4) \
2248  FMT_FOR_EACH4(f, x0, x1, x2, x3), f(x4, 4)
2249 #define FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5) \
2250  FMT_FOR_EACH5(f, x0, x1, x2, x3, x4), f(x5, 5)
2251 #define FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6) \
2252  FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5), f(x6, 6)
2253 #define FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7) \
2254  FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6), f(x7, 7)
2255 #define FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8) \
2256  FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7), f(x8, 8)
2257 #define FMT_FOR_EACH10(f, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) \
2258  FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8), f(x9, 9)
2259 
2265  private:
2266  void init(int err_code, CStringRef format_str, ArgList args);
2267 
2268  protected:
2270 
2271  typedef char Char; // For FMT_VARIADIC_CTOR.
2272 
2274 
2275  public:
2294  SystemError(int error_code, CStringRef message) {
2295  init(error_code, message, ArgList());
2296  }
2297  FMT_VARIADIC_CTOR(SystemError, init, int, CStringRef)
2298 
2299  ~SystemError() throw();
2300 
2301  int error_code() const { return error_code_; }
2302 };
2303 
2320 FMT_API void format_system_error(fmt::Writer &out, int error_code,
2321  fmt::StringRef message) FMT_NOEXCEPT;
2322 
2341 template <typename Char>
2342 class BasicWriter {
2343  private:
2344  // Output buffer.
2346 
2348 
2350 
2351 #if FMT_SECURE_SCL
2352  // Returns pointer value.
2353  static Char *get(CharPtr p) { return p.base(); }
2354 #else
2355  static Char *get(Char *p) { return p; }
2356 #endif
2357 
2358  // Fills the padding around the content and returns the pointer to the
2359  // content area.
2360  static CharPtr fill_padding(CharPtr buffer,
2361  unsigned total_size, std::size_t content_size, wchar_t fill);
2362 
2363  // Grows the buffer by n characters and returns a pointer to the newly
2364  // allocated area.
2365  CharPtr grow_buffer(std::size_t n) {
2366  std::size_t size = buffer_.size();
2367  buffer_.resize(size + n);
2368  return internal::make_ptr(&buffer_[size], n);
2369  }
2370 
2371  // Writes an unsigned decimal integer.
2372  template <typename UInt>
2373  Char *write_unsigned_decimal(UInt value, unsigned prefix_size = 0) {
2374  unsigned num_digits = internal::count_digits(value);
2375  Char *ptr = get(grow_buffer(prefix_size + num_digits));
2376  internal::format_decimal(ptr + prefix_size, value, num_digits);
2377  return ptr;
2378  }
2379 
2380  // Writes a decimal integer.
2381  template <typename Int>
2382  void write_decimal(Int value) {
2383  typedef typename internal::IntTraits<Int>::MainType MainType;
2384  MainType abs_value = static_cast<MainType>(value);
2385  if (internal::is_negative(value)) {
2386  abs_value = 0 - abs_value;
2387  *write_unsigned_decimal(abs_value, 1) = '-';
2388  } else {
2389  write_unsigned_decimal(abs_value, 0);
2390  }
2391  }
2392 
2393  // Prepare a buffer for integer formatting.
2394  CharPtr prepare_int_buffer(unsigned num_digits,
2395  const EmptySpec &, const char *prefix, unsigned prefix_size) {
2396  unsigned size = prefix_size + num_digits;
2397  CharPtr p = grow_buffer(size);
2398  std::uninitialized_copy(prefix, prefix + prefix_size, p);
2399  return p + size - 1;
2400  }
2401 
2402  template <typename Spec>
2403  CharPtr prepare_int_buffer(unsigned num_digits,
2404  const Spec &spec, const char *prefix, unsigned prefix_size);
2405 
2406  // Formats an integer.
2407  template <typename T, typename Spec>
2408  void write_int(T value, Spec spec);
2409 
2410  // Formats a floating-point number (double or long double).
2411  template <typename T>
2412  void write_double(T value, const FormatSpec &spec);
2413 
2414  // Writes a formatted string.
2415  template <typename StrChar>
2416  CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec);
2417 
2418  template <typename StrChar>
2419  void write_str(const internal::Arg::StringValue<StrChar> &str,
2420  const FormatSpec &spec);
2421 
2422  // This following methods are private to disallow writing wide characters
2423  // and strings to a char stream. If you want to print a wide string as a
2424  // pointer as std::ostream does, cast it to const void*.
2425  // Do not implement!
2426  void operator<<(typename internal::WCharHelper<wchar_t, Char>::Unsupported);
2427  void operator<<(
2429 
2430  // Appends floating-point length specifier to the format string.
2431  // The second argument is only used for overload resolution.
2432  void append_float_length(Char *&format_ptr, long double) {
2433  *format_ptr++ = 'L';
2434  }
2435 
2436  template<typename T>
2437  void append_float_length(Char *&, T) {}
2438 
2439  template <typename Impl, typename Char_>
2441 
2442  template <typename Impl, typename Char_>
2444 
2445  protected:
2449  explicit BasicWriter(Buffer<Char> &b) : buffer_(b) {}
2450 
2451  public:
2457  virtual ~BasicWriter() {}
2458 
2462  std::size_t size() const { return buffer_.size(); }
2463 
2468  const Char *data() const FMT_NOEXCEPT { return &buffer_[0]; }
2469 
2474  const Char *c_str() const {
2475  std::size_t size = buffer_.size();
2476  buffer_.reserve(size + 1);
2477  buffer_[size] = '\0';
2478  return &buffer_[0];
2479  }
2480 
2486  std::basic_string<Char> str() const {
2487  return std::basic_string<Char>(&buffer_[0], buffer_.size());
2488  }
2489 
2516  BasicFormatter<Char>(args, *this).format(format);
2517  }
2519 
2520  BasicWriter &operator<<(int value) {
2521  write_decimal(value);
2522  return *this;
2523  }
2524  BasicWriter &operator<<(unsigned value) {
2525  return *this << IntFormatSpec<unsigned>(value);
2526  }
2527  BasicWriter &operator<<(long value) {
2528  write_decimal(value);
2529  return *this;
2530  }
2531  BasicWriter &operator<<(unsigned long value) {
2532  return *this << IntFormatSpec<unsigned long>(value);
2533  }
2534  BasicWriter &operator<<(LongLong value) {
2535  write_decimal(value);
2536  return *this;
2537  }
2538 
2544  BasicWriter &operator<<(ULongLong value) {
2545  return *this << IntFormatSpec<ULongLong>(value);
2546  }
2547 
2548  BasicWriter &operator<<(double value) {
2549  write_double(value, FormatSpec());
2550  return *this;
2551  }
2552 
2559  BasicWriter &operator<<(long double value) {
2560  write_double(value, FormatSpec());
2561  return *this;
2562  }
2563 
2567  BasicWriter &operator<<(char value) {
2568  buffer_.push_back(value);
2569  return *this;
2570  }
2571 
2574  buffer_.push_back(value);
2575  return *this;
2576  }
2577 
2583  BasicWriter &operator<<(fmt::BasicStringRef<Char> value) {
2584  const Char *str = value.data();
2585  buffer_.append(str, str + value.size());
2586  return *this;
2587  }
2588 
2591  const char *str = value.data();
2592  buffer_.append(str, str + value.size());
2593  return *this;
2594  }
2595 
2596  template <typename T, typename Spec, typename FillChar>
2597  BasicWriter &operator<<(IntFormatSpec<T, Spec, FillChar> spec) {
2599  write_int(spec.value(), spec);
2600  return *this;
2601  }
2602 
2603  template <typename StrChar>
2604  BasicWriter &operator<<(const StrFormatSpec<StrChar> &spec) {
2605  const StrChar *s = spec.str();
2606  write_str(s, std::char_traits<Char>::length(s), spec);
2607  return *this;
2608  }
2609 
2610  void clear() FMT_NOEXCEPT { buffer_.clear(); }
2611 
2612  Buffer<Char> &buffer() FMT_NOEXCEPT { return buffer_; }
2613 };
2614 
2615 template <typename Char>
2616 template <typename StrChar>
2618  const StrChar *s, std::size_t size, const AlignSpec &spec) {
2619  CharPtr out = CharPtr();
2620  if (spec.width() > size) {
2621  out = grow_buffer(spec.width());
2622  Char fill = internal::CharTraits<Char>::cast(spec.fill());
2623  if (spec.align() == ALIGN_RIGHT) {
2624  std::uninitialized_fill_n(out, spec.width() - size, fill);
2625  out += spec.width() - size;
2626  } else if (spec.align() == ALIGN_CENTER) {
2627  out = fill_padding(out, spec.width(), size, fill);
2628  } else {
2629  std::uninitialized_fill_n(out + size, spec.width() - size, fill);
2630  }
2631  } else {
2632  out = grow_buffer(size);
2633  }
2634  std::uninitialized_copy(s, s + size, out);
2635  return out;
2636 }
2637 
2638 template <typename Char>
2639 template <typename StrChar>
2641  const internal::Arg::StringValue<StrChar> &s, const FormatSpec &spec) {
2642  // Check if StrChar is convertible to Char.
2644  if (spec.type_ && spec.type_ != 's')
2645  internal::report_unknown_type(spec.type_, "string");
2646  const StrChar *str_value = s.value;
2647  std::size_t str_size = s.size;
2648  if (str_size == 0) {
2649  if (!str_value) {
2650  FMT_THROW(FormatError("string pointer is null"));
2651  }
2652  }
2653  std::size_t precision = static_cast<std::size_t>(spec.precision_);
2654  if (spec.precision_ >= 0 && precision < str_size)
2655  str_size = precision;
2656  write_str(str_value, str_size, spec);
2657 }
2658 
2659 template <typename Char>
2662  CharPtr buffer, unsigned total_size,
2663  std::size_t content_size, wchar_t fill) {
2664  std::size_t padding = total_size - content_size;
2665  std::size_t left_padding = padding / 2;
2666  Char fill_char = internal::CharTraits<Char>::cast(fill);
2667  std::uninitialized_fill_n(buffer, left_padding, fill_char);
2668  buffer += left_padding;
2669  CharPtr content = buffer;
2670  std::uninitialized_fill_n(buffer + content_size,
2671  padding - left_padding, fill_char);
2672  return content;
2673 }
2674 
2675 template <typename Char>
2676 template <typename Spec>
2679  unsigned num_digits, const Spec &spec,
2680  const char *prefix, unsigned prefix_size) {
2681  unsigned width = spec.width();
2682  Alignment align = spec.align();
2683  Char fill = internal::CharTraits<Char>::cast(spec.fill());
2684  if (spec.precision() > static_cast<int>(num_digits)) {
2685  // Octal prefix '0' is counted as a digit, so ignore it if precision
2686  // is specified.
2687  if (prefix_size > 0 && prefix[prefix_size - 1] == '0')
2688  --prefix_size;
2689  unsigned number_size =
2690  prefix_size + internal::to_unsigned(spec.precision());
2691  AlignSpec subspec(number_size, '0', ALIGN_NUMERIC);
2692  if (number_size >= width)
2693  return prepare_int_buffer(num_digits, subspec, prefix, prefix_size);
2694  buffer_.reserve(width);
2695  unsigned fill_size = width - number_size;
2696  if (align != ALIGN_LEFT) {
2697  CharPtr p = grow_buffer(fill_size);
2698  std::uninitialized_fill(p, p + fill_size, fill);
2699  }
2700  CharPtr result = prepare_int_buffer(
2701  num_digits, subspec, prefix, prefix_size);
2702  if (align == ALIGN_LEFT) {
2703  CharPtr p = grow_buffer(fill_size);
2704  std::uninitialized_fill(p, p + fill_size, fill);
2705  }
2706  return result;
2707  }
2708  unsigned size = prefix_size + num_digits;
2709  if (width <= size) {
2710  CharPtr p = grow_buffer(size);
2711  std::uninitialized_copy(prefix, prefix + prefix_size, p);
2712  return p + size - 1;
2713  }
2714  CharPtr p = grow_buffer(width);
2715  CharPtr end = p + width;
2716  if (align == ALIGN_LEFT) {
2717  std::uninitialized_copy(prefix, prefix + prefix_size, p);
2718  p += size;
2719  std::uninitialized_fill(p, end, fill);
2720  } else if (align == ALIGN_CENTER) {
2721  p = fill_padding(p, width, size, fill);
2722  std::uninitialized_copy(prefix, prefix + prefix_size, p);
2723  p += size;
2724  } else {
2725  if (align == ALIGN_NUMERIC) {
2726  if (prefix_size != 0) {
2727  p = std::uninitialized_copy(prefix, prefix + prefix_size, p);
2728  size -= prefix_size;
2729  }
2730  } else {
2731  std::uninitialized_copy(prefix, prefix + prefix_size, end - size);
2732  }
2733  std::uninitialized_fill(p, end - size, fill);
2734  p = end;
2735  }
2736  return p - 1;
2737 }
2738 
2739 template <typename Char>
2740 template <typename T, typename Spec>
2741 void BasicWriter<Char>::write_int(T value, Spec spec) {
2742  unsigned prefix_size = 0;
2743  typedef typename internal::IntTraits<T>::MainType UnsignedType;
2744  UnsignedType abs_value = static_cast<UnsignedType>(value);
2745  char prefix[4] = "";
2746  if (internal::is_negative(value)) {
2747  prefix[0] = '-';
2748  ++prefix_size;
2749  abs_value = 0 - abs_value;
2750  } else if (spec.flag(SIGN_FLAG)) {
2751  prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' ';
2752  ++prefix_size;
2753  }
2754  switch (spec.type()) {
2755  case 0: case 'd': {
2756  unsigned num_digits = internal::count_digits(abs_value);
2757  CharPtr p = prepare_int_buffer(num_digits, spec, prefix, prefix_size) + 1;
2758  internal::format_decimal(get(p), abs_value, 0);
2759  break;
2760  }
2761  case 'x': case 'X': {
2762  UnsignedType n = abs_value;
2763  if (spec.flag(HASH_FLAG)) {
2764  prefix[prefix_size++] = '0';
2765  prefix[prefix_size++] = spec.type();
2766  }
2767  unsigned num_digits = 0;
2768  do {
2769  ++num_digits;
2770  } while ((n >>= 4) != 0);
2771  Char *p = get(prepare_int_buffer(
2772  num_digits, spec, prefix, prefix_size));
2773  n = abs_value;
2774  const char *digits = spec.type() == 'x' ?
2775  "0123456789abcdef" : "0123456789ABCDEF";
2776  do {
2777  *p-- = digits[n & 0xf];
2778  } while ((n >>= 4) != 0);
2779  break;
2780  }
2781  case 'b': case 'B': {
2782  UnsignedType n = abs_value;
2783  if (spec.flag(HASH_FLAG)) {
2784  prefix[prefix_size++] = '0';
2785  prefix[prefix_size++] = spec.type();
2786  }
2787  unsigned num_digits = 0;
2788  do {
2789  ++num_digits;
2790  } while ((n >>= 1) != 0);
2791  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
2792  n = abs_value;
2793  do {
2794  *p-- = static_cast<Char>('0' + (n & 1));
2795  } while ((n >>= 1) != 0);
2796  break;
2797  }
2798  case 'o': {
2799  UnsignedType n = abs_value;
2800  if (spec.flag(HASH_FLAG))
2801  prefix[prefix_size++] = '0';
2802  unsigned num_digits = 0;
2803  do {
2804  ++num_digits;
2805  } while ((n >>= 3) != 0);
2806  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
2807  n = abs_value;
2808  do {
2809  *p-- = static_cast<Char>('0' + (n & 7));
2810  } while ((n >>= 3) != 0);
2811  break;
2812  }
2813  case 'n': {
2814  unsigned num_digits = internal::count_digits(abs_value);
2815  fmt::StringRef sep = internal::thousands_sep(std::localeconv());
2816  unsigned size = static_cast<unsigned>(
2817  num_digits + sep.size() * ((num_digits - 1) / 3));
2818  CharPtr p = prepare_int_buffer(size, spec, prefix, prefix_size) + 1;
2819  internal::format_decimal(get(p), abs_value, 0, internal::ThousandsSep(sep));
2820  break;
2821  }
2822  default:
2824  spec.type(), spec.flag(CHAR_FLAG) ? "char" : "integer");
2825  break;
2826  }
2827 }
2828 
2829 template <typename Char>
2830 template <typename T>
2831 void BasicWriter<Char>::write_double(T value, const FormatSpec &spec) {
2832  // Check type.
2833  char type = spec.type();
2834  bool upper = false;
2835  switch (type) {
2836  case 0:
2837  type = 'g';
2838  break;
2839  case 'e': case 'f': case 'g': case 'a':
2840  break;
2841  case 'F':
2842 #if FMT_MSC_VER
2843  // MSVC's printf doesn't support 'F'.
2844  type = 'f';
2845 #endif
2846  // Fall through.
2847  case 'E': case 'G': case 'A':
2848  upper = true;
2849  break;
2850  default:
2851  internal::report_unknown_type(type, "double");
2852  break;
2853  }
2854 
2855  char sign = 0;
2856  // Use isnegative instead of value < 0 because the latter is always
2857  // false for NaN.
2858  if (internal::FPUtil::isnegative(static_cast<double>(value))) {
2859  sign = '-';
2860  value = -value;
2861  } else if (spec.flag(SIGN_FLAG)) {
2862  sign = spec.flag(PLUS_FLAG) ? '+' : ' ';
2863  }
2864 
2865  if (internal::FPUtil::isnotanumber(value)) {
2866  // Format NaN ourselves because sprintf's output is not consistent
2867  // across platforms.
2868  std::size_t nan_size = 4;
2869  const char *nan = upper ? " NAN" : " nan";
2870  if (!sign) {
2871  --nan_size;
2872  ++nan;
2873  }
2874  CharPtr out = write_str(nan, nan_size, spec);
2875  if (sign)
2876  *out = sign;
2877  return;
2878  }
2879 
2880  if (internal::FPUtil::isinfinity(value)) {
2881  // Format infinity ourselves because sprintf's output is not consistent
2882  // across platforms.
2883  std::size_t inf_size = 4;
2884  const char *inf = upper ? " INF" : " inf";
2885  if (!sign) {
2886  --inf_size;
2887  ++inf;
2888  }
2889  CharPtr out = write_str(inf, inf_size, spec);
2890  if (sign)
2891  *out = sign;
2892  return;
2893  }
2894 
2895  std::size_t offset = buffer_.size();
2896  unsigned width = spec.width();
2897  if (sign) {
2898  buffer_.reserve(buffer_.size() + (width > 1u ? width : 1u));
2899  if (width > 0)
2900  --width;
2901  ++offset;
2902  }
2903 
2904  // Build format string.
2905  enum { MAX_FORMAT_SIZE = 10}; // longest format: %#-*.*Lg
2906  Char format[MAX_FORMAT_SIZE];
2907  Char *format_ptr = format;
2908  *format_ptr++ = '%';
2909  unsigned width_for_sprintf = width;
2910  if (spec.flag(HASH_FLAG))
2911  *format_ptr++ = '#';
2912  if (spec.align() == ALIGN_CENTER) {
2913  width_for_sprintf = 0;
2914  } else {
2915  if (spec.align() == ALIGN_LEFT)
2916  *format_ptr++ = '-';
2917  if (width != 0)
2918  *format_ptr++ = '*';
2919  }
2920  if (spec.precision() >= 0) {
2921  *format_ptr++ = '.';
2922  *format_ptr++ = '*';
2923  }
2924 
2925  append_float_length(format_ptr, value);
2926  *format_ptr++ = type;
2927  *format_ptr = '\0';
2928 
2929  // Format using snprintf.
2930  Char fill = internal::CharTraits<Char>::cast(spec.fill());
2931  unsigned n = 0;
2932  Char *start = 0;
2933  for (;;) {
2934  std::size_t buffer_size = buffer_.capacity() - offset;
2935 #if FMT_MSC_VER
2936  // MSVC's vsnprintf_s doesn't work with zero size, so reserve
2937  // space for at least one extra character to make the size non-zero.
2938  // Note that the buffer's capacity will increase by more than 1.
2939  if (buffer_size == 0) {
2940  buffer_.reserve(offset + 1);
2941  buffer_size = buffer_.capacity() - offset;
2942  }
2943 #endif
2944  start = &buffer_[offset];
2946  start, buffer_size, format, width_for_sprintf, spec.precision(), value);
2947  if (result >= 0) {
2948  n = internal::to_unsigned(result);
2949  if (offset + n < buffer_.capacity())
2950  break; // The buffer is large enough - continue with formatting.
2951  buffer_.reserve(offset + n + 1);
2952  } else {
2953  // If result is negative we ask to increase the capacity by at least 1,
2954  // but as std::vector, the buffer grows exponentially.
2955  buffer_.reserve(buffer_.capacity() + 1);
2956  }
2957  }
2958  if (sign) {
2959  if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) ||
2960  *start != ' ') {
2961  *(start - 1) = sign;
2962  sign = 0;
2963  } else {
2964  *(start - 1) = fill;
2965  }
2966  ++n;
2967  }
2968  if (spec.align() == ALIGN_CENTER && spec.width() > n) {
2969  width = spec.width();
2970  CharPtr p = grow_buffer(width);
2971  std::memmove(get(p) + (width - n) / 2, get(p), n * sizeof(Char));
2972  fill_padding(p, spec.width(), n, fill);
2973  return;
2974  }
2975  if (spec.fill() != ' ' || sign) {
2976  while (*start == ' ')
2977  *start++ = fill;
2978  if (sign)
2979  *(start - 1) = sign;
2980  }
2981  grow_buffer(n);
2982 }
2983 
3018 template <typename Char, typename Allocator = std::allocator<Char> >
3019 class BasicMemoryWriter : public BasicWriter<Char> {
3020  private:
3022 
3023  public:
3024  explicit BasicMemoryWriter(const Allocator& alloc = Allocator())
3025  : BasicWriter<Char>(buffer_), buffer_(alloc) {}
3026 
3027 #if FMT_USE_RVALUE_REFERENCES
3028 
3035  : BasicWriter<Char>(buffer_), buffer_(std::move(other.buffer_)) {
3036  }
3037 
3043  BasicMemoryWriter &operator=(BasicMemoryWriter &&other) {
3044  buffer_ = std::move(other.buffer_);
3045  return *this;
3046  }
3047 #endif
3048 };
3049 
3052 
3073 template <typename Char>
3074 class BasicArrayWriter : public BasicWriter<Char> {
3075  private:
3077 
3078  public:
3085  BasicArrayWriter(Char *array, std::size_t size)
3086  : BasicWriter<Char>(buffer_), buffer_(array, size) {}
3087 
3094  template <std::size_t SIZE>
3095  explicit BasicArrayWriter(Char (&array)[SIZE])
3096  : BasicWriter<Char>(buffer_), buffer_(array, SIZE) {}
3097 };
3098 
3101 
3102 // Reports a system error without throwing an exception.
3103 // Can be used to report errors from destructors.
3104 FMT_API void report_system_error(int error_code,
3105  StringRef message) FMT_NOEXCEPT;
3106 
3107 #if FMT_USE_WINDOWS_H
3108 
3110 class WindowsError : public SystemError {
3111  private:
3112  FMT_API void init(int error_code, CStringRef format_str, ArgList args);
3113 
3114  public:
3143  WindowsError(int error_code, CStringRef message) {
3144  init(error_code, message, ArgList());
3145  }
3146  FMT_VARIADIC_CTOR(WindowsError, init, int, CStringRef)
3147 };
3148 
3149 // Reports a Windows error without throwing an exception.
3150 // Can be used to report errors from destructors.
3151 FMT_API void report_windows_error(int error_code,
3152  StringRef message) FMT_NOEXCEPT;
3153 
3154 #endif
3155 
3157 
3164 FMT_API void print_colored(Color c, CStringRef format, ArgList args);
3165 
3175 inline std::string format(CStringRef format_str, ArgList args) {
3176  MemoryWriter w;
3177  w.write(format_str, args);
3178  return w.str();
3179 }
3180 
3181 inline std::wstring format(WCStringRef format_str, ArgList args) {
3182  WMemoryWriter w;
3183  w.write(format_str, args);
3184  return w.str();
3185 }
3186 
3196 FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args);
3197 
3207 FMT_API void print(CStringRef format_str, ArgList args);
3208 
3212 class FormatInt {
3213  private:
3214  // Buffer should be large enough to hold all digits (digits10 + 1),
3215  // a sign and a null character.
3216  enum {BUFFER_SIZE = std::numeric_limits<ULongLong>::digits10 + 3};
3217  mutable char buffer_[BUFFER_SIZE];
3218  char *str_;
3219 
3220  // Formats value in reverse and returns the number of digits.
3221  char *format_decimal(ULongLong value) {
3222  char *buffer_end = buffer_ + BUFFER_SIZE - 1;
3223  while (value >= 100) {
3224  // Integer division is slow so do it for a group of two digits instead
3225  // of for every digit. The idea comes from the talk by Alexandrescu
3226  // "Three Optimization Tips for C++". See speed-test for a comparison.
3227  unsigned index = static_cast<unsigned>((value % 100) * 2);
3228  value /= 100;
3229  *--buffer_end = internal::Data::DIGITS[index + 1];
3230  *--buffer_end = internal::Data::DIGITS[index];
3231  }
3232  if (value < 10) {
3233  *--buffer_end = static_cast<char>('0' + value);
3234  return buffer_end;
3235  }
3236  unsigned index = static_cast<unsigned>(value * 2);
3237  *--buffer_end = internal::Data::DIGITS[index + 1];
3238  *--buffer_end = internal::Data::DIGITS[index];
3239  return buffer_end;
3240  }
3241 
3242  void FormatSigned(LongLong value) {
3243  ULongLong abs_value = static_cast<ULongLong>(value);
3244  bool negative = value < 0;
3245  if (negative)
3246  abs_value = 0 - abs_value;
3247  str_ = format_decimal(abs_value);
3248  if (negative)
3249  *--str_ = '-';
3250  }
3251 
3252  public:
3253  explicit FormatInt(int value) { FormatSigned(value); }
3254  explicit FormatInt(long value) { FormatSigned(value); }
3255  explicit FormatInt(LongLong value) { FormatSigned(value); }
3256  explicit FormatInt(unsigned value) : str_(format_decimal(value)) {}
3257  explicit FormatInt(unsigned long value) : str_(format_decimal(value)) {}
3258  explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {}
3259 
3261  std::size_t size() const {
3262  return internal::to_unsigned(buffer_ - str_ + BUFFER_SIZE - 1);
3263  }
3264 
3269  const char *data() const { return str_; }
3270 
3275  const char *c_str() const {
3276  buffer_[BUFFER_SIZE - 1] = '\0';
3277  return str_;
3278  }
3279 
3285  std::string str() const { return std::string(str_, size()); }
3286 };
3287 
3288 // Formats a decimal integer value writing into buffer and returns
3289 // a pointer to the end of the formatted string. This function doesn't
3290 // write a terminating null character.
3291 template <typename T>
3292 inline void format_decimal(char *&buffer, T value) {
3293  typedef typename internal::IntTraits<T>::MainType MainType;
3294  MainType abs_value = static_cast<MainType>(value);
3295  if (internal::is_negative(value)) {
3296  *buffer++ = '-';
3297  abs_value = 0 - abs_value;
3298  }
3299  if (abs_value < 100) {
3300  if (abs_value < 10) {
3301  *buffer++ = static_cast<char>('0' + abs_value);
3302  return;
3303  }
3304  unsigned index = static_cast<unsigned>(abs_value * 2);
3305  *buffer++ = internal::Data::DIGITS[index];
3306  *buffer++ = internal::Data::DIGITS[index + 1];
3307  return;
3308  }
3309  unsigned num_digits = internal::count_digits(abs_value);
3310  internal::format_decimal(buffer, abs_value, num_digits);
3311  buffer += num_digits;
3312 }
3313 
3324 template <typename T>
3325 inline internal::NamedArg<char> arg(StringRef name, const T &arg) {
3327 }
3328 
3329 template <typename T>
3330 inline internal::NamedArg<wchar_t> arg(WStringRef name, const T &arg) {
3332 }
3333 
3334 // The following two functions are deleted intentionally to disable
3335 // nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``.
3336 template <typename Char>
3337 void arg(StringRef, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
3338 template <typename Char>
3339 void arg(WStringRef, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
3340 }
3341 
3342 #if FMT_GCC_VERSION
3343 // Use the system_header pragma to suppress warnings about variadic macros
3344 // because suppressing -Wvariadic-macros with the diagnostic pragma doesn't
3345 // work. It is used at the end because we want to suppress as little warnings
3346 // as possible.
3347 # pragma GCC system_header
3348 #endif
3349 
3350 // This is used to work around VC++ bugs in handling variadic macros.
3351 #define FMT_EXPAND(args) args
3352 
3353 // Returns the number of arguments.
3354 // Based on https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s.
3355 #define FMT_NARG(...) FMT_NARG_(__VA_ARGS__, FMT_RSEQ_N())
3356 #define FMT_NARG_(...) FMT_EXPAND(FMT_ARG_N(__VA_ARGS__))
3357 #define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
3358 #define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
3359 
3360 #define FMT_FOR_EACH_(N, f, ...) \
3361  FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__))
3362 #define FMT_FOR_EACH(f, ...) \
3363  FMT_EXPAND(FMT_FOR_EACH_(FMT_NARG(__VA_ARGS__), f, __VA_ARGS__))
3364 
3365 #define FMT_ADD_ARG_NAME(type, index) type arg##index
3366 #define FMT_GET_ARG_NAME(type, index) arg##index
3367 
3368 #if FMT_USE_VARIADIC_TEMPLATES
3369 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
3370  template <typename... Args> \
3371  ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
3372  const Args & ... args) { \
3373  typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
3374  typename ArgArray::Type array{ \
3375  ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
3376  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), \
3377  fmt::ArgList(fmt::internal::make_type(args...), array)); \
3378  }
3379 #else
3380 // Defines a wrapper for a function taking __VA_ARGS__ arguments
3381 // and n additional arguments of arbitrary types.
3382 # define FMT_WRAP(Char, ReturnType, func, call, n, ...) \
3383  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
3384  inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
3385  FMT_GEN(n, FMT_MAKE_ARG)) { \
3386  fmt::internal::ArgArray<n>::Type arr; \
3387  FMT_GEN(n, FMT_ASSIGN_##Char); \
3388  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList( \
3389  fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), arr)); \
3390  }
3391 
3392 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
3393  inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) { \
3394  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList()); \
3395  } \
3396  FMT_WRAP(Char, ReturnType, func, call, 1, __VA_ARGS__) \
3397  FMT_WRAP(Char, ReturnType, func, call, 2, __VA_ARGS__) \
3398  FMT_WRAP(Char, ReturnType, func, call, 3, __VA_ARGS__) \
3399  FMT_WRAP(Char, ReturnType, func, call, 4, __VA_ARGS__) \
3400  FMT_WRAP(Char, ReturnType, func, call, 5, __VA_ARGS__) \
3401  FMT_WRAP(Char, ReturnType, func, call, 6, __VA_ARGS__) \
3402  FMT_WRAP(Char, ReturnType, func, call, 7, __VA_ARGS__) \
3403  FMT_WRAP(Char, ReturnType, func, call, 8, __VA_ARGS__) \
3404  FMT_WRAP(Char, ReturnType, func, call, 9, __VA_ARGS__) \
3405  FMT_WRAP(Char, ReturnType, func, call, 10, __VA_ARGS__) \
3406  FMT_WRAP(Char, ReturnType, func, call, 11, __VA_ARGS__) \
3407  FMT_WRAP(Char, ReturnType, func, call, 12, __VA_ARGS__) \
3408  FMT_WRAP(Char, ReturnType, func, call, 13, __VA_ARGS__) \
3409  FMT_WRAP(Char, ReturnType, func, call, 14, __VA_ARGS__) \
3410  FMT_WRAP(Char, ReturnType, func, call, 15, __VA_ARGS__)
3411 #endif // FMT_USE_VARIADIC_TEMPLATES
3412 
3440 #define FMT_VARIADIC(ReturnType, func, ...) \
3441  FMT_VARIADIC_(char, ReturnType, func, return func, __VA_ARGS__)
3442 
3443 #define FMT_VARIADIC_W(ReturnType, func, ...) \
3444  FMT_VARIADIC_(wchar_t, ReturnType, func, return func, __VA_ARGS__)
3445 
3446 #define FMT_CAPTURE_ARG_(id, index) ::fmt::arg(#id, id)
3447 
3448 #define FMT_CAPTURE_ARG_W_(id, index) ::fmt::arg(L###id, id)
3449 
3464 #define FMT_CAPTURE(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_, __VA_ARGS__)
3465 
3466 #define FMT_CAPTURE_W(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_W_, __VA_ARGS__)
3467 
3468 namespace fmt {
3469 FMT_VARIADIC(std::string, format, CStringRef)
3470 FMT_VARIADIC_W(std::wstring, format, WCStringRef)
3471 FMT_VARIADIC(void, print, CStringRef)
3472 FMT_VARIADIC(void, print, std::FILE *, CStringRef)
3473 FMT_VARIADIC(void, print_colored, Color, CStringRef)
3474 
3475 namespace internal {
3476 template <typename Char>
3477 inline bool is_name_start(Char c) {
3478  return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || '_' == c;
3479 }
3480 
3481 // Parses an unsigned integer advancing s to the end of the parsed input.
3482 // This function assumes that the first character of s is a digit.
3483 template <typename Char>
3484 unsigned parse_nonnegative_int(const Char *&s) {
3485  assert('0' <= *s && *s <= '9');
3486  unsigned value = 0;
3487  do {
3488  unsigned new_value = value * 10 + (*s++ - '0');
3489  // Check if value wrapped around.
3490  if (new_value < value) {
3491  value = (std::numeric_limits<unsigned>::max)();
3492  break;
3493  }
3494  value = new_value;
3495  } while ('0' <= *s && *s <= '9');
3496  // Convert to unsigned to prevent a warning.
3497  unsigned max_int = (std::numeric_limits<int>::max)();
3498  if (value > max_int)
3499  FMT_THROW(FormatError("number is too big"));
3500  return value;
3501 }
3502 
3503 inline void require_numeric_argument(const Arg &arg, char spec) {
3504  if (arg.type > Arg::LAST_NUMERIC_TYPE) {
3505  std::string message =
3506  fmt::format("format specifier '{}' requires numeric argument", spec);
3507  FMT_THROW(fmt::FormatError(message));
3508  }
3509 }
3510 
3511 template <typename Char>
3512 void check_sign(const Char *&s, const Arg &arg) {
3513  char sign = static_cast<char>(*s);
3514  require_numeric_argument(arg, sign);
3515  if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) {
3517  "format specifier '{}' requires signed argument", sign)));
3518  }
3519  ++s;
3520 }
3521 } // namespace internal
3522 
3523 template <typename Char, typename AF>
3525  BasicStringRef<Char> arg_name, const char *&error) {
3526  if (check_no_auto_index(error)) {
3527  map_.init(args());
3528  const internal::Arg *arg = map_.find(arg_name);
3529  if (arg)
3530  return *arg;
3531  error = "argument not found";
3532  }
3533  return internal::Arg();
3534 }
3535 
3536 template <typename Char, typename AF>
3538  const char *error = 0;
3540  next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error);
3541  if (error) {
3543  *s != '}' && *s != ':' ? "invalid format string" : error));
3544  }
3545  return arg;
3546 }
3547 
3548 template <typename Char, typename AF>
3550  assert(internal::is_name_start(*s));
3551  const Char *start = s;
3552  Char c;
3553  do {
3554  c = *++s;
3555  } while (internal::is_name_start(c) || ('0' <= c && c <= '9'));
3556  const char *error = 0;
3557  internal::Arg arg = get_arg(BasicStringRef<Char>(start, s - start), error);
3558  if (error)
3559  FMT_THROW(FormatError(error));
3560  return arg;
3561 }
3562 
3563 template <typename Char, typename ArgFormatter>
3565  const Char *&format_str, const internal::Arg &arg) {
3566  using internal::Arg;
3567  const Char *s = format_str;
3568  FormatSpec spec;
3569  if (*s == ':') {
3570  if (arg.type == Arg::CUSTOM) {
3571  arg.custom.format(this, arg.custom.value, &s);
3572  return s;
3573  }
3574  ++s;
3575  // Parse fill and alignment.
3576  if (Char c = *s) {
3577  const Char *p = s + 1;
3578  spec.align_ = ALIGN_DEFAULT;
3579  do {
3580  switch (*p) {
3581  case '<':
3582  spec.align_ = ALIGN_LEFT;
3583  break;
3584  case '>':
3585  spec.align_ = ALIGN_RIGHT;
3586  break;
3587  case '=':
3588  spec.align_ = ALIGN_NUMERIC;
3589  break;
3590  case '^':
3591  spec.align_ = ALIGN_CENTER;
3592  break;
3593  }
3594  if (spec.align_ != ALIGN_DEFAULT) {
3595  if (p != s) {
3596  if (c == '}') break;
3597  if (c == '{')
3598  FMT_THROW(FormatError("invalid fill character '{'"));
3599  s += 2;
3600  spec.fill_ = c;
3601  } else ++s;
3602  if (spec.align_ == ALIGN_NUMERIC)
3603  require_numeric_argument(arg, '=');
3604  break;
3605  }
3606  } while (--p >= s);
3607  }
3608 
3609  // Parse sign.
3610  switch (*s) {
3611  case '+':
3612  check_sign(s, arg);
3613  spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
3614  break;
3615  case '-':
3616  check_sign(s, arg);
3617  spec.flags_ |= MINUS_FLAG;
3618  break;
3619  case ' ':
3620  check_sign(s, arg);
3621  spec.flags_ |= SIGN_FLAG;
3622  break;
3623  }
3624 
3625  if (*s == '#') {
3626  require_numeric_argument(arg, '#');
3627  spec.flags_ |= HASH_FLAG;
3628  ++s;
3629  }
3630 
3631  // Parse zero flag.
3632  if (*s == '0') {
3633  require_numeric_argument(arg, '0');
3634  spec.align_ = ALIGN_NUMERIC;
3635  spec.fill_ = '0';
3636  ++s;
3637  }
3638 
3639  // Parse width.
3640  if ('0' <= *s && *s <= '9') {
3642  } else if (*s == '{') {
3643  ++s;
3644  Arg width_arg = internal::is_name_start(*s) ?
3645  parse_arg_name(s) : parse_arg_index(s);
3646  if (*s++ != '}')
3647  FMT_THROW(FormatError("invalid format string"));
3648  ULongLong value = 0;
3649  switch (width_arg.type) {
3650  case Arg::INT:
3651  if (width_arg.int_value < 0)
3652  FMT_THROW(FormatError("negative width"));
3653  value = width_arg.int_value;
3654  break;
3655  case Arg::UINT:
3656  value = width_arg.uint_value;
3657  break;
3658  case Arg::LONG_LONG:
3659  if (width_arg.long_long_value < 0)
3660  FMT_THROW(FormatError("negative width"));
3661  value = width_arg.long_long_value;
3662  break;
3663  case Arg::ULONG_LONG:
3664  value = width_arg.ulong_long_value;
3665  break;
3666  default:
3667  FMT_THROW(FormatError("width is not integer"));
3668  }
3669  if (value > (std::numeric_limits<int>::max)())
3670  FMT_THROW(FormatError("number is too big"));
3671  spec.width_ = static_cast<int>(value);
3672  }
3673 
3674  // Parse precision.
3675  if (*s == '.') {
3676  ++s;
3677  spec.precision_ = 0;
3678  if ('0' <= *s && *s <= '9') {
3680  } else if (*s == '{') {
3681  ++s;
3682  Arg precision_arg = internal::is_name_start(*s) ?
3683  parse_arg_name(s) : parse_arg_index(s);
3684  if (*s++ != '}')
3685  FMT_THROW(FormatError("invalid format string"));
3686  ULongLong value = 0;
3687  switch (precision_arg.type) {
3688  case Arg::INT:
3689  if (precision_arg.int_value < 0)
3690  FMT_THROW(FormatError("negative precision"));
3691  value = precision_arg.int_value;
3692  break;
3693  case Arg::UINT:
3694  value = precision_arg.uint_value;
3695  break;
3696  case Arg::LONG_LONG:
3697  if (precision_arg.long_long_value < 0)
3698  FMT_THROW(FormatError("negative precision"));
3699  value = precision_arg.long_long_value;
3700  break;
3701  case Arg::ULONG_LONG:
3702  value = precision_arg.ulong_long_value;
3703  break;
3704  default:
3705  FMT_THROW(FormatError("precision is not integer"));
3706  }
3707  if (value > (std::numeric_limits<int>::max)())
3708  FMT_THROW(FormatError("number is too big"));
3709  spec.precision_ = static_cast<int>(value);
3710  } else {
3711  FMT_THROW(FormatError("missing precision specifier"));
3712  }
3713  if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) {
3715  fmt::format("precision not allowed in {} format specifier",
3716  arg.type == Arg::POINTER ? "pointer" : "integer")));
3717  }
3718  }
3719 
3720  // Parse type.
3721  if (*s != '}' && *s)
3722  spec.type_ = static_cast<char>(*s++);
3723  }
3724 
3725  if (*s++ != '}')
3726  FMT_THROW(FormatError("missing '}' in format string"));
3727 
3728  // Format argument.
3729  ArgFormatter(*this, spec, s - 1).visit(arg);
3730  return s;
3731 }
3732 
3733 template <typename Char, typename AF>
3735  const Char *s = format_str.c_str();
3736  const Char *start = s;
3737  while (*s) {
3738  Char c = *s++;
3739  if (c != '{' && c != '}') continue;
3740  if (*s == c) {
3741  write(writer_, start, s);
3742  start = ++s;
3743  continue;
3744  }
3745  if (c == '}')
3746  FMT_THROW(FormatError("unmatched '}' in format string"));
3747  write(writer_, start, s - 1);
3749  parse_arg_name(s) : parse_arg_index(s);
3750  start = s = format(s, arg);
3751  }
3752  write(writer_, start, s);
3753 }
3754 } // namespace fmt
3755 
3756 #if FMT_USE_USER_DEFINED_LITERALS
3757 namespace fmt {
3758 namespace internal {
3759 
3760 template <typename Char>
3761 struct UdlFormat {
3762  const Char *str;
3763 
3764  template <typename... Args>
3765  auto operator()(Args && ... args) const
3766  -> decltype(format(str, std::forward<Args>(args)...)) {
3767  return format(str, std::forward<Args>(args)...);
3768  }
3769 };
3770 
3771 template <typename Char>
3772 struct UdlArg {
3773  const Char *str;
3774 
3775  template <typename T>
3776  NamedArg<Char> operator=(T &&value) const {
3777  return {str, std::forward<T>(value)};
3778  }
3779 };
3780 
3781 } // namespace internal
3782 
3783 inline namespace literals {
3784 
3795 inline internal::UdlFormat<char>
3796 operator"" _format(const char *s, std::size_t) { return {s}; }
3797 inline internal::UdlFormat<wchar_t>
3798 operator"" _format(const wchar_t *s, std::size_t) { return {s}; }
3799 
3810 inline internal::UdlArg<char>
3811 operator"" _a(const char *s, std::size_t) { return {s}; }
3812 inline internal::UdlArg<wchar_t>
3813 operator"" _a(const wchar_t *s, std::size_t) { return {s}; }
3814 
3815 } // inline namespace literals
3816 } // namespace fmt
3817 #endif // FMT_USE_USER_DEFINED_LITERALS
3818 
3819 // Restore warnings.
3820 #if FMT_GCC_VERSION >= 406
3821 # pragma GCC diagnostic pop
3822 #endif
3823 
3824 #if defined(__clang__) && !defined(FMT_ICC_VERSION)
3825 # pragma clang diagnostic pop
3826 #endif
3827 
3828 #ifdef FMT_HEADER_ONLY
3829 # define FMT_FUNC inline
3830 # include "format.cc"
3831 #else
3832 # define FMT_FUNC
3833 #endif
3834 
3835 #endif // FMT_FORMAT_H_
GLsizei GLenum GLuint GLuint GLsizei GLchar * message
Definition: glew.h:2584
unsigned flags_
Definition: format.h:1648
void format_decimal(Char *buffer, UInt value, unsigned num_digits, ThousandsSep thousands_sep)
Definition: format.h:919
void set_string(WStringRef str)
Definition: format.h:1205
std::vector< std::pair< fmt::BasicStringRef< Char >, internal::Arg > > MapType
Definition: format.h:1815
std::size_t size() const
Definition: format.h:3261
BasicWriter< Char > & writer()
Definition: format.h:2069
BasicFormatter< Char, Impl > & formatter_
Definition: format.h:1999
Result visit_custom(Arg::CustomValue)
Definition: format.h:1539
static Value make(const T &value)
Definition: format.h:2113
BasicArgFormatter(BasicFormatter< Char, Impl > &formatter, FormatSpec &spec, const Char *fmt)
Definition: format.h:2011
std::size_t size() const
Definition: format.h:455
#define FMT_GCC_EXTENSION
Definition: format.h:97
#define FMT_MAKE_VALUE_(Type, field, TYPE, rhs)
Definition: format.h:1222
T * ptr_
Definition: format.h:596
CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec)
Definition: format.h:2617
StringRef thousands_sep(LConv *lc, LConvCheck< char *LConv::*,&LConv::thousands_sep >=0)
Definition: format.h:1135
friend bool operator==(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:466
FMT_FUNC void print_colored(Color c, CStringRef format, ArgList args)
Definition: format.cc:496
void push_back(const T &value)
Definition: format.h:641
const internal::Arg * args_
Definition: format.h:1376
#define FMT_NOEXCEPT
Definition: format.h:190
FormatInt(unsigned long value)
Definition: format.h:3257
const void * pointer
Definition: format.h:1018
bool is_negative(T value)
Definition: format.h:811
void format_arg(Formatter &, const Char *, const T &)
Definition: format.h:1164
#define FMT_ASSERT(condition, message)
Definition: format.h:229
BasicCStringRef(const Char *s)
Definition: format.h:521
static const uint32_t POWERS_OF_10_32[]
Definition: format.h:836
BasicStringRef< Char > name
Definition: format.h:1346
Result visit_string(Arg::StringValue< char >)
Definition: format.h:1524
int64_t intmax_t
Definition: stdint.h:169
ThousandsSep(fmt::StringRef sep)
Definition: format.h:903
#define FMT_VARIADIC(ReturnType, func,...)
Definition: format.h:3440
void visit_char(int value)
Definition: format.h:1879
Result visit_pointer(const void *)
Definition: format.h:1534
FormatterBase(const ArgList &args)
Definition: format.h:1943
char Yes[1]
Definition: format.h:1062
IntFormatSpec< int, AlignTypeSpec< TYPE_CODE >, Char > pad(int value, unsigned width, Char fill= ' ')
IntFormatSpec< int, TypeSpec<'x'> > hex(int value)
static uint64_t type(long)
Definition: format.h:1243
Buffer< Char > & buffer_
Definition: format.h:2345
bool check_no_auto_index(const char *&error)
Definition: format.h:1962
void operator()(Char *&buffer)
Definition: format.h:906
BasicWriter & operator<<(long double value)
Definition: format.h:2559
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1256
#define FMT_MAKE_WSTR_VALUE(Type, TYPE)
Definition: format.h:1288
char type() const
Definition: format.h:1658
Result visit_uint(unsigned value)
Definition: format.h:1477
#define FMT_MAKE_VALUE(Type, field, TYPE)
Definition: format.h:1226
uint64_t make_type()
Definition: format.h:2098
IntFormatSpec< int, TypeSpec<'o'> > oct(int value)
BasicCStringRef(const std::basic_string< Char > &s)
Definition: format.h:528
T * make_ptr(T *ptr, std::size_t)
Definition: format.h:581
FMT_API void report_unknown_type(char code, const char *type)
Definition: format.cc:296
const GLfloat * c
Definition: glew.h:16629
Result visit_bool(bool value)
Definition: format.h:1487
CharPtr grow_buffer(std::size_t n)
Definition: format.h:2365
FMT_FUNC void write(std::ostream &os, Writer &w)
Definition: ostream.cc:15
MakeValue(const NamedArg< Char_ > &value)
Definition: format.h:1324
long double long_double_value
Definition: format.h:1017
int precision() const
Definition: format.h:1657
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3175
const Char * data() const FMT_NOEXCEPT
Definition: format.h:2468
Result visit_ulong_long(ULongLong value)
Definition: format.h:1482
#define FMT_DISPATCH(call)
Definition: format.h:1427
friend bool operator<(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:472
StringValue< char > string
Definition: format.h:1019
void append_float_length(Char *&, T)
Definition: format.h:2437
T const_check(T value)
Definition: format.h:312
GLuint const GLfloat * val
Definition: glew.h:2797
Result visit_any_int(T)
Definition: format.h:1498
SystemError(int error_code, CStringRef message)
Definition: format.h:2294
const Char * c_str() const
Definition: format.h:2474
Result visit_long_long(LongLong value)
Definition: format.h:1472
FMT_GCC_EXTENSION typedef unsigned long long ULongLong
Definition: format.h:369
FormatError(CStringRef message)
Definition: format.h:540
static Char cast(int value)
Definition: format.h:763
int compare(BasicStringRef other) const
Definition: format.h:458
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
Definition: glew.h:12898
GLenum GLint GLuint mask
Definition: glew.h:1848
Definition: format.h:316
void format(BasicCStringRef< Char > format_str)
Definition: format.h:3734
GLsizei const GLfloat * value
Definition: glew.h:1852
void write_decimal(Int value)
Definition: format.h:2382
DummyInt isinf(...)
Definition: format.h:304
Result visit_any_double(T)
Definition: format.h:1514
bool flag(unsigned f) const
Definition: format.h:1656
#define FMT_STATIC_ASSERT(cond, message)
Definition: format.h:1159
Alignment align() const
Definition: format.h:1604
internal::Arg get_arg(BasicStringRef< Char > arg_name, const char *&error)
Definition: format.h:3524
const T & operator[](std::size_t index) const
Definition: format.h:652
friend bool operator>(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:478
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1382
std::size_t size_
Definition: format.h:597
typedef void(GLAPIENTRY *PFNGLCOPYTEXSUBIMAGE3DPROC)(GLenum target
FMT_FUNC void format_system_error(Writer &out, int error_code, StringRef message) FMT_NOEXCEPT
Definition: format.cc:219
void append_float_length(Char *&format_ptr, long double)
Definition: format.h:2432
Alignment align_
Definition: format.h:1627
GLdouble GLdouble t
Definition: glew.h:1401
FormatInt(int value)
Definition: format.h:3253
unsigned parse_nonnegative_int(const Char *&s)
Definition: format.h:3484
BasicWriter & operator<<(long value)
Definition: format.h:2527
ArgList(ULongLong types, const internal::Arg *args)
Definition: format.h:1397
const char * data() const
Definition: format.h:3269
BasicMemoryWriter(const Allocator &alloc=Allocator())
Definition: format.h:3024
wchar_t fill_
Definition: format.h:1617
bool flag(unsigned) const
Definition: format.h:1607
internal::Arg::Type type(unsigned index) const
Definition: format.h:1379
GLint GLsizei const GLuint64 * values
Definition: glew.h:3624
GLuint GLsizei GLsizei * length
Definition: glew.h:1828
#define FMT_THROW(x)
Definition: format.h:172
const Char * c_str() const
Definition: format.h:531
internal::Arg operator[](unsigned index) const
Definition: format.h:1401
Yes & convert(fmt::ULongLong)
FMT_FUNC void report_system_error(int error_code, fmt::StringRef message) FMT_NOEXCEPT
Definition: format.cc:472
GLdouble GLdouble GLdouble b
Definition: glew.h:9162
BasicWriter & operator<<(unsigned long value)
Definition: format.h:2531
GLintptr offset
Definition: glew.h:1685
internal::ArgMap< Char > map_
Definition: format.h:2041
Buffer(T *ptr=0, std::size_t capacity=0)
Definition: format.h:600
MakeValue(typename WCharHelper< wchar_t, Char >::Supported value)
Definition: format.h:1268
void visit_any_double(T value)
Definition: format.h:1871
StringValue< signed char > sstring
Definition: format.h:1020
GLenum array
Definition: glew.h:9106
const Char * data() const
Definition: format.h:452
Arg get_arg(unsigned arg_index, const char *&error)
Definition: format.h:1958
typedef BOOL(WINAPI *PFNWGLSETSTEREOEMITTERSTATE3DLPROC)(HDC hDC
const internal::Arg * find(const fmt::BasicStringRef< Char > &name) const
Definition: format.h:1823
BasicStringRef(const Char *s)
Definition: format.h:431
#define FMT_API
Definition: format.h:75
typedef INT(WINAPI *PFNWGLGETGPUINFOAMDPROC)(UINT id
#define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type)
Definition: format.h:2225
const Char * str_
Definition: format.h:1678
GLuint GLuint end
Definition: glew.h:1256
internal::MemoryBuffer< Char, internal::INLINE_BUFFER_SIZE, Allocator > buffer_
Definition: format.h:3021
GLuint64EXT * result
Definition: glew.h:14309
BasicStringRef< wchar_t > WStringRef
Definition: format.h:487
static uint64_t type(const NamedArg< Char_ > &)
Definition: format.h:1327
const Char * format_
Definition: format.h:2000
static uint64_t type(unsigned long)
Definition: format.h:1253
static uint64_t type(const T &)
Definition: format.h:1317
void visit_bool(bool value)
Definition: format.h:1873
static uint64_t type(wchar_t)
Definition: format.h:1271
void grow(std::size_t size)
Definition: format.h:726
AlignTypeSpec(unsigned width, wchar_t fill)
Definition: format.h:1640
ArgFormatterBase(BasicWriter< Char > &w, FormatSpec &s)
Definition: format.h:1864
CharPtr prepare_int_buffer(unsigned num_digits, const EmptySpec &, const char *prefix, unsigned prefix_size)
Definition: format.h:2394
BasicStringRef< char > StringRef
Definition: format.h:486
WidthSpec(unsigned width, wchar_t fill)
Definition: format.h:1619
#define FMT_MAKE_STR_VALUE(Type, TYPE)
Definition: format.h:1274
BasicStringRef(const std::basic_string< Char > &s)
Definition: format.h:439
fmt::BufferedFile & move(fmt::BufferedFile &f)
Definition: posix.h:361
BasicStringRef(const Char *s, std::size_t size)
Definition: format.h:423
FMT_DISABLE_CONVERSION_TO_INT(float)
Result visit_wstring(Arg::StringValue< wchar_t >)
Definition: format.h:1529
Alignment
Definition: format.h:1588
Result visit_double(double value)
Definition: format.h:1503
BasicMemoryWriter< char > MemoryWriter
Definition: format.h:3050
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1893
NamedArg(BasicStringRef< Char > argname, const T &value)
Definition: format.h:1349
Result visit_long_double(long double value)
Definition: format.h:1508
void write(BasicCStringRef< Char > format, ArgList args)
Definition: format.h:2515
ULongLong ulong_long_value
Definition: format.h:1015
Result visit_char(int value)
Definition: format.h:1492
void write(bool value)
Definition: format.h:1852
bool flag(unsigned) const
Definition: format.h:1642
GLenum GLint GLint * precision
Definition: glew.h:3512
GLuint start
Definition: glew.h:1256
#define FMT_ARG_TYPE_DEFAULT(n)
Definition: format.h:2151
unsigned int uint32_t
Definition: stdint.h:126
BasicArrayWriter(Char(&array)[SIZE])
Definition: format.h:3095
const char * c_str() const
Definition: format.h:3275
DummyInt isnan(...)
Definition: format.h:306
BasicFormatter(const ArgList &args, BasicWriter< Char > &w)
Definition: format.h:2065
void write_double(T value, const FormatSpec &spec)
Definition: format.h:2831
AlignSpec(unsigned width, wchar_t fill, Alignment align=ALIGN_DEFAULT)
Definition: format.h:1629
IntFormatSpec< int, TypeSpec<'X'> > hexu(int value)
ArgList(ULongLong types, const internal::Value *values)
Definition: format.h:1395
BasicWriter< Char > & writer_
Definition: format.h:1837
BasicWriter & operator<<(double value)
Definition: format.h:2548
static wchar_t convert(char value)
Definition: format.h:787
BasicCStringRef< char > CStringRef
Definition: format.h:534
StringValue< unsigned char > ustring
Definition: format.h:1021
MapType::value_type Pair
Definition: format.h:1816
uint64_t types_
Definition: format.h:1368
void reserve(std::size_t capacity)
Definition: format.h:634
FormatSpec(unsigned width=0, char type=0, wchar_t fill= ' ')
Definition: format.h:1652
GLuint GLuint GLsizei count
Definition: glew.h:1256
BasicWriter< char > Writer
Definition: format.h:376
internal::Arg parse_arg_index(const Char *&s)
Definition: format.h:3537
static char convert(char value)
Definition: format.h:776
static Arg make(const T &value)
Definition: format.h:2131
unsigned __int64 uint64_t
Definition: stdint.h:136
GLfloat GLfloat p
Definition: glew.h:16654
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
void write_pointer(const void *p)
Definition: format.h:1842
std::size_t size_
Definition: format.h:419
void report_unhandled_arg()
Definition: format.h:1459
MakeUnsigned< Int >::Type to_unsigned(Int value)
Definition: format.h:564
MakeArg(const T &value)
Definition: format.h:1338
DummyInt signbit(...)
Definition: format.h:302
void check_sign(const Char *&s, const Arg &arg)
Definition: format.h:3512
char type() const
Definition: format.h:1608
BasicCStringRef< wchar_t > WCStringRef
Definition: format.h:535
FormatInt(LongLong value)
Definition: format.h:3255
CustomValue custom
Definition: format.h:1023
GLuint buffer
Definition: glew.h:1683
BasicWriter< Char > & writer()
Definition: format.h:1849
Alignment align() const
Definition: format.h:1632
No & operator<<(std::ostream &, int)
Buffer< Char > & buffer() FMT_NOEXCEPT
Definition: format.h:2612
#define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: format.h:211
void visit_wstring(Arg::StringValue< Char > value)
Definition: format.h:1921
BasicWriter & operator<<(typename internal::WCharHelper< StringRef, Char >::Supported value)
Definition: format.h:2589
Char * write_unsigned_decimal(UInt value, unsigned prefix_size=0)
Definition: format.h:2373
DummyInt _isnan(...)
Definition: format.h:307
BasicWriter & operator<<(unsigned value)
Definition: format.h:2524
int error_code() const
Definition: format.h:2301
char fill() const
Definition: format.h:1609
Arg next_arg(const char *&error)
Definition: format.h:1949
MemoryBuffer(const Allocator &alloc=Allocator())
Definition: format.h:684
BasicWriter< wchar_t > WWriter
Definition: format.h:379
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1257
internal::CharTraits< Char >::CharPtr CharPtr
Definition: format.h:2349
void clear() FMT_NOEXCEPT
Definition: format.h:2610
GLsizei GLenum GLenum * types
Definition: glew.h:3966
char No[2]
Definition: format.h:1063
void write(const char *value)
Definition: format.h:1858
int precision() const
Definition: format.h:1606
BasicArrayWriter< char > ArrayWriter
Definition: format.h:3099
BasicWriter & operator<<(char value)
Definition: format.h:2567
static CharPtr fill_padding(CharPtr buffer, unsigned total_size, std::size_t content_size, wchar_t fill)
Definition: format.h:2661
GLuint index
Definition: glew.h:1817
ArgFormatter(BasicFormatter< Char > &formatter, FormatSpec &spec, const Char *fmt)
Definition: format.h:2027
const internal::Value * values_
Definition: format.h:1375
char type() const
Definition: format.h:1643
Result visit_int(int value)
Definition: format.h:1467
T value() const
Definition: format.h:1671
Color
Definition: format.h:3156
FMT_SPECIALIZE_MAKE_UNSIGNED(char, unsigned char)
GLint GLint GLint GLint GLint x
Definition: glew.h:1255
static const uint64_t POWERS_OF_10_64[]
Definition: format.h:837
internal::NamedArg< char > arg(StringRef name, const T &arg)
Definition: format.h:3325
internal::FixedBuffer< Char > buffer_
Definition: format.h:3076
fmt::StringRef sep_
Definition: format.h:897
GLdouble GLdouble GLdouble r
Definition: glew.h:1409
BasicWriter< Char > & writer_
Definition: format.h:2040
void require_numeric_argument(const Arg &arg, char spec)
Definition: format.h:3503
FMT_FUNC void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:486
Result visit_cstring(const char *)
Definition: format.h:1519
ArgType(const T &arg)
Definition: format.h:2148
FormatInt(ULongLong value)
Definition: format.h:3258
unsigned count_digits(uint64_t n)
Definition: format.h:864
BasicWriter & operator<<(LongLong value)
Definition: format.h:2534
friend bool operator>=(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:481
std::size_t capacity() const
Definition: format.h:618
unsigned width_
Definition: format.h:1614
LongLong long_long_value
Definition: format.h:1014
void resize(std::size_t new_size)
Definition: format.h:623
GLuint const GLchar * name
Definition: glew.h:1817
void set_string(StringRef str)
Definition: format.h:1200
FixedBuffer(Char *array, std::size_t size)
Definition: format.h:749
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t0
Definition: glew.h:12898
StrFormatSpec(const Char *str, unsigned width, FillChar fill)
Definition: format.h:1682
void clear() FMT_NOEXCEPT
Definition: format.h:639
BasicWriter & operator<<(ULongLong value)
Definition: format.h:2544
GLsizeiptr size
Definition: glew.h:1684
T & operator[](std::size_t index)
Definition: format.h:651
IntFormatSpec(T val, const SpecT &spec=SpecT())
Definition: format.h:1668
void visit_custom(internal::Arg::CustomValue c)
Definition: format.h:2017
typedef UINT(WINAPI *PFNWGLGETCONTEXTGPUIDAMDPROC)(HGLRC hglrc)
const Char * data_
Definition: format.h:517
unsigned width() const
Definition: format.h:1621
GLsizei n
Definition: glew.h:4052
void append(const U *begin, const U *end)
Definition: format.h:657
char * str_
Definition: format.h:3218
IntFormatSpec< int, TypeSpec<'b'> > bin(int value)
Result visit(const Arg &arg)
Definition: format.h:1551
std::size_t capacity_
Definition: format.h:598
#define FMT_DEFINE_INT_FORMATTERS(TYPE)
Definition: format.h:1730
BasicData Data
Definition: format.h:851
const Char * data_
Definition: format.h:418
BasicWriter(Buffer< Char > &b)
Definition: format.h:2449
friend bool operator<=(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:475
No & convert(...)
char * format_decimal(ULongLong value)
Definition: format.h:3221
Definition: format.cc:82
internal::Arg parse_arg_name(const Char *&s)
Definition: format.h:3549
static void format_custom_arg(void *formatter, const void *arg, void *format_str_ptr)
Definition: format.h:1212
BasicMemoryWriter< wchar_t > WMemoryWriter
Definition: format.h:3051
MakeValue(unsigned long value)
Definition: format.h:1247
Allocator get_allocator() const
Definition: format.h:722
std::size_t size() const
Definition: format.h:615
GLsizei const void * pointer
Definition: glew.h:1526
const GLint * first
Definition: glew.h:1531
BasicArrayWriter(Char *array, std::size_t size)
Definition: format.h:3085
#define FMT_GEN15(f)
Definition: format.h:2095
void visit_cstring(const char *value)
Definition: format.h:1909
FormatInt(unsigned value)
Definition: format.h:3256
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1255
FormatInt(long value)
Definition: format.h:3254
int precision() const
Definition: format.h:1634
DummyInt _finite(...)
Definition: format.h:305
void write(BasicWriter< Char > &w, const Char *start, const Char *end)
Definition: format.h:1972
void visit_pointer(const void *value)
Definition: format.h:1925
StringValue< wchar_t > wstring
Definition: format.h:1022
Result visit_unhandled_arg()
Definition: format.h:1461
static bool is_negative(T value)
Definition: format.h:799
unsigned width() const
Definition: format.h:1605
BasicWriter & operator<<(typename internal::WCharHelper< wchar_t, Char >::Supported value)
Definition: format.h:2572
friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:469
#define FMT_VARIADIC_VOID(func, arg_type)
Definition: format.h:2208
Type
Type of JSON value.
Definition: rapidjson.h:642
wchar_t fill() const
Definition: format.h:1622
virtual ~BasicWriter()
Definition: format.h:2457
GLdouble s
Definition: glew.h:1393
std::basic_string< Char > str() const
Definition: format.h:2486
internal::Arg Arg
Definition: format.h:1456
std::numeric_limits< fmt::internal::DummyInt > FPUtil
Definition: format.h:298
void visit_string(Arg::StringValue< char > value)
Definition: format.h:1915
static wchar_t convert(wchar_t value)
Definition: format.h:788
unsigned uint_value
Definition: format.h:1013
#define FMT_VARIADIC_W(ReturnType, func,...)
Definition: format.h:3443
std::basic_string< Char > to_string() const
Definition: format.h:447
GLsizei const GLchar *const * string
Definition: glew.h:1847
MakeValue(const T &value, typename EnableIf< ConvertToInt< T >::value, int >::type=0)
Definition: format.h:1311
void write_int(T value, Spec spec)
Definition: format.h:2741
virtual ~Buffer()
Definition: format.h:612
const Char * str() const
Definition: format.h:1687
static const char DIGITS[]
Definition: format.h:838
GenericMemoryBuffer MemoryBuffer
Definition: memorybuffer.h:60
BasicArrayWriter< wchar_t > WArrayWriter
Definition: format.h:3100
FMT_GCC_EXTENSION typedef long long LongLong
Definition: format.h:368
const ArgList & args() const
Definition: format.h:1941
DummyInt _ecvt_s(...)
Definition: format.h:303
#define FMT_DELETED_OR_UNDEFINED
Definition: format.h:210
std::string str() const
Definition: format.h:3285
Formatter::Char Char
Definition: format.h:1175
bool is_name_start(Char c)
Definition: format.h:3477
void FormatSigned(LongLong value)
Definition: format.h:3242
std::size_t size() const
Definition: format.h:2462
GLclampf f
Definition: glew.h:3511