31 #define FMT_HEADER_ONLY // Added by diy for header-only usage
46 #define FMT_VERSION 40000
49 # define FMT_SECURE_SCL _SECURE_SCL
51 # define FMT_SECURE_SCL 0
59 # define FMT_MSC_VER _MSC_VER
61 # define FMT_MSC_VER 0
64 #if FMT_MSC_VER && FMT_MSC_VER <= 1500
65 typedef unsigned __int32 uint32_t;
66 typedef unsigned __int64 uint64_t;
67 typedef __int64 intmax_t;
72 #if !defined(FMT_HEADER_ONLY) && defined(_WIN32)
74 # define FMT_API __declspec(dllexport)
75 # elif defined(FMT_SHARED)
76 # define FMT_API __declspec(dllimport)
84 # define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
85 # define FMT_GCC_EXTENSION __extension__
86 # if FMT_GCC_VERSION >= 406
87 # pragma GCC diagnostic push
90 # pragma GCC diagnostic ignored "-Wlong-long"
93 # pragma GCC diagnostic ignored "-Wshadow"
96 # pragma GCC diagnostic ignored "-Wsign-conversion"
98 # if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__
99 # define FMT_HAS_GXX_CXX11 1
102 # define FMT_GCC_EXTENSION
105 #if defined(__INTEL_COMPILER)
106 # define FMT_ICC_VERSION __INTEL_COMPILER
108 # define FMT_ICC_VERSION __ICL
111 #if defined(__clang__) && !defined(FMT_ICC_VERSION)
112 # define FMT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__)
113 # pragma clang diagnostic push
114 # pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
115 # pragma clang diagnostic ignored "-Wpadded"
118 #ifdef __GNUC_LIBSTD__
119 # define FMT_GNUC_LIBSTD_VERSION (__GNUC_LIBSTD__ * 100 + __GNUC_LIBSTD_MINOR__)
123 # define FMT_HAS_FEATURE(x) __has_feature(x)
125 # define FMT_HAS_FEATURE(x) 0
129 # define FMT_HAS_BUILTIN(x) __has_builtin(x)
131 # define FMT_HAS_BUILTIN(x) 0
134 #ifdef __has_cpp_attribute
135 # define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
137 # define FMT_HAS_CPP_ATTRIBUTE(x) 0
140 #ifndef FMT_USE_VARIADIC_TEMPLATES
144 # define FMT_USE_VARIADIC_TEMPLATES \
145 (FMT_HAS_FEATURE(cxx_variadic_templates) || \
146 (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800)
149 #ifndef FMT_USE_RVALUE_REFERENCES
152 # if defined(FMT_GNUC_LIBSTD_VERSION) && FMT_GNUC_LIBSTD_VERSION <= 402
153 # define FMT_USE_RVALUE_REFERENCES 0
155 # define FMT_USE_RVALUE_REFERENCES \
156 (FMT_HAS_FEATURE(cxx_rvalue_references) || \
157 (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1600)
162 #if defined(__GNUC__) && !defined(__EXCEPTIONS)
163 # define FMT_EXCEPTIONS 0
165 #if FMT_MSC_VER && !_HAS_EXCEPTIONS
166 # define FMT_EXCEPTIONS 0
168 #ifndef FMT_EXCEPTIONS
169 # define FMT_EXCEPTIONS 1
174 # define FMT_THROW(x) throw x
176 # define FMT_THROW(x) assert(false)
181 #ifndef FMT_USE_NOEXCEPT
182 # define FMT_USE_NOEXCEPT 0
185 #if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \
186 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
188 # define FMT_DETECTED_NOEXCEPT noexcept
190 # define FMT_DETECTED_NOEXCEPT throw()
195 # define FMT_NOEXCEPT FMT_DETECTED_NOEXCEPT
197 # define FMT_NOEXCEPT
204 # define FMT_DTOR_NOEXCEPT FMT_DETECTED_NOEXCEPT
206 # define FMT_DTOR_NOEXCEPT FMT_NOEXCEPT
210 # if (defined(FMT_USE_OVERRIDE) && FMT_USE_OVERRIDE) || FMT_HAS_FEATURE(cxx_override) || \
211 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
213 # define FMT_OVERRIDE override
215 # define FMT_OVERRIDE
220 # if FMT_HAS_FEATURE(cxx_nullptr) || \
221 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
223 # define FMT_NULL nullptr
225 # define FMT_NULL NULL
231 #ifndef FMT_USE_DELETED_FUNCTIONS
232 # define FMT_USE_DELETED_FUNCTIONS 0
235 #if FMT_USE_DELETED_FUNCTIONS || FMT_HAS_FEATURE(cxx_deleted_functions) || \
236 (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800
237 # define FMT_DELETED_OR_UNDEFINED = delete
238 # define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
239 TypeName(const TypeName&) = delete; \
240 TypeName& operator=(const TypeName&) = delete
242 # define FMT_DELETED_OR_UNDEFINED
243 # define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
244 TypeName(const TypeName&); \
245 TypeName& operator=(const TypeName&)
248 #ifndef FMT_USE_DEFAULTED_FUNCTIONS
249 # define FMT_USE_DEFAULTED_FUNCTIONS 0
252 #ifndef FMT_DEFAULTED_COPY_CTOR
253 # if FMT_USE_DEFAULTED_FUNCTIONS || FMT_HAS_FEATURE(cxx_defaulted_functions) || \
254 (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1800
255 # define FMT_DEFAULTED_COPY_CTOR(TypeName) \
256 TypeName(const TypeName&) = default;
258 # define FMT_DEFAULTED_COPY_CTOR(TypeName)
262 #ifndef FMT_USE_USER_DEFINED_LITERALS
267 # define FMT_USE_USER_DEFINED_LITERALS \
268 FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES && \
269 (FMT_HAS_FEATURE(cxx_user_literals) || \
270 (FMT_GCC_VERSION >= 407 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900) && \
271 (!defined(FMT_ICC_VERSION) || FMT_ICC_VERSION >= 1500)
274 #ifndef FMT_USE_EXTERN_TEMPLATES
275 # define FMT_USE_EXTERN_TEMPLATES \
276 (FMT_CLANG_VERSION >= 209 || (FMT_GCC_VERSION >= 303 && FMT_HAS_GXX_CXX11))
279 #ifdef FMT_HEADER_ONLY
281 # undef FMT_USE_EXTERN_TEMPLATES
282 # define FMT_USE_EXTERN_TEMPLATES 0
286 # define FMT_ASSERT(condition, message) assert((condition) && message)
292 # if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz)
293 # define FMT_BUILTIN_CLZ(n) __builtin_clz(n)
296 # if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clzll)
297 # define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n)
305 #if FMT_MSC_VER && !defined(FMT_BUILTIN_CLZLL) && !defined(_MANAGED)
310 # pragma intrinsic(_BitScanReverse)
311 inline uint32_t clz(uint32_t x) {
313 _BitScanReverse(&r, x);
319 # pragma warning(suppress: 6102)
322 # define FMT_BUILTIN_CLZ(n) fmt::internal::clz(n)
325 # pragma intrinsic(_BitScanReverse64)
328 inline uint32_t clzll(uint64_t x) {
331 _BitScanReverse64(&r, x);
334 if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32)))
335 return 63 - (r + 32);
338 _BitScanReverse(&r, static_cast<uint32_t>(x));
345 # pragma warning(suppress: 6102)
348 # define FMT_BUILTIN_CLZLL(n) fmt::internal::clzll(n)
357 operator int()
const {
return 0; }
364 inline DummyInt _ecvt_s(...) {
return DummyInt(); }
365 inline DummyInt isinf(...) {
return DummyInt(); }
366 inline DummyInt _finite(...) {
return DummyInt(); }
367 inline DummyInt isnan(...) {
return DummyInt(); }
368 inline DummyInt _isnan(...) {
return DummyInt(); }
372 template <
typename T>
373 inline T const_check(T value) {
return value; }
383 class numeric_limits<fmt::internal::DummyInt> :
384 public std::numeric_limits<int> {
387 template <
typename T>
388 static bool isinfinity(T x) {
389 using namespace fmt::internal;
392 if (const_check(
sizeof(isinf(x)) ==
sizeof(
bool) ||
393 sizeof(isinf(x)) ==
sizeof(
int))) {
394 return isinf(x) != 0;
396 return !_finite(static_cast<double>(x));
400 template <
typename T>
401 static bool isnotanumber(T x) {
402 using namespace fmt::internal;
403 if (const_check(
sizeof(isnan(x)) ==
sizeof(
bool) ||
404 sizeof(isnan(x)) ==
sizeof(
int))) {
405 return isnan(x) != 0;
407 return _isnan(static_cast<double>(x)) != 0;
411 static bool isnegative(
double x) {
412 using namespace fmt::internal;
413 if (const_check(
sizeof(signbit(x)) ==
sizeof(
bool) ||
414 sizeof(signbit(x)) ==
sizeof(
int))) {
415 return signbit(x) != 0;
417 if (x < 0)
return true;
418 if (!isnotanumber(x))
return false;
419 int dec = 0, sign = 0;
421 _ecvt_s(buffer,
sizeof(buffer), x, 0, &dec, &sign);
431 FMT_GCC_EXTENSION
typedef long long LongLong;
432 FMT_GCC_EXTENSION
typedef unsigned long long ULongLong;
434 #if FMT_USE_RVALUE_REFERENCES
438 template <
typename Char>
444 template <
typename Char>
449 template <
typename Impl,
typename Char,
typename Spec = fmt::FormatSpec>
452 template <
typename CharType,
481 template <
typename Char>
498 : data_(s), size_(std::char_traits<Char>::length(s)) {}
505 template <
typename Allocator>
507 const std::basic_string<Char, std::char_traits<Char>, Allocator> &s)
508 : data_(s.c_str()), size_(s.size()) {}
516 return std::basic_string<Char>(data_, size_);
520 const Char *
data()
const {
return data_; }
523 std::size_t
size()
const {
return size_; }
527 std::size_t size = size_ < other.size_ ? size_ : other.size_;
528 int result = std::char_traits<Char>::compare(data_, other.data_, size);
530 result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1);
534 friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) {
535 return lhs.compare(rhs) == 0;
537 friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs) {
538 return lhs.compare(rhs) != 0;
540 friend bool operator<(BasicStringRef lhs, BasicStringRef rhs) {
541 return lhs.compare(rhs) < 0;
543 friend bool operator<=(BasicStringRef lhs, BasicStringRef rhs) {
544 return lhs.compare(rhs) <= 0;
546 friend bool operator>(BasicStringRef lhs, BasicStringRef rhs) {
547 return lhs.compare(rhs) > 0;
549 friend bool operator>=(BasicStringRef lhs, BasicStringRef rhs) {
550 return lhs.compare(rhs) >= 0;
554 typedef BasicStringRef<char> StringRef;
555 typedef BasicStringRef<wchar_t> WStringRef;
582 template <
typename Char>
596 template <
typename Allocator>
598 const std::basic_string<Char, std::char_traits<Char>, Allocator> &s)
599 : data_(s.c_str()) {}
602 const Char *
c_str()
const {
return data_; }
605 typedef BasicCStringRef<char> CStringRef;
606 typedef BasicCStringRef<wchar_t> WCStringRef;
612 : std::runtime_error(message.
c_str()) {}
620 template <
typename T>
623 #define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U) \
625 struct MakeUnsigned<T> { typedef U Type; }
627 FMT_SPECIALIZE_MAKE_UNSIGNED(
char,
unsigned char);
628 FMT_SPECIALIZE_MAKE_UNSIGNED(
signed char,
unsigned char);
629 FMT_SPECIALIZE_MAKE_UNSIGNED(
short,
unsigned short);
630 FMT_SPECIALIZE_MAKE_UNSIGNED(
int,
unsigned);
631 FMT_SPECIALIZE_MAKE_UNSIGNED(
long,
unsigned long);
632 FMT_SPECIALIZE_MAKE_UNSIGNED(LongLong, ULongLong);
635 template <
typename Int>
636 inline typename MakeUnsigned<Int>::Type to_unsigned(Int value) {
637 FMT_ASSERT(value >= 0,
"negative value");
638 return static_cast<typename MakeUnsigned<Int>::Type
>(value);
643 enum { INLINE_BUFFER_SIZE = 500 };
647 template <
typename T>
648 inline stdext::checked_array_iterator<T*> make_ptr(T *ptr, std::size_t size) {
649 return stdext::checked_array_iterator<T*>(ptr, size);
652 template <
typename T>
653 inline T *make_ptr(T *ptr, std::size_t) {
return ptr; }
662 template <
typename T>
665 FMT_DISALLOW_COPY_AND_ASSIGN(
Buffer);
670 std::size_t capacity_;
672 Buffer(T *ptr = FMT_NULL, std::size_t capacity = 0)
673 : ptr_(ptr), size_(0), capacity_(capacity) {}
681 virtual void grow(std::size_t size) = 0;
687 std::size_t
size()
const {
return size_; }
696 if (new_size > capacity_)
707 if (capacity > capacity_)
711 void clear() FMT_NOEXCEPT { size_ = 0; }
713 void push_back(
const T &value) {
714 if (size_ == capacity_)
716 ptr_[size_++] = value;
720 template <
typename U>
721 void append(
const U *begin,
const U *end);
723 T &operator[](std::size_t index) {
return ptr_[index]; }
724 const T &operator[](std::size_t index)
const {
return ptr_[index]; }
727 template <
typename T>
728 template <
typename U>
730 FMT_ASSERT(end >= begin,
"negative value");
731 std::size_t new_size = size_ + (end - begin);
732 if (new_size > capacity_)
734 std::uninitialized_copy(begin, end,
735 internal::make_ptr(ptr_, capacity_) + size_);
743 template <
typename T, std::
size_t SIZE,
typename Allocator = std::allocator<T> >
750 if (this->ptr_ != data_) Allocator::deallocate(this->ptr_, this->capacity_);
754 void grow(std::size_t size) FMT_OVERRIDE;
757 explicit MemoryBuffer(
const Allocator &alloc = Allocator())
758 : Allocator(alloc),
Buffer<T>(data_, SIZE) {}
761 #if FMT_USE_RVALUE_REFERENCES
765 Allocator &this_alloc = *
this, &other_alloc = other;
766 this_alloc = std::move(other_alloc);
767 this->size_ = other.size_;
768 this->capacity_ = other.capacity_;
769 if (other.ptr_ == other.data_) {
771 std::uninitialized_copy(other.data_, other.data_ + this->size_,
772 make_ptr(data_, this->capacity_));
774 this->ptr_ = other.ptr_;
777 other.ptr_ = other.data_;
787 assert(
this != &other);
795 Allocator get_allocator()
const {
return *
this; }
798 template <
typename T, std::
size_t SIZE,
typename Allocator>
800 std::size_t new_capacity = this->capacity_ + this->capacity_ / 2;
801 if (size > new_capacity)
803 T *new_ptr = this->allocate(new_capacity, FMT_NULL);
805 std::uninitialized_copy(this->ptr_, this->ptr_ + this->size_,
806 make_ptr(new_ptr, new_capacity));
807 std::size_t old_capacity = this->capacity_;
808 T *old_ptr = this->ptr_;
809 this->capacity_ = new_capacity;
810 this->ptr_ = new_ptr;
814 if (old_ptr != data_)
815 Allocator::deallocate(old_ptr, old_capacity);
819 template <
typename Char>
825 FMT_API
void grow(std::size_t size) FMT_OVERRIDE;
828 template <
typename Char>
832 typedef stdext::checked_array_iterator<Char*> CharPtr;
834 typedef Char *CharPtr;
836 static Char cast(
int value) {
return static_cast<Char
>(value); }
839 template <
typename Char>
846 static char convert(
wchar_t);
849 static char convert(
char value) {
return value; }
852 template <
typename T>
853 FMT_API
static int format_float(
char *buffer, std::size_t size,
854 const char *format,
unsigned width,
int precision, T value);
857 #if FMT_USE_EXTERN_TEMPLATES
859 (
char *buffer, std::size_t size,
860 const char* format,
unsigned width,
int precision,
double value);
862 (
char *buffer, std::size_t size,
863 const char* format,
unsigned width,
int precision,
long double value);
869 static wchar_t convert(
char value) {
return value; }
870 static wchar_t convert(
wchar_t value) {
return value; }
872 template <
typename T>
873 FMT_API
static int format_float(
wchar_t *buffer, std::size_t size,
874 const wchar_t *format,
unsigned width,
int precision, T value);
877 #if FMT_USE_EXTERN_TEMPLATES
879 (
wchar_t *buffer, std::size_t size,
880 const wchar_t* format,
unsigned width,
int precision,
double value);
882 (
wchar_t *buffer, std::size_t size,
883 const wchar_t* format,
unsigned width,
int precision,
long double value);
887 template <
bool IsSigned>
889 template <
typename T>
890 static bool is_negative(T value) {
return value < 0; }
895 template <
typename T>
896 static bool is_negative(T) {
return false; }
901 template <
typename T>
902 inline bool is_negative(T value) {
907 template <
bool FitsIn32Bits>
913 template <
typename T>
921 FMT_API
void report_unknown_type(
char code,
const char *type);
925 template <
typename T =
void>
927 static const uint32_t POWERS_OF_10_32[];
928 static const uint64_t POWERS_OF_10_64[];
929 static const char DIGITS[];
932 #if FMT_USE_EXTERN_TEMPLATES
938 #ifdef FMT_BUILTIN_CLZLL
941 inline unsigned count_digits(uint64_t n) {
944 int t = (64 - FMT_BUILTIN_CLZLL(n | 1)) * 1233 >> 12;
945 return to_unsigned(t) - (n < Data::POWERS_OF_10_64[t]) + 1;
949 inline unsigned count_digits(uint64_t n) {
955 if (n < 10)
return count;
956 if (n < 100)
return count + 1;
957 if (n < 1000)
return count + 2;
958 if (n < 10000)
return count + 3;
965 #ifdef FMT_BUILTIN_CLZ
967 inline unsigned count_digits(uint32_t n) {
968 int t = (32 - FMT_BUILTIN_CLZ(n | 1)) * 1233 >> 12;
969 return to_unsigned(t) - (n < Data::POWERS_OF_10_32[t]) + 1;
975 template <
typename Char>
976 void operator()(Char *) {}
985 unsigned digit_index_;
990 template <
typename Char>
991 void operator()(Char *&buffer) {
992 if (++digit_index_ % 3 != 0)
994 buffer -= sep_.
size();
995 std::uninitialized_copy(sep_.
data(), sep_.
data() + sep_.
size(),
996 internal::make_ptr(buffer, sep_.
size()));
1003 template <
typename UInt,
typename Char,
typename ThousandsSep>
1004 inline void format_decimal(Char *buffer, UInt value,
unsigned num_digits,
1006 buffer += num_digits;
1007 while (value >= 100) {
1011 unsigned index =
static_cast<unsigned>((value % 100) * 2);
1013 *--buffer = Data::DIGITS[index + 1];
1014 thousands_sep(buffer);
1015 *--buffer = Data::DIGITS[index];
1016 thousands_sep(buffer);
1019 *--buffer =
static_cast<char>(
'0' + value);
1022 unsigned index =
static_cast<unsigned>(value * 2);
1023 *--buffer = Data::DIGITS[index + 1];
1024 thousands_sep(buffer);
1025 *--buffer = Data::DIGITS[index];
1028 template <
typename UInt,
typename Char>
1029 inline void format_decimal(Char *buffer, UInt value,
unsigned num_digits) {
1035 # define FMT_USE_WINDOWS_H 0
1036 #elif !defined(FMT_USE_WINDOWS_H)
1037 # define FMT_USE_WINDOWS_H 1
1042 #if FMT_USE_WINDOWS_H
1050 FMT_API
explicit UTF8ToUTF16(StringRef s);
1051 operator WStringRef()
const {
return WStringRef(&buffer_[0], size()); }
1052 size_t size()
const {
return buffer_.
size() - 1; }
1053 const wchar_t *c_str()
const {
return &buffer_[0]; }
1054 std::wstring str()
const {
return std::wstring(&buffer_[0], size()); }
1065 FMT_API
explicit UTF16ToUTF8(WStringRef s);
1066 operator StringRef()
const {
return StringRef(&buffer_[0], size()); }
1067 size_t size()
const {
return buffer_.
size() - 1; }
1068 const char *c_str()
const {
return &buffer_[0]; }
1069 std::string str()
const {
return std::string(&buffer_[0], size()); }
1074 FMT_API
int convert(WStringRef s);
1077 FMT_API
void format_windows_error(
fmt::Writer &out,
int error_code,
1083 template <
typename Char>
1089 typedef void (*FormatFunc)(
1090 void *formatter,
const void *arg,
void *format_str_ptr);
1099 unsigned uint_value;
1100 LongLong long_long_value;
1101 ULongLong ulong_long_value;
1102 double double_value;
1103 long double long_double_value;
1104 const void *pointer;
1115 INT, UINT, LONG_LONG, ULONG_LONG, BOOL, CHAR, LAST_INTEGER_TYPE = CHAR,
1117 DOUBLE, LONG_DOUBLE, LAST_NUMERIC_TYPE = LONG_DOUBLE,
1118 CSTRING, STRING, WSTRING, POINTER, CUSTOM
1128 template <
typename Char>
1130 template <
typename Char,
typename T>
1133 template <
typename T =
void>
1138 template <
typename T,
typename Char>
1141 typedef T Unsupported;
1144 template <
typename T>
1146 typedef T Supported;
1150 typedef char Yes[1];
1153 template <
typename T>
1157 Yes &convert(fmt::ULongLong);
1160 template<
typename T,
bool ENABLE_CONVERSION>
1162 enum { value = ENABLE_CONVERSION };
1165 template<
typename T,
bool ENABLE_CONVERSION>
1167 enum { value =
false };
1170 template<
typename T>
1178 template<
typename T>
1181 enable_conversion =
sizeof(fmt::internal::convert(get<T>())) ==
sizeof(Yes)
1186 #define FMT_DISABLE_CONVERSION_TO_INT(Type) \
1188 struct ConvertToInt<Type> { enum { value = 0 }; }
1191 FMT_DISABLE_CONVERSION_TO_INT(
float);
1192 FMT_DISABLE_CONVERSION_TO_INT(
double);
1193 FMT_DISABLE_CONVERSION_TO_INT(
long double);
1195 template<
bool B,
class T =
void>
1201 template<
bool B,
class T,
class F>
1204 template<
class T,
class F>
1209 struct Not {
enum { value = 0 }; };
1212 struct Not<false> {
enum { value = 1 }; };
1214 template <
typename T>
1224 template <
typename LConv>
1227 return lc->thousands_sep;
1232 #define FMT_CONCAT(a, b) a##b
1234 #if FMT_GCC_VERSION >= 303
1235 # define FMT_UNUSED __attribute__((unused))
1240 #ifndef FMT_USE_STATIC_ASSERT
1241 # define FMT_USE_STATIC_ASSERT 0
1244 #if FMT_USE_STATIC_ASSERT || FMT_HAS_FEATURE(cxx_static_assert) || \
1245 (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600
1246 # define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message)
1248 # define FMT_CONCAT_(a, b) FMT_CONCAT(a, b)
1249 # define FMT_STATIC_ASSERT(cond, message) \
1250 typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED
1253 template <
typename Formatter,
typename Char,
typename T>
1254 void format_arg(Formatter &,
const Char *,
const T &) {
1256 "Cannot format argument. To enable the use of ostream "
1257 "operator<< include fmt/ostream.h. Otherwise provide "
1258 "an overload of format_arg.");
1262 template <
typename Formatter>
1265 typedef typename Formatter::Char Char;
1273 template <
typename T>
1275 template <
typename T>
1282 #if !FMT_MSC_VER || defined(_NATIVE_WCHAR_T_DEFINED)
1283 MakeValue(
typename WCharHelper<wchar_t, Char>::Unsupported);
1285 MakeValue(
typename WCharHelper<wchar_t *, Char>::Unsupported);
1286 MakeValue(
typename WCharHelper<const wchar_t *, Char>::Unsupported);
1287 MakeValue(
typename WCharHelper<const std::wstring &, Char>::Unsupported);
1288 MakeValue(
typename WCharHelper<WStringRef, Char>::Unsupported);
1291 string.value = str.
data();
1292 string.size = str.
size();
1296 wstring.value = str.
data();
1297 wstring.size = str.
size();
1301 template <
typename T>
1302 static void format_custom_arg(
1303 void *formatter,
const void *arg,
void *format_str_ptr) {
1304 format_arg(*static_cast<Formatter*>(formatter),
1305 *static_cast<const Char**>(format_str_ptr),
1306 *static_cast<const T*>(arg));
1312 #define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \
1313 MakeValue(Type value) { field = rhs; } \
1314 static uint64_t type(Type) { return Arg::TYPE; }
1316 #define FMT_MAKE_VALUE(Type, field, TYPE) \
1317 FMT_MAKE_VALUE_(Type, field, TYPE, value)
1319 FMT_MAKE_VALUE(
bool, int_value, BOOL)
1320 FMT_MAKE_VALUE(
short, int_value, INT)
1321 FMT_MAKE_VALUE(
unsigned short, uint_value, UINT)
1322 FMT_MAKE_VALUE(
int, int_value, INT)
1323 FMT_MAKE_VALUE(
unsigned, uint_value, UINT)
1328 if (const_check(
sizeof(
long) ==
sizeof(
int)))
1329 int_value =
static_cast<int>(value);
1331 long_long_value = value;
1333 static uint64_t type(
long) {
1334 return sizeof(long) ==
sizeof(
int) ? Arg::INT : Arg::LONG_LONG;
1338 if (const_check(
sizeof(
unsigned long) ==
sizeof(
unsigned)))
1339 uint_value =
static_cast<unsigned>(value);
1341 ulong_long_value = value;
1343 static uint64_t type(
unsigned long) {
1344 return sizeof(
unsigned long) ==
sizeof(
unsigned) ?
1345 Arg::UINT : Arg::ULONG_LONG;
1348 FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG)
1349 FMT_MAKE_VALUE(ULongLong, ulong_long_value, ULONG_LONG)
1350 FMT_MAKE_VALUE(
float, double_value, DOUBLE)
1351 FMT_MAKE_VALUE(
double, double_value, DOUBLE)
1352 FMT_MAKE_VALUE(
long double, long_double_value, LONG_DOUBLE)
1353 FMT_MAKE_VALUE(
signed char, int_value, INT)
1354 FMT_MAKE_VALUE(
unsigned char, uint_value, UINT)
1355 FMT_MAKE_VALUE(
char, int_value, CHAR)
1357 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
1361 static uint64_t type(
wchar_t) {
return Arg::CHAR; }
1364 #define FMT_MAKE_STR_VALUE(Type, TYPE) \
1365 MakeValue(Type value) { set_string(value); } \
1366 static uint64_t type(Type) { return Arg::TYPE; }
1368 FMT_MAKE_VALUE(
char *,
string.value, CSTRING)
1369 FMT_MAKE_VALUE(
const char *,
string.value, CSTRING)
1370 FMT_MAKE_VALUE(
signed char *, sstring.value, CSTRING)
1371 FMT_MAKE_VALUE(
const signed char *, sstring.value, CSTRING)
1372 FMT_MAKE_VALUE(
unsigned char *, ustring.value, CSTRING)
1373 FMT_MAKE_VALUE(
const unsigned char *, ustring.value, CSTRING)
1374 FMT_MAKE_STR_VALUE(
const std::string &, STRING)
1378 #define FMT_MAKE_WSTR_VALUE(Type, TYPE) \
1380 set_string(value); \
1382 static uint64_t type(Type) {
return Arg::TYPE; }
1384 FMT_MAKE_WSTR_VALUE(
wchar_t *, WSTRING)
1385 FMT_MAKE_WSTR_VALUE(
const wchar_t *, WSTRING)
1386 FMT_MAKE_WSTR_VALUE(
const std::wstring &, WSTRING)
1389 FMT_MAKE_VALUE(
void *, pointer, POINTER)
1390 FMT_MAKE_VALUE(
const void *, pointer, POINTER)
1392 template <
typename T>
1396 custom.value = &value;
1397 custom.format = &format_custom_arg<T>;
1400 template <
typename T>
1408 template <
typename Char_>
1410 template <
typename Char_,
typename T>
1413 template <
typename Char_>
1414 static uint64_t type(
const NamedArg<Char_> &) {
return Arg::NAMED_ARG; }
1415 template <
typename Char_,
typename T>
1419 template <
typename Formatter>
1426 template <
typename T>
1433 template <
typename Char>
1437 template <
typename T>
1442 template <
typename Char,
typename T>
1445 :
NamedArg<Char>(argname, value) {}
1455 template <
typename Char>
1475 internal::Arg::Type type(
unsigned index)
const {
1476 return type(types_, index);
1479 template <
typename Char>
1484 enum { MAX_PACKED_ARGS = 16 };
1489 : types_(types), values_(values) {}
1491 : types_(types), args_(args) {}
1493 uint64_t types()
const {
return types_; }
1499 bool use_values = type(MAX_PACKED_ARGS - 1) == Arg::NONE;
1500 if (index < MAX_PACKED_ARGS) {
1501 Arg::Type arg_type = type(index);
1503 if (arg_type != Arg::NONE)
1504 val = use_values ? values_[index] : args_[index];
1505 arg.type = arg_type;
1511 arg.type = Arg::NONE;
1514 for (
unsigned i = MAX_PACKED_ARGS; i <= index; ++i) {
1515 if (args_[i].type == Arg::NONE)
1518 return args_[index];
1521 static internal::Arg::Type type(uint64_t types,
unsigned index) {
1522 unsigned shift = index * 4;
1523 uint64_t mask = 0xf;
1524 return static_cast<internal::Arg::Type
>(
1525 (types & (mask << shift)) >> shift);
1529 #define FMT_DISPATCH(call) static_cast<Impl*>(this)->call
1555 template <
typename Impl,
typename Result>
1561 void report_unhandled_arg() {}
1563 Result visit_unhandled_arg() {
1564 FMT_DISPATCH(report_unhandled_arg());
1570 return FMT_DISPATCH(visit_any_int(value));
1575 return FMT_DISPATCH(visit_any_int(value));
1580 return FMT_DISPATCH(visit_any_int(value));
1585 return FMT_DISPATCH(visit_any_int(value));
1590 return FMT_DISPATCH(visit_any_int(value));
1595 return FMT_DISPATCH(visit_any_int(value));
1599 template <
typename T>
1601 return FMT_DISPATCH(visit_unhandled_arg());
1606 return FMT_DISPATCH(visit_any_double(value));
1611 return FMT_DISPATCH(visit_any_double(value));
1615 template <
typename T>
1617 return FMT_DISPATCH(visit_unhandled_arg());
1622 return FMT_DISPATCH(visit_unhandled_arg());
1627 return FMT_DISPATCH(visit_unhandled_arg());
1632 return FMT_DISPATCH(visit_unhandled_arg());
1637 return FMT_DISPATCH(visit_unhandled_arg());
1642 return FMT_DISPATCH(visit_unhandled_arg());
1656 case Arg::NAMED_ARG:
1657 FMT_ASSERT(
false,
"invalid argument type");
1660 return FMT_DISPATCH(visit_int(arg.int_value));
1662 return FMT_DISPATCH(visit_uint(arg.uint_value));
1663 case Arg::LONG_LONG:
1664 return FMT_DISPATCH(visit_long_long(arg.long_long_value));
1665 case Arg::ULONG_LONG:
1666 return FMT_DISPATCH(visit_ulong_long(arg.ulong_long_value));
1668 return FMT_DISPATCH(visit_bool(arg.int_value != 0));
1670 return FMT_DISPATCH(visit_char(arg.int_value));
1672 return FMT_DISPATCH(visit_double(arg.double_value));
1673 case Arg::LONG_DOUBLE:
1674 return FMT_DISPATCH(visit_long_double(arg.long_double_value));
1676 return FMT_DISPATCH(visit_cstring(arg.string.value));
1678 return FMT_DISPATCH(visit_string(arg.string));
1680 return FMT_DISPATCH(visit_wstring(arg.wstring));
1682 return FMT_DISPATCH(visit_pointer(arg.pointer));
1684 return FMT_DISPATCH(visit_custom(arg.custom));
1691 ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC
1696 SIGN_FLAG = 1, PLUS_FLAG = 2, MINUS_FLAG = 4, HASH_FLAG = 8,
1704 template <
char TYPE>
1706 Alignment align()
const {
return ALIGN_DEFAULT; }
1707 unsigned width()
const {
return 0; }
1708 int precision()
const {
return -1; }
1709 bool flag(
unsigned)
const {
return false; }
1710 char type()
const {
return TYPE; }
1711 char type_prefix()
const {
return TYPE; }
1712 char fill()
const {
return ' '; }
1722 WidthSpec(
unsigned width,
wchar_t fill) : width_(width), fill_(fill) {}
1724 unsigned width()
const {
return width_; }
1725 wchar_t fill()
const {
return fill_; }
1732 AlignSpec(
unsigned width,
wchar_t fill, Alignment align = ALIGN_DEFAULT)
1733 :
WidthSpec(width, fill), align_(align) {}
1735 Alignment align()
const {
return align_; }
1737 int precision()
const {
return -1; }
1741 template <
char TYPE>
1745 bool flag(
unsigned)
const {
return false; }
1746 char type()
const {
return TYPE; }
1747 char type_prefix()
const {
return TYPE; }
1757 unsigned width = 0,
char type = 0,
wchar_t fill =
' ')
1758 :
AlignSpec(width, fill), flags_(0), precision_(-1), type_(type) {}
1760 bool flag(
unsigned f)
const {
return (flags_ & f) != 0; }
1761 int precision()
const {
return precision_; }
1762 char type()
const {
return type_; }
1763 char type_prefix()
const {
return type_; }
1767 template <
typename T,
typename SpecT = TypeSpec<0>,
typename Char =
char>
1774 : SpecT(spec), value_(val) {}
1776 T value()
const {
return value_; }
1780 template <
typename Char>
1786 template <
typename FillChar>
1787 StrFormatSpec(
const Char *str,
unsigned width, FillChar fill)
1792 const Char *str()
const {
return str_; }
1831 template <
char TYPE_CODE,
typename Char>
1833 int value,
unsigned width, Char fill =
' ');
1835 #define FMT_DEFINE_INT_FORMATTERS(TYPE) \
1836 inline IntFormatSpec<TYPE, TypeSpec<'b'> > bin(TYPE value) { \
1837 return IntFormatSpec<TYPE, TypeSpec<'b'> >(value, TypeSpec<'b'>()); \
1840 inline IntFormatSpec<TYPE, TypeSpec<'o'> > oct(TYPE value) { \
1841 return IntFormatSpec<TYPE, TypeSpec<'o'> >(value, TypeSpec<'o'>()); \
1844 inline IntFormatSpec<TYPE, TypeSpec<'x'> > hex(TYPE value) { \
1845 return IntFormatSpec<TYPE, TypeSpec<'x'> >(value, TypeSpec<'x'>()); \
1848 inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
1849 return IntFormatSpec<TYPE, TypeSpec<'X'> >(value, TypeSpec<'X'>()); \
1852 template <char TYPE_CODE> \
1853 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> > pad( \
1854 IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, unsigned width) { \
1855 return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> >( \
1856 f.value(), AlignTypeSpec<TYPE_CODE>(width, ' ')); \
1863 template <char TYPE_CODE, typename Char> \
1864 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char> pad( \
1865 IntFormatSpec<TYPE, TypeSpec<TYPE_CODE>, Char> f, \
1866 unsigned width, Char fill) { \
1867 return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char>( \
1868 f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \
1871 inline IntFormatSpec<TYPE, AlignTypeSpec<0> > pad( \
1872 TYPE value, unsigned width) { \
1873 return IntFormatSpec<TYPE, AlignTypeSpec<0> >( \
1874 value, AlignTypeSpec<0>(width, ' ')); \
1877 template <typename Char> \
1878 inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
1879 TYPE value, unsigned width, Char fill) { \
1880 return IntFormatSpec<TYPE, AlignTypeSpec<0>, Char>( \
1881 value, AlignTypeSpec<0>(width, fill)); \
1884 FMT_DEFINE_INT_FORMATTERS(
int)
1885 FMT_DEFINE_INT_FORMATTERS(
long)
1886 FMT_DEFINE_INT_FORMATTERS(
unsigned)
1887 FMT_DEFINE_INT_FORMATTERS(
unsigned long)
1888 FMT_DEFINE_INT_FORMATTERS(LongLong)
1889 FMT_DEFINE_INT_FORMATTERS(ULongLong)
1903 template <typename Char>
1905 const Char *str,
unsigned width, Char fill = ' ') {
1909 inline StrFormatSpec<wchar_t> pad(
1910 const wchar_t *str,
unsigned width,
char fill =
' ') {
1911 return StrFormatSpec<wchar_t>(str, width, fill);
1914 namespace internal {
1916 template <
typename Char>
1919 typedef std::vector<
1920 std::pair<fmt::BasicStringRef<Char>, internal::Arg> > MapType;
1921 typedef typename MapType::value_type Pair;
1926 FMT_API
void init(
const ArgList &args);
1930 for (
typename MapType::const_iterator it = map_.begin(), end = map_.end();
1932 if (it->first == name)
1939 template <
typename Impl,
typename Char,
typename Spec = fmt::FormatSpec>
1947 void write_pointer(
const void *p) {
1948 spec_.flags_ = HASH_FLAG;
1950 writer_.write_int(reinterpret_cast<uintptr_t>(p), spec_);
1958 Spec &spec() {
return spec_; }
1960 void write(
bool value) {
1961 const char *str_value = value ?
"true" :
"false";
1963 writer_.write_str(str, spec_);
1966 void write(
const char *value) {
1968 writer_.write_str(str, spec_);
1972 typedef Spec SpecType;
1975 : writer_(w), spec_(s) {}
1977 template <
typename T>
1978 void visit_any_int(T value) { writer_.write_int(value, spec_); }
1980 template <
typename T>
1981 void visit_any_double(T value) { writer_.write_double(value, spec_); }
1983 void visit_bool(
bool value) {
1985 visit_any_int(value);
1991 void visit_char(
int value) {
1992 if (spec_.type_ && spec_.type_ !=
'c') {
1993 spec_.flags_ |= CHAR_FLAG;
1994 writer_.write_int(value, spec_);
1997 if (spec_.align_ == ALIGN_NUMERIC || spec_.flags_ != 0)
1998 FMT_THROW(
FormatError(
"invalid format specifier for char"));
1999 typedef typename BasicWriter<Char>::CharPtr CharPtr;
2001 CharPtr out = CharPtr();
2002 const unsigned CHAR_SIZE = 1;
2003 if (spec_.width_ > CHAR_SIZE) {
2004 out = writer_.grow_buffer(spec_.width_);
2005 if (spec_.align_ == ALIGN_RIGHT) {
2006 std::uninitialized_fill_n(out, spec_.width_ - CHAR_SIZE, fill);
2007 out += spec_.width_ - CHAR_SIZE;
2008 }
else if (spec_.align_ == ALIGN_CENTER) {
2009 out = writer_.fill_padding(out, spec_.width_,
2010 internal::const_check(CHAR_SIZE), fill);
2012 std::uninitialized_fill_n(out + CHAR_SIZE,
2013 spec_.width_ - CHAR_SIZE, fill);
2016 out = writer_.grow_buffer(CHAR_SIZE);
2021 void visit_cstring(
const char *value) {
2022 if (spec_.type_ ==
'p')
2023 return write_pointer(value);
2029 writer_.write_str(value, spec_);
2035 writer_.write_str(value, spec_);
2038 void visit_pointer(
const void *value) {
2039 if (spec_.type_ && spec_.type_ !=
'p')
2040 report_unknown_type(spec_.type_,
"pointer");
2041 write_pointer(value);
2048 int next_arg_index_;
2051 FMT_API
Arg do_get_arg(
unsigned arg_index,
const char *&error);
2054 const ArgList &args()
const {
return args_; }
2058 next_arg_index_ = 0;
2062 Arg next_arg(
const char *&error) {
2063 if (next_arg_index_ >= 0)
2064 return do_get_arg(internal::to_unsigned(next_arg_index_++), error);
2065 error =
"cannot switch from manual to automatic argument indexing";
2071 Arg get_arg(
unsigned arg_index,
const char *&error) {
2072 return check_no_auto_index(error) ? do_get_arg(arg_index, error) :
Arg();
2075 bool check_no_auto_index(
const char *&error) {
2076 if (next_arg_index_ > 0) {
2077 error =
"cannot switch from automatic to manual argument indexing";
2080 next_arg_index_ = -1;
2084 template <
typename Char>
2087 w << BasicStringRef<Char>(start, internal::to_unsigned(end - start));
2109 template <
typename Impl,
typename Char,
typename Spec = fmt::FormatSpec>
2113 const Char *format_;
2125 Spec &spec,
const Char *fmt)
2127 formatter_(formatter), format_(fmt) {}
2131 c.format(&formatter_, c.value, &format_);
2136 template <
typename Char>
2137 class ArgFormatter :
2138 public BasicArgFormatter<ArgFormatter<Char>, Char, FormatSpec> {
2148 template <
typename CharType,
typename ArgFormatter>
2149 class BasicFormatter :
private internal::FormatterBase {
2160 using internal::FormatterBase::get_arg;
2190 const Char *format(
const Char *&format_str,
const internal::Arg &arg);
2195 # define FMT_GEN(n, f) FMT_GEN##n(f)
2196 # define FMT_GEN1(f) f(0)
2197 # define FMT_GEN2(f) FMT_GEN1(f), f(1)
2198 # define FMT_GEN3(f) FMT_GEN2(f), f(2)
2199 # define FMT_GEN4(f) FMT_GEN3(f), f(3)
2200 # define FMT_GEN5(f) FMT_GEN4(f), f(4)
2201 # define FMT_GEN6(f) FMT_GEN5(f), f(5)
2202 # define FMT_GEN7(f) FMT_GEN6(f), f(6)
2203 # define FMT_GEN8(f) FMT_GEN7(f), f(7)
2204 # define FMT_GEN9(f) FMT_GEN8(f), f(8)
2205 # define FMT_GEN10(f) FMT_GEN9(f), f(9)
2206 # define FMT_GEN11(f) FMT_GEN10(f), f(10)
2207 # define FMT_GEN12(f) FMT_GEN11(f), f(11)
2208 # define FMT_GEN13(f) FMT_GEN12(f), f(12)
2209 # define FMT_GEN14(f) FMT_GEN13(f), f(13)
2210 # define FMT_GEN15(f) FMT_GEN14(f), f(14)
2212 namespace internal {
2213 inline uint64_t make_type() {
return 0; }
2215 template <
typename T>
2216 inline uint64_t make_type(
const T &arg) {
2220 template <std::size_t N,
bool= (N < ArgList::MAX_PACKED_ARGS)>
2223 template <std::
size_t N>
2225 typedef Value Type[N > 0 ? N : 1];
2227 template <
typename Formatter,
typename T>
2228 static Value make(
const T &value) {
2233 (void)result.custom.format;
2241 template <std::
size_t N>
2243 typedef Arg Type[N + 1];
2245 template <
typename Formatter,
typename T>
2249 #if FMT_USE_VARIADIC_TEMPLATES
2250 template <
typename Arg,
typename... Args>
2251 inline uint64_t make_type(
const Arg &first,
const Args & ... tail) {
2252 return make_type(first) | (make_type(tail...) << 4);
2262 template <
typename T>
2263 ArgType(
const T &arg) : type(make_type(arg)) {}
2266 # define FMT_ARG_TYPE_DEFAULT(n) ArgType t##n = ArgType()
2268 inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) {
2269 return t0.type | (t1.type << 4) | (t2.type << 8) | (t3.type << 12) |
2270 (t4.type << 16) | (t5.type << 20) | (t6.type << 24) | (t7.type << 28) |
2271 (t8.type << 32) | (t9.type << 36) | (t10.type << 40) | (t11.type << 44) |
2272 (t12.type << 48) | (t13.type << 52) | (t14.type << 56);
2277 # define FMT_MAKE_TEMPLATE_ARG(n) typename T##n
2278 # define FMT_MAKE_ARG_TYPE(n) T##n
2279 # define FMT_MAKE_ARG(n) const T##n &v##n
2280 # define FMT_ASSIGN_char(n) \
2281 arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter<char> >(v##n)
2282 # define FMT_ASSIGN_wchar_t(n) \
2283 arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter<wchar_t> >(v##n)
2285 #if FMT_USE_VARIADIC_TEMPLATES
2287 # define FMT_VARIADIC_VOID(func, arg_type) \
2288 template <typename... Args> \
2289 void func(arg_type arg0, const Args & ... args) { \
2290 typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
2291 typename ArgArray::Type array{ \
2292 ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
2293 func(arg0, fmt::ArgList(fmt::internal::make_type(args...), array)); \
2297 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
2298 template <typename... Args> \
2299 ctor(arg0_type arg0, arg1_type arg1, const Args & ... args) { \
2300 typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
2301 typename ArgArray::Type array{ \
2302 ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
2303 func(arg0, arg1, fmt::ArgList(fmt::internal::make_type(args...), array)); \
2308 # define FMT_MAKE_REF(n) \
2309 fmt::internal::MakeValue< fmt::BasicFormatter<Char> >(v##n)
2310 # define FMT_MAKE_REF2(n) v##n
2314 # define FMT_WRAP1(func, arg_type, n) \
2315 template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2316 inline void func(arg_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
2317 const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
2318 func(arg1, fmt::ArgList( \
2319 fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
2323 # define FMT_VARIADIC_VOID(func, arg_type) \
2324 inline void func(arg_type arg) { func(arg, fmt::ArgList()); } \
2325 FMT_WRAP1(func, arg_type, 1) FMT_WRAP1(func, arg_type, 2) \
2326 FMT_WRAP1(func, arg_type, 3) FMT_WRAP1(func, arg_type, 4) \
2327 FMT_WRAP1(func, arg_type, 5) FMT_WRAP1(func, arg_type, 6) \
2328 FMT_WRAP1(func, arg_type, 7) FMT_WRAP1(func, arg_type, 8) \
2329 FMT_WRAP1(func, arg_type, 9) FMT_WRAP1(func, arg_type, 10)
2331 # define FMT_CTOR(ctor, func, arg0_type, arg1_type, n) \
2332 template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2333 ctor(arg0_type arg0, arg1_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
2334 const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
2335 func(arg0, arg1, fmt::ArgList( \
2336 fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
2340 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
2341 FMT_CTOR(ctor, func, arg0_type, arg1_type, 1) \
2342 FMT_CTOR(ctor, func, arg0_type, arg1_type, 2) \
2343 FMT_CTOR(ctor, func, arg0_type, arg1_type, 3) \
2344 FMT_CTOR(ctor, func, arg0_type, arg1_type, 4) \
2345 FMT_CTOR(ctor, func, arg0_type, arg1_type, 5) \
2346 FMT_CTOR(ctor, func, arg0_type, arg1_type, 6) \
2347 FMT_CTOR(ctor, func, arg0_type, arg1_type, 7) \
2348 FMT_CTOR(ctor, func, arg0_type, arg1_type, 8) \
2349 FMT_CTOR(ctor, func, arg0_type, arg1_type, 9) \
2350 FMT_CTOR(ctor, func, arg0_type, arg1_type, 10)
2355 #define FMT_FOR_EACH1(f, x0) f(x0, 0)
2356 #define FMT_FOR_EACH2(f, x0, x1) \
2357 FMT_FOR_EACH1(f, x0), f(x1, 1)
2358 #define FMT_FOR_EACH3(f, x0, x1, x2) \
2359 FMT_FOR_EACH2(f, x0 ,x1), f(x2, 2)
2360 #define FMT_FOR_EACH4(f, x0, x1, x2, x3) \
2361 FMT_FOR_EACH3(f, x0, x1, x2), f(x3, 3)
2362 #define FMT_FOR_EACH5(f, x0, x1, x2, x3, x4) \
2363 FMT_FOR_EACH4(f, x0, x1, x2, x3), f(x4, 4)
2364 #define FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5) \
2365 FMT_FOR_EACH5(f, x0, x1, x2, x3, x4), f(x5, 5)
2366 #define FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6) \
2367 FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5), f(x6, 6)
2368 #define FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7) \
2369 FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6), f(x7, 7)
2370 #define FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8) \
2371 FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7), f(x8, 8)
2372 #define FMT_FOR_EACH10(f, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) \
2373 FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8), f(x9, 9)
2410 init(error_code, message,
ArgList());
2415 FMT_API ~SystemError() FMT_DTOR_NOEXCEPT;
2417 int error_code()
const {
return error_code_; }
2436 FMT_API
void format_system_error(
fmt::Writer &out,
int error_code,
2457 template <
typename Char>
2461 Buffer<Char> &buffer_;
2463 FMT_DISALLOW_COPY_AND_ASSIGN(BasicWriter);
2465 typedef typename internal::CharTraits<Char>::CharPtr CharPtr;
2469 static Char *
get(CharPtr p) {
return p.base(); }
2471 static Char *
get(Char *p) {
return p; }
2476 static CharPtr fill_padding(CharPtr buffer,
2477 unsigned total_size, std::size_t content_size,
wchar_t fill);
2481 CharPtr grow_buffer(std::size_t n) {
2482 std::size_t size = buffer_.
size();
2483 buffer_.
resize(size + n);
2484 return internal::make_ptr(&buffer_[size], n);
2488 template <
typename UInt>
2489 Char *write_unsigned_decimal(UInt value,
unsigned prefix_size = 0) {
2490 unsigned num_digits = internal::count_digits(value);
2491 Char *ptr =
get(grow_buffer(prefix_size + num_digits));
2492 internal::format_decimal(ptr + prefix_size, value, num_digits);
2497 template <
typename Int>
2498 void write_decimal(Int value) {
2499 typedef typename internal::IntTraits<Int>::MainType MainType;
2500 MainType abs_value =
static_cast<MainType
>(value);
2501 if (internal::is_negative(value)) {
2502 abs_value = 0 - abs_value;
2503 *write_unsigned_decimal(abs_value, 1) =
'-';
2505 write_unsigned_decimal(abs_value, 0);
2510 CharPtr prepare_int_buffer(
unsigned num_digits,
2511 const EmptySpec &,
const char *prefix,
unsigned prefix_size) {
2512 unsigned size = prefix_size + num_digits;
2513 CharPtr p = grow_buffer(size);
2514 std::uninitialized_copy(prefix, prefix + prefix_size, p);
2515 return p + size - 1;
2518 template <
typename Spec>
2519 CharPtr prepare_int_buffer(
unsigned num_digits,
2520 const Spec &spec,
const char *prefix,
unsigned prefix_size);
2523 template <
typename T,
typename Spec>
2524 void write_int(T value, Spec spec);
2527 template <
typename T,
typename Spec>
2528 void write_double(T value,
const Spec &spec);
2531 template <
typename StrChar>
2532 CharPtr write_str(
const StrChar *s, std::size_t size,
const AlignSpec &spec);
2534 template <
typename StrChar,
typename Spec>
2535 void write_str(
const internal::Arg::StringValue<StrChar> &str,
2542 void operator<<(typename internal::WCharHelper<wchar_t, Char>::Unsupported);
2544 typename internal::WCharHelper<const wchar_t *, Char>::Unsupported);
2548 void append_float_length(Char *&format_ptr,
long double) {
2549 *format_ptr++ =
'L';
2552 template<
typename T>
2553 void append_float_length(Char *&, T) {}
2555 template <
typename Impl,
typename Char_,
typename Spec_>
2556 friend class internal::ArgFormatterBase;
2558 template <
typename Impl,
typename Char_,
typename Spec_>
2559 friend class BasicPrintfArgFormatter;
2584 const Char *
data() const FMT_NOEXCEPT {
return &buffer_[0]; }
2591 std::size_t size = buffer_.
size();
2593 buffer_[size] =
'\0';
2602 std::basic_string<Char>
str()
const {
2603 return std::basic_string<Char>(&buffer_[0], buffer_.
size());
2637 write_decimal(value);
2640 BasicWriter &operator<<(
unsigned value) {
2641 return *this << IntFormatSpec<unsigned>(value);
2643 BasicWriter &operator<<(
long value) {
2644 write_decimal(value);
2647 BasicWriter &operator<<(
unsigned long value) {
2648 return *this << IntFormatSpec<unsigned long>(value);
2650 BasicWriter &operator<<(LongLong value) {
2651 write_decimal(value);
2661 return *this << IntFormatSpec<ULongLong>(value);
2684 buffer_.push_back(value);
2690 buffer_.push_back(value);
2700 const Char *str = value.data();
2701 buffer_.
append(str, str + value.size());
2707 const char *str = value.data();
2708 buffer_.
append(str, str + value.size());
2712 template <
typename T,
typename Spec,
typename FillChar>
2713 BasicWriter &operator<<(IntFormatSpec<T, Spec, FillChar> spec) {
2714 internal::CharTraits<Char>::convert(FillChar());
2715 write_int(spec.value(), spec);
2719 template <
typename StrChar>
2720 BasicWriter &operator<<(const StrFormatSpec<StrChar> &spec) {
2721 const StrChar *s = spec.str();
2722 write_str(s, std::char_traits<Char>::length(s), spec);
2726 void clear() FMT_NOEXCEPT { buffer_.clear(); }
2728 Buffer<Char> &buffer() FMT_NOEXCEPT {
return buffer_; }
2731 template <
typename Char>
2732 template <
typename StrChar>
2733 typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
2734 const StrChar *s, std::size_t size,
const AlignSpec &spec) {
2735 CharPtr out = CharPtr();
2736 if (spec.width() > size) {
2737 out = grow_buffer(spec.width());
2738 Char fill = internal::CharTraits<Char>::cast(spec.fill());
2739 if (spec.align() == ALIGN_RIGHT) {
2740 std::uninitialized_fill_n(out, spec.width() - size, fill);
2741 out += spec.width() - size;
2742 }
else if (spec.align() == ALIGN_CENTER) {
2743 out = fill_padding(out, spec.width(), size, fill);
2745 std::uninitialized_fill_n(out + size, spec.width() - size, fill);
2748 out = grow_buffer(size);
2750 std::uninitialized_copy(s, s + size, out);
2754 template <
typename Char>
2755 template <
typename StrChar,
typename Spec>
2756 void BasicWriter<Char>::write_str(
2757 const internal::Arg::StringValue<StrChar> &s,
const Spec &spec) {
2759 internal::CharTraits<Char>::convert(StrChar());
2760 if (spec.type_ && spec.type_ !=
's')
2761 internal::report_unknown_type(spec.type_,
"string");
2762 const StrChar *str_value = s.value;
2763 std::size_t str_size = s.size;
2764 if (str_size == 0) {
2766 FMT_THROW(FormatError(
"string pointer is null"));
2769 std::size_t precision =
static_cast<std::size_t
>(spec.precision_);
2770 if (spec.precision_ >= 0 && precision < str_size)
2771 str_size = precision;
2772 write_str(str_value, str_size, spec);
2775 template <
typename Char>
2776 typename BasicWriter<Char>::CharPtr
2777 BasicWriter<Char>::fill_padding(
2778 CharPtr buffer,
unsigned total_size,
2779 std::size_t content_size,
wchar_t fill) {
2780 std::size_t padding = total_size - content_size;
2781 std::size_t left_padding = padding / 2;
2782 Char fill_char = internal::CharTraits<Char>::cast(fill);
2783 std::uninitialized_fill_n(buffer, left_padding, fill_char);
2784 buffer += left_padding;
2785 CharPtr content = buffer;
2786 std::uninitialized_fill_n(buffer + content_size,
2787 padding - left_padding, fill_char);
2791 template <
typename Char>
2792 template <
typename Spec>
2793 typename BasicWriter<Char>::CharPtr
2794 BasicWriter<Char>::prepare_int_buffer(
2795 unsigned num_digits,
const Spec &spec,
2796 const char *prefix,
unsigned prefix_size) {
2797 unsigned width = spec.width();
2798 Alignment align = spec.align();
2799 Char fill = internal::CharTraits<Char>::cast(spec.fill());
2800 if (spec.precision() >
static_cast<int>(num_digits)) {
2803 if (prefix_size > 0 && prefix[prefix_size - 1] ==
'0')
2805 unsigned number_size =
2806 prefix_size + internal::to_unsigned(spec.precision());
2807 AlignSpec subspec(number_size,
'0', ALIGN_NUMERIC);
2808 if (number_size >= width)
2809 return prepare_int_buffer(num_digits, subspec, prefix, prefix_size);
2811 unsigned fill_size = width - number_size;
2812 if (align != ALIGN_LEFT) {
2813 CharPtr p = grow_buffer(fill_size);
2814 std::uninitialized_fill(p, p + fill_size, fill);
2816 CharPtr result = prepare_int_buffer(
2817 num_digits, subspec, prefix, prefix_size);
2818 if (align == ALIGN_LEFT) {
2819 CharPtr p = grow_buffer(fill_size);
2820 std::uninitialized_fill(p, p + fill_size, fill);
2824 unsigned size = prefix_size + num_digits;
2825 if (width <= size) {
2826 CharPtr p = grow_buffer(size);
2827 std::uninitialized_copy(prefix, prefix + prefix_size, p);
2828 return p + size - 1;
2830 CharPtr p = grow_buffer(width);
2831 CharPtr end = p + width;
2832 if (align == ALIGN_LEFT) {
2833 std::uninitialized_copy(prefix, prefix + prefix_size, p);
2835 std::uninitialized_fill(p, end, fill);
2836 }
else if (align == ALIGN_CENTER) {
2837 p = fill_padding(p, width, size, fill);
2838 std::uninitialized_copy(prefix, prefix + prefix_size, p);
2841 if (align == ALIGN_NUMERIC) {
2842 if (prefix_size != 0) {
2843 p = std::uninitialized_copy(prefix, prefix + prefix_size, p);
2844 size -= prefix_size;
2847 std::uninitialized_copy(prefix, prefix + prefix_size, end - size);
2849 std::uninitialized_fill(p, end - size, fill);
2855 template <
typename Char>
2856 template <
typename T,
typename Spec>
2857 void BasicWriter<Char>::write_int(T value, Spec spec) {
2858 unsigned prefix_size = 0;
2859 typedef typename internal::IntTraits<T>::MainType UnsignedType;
2860 UnsignedType abs_value =
static_cast<UnsignedType
>(value);
2861 char prefix[4] =
"";
2862 if (internal::is_negative(value)) {
2865 abs_value = 0 - abs_value;
2866 }
else if (spec.flag(SIGN_FLAG)) {
2867 prefix[0] = spec.flag(PLUS_FLAG) ?
'+' :
' ';
2870 switch (spec.type()) {
2872 unsigned num_digits = internal::count_digits(abs_value);
2873 CharPtr p = prepare_int_buffer(num_digits, spec, prefix, prefix_size) + 1;
2874 internal::format_decimal(
get(p), abs_value, 0);
2877 case 'x':
case 'X': {
2878 UnsignedType n = abs_value;
2879 if (spec.flag(HASH_FLAG)) {
2880 prefix[prefix_size++] =
'0';
2881 prefix[prefix_size++] = spec.type_prefix();
2883 unsigned num_digits = 0;
2886 }
while ((n >>= 4) != 0);
2887 Char *p =
get(prepare_int_buffer(
2888 num_digits, spec, prefix, prefix_size));
2890 const char *digits = spec.type() ==
'x' ?
2891 "0123456789abcdef" :
"0123456789ABCDEF";
2893 *p-- = digits[n & 0xf];
2894 }
while ((n >>= 4) != 0);
2897 case 'b':
case 'B': {
2898 UnsignedType n = abs_value;
2899 if (spec.flag(HASH_FLAG)) {
2900 prefix[prefix_size++] =
'0';
2901 prefix[prefix_size++] = spec.type_prefix();
2903 unsigned num_digits = 0;
2906 }
while ((n >>= 1) != 0);
2907 Char *p =
get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
2910 *p-- =
static_cast<Char
>(
'0' + (n & 1));
2911 }
while ((n >>= 1) != 0);
2915 UnsignedType n = abs_value;
2916 if (spec.flag(HASH_FLAG))
2917 prefix[prefix_size++] =
'0';
2918 unsigned num_digits = 0;
2921 }
while ((n >>= 3) != 0);
2922 Char *p =
get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
2925 *p-- =
static_cast<Char
>(
'0' + (n & 7));
2926 }
while ((n >>= 3) != 0);
2930 unsigned num_digits = internal::count_digits(abs_value);
2932 #if !(defined(ANDROID) || defined(__ANDROID__))
2933 sep = internal::thousands_sep(std::localeconv());
2935 unsigned size =
static_cast<unsigned>(
2936 num_digits + sep.
size() * ((num_digits - 1) / 3));
2937 CharPtr p = prepare_int_buffer(size, spec, prefix, prefix_size) + 1;
2938 internal::format_decimal(
get(p), abs_value, 0, internal::ThousandsSep(sep));
2942 internal::report_unknown_type(
2943 spec.type(), spec.flag(CHAR_FLAG) ?
"char" :
"integer");
2948 template <
typename Char>
2949 template <
typename T,
typename Spec>
2950 void BasicWriter<Char>::write_double(T value,
const Spec &spec) {
2952 char type = spec.type();
2958 case 'e':
case 'f':
case 'g':
case 'a':
2966 case 'E':
case 'G':
case 'A':
2970 internal::report_unknown_type(type,
"double");
2977 if (internal::FPUtil::isnegative(static_cast<double>(value))) {
2980 }
else if (spec.flag(SIGN_FLAG)) {
2981 sign = spec.flag(PLUS_FLAG) ?
'+' :
' ';
2984 if (internal::FPUtil::isnotanumber(value)) {
2987 std::size_t nan_size = 4;
2988 const char *nan = upper ?
" NAN" :
" nan";
2993 CharPtr out = write_str(nan, nan_size, spec);
2999 if (internal::FPUtil::isinfinity(value)) {
3002 std::size_t inf_size = 4;
3003 const char *inf = upper ?
" INF" :
" inf";
3008 CharPtr out = write_str(inf, inf_size, spec);
3014 std::size_t offset = buffer_.
size();
3015 unsigned width = spec.width();
3017 buffer_.
reserve(buffer_.
size() + (width > 1u ? width : 1u));
3024 enum { MAX_FORMAT_SIZE = 10};
3025 Char format[MAX_FORMAT_SIZE];
3026 Char *format_ptr = format;
3027 *format_ptr++ =
'%';
3028 unsigned width_for_sprintf = width;
3029 if (spec.flag(HASH_FLAG))
3030 *format_ptr++ =
'#';
3031 if (spec.align() == ALIGN_CENTER) {
3032 width_for_sprintf = 0;
3034 if (spec.align() == ALIGN_LEFT)
3035 *format_ptr++ =
'-';
3037 *format_ptr++ =
'*';
3039 if (spec.precision() >= 0) {
3040 *format_ptr++ =
'.';
3041 *format_ptr++ =
'*';
3044 append_float_length(format_ptr, value);
3045 *format_ptr++ = type;
3049 Char fill = internal::CharTraits<Char>::cast(spec.fill());
3051 Char *start = FMT_NULL;
3053 std::size_t buffer_size = buffer_.
capacity() - offset;
3058 if (buffer_size == 0) {
3060 buffer_size = buffer_.
capacity() - offset;
3063 start = &buffer_[offset];
3064 int result = internal::CharTraits<Char>::format_float(
3065 start, buffer_size, format, width_for_sprintf, spec.precision(), value);
3067 n = internal::to_unsigned(result);
3068 if (offset + n < buffer_.
capacity())
3070 buffer_.
reserve(offset + n + 1);
3078 if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) ||
3080 *(start - 1) = sign;
3083 *(start - 1) = fill;
3087 if (spec.align() == ALIGN_CENTER && spec.width() > n) {
3088 width = spec.width();
3089 CharPtr p = grow_buffer(width);
3090 std::memmove(
get(p) + (width - n) / 2,
get(p), n *
sizeof(Char));
3091 fill_padding(p, spec.width(), n, fill);
3094 if (spec.fill() !=
' ' || sign) {
3095 while (*start ==
' ')
3098 *(start - 1) = sign;
3137 template <
typename Char,
typename Allocator = std::allocator<Char> >
3146 #if FMT_USE_RVALUE_REFERENCES
3163 buffer_ = std::move(other.buffer_);
3192 template <
typename Char>
3205 :
BasicWriter<Char>(buffer_), buffer_(array, size) {}
3213 template <std::
size_t SIZE>
3215 :
BasicWriter<Char>(buffer_), buffer_(array, SIZE) {}
3218 typedef BasicArrayWriter<char> ArrayWriter;
3219 typedef BasicArrayWriter<wchar_t> WArrayWriter;
3223 FMT_API
void report_system_error(
int error_code,
3224 StringRef message) FMT_NOEXCEPT;
3226 #if FMT_USE_WINDOWS_H
3229 class WindowsError :
public SystemError {
3231 FMT_API
void init(
int error_code, CStringRef format_str, ArgList args);
3262 WindowsError(
int error_code, CStringRef message) {
3263 init(error_code, message, ArgList());
3265 FMT_VARIADIC_CTOR(WindowsError, init,
int, CStringRef)
3270 FMT_API
void report_windows_error(
int error_code,
3271 StringRef message) FMT_NOEXCEPT;
3275 enum Color { BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE };
3283 FMT_API
void print_colored(Color c, CStringRef format, ArgList args);
3294 inline std::string format(CStringRef format_str, ArgList args) {
3296 w.write(format_str, args);
3300 inline std::wstring format(WCStringRef format_str, ArgList args) {
3302 w.write(format_str, args);
3315 FMT_API
void print(std::FILE *f, CStringRef format_str, ArgList args);
3326 FMT_API
void print(CStringRef format_str, ArgList args);
3335 enum {BUFFER_SIZE = std::numeric_limits<ULongLong>::digits10 + 3};
3336 mutable char buffer_[BUFFER_SIZE];
3340 char *format_decimal(ULongLong value) {
3341 char *buffer_end = buffer_ + BUFFER_SIZE - 1;
3342 while (value >= 100) {
3346 unsigned index =
static_cast<unsigned>((value % 100) * 2);
3348 *--buffer_end = internal::Data::DIGITS[index + 1];
3349 *--buffer_end = internal::Data::DIGITS[index];
3352 *--buffer_end =
static_cast<char>(
'0' + value);
3355 unsigned index =
static_cast<unsigned>(value * 2);
3356 *--buffer_end = internal::Data::DIGITS[index + 1];
3357 *--buffer_end = internal::Data::DIGITS[index];
3361 void FormatSigned(LongLong value) {
3362 ULongLong abs_value =
static_cast<ULongLong
>(value);
3363 bool negative = value < 0;
3365 abs_value = 0 - abs_value;
3366 str_ = format_decimal(abs_value);
3372 explicit FormatInt(
int value) { FormatSigned(value); }
3373 explicit FormatInt(
long value) { FormatSigned(value); }
3374 explicit FormatInt(LongLong value) { FormatSigned(value); }
3375 explicit FormatInt(
unsigned value) : str_(format_decimal(value)) {}
3376 explicit FormatInt(
unsigned long value) : str_(format_decimal(value)) {}
3377 explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {}
3381 return internal::to_unsigned(buffer_ - str_ + BUFFER_SIZE - 1);
3388 const char *
data()
const {
return str_; }
3395 buffer_[BUFFER_SIZE - 1] =
'\0';
3404 std::string
str()
const {
return std::string(str_, size()); }
3410 template <
typename T>
3411 inline void format_decimal(
char *&buffer, T value) {
3412 typedef typename internal::IntTraits<T>::MainType MainType;
3413 MainType abs_value =
static_cast<MainType
>(value);
3414 if (internal::is_negative(value)) {
3416 abs_value = 0 - abs_value;
3418 if (abs_value < 100) {
3419 if (abs_value < 10) {
3420 *buffer++ =
static_cast<char>(
'0' + abs_value);
3423 unsigned index =
static_cast<unsigned>(abs_value * 2);
3424 *buffer++ = internal::Data::DIGITS[index];
3425 *buffer++ = internal::Data::DIGITS[index + 1];
3428 unsigned num_digits = internal::count_digits(abs_value);
3429 internal::format_decimal(buffer, abs_value, num_digits);
3430 buffer += num_digits;
3443 template <
typename T>
3444 inline internal::NamedArgWithType<char, T> arg(StringRef name,
const T &arg) {
3445 return internal::NamedArgWithType<char, T>(name, arg);
3448 template <
typename T>
3449 inline internal::NamedArgWithType<wchar_t, T> arg(WStringRef name,
const T &arg) {
3450 return internal::NamedArgWithType<wchar_t, T>(name, arg);
3455 template <
typename Char>
3456 void arg(StringRef,
const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
3457 template <typename Char>
3458 void arg(WStringRef, const internal::
NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
3466 # pragma GCC system_header
3470 #define FMT_EXPAND(args) args
3474 #define FMT_NARG(...) FMT_NARG_(__VA_ARGS__, FMT_RSEQ_N())
3475 #define FMT_NARG_(...) FMT_EXPAND(FMT_ARG_N(__VA_ARGS__))
3476 #define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
3477 #define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
3479 #define FMT_FOR_EACH_(N, f, ...) \
3480 FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__))
3481 #define FMT_FOR_EACH(f, ...) \
3482 FMT_EXPAND(FMT_FOR_EACH_(FMT_NARG(__VA_ARGS__), f, __VA_ARGS__))
3484 #define FMT_ADD_ARG_NAME(type, index) type arg##index
3485 #define FMT_GET_ARG_NAME(type, index) arg##index
3487 #if FMT_USE_VARIADIC_TEMPLATES
3488 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
3489 template <typename... Args> \
3490 ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
3491 const Args & ... args) { \
3492 typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
3493 typename ArgArray::Type array{ \
3494 ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
3495 call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), \
3496 fmt::ArgList(fmt::internal::make_type(args...), array)); \
3501 # define FMT_WRAP(Char, ReturnType, func, call, n, ...) \
3502 template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
3503 inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
3504 FMT_GEN(n, FMT_MAKE_ARG)) { \
3505 fmt::internal::ArgArray<n>::Type arr; \
3506 FMT_GEN(n, FMT_ASSIGN_##Char); \
3507 call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList( \
3508 fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), arr)); \
3511 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
3512 inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) { \
3513 call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList()); \
3515 FMT_WRAP(Char, ReturnType, func, call, 1, __VA_ARGS__) \
3516 FMT_WRAP(Char, ReturnType, func, call, 2, __VA_ARGS__) \
3517 FMT_WRAP(Char, ReturnType, func, call, 3, __VA_ARGS__) \
3518 FMT_WRAP(Char, ReturnType, func, call, 4, __VA_ARGS__) \
3519 FMT_WRAP(Char, ReturnType, func, call, 5, __VA_ARGS__) \
3520 FMT_WRAP(Char, ReturnType, func, call, 6, __VA_ARGS__) \
3521 FMT_WRAP(Char, ReturnType, func, call, 7, __VA_ARGS__) \
3522 FMT_WRAP(Char, ReturnType, func, call, 8, __VA_ARGS__) \
3523 FMT_WRAP(Char, ReturnType, func, call, 9, __VA_ARGS__) \
3524 FMT_WRAP(Char, ReturnType, func, call, 10, __VA_ARGS__) \
3525 FMT_WRAP(Char, ReturnType, func, call, 11, __VA_ARGS__) \
3526 FMT_WRAP(Char, ReturnType, func, call, 12, __VA_ARGS__) \
3527 FMT_WRAP(Char, ReturnType, func, call, 13, __VA_ARGS__) \
3528 FMT_WRAP(Char, ReturnType, func, call, 14, __VA_ARGS__) \
3529 FMT_WRAP(Char, ReturnType, func, call, 15, __VA_ARGS__)
3530 #endif // FMT_USE_VARIADIC_TEMPLATES
3559 #define FMT_VARIADIC(ReturnType, func, ...) \
3560 FMT_VARIADIC_(char, ReturnType, func, return func, __VA_ARGS__)
3562 #define FMT_VARIADIC_W(ReturnType, func, ...) \
3563 FMT_VARIADIC_(wchar_t, ReturnType, func, return func, __VA_ARGS__)
3565 #define FMT_CAPTURE_ARG_(id, index) ::fmt::arg(#id, id)
3567 #define FMT_CAPTURE_ARG_W_(id, index) ::fmt::arg(L###id, id)
3583 #define FMT_CAPTURE(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_, __VA_ARGS__)
3585 #define FMT_CAPTURE_W(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_W_, __VA_ARGS__)
3588 FMT_VARIADIC(std::string, format, CStringRef)
3589 FMT_VARIADIC_W(std::wstring, format, WCStringRef)
3590 FMT_VARIADIC(
void, print, CStringRef)
3591 FMT_VARIADIC(
void, print, std::FILE *, CStringRef)
3592 FMT_VARIADIC(
void, print_colored, Color, CStringRef)
3594 namespace internal {
3595 template <
typename Char>
3596 inline bool is_name_start(Char c) {
3597 return (
'a' <= c && c <=
'z') || (
'A' <= c && c <=
'Z') ||
'_' == c;
3602 template <
typename Char>
3603 unsigned parse_nonnegative_int(
const Char *&s) {
3604 assert(
'0' <= *s && *s <=
'9');
3607 unsigned new_value = value * 10 + (*s++ -
'0');
3609 if (new_value < value) {
3610 value = (std::numeric_limits<unsigned>::max)();
3614 }
while (
'0' <= *s && *s <=
'9');
3616 unsigned max_int = (std::numeric_limits<int>::max)();
3617 if (value > max_int)
3618 FMT_THROW(FormatError(
"number is too big"));
3622 inline void require_numeric_argument(
const Arg &arg,
char spec) {
3623 if (arg.type > Arg::LAST_NUMERIC_TYPE) {
3624 std::string message =
3625 fmt::format(
"format specifier '{}' requires numeric argument", spec);
3630 template <
typename Char>
3631 void check_sign(
const Char *&s,
const Arg &arg) {
3632 char sign =
static_cast<char>(*s);
3633 require_numeric_argument(arg, sign);
3634 if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) {
3635 FMT_THROW(FormatError(fmt::format(
3636 "format specifier '{}' requires signed argument", sign)));
3642 template <
typename Char,
typename AF>
3643 inline internal::Arg BasicFormatter<Char, AF>::get_arg(
3644 BasicStringRef<Char> arg_name,
const char *&error) {
3645 if (check_no_auto_index(error)) {
3647 const internal::Arg *arg = map_.find(arg_name);
3650 error =
"argument not found";
3652 return internal::Arg();
3655 template <
typename Char,
typename AF>
3656 inline internal::Arg BasicFormatter<Char, AF>::parse_arg_index(
const Char *&s) {
3657 const char *error = FMT_NULL;
3658 internal::Arg arg = *s < '0' || *s >
'9' ?
3659 next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error);
3661 FMT_THROW(FormatError(
3662 *s !=
'}' && *s !=
':' ?
"invalid format string" : error));
3667 template <
typename Char,
typename AF>
3668 inline internal::Arg BasicFormatter<Char, AF>::parse_arg_name(
const Char *&s) {
3669 assert(internal::is_name_start(*s));
3670 const Char *start = s;
3674 }
while (internal::is_name_start(c) || (
'0' <= c && c <=
'9'));
3675 const char *error = FMT_NULL;
3676 internal::Arg arg = get_arg(BasicStringRef<Char>(start, s - start), error);
3678 FMT_THROW(FormatError(error));
3682 template <
typename Char,
typename ArgFormatter>
3683 const Char *BasicFormatter<Char, ArgFormatter>::format(
3684 const Char *&format_str,
const internal::Arg &arg) {
3685 using internal::Arg;
3686 const Char *s = format_str;
3687 typename ArgFormatter::SpecType spec;
3689 if (arg.type == Arg::CUSTOM) {
3690 arg.custom.format(
this, arg.custom.value, &s);
3696 const Char *p = s + 1;
3697 spec.align_ = ALIGN_DEFAULT;
3701 spec.align_ = ALIGN_LEFT;
3704 spec.align_ = ALIGN_RIGHT;
3707 spec.align_ = ALIGN_NUMERIC;
3710 spec.align_ = ALIGN_CENTER;
3713 if (spec.align_ != ALIGN_DEFAULT) {
3715 if (c ==
'}')
break;
3717 FMT_THROW(FormatError(
"invalid fill character '{'"));
3721 if (spec.align_ == ALIGN_NUMERIC)
3722 require_numeric_argument(arg,
'=');
3732 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
3736 spec.flags_ |= MINUS_FLAG;
3740 spec.flags_ |= SIGN_FLAG;
3745 require_numeric_argument(arg,
'#');
3746 spec.flags_ |= HASH_FLAG;
3752 require_numeric_argument(arg,
'0');
3753 spec.align_ = ALIGN_NUMERIC;
3759 if (
'0' <= *s && *s <=
'9') {
3760 spec.width_ = internal::parse_nonnegative_int(s);
3761 }
else if (*s ==
'{') {
3763 Arg width_arg = internal::is_name_start(*s) ?
3764 parse_arg_name(s) : parse_arg_index(s);
3766 FMT_THROW(FormatError(
"invalid format string"));
3767 ULongLong value = 0;
3768 switch (width_arg.type) {
3770 if (width_arg.int_value < 0)
3771 FMT_THROW(FormatError(
"negative width"));
3772 value = width_arg.int_value;
3775 value = width_arg.uint_value;
3777 case Arg::LONG_LONG:
3778 if (width_arg.long_long_value < 0)
3779 FMT_THROW(FormatError(
"negative width"));
3780 value = width_arg.long_long_value;
3782 case Arg::ULONG_LONG:
3783 value = width_arg.ulong_long_value;
3786 FMT_THROW(FormatError(
"width is not integer"));
3788 if (value > (std::numeric_limits<int>::max)())
3789 FMT_THROW(FormatError(
"number is too big"));
3790 spec.width_ =
static_cast<int>(value);
3796 spec.precision_ = 0;
3797 if (
'0' <= *s && *s <=
'9') {
3798 spec.precision_ = internal::parse_nonnegative_int(s);
3799 }
else if (*s ==
'{') {
3801 Arg precision_arg = internal::is_name_start(*s) ?
3802 parse_arg_name(s) : parse_arg_index(s);
3804 FMT_THROW(FormatError(
"invalid format string"));
3805 ULongLong value = 0;
3806 switch (precision_arg.type) {
3808 if (precision_arg.int_value < 0)
3809 FMT_THROW(FormatError(
"negative precision"));
3810 value = precision_arg.int_value;
3813 value = precision_arg.uint_value;
3815 case Arg::LONG_LONG:
3816 if (precision_arg.long_long_value < 0)
3817 FMT_THROW(FormatError(
"negative precision"));
3818 value = precision_arg.long_long_value;
3820 case Arg::ULONG_LONG:
3821 value = precision_arg.ulong_long_value;
3824 FMT_THROW(FormatError(
"precision is not integer"));
3826 if (value > (std::numeric_limits<int>::max)())
3827 FMT_THROW(FormatError(
"number is too big"));
3828 spec.precision_ =
static_cast<int>(value);
3830 FMT_THROW(FormatError(
"missing precision specifier"));
3832 if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) {
3833 FMT_THROW(FormatError(
3834 fmt::format(
"precision not allowed in {} format specifier",
3835 arg.type == Arg::POINTER ?
"pointer" :
"integer")));
3840 if (*s !=
'}' && *s)
3841 spec.type_ =
static_cast<char>(*s++);
3845 FMT_THROW(FormatError(
"missing '}' in format string"));
3848 ArgFormatter(*
this, spec, s - 1).visit(arg);
3852 template <
typename Char,
typename AF>
3855 const Char *start = s;
3858 if (c !=
'{' && c !=
'}')
continue;
3860 write(writer_, start, s);
3865 FMT_THROW(
FormatError(
"unmatched '}' in format string"));
3866 write(writer_, start, s - 1);
3868 parse_arg_name(s) : parse_arg_index(s);
3869 start = s = format(s, arg);
3871 write(writer_, start, s);
3874 template <
typename Char,
typename It>
3886 template <
typename It>
3891 template <
typename It>
3892 ArgJoin<wchar_t, It> join(It first, It last,
const BasicCStringRef<wchar_t>& sep) {
3893 return ArgJoin<wchar_t, It>(first, last, sep);
3896 #if FMT_HAS_GXX_CXX11
3897 template <
typename Range>
3898 auto join(
const Range& range,
const BasicCStringRef<char>& sep)
3899 -> ArgJoin<char, decltype(std::begin(range))> {
3900 return join(std::begin(range), std::end(range), sep);
3903 template <
typename Range>
3904 auto join(
const Range& range,
const BasicCStringRef<wchar_t>& sep)
3905 -> ArgJoin<wchar_t, decltype(std::begin(range))> {
3906 return join(std::begin(range), std::end(range), sep);
3910 template <
typename ArgFormatter,
typename Char,
typename It>
3912 const Char *&format_str,
const ArgJoin<Char, It>& e) {
3913 const Char* end = format_str;
3916 while (*end && *end !=
'}')
3919 FMT_THROW(FormatError(
"missing '}' in format string"));
3923 const Char*
save = format_str;
3925 while (it != e.last) {
3931 format_str = end + 1;
3935 #if FMT_USE_USER_DEFINED_LITERALS
3937 namespace internal {
3939 template <
typename Char>
3943 template <
typename... Args>
3944 auto operator()(Args && ... args) const
3945 -> decltype(format(str, std::forward<Args>(args)...)) {
3946 return format(str, std::forward<Args>(args)...);
3950 template <
typename Char>
3954 template <
typename T>
3956 return {str, std::forward<T>(value)};
3962 inline namespace literals {
3974 inline internal::UdlFormat<char>
3975 operator"" _format(
const char *s, std::size_t) {
return {s}; }
3976 inline internal::UdlFormat<wchar_t>
3977 operator"" _format(
const wchar_t *s, std::size_t) {
return {s}; }
3989 inline internal::UdlArg<char>
3990 operator"" _a(
const char *s, std::size_t) {
return {s}; }
3991 inline internal::UdlArg<wchar_t>
3992 operator"" _a(
const wchar_t *s, std::size_t) {
return {s}; }
3996 #endif // FMT_USE_USER_DEFINED_LITERALS
3999 #if FMT_GCC_VERSION >= 406
4000 # pragma GCC diagnostic pop
4003 #if defined(__clang__) && !defined(FMT_ICC_VERSION)
4004 # pragma clang diagnostic pop
4007 #ifdef FMT_HEADER_ONLY
4008 # define FMT_FUNC inline
4009 # include "format.cc"
4014 #endif // FMT_FORMAT_H_
Definition: format.h:1082
Result visit_custom(Arg::CustomValue)
Definition: format.h:1641
std::size_t size() const
Definition: format.h:523
Definition: format.h:1161
BasicCStringRef(const Char *s)
Definition: format.h:589
Result visit_string(Arg::StringValue< char >)
Definition: format.h:1626
Definition: format.h:1171
Result visit_pointer(const void *)
Definition: format.h:1636
Definition: format.h:1139
BasicWriter & operator<<(long double value)
Definition: format.h:2675
Definition: format.h:1460
Definition: format.h:2221
Result visit_uint(unsigned value)
Definition: format.h:1579
Result visit_bool(bool value)
Definition: format.h:1589
Definition: format.h:1420
const Char * data() const FMT_NOEXCEPT
Definition: format.h:2584
Result visit_ulong_long(ULongLong value)
Definition: format.h:1584
Result visit_any_int(T)
Definition: format.h:1600
SystemError(int error_code, CStringRef message)
Definition: format.h:2409
const Char * c_str() const
Definition: format.h:2590
Result visit_long_long(LongLong value)
Definition: format.h:1574
Definition: format.h:1092
Definition: format.h:1124
Result visit_any_double(T)
Definition: format.h:1616
Definition: format.h:1742
Definition: format.h:1217
Definition: format.h:1556
void save(BinaryBuffer &bb, const T &x)
Saves x to bb by calling diy::Serialization<T>::save(bb,x).
Definition: serialization.hpp:102
const Char * c_str() const
Definition: format.h:602
internal::Arg operator[](unsigned index) const
Definition: format.h:1496
Definition: format.h:1145
Definition: format.h:3193
const Char * data() const
Definition: format.h:520
BasicStringRef(const Char *s)
Definition: format.h:497
BasicCStringRef(const std::basic_string< Char, std::char_traits< Char >, Allocator > &s)
Definition: format.h:597
BasicStringRef(const Char *s, std::size_t size)
Definition: format.h:489
Result visit_wstring(Arg::StringValue< wchar_t >)
Definition: format.h:1631
Result visit_double(double value)
Definition: format.h:1605
Result visit_long_double(long double value)
Definition: format.h:1610
void write(BasicCStringRef< Char > format, ArgList args)
Definition: format.h:2631
Result visit_char(int value)
Definition: format.h:1594
BasicStringRef(const std::basic_string< Char, std::char_traits< Char >, Allocator > &s)
Definition: format.h:506
Definition: format.h:1084
BasicArrayWriter(Char(&array)[SIZE])
Definition: format.h:3214
void reserve(std::size_t capacity)
Definition: format.h:706
Definition: format.h:1166
Definition: format.h:1196
Definition: format.h:1131
Definition: format.h:1448
Definition: format.h:1729
BasicWriter & operator<<(char value)
Definition: format.h:2683
Definition: format.h:1705
Definition: format.h:1129
Result visit_int(int value)
Definition: format.h:1569
Definition: format.h:3138
Definition: format.h:1701
Definition: format.h:1456
Result visit_cstring(const char *)
Definition: format.h:1621
std::size_t capacity() const
Definition: format.h:690
void resize(std::size_t new_size)
Definition: format.h:695
BasicWriter & operator<<(ULongLong value)
Definition: format.h:2660
void append(const U *begin, const U *end)
Definition: format.h:729
Result visit(const Arg &arg)
Definition: format.h:1653
Definition: format.h:1209
BasicWriter(Buffer< Char > &b)
Definition: format.h:2565
std::size_t size() const
Definition: format.h:687
BasicArrayWriter(Char *array, std::size_t size)
Definition: format.h:3204
Definition: format.h:2257
Definition: format.h:2379
Definition: format.h:1202
Definition: format.h:1134
virtual ~BasicWriter()
Definition: format.h:2573
std::basic_string< Char > str() const
Definition: format.h:2602
Definition: format.h:1179
std::basic_string< Char > to_string() const
Definition: format.h:515
Definition: format.h:1215
Definition: format.h:3875
Definition: format.h:1263
Definition: format.h:1716
std::size_t size() const
Definition: format.h:2578