31 #define FMT_HEADER_ONLY // Added by diy for header-only usage
46 # define FMT_SECURE_SCL _SECURE_SCL
48 # define FMT_SECURE_SCL 0
55 #if defined(_MSC_VER) && _MSC_VER <= 1500
56 typedef unsigned __int32 uint32_t;
57 typedef unsigned __int64 uint64_t;
58 typedef __int64 intmax_t;
63 #if !defined(FMT_HEADER_ONLY) && defined(_WIN32)
65 # define FMT_API __declspec(dllexport)
66 # elif defined(FMT_SHARED)
67 # define FMT_API __declspec(dllimport)
75 # define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
76 # define FMT_GCC_EXTENSION __extension__
77 # if FMT_GCC_VERSION >= 406
78 # pragma GCC diagnostic push
81 # pragma GCC diagnostic ignored "-Wlong-long"
84 # pragma GCC diagnostic ignored "-Wshadow"
87 # pragma GCC diagnostic ignored "-Wsign-conversion"
89 # if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__
90 # define FMT_HAS_GXX_CXX11 1
93 # define FMT_GCC_EXTENSION
96 #if defined(__INTEL_COMPILER)
97 # define FMT_ICC_VERSION __INTEL_COMPILER
99 # define FMT_ICC_VERSION __ICL
102 #if defined(__clang__) && !defined(FMT_ICC_VERSION)
103 # pragma clang diagnostic push
104 # pragma clang diagnostic ignored "-Wdocumentation"
107 #ifdef __GNUC_LIBSTD__
108 # define FMT_GNUC_LIBSTD_VERSION (__GNUC_LIBSTD__ * 100 + __GNUC_LIBSTD_MINOR__)
112 # define FMT_HAS_FEATURE(x) __has_feature(x)
114 # define FMT_HAS_FEATURE(x) 0
118 # define FMT_HAS_BUILTIN(x) __has_builtin(x)
120 # define FMT_HAS_BUILTIN(x) 0
123 #ifdef __has_cpp_attribute
124 # define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
126 # define FMT_HAS_CPP_ATTRIBUTE(x) 0
129 #ifndef FMT_USE_VARIADIC_TEMPLATES
133 # define FMT_USE_VARIADIC_TEMPLATES \
134 (FMT_HAS_FEATURE(cxx_variadic_templates) || \
135 (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1800)
138 #ifndef FMT_USE_RVALUE_REFERENCES
141 # if defined(FMT_GNUC_LIBSTD_VERSION) && FMT_GNUC_LIBSTD_VERSION <= 402
142 # define FMT_USE_RVALUE_REFERENCES 0
144 # define FMT_USE_RVALUE_REFERENCES \
145 (FMT_HAS_FEATURE(cxx_rvalue_references) || \
146 (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600)
150 #if FMT_USE_RVALUE_REFERENCES
155 #if defined(__GNUC__) && !defined(__EXCEPTIONS)
156 # define FMT_EXCEPTIONS 0
158 #if defined(_MSC_VER) && !_HAS_EXCEPTIONS
159 # define FMT_EXCEPTIONS 0
161 #ifndef FMT_EXCEPTIONS
162 # define FMT_EXCEPTIONS 1
167 # define FMT_THROW(x) throw x
169 # define FMT_THROW(x) assert(false)
174 #ifndef FMT_USE_NOEXCEPT
175 # define FMT_USE_NOEXCEPT 0
180 # if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \
181 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \
183 # define FMT_NOEXCEPT noexcept
185 # define FMT_NOEXCEPT throw()
188 # define FMT_NOEXCEPT
194 #ifndef FMT_USE_DELETED_FUNCTIONS
195 # define FMT_USE_DELETED_FUNCTIONS 0
198 #if FMT_USE_DELETED_FUNCTIONS || FMT_HAS_FEATURE(cxx_deleted_functions) || \
199 (FMT_GCC_VERSION >= 404 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1800
200 # define FMT_DELETED_OR_UNDEFINED = delete
201 # define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
202 TypeName(const TypeName&) = delete; \
203 TypeName& operator=(const TypeName&) = delete
205 # define FMT_DELETED_OR_UNDEFINED
206 # define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
207 TypeName(const TypeName&); \
208 TypeName& operator=(const TypeName&)
211 #ifndef FMT_USE_USER_DEFINED_LITERALS
216 # define FMT_USE_USER_DEFINED_LITERALS \
217 FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES && \
218 (FMT_HAS_FEATURE(cxx_user_literals) || \
219 (FMT_GCC_VERSION >= 407 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1900) && \
220 (!defined(FMT_ICC_VERSION) || FMT_ICC_VERSION >= 1500)
224 # define FMT_ASSERT(condition, message) assert((condition) && message)
228 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz)
229 # define FMT_BUILTIN_CLZ(n) __builtin_clz(n)
232 #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clzll)
233 # define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n)
240 #if defined(_MSC_VER) && !defined(FMT_BUILTIN_CLZLL)
245 # pragma intrinsic(_BitScanReverse)
246 inline uint32_t clz(uint32_t x) {
248 _BitScanReverse(&r, x);
254 # pragma warning(suppress: 6102)
257 # define FMT_BUILTIN_CLZ(n) fmt::internal::clz(n)
260 # pragma intrinsic(_BitScanReverse64)
263 inline uint32_t clzll(uint64_t x) {
266 _BitScanReverse64(&r, x);
269 if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32)))
270 return 63 - (r + 32);
273 _BitScanReverse(&r, static_cast<uint32_t>(x));
280 # pragma warning(suppress: 6102)
283 # define FMT_BUILTIN_CLZLL(n) fmt::internal::clzll(n)
292 operator int()
const {
return 0; }
299 inline DummyInt _ecvt_s(...) {
return DummyInt(); }
300 inline DummyInt isinf(...) {
return DummyInt(); }
301 inline DummyInt _finite(...) {
return DummyInt(); }
302 inline DummyInt isnan(...) {
return DummyInt(); }
303 inline DummyInt _isnan(...) {
return DummyInt(); }
307 template <
typename T>
308 inline T check(T value) {
return value; }
318 class numeric_limits<fmt::internal::DummyInt> :
319 public std::numeric_limits<int> {
322 template <
typename T>
323 static bool isinfinity(T x) {
324 using namespace fmt::internal;
327 if (check(
sizeof(isinf(x)) ==
sizeof(
bool) ||
328 sizeof(isinf(x)) ==
sizeof(
int))) {
329 return isinf(x) != 0;
331 return !_finite(static_cast<double>(x));
335 template <
typename T>
336 static bool isnotanumber(T x) {
337 using namespace fmt::internal;
338 if (check(
sizeof(isnan(x)) ==
sizeof(
bool) ||
339 sizeof(isnan(x)) ==
sizeof(
int))) {
340 return isnan(x) != 0;
342 return _isnan(static_cast<double>(x)) != 0;
346 static bool isnegative(
double x) {
347 using namespace fmt::internal;
348 if (check(
sizeof(signbit(x)) ==
sizeof(
int)))
349 return signbit(x) != 0;
350 if (x < 0)
return true;
351 if (!isnotanumber(x))
return false;
352 int dec = 0, sign = 0;
354 _ecvt_s(buffer,
sizeof(buffer), x, 0, &dec, &sign);
364 FMT_GCC_EXTENSION
typedef long long LongLong;
365 FMT_GCC_EXTENSION
typedef unsigned long long ULongLong;
367 #if FMT_USE_RVALUE_REFERENCES
371 template <
typename Char>
377 template <
typename Char>
380 template <
typename CharType,
408 template <
typename Char>
425 : data_(s), size_(std::char_traits<Char>::length(s)) {}
433 : data_(s.c_str()), size_(s.size()) {}
441 return std::basic_string<Char>(data_, size_);
445 const Char *
data()
const {
return data_; }
448 std::size_t
size()
const {
return size_; }
452 std::size_t size = size_ < other.size_ ? size_ : other.size_;
453 int result = std::char_traits<Char>::compare(data_, other.data_, size);
455 result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1);
459 friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) {
460 return lhs.compare(rhs) == 0;
462 friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs) {
463 return lhs.compare(rhs) != 0;
465 friend bool operator<(BasicStringRef lhs, BasicStringRef rhs) {
466 return lhs.compare(rhs) < 0;
468 friend bool operator<=(BasicStringRef lhs, BasicStringRef rhs) {
469 return lhs.compare(rhs) <= 0;
471 friend bool operator>(BasicStringRef lhs, BasicStringRef rhs) {
472 return lhs.compare(rhs) > 0;
474 friend bool operator>=(BasicStringRef lhs, BasicStringRef rhs) {
475 return lhs.compare(rhs) >= 0;
479 typedef BasicStringRef<char> StringRef;
480 typedef BasicStringRef<wchar_t> WStringRef;
507 template <
typename Char>
524 const Char *
c_str()
const {
return data_; }
527 typedef BasicCStringRef<char> CStringRef;
528 typedef BasicCStringRef<wchar_t> WCStringRef;
536 : std::runtime_error(message.
c_str()) {}
542 template <
typename T>
545 #define FMT_SPECIALIZE_MAKE_UNSIGNED(T, U) \
547 struct MakeUnsigned<T> { typedef U Type; }
549 FMT_SPECIALIZE_MAKE_UNSIGNED(
char,
unsigned char);
550 FMT_SPECIALIZE_MAKE_UNSIGNED(
signed char,
unsigned char);
551 FMT_SPECIALIZE_MAKE_UNSIGNED(
short,
unsigned short);
552 FMT_SPECIALIZE_MAKE_UNSIGNED(
int,
unsigned);
553 FMT_SPECIALIZE_MAKE_UNSIGNED(
long,
unsigned long);
554 FMT_SPECIALIZE_MAKE_UNSIGNED(LongLong, ULongLong);
557 template <
typename Int>
558 inline typename MakeUnsigned<Int>::Type to_unsigned(Int value) {
559 FMT_ASSERT(value >= 0,
"negative value");
560 return static_cast<typename MakeUnsigned<Int>::Type
>(value);
565 enum { INLINE_BUFFER_SIZE = 500 };
569 template <
typename T>
570 inline stdext::checked_array_iterator<T*> make_ptr(T *ptr, std::size_t size) {
571 return stdext::checked_array_iterator<T*>(ptr, size);
574 template <
typename T>
575 inline T *make_ptr(T *ptr, std::size_t) {
return ptr; }
584 template <
typename T>
587 FMT_DISALLOW_COPY_AND_ASSIGN(
Buffer);
592 std::size_t capacity_;
594 Buffer(T *ptr = 0, std::size_t capacity = 0)
595 : ptr_(ptr), size_(0), capacity_(capacity) {}
603 virtual void grow(std::size_t size) = 0;
609 std::size_t
size()
const {
return size_; }
618 if (new_size > capacity_)
629 if (capacity > capacity_)
633 void clear() FMT_NOEXCEPT { size_ = 0; }
635 void push_back(
const T &value) {
636 if (size_ == capacity_)
638 ptr_[size_++] = value;
642 template <
typename U>
643 void append(
const U *begin,
const U *end);
645 T &operator[](std::size_t index) {
return ptr_[index]; }
646 const T &operator[](std::size_t index)
const {
return ptr_[index]; }
649 template <
typename T>
650 template <
typename U>
652 std::size_t new_size = size_ + internal::to_unsigned(end - begin);
653 if (new_size > capacity_)
655 std::uninitialized_copy(begin, end,
656 internal::make_ptr(ptr_, capacity_) + size_);
664 template <
typename T, std::
size_t SIZE,
typename Allocator = std::allocator<T> >
671 if (this->ptr_ != data_) Allocator::deallocate(this->ptr_, this->capacity_);
675 void grow(std::size_t size);
678 explicit MemoryBuffer(
const Allocator &alloc = Allocator())
679 : Allocator(alloc),
Buffer<T>(data_, SIZE) {}
682 #if FMT_USE_RVALUE_REFERENCES
686 Allocator &this_alloc = *
this, &other_alloc = other;
687 this_alloc = std::move(other_alloc);
688 this->size_ = other.size_;
689 this->capacity_ = other.capacity_;
690 if (other.ptr_ == other.data_) {
692 std::uninitialized_copy(other.data_, other.data_ + this->size_,
693 make_ptr(data_, this->capacity_));
695 this->ptr_ = other.ptr_;
698 other.ptr_ = other.data_;
708 assert(
this != &other);
716 Allocator get_allocator()
const {
return *
this; }
719 template <
typename T, std::
size_t SIZE,
typename Allocator>
721 std::size_t new_capacity = this->capacity_ + this->capacity_ / 2;
722 if (size > new_capacity)
724 T *new_ptr = this->allocate(new_capacity);
726 std::uninitialized_copy(this->ptr_, this->ptr_ + this->size_,
727 make_ptr(new_ptr, new_capacity));
728 std::size_t old_capacity = this->capacity_;
729 T *old_ptr = this->ptr_;
730 this->capacity_ = new_capacity;
731 this->ptr_ = new_ptr;
735 if (old_ptr != data_)
736 Allocator::deallocate(old_ptr, old_capacity);
740 template <
typename Char>
746 FMT_API
void grow(std::size_t size);
749 template <
typename Char>
753 typedef stdext::checked_array_iterator<Char*> CharPtr;
755 typedef Char *CharPtr;
757 static Char cast(
int value) {
return static_cast<Char
>(value); }
760 template <
typename Char>
767 static char convert(
wchar_t);
770 static char convert(
char value) {
return value; }
773 template <
typename T>
774 FMT_API
static int format_float(
char *buffer, std::size_t size,
775 const char *format,
unsigned width,
int precision, T value);
781 static wchar_t convert(
char value) {
return value; }
782 static wchar_t convert(
wchar_t value) {
return value; }
784 template <
typename T>
785 FMT_API
static int format_float(
wchar_t *buffer, std::size_t size,
786 const wchar_t *format,
unsigned width,
int precision, T value);
790 template <
bool IsSigned>
792 template <
typename T>
793 static bool is_negative(T value) {
return value < 0; }
798 template <
typename T>
799 static bool is_negative(T) {
return false; }
804 template <
typename T>
805 inline bool is_negative(T value) {
810 template <
bool FitsIn32Bits>
816 template <
typename T>
824 FMT_API
void report_unknown_type(
char code,
const char *type);
828 template <
typename T =
void>
830 static const uint32_t POWERS_OF_10_32[];
831 static const uint64_t POWERS_OF_10_64[];
832 static const char DIGITS[];
837 #ifdef FMT_BUILTIN_CLZLL
840 inline unsigned count_digits(uint64_t n) {
843 int t = (64 - FMT_BUILTIN_CLZLL(n | 1)) * 1233 >> 12;
844 return to_unsigned(t) - (n < Data::POWERS_OF_10_64[t]) + 1;
848 inline unsigned count_digits(uint64_t n) {
854 if (n < 10)
return count;
855 if (n < 100)
return count + 1;
856 if (n < 1000)
return count + 2;
857 if (n < 10000)
return count + 3;
864 #ifdef FMT_BUILTIN_CLZ
866 inline unsigned count_digits(uint32_t n) {
867 int t = (32 - FMT_BUILTIN_CLZ(n | 1)) * 1233 >> 12;
868 return to_unsigned(t) - (n < Data::POWERS_OF_10_32[t]) + 1;
874 template <
typename Char>
875 void operator()(Char *) {}
884 unsigned digit_index_;
889 template <
typename Char>
890 void operator()(Char *&buffer) {
891 if (++digit_index_ % 3 != 0)
893 buffer -= sep_.size();
894 std::uninitialized_copy(sep_.data(), sep_.data() + sep_.size(),
895 internal::make_ptr(buffer, sep_.size()));
902 template <
typename UInt,
typename Char,
typename ThousandsSep>
903 inline void format_decimal(Char *buffer, UInt value,
unsigned num_digits,
905 buffer += num_digits;
906 while (value >= 100) {
910 unsigned index =
static_cast<unsigned>((value % 100) * 2);
912 *--buffer = Data::DIGITS[index + 1];
913 thousands_sep(buffer);
914 *--buffer = Data::DIGITS[index];
915 thousands_sep(buffer);
918 *--buffer =
static_cast<char>(
'0' + value);
921 unsigned index =
static_cast<unsigned>(value * 2);
922 *--buffer = Data::DIGITS[index + 1];
923 *--buffer = Data::DIGITS[index];
926 template <
typename UInt,
typename Char>
927 inline void format_decimal(Char *buffer, UInt value,
unsigned num_digits) {
928 return format_decimal(buffer, value, num_digits,
NoThousandsSep());
932 # define FMT_USE_WINDOWS_H 0
933 #elif !defined(FMT_USE_WINDOWS_H)
934 # define FMT_USE_WINDOWS_H 1
939 #if FMT_USE_WINDOWS_H
947 FMT_API
explicit UTF8ToUTF16(StringRef s);
948 operator WStringRef()
const {
return WStringRef(&buffer_[0], size()); }
949 size_t size()
const {
return buffer_.size() - 1; }
950 const wchar_t *c_str()
const {
return &buffer_[0]; }
951 std::wstring str()
const {
return std::wstring(&buffer_[0], size()); }
962 FMT_API
explicit UTF16ToUTF8(WStringRef s);
963 operator StringRef()
const {
return StringRef(&buffer_[0], size()); }
964 size_t size()
const {
return buffer_.size() - 1; }
965 const char *c_str()
const {
return &buffer_[0]; }
966 std::string str()
const {
return std::string(&buffer_[0], size()); }
971 FMT_API
int convert(WStringRef s);
974 FMT_API
void format_windows_error(
fmt::Writer &out,
int error_code,
978 FMT_API
void format_system_error(
fmt::Writer &out,
int error_code,
983 template <
typename Char>
989 typedef void (*FormatFunc)(
990 void *formatter,
const void *arg,
void *format_str_ptr);
1000 LongLong long_long_value;
1001 ULongLong ulong_long_value;
1002 double double_value;
1003 long double long_double_value;
1004 const void *pointer;
1015 INT, UINT, LONG_LONG, ULONG_LONG, BOOL, CHAR, LAST_INTEGER_TYPE = CHAR,
1017 DOUBLE, LONG_DOUBLE, LAST_NUMERIC_TYPE = LONG_DOUBLE,
1018 CSTRING, STRING, WSTRING, POINTER, CUSTOM
1028 template <
typename Char>
1031 template <
typename T =
void>
1036 template <
typename T,
typename Char>
1039 typedef T Unsupported;
1042 template <
typename T>
1044 typedef T Supported;
1048 typedef char Yes[1];
1051 template <
typename T>
1055 Yes &convert(fmt::ULongLong);
1058 template<
typename T,
bool ENABLE_CONVERSION>
1060 enum { value = ENABLE_CONVERSION };
1063 template<
typename T,
bool ENABLE_CONVERSION>
1065 enum { value =
false };
1068 template<
typename T>
1076 template<
typename T>
1078 enum { enable_conversion =
sizeof(convert(get<T>())) ==
sizeof(Yes) };
1082 #define FMT_DISABLE_CONVERSION_TO_INT(Type) \
1084 struct ConvertToInt<Type> { enum { value = 0 }; }
1087 FMT_DISABLE_CONVERSION_TO_INT(
float);
1088 FMT_DISABLE_CONVERSION_TO_INT(
double);
1089 FMT_DISABLE_CONVERSION_TO_INT(
long double);
1091 template<
bool B,
class T =
void>
1097 template<
bool B,
class T,
class F>
1100 template<
class T,
class F>
1105 struct Not {
enum { value = 0 }; };
1108 struct Not<false> {
enum { value = 1 }; };
1111 template <
typename Formatter>
1114 typedef typename Formatter::Char Char;
1122 template <
typename T>
1124 template <
typename T>
1131 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
1132 MakeValue(
typename WCharHelper<wchar_t, Char>::Unsupported);
1134 MakeValue(
typename WCharHelper<wchar_t *, Char>::Unsupported);
1135 MakeValue(
typename WCharHelper<const wchar_t *, Char>::Unsupported);
1136 MakeValue(
typename WCharHelper<const std::wstring &, Char>::Unsupported);
1137 MakeValue(
typename WCharHelper<WStringRef, Char>::Unsupported);
1140 string.value = str.
data();
1141 string.size = str.
size();
1145 wstring.value = str.
data();
1146 wstring.size = str.
size();
1150 template <
typename T>
1151 static void format_custom_arg(
1152 void *formatter,
const void *arg,
void *format_str_ptr) {
1153 format(*static_cast<Formatter*>(formatter),
1154 *static_cast<const Char**>(format_str_ptr),
1155 *static_cast<const T*>(arg));
1161 #define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \
1162 MakeValue(Type value) { field = rhs; } \
1163 static uint64_t type(Type) { return Arg::TYPE; }
1165 #define FMT_MAKE_VALUE(Type, field, TYPE) \
1166 FMT_MAKE_VALUE_(Type, field, TYPE, value)
1168 FMT_MAKE_VALUE(
bool, int_value, BOOL)
1169 FMT_MAKE_VALUE(
short, int_value, INT)
1170 FMT_MAKE_VALUE(
unsigned short, uint_value, UINT)
1171 FMT_MAKE_VALUE(
int, int_value, INT)
1172 FMT_MAKE_VALUE(
unsigned, uint_value, UINT)
1177 if (check(
sizeof(
long) ==
sizeof(
int)))
1178 int_value =
static_cast<int>(value);
1180 long_long_value = value;
1182 static uint64_t type(
long) {
1183 return sizeof(long) ==
sizeof(
int) ? Arg::INT : Arg::LONG_LONG;
1187 if (check(
sizeof(
unsigned long) ==
sizeof(
unsigned)))
1188 uint_value =
static_cast<unsigned>(value);
1190 ulong_long_value = value;
1192 static uint64_t type(
unsigned long) {
1193 return sizeof(
unsigned long) ==
sizeof(
unsigned) ?
1194 Arg::UINT : Arg::ULONG_LONG;
1197 FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG)
1198 FMT_MAKE_VALUE(ULongLong, ulong_long_value, ULONG_LONG)
1199 FMT_MAKE_VALUE(
float, double_value, DOUBLE)
1200 FMT_MAKE_VALUE(
double, double_value, DOUBLE)
1201 FMT_MAKE_VALUE(
long double, long_double_value, LONG_DOUBLE)
1202 FMT_MAKE_VALUE(
signed char, int_value, INT)
1203 FMT_MAKE_VALUE(
unsigned char, uint_value, UINT)
1204 FMT_MAKE_VALUE(
char, int_value, CHAR)
1206 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
1210 static uint64_t type(
wchar_t) {
return Arg::CHAR; }
1213 #define FMT_MAKE_STR_VALUE(Type, TYPE) \
1214 MakeValue(Type value) { set_string(value); } \
1215 static uint64_t type(Type) { return Arg::TYPE; }
1217 FMT_MAKE_VALUE(
char *,
string.value, CSTRING)
1218 FMT_MAKE_VALUE(
const char *,
string.value, CSTRING)
1219 FMT_MAKE_VALUE(
const signed char *, sstring.value, CSTRING)
1220 FMT_MAKE_VALUE(
const unsigned char *, ustring.value, CSTRING)
1221 FMT_MAKE_STR_VALUE(
const std::string &, STRING)
1225 #define FMT_MAKE_WSTR_VALUE(Type, TYPE) \
1227 set_string(value); \
1229 static uint64_t type(Type) {
return Arg::TYPE; }
1231 FMT_MAKE_WSTR_VALUE(
wchar_t *, WSTRING)
1232 FMT_MAKE_WSTR_VALUE(
const wchar_t *, WSTRING)
1233 FMT_MAKE_WSTR_VALUE(
const std::wstring &, WSTRING)
1236 FMT_MAKE_VALUE(
void *, pointer, POINTER)
1237 FMT_MAKE_VALUE(
const void *, pointer, POINTER)
1239 template <
typename T>
1243 custom.value = &value;
1244 custom.format = &format_custom_arg<T>;
1247 template <
typename T>
1253 template <
typename T>
1254 static uint64_t type(
const T &) {
1260 template <
typename Char_>
1263 template <
typename Char_>
1264 static uint64_t type(
const NamedArg<Char_> &) {
return Arg::NAMED_ARG; }
1267 template <
typename Formatter>
1274 template <
typename T>
1281 template <
typename Char>
1285 template <
typename T>
1295 template <
typename Char>
1298 template <
typename Char>
1318 internal::Arg::Type type(
unsigned index)
const {
1319 unsigned shift = index * 4;
1320 uint64_t mask = 0xf;
1321 return static_cast<internal::Arg::Type
>(
1322 (types_ & (mask << shift)) >> shift);
1325 template <
typename Char>
1330 enum { MAX_PACKED_ARGS = 16 };
1335 : types_(types), values_(values) {}
1337 : types_(types), args_(args) {}
1343 bool use_values = type(MAX_PACKED_ARGS - 1) == Arg::NONE;
1344 if (index < MAX_PACKED_ARGS) {
1345 Arg::Type arg_type = type(index);
1347 if (arg_type != Arg::NONE)
1348 val = use_values ? values_[index] : args_[index];
1349 arg.type = arg_type;
1355 arg.type = Arg::NONE;
1358 for (
unsigned i = MAX_PACKED_ARGS; i <= index; ++i) {
1359 if (args_[i].type == Arg::NONE)
1362 return args_[index];
1366 #define FMT_DISPATCH(call) static_cast<Impl*>(this)->call
1392 template <
typename Impl,
typename Result>
1398 void report_unhandled_arg() {}
1400 Result visit_unhandled_arg() {
1401 FMT_DISPATCH(report_unhandled_arg());
1407 return FMT_DISPATCH(visit_any_int(value));
1412 return FMT_DISPATCH(visit_any_int(value));
1417 return FMT_DISPATCH(visit_any_int(value));
1422 return FMT_DISPATCH(visit_any_int(value));
1427 return FMT_DISPATCH(visit_any_int(value));
1432 return FMT_DISPATCH(visit_any_int(value));
1436 template <
typename T>
1438 return FMT_DISPATCH(visit_unhandled_arg());
1443 return FMT_DISPATCH(visit_any_double(value));
1448 return FMT_DISPATCH(visit_any_double(value));
1452 template <
typename T>
1454 return FMT_DISPATCH(visit_unhandled_arg());
1459 return FMT_DISPATCH(visit_unhandled_arg());
1464 return FMT_DISPATCH(visit_unhandled_arg());
1469 return FMT_DISPATCH(visit_unhandled_arg());
1474 return FMT_DISPATCH(visit_unhandled_arg());
1479 return FMT_DISPATCH(visit_unhandled_arg());
1493 FMT_ASSERT(
false,
"invalid argument type");
1496 return FMT_DISPATCH(visit_int(arg.int_value));
1498 return FMT_DISPATCH(visit_uint(arg.uint_value));
1499 case Arg::LONG_LONG:
1500 return FMT_DISPATCH(visit_long_long(arg.long_long_value));
1501 case Arg::ULONG_LONG:
1502 return FMT_DISPATCH(visit_ulong_long(arg.ulong_long_value));
1504 return FMT_DISPATCH(visit_bool(arg.int_value != 0));
1506 return FMT_DISPATCH(visit_char(arg.int_value));
1508 return FMT_DISPATCH(visit_double(arg.double_value));
1509 case Arg::LONG_DOUBLE:
1510 return FMT_DISPATCH(visit_long_double(arg.long_double_value));
1512 return FMT_DISPATCH(visit_cstring(arg.string.value));
1514 return FMT_DISPATCH(visit_string(arg.string));
1516 return FMT_DISPATCH(visit_wstring(arg.wstring));
1518 return FMT_DISPATCH(visit_pointer(arg.pointer));
1520 return FMT_DISPATCH(visit_custom(arg.custom));
1526 ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC
1531 SIGN_FLAG = 1, PLUS_FLAG = 2, MINUS_FLAG = 4, HASH_FLAG = 8,
1539 template <
char TYPE>
1541 Alignment align()
const {
return ALIGN_DEFAULT; }
1542 unsigned width()
const {
return 0; }
1543 int precision()
const {
return -1; }
1544 bool flag(
unsigned)
const {
return false; }
1545 char type()
const {
return TYPE; }
1546 char fill()
const {
return ' '; }
1556 WidthSpec(
unsigned width,
wchar_t fill) : width_(width), fill_(fill) {}
1558 unsigned width()
const {
return width_; }
1559 wchar_t fill()
const {
return fill_; }
1566 AlignSpec(
unsigned width,
wchar_t fill, Alignment align = ALIGN_DEFAULT)
1567 :
WidthSpec(width, fill), align_(align) {}
1569 Alignment align()
const {
return align_; }
1571 int precision()
const {
return -1; }
1575 template <
char TYPE>
1579 bool flag(
unsigned)
const {
return false; }
1580 char type()
const {
return TYPE; }
1590 unsigned width = 0,
char type = 0,
wchar_t fill =
' ')
1591 :
AlignSpec(width, fill), flags_(0), precision_(-1), type_(type) {}
1593 bool flag(
unsigned f)
const {
return (flags_ & f) != 0; }
1594 int precision()
const {
return precision_; }
1595 char type()
const {
return type_; }
1599 template <
typename T,
typename SpecT = TypeSpec<0>,
typename Char =
char>
1606 : SpecT(spec), value_(val) {}
1608 T value()
const {
return value_; }
1612 template <
typename Char>
1618 template <
typename FillChar>
1619 StrFormatSpec(
const Char *str,
unsigned width, FillChar fill)
1624 const Char *str()
const {
return str_; }
1663 template <
char TYPE_CODE,
typename Char>
1665 int value,
unsigned width, Char fill =
' ');
1667 #define FMT_DEFINE_INT_FORMATTERS(TYPE) \
1668 inline IntFormatSpec<TYPE, TypeSpec<'b'> > bin(TYPE value) { \
1669 return IntFormatSpec<TYPE, TypeSpec<'b'> >(value, TypeSpec<'b'>()); \
1672 inline IntFormatSpec<TYPE, TypeSpec<'o'> > oct(TYPE value) { \
1673 return IntFormatSpec<TYPE, TypeSpec<'o'> >(value, TypeSpec<'o'>()); \
1676 inline IntFormatSpec<TYPE, TypeSpec<'x'> > hex(TYPE value) { \
1677 return IntFormatSpec<TYPE, TypeSpec<'x'> >(value, TypeSpec<'x'>()); \
1680 inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
1681 return IntFormatSpec<TYPE, TypeSpec<'X'> >(value, TypeSpec<'X'>()); \
1684 template <char TYPE_CODE> \
1685 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> > pad( \
1686 IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, unsigned width) { \
1687 return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> >( \
1688 f.value(), AlignTypeSpec<TYPE_CODE>(width, ' ')); \
1695 template <char TYPE_CODE, typename Char> \
1696 inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char> pad( \
1697 IntFormatSpec<TYPE, TypeSpec<TYPE_CODE>, Char> f, \
1698 unsigned width, Char fill) { \
1699 return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char>( \
1700 f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \
1703 inline IntFormatSpec<TYPE, AlignTypeSpec<0> > pad( \
1704 TYPE value, unsigned width) { \
1705 return IntFormatSpec<TYPE, AlignTypeSpec<0> >( \
1706 value, AlignTypeSpec<0>(width, ' ')); \
1709 template <typename Char> \
1710 inline IntFormatSpec<TYPE, AlignTypeSpec<0>, Char> pad( \
1711 TYPE value, unsigned width, Char fill) { \
1712 return IntFormatSpec<TYPE, AlignTypeSpec<0>, Char>( \
1713 value, AlignTypeSpec<0>(width, fill)); \
1716 FMT_DEFINE_INT_FORMATTERS(
int)
1717 FMT_DEFINE_INT_FORMATTERS(
long)
1718 FMT_DEFINE_INT_FORMATTERS(
unsigned)
1719 FMT_DEFINE_INT_FORMATTERS(
unsigned long)
1720 FMT_DEFINE_INT_FORMATTERS(LongLong)
1721 FMT_DEFINE_INT_FORMATTERS(ULongLong)
1735 template <typename Char>
1737 const Char *str,
unsigned width, Char fill = ' ') {
1741 inline StrFormatSpec<wchar_t> pad(
1742 const wchar_t *str,
unsigned width,
char fill =
' ') {
1743 return StrFormatSpec<wchar_t>(str, width, fill);
1746 namespace internal {
1748 template <
typename Char>
1751 typedef std::vector<
1752 std::pair<fmt::BasicStringRef<Char>, internal::Arg> > MapType;
1753 typedef typename MapType::value_type Pair;
1758 FMT_API
void init(
const ArgList &args);
1762 for (
typename MapType::const_iterator it = map_.begin(), end = map_.end();
1764 if (it->first == name)
1771 template <
typename Impl,
typename Char>
1779 void write_pointer(
const void *p) {
1780 spec_.flags_ = HASH_FLAG;
1782 writer_.write_int(reinterpret_cast<uintptr_t>(p), spec_);
1789 void write(
bool value) {
1790 const char *str_value = value ?
"true" :
"false";
1792 writer_.write_str(str, spec_);
1795 void write(
const char *value) {
1797 writer_.write_str(str, spec_);
1802 : writer_(w), spec_(s) {}
1804 template <
typename T>
1805 void visit_any_int(T value) { writer_.write_int(value, spec_); }
1807 template <
typename T>
1808 void visit_any_double(T value) { writer_.write_double(value, spec_); }
1810 void visit_bool(
bool value) {
1812 return visit_any_int(value);
1816 void visit_char(
int value) {
1817 if (spec_.type_ && spec_.type_ !=
'c') {
1818 spec_.flags_ |= CHAR_FLAG;
1819 writer_.write_int(value, spec_);
1822 if (spec_.align_ == ALIGN_NUMERIC || spec_.flags_ != 0)
1823 FMT_THROW(
FormatError(
"invalid format specifier for char"));
1824 typedef typename BasicWriter<Char>::CharPtr CharPtr;
1826 CharPtr out = CharPtr();
1827 const unsigned CHAR_WIDTH = 1;
1828 if (spec_.width_ > CHAR_WIDTH) {
1829 out = writer_.grow_buffer(spec_.width_);
1830 if (spec_.align_ == ALIGN_RIGHT) {
1831 std::uninitialized_fill_n(out, spec_.width_ - CHAR_WIDTH, fill);
1832 out += spec_.width_ - CHAR_WIDTH;
1833 }
else if (spec_.align_ == ALIGN_CENTER) {
1834 out = writer_.fill_padding(out, spec_.width_,
1835 internal::check(CHAR_WIDTH), fill);
1837 std::uninitialized_fill_n(out + CHAR_WIDTH,
1838 spec_.width_ - CHAR_WIDTH, fill);
1841 out = writer_.grow_buffer(CHAR_WIDTH);
1846 void visit_cstring(
const char *value) {
1847 if (spec_.type_ ==
'p')
1848 return write_pointer(value);
1853 writer_.write_str(value, spec_);
1859 writer_.write_str(value, spec_);
1862 void visit_pointer(
const void *value) {
1863 if (spec_.type_ && spec_.type_ !=
'p')
1864 report_unknown_type(spec_.type_,
"pointer");
1865 write_pointer(value);
1872 int next_arg_index_;
1875 FMT_API
Arg do_get_arg(
unsigned arg_index,
const char *&error);
1878 const ArgList &args()
const {
return args_; }
1882 next_arg_index_ = 0;
1886 Arg next_arg(
const char *&error) {
1887 if (next_arg_index_ >= 0)
1888 return do_get_arg(internal::to_unsigned(next_arg_index_++), error);
1889 error =
"cannot switch from manual to automatic argument indexing";
1895 Arg get_arg(
unsigned arg_index,
const char *&error) {
1896 return check_no_auto_index(error) ? do_get_arg(arg_index, error) :
Arg();
1899 bool check_no_auto_index(
const char *&error) {
1900 if (next_arg_index_ > 0) {
1901 error =
"cannot switch from automatic to manual argument indexing";
1904 next_arg_index_ = -1;
1908 template <
typename Char>
1911 w << BasicStringRef<Char>(start, internal::to_unsigned(end - start));
1916 template <
typename Char>
1919 void parse_flags(
FormatSpec &spec,
const Char *&s);
1923 Arg get_arg(
const Char *s,
1924 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
1927 unsigned parse_header(
const Char *&s,
FormatSpec &spec);
1953 template <
typename Impl,
typename Char>
1957 const Char *format_;
1971 formatter_(formatter), format_(fmt) {}
1975 c.format(&formatter_, c.value, &format_);
1980 template <
typename Char>
1981 class ArgFormatter :
public BasicArgFormatter<ArgFormatter<Char>, Char> {
1990 template <
typename CharType,
typename ArgFormatter>
1991 class BasicFormatter :
private internal::FormatterBase {
2002 using internal::FormatterBase::get_arg;
2032 const Char *format(
const Char *&format_str,
const internal::Arg &arg);
2037 # define FMT_GEN(n, f) FMT_GEN##n(f)
2038 # define FMT_GEN1(f) f(0)
2039 # define FMT_GEN2(f) FMT_GEN1(f), f(1)
2040 # define FMT_GEN3(f) FMT_GEN2(f), f(2)
2041 # define FMT_GEN4(f) FMT_GEN3(f), f(3)
2042 # define FMT_GEN5(f) FMT_GEN4(f), f(4)
2043 # define FMT_GEN6(f) FMT_GEN5(f), f(5)
2044 # define FMT_GEN7(f) FMT_GEN6(f), f(6)
2045 # define FMT_GEN8(f) FMT_GEN7(f), f(7)
2046 # define FMT_GEN9(f) FMT_GEN8(f), f(8)
2047 # define FMT_GEN10(f) FMT_GEN9(f), f(9)
2048 # define FMT_GEN11(f) FMT_GEN10(f), f(10)
2049 # define FMT_GEN12(f) FMT_GEN11(f), f(11)
2050 # define FMT_GEN13(f) FMT_GEN12(f), f(12)
2051 # define FMT_GEN14(f) FMT_GEN13(f), f(13)
2052 # define FMT_GEN15(f) FMT_GEN14(f), f(14)
2054 namespace internal {
2055 inline uint64_t make_type() {
return 0; }
2057 template <
typename T>
2058 inline uint64_t make_type(
const T &arg) {
2062 template <
unsigned N,
bool= (N < ArgList::MAX_PACKED_ARGS)>
2065 template <
unsigned N>
2067 typedef Value Type[N > 0 ? N : 1];
2069 template <
typename Formatter,
typename T>
2070 static Value make(
const T &value) {
2075 (void)result.custom.format;
2083 template <
unsigned N>
2085 typedef Arg Type[N + 1];
2087 template <
typename Formatter,
typename T>
2091 #if FMT_USE_VARIADIC_TEMPLATES
2092 template <
typename Arg,
typename... Args>
2093 inline uint64_t make_type(
const Arg &first,
const Args & ... tail) {
2094 return make_type(first) | (make_type(tail...) << 4);
2104 template <
typename T>
2105 ArgType(
const T &arg) : type(make_type(arg)) {}
2108 # define FMT_ARG_TYPE_DEFAULT(n) ArgType t##n = ArgType()
2110 inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) {
2111 return t0.type | (t1.type << 4) | (t2.type << 8) | (t3.type << 12) |
2112 (t4.type << 16) | (t5.type << 20) | (t6.type << 24) | (t7.type << 28) |
2113 (t8.type << 32) | (t9.type << 36) | (t10.type << 40) | (t11.type << 44) |
2114 (t12.type << 48) | (t13.type << 52) | (t14.type << 56);
2119 # define FMT_MAKE_TEMPLATE_ARG(n) typename T##n
2120 # define FMT_MAKE_ARG_TYPE(n) T##n
2121 # define FMT_MAKE_ARG(n) const T##n &v##n
2122 # define FMT_ASSIGN_char(n) \
2123 arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter<char> >(v##n)
2124 # define FMT_ASSIGN_wchar_t(n) \
2125 arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter<wchar_t> >(v##n)
2127 #if FMT_USE_VARIADIC_TEMPLATES
2129 # define FMT_VARIADIC_VOID(func, arg_type) \
2130 template <typename... Args> \
2131 void func(arg_type arg0, const Args & ... args) { \
2132 typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
2133 typename ArgArray::Type array{ \
2134 ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
2135 func(arg0, fmt::ArgList(fmt::internal::make_type(args...), array)); \
2139 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
2140 template <typename... Args> \
2141 ctor(arg0_type arg0, arg1_type arg1, const Args & ... args) { \
2142 typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
2143 typename ArgArray::Type array{ \
2144 ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
2145 func(arg0, arg1, fmt::ArgList(fmt::internal::make_type(args...), array)); \
2150 # define FMT_MAKE_REF(n) \
2151 fmt::internal::MakeValue< fmt::BasicFormatter<Char> >(v##n)
2152 # define FMT_MAKE_REF2(n) v##n
2156 # define FMT_WRAP1(func, arg_type, n) \
2157 template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2158 inline void func(arg_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
2159 const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
2160 func(arg1, fmt::ArgList( \
2161 fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
2165 # define FMT_VARIADIC_VOID(func, arg_type) \
2166 inline void func(arg_type arg) { func(arg, fmt::ArgList()); } \
2167 FMT_WRAP1(func, arg_type, 1) FMT_WRAP1(func, arg_type, 2) \
2168 FMT_WRAP1(func, arg_type, 3) FMT_WRAP1(func, arg_type, 4) \
2169 FMT_WRAP1(func, arg_type, 5) FMT_WRAP1(func, arg_type, 6) \
2170 FMT_WRAP1(func, arg_type, 7) FMT_WRAP1(func, arg_type, 8) \
2171 FMT_WRAP1(func, arg_type, 9) FMT_WRAP1(func, arg_type, 10)
2173 # define FMT_CTOR(ctor, func, arg0_type, arg1_type, n) \
2174 template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
2175 ctor(arg0_type arg0, arg1_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \
2176 const fmt::internal::ArgArray<n>::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \
2177 func(arg0, arg1, fmt::ArgList( \
2178 fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \
2182 # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \
2183 FMT_CTOR(ctor, func, arg0_type, arg1_type, 1) \
2184 FMT_CTOR(ctor, func, arg0_type, arg1_type, 2) \
2185 FMT_CTOR(ctor, func, arg0_type, arg1_type, 3) \
2186 FMT_CTOR(ctor, func, arg0_type, arg1_type, 4) \
2187 FMT_CTOR(ctor, func, arg0_type, arg1_type, 5) \
2188 FMT_CTOR(ctor, func, arg0_type, arg1_type, 6) \
2189 FMT_CTOR(ctor, func, arg0_type, arg1_type, 7) \
2190 FMT_CTOR(ctor, func, arg0_type, arg1_type, 8) \
2191 FMT_CTOR(ctor, func, arg0_type, arg1_type, 9) \
2192 FMT_CTOR(ctor, func, arg0_type, arg1_type, 10)
2197 #define FMT_FOR_EACH1(f, x0) f(x0, 0)
2198 #define FMT_FOR_EACH2(f, x0, x1) \
2199 FMT_FOR_EACH1(f, x0), f(x1, 1)
2200 #define FMT_FOR_EACH3(f, x0, x1, x2) \
2201 FMT_FOR_EACH2(f, x0 ,x1), f(x2, 2)
2202 #define FMT_FOR_EACH4(f, x0, x1, x2, x3) \
2203 FMT_FOR_EACH3(f, x0, x1, x2), f(x3, 3)
2204 #define FMT_FOR_EACH5(f, x0, x1, x2, x3, x4) \
2205 FMT_FOR_EACH4(f, x0, x1, x2, x3), f(x4, 4)
2206 #define FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5) \
2207 FMT_FOR_EACH5(f, x0, x1, x2, x3, x4), f(x5, 5)
2208 #define FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6) \
2209 FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5), f(x6, 6)
2210 #define FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7) \
2211 FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6), f(x7, 7)
2212 #define FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8) \
2213 FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7), f(x8, 8)
2214 #define FMT_FOR_EACH10(f, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) \
2215 FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8), f(x9, 9)
2259 init(error_code, message,
ArgList());
2263 int error_code()
const {
return error_code_; }
2284 template <
typename Char>
2288 Buffer<Char> &buffer_;
2290 FMT_DISALLOW_COPY_AND_ASSIGN(BasicWriter);
2292 typedef typename internal::CharTraits<Char>::CharPtr CharPtr;
2296 static Char *
get(CharPtr p) {
return p.base(); }
2298 static Char *
get(Char *p) {
return p; }
2303 static CharPtr fill_padding(CharPtr buffer,
2304 unsigned total_size, std::size_t content_size,
wchar_t fill);
2308 CharPtr grow_buffer(std::size_t n) {
2309 std::size_t size = buffer_.size();
2310 buffer_.resize(size + n);
2311 return internal::make_ptr(&buffer_[size], n);
2315 template <
typename UInt>
2316 Char *write_unsigned_decimal(UInt value,
unsigned prefix_size = 0) {
2317 unsigned num_digits = internal::count_digits(value);
2318 Char *ptr =
get(grow_buffer(prefix_size + num_digits));
2319 internal::format_decimal(ptr + prefix_size, value, num_digits);
2324 template <
typename Int>
2325 void write_decimal(Int value) {
2326 typedef typename internal::IntTraits<Int>::MainType MainType;
2327 MainType abs_value =
static_cast<MainType
>(value);
2328 if (internal::is_negative(value)) {
2329 abs_value = 0 - abs_value;
2330 *write_unsigned_decimal(abs_value, 1) =
'-';
2332 write_unsigned_decimal(abs_value, 0);
2337 CharPtr prepare_int_buffer(
unsigned num_digits,
2338 const EmptySpec &,
const char *prefix,
unsigned prefix_size) {
2339 unsigned size = prefix_size + num_digits;
2340 CharPtr p = grow_buffer(size);
2341 std::uninitialized_copy(prefix, prefix + prefix_size, p);
2342 return p + size - 1;
2345 template <
typename Spec>
2346 CharPtr prepare_int_buffer(
unsigned num_digits,
2347 const Spec &spec,
const char *prefix,
unsigned prefix_size);
2350 template <
typename T,
typename Spec>
2351 void write_int(T value, Spec spec);
2354 template <
typename T>
2355 void write_double(T value,
const FormatSpec &spec);
2358 template <
typename StrChar>
2359 CharPtr write_str(
const StrChar *s, std::size_t size,
const AlignSpec &spec);
2361 template <
typename StrChar>
2362 void write_str(
const internal::Arg::StringValue<StrChar> &str,
2363 const FormatSpec &spec);
2369 void operator<<(typename internal::WCharHelper<wchar_t, Char>::Unsupported);
2371 typename internal::WCharHelper<const wchar_t *, Char>::Unsupported);
2375 void append_float_length(Char *&format_ptr,
long double) {
2376 *format_ptr++ =
'L';
2379 template<
typename T>
2380 void append_float_length(Char *&, T) {}
2382 template <
typename Impl,
typename Char_>
2383 friend class internal::ArgFormatterBase;
2385 friend class internal::PrintfArgFormatter<Char>;
2404 std::size_t
size()
const {
return buffer_.size(); }
2410 const Char *
data() const FMT_NOEXCEPT {
return &buffer_[0]; }
2417 std::size_t size = buffer_.size();
2418 buffer_.reserve(size + 1);
2419 buffer_[size] =
'\0';
2428 std::basic_string<Char>
str()
const {
2429 return std::basic_string<Char>(&buffer_[0], buffer_.size());
2463 write_decimal(value);
2466 BasicWriter &operator<<(
unsigned value) {
2467 return *this << IntFormatSpec<unsigned>(value);
2469 BasicWriter &operator<<(
long value) {
2470 write_decimal(value);
2473 BasicWriter &operator<<(
unsigned long value) {
2474 return *this << IntFormatSpec<unsigned long>(value);
2476 BasicWriter &operator<<(LongLong value) {
2477 write_decimal(value);
2487 return *this << IntFormatSpec<ULongLong>(value);
2510 buffer_.push_back(value);
2516 buffer_.push_back(value);
2526 const Char *str = value.data();
2527 buffer_.append(str, str + value.size());
2533 const char *str = value.data();
2534 buffer_.append(str, str + value.size());
2538 template <
typename T,
typename Spec,
typename FillChar>
2539 BasicWriter &operator<<(IntFormatSpec<T, Spec, FillChar> spec) {
2540 internal::CharTraits<Char>::convert(FillChar());
2541 write_int(spec.value(), spec);
2545 template <
typename StrChar>
2546 BasicWriter &operator<<(const StrFormatSpec<StrChar> &spec) {
2547 const StrChar *s = spec.str();
2548 write_str(s, std::char_traits<Char>::length(s), spec);
2552 void clear() FMT_NOEXCEPT { buffer_.clear(); }
2554 Buffer<Char> &buffer() FMT_NOEXCEPT {
return buffer_; }
2557 template <
typename Char>
2558 template <
typename StrChar>
2559 typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
2560 const StrChar *s, std::size_t size,
const AlignSpec &spec) {
2561 CharPtr out = CharPtr();
2562 if (spec.width() > size) {
2563 out = grow_buffer(spec.width());
2564 Char fill = internal::CharTraits<Char>::cast(spec.fill());
2565 if (spec.align() == ALIGN_RIGHT) {
2566 std::uninitialized_fill_n(out, spec.width() - size, fill);
2567 out += spec.width() - size;
2568 }
else if (spec.align() == ALIGN_CENTER) {
2569 out = fill_padding(out, spec.width(), size, fill);
2571 std::uninitialized_fill_n(out + size, spec.width() - size, fill);
2574 out = grow_buffer(size);
2576 std::uninitialized_copy(s, s + size, out);
2580 template <
typename Char>
2581 template <
typename StrChar>
2582 void BasicWriter<Char>::write_str(
2583 const internal::Arg::StringValue<StrChar> &s,
const FormatSpec &spec) {
2585 internal::CharTraits<Char>::convert(StrChar());
2586 if (spec.type_ && spec.type_ !=
's')
2587 internal::report_unknown_type(spec.type_,
"string");
2588 const StrChar *str_value = s.value;
2589 std::size_t str_size = s.size;
2590 if (str_size == 0) {
2592 FMT_THROW(FormatError(
"string pointer is null"));
2596 std::size_t precision =
static_cast<std::size_t
>(spec.precision_);
2597 if (spec.precision_ >= 0 && precision < str_size)
2598 str_size = precision;
2599 write_str(str_value, str_size, spec);
2602 template <
typename Char>
2603 typename BasicWriter<Char>::CharPtr
2604 BasicWriter<Char>::fill_padding(
2605 CharPtr buffer,
unsigned total_size,
2606 std::size_t content_size,
wchar_t fill) {
2607 std::size_t padding = total_size - content_size;
2608 std::size_t left_padding = padding / 2;
2609 Char fill_char = internal::CharTraits<Char>::cast(fill);
2610 std::uninitialized_fill_n(buffer, left_padding, fill_char);
2611 buffer += left_padding;
2612 CharPtr content = buffer;
2613 std::uninitialized_fill_n(buffer + content_size,
2614 padding - left_padding, fill_char);
2618 template <
typename Char>
2619 template <
typename Spec>
2620 typename BasicWriter<Char>::CharPtr
2621 BasicWriter<Char>::prepare_int_buffer(
2622 unsigned num_digits,
const Spec &spec,
2623 const char *prefix,
unsigned prefix_size) {
2624 unsigned width = spec.width();
2625 Alignment align = spec.align();
2626 Char fill = internal::CharTraits<Char>::cast(spec.fill());
2627 if (spec.precision() >
static_cast<int>(num_digits)) {
2630 if (prefix_size > 0 && prefix[prefix_size - 1] ==
'0')
2632 unsigned number_size =
2633 prefix_size + internal::to_unsigned(spec.precision());
2634 AlignSpec subspec(number_size,
'0', ALIGN_NUMERIC);
2635 if (number_size >= width)
2636 return prepare_int_buffer(num_digits, subspec, prefix, prefix_size);
2637 buffer_.reserve(width);
2638 unsigned fill_size = width - number_size;
2639 if (align != ALIGN_LEFT) {
2640 CharPtr p = grow_buffer(fill_size);
2641 std::uninitialized_fill(p, p + fill_size, fill);
2643 CharPtr result = prepare_int_buffer(
2644 num_digits, subspec, prefix, prefix_size);
2645 if (align == ALIGN_LEFT) {
2646 CharPtr p = grow_buffer(fill_size);
2647 std::uninitialized_fill(p, p + fill_size, fill);
2651 unsigned size = prefix_size + num_digits;
2652 if (width <= size) {
2653 CharPtr p = grow_buffer(size);
2654 std::uninitialized_copy(prefix, prefix + prefix_size, p);
2655 return p + size - 1;
2657 CharPtr p = grow_buffer(width);
2658 CharPtr end = p + width;
2659 if (align == ALIGN_LEFT) {
2660 std::uninitialized_copy(prefix, prefix + prefix_size, p);
2662 std::uninitialized_fill(p, end, fill);
2663 }
else if (align == ALIGN_CENTER) {
2664 p = fill_padding(p, width, size, fill);
2665 std::uninitialized_copy(prefix, prefix + prefix_size, p);
2668 if (align == ALIGN_NUMERIC) {
2669 if (prefix_size != 0) {
2670 p = std::uninitialized_copy(prefix, prefix + prefix_size, p);
2671 size -= prefix_size;
2674 std::uninitialized_copy(prefix, prefix + prefix_size, end - size);
2676 std::uninitialized_fill(p, end - size, fill);
2682 template <
typename Char>
2683 template <
typename T,
typename Spec>
2684 void BasicWriter<Char>::write_int(T value, Spec spec) {
2685 unsigned prefix_size = 0;
2686 typedef typename internal::IntTraits<T>::MainType UnsignedType;
2687 UnsignedType abs_value =
static_cast<UnsignedType
>(value);
2688 char prefix[4] =
"";
2689 if (internal::is_negative(value)) {
2692 abs_value = 0 - abs_value;
2693 }
else if (spec.flag(SIGN_FLAG)) {
2694 prefix[0] = spec.flag(PLUS_FLAG) ?
'+' :
' ';
2697 switch (spec.type()) {
2699 unsigned num_digits = internal::count_digits(abs_value);
2700 CharPtr p = prepare_int_buffer(num_digits, spec, prefix, prefix_size) + 1;
2701 internal::format_decimal(
get(p), abs_value, 0);
2704 case 'x':
case 'X': {
2705 UnsignedType n = abs_value;
2706 if (spec.flag(HASH_FLAG)) {
2707 prefix[prefix_size++] =
'0';
2708 prefix[prefix_size++] = spec.type();
2710 unsigned num_digits = 0;
2713 }
while ((n >>= 4) != 0);
2714 Char *p =
get(prepare_int_buffer(
2715 num_digits, spec, prefix, prefix_size));
2717 const char *digits = spec.type() ==
'x' ?
2718 "0123456789abcdef" :
"0123456789ABCDEF";
2720 *p-- = digits[n & 0xf];
2721 }
while ((n >>= 4) != 0);
2724 case 'b':
case 'B': {
2725 UnsignedType n = abs_value;
2726 if (spec.flag(HASH_FLAG)) {
2727 prefix[prefix_size++] =
'0';
2728 prefix[prefix_size++] = spec.type();
2730 unsigned num_digits = 0;
2733 }
while ((n >>= 1) != 0);
2734 Char *p =
get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
2737 *p-- =
static_cast<Char
>(
'0' + (n & 1));
2738 }
while ((n >>= 1) != 0);
2742 UnsignedType n = abs_value;
2743 if (spec.flag(HASH_FLAG))
2744 prefix[prefix_size++] =
'0';
2745 unsigned num_digits = 0;
2748 }
while ((n >>= 3) != 0);
2749 Char *p =
get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
2752 *p-- =
static_cast<Char
>(
'0' + (n & 7));
2753 }
while ((n >>= 3) != 0);
2757 unsigned num_digits = internal::count_digits(abs_value);
2759 unsigned size =
static_cast<unsigned>(
2760 num_digits + sep.
size() * (num_digits - 1) / 3);
2761 CharPtr p = prepare_int_buffer(size, spec, prefix, prefix_size) + 1;
2762 internal::format_decimal(
get(p), abs_value, 0, internal::ThousandsSep(sep));
2766 internal::report_unknown_type(
2767 spec.type(), spec.flag(CHAR_FLAG) ?
"char" :
"integer");
2772 template <
typename Char>
2773 template <
typename T>
2774 void BasicWriter<Char>::write_double(T value,
const FormatSpec &spec) {
2776 char type = spec.type();
2782 case 'e':
case 'f':
case 'g':
case 'a':
2790 case 'E':
case 'G':
case 'A':
2794 internal::report_unknown_type(type,
"double");
2801 if (internal::FPUtil::isnegative(static_cast<double>(value))) {
2804 }
else if (spec.flag(SIGN_FLAG)) {
2805 sign = spec.flag(PLUS_FLAG) ?
'+' :
' ';
2808 if (internal::FPUtil::isnotanumber(value)) {
2811 std::size_t nan_size = 4;
2812 const char *nan = upper ?
" NAN" :
" nan";
2817 CharPtr out = write_str(nan, nan_size, spec);
2823 if (internal::FPUtil::isinfinity(value)) {
2826 std::size_t inf_size = 4;
2827 const char *inf = upper ?
" INF" :
" inf";
2832 CharPtr out = write_str(inf, inf_size, spec);
2838 std::size_t offset = buffer_.size();
2839 unsigned width = spec.width();
2841 buffer_.reserve(buffer_.size() + (width > 1u ? width : 1u));
2848 enum { MAX_FORMAT_SIZE = 10};
2849 Char format[MAX_FORMAT_SIZE];
2850 Char *format_ptr = format;
2851 *format_ptr++ =
'%';
2852 unsigned width_for_sprintf = width;
2853 if (spec.flag(HASH_FLAG))
2854 *format_ptr++ =
'#';
2855 if (spec.align() == ALIGN_CENTER) {
2856 width_for_sprintf = 0;
2858 if (spec.align() == ALIGN_LEFT)
2859 *format_ptr++ =
'-';
2861 *format_ptr++ =
'*';
2863 if (spec.precision() >= 0) {
2864 *format_ptr++ =
'.';
2865 *format_ptr++ =
'*';
2868 append_float_length(format_ptr, value);
2869 *format_ptr++ = type;
2873 Char fill = internal::CharTraits<Char>::cast(spec.fill());
2877 std::size_t buffer_size = buffer_.capacity() - offset;
2882 if (buffer_size == 0) {
2883 buffer_.reserve(offset + 1);
2884 buffer_size = buffer_.capacity() - offset;
2887 start = &buffer_[offset];
2888 int result = internal::CharTraits<Char>::format_float(
2889 start, buffer_size, format, width_for_sprintf, spec.precision(), value);
2891 n = internal::to_unsigned(result);
2892 if (offset + n < buffer_.capacity())
2894 buffer_.reserve(offset + n + 1);
2898 buffer_.reserve(buffer_.capacity() + 1);
2902 if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) ||
2904 *(start - 1) = sign;
2907 *(start - 1) = fill;
2911 if (spec.align() == ALIGN_CENTER && spec.width() > n) {
2912 width = spec.width();
2913 CharPtr p = grow_buffer(width);
2914 std::memmove(
get(p) + (width - n) / 2,
get(p), n *
sizeof(Char));
2915 fill_padding(p, spec.width(), n, fill);
2918 if (spec.fill() !=
' ' || sign) {
2919 while (*start ==
' ')
2922 *(start - 1) = sign;
2961 template <
typename Char,
typename Allocator = std::allocator<Char> >
2970 #if FMT_USE_RVALUE_REFERENCES
2987 buffer_ = std::move(other.buffer_);
3016 template <
typename Char>
3029 :
BasicWriter<Char>(buffer_), buffer_(array, size) {}
3037 template <std::
size_t SIZE>
3039 :
BasicWriter<Char>(buffer_), buffer_(array, SIZE) {}
3042 typedef BasicArrayWriter<char> ArrayWriter;
3043 typedef BasicArrayWriter<wchar_t> WArrayWriter;
3047 FMT_API
void report_system_error(
int error_code,
3048 StringRef message) FMT_NOEXCEPT;
3050 #if FMT_USE_WINDOWS_H
3053 class WindowsError :
public SystemError {
3055 FMT_API
void init(
int error_code, CStringRef format_str, ArgList args);
3086 WindowsError(
int error_code, CStringRef message) {
3087 init(error_code, message, ArgList());
3089 FMT_VARIADIC_CTOR(WindowsError, init,
int, CStringRef)
3094 FMT_API
void report_windows_error(
int error_code,
3095 StringRef message) FMT_NOEXCEPT;
3099 enum Color { BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE };
3107 FMT_API
void print_colored(Color c, CStringRef format, ArgList args);
3118 inline std::string format(CStringRef format_str, ArgList args) {
3120 w.write(format_str, args);
3124 inline std::wstring format(WCStringRef format_str, ArgList args) {
3126 w.write(format_str, args);
3139 FMT_API
void print(std::FILE *f, CStringRef format_str, ArgList args);
3150 FMT_API
void print(CStringRef format_str, ArgList args);
3152 template <
typename Char>
3153 void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args) {
3154 internal::PrintfFormatter<Char>(args).format(w, format);
3166 inline std::string sprintf(CStringRef format, ArgList args) {
3168 printf(w, format, args);
3172 inline std::wstring sprintf(WCStringRef format, ArgList args) {
3174 printf(w, format, args);
3187 FMT_API
int fprintf(std::FILE *f, CStringRef format, ArgList args);
3198 inline int printf(CStringRef format, ArgList args) {
3199 return fprintf(stdout, format, args);
3209 enum {BUFFER_SIZE = std::numeric_limits<ULongLong>::digits10 + 3};
3210 mutable char buffer_[BUFFER_SIZE];
3214 char *format_decimal(ULongLong value) {
3215 char *buffer_end = buffer_ + BUFFER_SIZE - 1;
3216 while (value >= 100) {
3220 unsigned index =
static_cast<unsigned>((value % 100) * 2);
3222 *--buffer_end = internal::Data::DIGITS[index + 1];
3223 *--buffer_end = internal::Data::DIGITS[index];
3226 *--buffer_end =
static_cast<char>(
'0' + value);
3229 unsigned index =
static_cast<unsigned>(value * 2);
3230 *--buffer_end = internal::Data::DIGITS[index + 1];
3231 *--buffer_end = internal::Data::DIGITS[index];
3235 void FormatSigned(LongLong value) {
3236 ULongLong abs_value =
static_cast<ULongLong
>(value);
3237 bool negative = value < 0;
3239 abs_value = 0 - abs_value;
3240 str_ = format_decimal(abs_value);
3246 explicit FormatInt(
int value) { FormatSigned(value); }
3247 explicit FormatInt(
long value) { FormatSigned(value); }
3248 explicit FormatInt(LongLong value) { FormatSigned(value); }
3249 explicit FormatInt(
unsigned value) : str_(format_decimal(value)) {}
3250 explicit FormatInt(
unsigned long value) : str_(format_decimal(value)) {}
3251 explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {}
3255 return internal::to_unsigned(buffer_ - str_ + BUFFER_SIZE - 1);
3262 const char *
data()
const {
return str_; }
3269 buffer_[BUFFER_SIZE - 1] =
'\0';
3278 std::string
str()
const {
return std::string(str_, size()); }
3284 template <
typename T>
3285 inline void format_decimal(
char *&buffer, T value) {
3286 typedef typename internal::IntTraits<T>::MainType MainType;
3287 MainType abs_value =
static_cast<MainType
>(value);
3288 if (internal::is_negative(value)) {
3290 abs_value = 0 - abs_value;
3292 if (abs_value < 100) {
3293 if (abs_value < 10) {
3294 *buffer++ =
static_cast<char>(
'0' + abs_value);
3297 unsigned index =
static_cast<unsigned>(abs_value * 2);
3298 *buffer++ = internal::Data::DIGITS[index];
3299 *buffer++ = internal::Data::DIGITS[index + 1];
3302 unsigned num_digits = internal::count_digits(abs_value);
3303 internal::format_decimal(buffer, abs_value, num_digits);
3304 buffer += num_digits;
3317 template <
typename T>
3318 inline internal::NamedArg<char> arg(StringRef name,
const T &arg) {
3319 return internal::NamedArg<char>(name, arg);
3322 template <
typename T>
3323 inline internal::NamedArg<wchar_t> arg(WStringRef name,
const T &arg) {
3324 return internal::NamedArg<wchar_t>(name, arg);
3329 template <
typename Char>
3330 void arg(StringRef,
const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
3331 template <typename Char>
3332 void arg(WStringRef, const internal::
NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
3340 # pragma GCC system_header
3344 #define FMT_EXPAND(args) args
3348 #define FMT_NARG(...) FMT_NARG_(__VA_ARGS__, FMT_RSEQ_N())
3349 #define FMT_NARG_(...) FMT_EXPAND(FMT_ARG_N(__VA_ARGS__))
3350 #define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
3351 #define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
3353 #define FMT_CONCAT(a, b) a##b
3354 #define FMT_FOR_EACH_(N, f, ...) \
3355 FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__))
3356 #define FMT_FOR_EACH(f, ...) \
3357 FMT_EXPAND(FMT_FOR_EACH_(FMT_NARG(__VA_ARGS__), f, __VA_ARGS__))
3359 #define FMT_ADD_ARG_NAME(type, index) type arg##index
3360 #define FMT_GET_ARG_NAME(type, index) arg##index
3362 #if FMT_USE_VARIADIC_TEMPLATES
3363 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
3364 template <typename... Args> \
3365 ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
3366 const Args & ... args) { \
3367 typedef fmt::internal::ArgArray<sizeof...(Args)> ArgArray; \
3368 typename ArgArray::Type array{ \
3369 ArgArray::template make<fmt::BasicFormatter<Char> >(args)...}; \
3370 call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), \
3371 fmt::ArgList(fmt::internal::make_type(args...), array)); \
3376 # define FMT_WRAP(Char, ReturnType, func, call, n, ...) \
3377 template <FMT_GEN(n, FMT_MAKE_TEMPLATE_ARG)> \
3378 inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \
3379 FMT_GEN(n, FMT_MAKE_ARG)) { \
3380 fmt::internal::ArgArray<n>::Type arr; \
3381 FMT_GEN(n, FMT_ASSIGN_##Char); \
3382 call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList( \
3383 fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), arr)); \
3386 # define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \
3387 inline ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__)) { \
3388 call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::ArgList()); \
3390 FMT_WRAP(Char, ReturnType, func, call, 1, __VA_ARGS__) \
3391 FMT_WRAP(Char, ReturnType, func, call, 2, __VA_ARGS__) \
3392 FMT_WRAP(Char, ReturnType, func, call, 3, __VA_ARGS__) \
3393 FMT_WRAP(Char, ReturnType, func, call, 4, __VA_ARGS__) \
3394 FMT_WRAP(Char, ReturnType, func, call, 5, __VA_ARGS__) \
3395 FMT_WRAP(Char, ReturnType, func, call, 6, __VA_ARGS__) \
3396 FMT_WRAP(Char, ReturnType, func, call, 7, __VA_ARGS__) \
3397 FMT_WRAP(Char, ReturnType, func, call, 8, __VA_ARGS__) \
3398 FMT_WRAP(Char, ReturnType, func, call, 9, __VA_ARGS__) \
3399 FMT_WRAP(Char, ReturnType, func, call, 10, __VA_ARGS__) \
3400 FMT_WRAP(Char, ReturnType, func, call, 11, __VA_ARGS__) \
3401 FMT_WRAP(Char, ReturnType, func, call, 12, __VA_ARGS__) \
3402 FMT_WRAP(Char, ReturnType, func, call, 13, __VA_ARGS__) \
3403 FMT_WRAP(Char, ReturnType, func, call, 14, __VA_ARGS__) \
3404 FMT_WRAP(Char, ReturnType, func, call, 15, __VA_ARGS__)
3405 #endif // FMT_USE_VARIADIC_TEMPLATES
3434 #define FMT_VARIADIC(ReturnType, func, ...) \
3435 FMT_VARIADIC_(char, ReturnType, func, return func, __VA_ARGS__)
3437 #define FMT_VARIADIC_W(ReturnType, func, ...) \
3438 FMT_VARIADIC_(wchar_t, ReturnType, func, return func, __VA_ARGS__)
3440 #define FMT_CAPTURE_ARG_(id, index) ::fmt::arg(#id, id)
3442 #define FMT_CAPTURE_ARG_W_(id, index) ::fmt::arg(L###id, id)
3458 #define FMT_CAPTURE(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_, __VA_ARGS__)
3460 #define FMT_CAPTURE_W(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_W_, __VA_ARGS__)
3463 FMT_VARIADIC(std::string, format, CStringRef)
3464 FMT_VARIADIC_W(std::wstring, format, WCStringRef)
3465 FMT_VARIADIC(
void, print, CStringRef)
3466 FMT_VARIADIC(
void, print, std::FILE *, CStringRef)
3468 FMT_VARIADIC(
void, print_colored, Color, CStringRef)
3469 FMT_VARIADIC(std::
string, sprintf, CStringRef)
3470 FMT_VARIADIC_W(std::wstring, sprintf, WCStringRef)
3471 FMT_VARIADIC(
int, printf, CStringRef)
3472 FMT_VARIADIC(
int, fprintf, std::FILE *, CStringRef)
3474 namespace internal {
3475 template <
typename Char>
3476 inline bool is_name_start(Char c) {
3477 return (
'a' <= c && c <=
'z') || (
'A' <= c && c <=
'Z') ||
'_' == c;
3482 template <
typename Char>
3483 unsigned parse_nonnegative_int(
const Char *&s) {
3484 assert(
'0' <= *s && *s <=
'9');
3487 unsigned new_value = value * 10 + (*s++ -
'0');
3489 if (new_value < value) {
3490 value = (std::numeric_limits<unsigned>::max)();
3494 }
while (
'0' <= *s && *s <=
'9');
3496 unsigned max_int = (std::numeric_limits<int>::max)();
3497 if (value > max_int)
3498 FMT_THROW(FormatError(
"number is too big"));
3502 inline void require_numeric_argument(
const Arg &arg,
char spec) {
3503 if (arg.type > Arg::LAST_NUMERIC_TYPE) {
3504 std::string message =
3505 fmt::format(
"format specifier '{}' requires numeric argument", spec);
3510 template <
typename Char>
3511 void check_sign(
const Char *&s,
const Arg &arg) {
3512 char sign =
static_cast<char>(*s);
3513 require_numeric_argument(arg, sign);
3514 if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) {
3515 FMT_THROW(FormatError(fmt::format(
3516 "format specifier '{}' requires signed argument", sign)));
3522 template <
typename Char,
typename AF>
3523 inline internal::Arg BasicFormatter<Char, AF>::get_arg(
3524 BasicStringRef<Char> arg_name,
const char *&error) {
3525 if (check_no_auto_index(error)) {
3527 const internal::Arg *arg = map_.find(arg_name);
3530 error =
"argument not found";
3532 return internal::Arg();
3535 template <
typename Char,
typename AF>
3536 inline internal::Arg BasicFormatter<Char, AF>::parse_arg_index(
const Char *&s) {
3537 const char *error = 0;
3538 internal::Arg arg = *s < '0' || *s >
'9' ?
3539 next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error);
3541 FMT_THROW(FormatError(
3542 *s !=
'}' && *s !=
':' ?
"invalid format string" : error));
3547 template <
typename Char,
typename AF>
3548 inline internal::Arg BasicFormatter<Char, AF>::parse_arg_name(
const Char *&s) {
3549 assert(internal::is_name_start(*s));
3550 const Char *start = s;
3554 }
while (internal::is_name_start(c) || (
'0' <= c && c <=
'9'));
3555 const char *error = 0;
3556 internal::Arg arg = get_arg(BasicStringRef<Char>(start, s - start), error);
3558 FMT_THROW(FormatError(error));
3562 template <
typename Char,
typename ArgFormatter>
3563 const Char *BasicFormatter<Char, ArgFormatter>::format(
3564 const Char *&format_str,
const internal::Arg &arg) {
3565 using internal::Arg;
3566 const Char *s = format_str;
3569 if (arg.type == Arg::CUSTOM) {
3570 arg.custom.format(
this, arg.custom.value, &s);
3576 const Char *p = s + 1;
3577 spec.align_ = ALIGN_DEFAULT;
3581 spec.align_ = ALIGN_LEFT;
3584 spec.align_ = ALIGN_RIGHT;
3587 spec.align_ = ALIGN_NUMERIC;
3590 spec.align_ = ALIGN_CENTER;
3593 if (spec.align_ != ALIGN_DEFAULT) {
3595 if (c ==
'}')
break;
3597 FMT_THROW(FormatError(
"invalid fill character '{'"));
3601 if (spec.align_ == ALIGN_NUMERIC)
3602 require_numeric_argument(arg,
'=');
3612 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
3616 spec.flags_ |= MINUS_FLAG;
3620 spec.flags_ |= SIGN_FLAG;
3625 require_numeric_argument(arg,
'#');
3626 spec.flags_ |= HASH_FLAG;
3632 require_numeric_argument(arg,
'0');
3633 spec.align_ = ALIGN_NUMERIC;
3639 if (
'0' <= *s && *s <=
'9') {
3640 spec.width_ = internal::parse_nonnegative_int(s);
3641 }
else if (*s ==
'{') {
3643 Arg width_arg = internal::is_name_start(*s) ?
3644 parse_arg_name(s) : parse_arg_index(s);
3646 FMT_THROW(FormatError(
"invalid format string"));
3647 ULongLong value = 0;
3648 switch (width_arg.type) {
3650 if (width_arg.int_value < 0)
3651 FMT_THROW(FormatError(
"negative width"));
3652 value = width_arg.int_value;
3655 value = width_arg.uint_value;
3657 case Arg::LONG_LONG:
3658 if (width_arg.long_long_value < 0)
3659 FMT_THROW(FormatError(
"negative width"));
3660 value = width_arg.long_long_value;
3662 case Arg::ULONG_LONG:
3663 value = width_arg.ulong_long_value;
3666 FMT_THROW(FormatError(
"width is not integer"));
3668 if (value > (std::numeric_limits<int>::max)())
3669 FMT_THROW(FormatError(
"number is too big"));
3670 spec.width_ =
static_cast<int>(value);
3676 spec.precision_ = 0;
3677 if (
'0' <= *s && *s <=
'9') {
3678 spec.precision_ = internal::parse_nonnegative_int(s);
3679 }
else if (*s ==
'{') {
3681 Arg precision_arg = internal::is_name_start(*s) ?
3682 parse_arg_name(s) : parse_arg_index(s);
3684 FMT_THROW(FormatError(
"invalid format string"));
3685 ULongLong value = 0;
3686 switch (precision_arg.type) {
3688 if (precision_arg.int_value < 0)
3689 FMT_THROW(FormatError(
"negative precision"));
3690 value = precision_arg.int_value;
3693 value = precision_arg.uint_value;
3695 case Arg::LONG_LONG:
3696 if (precision_arg.long_long_value < 0)
3697 FMT_THROW(FormatError(
"negative precision"));
3698 value = precision_arg.long_long_value;
3700 case Arg::ULONG_LONG:
3701 value = precision_arg.ulong_long_value;
3704 FMT_THROW(FormatError(
"precision is not integer"));
3706 if (value > (std::numeric_limits<int>::max)())
3707 FMT_THROW(FormatError(
"number is too big"));
3708 spec.precision_ =
static_cast<int>(value);
3710 FMT_THROW(FormatError(
"missing precision specifier"));
3712 if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) {
3713 FMT_THROW(FormatError(
3714 fmt::format(
"precision not allowed in {} format specifier",
3715 arg.type == Arg::POINTER ?
"pointer" :
"integer")));
3720 if (*s !=
'}' && *s)
3721 spec.type_ =
static_cast<char>(*s++);
3725 FMT_THROW(FormatError(
"missing '}' in format string"));
3728 ArgFormatter(*
this, spec, s - 1).visit(arg);
3732 template <
typename Char,
typename AF>
3735 const Char *start = s;
3738 if (c !=
'{' && c !=
'}')
continue;
3740 write(writer_, start, s);
3745 FMT_THROW(
FormatError(
"unmatched '}' in format string"));
3746 write(writer_, start, s - 1);
3748 parse_arg_name(s) : parse_arg_index(s);
3749 start = s = format(s, arg);
3751 write(writer_, start, s);
3755 #if FMT_USE_USER_DEFINED_LITERALS
3757 namespace internal {
3759 template <
typename Char>
3763 template <
typename... Args>
3764 auto operator()(Args && ... args) const
3765 -> decltype(format(str, std::forward<Args>(args)...)) {
3766 return format(str, std::forward<Args>(args)...);
3770 template <
typename Char>
3774 template <
typename T>
3776 return {str, std::forward<T>(value)};
3782 inline namespace literals {
3794 inline internal::UdlFormat<char>
3795 operator"" _format(
const char *s, std::size_t) {
return {s}; }
3796 inline internal::UdlFormat<wchar_t>
3797 operator"" _format(
const wchar_t *s, std::size_t) {
return {s}; }
3809 inline internal::UdlArg<char>
3810 operator"" _a(
const char *s, std::size_t) {
return {s}; }
3811 inline internal::UdlArg<wchar_t>
3812 operator"" _a(
const wchar_t *s, std::size_t) {
return {s}; }
3816 #endif // FMT_USE_USER_DEFINED_LITERALS
3819 #if FMT_GCC_VERSION >= 406
3820 # pragma GCC diagnostic pop
3823 #if defined(__clang__) && !defined(FMT_ICC_VERSION)
3824 # pragma clang diagnostic pop
3827 #ifdef FMT_HEADER_ONLY
3828 # define FMT_FUNC inline
3829 # include "format.cc"
3834 #endif // FMT_FORMAT_H_
Result visit_custom(Arg::CustomValue)
Definition: format.h:1478
std::size_t size() const
Definition: format.h:448
Definition: format.h:1059
BasicCStringRef(const Char *s)
Definition: format.h:514
Result visit_string(Arg::StringValue< char >)
Definition: format.h:1463
Result visit_pointer(const void *)
Definition: format.h:1473
Definition: format.h:1037
BasicWriter & operator<<(long double value)
Definition: format.h:2501
Definition: format.h:1303
Definition: format.h:2063
Result visit_uint(unsigned value)
Definition: format.h:1416
BasicCStringRef(const std::basic_string< Char > &s)
Definition: format.h:521
Result visit_bool(bool value)
Definition: format.h:1426
Definition: format.h:1268
const Char * data() const FMT_NOEXCEPT
Definition: format.h:2410
Result visit_ulong_long(ULongLong value)
Definition: format.h:1421
Result visit_any_int(T)
Definition: format.h:1437
SystemError(int error_code, CStringRef message)
Definition: format.h:2258
const Char * c_str() const
Definition: format.h:2416
Result visit_long_long(LongLong value)
Definition: format.h:1411
Definition: format.h:1024
Result visit_any_double(T)
Definition: format.h:1453
Definition: format.h:1576
Definition: format.h:1393
const Char * c_str() const
Definition: format.h:524
internal::Arg operator[](unsigned index) const
Definition: format.h:1340
Definition: format.h:3017
const Char * data() const
Definition: format.h:445
BasicStringRef(const Char *s)
Definition: format.h:424
BasicStringRef(const std::basic_string< Char > &s)
Definition: format.h:432
BasicStringRef(const Char *s, std::size_t size)
Definition: format.h:416
Result visit_wstring(Arg::StringValue< wchar_t >)
Definition: format.h:1468
Result visit_double(double value)
Definition: format.h:1442
Result visit_long_double(long double value)
Definition: format.h:1447
void write(BasicCStringRef< Char > format, ArgList args)
Definition: format.h:2457
Result visit_char(int value)
Definition: format.h:1431
BasicArrayWriter(Char(&array)[SIZE])
Definition: format.h:3038
void reserve(std::size_t capacity)
Definition: format.h:628
Definition: format.h:1064
Definition: format.h:1092
Definition: format.h:1290
Definition: format.h:1563
BasicWriter & operator<<(char value)
Definition: format.h:2509
Definition: format.h:1540
Definition: format.h:1029
Result visit_int(int value)
Definition: format.h:1406
Definition: format.h:2962
Definition: format.h:1536
Definition: format.h:1299
Result visit_cstring(const char *)
Definition: format.h:1458
std::size_t capacity() const
Definition: format.h:612
void resize(std::size_t new_size)
Definition: format.h:617
BasicWriter & operator<<(ULongLong value)
Definition: format.h:2486
Result visit(const Arg &arg)
Definition: format.h:1490
Definition: format.h:1105
BasicWriter(Buffer< Char > &b)
Definition: format.h:2391
std::size_t size() const
Definition: format.h:609
BasicArrayWriter(Char *array, std::size_t size)
Definition: format.h:3028
Definition: format.h:2099
Definition: format.h:2221
Definition: format.h:1098
Definition: format.h:1032
virtual ~BasicWriter()
Definition: format.h:2399
std::basic_string< Char > str() const
Definition: format.h:2428
Definition: format.h:1077
std::basic_string< Char > to_string() const
Definition: format.h:440
Definition: format.h:1112
Definition: format.h:1550
std::size_t size() const
Definition: format.h:2404