Pyrogenesis  trunk
format.h
Go to the documentation of this file.
1 /*
2  * Slightly modified version of cppformat, by Wildfire Games, for 0 A.D.
3  * Based on cppformat v0.11.0 from https://github.com/cppformat/cppformat
4  */
5 
6 /*
7  Formatting library for C++
8 
9  Copyright (c) 2012 - 2014, Victor Zverovich
10  All rights reserved.
11 
12  Redistribution and use in source and binary forms, with or without
13  modification, are permitted provided that the following conditions are met:
14 
15  1. Redistributions of source code must retain the above copyright notice, this
16  list of conditions and the following disclaimer.
17  2. Redistributions in binary form must reproduce the above copyright notice,
18  this list of conditions and the following disclaimer in the documentation
19  and/or other materials provided with the distribution.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef FMT_FORMAT_H_
34 #define FMT_FORMAT_H_
35 
36 #include <stdint.h>
37 
38 #include <cassert>
39 #include <cstddef> // for std::ptrdiff_t
40 #include <cstdio>
41 #include <algorithm>
42 #include <limits>
43 #include <stdexcept>
44 #include <string>
45 #include <sstream>
46 
47 #if defined(_SECURE_SCL) && _SECURE_SCL
48 # include <iterator>
49 #endif
50 
51 #ifdef __GNUC__
52 # define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
53 # define FMT_GCC_EXTENSION __extension__
54 // Disable warning about "long long" which is sometimes reported even
55 // when using __extension__.
56 # if FMT_GCC_VERSION >= 406
57 # pragma GCC diagnostic push
58 # pragma GCC diagnostic ignored "-Wlong-long"
59 # endif
60 #else
61 # define FMT_GCC_EXTENSION
62 #endif
63 
64 #ifdef __GNUC_LIBSTD__
65 # define FMT_GNUC_LIBSTD_VERSION (__GNUC_LIBSTD__ * 100 + __GNUC_LIBSTD_MINOR__)
66 #endif
67 
68 #ifdef __has_feature
69 # define FMT_HAS_FEATURE(x) __has_feature(x)
70 #else
71 # define FMT_HAS_FEATURE(x) 0
72 #endif
73 
74 #ifdef __has_builtin
75 # define FMT_HAS_BUILTIN(x) __has_builtin(x)
76 #else
77 # define FMT_HAS_BUILTIN(x) 0
78 #endif
79 
80 #ifndef FMT_USE_VARIADIC_TEMPLATES
81 // Variadic templates are available in GCC since version 4.4
82 // (http://gcc.gnu.org/projects/cxx0x.html) and in Visual C++
83 // since version 2013.
84 # define FMT_USE_VARIADIC_TEMPLATES \
85  (FMT_HAS_FEATURE(cxx_variadic_templates) || \
86  (FMT_GCC_VERSION >= 404 && __cplusplus >= 201103) || (defined(_MSC_VER) && _MSC_VER >= 1800))
87 #endif
88 
89 #ifndef FMT_USE_RVALUE_REFERENCES
90 // Don't use rvalue references when compiling with clang and an old libstdc++
91 // as the latter doesn't provide std::move.
92 # if defined(FMT_GNUC_LIBSTD_VERSION) && FMT_GNUC_LIBSTD_VERSION <= 402
93 # define FMT_USE_RVALUE_REFERENCES 0
94 # else
95 # define FMT_USE_RVALUE_REFERENCES \
96  (FMT_HAS_FEATURE(cxx_rvalue_references) || \
97  (FMT_GCC_VERSION >= 403 && __cplusplus >= 201103) || (defined(_MSC_VER) && _MSC_VER >= 1600))
98 # endif
99 #endif
100 
101 #if FMT_USE_RVALUE_REFERENCES
102 # include <utility> // for std::move
103 #endif
104 
105 // Define FMT_USE_NOEXCEPT to make C++ Format use noexcept (C++11 feature).
106 #if (defined(FMT_USE_NOEXCEPT) && FMT_USE_NOEXCEPT) || FMT_HAS_FEATURE(cxx_noexcept) || \
107  (FMT_GCC_VERSION >= 408 && __cplusplus >= 201103)
108 # define FMT_NOEXCEPT(expr) noexcept(expr)
109 #else
110 # define FMT_NOEXCEPT(expr)
111 #endif
112 
113 // A macro to disallow the copy constructor and operator= functions
114 // This should be used in the private: declarations for a class
115 #define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
116  TypeName(const TypeName&); \
117  void operator=(const TypeName&)
118 
119 namespace fmt {
120 
121 // Fix the warning about long long on older versions of GCC
122 // that don't support the diagnostic pragma.
123 FMT_GCC_EXTENSION typedef long long LongLong;
124 FMT_GCC_EXTENSION typedef unsigned long long ULongLong;
125 
126 #if FMT_USE_RVALUE_REFERENCES
127 using std::move;
128 #endif
129 
130 template <typename Char>
132 
133 typedef BasicWriter<char> Writer;
135 
136 template <typename Char>
138 
139 template <typename Char, typename T>
140 void format(BasicFormatter<Char> &f, const Char *format_str, const T &value);
141 
142 /**
143  \rst
144  A string reference. It can be constructed from a C string or
145  ``std::string``.
146 
147  You can use one of the following typedefs for common character types:
148 
149  +------------+-------------------------+
150  | Type | Definition |
151  +============+=========================+
152  | StringRef | BasicStringRef<char> |
153  +------------+-------------------------+
154  | WStringRef | BasicStringRef<wchar_t> |
155  +------------+-------------------------+
156 
157  This class is most useful as a parameter type to allow passing
158  different types of strings to a function, for example::
159 
160  template<typename... Args>
161  std::string format(StringRef format, const Args & ... args);
162 
163  format("{}", 42);
164  format(std::string("{}"), 42);
165  \endrst
166  */
167 template <typename Char>
169  private:
170  const Char *data_;
171  mutable std::size_t size_;
172 
173  public:
174  /**
175  Constructs a string reference object from a C string and a size.
176  If *size* is zero, which is the default, the size is computed
177  automatically.
178  */
179  BasicStringRef(const Char *s, std::size_t size = 0) : data_(s), size_(size) {}
180 
181  /**
182  Constructs a string reference from an `std::string` object.
183  */
184  BasicStringRef(const std::basic_string<Char> &s)
185  : data_(s.c_str()), size_(s.size()) {}
186 
187  /**
188  Converts a string reference to an `std::string` object.
189  */
190  operator std::basic_string<Char>() const {
191  return std::basic_string<Char>(data_, size());
192  }
193 
194  /**
195  Returns the pointer to a C string.
196  */
197  const Char *c_str() const { return data_; }
198 
199  /**
200  Returns the string size.
201  */
202  std::size_t size() const {
203  if (size_ == 0 && data_) size_ = std::char_traits<Char>::length(data_);
204  return size_;
205  }
206 
207  friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) {
208  return lhs.data_ == rhs.data_;
209  }
210  friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs) {
211  return lhs.data_ != rhs.data_;
212  }
213 };
214 
217 
218 /**
219  A formatting error such as invalid format string.
220 */
221 class FormatError : public std::runtime_error {
222 public:
223  explicit FormatError(const std::string &message)
224  : std::runtime_error(message) {}
225 };
226 
227 namespace internal {
228 
229 // The number of characters to store in the Array object, representing the
230 // output buffer, itself to avoid dynamic memory allocation.
231 enum { INLINE_BUFFER_SIZE = 500 };
232 
233 #if defined(_SECURE_SCL) && _SECURE_SCL
234 // Use checked iterator to avoid warnings on MSVC.
235 template <typename T>
236 inline stdext::checked_array_iterator<T*> make_ptr(T *ptr, std::size_t size) {
237  return stdext::checked_array_iterator<T*>(ptr, size);
238 }
239 #else
240 template <typename T>
241 inline T *make_ptr(T *ptr, std::size_t) { return ptr; }
242 #endif
243 
244 // A simple array for POD types with the first SIZE elements stored in
245 // the object itself. It supports a subset of std::vector's operations.
246 template <typename T, std::size_t SIZE>
247 class Array {
248  private:
249  std::size_t size_;
250  std::size_t capacity_;
251  T *ptr_;
252  T data_[SIZE];
253 
254  void grow(std::size_t size);
255 
256  // Free memory allocated by the array.
257  void free() {
258  if (ptr_ != data_) delete [] ptr_;
259  }
260 
261  // Move data from other to this array.
262  void move(Array &other) {
263  size_ = other.size_;
264  capacity_ = other.capacity_;
265  if (other.ptr_ == other.data_) {
266  ptr_ = data_;
267  std::copy(other.data_, other.data_ + size_, make_ptr(data_, capacity_));
268  } else {
269  ptr_ = other.ptr_;
270  // Set pointer to the inline array so that delete is not called
271  // when freeing.
272  other.ptr_ = other.data_;
273  }
274  }
275 
277 
278  public:
279  explicit Array(std::size_t size = 0)
280  : size_(size), capacity_(SIZE), ptr_(data_) {}
281  ~Array() { free(); }
282 
283 #if FMT_USE_RVALUE_REFERENCES
284  Array(Array &&other) {
285  move(other);
286  }
287 
288  Array& operator=(Array &&other) {
289  assert(this != &other);
290  free();
291  move(other);
292  return *this;
293  }
294 #endif
295 
296  // Returns the size of this array.
297  std::size_t size() const { return size_; }
298 
299  // Returns the capacity of this array.
300  std::size_t capacity() const { return capacity_; }
301 
302  // Resizes the array. If T is a POD type new elements are not initialized.
303  void resize(std::size_t new_size) {
304  if (new_size > capacity_)
305  grow(new_size);
306  size_ = new_size;
307  }
308 
309  // Reserves space to store at least capacity elements.
310  void reserve(std::size_t capacity) {
311  if (capacity > capacity_)
312  grow(capacity);
313  }
314 
315  void clear() { size_ = 0; }
316 
317  void push_back(const T &value) {
318  if (size_ == capacity_)
319  grow(size_ + 1);
320  ptr_[size_++] = value;
321  }
322 
323  // Appends data to the end of the array.
324  void append(const T *begin, const T *end);
325 
326  T &operator[](std::size_t index) { return ptr_[index]; }
327  const T &operator[](std::size_t index) const { return ptr_[index]; }
328 };
329 
330 template <typename T, std::size_t SIZE>
331 void Array<T, SIZE>::grow(std::size_t size) {
332  capacity_ = (std::max)(size, capacity_ + capacity_ / 2);
333  T *p = new T[capacity_];
334  std::copy(ptr_, ptr_ + size_, make_ptr(p, capacity_));
335  if (ptr_ != data_)
336  delete [] ptr_;
337  ptr_ = p;
338 }
339 
340 template <typename T, std::size_t SIZE>
341 void Array<T, SIZE>::append(const T *begin, const T *end) {
342  std::ptrdiff_t num_elements = end - begin;
343  if (size_ + num_elements > capacity_)
344  grow(size_ + num_elements);
345  std::copy(begin, end, make_ptr(ptr_, capacity_) + size_);
346  size_ += num_elements;
347 }
348 
349 template <typename Char>
351  public:
352 #if defined(_SECURE_SCL) && _SECURE_SCL
353  typedef stdext::checked_array_iterator<Char*> CharPtr;
354 #else
355  typedef Char *CharPtr;
356 #endif
357 };
358 
359 template <typename Char>
361 
362 template <>
363 class CharTraits<char> : public BasicCharTraits<char> {
364  private:
365  // Conversion from wchar_t to char is not allowed.
366  static char convert(wchar_t);
367 
368 public:
369  typedef const wchar_t *UnsupportedStrType;
370 
371  static char convert(char value) { return value; }
372 
373  // Formats a floating-point number.
374  template <typename T>
375  static int format_float(char *buffer, std::size_t size,
376  const char *format, unsigned width, int precision, T value);
377 };
378 
379 template <>
380 class CharTraits<wchar_t> : public BasicCharTraits<wchar_t> {
381  public:
382  typedef const char *UnsupportedStrType;
383 
384  static wchar_t convert(char value) { return value; }
385  static wchar_t convert(wchar_t value) { return value; }
386 
387  template <typename T>
388  static int format_float(wchar_t *buffer, std::size_t size,
389  const wchar_t *format, unsigned width, int precision, T value);
390 };
391 
392 // Selects uint32_t if FitsIn32Bits is true, uint64_t otherwise.
393 template <bool FitsIn32Bits>
394 struct TypeSelector { typedef uint32_t Type; };
395 
396 template <>
397 struct TypeSelector<false> { typedef uint64_t Type; };
398 
399 // Checks if a number is negative - used to avoid warnings.
400 template <bool IsSigned>
401 struct SignChecker {
402  template <typename T>
403  static bool is_negative(T) { return false; }
404 };
405 
406 template <>
407 struct SignChecker<true> {
408  template <typename T>
409  static bool is_negative(T value) { return value < 0; }
410 };
411 
412 // Returns true if value is negative, false otherwise.
413 // Same as (value < 0) but doesn't produce warnings if T is an unsigned type.
414 template <typename T>
415 inline bool is_negative(T value) {
417 }
418 
419 template <typename T>
420 struct IntTraits {
421  // Smallest of uint32_t and uint64_t that is large enough to represent
422  // all values of T.
423  typedef typename
424  TypeSelector<std::numeric_limits<T>::digits <= 32>::Type MainType;
425 };
426 
427 // MakeUnsigned<T>::Type gives an unsigned type corresponding to integer type T.
428 template <typename T>
429 struct MakeUnsigned { typedef T Type; };
430 
431 #define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U) \
432  template <> \
433  struct MakeUnsigned<T> { typedef U Type; }
434 
435 FMT_SPECIALIZE_MAKE_UNSIGNED(char, unsigned char);
436 FMT_SPECIALIZE_MAKE_UNSIGNED(signed char, unsigned char);
437 FMT_SPECIALIZE_MAKE_UNSIGNED(short, unsigned short);
438 FMT_SPECIALIZE_MAKE_UNSIGNED(int, unsigned);
439 FMT_SPECIALIZE_MAKE_UNSIGNED(long, unsigned long);
440 FMT_SPECIALIZE_MAKE_UNSIGNED(LongLong, ULongLong);
441 
442 void report_unknown_type(char code, const char *type);
443 
444 extern const uint32_t POWERS_OF_10_32[];
445 extern const uint64_t POWERS_OF_10_64[];
446 
447 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clzll)
448 // Returns the number of decimal digits in n. Leading zeros are not counted
449 // except for n == 0 in which case count_digits returns 1.
450 inline unsigned count_digits(uint64_t n) {
451  // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
452  // and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits.
453  unsigned t = (64 - __builtin_clzll(n | 1)) * 1233 >> 12;
454  return t - (n < POWERS_OF_10_64[t]) + 1;
455 }
456 # if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz)
457 // Optional version of count_digits for better performance on 32-bit platforms.
458 inline unsigned count_digits(uint32_t n) {
459  uint32_t t = (32 - __builtin_clz(n | 1)) * 1233 >> 12;
460  return t - (n < POWERS_OF_10_32[t]) + 1;
461 }
462 # endif
463 #else
464 // Slower version of count_digits used when __builtin_clz is not available.
465 inline unsigned count_digits(uint64_t n) {
466  unsigned count = 1;
467  for (;;) {
468  // Integer division is slow so do it for a group of four digits instead
469  // of for every digit. The idea comes from the talk by Alexandrescu
470  // "Three Optimization Tips for C++". See speed-test for a comparison.
471  if (n < 10) return count;
472  if (n < 100) return count + 1;
473  if (n < 1000) return count + 2;
474  if (n < 10000) return count + 3;
475  n /= 10000u;
476  count += 4;
477  }
478 }
479 #endif
480 
481 extern const char DIGITS[];
482 
483 // Formats a decimal unsigned integer value writing into buffer.
484 template <typename UInt, typename Char>
485 inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) {
486  --num_digits;
487  while (value >= 100) {
488  // Integer division is slow so do it for a group of two digits instead
489  // of for every digit. The idea comes from the talk by Alexandrescu
490  // "Three Optimization Tips for C++". See speed-test for a comparison.
491  unsigned index = (value % 100) * 2;
492  value /= 100;
493  buffer[num_digits] = DIGITS[index + 1];
494  buffer[num_digits - 1] = DIGITS[index];
495  num_digits -= 2;
496  }
497  if (value < 10) {
498  *buffer = static_cast<char>('0' + value);
499  return;
500  }
501  unsigned index = static_cast<unsigned>(value * 2);
502  buffer[1] = DIGITS[index + 1];
503  buffer[0] = DIGITS[index];
504 }
505 
506 #ifdef _WIN32
507 // A converter from UTF-8 to UTF-16.
508 // It is only provided for Windows since other systems use UTF-8.
509 class UTF8ToUTF16 {
510  private:
512 
513  public:
514  explicit UTF8ToUTF16(StringRef s);
515  operator WStringRef() const { return WStringRef(&buffer_[0], size()); }
516  size_t size() const { return buffer_.size() - 1; }
517  const wchar_t *c_str() const { return &buffer_[0]; }
518  std::wstring str() const { return std::wstring(&buffer_[0], size()); }
519 };
520 
521 // A converter from UTF-16 to UTF-8.
522 // It is only provided for Windows since other systems use UTF-8.
523 class UTF16ToUTF8 {
524  private:
526 
527  public:
528  UTF16ToUTF8() {}
529  explicit UTF16ToUTF8(WStringRef s);
530  operator StringRef() const { return StringRef(&buffer_[0], size()); }
531  size_t size() const { return buffer_.size() - 1; }
532  const char *c_str() const { return &buffer_[0]; }
533  std::string str() const { return std::string(&buffer_[0], size()); }
534 
535  // Performs conversion returning a system error code instead of
536  // throwing exception on error.
537  int convert(WStringRef s);
538 };
539 #endif
540 
541 // Portable thread-safe version of strerror.
542 // Sets buffer to point to a string describing the error code.
543 // This can be either a pointer to a string stored in buffer,
544 // or a pointer to some static immutable string.
545 // Returns one of the following values:
546 // 0 - success
547 // ERANGE - buffer is not large enough to store the error message
548 // other - failure
549 // Buffer should be at least of size 1.
550 int safe_strerror(int error_code,
551  char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT(true);
552 
554  fmt::Writer &out, int error_code, fmt::StringRef message);
555 
556 #ifdef _WIN32
557 void format_windows_error(
558  fmt::Writer &out, int error_code, fmt::StringRef message);
559 #endif
560 
561 // Throws Exception(message) if format contains '}', otherwise throws
562 // FormatError reporting unmatched '{'. The idea is that unmatched '{'
563 // should override other errors.
564 template <typename Char>
567  void operator()(const Char *s, fmt::StringRef message) const;
568 };
569 
570 // Computes max(Arg, 1) at compile time. It is used to avoid errors about
571 // allocating an array of 0 size.
572 template <unsigned Arg>
573 struct NonZero {
574  enum { VALUE = Arg };
575 };
576 
577 template <>
578 struct NonZero<0> {
579  enum { VALUE = 1 };
580 };
581 
582 // A formatting argument. It is a POD type to allow storage in internal::Array.
583 struct Arg {
584  enum Type {
585  // Integer types should go first,
586  INT, UINT, LONG_LONG, ULONG_LONG, CHAR, LAST_INTEGER_TYPE = CHAR,
587  // followed by floating-point types.
588  DOUBLE, LONG_DOUBLE, LAST_NUMERIC_TYPE = LONG_DOUBLE,
589  STRING, WSTRING, POINTER, CUSTOM
590  };
592 
593  template <typename Char>
594  struct StringValue {
595  const Char *value;
596  std::size_t size;
597  };
598 
599  typedef void (*FormatFunc)(
600  void *formatter, const void *arg, const void *format_str);
601 
602  struct CustomValue {
603  const void *value;
605  };
606 
607  union {
609  unsigned uint_value;
610  LongLong long_long_value;
611  ULongLong ulong_long_value;
612  double double_value;
613  long double long_double_value;
614  const void *pointer_value;
618  };
619 };
620 
621 // Makes an Arg object from any type.
622 template <typename Char>
623 class MakeArg : public Arg {
624  private:
625  // The following two methods are private to disallow formatting of
626  // arbitrary pointers. If you want to output a pointer cast it to
627  // "void *" or "const void *". In particular, this forbids formatting
628  // of "[const] volatile char *" which is printed as bool by iostreams.
629  // Do not implement!
630  template <typename T>
631  MakeArg(const T *value);
632  template <typename T>
633  MakeArg(T *value);
634 
635  void set_string(StringRef str) {
636  type = STRING;
637  string.value = str.c_str();
638  string.size = str.size();
639  }
640 
641  void set_string(WStringRef str) {
642  type = WSTRING;
643  CharTraits<Char>::convert(wchar_t());
644  wstring.value = str.c_str();
645  wstring.size = str.size();
646  }
647 
648  // Formats an argument of a custom type, such as a user-defined class.
649  template <typename T>
650  static void format_custom_arg(
651  void *formatter, const void *arg, const void *format_str) {
652  format(*static_cast<BasicFormatter<Char>*>(formatter),
653  static_cast<const Char*>(format_str), *static_cast<const T*>(arg));
654  }
655 
656 public:
657  MakeArg() {}
658  MakeArg(bool value) { type = INT; int_value = value; }
659  MakeArg(short value) { type = INT; int_value = value; }
660  MakeArg(unsigned short value) { type = UINT; uint_value = value; }
661  MakeArg(int value) { type = INT; int_value = value; }
662  MakeArg(unsigned value) { type = UINT; uint_value = value; }
663  MakeArg(long value) {
664  // To minimize the number of types we need to deal with, long is
665  // translated either to int or to long long depending on its size.
666  if (sizeof(long) == sizeof(int)) {
667  type = INT;
668  int_value = static_cast<int>(value);
669  } else {
670  type = LONG_LONG;
671  long_long_value = value;
672  }
673  }
674  MakeArg(unsigned long value) {
675  if (sizeof(unsigned long) == sizeof(unsigned)) {
676  type = UINT;
677  uint_value = static_cast<unsigned>(value);
678  } else {
679  type = ULONG_LONG;
680  ulong_long_value = value;
681  }
682  }
683  MakeArg(LongLong value) { type = LONG_LONG; long_long_value = value; }
684  MakeArg(ULongLong value) { type = ULONG_LONG; ulong_long_value = value; }
685  MakeArg(float value) { type = DOUBLE; double_value = value; }
686  MakeArg(double value) { type = DOUBLE; double_value = value; }
687  MakeArg(long double value) { type = LONG_DOUBLE; long_double_value = value; }
688  MakeArg(signed char value) { type = CHAR; int_value = value; }
689  MakeArg(unsigned char value) { type = CHAR; int_value = value; }
690  MakeArg(char value) { type = CHAR; int_value = value; }
691  MakeArg(wchar_t value) {
692  type = CHAR;
693  int_value = internal::CharTraits<Char>::convert(value);
694  }
695 
696  MakeArg(char *value) { set_string(value); }
697  MakeArg(const char *value) { set_string(value); }
698  MakeArg(const std::string &value) { set_string(value); }
699  MakeArg(StringRef value) { set_string(value); }
700 
701  MakeArg(wchar_t *value) { set_string(value); }
702  MakeArg(const wchar_t *value) { set_string(value); }
703  MakeArg(const std::wstring &value) { set_string(value); }
704  MakeArg(WStringRef value) { set_string(value); }
705 
706  MakeArg(void *value) { type = POINTER; pointer_value = value; }
707  MakeArg(const void *value) { type = POINTER; pointer_value = value; }
708 
709 #if 0
710  // WFG: Removed this because otherwise you can pass a CStr8 or an enum etc
711  // into fmt::sprintf, and it will be interpreted as a CUSTOM type and then
712  // will throw an exception at runtime, which is terrible behaviour.
713  template <typename T>
714  MakeArg(const T &value) {
715  type = CUSTOM;
716  custom.value = &value;
717  custom.format = &format_custom_arg<T>;
718  }
719 #endif
720 };
721 
722 #define FMT_DISPATCH(call) static_cast<Impl*>(this)->call
723 
724 // An argument visitor.
725 // To use ArgVisitor define a subclass that implements some or all of the
726 // visit methods with the same signatures as the methods in ArgVisitor,
727 // for example, visit_int(int).
728 // Specify the subclass name as the Impl template parameter. Then calling
729 // ArgVisitor::visit for some argument will dispatch to a visit method
730 // specific to the argument type. For example, if the argument type is
731 // double then visit_double(double) method of a subclass will be called.
732 // If the subclass doesn't contain a method with this signature, then
733 // a corresponding method of ArgVisitor will be called.
734 //
735 // Example:
736 // class MyArgVisitor : public ArgVisitor<MyArgVisitor, void> {
737 // public:
738 // void visit_int(int value) { print("{}", value); }
739 // void visit_double(double value) { print("{}", value ); }
740 // };
741 //
742 // ArgVisitor uses the curiously recurring template pattern:
743 // http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
744 template <typename Impl, typename Result>
745 class ArgVisitor {
746  public:
747  Result visit_unhandled_arg() { return Result(); }
748 
749  Result visit_int(int value) {
750  return FMT_DISPATCH(visit_any_int(value));
751  }
752  Result visit_long_long(LongLong value) {
753  return FMT_DISPATCH(visit_any_int(value));
754  }
755  Result visit_uint(unsigned value) {
756  return FMT_DISPATCH(visit_any_int(value));
757  }
758  Result visit_ulong_long(ULongLong value) {
759  return FMT_DISPATCH(visit_any_int(value));
760  }
761  Result visit_char(int value) {
762  return FMT_DISPATCH(visit_any_int(value));
763  }
764  template <typename T>
765  Result visit_any_int(T) {
766  return FMT_DISPATCH(visit_unhandled_arg());
767  }
768 
769  Result visit_double(double value) {
770  return FMT_DISPATCH(visit_any_double(value));
771  }
772  Result visit_long_double(long double value) {
773  return FMT_DISPATCH(visit_any_double(value));
774  }
775  template <typename T>
776  Result visit_any_double(T) {
777  return FMT_DISPATCH(visit_unhandled_arg());
778  }
779 
781  return FMT_DISPATCH(visit_unhandled_arg());
782  }
784  return FMT_DISPATCH(visit_unhandled_arg());
785  }
786  Result visit_pointer(const void *) {
787  return FMT_DISPATCH(visit_unhandled_arg());
788  }
790  return FMT_DISPATCH(visit_unhandled_arg());
791  }
792 
793  Result visit(const Arg &arg) {
794  switch (arg.type) {
795  default:
796  assert(false);
797  // Fall through.
798  case Arg::INT:
799  return FMT_DISPATCH(visit_int(arg.int_value));
800  case Arg::UINT:
801  return FMT_DISPATCH(visit_uint(arg.uint_value));
802  case Arg::LONG_LONG:
803  return FMT_DISPATCH(visit_long_long(arg.long_long_value));
804  case Arg::ULONG_LONG:
805  return FMT_DISPATCH(visit_ulong_long(arg.ulong_long_value));
806  case Arg::DOUBLE:
807  return FMT_DISPATCH(visit_double(arg.double_value));
808  case Arg::LONG_DOUBLE:
809  return FMT_DISPATCH(visit_long_double(arg.long_double_value));
810  case Arg::CHAR:
811  return FMT_DISPATCH(visit_char(arg.int_value));
812  case Arg::STRING:
813  return FMT_DISPATCH(visit_string(arg.string));
814  case Arg::WSTRING:
815  return FMT_DISPATCH(visit_wstring(arg.wstring));
816  case Arg::POINTER:
817  return FMT_DISPATCH(visit_pointer(arg.pointer_value));
818  case Arg::CUSTOM:
819  return FMT_DISPATCH(visit_custom(arg.custom));
820  }
821  }
822 };
823 
824 class RuntimeError : public std::runtime_error {
825  protected:
826  RuntimeError() : std::runtime_error("") {}
827 };
828 
829 template <typename Char>
830 class ArgFormatter;
831 } // namespace internal
832 
833 /**
834  An argument list.
835  */
836 class ArgList {
837  private:
839  std::size_t size_;
840 
841  public:
842  ArgList() : size_(0) {}
843  ArgList(const internal::Arg *args, std::size_t size)
844  : args_(args), size_(size) {}
845 
846  /**
847  Returns the list size (the number of arguments).
848  */
849  std::size_t size() const { return size_; }
850 
851  /**
852  Returns the argument at specified index.
853  */
854  const internal::Arg &operator[](std::size_t index) const {
855  return args_[index];
856  }
857 };
858 
859 struct FormatSpec;
860 
861 namespace internal {
862 
864 protected:
867  const char *error_;
868 
869  FormatterBase() : error_(0) {}
870 
871  const Arg &next_arg();
872 
873  const Arg &handle_arg_index(unsigned arg_index);
874 
875  template <typename Char>
876  void write(BasicWriter<Char> &w, const Char *start, const Char *end) {
877  if (start != end)
878  w << BasicStringRef<Char>(start, end - start);
879  }
880 
881  // TODO
882 };
883 
884 // A printf formatter.
885 template <typename Char>
887  private:
888  void parse_flags(FormatSpec &spec, const Char *&s);
889 
890  // Parses argument index, flags and width and returns the parsed
891  // argument index.
892  unsigned parse_header(const Char *&s, FormatSpec &spec);
893 
894  public:
895  void format(BasicWriter<Char> &writer,
896  BasicStringRef<Char> format, const ArgList &args);
897 };
898 } // namespace internal
899 
900 // A formatter.
901 template <typename Char>
902 class BasicFormatter : private internal::FormatterBase {
903 private:
905  const Char *start_;
907 
908  // Parses argument index and returns an argument with this index.
909  const internal::Arg &parse_arg_index(const Char *&s);
910 
911  void check_sign(const Char *&s, const internal::Arg &arg);
912 
913 public:
914  explicit BasicFormatter(BasicWriter<Char> &w) : writer_(w) {}
915 
916  BasicWriter<Char> &writer() { return writer_; }
917 
918  void format(BasicStringRef<Char> format_str, const ArgList &args);
919 
920  const Char *format(const Char *format_str, const internal::Arg &arg);
921 };
922 
923 enum Alignment {
925 };
926 
927 // Flags.
928 enum {
930  CHAR_FLAG = 0x10 // Argument has char type - used in error reporting.
931 };
932 
933 // An empty format specifier.
934 struct EmptySpec {};
935 
936 // A type specifier.
937 template <char TYPE>
938 struct TypeSpec : EmptySpec {
939  Alignment align() const { return ALIGN_DEFAULT; }
940  unsigned width() const { return 0; }
941  int precision() const { return -1; }
942  bool flag(unsigned) const { return false; }
943  char type() const { return TYPE; }
944  char fill() const { return ' '; }
945 };
946 
947 // A width specifier.
948 struct WidthSpec {
949  unsigned width_;
950  // Fill is always wchar_t and cast to char if necessary to avoid having
951  // two specialization of WidthSpec and its subclasses.
952  wchar_t fill_;
953 
954  WidthSpec(unsigned width, wchar_t fill) : width_(width), fill_(fill) {}
955 
956  unsigned width() const { return width_; }
957  wchar_t fill() const { return fill_; }
958 };
959 
960 // An alignment specifier.
963 
964  AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT)
965  : WidthSpec(width, fill), align_(align) {}
966 
967  Alignment align() const { return align_; }
968 
969  int precision() const { return -1; }
970 };
971 
972 // An alignment and type specifier.
973 template <char TYPE>
975  AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {}
976 
977  bool flag(unsigned) const { return false; }
978  char type() const { return TYPE; }
979 };
980 
981 // A full format specifier.
983  unsigned flags_;
985  char type_;
986 
988  unsigned width = 0, char type = 0, wchar_t fill = ' ')
989  : AlignSpec(width, fill), flags_(0), precision_(-1), type_(type) {}
990 
991  bool flag(unsigned f) const { return (flags_ & f) != 0; }
992  int precision() const { return precision_; }
993  char type() const { return type_; }
994 };
995 
996 // An integer format specifier.
997 template <typename T, typename SpecT = TypeSpec<0>, typename Char = char>
998 class IntFormatSpec : public SpecT {
999  private:
1001 
1002  public:
1003  IntFormatSpec(T value, const SpecT &spec = SpecT())
1004  : SpecT(spec), value_(value) {}
1005 
1006  T value() const { return value_; }
1007 };
1008 
1009 // A string format specifier.
1010 template <typename T>
1011 class StrFormatSpec : public AlignSpec {
1012  private:
1013  const T *str_;
1014 
1015  public:
1016  StrFormatSpec(const T *str, unsigned width, wchar_t fill)
1017  : AlignSpec(width, fill), str_(str) {}
1018 
1019  const T *str() const { return str_; }
1020 };
1021 
1022 /**
1023  Returns an integer format specifier to format the value in base 2.
1024  */
1026 
1027 /**
1028  Returns an integer format specifier to format the value in base 8.
1029  */
1031 
1032 /**
1033  Returns an integer format specifier to format the value in base 16 using
1034  lower-case letters for the digits above 9.
1035  */
1037 
1038 /**
1039  Returns an integer formatter format specifier to format in base 16 using
1040  upper-case letters for the digits above 9.
1041  */
1043 
1044 /**
1045  \rst
1046  Returns an integer format specifier to pad the formatted argument with the
1047  fill character to the specified width using the default (right) numeric
1048  alignment.
1049 
1050  **Example**::
1051 
1052  Writer out;
1053  out << pad(hex(0xcafe), 8, '0');
1054  // out.str() == "0000cafe"
1055 
1056  \endrst
1057  */
1058 template <char TYPE_CODE, typename Char>
1060  int value, unsigned width, Char fill = ' ');
1061 
1062 #define FMT_DEFINE_INT_FORMATTERS(TYPE) \
1063 inline IntFormatSpec<TYPE, TypeSpec<'b'> > bin(TYPE value) { \
1064  return IntFormatSpec<TYPE, TypeSpec<'b'> >(value, TypeSpec<'b'>()); \
1065 } \
1066  \
1067 inline IntFormatSpec<TYPE, TypeSpec<'o'> > oct(TYPE value) { \
1068  return IntFormatSpec<TYPE, TypeSpec<'o'> >(value, TypeSpec<'o'>()); \
1069 } \
1070  \
1071 inline IntFormatSpec<TYPE, TypeSpec<'x'> > hex(TYPE value) { \
1072  return IntFormatSpec<TYPE, TypeSpec<'x'> >(value, TypeSpec<'x'>()); \
1073 } \
1074  \
1075 inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
1076  return IntFormatSpec<TYPE, TypeSpec<'X'> >(value, TypeSpec<'X'>()); \
1077 } \
1078  \
1079 template <char TYPE_CODE> \
1080 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> > pad( \
1081  IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, unsigned width) { \
1082  return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> >( \
1083  f.value(), AlignTypeSpec<TYPE_CODE>(width, ' ')); \
1084 } \
1085  \
1086 /* For compatibility with older compilers we provide two overloads for pad, */ \
1087 /* one that takes a fill character and one that doesn't. In the future this */ \
1088 /* can be replaced with one overload making the template argument Char */ \
1089 /* default to char (C++11). */ \
1090 template <char TYPE_CODE, typename Char> \
1091 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char> pad( \
1092  IntFormatSpec<TYPE, TypeSpec<TYPE_CODE>, Char> f, \
1093  unsigned width, Char fill) { \
1094  return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char>( \
1095  f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \
1096 } \
1097  \
1098 inline IntFormatSpec<TYPE, AlignTypeSpec<0> > pad( \
1099  TYPE value, unsigned width) { \
1100  return IntFormatSpec<TYPE, AlignTypeSpec<0> >( \
1101  value, AlignTypeSpec<0>(width, ' ')); \
1102 } \
1103  \
1104 template <typename Char> \
1105 inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
1106  TYPE value, unsigned width, Char fill) { \
1107  return IntFormatSpec<TYPE, AlignTypeSpec<0>, Char>( \
1108  value, AlignTypeSpec<0>(width, fill)); \
1109 }
1110 
1113 FMT_DEFINE_INT_FORMATTERS(unsigned)
1114 FMT_DEFINE_INT_FORMATTERS(unsigned long)
1115 FMT_DEFINE_INT_FORMATTERS(LongLong)
1116 FMT_DEFINE_INT_FORMATTERS(ULongLong)
1117 
1118 /**
1119  \rst
1120  Returns a string formatter that pads the formatted argument with the fill
1121  character to the specified width using the default (left) string alignment.
1122 
1123  **Example**::
1124 
1125  std::string s = str(Writer() << pad("abc", 8));
1126  // s == "abc "
1127 
1128  \endrst
1129  */
1130 template <typename Char>
1131 inline StrFormatSpec<Char> pad(
1132  const Char *str, unsigned width, Char fill = ' ') {
1133  return StrFormatSpec<Char>(str, width, fill);
1134 }
1135 
1137  const wchar_t *str, unsigned width, char fill = ' ') {
1138  return StrFormatSpec<wchar_t>(str, width, fill);
1139 }
1140 
1141 // Generates a comma-separated list with results of applying f to numbers 0..n-1.
1142 # define FMT_GEN(n, f) FMT_GEN##n(f)
1143 # define FMT_GEN1(f) f(0)
1144 # define FMT_GEN2(f) FMT_GEN1(f), f(1)
1145 # define FMT_GEN3(f) FMT_GEN2(f), f(2)
1146 # define FMT_GEN4(f) FMT_GEN3(f), f(3)
1147 # define FMT_GEN5(f) FMT_GEN4(f), f(4)
1148 # define FMT_GEN6(f) FMT_GEN5(f), f(5)
1149 # define FMT_GEN7(f) FMT_GEN6(f), f(6)
1150 # define FMT_GEN8(f) FMT_GEN7(f), f(7)
1151 # define FMT_GEN9(f) FMT_GEN8(f), f(8)
1152 # define FMT_GEN10(f) FMT_GEN9(f), f(9)
1153 
1154 # define FMT_MAKE_TEMPLATE_ARG(n) typename T##n
1155 # define FMT_MAKE_ARG(n) const T##n &v##n
1156 # define FMT_MAKE_REF_char(n) fmt::internal::MakeArg<char>(v##n)
1157 # define FMT_MAKE_REF_wchar_t(n) fmt::internal::MakeArg<wchar_t>(v##n)
1158 
1159 #if FMT_USE_VARIADIC_TEMPLATES
1160 // Defines a variadic function returning void.
1161 # define FMT_VARIADIC_VOID(func, arg_type) \
1162  template<typename... Args> \
1163  void func(arg_type arg1, const Args & ... args) { \
1164  using fmt::internal::Arg; \
1165  const Arg arg_array[fmt::internal::NonZero<sizeof...(Args)>::VALUE] = { \
1166  fmt::internal::MakeArg<Char>(args)... \
1167  }; \
1168  func(arg1, ArgList(arg_array, sizeof...(Args))); \
1169  }
1170 
1171 // Defines a variadic constructor.
1172 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
1173  template<typename... Args> \
1174  ctor(arg0_type arg0, arg1_type arg1, const Args & ... args) { \
1175  using fmt::internal::Arg; \
1176  const Arg arg_array[fmt::internal::NonZero<sizeof...(Args)>::VALUE] = { \
1177  fmt::internal::MakeArg<Char>(args)... \
1178  }; \
1179  func(arg0, arg1, ArgList(arg_array, sizeof...(Args))); \
1180  }
1181 
1182 #else
1183 
1184 # define FMT_MAKE_REF(n) fmt::internal::MakeArg<Char>(v##n)
1185 // Defines a wrapper for a function taking one argument of type arg_type
1186 // and n additional arguments of arbitrary types.
1187 # define FMT_WRAP1(func, arg_type, n) \
1188  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
1189  inline void func(arg_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
1190  const fmt::internal::Arg args[] = {FMT_GEN(n, FMT_MAKE_REF)}; \
1191  func(arg1, fmt::ArgList(args, sizeof(args) / sizeof(*args))); \
1192  }
1193 
1194 // Emulates a variadic function returning void on a pre-C++11 compiler.
1195 # define FMT_VARIADIC_VOID(func, arg_type) \
1196  FMT_WRAP1(func, arg_type, 1) FMT_WRAP1(func, arg_type, 2) \
1197  FMT_WRAP1(func, arg_type, 3) FMT_WRAP1(func, arg_type, 4) \
1198  FMT_WRAP1(func, arg_type, 5) FMT_WRAP1(func, arg_type, 6) \
1199  FMT_WRAP1(func, arg_type, 7) FMT_WRAP1(func, arg_type, 8) \
1200  FMT_WRAP1(func, arg_type, 9) FMT_WRAP1(func, arg_type, 10)
1201 
1202 # define FMT_CTOR(ctor, func, arg0_type, arg1_type, n) \
1203  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
1204  ctor(arg0_type arg0, arg1_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
1205  const fmt::internal::Arg args[] = {FMT_GEN(n, FMT_MAKE_REF)}; \
1206  func(arg0, arg1, fmt::ArgList(args, sizeof(args) / sizeof(*args))); \
1207  }
1208 
1209 // Emulates a variadic constructor on a pre-C++11 compiler.
1210 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
1211  FMT_CTOR(ctor, func, arg0_type, arg1_type, 1) \
1212  FMT_CTOR(ctor, func, arg0_type, arg1_type, 2) \
1213  FMT_CTOR(ctor, func, arg0_type, arg1_type, 3) \
1214  FMT_CTOR(ctor, func, arg0_type, arg1_type, 4) \
1215  FMT_CTOR(ctor, func, arg0_type, arg1_type, 5) \
1216  FMT_CTOR(ctor, func, arg0_type, arg1_type, 6) \
1217  FMT_CTOR(ctor, func, arg0_type, arg1_type, 7) \
1218  FMT_CTOR(ctor, func, arg0_type, arg1_type, 8) \
1219  FMT_CTOR(ctor, func, arg0_type, arg1_type, 9) \
1220  FMT_CTOR(ctor, func, arg0_type, arg1_type, 10)
1221 #endif
1222 
1223 // Generates a comma-separated list with results of applying f to pairs
1224 // (argument, index).
1225 #define FMT_FOR_EACH1(f, x0) f(x0, 0)
1226 #define FMT_FOR_EACH2(f, x0, x1) \
1227  FMT_FOR_EACH1(f, x0), f(x1, 1)
1228 #define FMT_FOR_EACH3(f, x0, x1, x2) \
1229  FMT_FOR_EACH2(f, x0 ,x1), f(x2, 2)
1230 #define FMT_FOR_EACH4(f, x0, x1, x2, x3) \
1231  FMT_FOR_EACH3(f, x0, x1, x2), f(x3, 3)
1232 #define FMT_FOR_EACH5(f, x0, x1, x2, x3, x4) \
1233  FMT_FOR_EACH4(f, x0, x1, x2, x3), f(x4, 4)
1234 #define FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5) \
1235  FMT_FOR_EACH5(f, x0, x1, x2, x3, x4), f(x5, 5)
1236 #define FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6) \
1237  FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5), f(x6, 6)
1238 #define FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7) \
1239  FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6), f(x7, 7)
1240 #define FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8) \
1241  FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7), f(x8, 8)
1242 #define FMT_FOR_EACH10(f, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) \
1243  FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8), f(x9, 9)
1244 
1245 /**
1246 An error returned by an operating system or a language runtime,
1247 for example a file opening error.
1248 */
1250  private:
1251  void init(int error_code, StringRef format_str, const ArgList &args);
1252 
1253  protected:
1255 
1256  typedef char Char; // For FMT_VARIADIC_CTOR.
1257 
1259 
1260  public:
1261  /**
1262  \rst
1263  Constructs a :cpp:class:`fmt::SystemError` object with the description
1264  of the form "*<message>*: *<system-message>*", where *<message>* is the
1265  formatted message and *<system-message>* is the system message corresponding
1266  to the error code.
1267  *error_code* is a system error code as given by ``errno``.
1268  \endrst
1269  */
1270  SystemError(int error_code, StringRef message) {
1271  init(error_code, message, ArgList());
1272  }
1273  FMT_VARIADIC_CTOR(SystemError, init, int, StringRef)
1274 
1275  int error_code() const { return error_code_; }
1276 };
1277 
1278 /**
1279  \rst
1280  This template provides operations for formatting and writing data into
1281  a character stream. The output is stored in a memory buffer that grows
1282  dynamically.
1283 
1284  You can use one of the following typedefs for common character types:
1285 
1286  +---------+----------------------+
1287  | Type | Definition |
1288  +=========+======================+
1289  | Writer | BasicWriter<char> |
1290  +---------+----------------------+
1291  | WWriter | BasicWriter<wchar_t> |
1292  +---------+----------------------+
1293 
1294  **Example**::
1295 
1296  Writer out;
1297  out << "The answer is " << 42 << "\n";
1298  out.write("({:+f}, {:+f})", -3.14, 3.14);
1299 
1300  This will write the following output to the ``out`` object:
1301 
1302  .. code-block:: none
1303 
1304  The answer is 42
1305  (-3.140000, +3.140000)
1306 
1307  The output can be converted to an ``std::string`` with ``out.str()`` or
1308  accessed as a C string with ``out.c_str()``.
1309  \endrst
1310  */
1311 template <typename Char>
1312 class BasicWriter {
1313  private:
1314  // Output buffer.
1316 
1318 
1319 #if defined(_SECURE_SCL) && _SECURE_SCL
1320  // Returns pointer value.
1321  static Char *get(CharPtr p) { return p.base(); }
1322 #else
1323  static Char *get(Char *p) { return p; }
1324 #endif
1325 
1326  static CharPtr fill_padding(CharPtr buffer,
1327  unsigned total_size, std::size_t content_size, wchar_t fill);
1328 
1329  // Grows the buffer by n characters and returns a pointer to the newly
1330  // allocated area.
1331  CharPtr grow_buffer(std::size_t n) {
1332  std::size_t size = buffer_.size();
1333  buffer_.resize(size + n);
1334  return internal::make_ptr(&buffer_[size], n);
1335  }
1336 
1337  // Prepare a buffer for integer formatting.
1338  CharPtr prepare_int_buffer(unsigned num_digits,
1339  const EmptySpec &, const char *prefix, unsigned prefix_size) {
1340  unsigned size = prefix_size + num_digits;
1341  CharPtr p = grow_buffer(size);
1342  std::copy(prefix, prefix + prefix_size, p);
1343  return p + size - 1;
1344  }
1345 
1346  template <typename Spec>
1347  CharPtr prepare_int_buffer(unsigned num_digits,
1348  const Spec &spec, const char *prefix, unsigned prefix_size);
1349 
1350  // Formats an integer.
1351  template <typename T, typename Spec>
1352  void write_int(T value, const Spec &spec);
1353 
1354  // Formats a floating-point number (double or long double).
1355  template <typename T>
1356  void write_double(T value, const FormatSpec &spec);
1357 
1358  // Writes a formatted string.
1359  template <typename StrChar>
1360  CharPtr write_str(
1361  const StrChar *s, std::size_t size, const AlignSpec &spec);
1362 
1363  template <typename StrChar>
1364  void write_str(
1365  const internal::Arg::StringValue<StrChar> &str, const FormatSpec &spec);
1366 
1367  // This method is private to disallow writing a wide string to a
1368  // char stream and vice versa. If you want to print a wide string
1369  // as a pointer as std::ostream does, cast it to const void*.
1370  // Do not implement!
1371  void operator<<(typename internal::CharTraits<Char>::UnsupportedStrType);
1372 
1373  friend class internal::ArgFormatter<Char>;
1374  friend class internal::PrintfFormatter<Char>;
1375 
1376  public:
1377  /**
1378  Constructs a ``BasicWriter`` object.
1379  */
1381 
1382 #if FMT_USE_RVALUE_REFERENCES
1383  /**
1384  Constructs a ``BasicWriter`` object moving the content of the other
1385  object to it.
1386  */
1387  BasicWriter(BasicWriter &&other) : buffer_(std::move(other.buffer_)) {}
1388 
1389  /**
1390  Moves the content of the other ``BasicWriter`` object to this one.
1391  */
1392  BasicWriter& operator=(BasicWriter &&other) {
1393  assert(this != &other);
1394  buffer_ = std::move(other.buffer_);
1395  return *this;
1396  }
1397 #endif
1398 
1399  /**
1400  Returns the total number of characters written.
1401  */
1402  std::size_t size() const { return buffer_.size(); }
1403 
1404  /**
1405  Returns a pointer to the output buffer content. No terminating null
1406  character is appended.
1407  */
1408  const Char *data() const { return &buffer_[0]; }
1409 
1410  /**
1411  Returns a pointer to the output buffer content with terminating null
1412  character appended.
1413  */
1414  const Char *c_str() const {
1415  std::size_t size = buffer_.size();
1416  buffer_.reserve(size + 1);
1417  buffer_[size] = '\0';
1418  return &buffer_[0];
1419  }
1420 
1421  /**
1422  Returns the content of the output buffer as an `std::string`.
1423  */
1424  std::basic_string<Char> str() const {
1425  return std::basic_string<Char>(&buffer_[0], buffer_.size());
1426  }
1427 
1428  /**
1429  \rst
1430  Writes formatted data.
1431 
1432  *args* is an argument list representing arbitrary arguments.
1433 
1434  **Example**::
1435 
1436  Writer out;
1437  out.write("Current point:\n");
1438  out.write("({:+f}, {:+f})", -3.14, 3.14);
1439 
1440  This will write the following output to the ``out`` object:
1441 
1442  .. code-block:: none
1443 
1444  Current point:
1445  (-3.140000, +3.140000)
1446 
1447  The output can be accessed using :meth:`data`, :meth:`c_str` or :meth:`str`
1448  methods.
1449 
1450  See also `Format String Syntax`_.
1451  \endrst
1452  */
1454  BasicFormatter<Char>(*this).format(format, args);
1455  }
1457 
1458  BasicWriter &operator<<(int value) {
1459  return *this << IntFormatSpec<int>(value);
1460  }
1461  BasicWriter &operator<<(unsigned value) {
1462  return *this << IntFormatSpec<unsigned>(value);
1463  }
1464  BasicWriter &operator<<(long value) {
1465  return *this << IntFormatSpec<long>(value);
1466  }
1467  BasicWriter &operator<<(unsigned long value) {
1468  return *this << IntFormatSpec<unsigned long>(value);
1469  }
1470  BasicWriter &operator<<(LongLong value) {
1471  return *this << IntFormatSpec<LongLong>(value);
1472  }
1473 
1474  /**
1475  Formats *value* and writes it to the stream.
1476  */
1477  BasicWriter &operator<<(ULongLong value) {
1478  return *this << IntFormatSpec<ULongLong>(value);
1479  }
1480 
1481  BasicWriter &operator<<(double value) {
1482  write_double(value, FormatSpec());
1483  return *this;
1484  }
1485 
1486  /**
1487  Formats *value* using the general format for floating-point numbers
1488  (``'g'``) and writes it to the stream.
1489  */
1490  BasicWriter &operator<<(long double value) {
1491  write_double(value, FormatSpec());
1492  return *this;
1493  }
1494 
1495  /**
1496  Writes a character to the stream.
1497  */
1498  BasicWriter &operator<<(char value) {
1499  buffer_.push_back(value);
1500  return *this;
1501  }
1502 
1503  BasicWriter &operator<<(wchar_t value) {
1504  buffer_.push_back(internal::CharTraits<Char>::convert(value));
1505  return *this;
1506  }
1507 
1508  /**
1509  Writes *value* to the stream.
1510  */
1511  BasicWriter &operator<<(fmt::BasicStringRef<Char> value) {
1512  const Char *str = value.c_str();
1513  buffer_.append(str, str + value.size());
1514  return *this;
1515  }
1516 
1517  template <typename T, typename Spec, typename FillChar>
1518  BasicWriter &operator<<(const IntFormatSpec<T, Spec, FillChar> &spec) {
1520  write_int(spec.value(), spec);
1521  return *this;
1522  }
1523 
1524  template <typename StrChar>
1525  BasicWriter &operator<<(const StrFormatSpec<StrChar> &spec) {
1526  const StrChar *s = spec.str();
1527  // TODO: error if fill is not convertible to Char
1528  write_str(s, std::char_traits<Char>::length(s), spec);
1529  return *this;
1530  }
1531 
1532  void clear() { buffer_.clear(); }
1533 };
1534 
1535 template <typename Char>
1536 template <typename StrChar>
1538  const StrChar *s, std::size_t size, const AlignSpec &spec) {
1539  CharPtr out = CharPtr();
1540  if (spec.width() > size) {
1541  out = grow_buffer(spec.width());
1542  Char fill = static_cast<Char>(spec.fill());
1543  if (spec.align() == ALIGN_RIGHT) {
1544  std::fill_n(out, spec.width() - size, fill);
1545  out += spec.width() - size;
1546  } else if (spec.align() == ALIGN_CENTER) {
1547  out = fill_padding(out, spec.width(), size, fill);
1548  } else {
1549  std::fill_n(out + size, spec.width() - size, fill);
1550  }
1551  } else {
1552  out = grow_buffer(size);
1553  }
1554  std::copy(s, s + size, out);
1555  return out;
1556 }
1557 
1558 template <typename Char>
1559 template <typename Spec>
1562  unsigned num_digits, const Spec &spec,
1563  const char *prefix, unsigned prefix_size) {
1564  unsigned width = spec.width();
1565  Alignment align = spec.align();
1566  Char fill = static_cast<Char>(spec.fill());
1567  if (spec.precision() > static_cast<int>(num_digits)) {
1568  // Octal prefix '0' is counted as a digit, so ignore it if precision
1569  // is specified.
1570  if (prefix_size > 0 && prefix[prefix_size - 1] == '0')
1571  --prefix_size;
1572  unsigned number_size = prefix_size + spec.precision();
1573  AlignSpec subspec(number_size, '0', ALIGN_NUMERIC);
1574  if (number_size >= width)
1575  return prepare_int_buffer(num_digits, subspec, prefix, prefix_size);
1576  buffer_.reserve(width);
1577  unsigned fill_size = width - number_size;
1578  if (align != ALIGN_LEFT) {
1579  CharPtr p = grow_buffer(fill_size);
1580  std::fill(p, p + fill_size, fill);
1581  }
1582  CharPtr result = prepare_int_buffer(
1583  num_digits, subspec, prefix, prefix_size);
1584  if (align == ALIGN_LEFT) {
1585  CharPtr p = grow_buffer(fill_size);
1586  std::fill(p, p + fill_size, fill);
1587  }
1588  return result;
1589  }
1590  unsigned size = prefix_size + num_digits;
1591  if (width <= size) {
1592  CharPtr p = grow_buffer(size);
1593  std::copy(prefix, prefix + prefix_size, p);
1594  return p + size - 1;
1595  }
1596  CharPtr p = grow_buffer(width);
1597  CharPtr end = p + width;
1598  if (align == ALIGN_LEFT) {
1599  std::copy(prefix, prefix + prefix_size, p);
1600  p += size;
1601  std::fill(p, end, fill);
1602  } else if (align == ALIGN_CENTER) {
1603  p = fill_padding(p, width, size, fill);
1604  std::copy(prefix, prefix + prefix_size, p);
1605  p += size;
1606  } else {
1607  if (align == ALIGN_NUMERIC) {
1608  if (prefix_size != 0) {
1609  p = std::copy(prefix, prefix + prefix_size, p);
1610  size -= prefix_size;
1611  }
1612  } else {
1613  std::copy(prefix, prefix + prefix_size, end - size);
1614  }
1615  std::fill(p, end - size, fill);
1616  p = end;
1617  }
1618  return p - 1;
1619 }
1620 
1621 template <typename Char>
1622 template <typename T, typename Spec>
1623 void BasicWriter<Char>::write_int(T value, const Spec &spec) {
1624  unsigned prefix_size = 0;
1625  typedef typename internal::IntTraits<T>::MainType UnsignedType;
1626  UnsignedType abs_value = value;
1627  char prefix[4] = "";
1628  if (internal::is_negative(value)) {
1629  prefix[0] = '-';
1630  ++prefix_size;
1631  abs_value = 0 - abs_value;
1632  } else if (spec.flag(SIGN_FLAG)) {
1633  prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' ';
1634  ++prefix_size;
1635  }
1636  switch (spec.type()) {
1637  case 0: case 'd': {
1638  unsigned num_digits = internal::count_digits(abs_value);
1639  CharPtr p = prepare_int_buffer(
1640  num_digits, spec, prefix, prefix_size) + 1 - num_digits;
1641  internal::format_decimal(get(p), abs_value, num_digits);
1642  break;
1643  }
1644  case 'x': case 'X': {
1645  UnsignedType n = abs_value;
1646  if (spec.flag(HASH_FLAG)) {
1647  prefix[prefix_size++] = '0';
1648  prefix[prefix_size++] = spec.type();
1649  }
1650  unsigned num_digits = 0;
1651  do {
1652  ++num_digits;
1653  } while ((n >>= 4) != 0);
1654  Char *p = get(prepare_int_buffer(
1655  num_digits, spec, prefix, prefix_size));
1656  n = abs_value;
1657  const char *digits = spec.type() == 'x' ?
1658  "0123456789abcdef" : "0123456789ABCDEF";
1659  do {
1660  *p-- = digits[n & 0xf];
1661  } while ((n >>= 4) != 0);
1662  break;
1663  }
1664  case 'b': case 'B': {
1665  UnsignedType n = abs_value;
1666  if (spec.flag(HASH_FLAG)) {
1667  prefix[prefix_size++] = '0';
1668  prefix[prefix_size++] = spec.type();
1669  }
1670  unsigned num_digits = 0;
1671  do {
1672  ++num_digits;
1673  } while ((n >>= 1) != 0);
1674  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
1675  n = abs_value;
1676  do {
1677  *p-- = '0' + (n & 1);
1678  } while ((n >>= 1) != 0);
1679  break;
1680  }
1681  case 'o': {
1682  UnsignedType n = abs_value;
1683  if (spec.flag(HASH_FLAG))
1684  prefix[prefix_size++] = '0';
1685  unsigned num_digits = 0;
1686  do {
1687  ++num_digits;
1688  } while ((n >>= 3) != 0);
1689  Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
1690  n = abs_value;
1691  do {
1692  *p-- = '0' + (n & 7);
1693  } while ((n >>= 3) != 0);
1694  break;
1695  }
1696  default:
1698  spec.type(), spec.flag(CHAR_FLAG) ? "char" : "integer");
1699  break;
1700  }
1701 }
1702 
1703 // Formats a value.
1704 template <typename Char, typename T>
1705 void format(BasicFormatter<Char> &f, const Char *format_str, const T &value) {
1706  std::basic_ostringstream<Char> os;
1707  os << value;
1708  f.format(format_str, internal::MakeArg<Char>(os.str()));
1709 }
1710 
1711 // Reports a system error without throwing an exception.
1712 // Can be used to report errors from destructors.
1713 void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT(true);
1714 
1715 #ifdef _WIN32
1716 
1717 /**
1718  A Windows error.
1719 */
1720 class WindowsError : public SystemError {
1721  private:
1722  void init(int error_code, StringRef format_str, const ArgList &args);
1723 
1724  public:
1725  /**
1726  \rst
1727  Constructs a :cpp:class:`fmt::WindowsError` object with the description
1728  of the form "*<message>*: *<system-message>*", where *<message>* is the
1729  formatted message and *<system-message>* is the system message corresponding
1730  to the error code.
1731  *error_code* is a Windows error code as given by ``GetLastError``.
1732  \endrst
1733  */
1734  WindowsError(int error_code, StringRef message) {
1735  init(error_code, message, ArgList());
1736  }
1737  FMT_VARIADIC_CTOR(WindowsError, init, int, StringRef)
1738 };
1739 
1740 // Reports a Windows error without throwing an exception.
1741 // Can be used to report errors from destructors.
1742 void report_windows_error(int error_code, StringRef message) FMT_NOEXCEPT(true);
1743 
1744 #endif
1745 
1747 
1748 /**
1749  Formats a string and prints it to stdout using ANSI escape sequences
1750  to specify color (experimental).
1751  Example:
1752  PrintColored(fmt::RED, "Elapsed time: {0:.2f} seconds") << 1.23;
1753  */
1754 void print_colored(Color c, StringRef format, const ArgList &args);
1755 
1756 /**
1757  \rst
1758  Formats arguments and returns the result as a string.
1759 
1760  **Example**::
1761 
1762  std::string message = format("The answer is {}", 42);
1763  \endrst
1764 */
1765 inline std::string format(StringRef format_str, const ArgList &args) {
1766  Writer w;
1767  w.write(format_str, args);
1768  return w.str();
1769 }
1770 
1771 inline std::wstring format(WStringRef format_str, const ArgList &args) {
1772  WWriter w;
1773  w.write(format_str, args);
1774  return w.str();
1775 }
1776 
1777 /**
1778  \rst
1779  Prints formatted data to the file *f*.
1780 
1781  **Example**::
1782 
1783  print(stderr, "Don't {}!", "panic");
1784  \endrst
1785  */
1786 void print(std::FILE *f, StringRef format_str, const ArgList &args);
1787 
1788 /**
1789  \rst
1790  Prints formatted data to ``stdout``.
1791 
1792  **Example**::
1793 
1794  print("Elapsed time: {0:.2f} seconds", 1.23);
1795  \endrst
1796  */
1797 inline void print(StringRef format_str, const ArgList &args) {
1798  print(stdout, format_str, args);
1799 }
1800 
1801 /**
1802  \rst
1803  Prints formatted data to the stream *os*.
1804 
1805  **Example**::
1806 
1807  print(cerr, "Don't {}!", "panic");
1808  \endrst
1809  */
1810 void print(std::ostream &os, StringRef format_str, const ArgList &args);
1811 
1812 template <typename Char>
1814  BasicStringRef<Char> format, const ArgList &args) {
1815  internal::PrintfFormatter<Char>().format(w, format, args);
1816 }
1817 
1818 /**
1819  \rst
1820  Formats arguments and returns the result as a string.
1821 
1822  **Example**::
1823 
1824  std::string message = fmt::sprintf("The answer is %d", 42);
1825  \endrst
1826 */
1827 inline std::string sprintf(StringRef format, const ArgList &args) {
1828  Writer w;
1829  printf(w, format, args);
1830  return w.str();
1831 }
1832 
1833 /**
1834  \rst
1835  Prints formatted data to the file *f*.
1836 
1837  **Example**::
1838 
1839  fmt::fprintf(stderr, "Don't %s!", "panic");
1840  \endrst
1841  */
1842 int fprintf(std::FILE *f, StringRef format, const ArgList &args);
1843 
1844 /**
1845  \rst
1846  Prints formatted data to ``stdout``.
1847 
1848  **Example**::
1849 
1850  fmt::printf("Elapsed time: %.2f seconds", 1.23);
1851  \endrst
1852  */
1853 inline int printf(StringRef format, const ArgList &args) {
1854  return fprintf(stdout, format, args);
1855 }
1856 
1857 /**
1858  Fast integer formatter.
1859  */
1860 class FormatInt {
1861  private:
1862  // Buffer should be large enough to hold all digits (digits10 + 1),
1863  // a sign and a null character.
1864  enum {BUFFER_SIZE = std::numeric_limits<ULongLong>::digits10 + 3};
1865  mutable char buffer_[BUFFER_SIZE];
1866  char *str_;
1867 
1868  // Formats value in reverse and returns the number of digits.
1869  char *format_decimal(ULongLong value) {
1870  char *buffer_end = buffer_ + BUFFER_SIZE - 1;
1871  while (value >= 100) {
1872  // Integer division is slow so do it for a group of two digits instead
1873  // of for every digit. The idea comes from the talk by Alexandrescu
1874  // "Three Optimization Tips for C++". See speed-test for a comparison.
1875  unsigned index = (value % 100) * 2;
1876  value /= 100;
1877  *--buffer_end = internal::DIGITS[index + 1];
1878  *--buffer_end = internal::DIGITS[index];
1879  }
1880  if (value < 10) {
1881  *--buffer_end = static_cast<char>('0' + value);
1882  return buffer_end;
1883  }
1884  unsigned index = static_cast<unsigned>(value * 2);
1885  *--buffer_end = internal::DIGITS[index + 1];
1886  *--buffer_end = internal::DIGITS[index];
1887  return buffer_end;
1888  }
1889 
1890  void FormatSigned(LongLong value) {
1891  ULongLong abs_value = static_cast<ULongLong>(value);
1892  bool negative = value < 0;
1893  if (negative)
1894  abs_value = 0 - abs_value;
1895  str_ = format_decimal(abs_value);
1896  if (negative)
1897  *--str_ = '-';
1898  }
1899 
1900  public:
1901  explicit FormatInt(int value) { FormatSigned(value); }
1902  explicit FormatInt(long value) { FormatSigned(value); }
1903  explicit FormatInt(LongLong value) { FormatSigned(value); }
1904  explicit FormatInt(unsigned value) : str_(format_decimal(value)) {}
1905  explicit FormatInt(unsigned long value) : str_(format_decimal(value)) {}
1906  explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {}
1907 
1908  /**
1909  Returns the number of characters written to the output buffer.
1910  */
1911  std::size_t size() const { return buffer_ - str_ + BUFFER_SIZE - 1; }
1912 
1913  /**
1914  Returns a pointer to the output buffer content. No terminating null
1915  character is appended.
1916  */
1917  const char *data() const { return str_; }
1918 
1919  /**
1920  Returns a pointer to the output buffer content with terminating null
1921  character appended.
1922  */
1923  const char *c_str() const {
1924  buffer_[BUFFER_SIZE - 1] = '\0';
1925  return str_;
1926  }
1927 
1928  /**
1929  Returns the content of the output buffer as an `std::string`.
1930  */
1931  std::string str() const { return std::string(str_, size()); }
1932 };
1933 
1934 // Formats a decimal integer value writing into buffer and returns
1935 // a pointer to the end of the formatted string. This function doesn't
1936 // write a terminating null character.
1937 template <typename T>
1938 inline void format_decimal(char *&buffer, T value) {
1939  typename internal::IntTraits<T>::MainType abs_value = value;
1940  if (internal::is_negative(value)) {
1941  *buffer++ = '-';
1942  abs_value = 0 - abs_value;
1943  }
1944  if (abs_value < 100) {
1945  if (abs_value < 10) {
1946  *buffer++ = static_cast<char>('0' + abs_value);
1947  return;
1948  }
1949  unsigned index = static_cast<unsigned>(abs_value * 2);
1950  *buffer++ = internal::DIGITS[index];
1951  *buffer++ = internal::DIGITS[index + 1];
1952  return;
1953  }
1954  unsigned num_digits = internal::count_digits(abs_value);
1955  internal::format_decimal(buffer, abs_value, num_digits);
1956  buffer += num_digits;
1957 }
1958 }
1959 
1960 #if FMT_GCC_VERSION
1961 // Use the system_header pragma to suppress warnings about variadic macros
1962 // because suppressing -Wvariadic-macros with the diagnostic pragma doesn't
1963 // work. It is used at the end because we want to suppress as little warnings
1964 // as possible.
1965 # pragma GCC system_header
1966 #endif
1967 
1968 // This is used to work around VC++ bugs in handling variadic macros.
1969 #define FMT_EXPAND(args) args
1970 
1971 // Returns the number of arguments.
1972 // Based on https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s.
1973 #define FMT_NARG(...) FMT_NARG_(__VA_ARGS__, FMT_RSEQ_N())
1974 #define FMT_NARG_(...) FMT_EXPAND(FMT_ARG_N(__VA_ARGS__))
1975 #define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
1976 #define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
1977 
1978 #define FMT_CONCAT(a, b) a##b
1979 #define FMT_FOR_EACH_(N, f, ...) \
1980  FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__))
1981 #define FMT_FOR_EACH(f, ...) \
1982  FMT_EXPAND(FMT_FOR_EACH_(FMT_NARG(__VA_ARGS__), f, __VA_ARGS__))
1983 
1984 #define FMT_ADD_ARG_NAME(type, index) type arg##index
1985 #define FMT_GET_ARG_NAME(type, index) arg##index
1986 
1987 #if FMT_USE_VARIADIC_TEMPLATES
1988 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
1989  template<typename... Args> \
1990  ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
1991  const Args & ... args) { \
1992  using fmt::internal::Arg; \
1993  const Arg array[fmt::internal::NonZero<sizeof...(Args)>::VALUE] = { \
1994  fmt::internal::MakeArg<Char>(args)... \
1995  }; \
1996  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), \
1997  fmt::ArgList(array, sizeof...(Args))); \
1998  }
1999 #else
2000 // Defines a wrapper for a function taking __VA_ARGS__ arguments
2001 // and n additional arguments of arbitrary types.
2002 # define FMT_WRAP(Char, ReturnType, func, call, n, ...) \
2003  template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2004  inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
2005  FMT_GEN(n, FMT_MAKE_ARG)) { \
2006  const fmt::internal::Arg args[] = {FMT_GEN(n, FMT_MAKE_REF_##Char)}; \
2007  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), \
2008  fmt::ArgList(args, sizeof(args) / sizeof(*args))); \
2009  }
2010 
2011 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
2012  inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) { \
2013  call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList()); \
2014  } \
2015  FMT_WRAP(Char, ReturnType, func, call, 1, __VA_ARGS__) \
2016  FMT_WRAP(Char, ReturnType, func, call, 2, __VA_ARGS__) \
2017  FMT_WRAP(Char, ReturnType, func, call, 3, __VA_ARGS__) \
2018  FMT_WRAP(Char, ReturnType, func, call, 4, __VA_ARGS__) \
2019  FMT_WRAP(Char, ReturnType, func, call, 5, __VA_ARGS__) \
2020  FMT_WRAP(Char, ReturnType, func, call, 6, __VA_ARGS__) \
2021  FMT_WRAP(Char, ReturnType, func, call, 7, __VA_ARGS__) \
2022  FMT_WRAP(Char, ReturnType, func, call, 8, __VA_ARGS__) \
2023  FMT_WRAP(Char, ReturnType, func, call, 9, __VA_ARGS__) \
2024  FMT_WRAP(Char, ReturnType, func, call, 10, __VA_ARGS__)
2025 #endif // FMT_USE_VARIADIC_TEMPLATES
2026 
2027 /**
2028  \rst
2029  Defines a variadic function with the specified return type, function name
2030  and argument types passed as variable arguments to this macro.
2031 
2032  **Example**::
2033 
2034  void print_error(const char *file, int line, const char *format,
2035  const fmt::ArgList &args) {
2036  fmt::print("{}: {}: ", file, line);
2037  fmt::print(format, args);
2038  }
2039  FMT_VARIADIC(void, print_error, const char *, int, const char *)
2040 
2041  ``FMT_VARIADIC`` is used for compatibility with legacy C++ compilers that
2042  don't implement variadic templates. You don't have to use this macro if
2043  you don't need legacy compiler support and can use variadic templates
2044  directly::
2045 
2046  template<typename... Args>
2047  void print_error(const char *file, int line, const char *format,
2048  const Args & ... args) {
2049  fmt::print("{}: {}: ", file, line);
2050  fmt::print(format, args...);
2051  }
2052  \endrst
2053  */
2054 #define FMT_VARIADIC(ReturnType, func, ...) \
2055  FMT_VARIADIC_(char, ReturnType, func, return func, __VA_ARGS__)
2056 
2057 #define FMT_VARIADIC_W(ReturnType, func, ...) \
2058  FMT_VARIADIC_(wchar_t, ReturnType, func, return func, __VA_ARGS__)
2059 
2060 namespace fmt {
2061 FMT_VARIADIC(std::string, format, StringRef)
2062 FMT_VARIADIC_W(std::wstring, format, WStringRef)
2064 FMT_VARIADIC(void, print, std::FILE *, StringRef)
2065 FMT_VARIADIC(void, print, std::ostream &, StringRef)
2067 FMT_VARIADIC(std::string, sprintf, StringRef)
2069 FMT_VARIADIC(int, fprintf, std::FILE *, StringRef)
2070 }
2071 
2072 // Restore warnings.
2073 #if FMT_GCC_VERSION >= 406
2074 # pragma GCC diagnostic pop
2075 #endif
2076 
2077 #endif // FMT_FORMAT_H_
CharPtr prepare_int_buffer(unsigned num_digits, const EmptySpec &, const char *prefix, unsigned prefix_size)
Definition: format.h:1338
MakeArg(bool value)
Definition: format.h:658
Definition: format.h:863
AlignTypeSpec(unsigned width, wchar_t fill)
Definition: format.h:975
MakeArg(unsigned char value)
Definition: format.h:689
static void format_custom_arg(void *formatter, const void *arg, const void *format_str)
Definition: format.h:650
uint64_t Type
Definition: format.h:397
#define FMT_GCC_EXTENSION
Definition: format.h:61
MakeArg(wchar_t *value)
Definition: format.h:701
FormatInt(unsigned long value)
Definition: format.h:1905
void clear()
Definition: format.h:315
Definition: format.h:429
void format_system_error(fmt::Writer &out, int error_code, fmt::StringRef message)
Definition: format.cpp:458
void printf(BasicWriter< Char > &w, BasicStringRef< Char > format, const ArgList &args)
Definition: format.h:1813
Definition: format.h:929
#define FMT_NOEXCEPT(expr)
Definition: format.h:110
int precision() const
Definition: format.h:992
MakeArg(signed char value)
Definition: format.h:688
FormatError(const std::string &message)
Definition: format.h:223
bool is_negative(T value)
Definition: format.h:415
int safe_strerror(int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT(true)
Definition: format.cpp:430
Alignment align_
Definition: format.h:962
Definition: format.h:924
void print(std::FILE *f, StringRef format_str, const ArgList &args)
Prints formatted data to the file f.
Definition: format.cpp:1261
MakeArg()
Definition: format.h:657
#define FMT_VARIADIC(ReturnType, func,...)
Defines a variadic function with the specified return type, function name and argument types passed a...
Definition: format.h:2054
ArgList args_
Definition: format.h:865
MakeArg(short value)
Definition: format.h:659
Alignment align() const
Definition: format.h:939
friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:210
int precision() const
Definition: format.h:969
Definition: format.h:929
IntFormatSpec< int, AlignTypeSpec< TYPE_CODE >, Char > pad(int value, unsigned width, Char fill= ' ')
Returns an integer format specifier to pad the formatted argument with the fill character to the spec...
wchar_t fill_
Definition: format.h:952
IntFormatSpec< int, TypeSpec<'x'> > hex(int value)
Returns an integer format specifier to format the value in base 16 using lower-case letters for the d...
long double long_double_value
Definition: format.h:613
void set_string(WStringRef str)
Definition: format.h:641
Result visit_wstring(Arg::StringValue< wchar_t >)
Definition: format.h:783
Definition: format.h:1746
T Type
Definition: format.h:429
void format(BasicStringRef< Char > format_str, const ArgList &args)
Definition: format.cpp:1224
IntFormatSpec< int, TypeSpec<'o'> > oct(int value)
Returns an integer format specifier to format the value in base 8.
Fast integer formatter.
Definition: format.h:1860
void reserve(std::size_t capacity)
Definition: format.h:310
WidthSpec(unsigned width, wchar_t fill)
Definition: format.h:954
void free()
Definition: format.h:257
Definition: format.h:247
Definition: format.h:420
T * make_ptr(T *ptr, std::size_t)
Definition: format.h:241
internal::Array< Char, internal::INLINE_BUFFER_SIZE > buffer_
Definition: format.h:1315
Definition: format.h:583
MakeArg(const char *value)
Definition: format.h:697
const Char * data() const
Returns a pointer to the output buffer content.
Definition: format.h:1408
const char * c_str() const
Returns a pointer to the output buffer content with terminating null character appended.
Definition: format.h:1923
std::size_t size() const
Returns the list size (the number of arguments).
Definition: format.h:849
int next_arg_index_
Definition: format.h:866
const uint32_t POWERS_OF_10_32[]
Definition: format.cpp:365
double double_value
Definition: format.h:612
MakeArg(char value)
Definition: format.h:690
const T & operator[](std::size_t index) const
Definition: format.h:327
static CStr prefix
Definition: DllLoader.cpp:46
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:419
char Char
Definition: format.h:1256
Definition: format.h:1746
static wchar_t convert(char value)
Definition: format.h:384
StringValue< char > string
Definition: format.h:615
Definition: format.h:589
const char DIGITS[]
Definition: format.cpp:347
BasicWriter & operator<<(unsigned long value)
Definition: format.h:1467
#define FMT_DISPATCH(call)
Definition: format.h:722
FormatInt(ULongLong value)
Definition: format.h:1906
void set_string(StringRef str)
Definition: format.h:635
unsigned width_
Definition: format.h:949
int INT
Definition: wgl.h:53
IntFormatSpec(T value, const SpecT &spec=SpecT())
Definition: format.h:1003
const Char * c_str() const
Returns the pointer to a C string.
Definition: format.h:197
A formatting error such as invalid format string.
Definition: format.h:221
const T * str_
Definition: format.h:1013
FMT_GCC_EXTENSION typedef unsigned long long ULongLong
Definition: format.h:124
Result visit_long_long(LongLong value)
Definition: format.h:752
char * str_
Definition: format.h:1866
BasicWriter()
Constructs a BasicWriter object.
Definition: format.h:1380
Definition: format.cpp:509
MakeArg(unsigned short value)
Definition: format.h:660
Definition: format.h:1746
Definition: unique_range.h:196
Definition: format.h:394
Alignment align() const
Definition: format.h:967
Definition: format.h:924
std::size_t capacity() const
Definition: format.h:300
FormatFunc format
Definition: format.h:604
void write(BasicStringRef< Char > format, const ArgList &args)
Writes formatted data.
Definition: format.h:1453
std::basic_string< Char > str() const
Returns the content of the output buffer as an std::string.
Definition: format.h:1424
bool flag(unsigned) const
Definition: format.h:942
LongLong long_long_value
Definition: format.h:610
BasicWriter & operator<<(ULongLong value)
Formats value and writes it to the stream.
Definition: format.h:1477
Definition: format.h:982
MakeArg(const std::wstring &value)
Definition: format.h:703
BasicWriter & operator<<(double value)
Definition: format.h:1481
static wchar_t convert(wchar_t value)
Definition: format.h:385
#define STRING(id)
uint32_t Type
Definition: format.h:394
#define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U)
Definition: format.h:431
int int_value
Definition: format.h:608
MakeArg(unsigned long value)
Definition: format.h:674
FormatInt(unsigned value)
Definition: format.h:1904
Result visit_string(Arg::StringValue< char >)
Definition: format.h:780
Result visit_ulong_long(ULongLong value)
Definition: format.h:758
Definition: format.h:998
Definition: format.h:1011
char CHAR
Definition: wgl.h:60
FormatSpec(unsigned width=0, char type=0, wchar_t fill= ' ')
Definition: format.h:987
void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT(true)
Definition: format.cpp:1247
bool flag(unsigned f) const
Definition: format.h:991
Definition: format.h:573
Definition: format.h:137
Type
Definition: format.h:584
Result visit_uint(unsigned value)
Definition: format.h:755
Definition: format.h:924
Result visit_char(int value)
Definition: format.h:761
std::size_t size() const
Returns the number of characters written to the output buffer.
Definition: format.h:1911
MakeArg(long double value)
Definition: format.h:687
wchar_t fill() const
Definition: format.h:957
#define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type)
Definition: format.h:1210
BasicStringRef< wchar_t > WStringRef
Definition: format.h:216
void format(BasicFormatter< Char > &f, const Char *format_str, const T &value)
Definition: format.h:1705
unsigned width() const
Definition: format.h:940
Definition: format.h:974
Definition: format.h:360
unsigned long long uint64_t
Definition: wposix_types.h:57
BasicStringRef< char > StringRef
Definition: format.h:215
MakeArg(char *value)
Definition: format.h:696
std::size_t capacity_
Definition: format.h:250
ArgList(const internal::Arg *args, std::size_t size)
Definition: format.h:843
Result visit(const Arg &arg)
Definition: format.h:793
std::size_t size_
Definition: format.h:249
const wchar_t * UnsupportedStrType
Definition: format.h:369
BasicWriter & operator<<(long value)
Definition: format.h:1464
void print_colored(Color c, StringRef format, const ArgList &args)
Formats a string and prints it to stdout using ANSI escape sequences to specify color (experimental)...
Definition: format.cpp:1273
Alignment
Definition: format.h:923
void resize(std::size_t new_size)
Definition: format.h:303
const char * error_
Definition: format.h:867
MakeArg(float value)
Definition: format.h:685
Definition: format.h:1746
RuntimeError()
Definition: format.h:826
CharPtr grow_buffer(std::size_t n)
Definition: format.h:1331
CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec)
Definition: format.h:1537
T value_
Definition: format.h:1000
Definition: format.h:594
IntFormatSpec< int, TypeSpec<'X'> > hexu(int value)
Returns an integer formatter format specifier to format in base 16 using upper-case letters for the d...
BasicWriter & operator<<(LongLong value)
Definition: format.h:1470
An argument list.
Definition: format.h:836
SystemError()
Definition: format.h:1258
ULongLong ulong_long_value
Definition: format.h:611
const Char * start_
Definition: format.h:905
Result visit_double(double value)
Definition: format.h:769
const void * value
Definition: format.h:603
friend bool operator==(BasicStringRef lhs, BasicStringRef rhs)
Definition: format.h:207
Definition: format.h:886
BasicWriter< char > Writer
Definition: format.h:131
std::size_t size() const
Definition: format.h:297
char type() const
Definition: format.h:993
Result visit_custom(Arg::CustomValue)
Definition: format.h:789
void FormatSigned(LongLong value)
Definition: format.h:1890
MakeArg(const void *value)
Definition: format.h:707
std::size_t size() const
Returns the total number of characters written.
Definition: format.h:1402
ArgList()
Definition: format.h:842
void move(Array &other)
Definition: format.h:262
Config::Array_type Array
Definition: json_spirit_value.h:184
BasicWriter< Char > & writer_
Definition: format.h:904
BasicWriter & operator<<(char value)
Writes a character to the stream.
Definition: format.h:1498
const char * data() const
Returns a pointer to the output buffer content.
Definition: format.h:1917
#define TYPE(T)
Definition: GUIutil.cpp:419
Definition: format.h:401
const char * UnsupportedStrType
Definition: format.h:382
unsigned uint_value
Definition: format.h:609
The MIT License free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
Definition: LICENSE.txt:8
const internal::Arg & operator[](std::size_t index) const
Returns the argument at specified index.
Definition: format.h:854
internal::FormatErrorReporter< Char > report_error_
Definition: format.h:906
std::size_t size() const
Returns the string size.
Definition: format.h:202
#define T(string_literal)
Definition: secure_crt.cpp:76
const internal::Arg * args_
Definition: format.h:838
#define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: format.h:115
Result visit_any_double(T)
Definition: format.h:776
Definition: format.h:1746
Definition: format.h:934
void(* FormatFunc)(fmt::Writer &, int, fmt::StringRef)
Definition: format.cpp:146
Char * CharPtr
Definition: format.h:355
Definition: format.h:745
BasicWriter< wchar_t > WWriter
Definition: format.h:134
BasicWriter & operator<<(wchar_t value)
Definition: format.h:1503
BasicWriter & operator<<(long double value)
Formats value using the general format for floating-point numbers (&#39;g&#39;) and writes it to the stream...
Definition: format.h:1490
internal::CharTraits< Char >::CharPtr CharPtr
Definition: format.h:1317
Definition: format.h:1746
Definition: format.h:961
CustomValue custom
Definition: format.h:617
Definition: format.h:924
MakeArg(unsigned value)
Definition: format.h:662
Color
Definition: format.h:1746
T * ptr_
Definition: format.h:251
StringValue< wchar_t > wstring
Definition: format.h:616
Definition: format.h:588
FormatInt(long value)
Definition: format.h:1902
char type() const
Definition: format.h:943
Definition: format.h:938
void report_unknown_type(char code, const char *type)
Definition: format.cpp:375
~Array()
Definition: format.h:281
static char convert(char value)
Definition: format.h:371
Result visit_long_double(long double value)
Definition: format.h:772
BasicFormatter(BasicWriter< Char > &w)
Definition: format.h:914
FormatInt(LongLong value)
Definition: format.h:1903
const void * pointer_value
Definition: format.h:614
BasicWriter & operator<<(unsigned value)
Definition: format.h:1461
char * format_decimal(ULongLong value)
Definition: format.h:1869
std::string sprintf(StringRef format, const ArgList &args)
Formats arguments and returns the result as a string.
Definition: format.h:1827
Definition: format.h:929
unsigned short wchar_t
Definition: wgl.h:78
unsigned count_digits(uint64_t n)
Definition: format.h:465
T value() const
Definition: format.h:1006
int precision() const
Definition: format.h:941
This template provides operations for formatting and writing data into a character stream...
Definition: format.h:131
MakeArg(double value)
Definition: format.h:686
unsigned int uint32_t
Definition: wposix_types.h:53
Result visit_int(int value)
Definition: format.h:749
Array(std::size_t size=0)
Definition: format.h:279
FormatterBase()
Definition: format.h:869
BasicStringRef(const Char *s, std::size_t size=0)
Constructs a string reference object from a C string and a size.
Definition: format.h:179
static bool is_negative(T value)
Definition: format.h:409
Result visit_any_int(T)
Definition: format.h:765
void write(OutputCB &output, const T &data)
Outputs a structure, using sizeof to get the size.
Definition: CommonConvert.h:128
Definition: format.h:929
void write(BasicWriter< Char > &w, const Char *start, const Char *end)
Definition: format.h:876
int num_open_braces
Definition: format.h:566
Definition: format.h:1746
BasicStringRef(const std::basic_string< Char > &s)
Constructs a string reference from an std::string object.
Definition: format.h:184
char fill() const
Definition: format.h:944
Definition: format.h:1746
MakeArg(int value)
Definition: format.h:661
MakeArg(long value)
Definition: format.h:663
IntFormatSpec< int, TypeSpec<'b'> > bin(int value)
Returns an integer format specifier to format the value in base 2.
unsigned int UINT
Definition: wgl.h:54
Definition: format.h:350
MakeArg(const std::string &value)
Definition: format.h:698
#define FMT_DEFINE_INT_FORMATTERS(TYPE)
Definition: format.h:1062
A string reference.
Definition: format.h:168
void push_back(const T &value)
Definition: format.h:317
void write_int(T value, const Spec &spec)
Definition: format.h:1623
Result visit_unhandled_arg()
Definition: format.h:747
std::size_t size_
Definition: format.h:839
Definition: format.h:119
bool flag(unsigned) const
Definition: format.h:977
unsigned flags_
Definition: format.h:983
StrFormatSpec(const T *str, unsigned width, wchar_t fill)
Definition: format.h:1016
std::string str() const
Returns the content of the output buffer as an std::string.
Definition: format.h:1931
Definition: format.h:948
Definition: format.h:586
MakeArg(const wchar_t *value)
Definition: format.h:702
Type type
Definition: format.h:591
An error returned by an operating system or a language runtime, for example a file opening error...
Definition: format.h:1249
Definition: format.h:623
const T * str() const
Definition: format.h:1019
int precision_
Definition: format.h:984
AlignSpec(unsigned width, wchar_t fill, Alignment align=ALIGN_DEFAULT)
Definition: format.h:964
unsigned width() const
Definition: format.h:956
int fprintf(std::FILE *f, StringRef format, const ArgList &args)
Prints formatted data to the file f.
Definition: format.cpp:1281
const Char * c_str() const
Returns a pointer to the output buffer content with terminating null character appended.
Definition: format.h:1414
void format_decimal(Char *buffer, UInt value, unsigned num_digits)
Definition: format.h:485
MakeArg(WStringRef value)
Definition: format.h:704
MakeArg(ULongLong value)
Definition: format.h:684
Definition: format.h:924
Definition: format.h:930
#define FMT_VARIADIC_VOID(func, arg_type)
Definition: format.h:1195
const uint64_t POWERS_OF_10_64[]
Definition: format.cpp:366
MakeArg(LongLong value)
Definition: format.h:683
char type() const
Definition: format.h:978
void init(ScriptInterface &scriptInterface)
Definition: JSInterface_GUITypes.cpp:260
Result visit_pointer(const void *)
Definition: format.h:786
void clear()
Definition: format.h:1532
SystemError(int error_code, StringRef message)
Constructs a :cpp:class:fmt::SystemError object with the description of the form "*<message>*: *<syst...
Definition: format.h:1270
#define FMT_VARIADIC_W(ReturnType, func,...)
Definition: format.h:2057
MakeArg(StringRef value)
Definition: format.h:699
MakeArg(void *value)
Definition: format.h:706
MakeArg(wchar_t value)
Definition: format.h:691
const Char * value
Definition: format.h:595
BasicWriter< Char > & writer()
Definition: format.h:916
void format_decimal(char *&buffer, T value)
Definition: format.h:1938
T data_[SIZE]
Definition: format.h:252
FormatInt(int value)
Definition: format.h:1901
T & operator[](std::size_t index)
Definition: format.h:326
Definition: format.h:602
int error_code_
Definition: format.h:1254
std::size_t size
Definition: format.h:596
Definition: format.h:231
FMT_GCC_EXTENSION typedef long long LongLong
Definition: format.h:123
const Char * data_
Definition: format.h:170
char type_
Definition: format.h:985
Definition: format.h:824
Definition: format.h:565
static bool is_negative(T)
Definition: format.h:403
std::size_t size_
Definition: format.h:171