Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
format.h
1 /*
2 Formatting library for C++
3 Copyright (c) 2012 - 2015, Victor Zverovich
4 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 1. Redistributions of source code must retain the above copyright notice, this
8 list of conditions and the following disclaimer.
9 2. Redistributions in binary form must reproduce the above copyright notice,
10 this list of conditions and the following disclaimer in the documentation
11 and/or other materials provided with the distribution.
12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
13 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
16 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 #ifndef FMT_FORMAT_H_
25 #define FMT_FORMAT_H_
26 
27 #include <stdint.h>
28 
29 #include <cassert>
30 #include <cmath>
31 #include <cstddef> // for std::ptrdiff_t
32 #include <cstdio>
33 #include <algorithm>
34 #include <limits>
35 #include <stdexcept>
36 #include <string>
37 #include <sstream>
38 #include <map>
39 
40 #if _SECURE_SCL
41 # include <iterator>
42 #endif
43 
44 #ifdef _MSC_VER
45 # include <intrin.h> // _BitScanReverse, _BitScanReverse64
46 
47 namespace fmt {
48  namespace internal {
49 # pragma intrinsic(_BitScanReverse)
50  inline uint32_t clz(uint32_t x) {
51  unsigned long r = 0;
52  _BitScanReverse(&r, x);
53  return 31 - r;
54  }
55 # define FMT_BUILTIN_CLZ(n) fmt::internal::clz(n)
56 
57 # ifdef _WIN64
58 # pragma intrinsic(_BitScanReverse64)
59 # endif
60 
61  inline uint32_t clzll(uint64_t x) {
62  unsigned long r = 0;
63 # ifdef _WIN64
64  _BitScanReverse64(&r, x);
65 # else
66  // Scan the high 32 bits.
67  if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32)))
68  return 63 - (r + 32);
69 
70  // Scan the low 32 bits.
71  _BitScanReverse(&r, static_cast<uint32_t>(x));
72 # endif
73  return 63 - r;
74  }
75 # define FMT_BUILTIN_CLZLL(n) fmt::internal::clzll(n)
76  }
77 }
78 #endif
79 
80 #ifdef __GNUC__
81 # define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
82 # define FMT_GCC_EXTENSION __extension__
83 # if FMT_GCC_VERSION >= 406
84 # pragma GCC diagnostic push
85 // Disable the warning about "long long" which is sometimes reported even
86 // when using __extension__.
87 # pragma GCC diagnostic ignored "-Wlong-long"
88 // Disable the warning about declaration shadowing because it affects too
89 // many valid cases.
90 # pragma GCC diagnostic ignored "-Wshadow"
91 # endif
92 # if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__
93 # define FMT_HAS_GXX_CXX11 1
94 # endif
95 #else
96 # define FMT_GCC_EXTENSION
97 #endif
98 
99 #ifdef __clang__
100 # pragma clang diagnostic push
101 # pragma clang diagnostic ignored "-Wdocumentation"
102 #endif
103 
104 #ifdef __GNUC_LIBSTD__
105 # define FMT_GNUC_LIBSTD_VERSION (__GNUC_LIBSTD__ * 100 + __GNUC_LIBSTD_MINOR__)
106 #endif
107 
108 #ifdef __has_feature
109 # define FMT_HAS_FEATURE(x) __has_feature(x)
110 #else
111 # define FMT_HAS_FEATURE(x) 0
112 #endif
113 
114 #ifdef __has_builtin
115 # define FMT_HAS_BUILTIN(x) __has_builtin(x)
116 #else
117 # define FMT_HAS_BUILTIN(x) 0
118 #endif
119 
120 #ifdef __has_cpp_attribute
121 # define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
122 #else
123 # define FMT_HAS_CPP_ATTRIBUTE(x) 0
124 #endif
125 
126 #ifndef FMT_USE_VARIADIC_TEMPLATES
127 // Variadic templates are available in GCC since version 4.4
128 // (http://gcc.gnu.org/projects/cxx0x.html) and in Visual C++
129 // since version 2013.
130 # define FMT_USE_VARIADIC_TEMPLATES \
131  (FMT_HAS_FEATURE(cxx_variadic_templates) || \
132  (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1800)
133 #endif
134 
135 #ifndef FMT_USE_RVALUE_REFERENCES
136 // Don't use rvalue references when compiling with clang and an old libstdc++
137 // as the latter doesn't provide std::move.
138 # if defined(FMT_GNUC_LIBSTD_VERSION) && FMT_GNUC_LIBSTD_VERSION <= 402
139 # define FMT_USE_RVALUE_REFERENCES 0
140 # else
141 # define FMT_USE_RVALUE_REFERENCES \
142  (FMT_HAS_FEATURE(cxx_rvalue_references) || \
143  (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600)
144 # endif
145 #endif
146 
147 #if FMT_USE_RVALUE_REFERENCES
148 # include <utility> // for std::move
149 #endif
150 
151 // Define FMT_USE_NOEXCEPT to make C++ Format use noexcept (C++11 feature).
152 #ifndef FMT_NOEXCEPT
153 # if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \
154  (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11)
155 # define FMT_NOEXCEPT noexcept
156 # else
157 # define FMT_NOEXCEPT throw()
158 # endif
159 #endif
160 
161 // A macro to disallow the copy constructor and operator= functions
162 // This should be used in the private: declarations for a class
163 #if FMT_USE_DELETED_FUNCTIONS || FMT_HAS_FEATURE(cxx_deleted_functions) || \
164  (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1800
165 # define FMT_DELETED_OR_UNDEFINED = delete
166 # define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
167  TypeName(const TypeName&) = delete; \
168  TypeName& operator=(const TypeName&) = delete
169 #else
170 # define FMT_DELETED_OR_UNDEFINED
171 # define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
172  TypeName(const TypeName&); \
173  TypeName& operator=(const TypeName&)
174 #endif
175 
176 #ifndef FMT_ASSERT
177 # define FMT_ASSERT(condition, message) assert((condition) && message)
178 #endif
179 
180 namespace fmt {
181 
182  // Fix the warning about long long on older versions of GCC
183  // that don't support the diagnostic pragma.
184  FMT_GCC_EXTENSION typedef long long LongLong;
185  FMT_GCC_EXTENSION typedef unsigned long long ULongLong;
186 
187 #if FMT_USE_RVALUE_REFERENCES
188  using std::move;
189 #endif
190 
191  template <typename Char>
192  class BasicWriter;
193 
194  typedef BasicWriter<char> Writer;
196 
197  template <typename Char>
198  class BasicFormatter;
199 
200  template <typename Char, typename T>
201  void format(BasicFormatter<Char> &f, const Char *&format_str, const T &value);
202 
223  template <typename Char>
225  private:
226  const Char *data_;
227  std::size_t size_;
228 
229  public:
231  BasicStringRef(const Char *s, std::size_t size) : data_(s), size_(size) {}
232 
239  BasicStringRef(const Char *s)
240  : data_(s), size_(std::char_traits<Char>::length(s)) {}
241 
247  BasicStringRef(const std::basic_string<Char> &s)
248  : data_(s.c_str()), size_(s.size()) {}
249 
255  std::basic_string<Char> to_string() const {
256  return std::basic_string<Char>(data_, size_);
257  }
258 
260  const Char *data() const { return data_; }
261 
263  std::size_t size() const { return size_; }
264 
265  friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) {
266  return lhs.data_ == rhs.data_;
267  }
268  friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs) {
269  return lhs.data_ != rhs.data_;
270  }
271  friend bool operator<(BasicStringRef lhs, BasicStringRef rhs) {
272  return std::lexicographical_compare(
273  lhs.data_, lhs.data_ + lhs.size_, rhs.data_, rhs.data_ + rhs.size_);
274  }
275  };
276 
279 
300  template <typename Char>
302  private:
303  const Char *data_;
304 
305  public:
307  BasicCStringRef(const Char *s) : data_(s) {}
308 
314  BasicCStringRef(const std::basic_string<Char> &s) : data_(s.c_str()) {}
315 
317  const Char *c_str() const { return data_; }
318  };
319 
322 
326  class FormatError : public std::runtime_error {
327  public:
328  explicit FormatError(CStringRef message)
329  : std::runtime_error(message.c_str()) {}
330  };
331 
332  namespace internal {
333  // The number of characters to store in the MemoryBuffer object itself
334  // to avoid dynamic memory allocation.
335  enum { INLINE_BUFFER_SIZE = 500 };
336 
337 #if _SECURE_SCL
338  // Use checked iterator to avoid warnings on MSVC.
339  template <typename T>
340  inline stdext::checked_array_iterator<T*> make_ptr(T *ptr, std::size_t size) {
341  return stdext::checked_array_iterator<T*>(ptr, size);
342  }
343 #else
344  template <typename T>
345  inline T *make_ptr(T *ptr, std::size_t) { return ptr; }
346 #endif
347  } // namespace internal
348 
354  template <typename T>
355  class Buffer {
356  private:
357  FMT_DISALLOW_COPY_AND_ASSIGN(Buffer);
358 
359  protected:
360  T *ptr_;
361  std::size_t size_;
362  std::size_t capacity_;
363 
364  Buffer(T *ptr = 0, std::size_t capacity = 0)
365  : ptr_(ptr), size_(0), capacity_(capacity) {}
366 
373  virtual void grow(std::size_t size) = 0;
374 
375  public:
376  virtual ~Buffer() {}
377 
379  std::size_t size() const { return size_; }
380 
382  std::size_t capacity() const { return capacity_; }
383 
387  void resize(std::size_t new_size) {
388  if (new_size > capacity_)
389  grow(new_size);
390  size_ = new_size;
391  }
392 
398  void reserve(std::size_t capacity) {
399  if (capacity > capacity_)
400  grow(capacity);
401  }
402 
403  void clear() FMT_NOEXCEPT{ size_ = 0; }
404 
405  void push_back(const T &value) {
406  if (size_ == capacity_)
407  grow(size_ + 1);
408  ptr_[size_++] = value;
409  }
410 
412  template <typename U>
413  void append(const U *begin, const U *end);
414 
415  T &operator[](std::size_t index) { return ptr_[index]; }
416  const T &operator[](std::size_t index) const { return ptr_[index]; }
417  };
418 
419  template <typename T>
420  template <typename U>
421  void Buffer<T>::append(const U *begin, const U *end) {
422  std::ptrdiff_t num_elements = end - begin;
423  if (size_ + num_elements > capacity_)
424  grow(size_ + num_elements);
425  std::copy(begin, end, internal::make_ptr(ptr_, capacity_) + size_);
426  size_ += num_elements;
427  }
428 
429  namespace internal {
430 
431  // A memory buffer for POD types with the first SIZE elements stored in
432  // the object itself.
433  template <typename T, std::size_t SIZE, typename Allocator = std::allocator<T> >
434  class MemoryBuffer : private Allocator, public Buffer<T> {
435  private:
436  T data_[SIZE];
437 
438  // Free memory allocated by the buffer.
439  void free() {
440  if (this->ptr_ != data_) this->deallocate(this->ptr_, this->capacity_);
441  }
442 
443  protected:
444  void grow(std::size_t size);
445 
446  public:
447  explicit MemoryBuffer(const Allocator &alloc = Allocator())
448  : Allocator(alloc), Buffer<T>(data_, SIZE) {}
449  ~MemoryBuffer() { free(); }
450 
451 #if FMT_USE_RVALUE_REFERENCES
452  private:
453  // Move data from other to this buffer.
454  void move(MemoryBuffer &other) {
455  Allocator &this_alloc = *this, &other_alloc = other;
456  this_alloc = std::move(other_alloc);
457  this->size_ = other.size_;
458  this->capacity_ = other.capacity_;
459  if (other.ptr_ == other.data_) {
460  this->ptr_ = data_;
461  std::copy(other.data_,
462  other.data_ + this->size_, make_ptr(data_, this->capacity_));
463  }
464  else {
465  this->ptr_ = other.ptr_;
466  // Set pointer to the inline array so that delete is not called
467  // when freeing.
468  other.ptr_ = other.data_;
469  }
470  }
471 
472  public:
473  MemoryBuffer(MemoryBuffer &&other) {
474  move(other);
475  }
476 
477  MemoryBuffer &operator=(MemoryBuffer &&other) {
478  assert(this != &other);
479  free();
480  move(other);
481  return *this;
482  }
483 #endif
484 
485  // Returns a copy of the allocator associated with this buffer.
486  Allocator get_allocator() const { return *this; }
487  };
488 
489  template <typename T, std::size_t SIZE, typename Allocator>
490  void MemoryBuffer<T, SIZE, Allocator>::grow(std::size_t size) {
491  std::size_t new_capacity =
492  (std::max)(size, this->capacity_ + this->capacity_ / 2);
493  T *new_ptr = this->allocate(new_capacity);
494  // The following code doesn't throw, so the raw pointer above doesn't leak.
495  std::copy(this->ptr_,
496  this->ptr_ + this->size_, make_ptr(new_ptr, new_capacity));
497  std::size_t old_capacity = this->capacity_;
498  T *old_ptr = this->ptr_;
499  this->capacity_ = new_capacity;
500  this->ptr_ = new_ptr;
501  // deallocate may throw (at least in principle), but it doesn't matter since
502  // the buffer already uses the new storage and will deallocate it in case
503  // of exception.
504  if (old_ptr != data_)
505  this->deallocate(old_ptr, old_capacity);
506  }
507 
508  // A fixed-size buffer.
509  template <typename Char>
510  class FixedBuffer : public fmt::Buffer<Char> {
511  public:
512  FixedBuffer(Char *array, std::size_t size) : fmt::Buffer<Char>(array, size) {}
513 
514  protected:
515  void grow(std::size_t size);
516  };
517 
518 #ifndef _MSC_VER
519  // Portable version of signbit.
520  inline int getsign(double x) {
521  // When compiled in C++11 mode signbit is no longer a macro but a function
522  // defined in namespace std and the macro is undefined.
523 # ifdef signbit
524  return signbit(x);
525 # else
526  return std::signbit(x);
527 # endif
528  }
529 
530  // Portable version of isinf.
531 # ifdef isinf
532  inline int isinfinity(double x) { return isinf(x); }
533  inline int isinfinity(long double x) { return isinf(x); }
534 # else
535  inline int isinfinity(double x) { return std::isinf(x); }
536  inline int isinfinity(long double x) { return std::isinf(x); }
537 # endif
538 #else
539  inline int getsign(double value) {
540  if (value < 0) return 1;
541  if (value == value) return 0;
542  int dec = 0, sign = 0;
543  char buffer[2]; // The buffer size must be >= 2 or _ecvt_s will fail.
544  _ecvt_s(buffer, sizeof(buffer), value, 0, &dec, &sign);
545  return sign;
546  }
547  inline int isinfinity(double x) { return !_finite(x); }
548  inline int isinfinity(long double x) {
549  return !_finite(static_cast<double>(x));
550  }
551 #endif
552 
553  template <typename Char>
554  class BasicCharTraits {
555  public:
556 #if _SECURE_SCL
557  typedef stdext::checked_array_iterator<Char*> CharPtr;
558 #else
559  typedef Char *CharPtr;
560 #endif
561  static Char cast(wchar_t value) { return static_cast<Char>(value); }
562  };
563 
564  template <typename Char>
565  class CharTraits;
566 
567  template <>
568  class CharTraits<char> : public BasicCharTraits<char>{
569  private:
570  // Conversion from wchar_t to char is not allowed.
571  static char convert(wchar_t);
572 
573  public:
574  static char convert(char value) { return value; }
575 
576  // Formats a floating-point number.
577  template <typename T>
578  static int format_float(char *buffer, std::size_t size,
579  const char *format, unsigned width, int precision, T value);
580  };
581 
582  template <>
583  class CharTraits<wchar_t> : public BasicCharTraits<wchar_t>{
584  public:
585  static wchar_t convert(char value) { return value; }
586  static wchar_t convert(wchar_t value) { return value; }
587 
588  template <typename T>
589  static int format_float(wchar_t *buffer, std::size_t size,
590  const wchar_t *format, unsigned width, int precision, T value);
591  };
592 
593  // Checks if a number is negative - used to avoid warnings.
594  template <bool IsSigned>
595  struct SignChecker {
596  template <typename T>
597  static bool is_negative(T value) { return value < 0; }
598  };
599 
600  template <>
601  struct SignChecker<false> {
602  template <typename T>
603  static bool is_negative(T) { return false; }
604  };
605 
606  // Returns true if value is negative, false otherwise.
607  // Same as (value < 0) but doesn't produce warnings if T is an unsigned type.
608  template <typename T>
609  inline bool is_negative(T value) {
610  return SignChecker<std::numeric_limits<T>::is_signed>::is_negative(value);
611  }
612 
613  // Selects uint32_t if FitsIn32Bits is true, uint64_t otherwise.
614  template <bool FitsIn32Bits>
615  struct TypeSelector { typedef uint32_t Type; };
616 
617  template <>
618  struct TypeSelector<false> { typedef uint64_t Type; };
619 
620  template <typename T>
621  struct IntTraits {
622  // Smallest of uint32_t and uint64_t that is large enough to represent
623  // all values of T.
624  typedef typename
625  TypeSelector<std::numeric_limits<T>::digits <= 32>::Type MainType;
626  };
627 
628  // MakeUnsigned<T>::Type gives an unsigned type corresponding to integer type T.
629  template <typename T>
630  struct MakeUnsigned { typedef T Type; };
631 
632 #define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U) \
633  template <> \
634  struct MakeUnsigned<T> { typedef U Type; }
635 
636  FMT_SPECIALIZE_MAKE_UNSIGNED(char, unsigned char);
637  FMT_SPECIALIZE_MAKE_UNSIGNED(signed char, unsigned char);
638  FMT_SPECIALIZE_MAKE_UNSIGNED(short, unsigned short);
639  FMT_SPECIALIZE_MAKE_UNSIGNED(int, unsigned);
640  FMT_SPECIALIZE_MAKE_UNSIGNED(long, unsigned long);
641  FMT_SPECIALIZE_MAKE_UNSIGNED(LongLong, ULongLong);
642 
643  void report_unknown_type(char code, const char *type);
644 
645  // Static data is placed in this class template to allow header-only
646  // configuration.
647  template <typename T = void>
648  struct BasicData {
649  static const uint32_t POWERS_OF_10_32[];
650  static const uint64_t POWERS_OF_10_64[];
651  static const char DIGITS[];
652  };
653 
654  typedef BasicData<> Data;
655 
656 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz)
657 # define FMT_BUILTIN_CLZ(n) __builtin_clz(n)
658 #endif
659 
660 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clzll)
661 # define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n)
662 #endif
663 
664 #ifdef FMT_BUILTIN_CLZLL
665  // Returns the number of decimal digits in n. Leading zeros are not counted
666  // except for n == 0 in which case count_digits returns 1.
667  inline unsigned count_digits(uint64_t n) {
668  // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
669  // and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits.
670  unsigned t = (64 - FMT_BUILTIN_CLZLL(n | 1)) * 1233 >> 12;
671  return t - (n < Data::POWERS_OF_10_64[t]) + 1;
672  }
673 #else
674  // Fallback version of count_digits used when __builtin_clz is not available.
675  inline unsigned count_digits(uint64_t n) {
676  unsigned count = 1;
677  for (;;) {
678  // Integer division is slow so do it for a group of four digits instead
679  // of for every digit. The idea comes from the talk by Alexandrescu
680  // "Three Optimization Tips for C++". See speed-test for a comparison.
681  if (n < 10) return count;
682  if (n < 100) return count + 1;
683  if (n < 1000) return count + 2;
684  if (n < 10000) return count + 3;
685  n /= 10000u;
686  count += 4;
687  }
688  }
689 #endif
690 
691 #ifdef FMT_BUILTIN_CLZ
692  // Optional version of count_digits for better performance on 32-bit platforms.
693  inline unsigned count_digits(uint32_t n) {
694  uint32_t t = (32 - FMT_BUILTIN_CLZ(n | 1)) * 1233 >> 12;
695  return t - (n < Data::POWERS_OF_10_32[t]) + 1;
696  }
697 #endif
698 
699  // Formats a decimal unsigned integer value writing into buffer.
700  template <typename UInt, typename Char>
701  inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) {
702  buffer += num_digits;
703  while (value >= 100) {
704  // Integer division is slow so do it for a group of two digits instead
705  // of for every digit. The idea comes from the talk by Alexandrescu
706  // "Three Optimization Tips for C++". See speed-test for a comparison.
707  unsigned index = (value % 100) * 2;
708  value /= 100;
709  *--buffer = Data::DIGITS[index + 1];
710  *--buffer = Data::DIGITS[index];
711  }
712  if (value < 10) {
713  *--buffer = static_cast<char>('0' + value);
714  return;
715  }
716  unsigned index = static_cast<unsigned>(value * 2);
717  *--buffer = Data::DIGITS[index + 1];
718  *--buffer = Data::DIGITS[index];
719  }
720 
721 #ifndef _WIN32
722 # define FMT_USE_WINDOWS_H 0
723 #elif !defined(FMT_USE_WINDOWS_H)
724 # define FMT_USE_WINDOWS_H 1
725 #endif
726 
727  // Define FMT_USE_WINDOWS_H to 0 to disable use of windows.h.
728  // All the functionality that relies on it will be disabled too.
729 #if FMT_USE_WINDOWS_H
730  // A converter from UTF-8 to UTF-16.
731  // It is only provided for Windows since other systems support UTF-8 natively.
732  class UTF8ToUTF16 {
733  private:
734  MemoryBuffer<wchar_t, INLINE_BUFFER_SIZE> buffer_;
735 
736  public:
737  explicit UTF8ToUTF16(StringRef s);
738  operator WStringRef() const { return WStringRef(&buffer_[0], size()); }
739  size_t size() const { return buffer_.size() - 1; }
740  const wchar_t *c_str() const { return &buffer_[0]; }
741  std::wstring str() const { return std::wstring(&buffer_[0], size()); }
742  };
743 
744  // A converter from UTF-16 to UTF-8.
745  // It is only provided for Windows since other systems support UTF-8 natively.
746  class UTF16ToUTF8 {
747  private:
748  MemoryBuffer<char, INLINE_BUFFER_SIZE> buffer_;
749 
750  public:
751  UTF16ToUTF8() {}
752  explicit UTF16ToUTF8(WStringRef s);
753  operator StringRef() const { return StringRef(&buffer_[0], size()); }
754  size_t size() const { return buffer_.size() - 1; }
755  const char *c_str() const { return &buffer_[0]; }
756  std::string str() const { return std::string(&buffer_[0], size()); }
757 
758  // Performs conversion returning a system error code instead of
759  // throwing exception on conversion error. This method may still throw
760  // in case of memory allocation error.
761  int convert(WStringRef s);
762  };
763 
764  void format_windows_error(fmt::Writer &out, int error_code,
765  fmt::StringRef message) FMT_NOEXCEPT;
766 #endif
767 
768  void format_system_error(fmt::Writer &out, int error_code,
769  fmt::StringRef message) FMT_NOEXCEPT;
770 
771  // A formatting argument value.
772  struct Value {
773  template <typename Char>
774  struct StringValue {
775  const Char *value;
776  std::size_t size;
777  };
778 
779  typedef void(*FormatFunc)(
780  void *formatter, const void *arg, void *format_str_ptr);
781 
782  struct CustomValue {
783  const void *value;
784  FormatFunc format;
785  };
786 
787  union {
788  int int_value;
789  unsigned uint_value;
790  LongLong long_long_value;
791  ULongLong ulong_long_value;
792  double double_value;
793  long double long_double_value;
794  const void *pointer;
795  StringValue<char> string;
796  StringValue<signed char> sstring;
797  StringValue<unsigned char> ustring;
798  StringValue<wchar_t> wstring;
799  CustomValue custom;
800  };
801 
802  enum Type {
803  NONE, NAMED_ARG,
804  // Integer types should go first,
805  INT, UINT, LONG_LONG, ULONG_LONG, BOOL, CHAR, LAST_INTEGER_TYPE = CHAR,
806  // followed by floating-point types.
807  DOUBLE, LONG_DOUBLE, LAST_NUMERIC_TYPE = LONG_DOUBLE,
808  CSTRING, STRING, WSTRING, POINTER, CUSTOM
809  };
810  };
811 
812  // A formatting argument. It is a POD type to allow storage in
813  // internal::MemoryBuffer.
814  struct Arg : Value {
815  Type type;
816  };
817 
818  template <typename Char>
819  struct NamedArg;
820 
821  template <typename T = void>
822  struct Null {};
823 
824  // A helper class template to enable or disable overloads taking wide
825  // characters and strings in MakeValue.
826  template <typename T, typename Char>
827  struct WCharHelper {
828  typedef Null<T> Supported;
829  typedef T Unsupported;
830  };
831 
832  template <typename T>
833  struct WCharHelper<T, wchar_t> {
834  typedef T Supported;
835  typedef Null<T> Unsupported;
836  };
837 
838  template <typename T>
839  class IsConvertibleToInt {
840  private:
841  typedef char yes[1];
842  typedef char no[2];
843 
844  static const T &get();
845 
846  static yes &convert(fmt::ULongLong);
847  static no &convert(...);
848 
849  public:
850  enum { value = (sizeof(convert(get())) == sizeof(yes)) };
851  };
852 
853 #define FMT_CONVERTIBLE_TO_INT(Type) \
854  template <> \
855  class IsConvertibleToInt<Type> { \
856  public: \
857  enum { value = 1 }; \
858  }
859 
860  // Silence warnings about convering float to int.
861  FMT_CONVERTIBLE_TO_INT(float);
862  FMT_CONVERTIBLE_TO_INT(double);
863  FMT_CONVERTIBLE_TO_INT(long double);
864 
865  template<bool B, class T = void>
866  struct EnableIf {};
867 
868  template<class T>
869  struct EnableIf<true, T> { typedef T type; };
870 
871  template<bool B, class T, class F>
872  struct Conditional { typedef T type; };
873 
874  template<class T, class F>
875  struct Conditional<false, T, F> { typedef F type; };
876 
877  // A helper function to suppress bogus "conditional expression is constant"
878  // warnings.
879  inline bool check(bool value) { return value; }
880 
881  // Makes an Arg object from any type.
882  template <typename Char>
883  class MakeValue : public Arg {
884  private:
885  // The following two methods are private to disallow formatting of
886  // arbitrary pointers. If you want to output a pointer cast it to
887  // "void *" or "const void *". In particular, this forbids formatting
888  // of "[const] volatile char *" which is printed as bool by iostreams.
889  // Do not implement!
890  template <typename T>
891  MakeValue(const T *value);
892  template <typename T>
893  MakeValue(T *value);
894 
895  // The following methods are private to disallow formatting of wide
896  // characters and strings into narrow strings as in
897  // fmt::format("{}", L"test");
898  // To fix this, use a wide format string: fmt::format(L"{}", L"test").
899  MakeValue(typename WCharHelper<wchar_t, Char>::Unsupported);
900  MakeValue(typename WCharHelper<wchar_t *, Char>::Unsupported);
901  MakeValue(typename WCharHelper<const wchar_t *, Char>::Unsupported);
902  MakeValue(typename WCharHelper<const std::wstring &, Char>::Unsupported);
903  MakeValue(typename WCharHelper<WStringRef, Char>::Unsupported);
904 
905  void set_string(StringRef str) {
906  string.value = str.data();
907  string.size = str.size();
908  }
909 
910  void set_string(WStringRef str) {
911  wstring.value = str.data();
912  wstring.size = str.size();
913  }
914 
915  // Formats an argument of a custom type, such as a user-defined class.
916  template <typename T>
917  static void format_custom_arg(
918  void *formatter, const void *arg, void *format_str_ptr) {
919  format(*static_cast<BasicFormatter<Char>*>(formatter),
920  *static_cast<const Char**>(format_str_ptr),
921  *static_cast<const T*>(arg));
922  }
923 
924  public:
925  MakeValue() {}
926 
927 #define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \
928  MakeValue(Type value) { field = rhs; } \
929  static uint64_t type(Type) { return Arg::TYPE; }
930 
931 #define FMT_MAKE_VALUE(Type, field, TYPE) \
932  FMT_MAKE_VALUE_(Type, field, TYPE, value)
933 
934  FMT_MAKE_VALUE(bool, int_value, BOOL)
935  FMT_MAKE_VALUE(short, int_value, INT)
936  FMT_MAKE_VALUE(unsigned short, uint_value, UINT)
937  FMT_MAKE_VALUE(int, int_value, INT)
938  FMT_MAKE_VALUE(unsigned, uint_value, UINT)
939 
940  MakeValue(long value) {
941  // To minimize the number of types we need to deal with, long is
942  // translated either to int or to long long depending on its size.
943  if (check(sizeof(long) == sizeof(int)))
944  int_value = static_cast<int>(value);
945  else
946  long_long_value = value;
947  }
948  static uint64_t type(long) {
949  return sizeof(long) == sizeof(int) ? Arg::INT : Arg::LONG_LONG;
950  }
951 
952  MakeValue(unsigned long value) {
953  if (check(sizeof(unsigned long) == sizeof(unsigned)))
954  uint_value = static_cast<unsigned>(value);
955  else
956  ulong_long_value = value;
957  }
958  static uint64_t type(unsigned long) {
959  return sizeof(unsigned long) == sizeof(unsigned) ?
960  Arg::UINT : Arg::ULONG_LONG;
961  }
962 
963  FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG)
964  FMT_MAKE_VALUE(ULongLong, ulong_long_value, ULONG_LONG)
965  FMT_MAKE_VALUE(float, double_value, DOUBLE)
966  FMT_MAKE_VALUE(double, double_value, DOUBLE)
967  FMT_MAKE_VALUE(long double, long_double_value, LONG_DOUBLE)
968  FMT_MAKE_VALUE(signed char, int_value, CHAR)
969  FMT_MAKE_VALUE(unsigned char, int_value, CHAR)
970  FMT_MAKE_VALUE(char, int_value, CHAR)
971 
972  MakeValue(typename WCharHelper<wchar_t, Char>::Supported value) {
973  int_value = value;
974  }
975  static uint64_t type(wchar_t) { return Arg::CHAR; }
976 
977 #define FMT_MAKE_STR_VALUE(Type, TYPE) \
978  MakeValue(Type value) { set_string(value); } \
979  static uint64_t type(Type) { return Arg::TYPE; }
980 
981  FMT_MAKE_VALUE(char *, string.value, CSTRING)
982  FMT_MAKE_VALUE(const char *, string.value, CSTRING)
983  FMT_MAKE_VALUE(const signed char *, sstring.value, CSTRING)
984  FMT_MAKE_VALUE(const unsigned char *, ustring.value, CSTRING)
985  FMT_MAKE_STR_VALUE(const std::string &, STRING)
986  FMT_MAKE_STR_VALUE(StringRef, STRING)
987  FMT_MAKE_VALUE_(CStringRef, string.value, CSTRING, value.c_str())
988 
989 #define FMT_MAKE_WSTR_VALUE(Type, TYPE) \
990  MakeValue(typename WCharHelper<Type, Char>::Supported value) { \
991  set_string(value); \
992  } \
993  static uint64_t type(Type) { return Arg::TYPE; }
994 
995  FMT_MAKE_WSTR_VALUE(wchar_t *, WSTRING)
996  FMT_MAKE_WSTR_VALUE(const wchar_t *, WSTRING)
997  FMT_MAKE_WSTR_VALUE(const std::wstring &, WSTRING)
998  FMT_MAKE_WSTR_VALUE(WStringRef, WSTRING)
999 
1000  FMT_MAKE_VALUE(void *, pointer, POINTER)
1001  FMT_MAKE_VALUE(const void *, pointer, POINTER)
1002 
1003  template <typename T>
1004  MakeValue(const T &value,
1005  typename EnableIf<!IsConvertibleToInt<T>::value, int>::type = 0) {
1006  custom.value = &value;
1007  custom.format = &format_custom_arg<T>;
1008  }
1009 
1010  template <typename T>
1011  MakeValue(const T &value,
1012  typename EnableIf<IsConvertibleToInt<T>::value, int>::type = 0) {
1013  int_value = value;
1014  }
1015 
1016  template <typename T>
1017  static uint64_t type(const T &) {
1018  return IsConvertibleToInt<T>::value ? Arg::INT : Arg::CUSTOM;
1019  }
1020 
1021  // Additional template param `Char_` is needed here because make_type always
1022  // uses MakeValue<char>.
1023  template <typename Char_>
1024  MakeValue(const NamedArg<Char_> &value) { pointer = &value; }
1025 
1026  template <typename Char_>
1027  static uint64_t type(const NamedArg<Char_> &) { return Arg::NAMED_ARG; }
1028  };
1029 
1030  template <typename Char>
1031  struct NamedArg : Arg {
1032  BasicStringRef<Char> name;
1033 
1034  template <typename T>
1035  NamedArg(BasicStringRef<Char> argname, const T &value)
1036  : Arg(MakeValue<Char>(value)), name(argname) {
1037  type = static_cast<internal::Arg::Type>(MakeValue<Char>::type(value));
1038  }
1039  };
1040 
1041 #define FMT_DISPATCH(call) static_cast<Impl*>(this)->call
1042 
1043  // An argument visitor.
1044  // To use ArgVisitor define a subclass that implements some or all of the
1045  // visit methods with the same signatures as the methods in ArgVisitor,
1046  // for example, visit_int(int).
1047  // Specify the subclass name as the Impl template parameter. Then calling
1048  // ArgVisitor::visit for some argument will dispatch to a visit method
1049  // specific to the argument type. For example, if the argument type is
1050  // double then visit_double(double) method of a subclass will be called.
1051  // If the subclass doesn't contain a method with this signature, then
1052  // a corresponding method of ArgVisitor will be called.
1053  //
1054  // Example:
1055  // class MyArgVisitor : public ArgVisitor<MyArgVisitor, void> {
1056  // public:
1057  // void visit_int(int value) { print("{}", value); }
1058  // void visit_double(double value) { print("{}", value ); }
1059  // };
1060  //
1061  // ArgVisitor uses the curiously recurring template pattern:
1062  // http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
1063  template <typename Impl, typename Result>
1064  class ArgVisitor {
1065  public:
1066  void report_unhandled_arg() {}
1067 
1068  Result visit_unhandled_arg() {
1069  FMT_DISPATCH(report_unhandled_arg());
1070  return Result();
1071  }
1072 
1073  Result visit_int(int value) {
1074  return FMT_DISPATCH(visit_any_int(value));
1075  }
1076  Result visit_long_long(LongLong value) {
1077  return FMT_DISPATCH(visit_any_int(value));
1078  }
1079  Result visit_uint(unsigned value) {
1080  return FMT_DISPATCH(visit_any_int(value));
1081  }
1082  Result visit_ulong_long(ULongLong value) {
1083  return FMT_DISPATCH(visit_any_int(value));
1084  }
1085  Result visit_bool(bool value) {
1086  return FMT_DISPATCH(visit_any_int(value));
1087  }
1088  Result visit_char(int value) {
1089  return FMT_DISPATCH(visit_any_int(value));
1090  }
1091  template <typename T>
1092  Result visit_any_int(T) {
1093  return FMT_DISPATCH(visit_unhandled_arg());
1094  }
1095 
1096  Result visit_double(double value) {
1097  return FMT_DISPATCH(visit_any_double(value));
1098  }
1099  Result visit_long_double(long double value) {
1100  return FMT_DISPATCH(visit_any_double(value));
1101  }
1102  template <typename T>
1103  Result visit_any_double(T) {
1104  return FMT_DISPATCH(visit_unhandled_arg());
1105  }
1106 
1107  Result visit_string(Arg::StringValue<char>) {
1108  return FMT_DISPATCH(visit_unhandled_arg());
1109  }
1110  Result visit_wstring(Arg::StringValue<wchar_t>) {
1111  return FMT_DISPATCH(visit_unhandled_arg());
1112  }
1113  Result visit_pointer(const void *) {
1114  return FMT_DISPATCH(visit_unhandled_arg());
1115  }
1116  Result visit_custom(Arg::CustomValue) {
1117  return FMT_DISPATCH(visit_unhandled_arg());
1118  }
1119 
1120  Result visit(const Arg &arg) {
1121  switch (arg.type) {
1122  default:
1123  FMT_ASSERT(false, "invalid argument type");
1124  return Result();
1125  case Arg::INT:
1126  return FMT_DISPATCH(visit_int(arg.int_value));
1127  case Arg::UINT:
1128  return FMT_DISPATCH(visit_uint(arg.uint_value));
1129  case Arg::LONG_LONG:
1130  return FMT_DISPATCH(visit_long_long(arg.long_long_value));
1131  case Arg::ULONG_LONG:
1132  return FMT_DISPATCH(visit_ulong_long(arg.ulong_long_value));
1133  case Arg::BOOL:
1134  return FMT_DISPATCH(visit_bool(arg.int_value != 0));
1135  case Arg::CHAR:
1136  return FMT_DISPATCH(visit_char(arg.int_value));
1137  case Arg::DOUBLE:
1138  return FMT_DISPATCH(visit_double(arg.double_value));
1139  case Arg::LONG_DOUBLE:
1140  return FMT_DISPATCH(visit_long_double(arg.long_double_value));
1141  case Arg::CSTRING: {
1142  Arg::StringValue<char> str = arg.string;
1143  str.size = 0;
1144  return FMT_DISPATCH(visit_string(str));
1145  }
1146  case Arg::STRING:
1147  return FMT_DISPATCH(visit_string(arg.string));
1148  case Arg::WSTRING:
1149  return FMT_DISPATCH(visit_wstring(arg.wstring));
1150  case Arg::POINTER:
1151  return FMT_DISPATCH(visit_pointer(arg.pointer));
1152  case Arg::CUSTOM:
1153  return FMT_DISPATCH(visit_custom(arg.custom));
1154  }
1155  }
1156  };
1157 
1158  class RuntimeError : public std::runtime_error {
1159  protected:
1160  RuntimeError() : std::runtime_error("") {}
1161  };
1162 
1163  template <typename Impl, typename Char>
1164  class BasicArgFormatter;
1165 
1166  template <typename Char>
1167  class PrintfArgFormatter;
1168 
1169  template <typename Char>
1170  class ArgMap;
1171  } // namespace internal
1172 
1174  class ArgList {
1175  private:
1176  // To reduce compiled code size per formatting function call, types of first
1177  // MAX_PACKED_ARGS arguments are passed in the types_ field.
1178  uint64_t types_;
1179  union {
1180  // If the number of arguments is less than MAX_PACKED_ARGS, the argument
1181  // values are stored in values_, otherwise they are stored in args_.
1182  // This is done to reduce compiled code size as storing larger objects
1183  // may require more code (at least on x86-64) even if the same amount of
1184  // data is actually copied to stack. It saves ~10% on the bloat test.
1185  const internal::Value *values_;
1186  const internal::Arg *args_;
1187  };
1188 
1189  internal::Arg::Type type(unsigned index) const {
1190  unsigned shift = index * 4;
1191  uint64_t mask = 0xf;
1192  return static_cast<internal::Arg::Type>(
1193  (types_ & (mask << shift)) >> shift);
1194  }
1195 
1196  template <typename Char>
1197  friend class internal::ArgMap;
1198 
1199  public:
1200  // Maximum number of arguments with packed types.
1201  enum { MAX_PACKED_ARGS = 16 };
1202 
1203  ArgList() : types_(0) {}
1204 
1205  ArgList(ULongLong types, const internal::Value *values)
1206  : types_(types), values_(values) {}
1207  ArgList(ULongLong types, const internal::Arg *args)
1208  : types_(types), args_(args) {}
1209 
1211  internal::Arg operator[](unsigned index) const {
1212  using internal::Arg;
1213  Arg arg;
1214  bool use_values = type(MAX_PACKED_ARGS - 1) == Arg::NONE;
1215  if (index < MAX_PACKED_ARGS) {
1216  Arg::Type arg_type = type(index);
1217  internal::Value &val = arg;
1218  if (arg_type != Arg::NONE)
1219  val = use_values ? values_[index] : args_[index];
1220  arg.type = arg_type;
1221  return arg;
1222  }
1223  if (use_values) {
1224  // The index is greater than the number of arguments that can be stored
1225  // in values, so return a "none" argument.
1226  arg.type = Arg::NONE;
1227  return arg;
1228  }
1229  for (unsigned i = MAX_PACKED_ARGS; i <= index; ++i) {
1230  if (args_[i].type == Arg::NONE)
1231  return args_[i];
1232  }
1233  return args_[index];
1234  }
1235  };
1236 
1237  struct FormatSpec;
1238 
1239  namespace internal {
1240 
1241  template <typename Char>
1242  class ArgMap {
1243  private:
1244  typedef std::map<fmt::BasicStringRef<Char>, internal::Arg> MapType;
1245  typedef typename MapType::value_type Pair;
1246 
1247  MapType map_;
1248 
1249  public:
1250  void init(const ArgList &args);
1251 
1252  const internal::Arg* find(const fmt::BasicStringRef<Char> &name) const {
1253  typename MapType::const_iterator it = map_.find(name);
1254  return it != map_.end() ? &it->second : 0;
1255  }
1256  };
1257 
1258  class FormatterBase {
1259  private:
1260  ArgList args_;
1261  int next_arg_index_;
1262 
1263  // Returns the argument with specified index.
1264  Arg do_get_arg(unsigned arg_index, const char *&error);
1265 
1266  protected:
1267  const ArgList &args() const { return args_; }
1268 
1269  explicit FormatterBase(const ArgList &args) {
1270  args_ = args;
1271  next_arg_index_ = 0;
1272  }
1273 
1274  // Returns the next argument.
1275  Arg next_arg(const char *&error);
1276 
1277  // Checks if manual indexing is used and returns the argument with
1278  // specified index.
1279  Arg get_arg(unsigned arg_index, const char *&error);
1280 
1281  bool check_no_auto_index(const char *&error);
1282 
1283  template <typename Char>
1284  void write(BasicWriter<Char> &w, const Char *start, const Char *end) {
1285  if (start != end)
1286  w << BasicStringRef<Char>(start, end - start);
1287  }
1288  };
1289 
1290  // A printf formatter.
1291  template <typename Char>
1292  class PrintfFormatter : private FormatterBase {
1293  private:
1294  void parse_flags(FormatSpec &spec, const Char *&s);
1295 
1296  // Returns the argument with specified index or, if arg_index is equal
1297  // to the maximum unsigned value, the next argument.
1298  Arg get_arg(const Char *s,
1299  unsigned arg_index = (std::numeric_limits<unsigned>::max)());
1300 
1301  // Parses argument index, flags and width and returns the argument index.
1302  unsigned parse_header(const Char *&s, FormatSpec &spec);
1303 
1304  public:
1305  explicit PrintfFormatter(const ArgList &args) : FormatterBase(args) {}
1306  void format(BasicWriter<Char> &writer, BasicCStringRef<Char> format_str);
1307  };
1308  } // namespace internal
1309 
1310  // A formatter.
1311  template <typename Char>
1312  class BasicFormatter : private internal::FormatterBase {
1313  private:
1314  BasicWriter<Char> &writer_;
1315  internal::ArgMap<Char> map_;
1316 
1317  FMT_DISALLOW_COPY_AND_ASSIGN(BasicFormatter);
1318 
1319  using FormatterBase::get_arg;
1320 
1321  // Checks if manual indexing is used and returns the argument with
1322  // specified name.
1323  internal::Arg get_arg(BasicStringRef<Char> arg_name, const char *&error);
1324 
1325  // Parses argument index and returns corresponding argument.
1326  internal::Arg parse_arg_index(const Char *&s);
1327 
1328  // Parses argument name and returns corresponding argument.
1329  internal::Arg parse_arg_name(const Char *&s);
1330 
1331  public:
1332  BasicFormatter(const ArgList &args, BasicWriter<Char> &w)
1333  : FormatterBase(args), writer_(w) {}
1334 
1335  BasicWriter<Char> &writer() { return writer_; }
1336 
1337  void format(BasicCStringRef<Char> format_str);
1338 
1339  const Char *format(const Char *&format_str, const internal::Arg &arg);
1340  };
1341 
1342  enum Alignment {
1343  ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC
1344  };
1345 
1346  // Flags.
1347  enum {
1348  SIGN_FLAG = 1, PLUS_FLAG = 2, MINUS_FLAG = 4, HASH_FLAG = 8,
1349  CHAR_FLAG = 0x10 // Argument has char type - used in error reporting.
1350  };
1351 
1352  // An empty format specifier.
1353  struct EmptySpec {};
1354 
1355  // A type specifier.
1356  template <char TYPE>
1357  struct TypeSpec : EmptySpec {
1358  Alignment align() const { return ALIGN_DEFAULT; }
1359  unsigned width() const { return 0; }
1360  int precision() const { return -1; }
1361  bool flag(unsigned) const { return false; }
1362  char type() const { return TYPE; }
1363  char fill() const { return ' '; }
1364  };
1365 
1366  // A width specifier.
1367  struct WidthSpec {
1368  unsigned width_;
1369  // Fill is always wchar_t and cast to char if necessary to avoid having
1370  // two specialization of WidthSpec and its subclasses.
1371  wchar_t fill_;
1372 
1373  WidthSpec(unsigned width, wchar_t fill) : width_(width), fill_(fill) {}
1374 
1375  unsigned width() const { return width_; }
1376  wchar_t fill() const { return fill_; }
1377  };
1378 
1379  // An alignment specifier.
1380  struct AlignSpec : WidthSpec {
1381  Alignment align_;
1382 
1383  AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT)
1384  : WidthSpec(width, fill), align_(align) {}
1385 
1386  Alignment align() const { return align_; }
1387 
1388  int precision() const { return -1; }
1389  };
1390 
1391  // An alignment and type specifier.
1392  template <char TYPE>
1393  struct AlignTypeSpec : AlignSpec {
1394  AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {}
1395 
1396  bool flag(unsigned) const { return false; }
1397  char type() const { return TYPE; }
1398  };
1399 
1400  // A full format specifier.
1401  struct FormatSpec : AlignSpec {
1402  unsigned flags_;
1403  int precision_;
1404  char type_;
1405 
1406  FormatSpec(
1407  unsigned width = 0, char type = 0, wchar_t fill = ' ')
1408  : AlignSpec(width, fill), flags_(0), precision_(-1), type_(type) {}
1409 
1410  bool flag(unsigned f) const { return (flags_ & f) != 0; }
1411  int precision() const { return precision_; }
1412  char type() const { return type_; }
1413  };
1414 
1415  // An integer format specifier.
1416  template <typename T, typename SpecT = TypeSpec<0>, typename Char = char>
1417  class IntFormatSpec : public SpecT {
1418  private:
1419  T value_;
1420 
1421  public:
1422  IntFormatSpec(T val, const SpecT &spec = SpecT())
1423  : SpecT(spec), value_(val) {}
1424 
1425  T value() const { return value_; }
1426  };
1427 
1428  // A string format specifier.
1429  template <typename Char>
1430  class StrFormatSpec : public AlignSpec {
1431  private:
1432  const Char *str_;
1433 
1434  public:
1435  template <typename FillChar>
1436  StrFormatSpec(const Char *str, unsigned width, FillChar fill)
1437  : AlignSpec(width, fill), str_(str) {
1438  internal::CharTraits<Char>::convert(FillChar());
1439  }
1440 
1441  const Char *str() const { return str_; }
1442  };
1443 
1447  IntFormatSpec<int, TypeSpec<'b'> > bin(int value);
1448 
1452  IntFormatSpec<int, TypeSpec<'o'> > oct(int value);
1453 
1458  IntFormatSpec<int, TypeSpec<'x'> > hex(int value);
1459 
1464  IntFormatSpec<int, TypeSpec<'X'> > hexu(int value);
1465 
1477  template <char TYPE_CODE, typename Char>
1478  IntFormatSpec<int, AlignTypeSpec<TYPE_CODE>, Char> pad(
1479  int value, unsigned width, Char fill = ' ');
1480 
1481 #define FMT_DEFINE_INT_FORMATTERS(TYPE) \
1482 inline IntFormatSpec<TYPE, TypeSpec<'b'> > bin(TYPE value) { \
1483  return IntFormatSpec<TYPE, TypeSpec<'b'> >(value, TypeSpec<'b'>()); \
1484  } \
1485  \
1486 inline IntFormatSpec<TYPE, TypeSpec<'o'> > oct(TYPE value) { \
1487  return IntFormatSpec<TYPE, TypeSpec<'o'> >(value, TypeSpec<'o'>()); \
1488  } \
1489  \
1490 inline IntFormatSpec<TYPE, TypeSpec<'x'> > hex(TYPE value) { \
1491  return IntFormatSpec<TYPE, TypeSpec<'x'> >(value, TypeSpec<'x'>()); \
1492  } \
1493  \
1494 inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
1495  return IntFormatSpec<TYPE, TypeSpec<'X'> >(value, TypeSpec<'X'>()); \
1496  } \
1497  \
1498 template <char TYPE_CODE> \
1499 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> > pad( \
1500  IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, unsigned width) { \
1501  return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> >( \
1502  f.value(), AlignTypeSpec<TYPE_CODE>(width, ' ')); \
1503  } \
1504  \
1505 /* For compatibility with older compilers we provide two overloads for pad, */ \
1506 /* one that takes a fill character and one that doesn't. In the future this */ \
1507 /* can be replaced with one overload making the template argument Char */ \
1508 /* default to char (C++11). */ \
1509 template <char TYPE_CODE, typename Char> \
1510 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char> pad( \
1511  IntFormatSpec<TYPE, TypeSpec<TYPE_CODE>, Char> f, \
1512  unsigned width, Char fill) { \
1513  return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char>( \
1514  f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \
1515  } \
1516  \
1517 inline IntFormatSpec<TYPE, AlignTypeSpec<0> > pad( \
1518  TYPE value, unsigned width) { \
1519  return IntFormatSpec<TYPE, AlignTypeSpec<0> >( \
1520  value, AlignTypeSpec<0>(width, ' ')); \
1521  } \
1522  \
1523 template <typename Char> \
1524 inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
1525  TYPE value, unsigned width, Char fill) { \
1526  return IntFormatSpec<TYPE, AlignTypeSpec<0>, Char>( \
1527  value, AlignTypeSpec<0>(width, fill)); \
1528  }
1529 
1530  FMT_DEFINE_INT_FORMATTERS(int)
1531  FMT_DEFINE_INT_FORMATTERS(long)
1532  FMT_DEFINE_INT_FORMATTERS(unsigned)
1533  FMT_DEFINE_INT_FORMATTERS(unsigned long)
1534  FMT_DEFINE_INT_FORMATTERS(LongLong)
1535  FMT_DEFINE_INT_FORMATTERS(ULongLong)
1536 
1537 
1546  template <typename Char>
1547  inline StrFormatSpec<Char> pad(
1548  const Char *str, unsigned width, Char fill = ' ') {
1549  return StrFormatSpec<Char>(str, width, fill);
1550  }
1551 
1552  inline StrFormatSpec<wchar_t> pad(
1553  const wchar_t *str, unsigned width, char fill = ' ') {
1554  return StrFormatSpec<wchar_t>(str, width, fill);
1555  }
1556 
1557  // Generates a comma-separated list with results of applying f to
1558  // numbers 0..n-1.
1559 # define FMT_GEN(n, f) FMT_GEN##n(f)
1560 # define FMT_GEN1(f) f(0)
1561 # define FMT_GEN2(f) FMT_GEN1(f), f(1)
1562 # define FMT_GEN3(f) FMT_GEN2(f), f(2)
1563 # define FMT_GEN4(f) FMT_GEN3(f), f(3)
1564 # define FMT_GEN5(f) FMT_GEN4(f), f(4)
1565 # define FMT_GEN6(f) FMT_GEN5(f), f(5)
1566 # define FMT_GEN7(f) FMT_GEN6(f), f(6)
1567 # define FMT_GEN8(f) FMT_GEN7(f), f(7)
1568 # define FMT_GEN9(f) FMT_GEN8(f), f(8)
1569 # define FMT_GEN10(f) FMT_GEN9(f), f(9)
1570 # define FMT_GEN11(f) FMT_GEN10(f), f(10)
1571 # define FMT_GEN12(f) FMT_GEN11(f), f(11)
1572 # define FMT_GEN13(f) FMT_GEN12(f), f(12)
1573 # define FMT_GEN14(f) FMT_GEN13(f), f(13)
1574 # define FMT_GEN15(f) FMT_GEN14(f), f(14)
1575 
1576  namespace internal {
1577  inline uint64_t make_type() { return 0; }
1578 
1579  template <typename T>
1580  inline uint64_t make_type(const T &arg) { return MakeValue<char>::type(arg); }
1581 
1582  template <unsigned N>
1583  struct ArgArray {
1584  // Computes the argument array size by adding 1 to N, which is the number of
1585  // arguments, if N is zero, because array of zero size is invalid, or if N
1586  // is greater than ArgList::MAX_PACKED_ARGS to accommodate for an extra
1587  // argument that marks the end of the list.
1588  enum { SIZE = N + (N == 0 || N >= ArgList::MAX_PACKED_ARGS ? 1 : 0) };
1589 
1590  typedef typename Conditional<
1591  (N < ArgList::MAX_PACKED_ARGS), Value, Arg>::type Type[SIZE];
1592  };
1593 
1594 #if FMT_USE_VARIADIC_TEMPLATES
1595  template <typename Arg, typename... Args>
1596  inline uint64_t make_type(const Arg &first, const Args & ... tail) {
1597  return make_type(first) | (make_type(tail...) << 4);
1598  }
1599 
1600  inline void do_set_types(Arg *) {}
1601 
1602  template <typename T, typename... Args>
1603  inline void do_set_types(Arg *args, const T &arg, const Args & ... tail) {
1604  args->type = static_cast<Arg::Type>(MakeValue<T>::type(arg));
1605  do_set_types(args + 1, tail...);
1606  }
1607 
1608  template <typename... Args>
1609  inline void set_types(Arg *array, const Args & ... args) {
1610  if (check(sizeof...(Args) > ArgList::MAX_PACKED_ARGS))
1611  do_set_types(array, args...);
1612  array[sizeof...(Args)].type = Arg::NONE;
1613  }
1614 
1615  template <typename... Args>
1616  inline void set_types(Value *, const Args & ...) {
1617  // Do nothing as types are passed separately from values.
1618  }
1619 
1620  template <typename Char, typename Value>
1621  inline void store_args(Value *) {}
1622 
1623  template <typename Char, typename Arg, typename T, typename... Args>
1624  inline void store_args(Arg *args, const T &arg, const Args & ... tail) {
1625  // Assign only the Value subobject of Arg and don't overwrite type (if any)
1626  // that is assigned by set_types.
1627  Value &value = *args;
1628  value = MakeValue<Char>(arg);
1629  store_args<Char>(args + 1, tail...);
1630  }
1631 
1632  template <typename Char, typename... Args>
1633  ArgList make_arg_list(typename ArgArray<sizeof...(Args)>::Type array,
1634  const Args & ... args) {
1635  if (check(sizeof...(Args) >= ArgList::MAX_PACKED_ARGS))
1636  set_types(array, args...);
1637  store_args<Char>(array, args...);
1638  return ArgList(make_type(args...), array);
1639  }
1640 #else
1641 
1642  struct ArgType {
1643  uint64_t type;
1644 
1645  ArgType() : type(0) {}
1646 
1647  template <typename T>
1648  ArgType(const T &arg) : type(make_type(arg)) {}
1649  };
1650 
1651 # define FMT_ARG_TYPE_DEFAULT(n) ArgType t##n = ArgType()
1652 
1653  inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) {
1654  return t0.type | (t1.type << 4) | (t2.type << 8) | (t3.type << 12) |
1655  (t4.type << 16) | (t5.type << 20) | (t6.type << 24) | (t7.type << 28) |
1656  (t8.type << 32) | (t9.type << 36) | (t10.type << 40) | (t11.type << 44) |
1657  (t12.type << 48) | (t13.type << 52) | (t14.type << 56);
1658  }
1659 #endif
1660  } // namespace internal
1661 
1662 # define FMT_MAKE_TEMPLATE_ARG(n) typename T##n
1663 # define FMT_MAKE_ARG_TYPE(n) T##n
1664 # define FMT_MAKE_ARG(n) const T##n &v##n
1665 # define FMT_MAKE_REF_char(n) fmt::internal::MakeValue<char>(v##n)
1666 # define FMT_MAKE_REF_wchar_t(n) fmt::internal::MakeValue<wchar_t>(v##n)
1667 
1668 #if FMT_USE_VARIADIC_TEMPLATES
1669  // Defines a variadic function returning void.
1670 # define FMT_VARIADIC_VOID(func, arg_type) \
1671  template <typename... Args> \
1672  void func(arg_type arg0, const Args & ... args) { \
1673  typename fmt::internal::ArgArray<sizeof...(Args)>::Type array; \
1674  func(arg0, fmt::internal::make_arg_list<Char>(array, args...)); \
1675  }
1676 
1677  // Defines a variadic constructor.
1678 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
1679  template <typename... Args> \
1680  ctor(arg0_type arg0, arg1_type arg1, const Args & ... args) { \
1681  typename fmt::internal::ArgArray<sizeof...(Args)>::Type array; \
1682  func(arg0, arg1, fmt::internal::make_arg_list<Char>(array, args...)); \
1683  }
1684 
1685 #else
1686 
1687 # define FMT_MAKE_REF(n) fmt::internal::MakeValue<Char>(v##n)
1688 # define FMT_MAKE_REF2(n) v##n
1689 
1690  // Defines a wrapper for a function taking one argument of type arg_type
1691  // and n additional arguments of arbitrary types.
1692 # define FMT_WRAP1(func, arg_type, n) \
1693  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
1694  inline void func(arg_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
1695  const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
1696  func(arg1, fmt::ArgList( \
1697  fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
1698  }
1699 
1700  // Emulates a variadic function returning void on a pre-C++11 compiler.
1701 # define FMT_VARIADIC_VOID(func, arg_type) \
1702  inline void func(arg_type arg) { func(arg, fmt::ArgList()); } \
1703  FMT_WRAP1(func, arg_type, 1) FMT_WRAP1(func, arg_type, 2) \
1704  FMT_WRAP1(func, arg_type, 3) FMT_WRAP1(func, arg_type, 4) \
1705  FMT_WRAP1(func, arg_type, 5) FMT_WRAP1(func, arg_type, 6) \
1706  FMT_WRAP1(func, arg_type, 7) FMT_WRAP1(func, arg_type, 8) \
1707  FMT_WRAP1(func, arg_type, 9) FMT_WRAP1(func, arg_type, 10)
1708 
1709 # define FMT_CTOR(ctor, func, arg0_type, arg1_type, n) \
1710  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
1711  ctor(arg0_type arg0, arg1_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
1712  const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
1713  func(arg0, arg1, fmt::ArgList( \
1714  fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
1715  }
1716 
1717  // Emulates a variadic constructor on a pre-C++11 compiler.
1718 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
1719  FMT_CTOR(ctor, func, arg0_type, arg1_type, 1) \
1720  FMT_CTOR(ctor, func, arg0_type, arg1_type, 2) \
1721  FMT_CTOR(ctor, func, arg0_type, arg1_type, 3) \
1722  FMT_CTOR(ctor, func, arg0_type, arg1_type, 4) \
1723  FMT_CTOR(ctor, func, arg0_type, arg1_type, 5) \
1724  FMT_CTOR(ctor, func, arg0_type, arg1_type, 6) \
1725  FMT_CTOR(ctor, func, arg0_type, arg1_type, 7) \
1726  FMT_CTOR(ctor, func, arg0_type, arg1_type, 8) \
1727  FMT_CTOR(ctor, func, arg0_type, arg1_type, 9) \
1728  FMT_CTOR(ctor, func, arg0_type, arg1_type, 10)
1729 #endif
1730 
1731  // Generates a comma-separated list with results of applying f to pairs
1732  // (argument, index).
1733 #define FMT_FOR_EACH1(f, x0) f(x0, 0)
1734 #define FMT_FOR_EACH2(f, x0, x1) \
1735  FMT_FOR_EACH1(f, x0), f(x1, 1)
1736 #define FMT_FOR_EACH3(f, x0, x1, x2) \
1737  FMT_FOR_EACH2(f, x0 ,x1), f(x2, 2)
1738 #define FMT_FOR_EACH4(f, x0, x1, x2, x3) \
1739  FMT_FOR_EACH3(f, x0, x1, x2), f(x3, 3)
1740 #define FMT_FOR_EACH5(f, x0, x1, x2, x3, x4) \
1741  FMT_FOR_EACH4(f, x0, x1, x2, x3), f(x4, 4)
1742 #define FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5) \
1743  FMT_FOR_EACH5(f, x0, x1, x2, x3, x4), f(x5, 5)
1744 #define FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6) \
1745  FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5), f(x6, 6)
1746 #define FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7) \
1747  FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6), f(x7, 7)
1748 #define FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8) \
1749  FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7), f(x8, 8)
1750 #define FMT_FOR_EACH10(f, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) \
1751  FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8), f(x9, 9)
1752 
1757  class SystemError : public internal::RuntimeError {
1758  private:
1759  void init(int err_code, CStringRef format_str, ArgList args);
1760 
1761  protected:
1762  int error_code_;
1763 
1764  typedef char Char; // For FMT_VARIADIC_CTOR.
1765 
1766  SystemError() {}
1767 
1768  public:
1791  SystemError(int error_code, CStringRef message) {
1792  init(error_code, message, ArgList());
1793  }
1794  FMT_VARIADIC_CTOR(SystemError, init, int, CStringRef)
1795 
1796  int error_code() const { return error_code_; }
1797  };
1798 
1814  template <typename Char>
1815  class BasicWriter {
1816  private:
1817  // Output buffer.
1818  Buffer<Char> &buffer_;
1819 
1820  FMT_DISALLOW_COPY_AND_ASSIGN(BasicWriter);
1821 
1822  typedef typename internal::CharTraits<Char>::CharPtr CharPtr;
1823 
1824 #if _SECURE_SCL
1825  // Returns pointer value.
1826  static Char *get(CharPtr p) { return p.base(); }
1827 #else
1828  static Char *get(Char *p) { return p; }
1829 #endif
1830 
1831  // Fills the padding around the content and returns the pointer to the
1832  // content area.
1833  static CharPtr fill_padding(CharPtr buffer,
1834  unsigned total_size, std::size_t content_size, wchar_t fill);
1835 
1836  // Grows the buffer by n characters and returns a pointer to the newly
1837  // allocated area.
1838  CharPtr grow_buffer(std::size_t n) {
1839  std::size_t size = buffer_.size();
1840  buffer_.resize(size + n);
1841  return internal::make_ptr(&buffer_[size], n);
1842  }
1843 
1844  // Writes an unsigned decimal integer.
1845  template <typename UInt>
1846  Char *write_unsigned_decimal(UInt value, unsigned prefix_size = 0) {
1847  unsigned num_digits = internal::count_digits(value);
1848  Char *ptr = get(grow_buffer(prefix_size + num_digits));
1849  internal::format_decimal(ptr + prefix_size, value, num_digits);
1850  return ptr;
1851  }
1852 
1853  // Writes a decimal integer.
1854  template <typename Int>
1855  void write_decimal(Int value) {
1856  typename internal::IntTraits<Int>::MainType abs_value = value;
1857  if (internal::is_negative(value)) {
1858  abs_value = 0 - abs_value;
1859  *write_unsigned_decimal(abs_value, 1) = '-';
1860  }
1861  else {
1862  write_unsigned_decimal(abs_value, 0);
1863  }
1864  }
1865 
1866  // Prepare a buffer for integer formatting.
1867  CharPtr prepare_int_buffer(unsigned num_digits,
1868  const EmptySpec &, const char *prefix, unsigned prefix_size) {
1869  unsigned size = prefix_size + num_digits;
1870  CharPtr p = grow_buffer(size);
1871  std::copy(prefix, prefix + prefix_size, p);
1872  return p + size - 1;
1873  }
1874 
1875  template <typename Spec>
1876  CharPtr prepare_int_buffer(unsigned num_digits,
1877  const Spec &spec, const char *prefix, unsigned prefix_size);
1878 
1879  // Formats an integer.
1880  template <typename T, typename Spec>
1881  void write_int(T value, Spec spec);
1882 
1883  // Formats a floating-point number (double or long double).
1884  template <typename T>
1885  void write_double(T value, const FormatSpec &spec);
1886 
1887  // Writes a formatted string.
1888  template <typename StrChar>
1889  CharPtr write_str(
1890  const StrChar *s, std::size_t size, const AlignSpec &spec);
1891 
1892  template <typename StrChar>
1893  void write_str(
1894  const internal::Arg::StringValue<StrChar> &str, const FormatSpec &spec);
1895 
1896  // This following methods are private to disallow writing wide characters
1897  // and strings to a char stream. If you want to print a wide string as a
1898  // pointer as std::ostream does, cast it to const void*.
1899  // Do not implement!
1900  void operator<<(typename internal::WCharHelper<wchar_t, Char>::Unsupported);
1901  void operator<<(
1902  typename internal::WCharHelper<const wchar_t *, Char>::Unsupported);
1903 
1904  // Appends floating-point length specifier to the format string.
1905  // The second argument is only used for overload resolution.
1906  void append_float_length(Char *&format_ptr, long double) {
1907  *format_ptr++ = 'L';
1908  }
1909 
1910  template<typename T>
1911  void append_float_length(Char *&, T) {}
1912 
1913  template <typename Impl, typename Char_>
1914  friend class internal::BasicArgFormatter;
1915 
1916  friend class internal::PrintfArgFormatter<Char>;
1917 
1918  protected:
1922  explicit BasicWriter(Buffer<Char> &b) : buffer_(b) {}
1923 
1924  public:
1930  virtual ~BasicWriter() {}
1931 
1935  std::size_t size() const { return buffer_.size(); }
1936 
1941  const Char *data() const FMT_NOEXCEPT{ return &buffer_[0]; }
1942 
1947  const Char *c_str() const {
1948  std::size_t size = buffer_.size();
1949  buffer_.reserve(size + 1);
1950  buffer_[size] = '\0';
1951  return &buffer_[0];
1952  }
1953 
1959  std::basic_string<Char> str() const {
1960  return std::basic_string<Char>(&buffer_[0], buffer_.size());
1961  }
1962 
1981  void write(BasicCStringRef<Char> format, ArgList args) {
1982  BasicFormatter<Char>(args, *this).format(format);
1983  }
1984  FMT_VARIADIC_VOID(write, BasicCStringRef<Char>)
1985 
1986  BasicWriter &operator<<(int value) {
1987  write_decimal(value);
1988  return *this;
1989  }
1990  BasicWriter &operator<<(unsigned value) {
1991  return *this << IntFormatSpec<unsigned>(value);
1992  }
1993  BasicWriter &operator<<(long value) {
1994  write_decimal(value);
1995  return *this;
1996  }
1997  BasicWriter &operator<<(unsigned long value) {
1998  return *this << IntFormatSpec<unsigned long>(value);
1999  }
2000  BasicWriter &operator<<(LongLong value) {
2001  write_decimal(value);
2002  return *this;
2003  }
2004 
2010  BasicWriter &operator<<(ULongLong value) {
2011  return *this << IntFormatSpec<ULongLong>(value);
2012  }
2013 
2014  BasicWriter &operator<<(double value) {
2015  write_double(value, FormatSpec());
2016  return *this;
2017  }
2018 
2025  BasicWriter &operator<<(long double value) {
2026  write_double(value, FormatSpec());
2027  return *this;
2028  }
2029 
2033  BasicWriter &operator<<(char value) {
2034  buffer_.push_back(value);
2035  return *this;
2036  }
2037 
2039  typename internal::WCharHelper<wchar_t, Char>::Supported value) {
2040  buffer_.push_back(value);
2041  return *this;
2042  }
2043 
2049  BasicWriter &operator<<(fmt::BasicStringRef<Char> value) {
2050  const Char *str = value.data();
2051  buffer_.append(str, str + value.size());
2052  return *this;
2053  }
2054 
2056  typename internal::WCharHelper<StringRef, Char>::Supported value) {
2057  const char *str = value.data();
2058  buffer_.append(str, str + value.size());
2059  return *this;
2060  }
2061 
2062  template <typename T, typename Spec, typename FillChar>
2063  BasicWriter &operator<<(IntFormatSpec<T, Spec, FillChar> spec) {
2064  internal::CharTraits<Char>::convert(FillChar());
2065  write_int(spec.value(), spec);
2066  return *this;
2067  }
2068 
2069  template <typename StrChar>
2070  BasicWriter &operator<<(const StrFormatSpec<StrChar> &spec) {
2071  const StrChar *s = spec.str();
2072  write_str(s, std::char_traits<Char>::length(s), spec);
2073  return *this;
2074  }
2075 
2076  void clear() FMT_NOEXCEPT{ buffer_.clear(); }
2077  };
2078 
2079  template <typename Char>
2080  template <typename StrChar>
2081  typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
2082  const StrChar *s, std::size_t size, const AlignSpec &spec) {
2083  CharPtr out = CharPtr();
2084  if (spec.width() > size) {
2085  out = grow_buffer(spec.width());
2086  Char fill = internal::CharTraits<Char>::cast(spec.fill());
2087  if (spec.align() == ALIGN_RIGHT) {
2088  std::fill_n(out, spec.width() - size, fill);
2089  out += spec.width() - size;
2090  }
2091  else if (spec.align() == ALIGN_CENTER) {
2092  out = fill_padding(out, spec.width(), size, fill);
2093  }
2094  else {
2095  std::fill_n(out + size, spec.width() - size, fill);
2096  }
2097  }
2098  else {
2099  out = grow_buffer(size);
2100  }
2101  std::copy(s, s + size, out);
2102  return out;
2103  }
2104 
2105  template <typename Char>
2106  typename BasicWriter<Char>::CharPtr
2108  CharPtr buffer, unsigned total_size,
2109  std::size_t content_size, wchar_t fill) {
2110  std::size_t padding = total_size - content_size;
2111  std::size_t left_padding = padding / 2;
2112  Char fill_char = internal::CharTraits<Char>::cast(fill);
2113  std::fill_n(buffer, left_padding, fill_char);
2114  buffer += left_padding;
2115  CharPtr content = buffer;
2116  std::fill_n(buffer + content_size, padding - left_padding, fill_char);
2117  return content;
2118  }
2119 
2120  template <typename Char>
2121  template <typename Spec>
2122  typename BasicWriter<Char>::CharPtr
2124  unsigned num_digits, const Spec &spec,
2125  const char *prefix, unsigned prefix_size) {
2126  unsigned width = spec.width();
2127  Alignment align = spec.align();
2128  Char fill = internal::CharTraits<Char>::cast(spec.fill());
2129  if (spec.precision() > static_cast<int>(num_digits)) {
2130  // Octal prefix '0' is counted as a digit, so ignore it if precision
2131  // is specified.
2132  if (prefix_size > 0 && prefix[prefix_size - 1] == '0')
2133  --prefix_size;
2134  unsigned number_size = prefix_size + spec.precision();
2135  AlignSpec subspec(number_size, '0', ALIGN_NUMERIC);
2136  if (number_size >= width)
2137  return prepare_int_buffer(num_digits, subspec, prefix, prefix_size);
2138  buffer_.reserve(width);
2139  unsigned fill_size = width - number_size;
2140  if (align != ALIGN_LEFT) {
2141  CharPtr p = grow_buffer(fill_size);
2142  std::fill(p, p + fill_size, fill);
2143  }
2144  CharPtr result = prepare_int_buffer(
2145  num_digits, subspec, prefix, prefix_size);
2146  if (align == ALIGN_LEFT) {
2147  CharPtr p = grow_buffer(fill_size);
2148  std::fill(p, p + fill_size, fill);
2149  }
2150  return result;
2151  }
2152  unsigned size = prefix_size + num_digits;
2153  if (width <= size) {
2154  CharPtr p = grow_buffer(size);
2155  std::copy(prefix, prefix + prefix_size, p);
2156  return p + size - 1;
2157  }
2158  CharPtr p = grow_buffer(width);
2159  CharPtr end = p + width;
2160  if (align == ALIGN_LEFT) {
2161  std::copy(prefix, prefix + prefix_size, p);
2162  p += size;
2163  std::fill(p, end, fill);
2164  }
2165  else if (align == ALIGN_CENTER) {
2166  p = fill_padding(p, width, size, fill);
2167  std::copy(prefix, prefix + prefix_size, p);
2168  p += size;
2169  }
2170  else {
2171  if (align == ALIGN_NUMERIC) {
2172  if (prefix_size != 0) {
2173  p = std::copy(prefix, prefix + prefix_size, p);
2174  size -= prefix_size;
2175  }
2176  }
2177  else {
2178  std::copy(prefix, prefix + prefix_size, end - size);
2179  }
2180  std::fill(p, end - size, fill);
2181  p = end;
2182  }
2183  return p - 1;
2184  }
2185 
2186  template <typename Char>
2187  template <typename T, typename Spec>
2188  void BasicWriter<Char>::write_int(T value, Spec spec) {
2189  unsigned prefix_size = 0;
2190  typedef typename internal::IntTraits<T>::MainType UnsignedType;
2191  UnsignedType abs_value = value;
2192  char prefix[4] = "";
2193  if (internal::is_negative(value)) {
2194  prefix[0] = '-';
2195  ++prefix_size;
2196  abs_value = 0 - abs_value;
2197  }
2198  else if (spec.flag(SIGN_FLAG)) {
2199  prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' ';
2200  ++prefix_size;
2201  }
2202  switch (spec.type()) {
2203  case 0: case 'd': {
2204  unsigned num_digits = internal::count_digits(abs_value);
2205  CharPtr p = prepare_int_buffer(
2206  num_digits, spec, prefix, prefix_size) + 1 - num_digits;
2207  internal::format_decimal(get(p), abs_value, num_digits);
2208  break;
2209  }
2210  case 'x': case 'X': {
2211  UnsignedType n = abs_value;
2212  if (spec.flag(HASH_FLAG)) {
2213  prefix[prefix_size++] = '0';
2214  prefix[prefix_size++] = spec.type();
2215  }
2216  unsigned num_digits = 0;
2217  do {
2218  ++num_digits;
2219  } while ((n >>= 4) != 0);
2220  Char *p = get(prepare_int_buffer(
2221  num_digits, spec, prefix, prefix_size));
2222  n = abs_value;
2223  const char *digits = spec.type() == 'x' ?
2224  "0123456789abcdef" : "0123456789ABCDEF";
2225  do {
2226  *p-- = digits[n & 0xf];
2227  } while ((n >>= 4) != 0);
2228  break;
2229  }
2230  case 'b': case 'B': {
2231  UnsignedType n = abs_value;
2232  if (spec.flag(HASH_FLAG)) {
2233  prefix[prefix_size++] = '0';
2234  prefix[prefix_size++] = spec.type();
2235  }
2236  unsigned num_digits = 0;
2237  do {
2238  ++num_digits;
2239  } while ((n >>= 1) != 0);
2240  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
2241  n = abs_value;
2242  do {
2243  *p-- = '0' + (n & 1);
2244  } while ((n >>= 1) != 0);
2245  break;
2246  }
2247  case 'o': {
2248  UnsignedType n = abs_value;
2249  if (spec.flag(HASH_FLAG))
2250  prefix[prefix_size++] = '0';
2251  unsigned num_digits = 0;
2252  do {
2253  ++num_digits;
2254  } while ((n >>= 3) != 0);
2255  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
2256  n = abs_value;
2257  do {
2258  *p-- = '0' + (n & 7);
2259  } while ((n >>= 3) != 0);
2260  break;
2261  }
2262  default:
2263  internal::report_unknown_type(
2264  spec.type(), spec.flag(CHAR_FLAG) ? "char" : "integer");
2265  break;
2266  }
2267  }
2268 
2269  template <typename Char>
2270  template <typename T>
2272  T value, const FormatSpec &spec) {
2273  // Check type.
2274  char type = spec.type();
2275  bool upper = false;
2276  switch (type) {
2277  case 0:
2278  type = 'g';
2279  break;
2280  case 'e': case 'f': case 'g': case 'a':
2281  break;
2282  case 'F':
2283 #ifdef _MSC_VER
2284  // MSVC's printf doesn't support 'F'.
2285  type = 'f';
2286 #endif
2287  // Fall through.
2288  case 'E': case 'G': case 'A':
2289  upper = true;
2290  break;
2291  default:
2292  internal::report_unknown_type(type, "double");
2293  break;
2294  }
2295 
2296  char sign = 0;
2297  // Use getsign instead of value < 0 because the latter is always
2298  // false for NaN.
2299  if (internal::getsign(static_cast<double>(value))) {
2300  sign = '-';
2301  value = -value;
2302  }
2303  else if (spec.flag(SIGN_FLAG)) {
2304  sign = spec.flag(PLUS_FLAG) ? '+' : ' ';
2305  }
2306 
2307  if (value != value) {
2308  // Format NaN ourselves because sprintf's output is not consistent
2309  // across platforms.
2310  std::size_t nan_size = 4;
2311  const char *nan = upper ? " NAN" : " nan";
2312  if (!sign) {
2313  --nan_size;
2314  ++nan;
2315  }
2316  CharPtr out = write_str(nan, nan_size, spec);
2317  if (sign)
2318  *out = sign;
2319  return;
2320  }
2321 
2322  if (internal::isinfinity(value)) {
2323  // Format infinity ourselves because sprintf's output is not consistent
2324  // across platforms.
2325  std::size_t inf_size = 4;
2326  const char *inf = upper ? " INF" : " inf";
2327  if (!sign) {
2328  --inf_size;
2329  ++inf;
2330  }
2331  CharPtr out = write_str(inf, inf_size, spec);
2332  if (sign)
2333  *out = sign;
2334  return;
2335  }
2336 
2337  std::size_t offset = buffer_.size();
2338  unsigned width = spec.width();
2339  if (sign) {
2340  buffer_.reserve(buffer_.size() + (std::max)(width, 1u));
2341  if (width > 0)
2342  --width;
2343  ++offset;
2344  }
2345 
2346  // Build format string.
2347  enum { MAX_FORMAT_SIZE = 10 }; // longest format: %#-*.*Lg
2348  Char format[MAX_FORMAT_SIZE];
2349  Char *format_ptr = format;
2350  *format_ptr++ = '%';
2351  unsigned width_for_sprintf = width;
2352  if (spec.flag(HASH_FLAG))
2353  *format_ptr++ = '#';
2354  if (spec.align() == ALIGN_CENTER) {
2355  width_for_sprintf = 0;
2356  }
2357  else {
2358  if (spec.align() == ALIGN_LEFT)
2359  *format_ptr++ = '-';
2360  if (width != 0)
2361  *format_ptr++ = '*';
2362  }
2363  if (spec.precision() >= 0) {
2364  *format_ptr++ = '.';
2365  *format_ptr++ = '*';
2366  }
2367 
2368  append_float_length(format_ptr, value);
2369  *format_ptr++ = type;
2370  *format_ptr = '\0';
2371 
2372  // Format using snprintf.
2373  Char fill = internal::CharTraits<Char>::cast(spec.fill());
2374  for (;;) {
2375  std::size_t buffer_size = buffer_.capacity() - offset;
2376 #if _MSC_VER
2377  // MSVC's vsnprintf_s doesn't work with zero size, so reserve
2378  // space for at least one extra character to make the size non-zero.
2379  // Note that the buffer's capacity will increase by more than 1.
2380  if (buffer_size == 0) {
2381  buffer_.reserve(offset + 1);
2382  buffer_size = buffer_.capacity() - offset;
2383  }
2384 #endif
2385  Char *start = &buffer_[offset];
2386  int n = internal::CharTraits<Char>::format_float(
2387  start, buffer_size, format, width_for_sprintf, spec.precision(), value);
2388  if (n >= 0 && offset + n < buffer_.capacity()) {
2389  if (sign) {
2390  if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) ||
2391  *start != ' ') {
2392  *(start - 1) = sign;
2393  sign = 0;
2394  }
2395  else {
2396  *(start - 1) = fill;
2397  }
2398  ++n;
2399  }
2400  if (spec.align() == ALIGN_CENTER &&
2401  spec.width() > static_cast<unsigned>(n)) {
2402  width = spec.width();
2403  CharPtr p = grow_buffer(width);
2404  std::copy(p, p + n, p + (width - n) / 2);
2405  fill_padding(p, spec.width(), n, fill);
2406  return;
2407  }
2408  if (spec.fill() != ' ' || sign) {
2409  while (*start == ' ')
2410  *start++ = fill;
2411  if (sign)
2412  *(start - 1) = sign;
2413  }
2414  grow_buffer(n);
2415  return;
2416  }
2417  // If n is negative we ask to increase the capacity by at least 1,
2418  // but as std::vector, the buffer grows exponentially.
2419  buffer_.reserve(n >= 0 ? offset + n + 1 : buffer_.capacity() + 1);
2420  }
2421  }
2422 
2449  template <typename Char, typename Allocator = std::allocator<Char> >
2450  class BasicMemoryWriter : public BasicWriter<Char> {
2451  private:
2452  internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE, Allocator> buffer_;
2453 
2454  public:
2455  explicit BasicMemoryWriter(const Allocator& alloc = Allocator())
2456  : BasicWriter<Char>(buffer_), buffer_(alloc) {}
2457 
2458 #if FMT_USE_RVALUE_REFERENCES
2459 
2466  : BasicWriter<Char>(buffer_), buffer_(std::move(other.buffer_)) {
2467  }
2468 
2474  BasicMemoryWriter &operator=(BasicMemoryWriter &&other) {
2475  buffer_ = std::move(other.buffer_);
2476  return *this;
2477  }
2478 #endif
2479  };
2480 
2483 
2502  template <typename Char>
2503  class BasicArrayWriter : public BasicWriter<Char> {
2504  private:
2505  internal::FixedBuffer<Char> buffer_;
2506 
2507  public:
2514  BasicArrayWriter(Char *array, std::size_t size)
2515  : BasicWriter<Char>(buffer_), buffer_(array, size) {}
2516 
2523  template <std::size_t SIZE>
2524  explicit BasicArrayWriter(Char(&array)[SIZE])
2525  : BasicWriter<Char>(buffer_), buffer_(array, SIZE) {}
2526  };
2527 
2530 
2531  // Formats a value.
2532  template <typename Char, typename T>
2533  void format(BasicFormatter<Char> &f, const Char *&format_str, const T &value) {
2534  std::basic_ostringstream<Char> os;
2535  os << value;
2536  std::basic_string<Char> str = os.str();
2537  internal::Arg arg = internal::MakeValue<Char>(str);
2538  arg.type = static_cast<internal::Arg::Type>(
2539  internal::MakeValue<Char>::type(str));
2540  format_str = f.format(format_str, arg);
2541  }
2542 
2543  // Reports a system error without throwing an exception.
2544  // Can be used to report errors from destructors.
2545  void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT;
2546 
2547 #if FMT_USE_WINDOWS_H
2548 
2550  class WindowsError : public SystemError {
2551  private:
2552  void init(int error_code, CStringRef format_str, ArgList args);
2553 
2554  public:
2579  WindowsError(int error_code, CStringRef message) {
2580  init(error_code, message, ArgList());
2581  }
2582  FMT_VARIADIC_CTOR(WindowsError, init, int, CStringRef)
2583  };
2584 
2585  // Reports a Windows error without throwing an exception.
2586  // Can be used to report errors from destructors.
2587  void report_windows_error(int error_code, StringRef message) FMT_NOEXCEPT;
2588 
2589 #endif
2590 
2591  enum Color { BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE };
2592 
2599  void print_colored(Color c, CStringRef format, ArgList args);
2600 
2608  inline std::string format(CStringRef format_str, ArgList args) {
2609  MemoryWriter w;
2610  w.write(format_str, args);
2611  return w.str();
2612  }
2613 
2614  inline std::wstring format(WCStringRef format_str, ArgList args) {
2615  WMemoryWriter w;
2616  w.write(format_str, args);
2617  return w.str();
2618  }
2619 
2627  void print(std::FILE *f, CStringRef format_str, ArgList args);
2628 
2636  void print(CStringRef format_str, ArgList args);
2637 
2645  void print(std::ostream &os, CStringRef format_str, ArgList args);
2646 
2647  template <typename Char>
2648  void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args) {
2649  internal::PrintfFormatter<Char>(args).format(w, format);
2650  }
2651 
2659  inline std::string sprintf(CStringRef format, ArgList args) {
2660  MemoryWriter w;
2661  printf(w, format, args);
2662  return w.str();
2663  }
2664 
2672  int fprintf(std::FILE *f, CStringRef format, ArgList args);
2673 
2681  inline int printf(CStringRef format, ArgList args) {
2682  return fprintf(stdout, format, args);
2683  }
2684 
2688  class FormatInt {
2689  private:
2690  // Buffer should be large enough to hold all digits (digits10 + 1),
2691  // a sign and a null character.
2692  enum { BUFFER_SIZE = std::numeric_limits<ULongLong>::digits10 + 3 };
2693  mutable char buffer_[BUFFER_SIZE];
2694  char *str_;
2695 
2696  // Formats value in reverse and returns the number of digits.
2697  char *format_decimal(ULongLong value) {
2698  char *buffer_end = buffer_ + BUFFER_SIZE - 1;
2699  while (value >= 100) {
2700  // Integer division is slow so do it for a group of two digits instead
2701  // of for every digit. The idea comes from the talk by Alexandrescu
2702  // "Three Optimization Tips for C++". See speed-test for a comparison.
2703  unsigned index = (value % 100) * 2;
2704  value /= 100;
2705  *--buffer_end = internal::Data::DIGITS[index + 1];
2706  *--buffer_end = internal::Data::DIGITS[index];
2707  }
2708  if (value < 10) {
2709  *--buffer_end = static_cast<char>('0' + value);
2710  return buffer_end;
2711  }
2712  unsigned index = static_cast<unsigned>(value * 2);
2713  *--buffer_end = internal::Data::DIGITS[index + 1];
2714  *--buffer_end = internal::Data::DIGITS[index];
2715  return buffer_end;
2716  }
2717 
2718  void FormatSigned(LongLong value) {
2719  ULongLong abs_value = static_cast<ULongLong>(value);
2720  bool negative = value < 0;
2721  if (negative)
2722  abs_value = 0 - abs_value;
2723  str_ = format_decimal(abs_value);
2724  if (negative)
2725  *--str_ = '-';
2726  }
2727 
2728  public:
2729  explicit FormatInt(int value) { FormatSigned(value); }
2730  explicit FormatInt(long value) { FormatSigned(value); }
2731  explicit FormatInt(LongLong value) { FormatSigned(value); }
2732  explicit FormatInt(unsigned value) : str_(format_decimal(value)) {}
2733  explicit FormatInt(unsigned long value) : str_(format_decimal(value)) {}
2734  explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {}
2735 
2739  std::size_t size() const { return buffer_ - str_ + BUFFER_SIZE - 1; }
2740 
2745  const char *data() const { return str_; }
2746 
2751  const char *c_str() const {
2752  buffer_[BUFFER_SIZE - 1] = '\0';
2753  return str_;
2754  }
2755 
2761  std::string str() const { return std::string(str_, size()); }
2762  };
2763 
2764  // Formats a decimal integer value writing into buffer and returns
2765  // a pointer to the end of the formatted string. This function doesn't
2766  // write a terminating null character.
2767  template <typename T>
2768  inline void format_decimal(char *&buffer, T value) {
2769  typename internal::IntTraits<T>::MainType abs_value = value;
2770  if (internal::is_negative(value)) {
2771  *buffer++ = '-';
2772  abs_value = 0 - abs_value;
2773  }
2774  if (abs_value < 100) {
2775  if (abs_value < 10) {
2776  *buffer++ = static_cast<char>('0' + abs_value);
2777  return;
2778  }
2779  unsigned index = static_cast<unsigned>(abs_value * 2);
2780  *buffer++ = internal::Data::DIGITS[index];
2781  *buffer++ = internal::Data::DIGITS[index + 1];
2782  return;
2783  }
2784  unsigned num_digits = internal::count_digits(abs_value);
2785  internal::format_decimal(buffer, abs_value, num_digits);
2786  buffer += num_digits;
2787  }
2788 
2796  template <typename T>
2797  inline internal::NamedArg<char> arg(StringRef name, const T &arg) {
2798  return internal::NamedArg<char>(name, arg);
2799  }
2800 
2801  template <typename T>
2802  inline internal::NamedArg<wchar_t> arg(WStringRef name, const T &arg) {
2803  return internal::NamedArg<wchar_t>(name, arg);
2804  }
2805 
2806  // The following two functions are deleted intentionally to disable
2807  // nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``.
2808  template <typename Char>
2809  void arg(StringRef, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
2810  template <typename Char>
2811  void arg(WStringRef, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
2812 }
2813 
2814 #if FMT_GCC_VERSION
2815 // Use the system_header pragma to suppress warnings about variadic macros
2816 // because suppressing -Wvariadic-macros with the diagnostic pragma doesn't
2817 // work. It is used at the end because we want to suppress as little warnings
2818 // as possible.
2819 # pragma GCC system_header
2820 #endif
2821 
2822 // This is used to work around VC++ bugs in handling variadic macros.
2823 #define FMT_EXPAND(args) args
2824 
2825 // Returns the number of arguments.
2826 // Based on https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s.
2827 #define FMT_NARG(...) FMT_NARG_(__VA_ARGS__, FMT_RSEQ_N())
2828 #define FMT_NARG_(...) FMT_EXPAND(FMT_ARG_N(__VA_ARGS__))
2829 #define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
2830 #define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
2831 
2832 #define FMT_CONCAT(a, b) a##b
2833 #define FMT_FOR_EACH_(N, f, ...) \
2834  FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__))
2835 #define FMT_FOR_EACH(f, ...) \
2836  FMT_EXPAND(FMT_FOR_EACH_(FMT_NARG(__VA_ARGS__), f, __VA_ARGS__))
2837 
2838 #define FMT_ADD_ARG_NAME(type, index) type arg##index
2839 #define FMT_GET_ARG_NAME(type, index) arg##index
2840 
2841 #if FMT_USE_VARIADIC_TEMPLATES
2842 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
2843  template <typename... Args> \
2844  ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
2845  const Args & ... args) { \
2846  typename fmt::internal::ArgArray<sizeof...(Args)>::Type array; \
2847  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), \
2848  fmt::internal::make_arg_list<Char>(array, args...)); \
2849  }
2850 #else
2851 // Defines a wrapper for a function taking __VA_ARGS__ arguments
2852 // and n additional arguments of arbitrary types.
2853 # define FMT_WRAP(Char, ReturnType, func, call, n, ...) \
2854  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2855  inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
2856  FMT_GEN(n, FMT_MAKE_ARG)) { \
2857  fmt::internal::ArgArray<n>::Type arr = {FMT_GEN(n, FMT_MAKE_REF_##Char)}; \
2858  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList( \
2859  fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), arr)); \
2860  }
2861 
2862 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
2863  inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) { \
2864  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList()); \
2865  } \
2866  FMT_WRAP(Char, ReturnType, func, call, 1, __VA_ARGS__) \
2867  FMT_WRAP(Char, ReturnType, func, call, 2, __VA_ARGS__) \
2868  FMT_WRAP(Char, ReturnType, func, call, 3, __VA_ARGS__) \
2869  FMT_WRAP(Char, ReturnType, func, call, 4, __VA_ARGS__) \
2870  FMT_WRAP(Char, ReturnType, func, call, 5, __VA_ARGS__) \
2871  FMT_WRAP(Char, ReturnType, func, call, 6, __VA_ARGS__) \
2872  FMT_WRAP(Char, ReturnType, func, call, 7, __VA_ARGS__) \
2873  FMT_WRAP(Char, ReturnType, func, call, 8, __VA_ARGS__) \
2874  FMT_WRAP(Char, ReturnType, func, call, 9, __VA_ARGS__) \
2875  FMT_WRAP(Char, ReturnType, func, call, 10, __VA_ARGS__) \
2876  FMT_WRAP(Char, ReturnType, func, call, 11, __VA_ARGS__) \
2877  FMT_WRAP(Char, ReturnType, func, call, 12, __VA_ARGS__) \
2878  FMT_WRAP(Char, ReturnType, func, call, 13, __VA_ARGS__) \
2879  FMT_WRAP(Char, ReturnType, func, call, 14, __VA_ARGS__) \
2880  FMT_WRAP(Char, ReturnType, func, call, 15, __VA_ARGS__)
2881 #endif // FMT_USE_VARIADIC_TEMPLATES
2882 
2906 #define FMT_VARIADIC(ReturnType, func, ...) \
2907  FMT_VARIADIC_(char, ReturnType, func, return func, __VA_ARGS__)
2908 
2909 #define FMT_VARIADIC_W(ReturnType, func, ...) \
2910  FMT_VARIADIC_(wchar_t, ReturnType, func, return func, __VA_ARGS__)
2911 
2912 #define FMT_CAPTURE_ARG_(id, index) ::fmt::arg(#id, id)
2913 
2914 #define FMT_CAPTURE_ARG_W_(id, index) ::fmt::arg(L###id, id)
2915 
2927 #define FMT_CAPTURE(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_, __VA_ARGS__)
2928 
2929 #define FMT_CAPTURE_W(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_W_, __VA_ARGS__)
2930 
2931 namespace fmt {
2932  FMT_VARIADIC(std::string, format, CStringRef)
2933  FMT_VARIADIC_W(std::wstring, format, WCStringRef)
2934  FMT_VARIADIC(void, print, CStringRef)
2935  FMT_VARIADIC(void, print, std::FILE *, CStringRef)
2936  FMT_VARIADIC(void, print, std::ostream &, CStringRef)
2937  FMT_VARIADIC(void, print_colored, Color, CStringRef)
2938  FMT_VARIADIC(std::string, sprintf, CStringRef)
2939  FMT_VARIADIC(int, printf, CStringRef)
2940  FMT_VARIADIC(int, fprintf, std::FILE *, CStringRef)
2941 }
2942 
2943 // Restore warnings.
2944 #if FMT_GCC_VERSION >= 406
2945 # pragma GCC diagnostic pop
2946 #endif
2947 
2948 #ifdef __clang__
2949 # pragma clang diagnostic pop
2950 #endif
2951 
2952 #ifdef FMT_HEADER_ONLY
2953 # include "format.cc"
2954 #endif
2955 
2956 #endif // FMT_FORMAT_H_
BasicCStringRef(const Char *s)
Definition: format.h:307
double max(const series< double > &series, size_t period)
Returns the highest value found in series over given .
Definition: TSAFunctionsScalar.cpp:180
BasicStringRef(const Char *s)
Definition: format.h:239
const Char * data() const
Definition: format.h:260
void resize(std::size_t new_size)
Definition: format.h:387
BasicWriter(Buffer< Char > &b)
Definition: format.h:1922
Definition: format.h:2688
const char * c_str() const
Definition: format.h:2751
void write(BasicCStringRef< Char > format, ArgList args)
Definition: format.h:1981
std::size_t capacity() const
Definition: format.h:382
BasicArrayWriter(Char *array, std::size_t size)
Definition: format.h:2514
Definition: format.h:326
const Char * c_str() const
Definition: format.h:317
Definition: TSASQLite.h:39
Definition: format.h:2503
std::basic_string< Char > str() const
Definition: format.h:1959
BasicWriter & operator<<(ULongLong value)
Definition: format.h:2010
BasicStringRef(const Char *s, std::size_t size)
Definition: format.h:231
const Char * data() const FMT_NOEXCEPT
Definition: format.h:1941
virtual ~BasicWriter()
Definition: format.h:1930
BasicCStringRef(const std::basic_string< Char > &s)
Definition: format.h:314
void append(const U *begin, const U *end)
Definition: format.h:421
Definition: format.h:355
std::size_t size() const
Definition: format.h:2739
std::basic_string< Char > to_string() const
Definition: format.h:255
Definition: format.h:2450
std::ostream & operator<<(std::ostream &s, const transaction &t)
Writes human readable transaction information to stream.
Definition: TSAOrder.cpp:779
Definition: format.h:1174
std::size_t size() const
Definition: format.h:379
std::size_t size() const
Definition: format.h:1935
SystemError(int error_code, CStringRef message)
Definition: format.h:1791
BasicWriter & operator<<(char value)
Definition: format.h:2033
const char * data() const
Definition: format.h:2745
internal::Arg operator[](unsigned index) const
Definition: format.h:1211
sref< bool > operator==(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:130
std::size_t size() const
Definition: format.h:263
BasicWriter & operator<<(long double value)
Definition: format.h:2025
BasicArrayWriter(Char(&array)[SIZE])
Definition: format.h:2524
Definition: format.h:192
BasicStringRef(const std::basic_string< Char > &s)
Definition: format.h:247
Definition: format.h:224
void reserve(std::size_t capacity)
Definition: format.h:398
Definition: format.cpp:101
std::string str() const
Definition: format.h:2761
Definition: format.h:1757
const Char * c_str() const
Definition: format.h:1947
Definition: format.h:301
sref< bool > operator<(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:78