127 #ifndef TINYFORMAT_H_INCLUDED
128 #define TINYFORMAT_H_INCLUDED
152 #ifndef TINYFORMAT_ASSERT
154 # define TINYFORMAT_ASSERT(cond) assert(cond)
157 #ifndef TINYFORMAT_ERROR
159 # define TINYFORMAT_ERROR(reason) assert(0 && reason)
162 #if !defined(TINYFORMAT_USE_VARIADIC_TEMPLATES) && !defined(TINYFORMAT_NO_VARIADIC_TEMPLATES)
163 # ifdef __GXX_EXPERIMENTAL_CXX0X__
164 # define TINYFORMAT_USE_VARIADIC_TEMPLATES
168 #if defined(__GLIBCXX__) && __GLIBCXX__ < 20080201
171 # define TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
177 # define TINYFORMAT_HIDDEN __attribute__((visibility("hidden")))
179 # define TINYFORMAT_HIDDEN
183 #pragma warning(disable : 4127)
192 template <
typename T1,
typename T2>
202 static const T1&
makeT1();
206 # pragma warning(push)
207 # pragma warning(disable:4244)
208 # pragma warning(disable:4267)
217 # pragma warning(pop)
232 template<typename T, typename fmtT, bool convertible = is_convertible<T, fmtT>::value>
239 template<
typename T,
typename fmtT>
242 static void invoke(std::ostream& out,
const T& value)
243 { out << static_cast<fmtT>(value); }
246 #ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
247 template<typename T, bool convertible = is_convertible<T, int>::value>
248 struct formatZeroIntegerWorkaround
250 static bool invoke(std::ostream& ,
const T& ) {
return false; }
253 struct formatZeroIntegerWorkaround<T,true>
255 static bool invoke(std::ostream& out,
const T& value)
257 if (
static_cast<int>(value) == 0 && out.flags() & std::ios::showpos) {
264 #endif // TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
268 template<typename T, bool convertible = is_convertible<T,int>::value>
274 "integer for use as variable width or precision");
282 static int invoke(
const T& value) {
return static_cast<int>(value); }
289 std::ostringstream tmp;
291 std::string result = tmp.str();
292 out.write(result.c_str(), (std::min)(ntrunc,
static_cast<int>(result.size())));
294 #define TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(type) \
295 inline void formatTruncated(std::ostream& out, type* value, int ntrunc) \
297 std::streamsize len = 0; \
298 while (len < ntrunc && value[len] != 0) \
300 out.write(value, len); \
306 #undef TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR
329 const char* fmtEnd,
int ntrunc,
const T& value)
331 #ifndef TINYFORMAT_ALLOW_WCHAR_STRINGS
344 if (canConvertToChar && *(fmtEnd-1) ==
'c')
346 else if (canConvertToVoidPtr && *(fmtEnd-1) ==
'p')
348 #ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
349 else if (detail::formatZeroIntegerWorkaround<T>::invoke(out, value)) ;
351 else if (ntrunc >= 0) {
362 #define TINYFORMAT_DEFINE_FORMATVALUE_CHAR(charType) \
363 inline void formatValue(std::ostream& out, const char* , \
364 const char* fmtEnd, int , charType value) \
366 switch (*(fmtEnd-1)) { \
367 case 'u': case 'd': case 'i': case 'o': case 'X': case 'x': \
368 out << static_cast<int>(value); break; \
370 out << value; break; \
377 #undef TINYFORMAT_DEFINE_FORMATVALUE_CHAR
385 #define TINYFORMAT_ARGTYPES(n) TINYFORMAT_ARGTYPES_ ## n
386 #define TINYFORMAT_VARARGS(n) TINYFORMAT_VARARGS_ ## n
387 #define TINYFORMAT_PASSARGS(n) TINYFORMAT_PASSARGS_ ## n
388 #define TINYFORMAT_PASSARGS_TAIL(n) TINYFORMAT_PASSARGS_TAIL_ ## n
426 #define TINYFORMAT_ARGTYPES_1 class T1
427 #define TINYFORMAT_ARGTYPES_2 class T1, class T2
428 #define TINYFORMAT_ARGTYPES_3 class T1, class T2, class T3
429 #define TINYFORMAT_ARGTYPES_4 class T1, class T2, class T3, class T4
430 #define TINYFORMAT_ARGTYPES_5 class T1, class T2, class T3, class T4, class T5
431 #define TINYFORMAT_ARGTYPES_6 class T1, class T2, class T3, class T4, class T5, class T6
432 #define TINYFORMAT_ARGTYPES_7 class T1, class T2, class T3, class T4, class T5, class T6, class T7
433 #define TINYFORMAT_ARGTYPES_8 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8
434 #define TINYFORMAT_ARGTYPES_9 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9
435 #define TINYFORMAT_ARGTYPES_10 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10
436 #define TINYFORMAT_ARGTYPES_11 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11
437 #define TINYFORMAT_ARGTYPES_12 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12
438 #define TINYFORMAT_ARGTYPES_13 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13
439 #define TINYFORMAT_ARGTYPES_14 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14
440 #define TINYFORMAT_ARGTYPES_15 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15
441 #define TINYFORMAT_ARGTYPES_16 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15, class T16
443 #define TINYFORMAT_VARARGS_1 const T1& v1
444 #define TINYFORMAT_VARARGS_2 const T1& v1, const T2& v2
445 #define TINYFORMAT_VARARGS_3 const T1& v1, const T2& v2, const T3& v3
446 #define TINYFORMAT_VARARGS_4 const T1& v1, const T2& v2, const T3& v3, const T4& v4
447 #define TINYFORMAT_VARARGS_5 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5
448 #define TINYFORMAT_VARARGS_6 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6
449 #define TINYFORMAT_VARARGS_7 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7
450 #define TINYFORMAT_VARARGS_8 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8
451 #define TINYFORMAT_VARARGS_9 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9
452 #define TINYFORMAT_VARARGS_10 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10
453 #define TINYFORMAT_VARARGS_11 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11
454 #define TINYFORMAT_VARARGS_12 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12
455 #define TINYFORMAT_VARARGS_13 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12, const T13& v13
456 #define TINYFORMAT_VARARGS_14 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12, const T13& v13, const T14& v14
457 #define TINYFORMAT_VARARGS_15 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12, const T13& v13, const T14& v14, const T15& v15
458 #define TINYFORMAT_VARARGS_16 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12, const T13& v13, const T14& v14, const T15& v15, const T16& v16
460 #define TINYFORMAT_PASSARGS_1 v1
461 #define TINYFORMAT_PASSARGS_2 v1, v2
462 #define TINYFORMAT_PASSARGS_3 v1, v2, v3
463 #define TINYFORMAT_PASSARGS_4 v1, v2, v3, v4
464 #define TINYFORMAT_PASSARGS_5 v1, v2, v3, v4, v5
465 #define TINYFORMAT_PASSARGS_6 v1, v2, v3, v4, v5, v6
466 #define TINYFORMAT_PASSARGS_7 v1, v2, v3, v4, v5, v6, v7
467 #define TINYFORMAT_PASSARGS_8 v1, v2, v3, v4, v5, v6, v7, v8
468 #define TINYFORMAT_PASSARGS_9 v1, v2, v3, v4, v5, v6, v7, v8, v9
469 #define TINYFORMAT_PASSARGS_10 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10
470 #define TINYFORMAT_PASSARGS_11 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11
471 #define TINYFORMAT_PASSARGS_12 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12
472 #define TINYFORMAT_PASSARGS_13 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13
473 #define TINYFORMAT_PASSARGS_14 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14
474 #define TINYFORMAT_PASSARGS_15 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15
475 #define TINYFORMAT_PASSARGS_16 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16
477 #define TINYFORMAT_PASSARGS_TAIL_1
478 #define TINYFORMAT_PASSARGS_TAIL_2 , v2
479 #define TINYFORMAT_PASSARGS_TAIL_3 , v2, v3
480 #define TINYFORMAT_PASSARGS_TAIL_4 , v2, v3, v4
481 #define TINYFORMAT_PASSARGS_TAIL_5 , v2, v3, v4, v5
482 #define TINYFORMAT_PASSARGS_TAIL_6 , v2, v3, v4, v5, v6
483 #define TINYFORMAT_PASSARGS_TAIL_7 , v2, v3, v4, v5, v6, v7
484 #define TINYFORMAT_PASSARGS_TAIL_8 , v2, v3, v4, v5, v6, v7, v8
485 #define TINYFORMAT_PASSARGS_TAIL_9 , v2, v3, v4, v5, v6, v7, v8, v9
486 #define TINYFORMAT_PASSARGS_TAIL_10 , v2, v3, v4, v5, v6, v7, v8, v9, v10
487 #define TINYFORMAT_PASSARGS_TAIL_11 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11
488 #define TINYFORMAT_PASSARGS_TAIL_12 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12
489 #define TINYFORMAT_PASSARGS_TAIL_13 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13
490 #define TINYFORMAT_PASSARGS_TAIL_14 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14
491 #define TINYFORMAT_PASSARGS_TAIL_15 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15
492 #define TINYFORMAT_PASSARGS_TAIL_16 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16
494 #define TINYFORMAT_FOREACH_ARGNUM(m) \
495 m(1) m(2) m(3) m(4) m(5) m(6) m(7) m(8) m(9) m(10) m(11) m(12) m(13) m(14) m(15) m(16)
513 :
m_value(static_cast<const void*>(&value)),
518 void format(std::ostream& out,
const char* fmtBegin,
519 const char* fmtEnd,
int ntrunc)
const
536 const char* fmtEnd,
int ntrunc,
const void* value)
538 formatValue(out, fmtBegin, fmtEnd, ntrunc, *
static_cast<const T*
>(value));
549 const char* fmtEnd,
int ntrunc,
const void* value) =
nullptr;
559 for (;*c >=
'0' && *c <=
'9'; ++c)
560 i = 10*
i + (*c -
'0');
571 int& argIndex,
int numArgs)
573 if (*c >=
'0' && *c <=
'9') {
576 else if (*c ==
'*') {
579 if (positionalMode) {
582 TINYFORMAT_ERROR(
"tinyformat: Non-positional argument used after a positional one");
583 if (pos >= 0 && pos < numArgs)
590 if (argIndex < numArgs)
591 n = args[argIndex++].
toInt();
593 TINYFORMAT_ERROR(
"tinyformat: Not enough arguments to read variable width or precision");
612 out.write(fmt, c - fmt);
615 else if (*c ==
'%') {
616 out.write(fmt, c - fmt);
660 bool& spacePadPositive,
661 int& ntrunc,
const char* fmtStart,
663 int& argIndex,
int numArgs)
671 out.unsetf(std::ios::adjustfield | std::ios::basefield |
672 std::ios::floatfield | std::ios::showbase | std::ios::boolalpha |
673 std::ios::showpoint | std::ios::showpos | std::ios::uppercase);
674 bool precisionSet =
false;
675 bool widthSet =
false;
677 const char* c = fmtStart + 1;
681 if (*c >=
'0' && *c <=
'9') {
682 const char tmpc = *c;
686 if (value > 0 && value <= numArgs)
687 argIndex = value - 1;
691 positionalMode =
true;
693 else if (positionalMode) {
694 TINYFORMAT_ERROR(
"tinyformat: Non-positional argument used after a positional one");
701 out.setf(std::ios::internal, std::ios::adjustfield);
710 else if (positionalMode) {
711 TINYFORMAT_ERROR(
"tinyformat: Non-positional argument used after a positional one");
719 out.setf(std::ios::showpoint | std::ios::showbase);
723 if (!(out.flags() & std::ios::left)) {
727 out.setf(std::ios::internal, std::ios::adjustfield);
732 out.setf(std::ios::left, std::ios::adjustfield);
736 if (!(out.flags() & std::ios::showpos))
737 spacePadPositive =
true;
740 out.setf(std::ios::showpos);
741 spacePadPositive =
false;
752 args, argIndex, numArgs);
757 out.setf(std::ios::left, std::ios::adjustfield);
768 args, argIndex, numArgs);
771 precisionSet = precision >= 0;
773 out.precision(precision);
776 while (*c ==
'l' || *c ==
'h' || *c ==
'L' ||
777 *c ==
'j' || *c ==
'z' || *c ==
't') {
783 bool intConversion =
false;
785 case 'u':
case 'd':
case 'i':
786 out.setf(std::ios::dec, std::ios::basefield);
787 intConversion =
true;
790 out.setf(std::ios::oct, std::ios::basefield);
791 intConversion =
true;
794 out.setf(std::ios::uppercase);
797 out.setf(std::ios::hex, std::ios::basefield);
798 intConversion =
true;
801 out.setf(std::ios::uppercase);
804 out.setf(std::ios::scientific, std::ios::floatfield);
805 out.setf(std::ios::dec, std::ios::basefield);
808 out.setf(std::ios::uppercase);
811 out.setf(std::ios::fixed, std::ios::floatfield);
814 out.setf(std::ios::uppercase);
823 out.setf(std::ios::fixed | std::ios::scientific, std::ios::floatfield);
826 out.setf(std::ios::uppercase);
829 out.setf(std::ios::dec, std::ios::basefield);
831 out.flags(out.flags() & ~std::ios::floatfield);
838 ntrunc =
static_cast<int>(out.precision());
840 out.setf(std::ios::boolalpha);
848 "terminated by end of string");
853 if (intConversion && precisionSet && !widthSet) {
858 out.width(out.precision() + widthExtra);
859 out.setf(std::ios::internal, std::ios::adjustfield);
872 std::streamsize origWidth = out.width();
873 std::streamsize origPrecision = out.precision();
874 std::ios::fmtflags origFlags = out.flags();
875 char origFill = out.fill();
879 bool positionalMode =
false;
884 if (!positionalMode && argIndex < numArgs) {
885 TINYFORMAT_ERROR(
"tinyformat: Not enough conversion specifiers in format string");
889 bool spacePadPositive =
false;
892 args, argIndex, numArgs);
895 if (argIndex >= numArgs) {
896 TINYFORMAT_ERROR(
"tinyformat: Too many conversion specifiers in format string");
901 if (!spacePadPositive) {
902 arg.
format(out, fmt, fmtEnd, ntrunc);
909 std::ostringstream tmpStream;
910 tmpStream.copyfmt(out);
911 tmpStream.setf(std::ios::showpos);
912 arg.
format(tmpStream, fmt, fmtEnd, ntrunc);
913 std::string result = tmpStream.str();
914 for (
char &
i : result) {
926 out.width(origWidth);
927 out.precision(origPrecision);
928 out.flags(origFlags);
947 friend void vformat(std::ostream& out,
const char* fmt,
966 #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
967 template<
typename... Args>
971 { static_assert(
sizeof...(args) == N,
"Number of args must be N"); }
972 #else // C++98 version
974 # define TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR(n) \
976 template<TINYFORMAT_ARGTYPES(n)> \
977 FormatListN(TINYFORMAT_VARARGS(n)) \
978 : FormatList(&m_formatterStore[0], n) \
979 { TINYFORMAT_ASSERT(n == N); init(0, TINYFORMAT_PASSARGS(n)); } \
981 template<TINYFORMAT_ARGTYPES(n)> \
982 void init(int i, TINYFORMAT_VARARGS(n)) \
984 m_formatterStore[i] = FormatArg(v1); \
985 init(i+1 TINYFORMAT_PASSARGS_TAIL(n)); \
989 # undef TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR
1012 #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
1020 template<
typename... Args>
1021 detail::FormatListN<
sizeof...(Args)>
makeFormatList(
const Args&... args)
1023 return detail::FormatListN<
sizeof...(args)>(args...);
1026 #else // C++98 version
1032 #define TINYFORMAT_MAKE_MAKEFORMATLIST(n) \
1033 template<TINYFORMAT_ARGTYPES(n)> \
1034 detail::FormatListN<n> makeFormatList(TINYFORMAT_VARARGS(n)) \
1036 return detail::FormatListN<n>(TINYFORMAT_PASSARGS(n)); \
1039 #undef TINYFORMAT_MAKE_MAKEFORMATLIST
1053 #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
1056 template<
typename... Args>
1057 void format(std::ostream& out,
const char* fmt,
const Args&... args)
1064 template<
typename... Args>
1065 std::string
format(
const char* fmt,
const Args&... args)
1067 std::ostringstream oss;
1068 format(oss, fmt, args...);
1073 template<
typename... Args>
1074 void printf(
const char* fmt,
const Args&... args)
1076 format(std::cout, fmt, args...);
1079 template<
typename... Args>
1080 void printfln(
const char* fmt,
const Args&... args)
1082 format(std::cout, fmt, args...);
1087 #else // C++98 version
1089 inline void format(std::ostream& out,
const char* fmt)
1096 std::ostringstream oss;
1112 #define TINYFORMAT_MAKE_FORMAT_FUNCS(n) \
1114 template<TINYFORMAT_ARGTYPES(n)> \
1115 void format(std::ostream& out, const char* fmt, TINYFORMAT_VARARGS(n)) \
1117 vformat(out, fmt, makeFormatList(TINYFORMAT_PASSARGS(n))); \
1120 template<TINYFORMAT_ARGTYPES(n)> \
1121 std::string format(const char* fmt, TINYFORMAT_VARARGS(n)) \
1123 std::ostringstream oss; \
1124 format(oss, fmt, TINYFORMAT_PASSARGS(n)); \
1128 template<TINYFORMAT_ARGTYPES(n)> \
1129 void printf(const char* fmt, TINYFORMAT_VARARGS(n)) \
1131 format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \
1134 template<TINYFORMAT_ARGTYPES(n)> \
1135 void printfln(const char* fmt, TINYFORMAT_VARARGS(n)) \
1137 format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \
1138 std::cout << '\n'; \
1142 #undef TINYFORMAT_MAKE_FORMAT_FUNCS
1150 #pragma warning(default : 4127)
1153 #endif // TINYFORMAT_H_INCLUDED