Bitcoin Core  27.99.0
P2P Digital Currency
tinyformat.h
Go to the documentation of this file.
1 // tinyformat.h
2 // Copyright (C) 2011, Chris Foster [chris42f (at) gmail (d0t) com]
3 //
4 // Boost Software License - Version 1.0
5 //
6 // Permission is hereby granted, free of charge, to any person or organization
7 // obtaining a copy of the software and accompanying documentation covered by
8 // this license (the "Software") to use, reproduce, display, distribute,
9 // execute, and transmit the Software, and to prepare derivative works of the
10 // Software, and to permit third-parties to whom the Software is furnished to
11 // do so, all subject to the following:
12 //
13 // The copyright notices in the Software and this entire statement, including
14 // the above license grant, this restriction and the following disclaimer,
15 // must be included in all copies of the Software, in whole or in part, and
16 // all derivative works of the Software, unless such copies or derivative
17 // works are solely in the form of machine-executable object code generated by
18 // a source language processor.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 // DEALINGS IN THE SOFTWARE.
27 
28 //------------------------------------------------------------------------------
29 // Tinyformat: A minimal type safe printf replacement
30 //
31 // tinyformat.h is a type safe printf replacement library in a single C++
32 // header file. Design goals include:
33 //
34 // * Type safety and extensibility for user defined types.
35 // * C99 printf() compatibility, to the extent possible using std::ostream
36 // * POSIX extension for positional arguments
37 // * Simplicity and minimalism. A single header file to include and distribute
38 // with your projects.
39 // * Augment rather than replace the standard stream formatting mechanism
40 // * C++98 support, with optional C++11 niceties
41 //
42 //
43 // Main interface example usage
44 // ----------------------------
45 //
46 // To print a date to std::cout for American usage:
47 //
48 // std::string weekday = "Wednesday";
49 // const char* month = "July";
50 // size_t day = 27;
51 // long hour = 14;
52 // int min = 44;
53 //
54 // tfm::printf("%s, %s %d, %.2d:%.2d\n", weekday, month, day, hour, min);
55 //
56 // POSIX extension for positional arguments is available.
57 // The ability to rearrange formatting arguments is an important feature
58 // for localization because the word order may vary in different languages.
59 //
60 // Previous example for German usage. Arguments are reordered:
61 //
62 // tfm::printf("%1$s, %3$d. %2$s, %4$d:%5$.2d\n", weekday, month, day, hour, min);
63 //
64 // The strange types here emphasize the type safety of the interface; it is
65 // possible to print a std::string using the "%s" conversion, and a
66 // size_t using the "%d" conversion. A similar result could be achieved
67 // using either of the tfm::format() functions. One prints on a user provided
68 // stream:
69 //
70 // tfm::format(std::cerr, "%s, %s %d, %.2d:%.2d\n",
71 // weekday, month, day, hour, min);
72 //
73 // The other returns a std::string:
74 //
75 // std::string date = tfm::format("%s, %s %d, %.2d:%.2d\n",
76 // weekday, month, day, hour, min);
77 // std::cout << date;
78 //
79 // These are the three primary interface functions. There is also a
80 // convenience function printfln() which appends a newline to the usual result
81 // of printf() for super simple logging.
82 //
83 //
84 // User defined format functions
85 // -----------------------------
86 //
87 // Simulating variadic templates in C++98 is pretty painful since it requires
88 // writing out the same function for each desired number of arguments. To make
89 // this bearable tinyformat comes with a set of macros which are used
90 // internally to generate the API, but which may also be used in user code.
91 //
92 // The three macros TINYFORMAT_ARGTYPES(n), TINYFORMAT_VARARGS(n) and
93 // TINYFORMAT_PASSARGS(n) will generate a list of n argument types,
94 // type/name pairs and argument names respectively when called with an integer
95 // n between 1 and 16. We can use these to define a macro which generates the
96 // desired user defined function with n arguments. To generate all 16 user
97 // defined function bodies, use the macro TINYFORMAT_FOREACH_ARGNUM. For an
98 // example, see the implementation of printf() at the end of the source file.
99 //
100 // Sometimes it's useful to be able to pass a list of format arguments through
101 // to a non-template function. The FormatList class is provided as a way to do
102 // this by storing the argument list in a type-opaque way. Continuing the
103 // example from above, we construct a FormatList using makeFormatList():
104 //
105 // FormatListRef formatList = tfm::makeFormatList(weekday, month, day, hour, min);
106 //
107 // The format list can now be passed into any non-template function and used
108 // via a call to the vformat() function:
109 //
110 // tfm::vformat(std::cout, "%s, %s %d, %.2d:%.2d\n", formatList);
111 //
112 //
113 // Additional API information
114 // --------------------------
115 //
116 // Error handling: Define TINYFORMAT_ERROR to customize the error handling for
117 // format strings which are unsupported or have the wrong number of format
118 // specifiers (calls assert() by default).
119 //
120 // User defined types: Uses operator<< for user defined types by default.
121 // Overload formatValue() for more control.
122 
123 
124 #ifndef TINYFORMAT_H_INCLUDED
125 #define TINYFORMAT_H_INCLUDED
126 
127 namespace tinyformat {}
128 //------------------------------------------------------------------------------
129 // Config section. Customize to your liking!
130 
131 // Namespace alias to encourage brevity
132 namespace tfm = tinyformat;
133 
134 // Error handling; calls assert() by default.
135 #define TINYFORMAT_ERROR(reasonString) throw tinyformat::format_error(reasonString)
136 
137 // Define for C++11 variadic templates which make the code shorter & more
138 // general. If you don't define this, C++11 support is autodetected below.
139 #define TINYFORMAT_USE_VARIADIC_TEMPLATES
140 
141 
142 //------------------------------------------------------------------------------
143 // Implementation details.
144 #include <algorithm>
145 #include <iostream>
146 #include <sstream>
147 #include <stdexcept> // Added for Bitcoin Core
148 
149 #ifndef TINYFORMAT_ASSERT
150 # include <cassert>
151 # define TINYFORMAT_ASSERT(cond) assert(cond)
152 #endif
153 
154 #ifndef TINYFORMAT_ERROR
155 # include <cassert>
156 # define TINYFORMAT_ERROR(reason) assert(0 && reason)
157 #endif
158 
159 #if !defined(TINYFORMAT_USE_VARIADIC_TEMPLATES) && !defined(TINYFORMAT_NO_VARIADIC_TEMPLATES)
160 # ifdef __GXX_EXPERIMENTAL_CXX0X__
161 # define TINYFORMAT_USE_VARIADIC_TEMPLATES
162 # endif
163 #endif
164 
165 #if defined(__GLIBCXX__) && __GLIBCXX__ < 20080201
166 // std::showpos is broken on old libstdc++ as provided with macOS. See
167 // http://gcc.gnu.org/ml/libstdc++/2007-11/msg00075.html
168 # define TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
169 #endif
170 
171 #ifdef __APPLE__
172 // Workaround macOS linker warning: Xcode uses different default symbol
173 // visibilities for static libs vs executables (see issue #25)
174 # define TINYFORMAT_HIDDEN __attribute__((visibility("hidden")))
175 #else
176 # define TINYFORMAT_HIDDEN
177 #endif
178 
179 namespace tinyformat {
180 
181 // Added for Bitcoin Core
182 class format_error: public std::runtime_error
183 {
184 public:
185  explicit format_error(const std::string &what): std::runtime_error(what) {
186  }
187 };
188 
189 //------------------------------------------------------------------------------
190 namespace detail {
191 
192 // Test whether type T1 is convertible to type T2
193 template <typename T1, typename T2>
195 {
196  private:
197  // two types of different size
198  struct fail { char dummy[2]; };
199  struct succeed { char dummy; };
200  // Try to convert a T1 to a T2 by plugging into tryConvert
201  static fail tryConvert(...);
202  static succeed tryConvert(const T2&);
203  static const T1& makeT1();
204  public:
205 # ifdef _MSC_VER
206  // Disable spurious loss of precision warnings in tryConvert(makeT1())
207 # pragma warning(push)
208 # pragma warning(disable:4244)
209 # pragma warning(disable:4267)
210 # endif
211  // Standard trick: the (...) version of tryConvert will be chosen from
212  // the overload set only if the version taking a T2 doesn't match.
213  // Then we compare the sizes of the return types to check which
214  // function matched. Very neat, in a disgusting kind of way :)
215  static const bool value =
216  sizeof(tryConvert(makeT1())) == sizeof(succeed);
217 # ifdef _MSC_VER
218 # pragma warning(pop)
219 # endif
220 };
221 
222 
223 // Detect when a type is not a wchar_t string
224 template<typename T> struct is_wchar { typedef int tinyformat_wchar_is_not_supported; };
225 template<> struct is_wchar<wchar_t*> {};
226 template<> struct is_wchar<const wchar_t*> {};
227 template<int n> struct is_wchar<const wchar_t[n]> {};
228 template<int n> struct is_wchar<wchar_t[n]> {};
229 
230 
231 // Format the value by casting to type fmtT. This default implementation
232 // should never be called.
233 template<typename T, typename fmtT, bool convertible = is_convertible<T, fmtT>::value>
235 {
236  static void invoke(std::ostream& /*out*/, const T& /*value*/) { TINYFORMAT_ASSERT(0); }
237 };
238 // Specialized version for types that can actually be converted to fmtT, as
239 // indicated by the "convertible" template parameter.
240 template<typename T, typename fmtT>
241 struct formatValueAsType<T,fmtT,true>
242 {
243  static void invoke(std::ostream& out, const T& value)
244  { out << static_cast<fmtT>(value); }
245 };
246 
247 #ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
248 template<typename T, bool convertible = is_convertible<T, int>::value>
249 struct formatZeroIntegerWorkaround
250 {
251  static bool invoke(std::ostream& , const T& ) { return false; }
252 };
253 template<typename T>
254 struct formatZeroIntegerWorkaround<T,true>
255 {
256  static bool invoke(std::ostream& out, const T& value)
257  {
258  if (static_cast<int>(value) == 0 && out.flags() & std::ios::showpos) {
259  out << "+0";
260  return true;
261  }
262  return false;
263  }
264 };
265 #endif // TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
266 
267 // Convert an arbitrary type to integer. The version with convertible=false
268 // throws an error.
269 template<typename T, bool convertible = is_convertible<T,int>::value>
271 {
272  static int invoke(const T& /*value*/)
273  {
274  TINYFORMAT_ERROR("tinyformat: Cannot convert from argument type to "
275  "integer for use as variable width or precision");
276  return 0;
277  }
278 };
279 // Specialization for convertToInt when conversion is possible
280 template<typename T>
281 struct convertToInt<T,true>
282 {
283  static int invoke(const T& value) { return static_cast<int>(value); }
284 };
285 
286 // Format at most ntrunc characters to the given stream.
287 template<typename T>
288 inline void formatTruncated(std::ostream& out, const T& value, int ntrunc)
289 {
290  std::ostringstream tmp;
291  tmp << value;
292  std::string result = tmp.str();
293  out.write(result.c_str(), (std::min)(ntrunc, static_cast<int>(result.size())));
294 }
295 #define TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(type) \
296 inline void formatTruncated(std::ostream& out, type* value, int ntrunc) \
297 { \
298  std::streamsize len = 0; \
299  while (len < ntrunc && value[len] != 0) \
300  ++len; \
301  out.write(value, len); \
302 }
303 // Overload for const char* and char*. Could overload for signed & unsigned
304 // char too, but these are technically unneeded for printf compatibility.
307 #undef TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR
308 
309 } // namespace detail
310 
311 
312 //------------------------------------------------------------------------------
313 // Variable formatting functions. May be overridden for user-defined types if
314 // desired.
315 
316 
328 template<typename T>
329 inline void formatValue(std::ostream& out, const char* /*fmtBegin*/,
330  const char* fmtEnd, int ntrunc, const T& value)
331 {
332 #ifndef TINYFORMAT_ALLOW_WCHAR_STRINGS
333  // Since we don't support printing of wchar_t using "%ls", make it fail at
334  // compile time in preference to printing as a void* at runtime.
336  (void) DummyType(); // avoid unused type warning with gcc-4.8
337 #endif
338  // The mess here is to support the %c and %p conversions: if these
339  // conversions are active we try to convert the type to a char or const
340  // void* respectively and format that instead of the value itself. For the
341  // %p conversion it's important to avoid dereferencing the pointer, which
342  // could otherwise lead to a crash when printing a dangling (const char*).
343  const bool canConvertToChar = detail::is_convertible<T,char>::value;
344  const bool canConvertToVoidPtr = detail::is_convertible<T, const void*>::value;
345  if (canConvertToChar && *(fmtEnd-1) == 'c')
347  else if (canConvertToVoidPtr && *(fmtEnd-1) == 'p')
349 #ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
350  else if (detail::formatZeroIntegerWorkaround<T>::invoke(out, value)) ;
351 #endif
352  else if (ntrunc >= 0) {
353  // Take care not to overread C strings in truncating conversions like
354  // "%.4s" where at most 4 characters may be read.
355  detail::formatTruncated(out, value, ntrunc);
356  }
357  else
358  out << value;
359 }
360 
361 
362 // Overloaded version for char types to support printing as an integer
363 #define TINYFORMAT_DEFINE_FORMATVALUE_CHAR(charType) \
364 inline void formatValue(std::ostream& out, const char* /*fmtBegin*/, \
365  const char* fmtEnd, int , charType value) \
366 { \
367  switch (*(fmtEnd-1)) { \
368  case 'u': case 'd': case 'i': case 'o': case 'X': case 'x': \
369  out << static_cast<int>(value); break; \
370  default: \
371  out << value; break; \
372  } \
373 }
374 // per 3.9.1: char, signed char and unsigned char are all distinct types
378 #undef TINYFORMAT_DEFINE_FORMATVALUE_CHAR
379 
380 
381 //------------------------------------------------------------------------------
382 // Tools for emulating variadic templates in C++98. The basic idea here is
383 // stolen from the boost preprocessor metaprogramming library and cut down to
384 // be just general enough for what we need.
385 
386 #define TINYFORMAT_ARGTYPES(n) TINYFORMAT_ARGTYPES_ ## n
387 #define TINYFORMAT_VARARGS(n) TINYFORMAT_VARARGS_ ## n
388 #define TINYFORMAT_PASSARGS(n) TINYFORMAT_PASSARGS_ ## n
389 #define TINYFORMAT_PASSARGS_TAIL(n) TINYFORMAT_PASSARGS_TAIL_ ## n
390 
391 // To keep it as transparent as possible, the macros below have been generated
392 // using python via the excellent cog.py code generation script. This avoids
393 // the need for a bunch of complex (but more general) preprocessor tricks as
394 // used in boost.preprocessor.
395 //
396 // To rerun the code generation in place, use `cog.py -r tinyformat.h`
397 // (see http://nedbatchelder.com/code/cog). Alternatively you can just create
398 // extra versions by hand.
399 
400 /*[[[cog
401 maxParams = 16
402 
403 def makeCommaSepLists(lineTemplate, elemTemplate, startInd=1):
404  for j in range(startInd,maxParams+1):
405  list = ', '.join([elemTemplate % {'i':i} for i in range(startInd,j+1)])
406  cog.outl(lineTemplate % {'j':j, 'list':list})
407 
408 makeCommaSepLists('#define TINYFORMAT_ARGTYPES_%(j)d %(list)s',
409  'class T%(i)d')
410 
411 cog.outl()
412 makeCommaSepLists('#define TINYFORMAT_VARARGS_%(j)d %(list)s',
413  'const T%(i)d& v%(i)d')
414 
415 cog.outl()
416 makeCommaSepLists('#define TINYFORMAT_PASSARGS_%(j)d %(list)s', 'v%(i)d')
417 
418 cog.outl()
419 cog.outl('#define TINYFORMAT_PASSARGS_TAIL_1')
420 makeCommaSepLists('#define TINYFORMAT_PASSARGS_TAIL_%(j)d , %(list)s',
421  'v%(i)d', startInd = 2)
422 
423 cog.outl()
424 cog.outl('#define TINYFORMAT_FOREACH_ARGNUM(m) \\\n ' +
425  ' '.join(['m(%d)' % (j,) for j in range(1,maxParams+1)]))
426 ]]]*/
427 #define TINYFORMAT_ARGTYPES_1 class T1
428 #define TINYFORMAT_ARGTYPES_2 class T1, class T2
429 #define TINYFORMAT_ARGTYPES_3 class T1, class T2, class T3
430 #define TINYFORMAT_ARGTYPES_4 class T1, class T2, class T3, class T4
431 #define TINYFORMAT_ARGTYPES_5 class T1, class T2, class T3, class T4, class T5
432 #define TINYFORMAT_ARGTYPES_6 class T1, class T2, class T3, class T4, class T5, class T6
433 #define TINYFORMAT_ARGTYPES_7 class T1, class T2, class T3, class T4, class T5, class T6, class T7
434 #define TINYFORMAT_ARGTYPES_8 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8
435 #define TINYFORMAT_ARGTYPES_9 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9
436 #define TINYFORMAT_ARGTYPES_10 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10
437 #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
438 #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
439 #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
440 #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
441 #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
442 #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 
444 #define TINYFORMAT_VARARGS_1 const T1& v1
445 #define TINYFORMAT_VARARGS_2 const T1& v1, const T2& v2
446 #define TINYFORMAT_VARARGS_3 const T1& v1, const T2& v2, const T3& v3
447 #define TINYFORMAT_VARARGS_4 const T1& v1, const T2& v2, const T3& v3, const T4& v4
448 #define TINYFORMAT_VARARGS_5 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5
449 #define TINYFORMAT_VARARGS_6 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6
450 #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
451 #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
452 #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
453 #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
454 #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
455 #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
456 #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
457 #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
458 #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
459 #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 
461 #define TINYFORMAT_PASSARGS_1 v1
462 #define TINYFORMAT_PASSARGS_2 v1, v2
463 #define TINYFORMAT_PASSARGS_3 v1, v2, v3
464 #define TINYFORMAT_PASSARGS_4 v1, v2, v3, v4
465 #define TINYFORMAT_PASSARGS_5 v1, v2, v3, v4, v5
466 #define TINYFORMAT_PASSARGS_6 v1, v2, v3, v4, v5, v6
467 #define TINYFORMAT_PASSARGS_7 v1, v2, v3, v4, v5, v6, v7
468 #define TINYFORMAT_PASSARGS_8 v1, v2, v3, v4, v5, v6, v7, v8
469 #define TINYFORMAT_PASSARGS_9 v1, v2, v3, v4, v5, v6, v7, v8, v9
470 #define TINYFORMAT_PASSARGS_10 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10
471 #define TINYFORMAT_PASSARGS_11 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11
472 #define TINYFORMAT_PASSARGS_12 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12
473 #define TINYFORMAT_PASSARGS_13 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13
474 #define TINYFORMAT_PASSARGS_14 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14
475 #define TINYFORMAT_PASSARGS_15 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15
476 #define TINYFORMAT_PASSARGS_16 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16
477 
478 #define TINYFORMAT_PASSARGS_TAIL_1
479 #define TINYFORMAT_PASSARGS_TAIL_2 , v2
480 #define TINYFORMAT_PASSARGS_TAIL_3 , v2, v3
481 #define TINYFORMAT_PASSARGS_TAIL_4 , v2, v3, v4
482 #define TINYFORMAT_PASSARGS_TAIL_5 , v2, v3, v4, v5
483 #define TINYFORMAT_PASSARGS_TAIL_6 , v2, v3, v4, v5, v6
484 #define TINYFORMAT_PASSARGS_TAIL_7 , v2, v3, v4, v5, v6, v7
485 #define TINYFORMAT_PASSARGS_TAIL_8 , v2, v3, v4, v5, v6, v7, v8
486 #define TINYFORMAT_PASSARGS_TAIL_9 , v2, v3, v4, v5, v6, v7, v8, v9
487 #define TINYFORMAT_PASSARGS_TAIL_10 , v2, v3, v4, v5, v6, v7, v8, v9, v10
488 #define TINYFORMAT_PASSARGS_TAIL_11 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11
489 #define TINYFORMAT_PASSARGS_TAIL_12 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12
490 #define TINYFORMAT_PASSARGS_TAIL_13 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13
491 #define TINYFORMAT_PASSARGS_TAIL_14 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14
492 #define TINYFORMAT_PASSARGS_TAIL_15 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15
493 #define TINYFORMAT_PASSARGS_TAIL_16 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16
494 
495 #define TINYFORMAT_FOREACH_ARGNUM(m) \
496  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)
497 //[[[end]]]
498 
499 
500 
501 namespace detail {
502 
503 // Type-opaque holder for an argument to format(), with associated actions on
504 // the type held as explicit function pointers. This allows FormatArg's for
505 // each argument to be allocated as a homogeneous array inside FormatList
506 // whereas a naive implementation based on inheritance does not.
508 {
509  public:
510  FormatArg() = default;
511 
512  template<typename T>
513  explicit FormatArg(const T& value)
514  : m_value(static_cast<const void*>(&value)),
517  { }
518 
519  void format(std::ostream& out, const char* fmtBegin,
520  const char* fmtEnd, int ntrunc) const
521  {
524  m_formatImpl(out, fmtBegin, fmtEnd, ntrunc, m_value);
525  }
526 
527  int toInt() const
528  {
531  return m_toIntImpl(m_value);
532  }
533 
534  private:
535  template<typename T>
536  TINYFORMAT_HIDDEN static void formatImpl(std::ostream& out, const char* fmtBegin,
537  const char* fmtEnd, int ntrunc, const void* value)
538  {
539  formatValue(out, fmtBegin, fmtEnd, ntrunc, *static_cast<const T*>(value));
540  }
541 
542  template<typename T>
543  TINYFORMAT_HIDDEN static int toIntImpl(const void* value)
544  {
545  return convertToInt<T>::invoke(*static_cast<const T*>(value));
546  }
547 
548  const void* m_value{nullptr};
549  void (*m_formatImpl)(std::ostream& out, const char* fmtBegin,
550  const char* fmtEnd, int ntrunc, const void* value){nullptr};
551  int (*m_toIntImpl)(const void* value){nullptr};
552 };
553 
554 
555 // Parse and return an integer from the string c, as atoi()
556 // On return, c is set to one past the end of the integer.
557 inline int parseIntAndAdvance(const char*& c)
558 {
559  int i = 0;
560  for (;*c >= '0' && *c <= '9'; ++c)
561  i = 10*i + (*c - '0');
562  return i;
563 }
564 
565 // Parse width or precision `n` from format string pointer `c`, and advance it
566 // to the next character. If an indirection is requested with `*`, the argument
567 // is read from `args[argIndex]` and `argIndex` is incremented (or read
568 // from `args[n]` in positional mode). Returns true if one or more
569 // characters were read.
570 inline bool parseWidthOrPrecision(int& n, const char*& c, bool positionalMode,
571  const detail::FormatArg* args,
572  int& argIndex, int numArgs)
573 {
574  if (*c >= '0' && *c <= '9') {
575  n = parseIntAndAdvance(c);
576  }
577  else if (*c == '*') {
578  ++c;
579  n = 0;
580  if (positionalMode) {
581  int pos = parseIntAndAdvance(c) - 1;
582  if (*c != '$')
583  TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one");
584  if (pos >= 0 && pos < numArgs)
585  n = args[pos].toInt();
586  else
587  TINYFORMAT_ERROR("tinyformat: Positional argument out of range");
588  ++c;
589  }
590  else {
591  if (argIndex < numArgs)
592  n = args[argIndex++].toInt();
593  else
594  TINYFORMAT_ERROR("tinyformat: Not enough arguments to read variable width or precision");
595  }
596  }
597  else {
598  return false;
599  }
600  return true;
601 }
602 
603 // Print literal part of format string and return next format spec position.
604 //
605 // Skips over any occurrences of '%%', printing a literal '%' to the output.
606 // The position of the first % character of the next nontrivial format spec is
607 // returned, or the end of string.
608 inline const char* printFormatStringLiteral(std::ostream& out, const char* fmt)
609 {
610  const char* c = fmt;
611  for (;; ++c) {
612  if (*c == '\0') {
613  out.write(fmt, c - fmt);
614  return c;
615  }
616  else if (*c == '%') {
617  out.write(fmt, c - fmt);
618  if (*(c+1) != '%')
619  return c;
620  // for "%%", tack trailing % onto next literal section.
621  fmt = ++c;
622  }
623  }
624 }
625 
626 
627 // Parse a format string and set the stream state accordingly.
628 //
629 // The format mini-language recognized here is meant to be the one from C99,
630 // with the form "%[flags][width][.precision][length]type" with POSIX
631 // positional arguments extension.
632 //
633 // POSIX positional arguments extension:
634 // Conversions can be applied to the nth argument after the format in
635 // the argument list, rather than to the next unused argument. In this case,
636 // the conversion specifier character % (see below) is replaced by the sequence
637 // "%n$", where n is a decimal integer in the range [1,{NL_ARGMAX}],
638 // giving the position of the argument in the argument list. This feature
639 // provides for the definition of format strings that select arguments
640 // in an order appropriate to specific languages.
641 //
642 // The format can contain either numbered argument conversion specifications
643 // (that is, "%n$" and "*m$"), or unnumbered argument conversion specifications
644 // (that is, % and * ), but not both. The only exception to this is that %%
645 // can be mixed with the "%n$" form. The results of mixing numbered and
646 // unnumbered argument specifications in a format string are undefined.
647 // When numbered argument specifications are used, specifying the Nth argument
648 // requires that all the leading arguments, from the first to the (N-1)th,
649 // are specified in the format string.
650 //
651 // In format strings containing the "%n$" form of conversion specification,
652 // numbered arguments in the argument list can be referenced from the format
653 // string as many times as required.
654 //
655 // Formatting options which can't be natively represented using the ostream
656 // state are returned in spacePadPositive (for space padded positive numbers)
657 // and ntrunc (for truncating conversions). argIndex is incremented if
658 // necessary to pull out variable width and precision. The function returns a
659 // pointer to the character after the end of the current format spec.
660 inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode,
661  bool& spacePadPositive,
662  int& ntrunc, const char* fmtStart,
663  const detail::FormatArg* args,
664  int& argIndex, int numArgs)
665 {
666  TINYFORMAT_ASSERT(*fmtStart == '%');
667  // Reset stream state to defaults.
668  out.width(0);
669  out.precision(6);
670  out.fill(' ');
671  // Reset most flags; ignore irrelevant unitbuf & skipws.
672  out.unsetf(std::ios::adjustfield | std::ios::basefield |
673  std::ios::floatfield | std::ios::showbase | std::ios::boolalpha |
674  std::ios::showpoint | std::ios::showpos | std::ios::uppercase);
675  bool precisionSet = false;
676  bool widthSet = false;
677  int widthExtra = 0;
678  const char* c = fmtStart + 1;
679 
680  // 1) Parse an argument index (if followed by '$') or a width possibly
681  // preceded with '0' flag.
682  if (*c >= '0' && *c <= '9') {
683  const char tmpc = *c;
684  int value = parseIntAndAdvance(c);
685  if (*c == '$') {
686  // value is an argument index
687  if (value > 0 && value <= numArgs)
688  argIndex = value - 1;
689  else
690  TINYFORMAT_ERROR("tinyformat: Positional argument out of range");
691  ++c;
692  positionalMode = true;
693  }
694  else if (positionalMode) {
695  TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one");
696  }
697  else {
698  if (tmpc == '0') {
699  // Use internal padding so that numeric values are
700  // formatted correctly, eg -00010 rather than 000-10
701  out.fill('0');
702  out.setf(std::ios::internal, std::ios::adjustfield);
703  }
704  if (value != 0) {
705  // Nonzero value means that we parsed width.
706  widthSet = true;
707  out.width(value);
708  }
709  }
710  }
711  else if (positionalMode) {
712  TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one");
713  }
714  // 2) Parse flags and width if we did not do it in previous step.
715  if (!widthSet) {
716  // Parse flags
717  for (;; ++c) {
718  switch (*c) {
719  case '#':
720  out.setf(std::ios::showpoint | std::ios::showbase);
721  continue;
722  case '0':
723  // overridden by left alignment ('-' flag)
724  if (!(out.flags() & std::ios::left)) {
725  // Use internal padding so that numeric values are
726  // formatted correctly, eg -00010 rather than 000-10
727  out.fill('0');
728  out.setf(std::ios::internal, std::ios::adjustfield);
729  }
730  continue;
731  case '-':
732  out.fill(' ');
733  out.setf(std::ios::left, std::ios::adjustfield);
734  continue;
735  case ' ':
736  // overridden by show positive sign, '+' flag.
737  if (!(out.flags() & std::ios::showpos))
738  spacePadPositive = true;
739  continue;
740  case '+':
741  out.setf(std::ios::showpos);
742  spacePadPositive = false;
743  widthExtra = 1;
744  continue;
745  default:
746  break;
747  }
748  break;
749  }
750  // Parse width
751  int width = 0;
752  widthSet = parseWidthOrPrecision(width, c, positionalMode,
753  args, argIndex, numArgs);
754  if (widthSet) {
755  if (width < 0) {
756  // negative widths correspond to '-' flag set
757  out.fill(' ');
758  out.setf(std::ios::left, std::ios::adjustfield);
759  width = -width;
760  }
761  out.width(width);
762  }
763  }
764  // 3) Parse precision
765  if (*c == '.') {
766  ++c;
767  int precision = 0;
768  parseWidthOrPrecision(precision, c, positionalMode,
769  args, argIndex, numArgs);
770  // Presence of `.` indicates precision set, unless the inferred value
771  // was negative in which case the default is used.
772  precisionSet = precision >= 0;
773  if (precisionSet)
774  out.precision(precision);
775  }
776  // 4) Ignore any C99 length modifier
777  while (*c == 'l' || *c == 'h' || *c == 'L' ||
778  *c == 'j' || *c == 'z' || *c == 't') {
779  ++c;
780  }
781  // 5) We're up to the conversion specifier character.
782  // Set stream flags based on conversion specifier (thanks to the
783  // boost::format class for forging the way here).
784  bool intConversion = false;
785  switch (*c) {
786  case 'u': case 'd': case 'i':
787  out.setf(std::ios::dec, std::ios::basefield);
788  intConversion = true;
789  break;
790  case 'o':
791  out.setf(std::ios::oct, std::ios::basefield);
792  intConversion = true;
793  break;
794  case 'X':
795  out.setf(std::ios::uppercase);
796  [[fallthrough]];
797  case 'x': case 'p':
798  out.setf(std::ios::hex, std::ios::basefield);
799  intConversion = true;
800  break;
801  case 'E':
802  out.setf(std::ios::uppercase);
803  [[fallthrough]];
804  case 'e':
805  out.setf(std::ios::scientific, std::ios::floatfield);
806  out.setf(std::ios::dec, std::ios::basefield);
807  break;
808  case 'F':
809  out.setf(std::ios::uppercase);
810  [[fallthrough]];
811  case 'f':
812  out.setf(std::ios::fixed, std::ios::floatfield);
813  break;
814  case 'A':
815  out.setf(std::ios::uppercase);
816  [[fallthrough]];
817  case 'a':
818 # ifdef _MSC_VER
819  // Workaround https://developercommunity.visualstudio.com/content/problem/520472/hexfloat-stream-output-does-not-ignore-precision-a.html
820  // by always setting maximum precision on MSVC to avoid precision
821  // loss for doubles.
822  out.precision(13);
823 # endif
824  out.setf(std::ios::fixed | std::ios::scientific, std::ios::floatfield);
825  break;
826  case 'G':
827  out.setf(std::ios::uppercase);
828  [[fallthrough]];
829  case 'g':
830  out.setf(std::ios::dec, std::ios::basefield);
831  // As in boost::format, let stream decide float format.
832  out.flags(out.flags() & ~std::ios::floatfield);
833  break;
834  case 'c':
835  // Handled as special case inside formatValue()
836  break;
837  case 's':
838  if (precisionSet)
839  ntrunc = static_cast<int>(out.precision());
840  // Make %s print Booleans as "true" and "false"
841  out.setf(std::ios::boolalpha);
842  break;
843  case 'n':
844  // Not supported - will cause problems!
845  TINYFORMAT_ERROR("tinyformat: %n conversion spec not supported");
846  break;
847  case '\0':
848  TINYFORMAT_ERROR("tinyformat: Conversion spec incorrectly "
849  "terminated by end of string");
850  return c;
851  default:
852  break;
853  }
854  if (intConversion && precisionSet && !widthSet) {
855  // "precision" for integers gives the minimum number of digits (to be
856  // padded with zeros on the left). This isn't really supported by the
857  // iostreams, but we can approximately simulate it with the width if
858  // the width isn't otherwise used.
859  out.width(out.precision() + widthExtra);
860  out.setf(std::ios::internal, std::ios::adjustfield);
861  out.fill('0');
862  }
863  return c+1;
864 }
865 
866 
867 //------------------------------------------------------------------------------
868 inline void formatImpl(std::ostream& out, const char* fmt,
869  const detail::FormatArg* args,
870  int numArgs)
871 {
872  // Saved stream state
873  std::streamsize origWidth = out.width();
874  std::streamsize origPrecision = out.precision();
875  std::ios::fmtflags origFlags = out.flags();
876  char origFill = out.fill();
877 
878  // "Positional mode" means all format specs should be of the form "%n$..."
879  // with `n` an integer. We detect this in `streamStateFromFormat`.
880  bool positionalMode = false;
881  int argIndex = 0;
882  while (true) {
883  fmt = printFormatStringLiteral(out, fmt);
884  if (*fmt == '\0') {
885  if (!positionalMode && argIndex < numArgs) {
886  TINYFORMAT_ERROR("tinyformat: Not enough conversion specifiers in format string");
887  }
888  break;
889  }
890  bool spacePadPositive = false;
891  int ntrunc = -1;
892  const char* fmtEnd = streamStateFromFormat(out, positionalMode, spacePadPositive, ntrunc, fmt,
893  args, argIndex, numArgs);
894  // NB: argIndex may be incremented by reading variable width/precision
895  // in `streamStateFromFormat`, so do the bounds check here.
896  if (argIndex >= numArgs) {
897  TINYFORMAT_ERROR("tinyformat: Too many conversion specifiers in format string");
898  return;
899  }
900  const FormatArg& arg = args[argIndex];
901  // Format the arg into the stream.
902  if (!spacePadPositive) {
903  arg.format(out, fmt, fmtEnd, ntrunc);
904  }
905  else {
906  // The following is a special case with no direct correspondence
907  // between stream formatting and the printf() behaviour. Simulate
908  // it crudely by formatting into a temporary string stream and
909  // munging the resulting string.
910  std::ostringstream tmpStream;
911  tmpStream.copyfmt(out);
912  tmpStream.setf(std::ios::showpos);
913  arg.format(tmpStream, fmt, fmtEnd, ntrunc);
914  std::string result = tmpStream.str(); // allocates... yuck.
915  for (size_t i = 0, iend = result.size(); i < iend; ++i) {
916  if (result[i] == '+')
917  result[i] = ' ';
918  }
919  out << result;
920  }
921  if (!positionalMode)
922  ++argIndex;
923  fmt = fmtEnd;
924  }
925 
926  // Restore stream state
927  out.width(origWidth);
928  out.precision(origPrecision);
929  out.flags(origFlags);
930  out.fill(origFill);
931 }
932 
933 } // namespace detail
934 
935 
943 {
944  public:
946  : m_args(args), m_N(N) { }
947 
948  friend void vformat(std::ostream& out, const char* fmt,
949  const FormatList& list);
950 
951  private:
953  int m_N;
954 };
955 
957 typedef const FormatList& FormatListRef;
958 
959 
960 namespace detail {
961 
962 // Format list subclass with fixed storage to avoid dynamic allocation
963 template<int N>
964 class FormatListN : public FormatList
965 {
966  public:
967 #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
968  template<typename... Args>
969  explicit FormatListN(const Args&... args)
970  : FormatList(&m_formatterStore[0], N),
972  { static_assert(sizeof...(args) == N, "Number of args must be N"); }
973 #else // C++98 version
974  void init(int) {}
975 # define TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR(n) \
976  \
977  template<TINYFORMAT_ARGTYPES(n)> \
978  FormatListN(TINYFORMAT_VARARGS(n)) \
979  : FormatList(&m_formatterStore[0], n) \
980  { TINYFORMAT_ASSERT(n == N); init(0, TINYFORMAT_PASSARGS(n)); } \
981  \
982  template<TINYFORMAT_ARGTYPES(n)> \
983  void init(int i, TINYFORMAT_VARARGS(n)) \
984  { \
985  m_formatterStore[i] = FormatArg(v1); \
986  init(i+1 TINYFORMAT_PASSARGS_TAIL(n)); \
987  }
988 
989  TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR)
990 # undef TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR
991 #endif
992  FormatListN(const FormatListN& other)
993  : FormatList(&m_formatterStore[0], N)
994  { std::copy(&other.m_formatterStore[0], &other.m_formatterStore[N],
995  &m_formatterStore[0]); }
996 
997  private:
999 };
1000 
1001 // Special 0-arg version - MSVC says zero-sized C array in struct is nonstandard
1002 template<> class FormatListN<0> : public FormatList
1003 {
1004 public:
1005  FormatListN() : FormatList(nullptr, 0) {}
1006 };
1007 
1008 } // namespace detail
1009 
1010 
1011 //------------------------------------------------------------------------------
1012 // Primary API functions
1013 
1014 #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
1015 
1022 template<typename... Args>
1023 detail::FormatListN<sizeof...(Args)> makeFormatList(const Args&... args)
1024 {
1025  return detail::FormatListN<sizeof...(args)>(args...);
1026 }
1027 
1028 #else // C++98 version
1029 
1030 inline detail::FormatListN<0> makeFormatList()
1031 {
1032  return detail::FormatListN<0>();
1033 }
1034 #define TINYFORMAT_MAKE_MAKEFORMATLIST(n) \
1035 template<TINYFORMAT_ARGTYPES(n)> \
1036 detail::FormatListN<n> makeFormatList(TINYFORMAT_VARARGS(n)) \
1037 { \
1038  return detail::FormatListN<n>(TINYFORMAT_PASSARGS(n)); \
1039 }
1040 TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_MAKEFORMATLIST)
1041 #undef TINYFORMAT_MAKE_MAKEFORMATLIST
1042 
1043 #endif
1044 
1049 inline void vformat(std::ostream& out, const char* fmt, FormatListRef list)
1050 {
1051  detail::formatImpl(out, fmt, list.m_args, list.m_N);
1052 }
1053 
1054 
1055 #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
1056 
1058 template<typename... Args>
1059 void format(std::ostream& out, const char* fmt, const Args&... args)
1060 {
1061  vformat(out, fmt, makeFormatList(args...));
1062 }
1063 
1066 template<typename... Args>
1067 std::string format(const char* fmt, const Args&... args)
1068 {
1069  std::ostringstream oss;
1070  format(oss, fmt, args...);
1071  return oss.str();
1072 }
1073 
1075 template<typename... Args>
1076 void printf(const char* fmt, const Args&... args)
1077 {
1078  format(std::cout, fmt, args...);
1079 }
1080 
1081 template<typename... Args>
1082 void printfln(const char* fmt, const Args&... args)
1083 {
1084  format(std::cout, fmt, args...);
1085  std::cout << '\n';
1086 }
1087 
1088 
1089 #else // C++98 version
1090 
1091 inline void format(std::ostream& out, const char* fmt)
1092 {
1093  vformat(out, fmt, makeFormatList());
1094 }
1095 
1096 inline std::string format(const char* fmt)
1097 {
1098  std::ostringstream oss;
1099  format(oss, fmt);
1100  return oss.str();
1101 }
1102 
1103 inline void printf(const char* fmt)
1104 {
1105  format(std::cout, fmt);
1106 }
1107 
1108 inline void printfln(const char* fmt)
1109 {
1110  format(std::cout, fmt);
1111  std::cout << '\n';
1112 }
1113 
1114 #define TINYFORMAT_MAKE_FORMAT_FUNCS(n) \
1115  \
1116 template<TINYFORMAT_ARGTYPES(n)> \
1117 void format(std::ostream& out, const char* fmt, TINYFORMAT_VARARGS(n)) \
1118 { \
1119  vformat(out, fmt, makeFormatList(TINYFORMAT_PASSARGS(n))); \
1120 } \
1121  \
1122 template<TINYFORMAT_ARGTYPES(n)> \
1123 std::string format(const char* fmt, TINYFORMAT_VARARGS(n)) \
1124 { \
1125  std::ostringstream oss; \
1126  format(oss, fmt, TINYFORMAT_PASSARGS(n)); \
1127  return oss.str(); \
1128 } \
1129  \
1130 template<TINYFORMAT_ARGTYPES(n)> \
1131 void printf(const char* fmt, TINYFORMAT_VARARGS(n)) \
1132 { \
1133  format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \
1134 } \
1135  \
1136 template<TINYFORMAT_ARGTYPES(n)> \
1137 void printfln(const char* fmt, TINYFORMAT_VARARGS(n)) \
1138 { \
1139  format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \
1140  std::cout << '\n'; \
1141 }
1142 
1143 TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMAT_FUNCS)
1144 #undef TINYFORMAT_MAKE_FORMAT_FUNCS
1145 
1146 #endif
1147 
1148 // Added for Bitcoin Core
1149 template<typename... Args>
1150 std::string format(const std::string &fmt, const Args&... args)
1151 {
1152  std::ostringstream oss;
1153  format(oss, fmt.c_str(), args...);
1154  return oss.str();
1155 }
1156 
1157 } // namespace tinyformat
1158 
1159 // Added for Bitcoin Core:
1161 #define strprintf tfm::format
1162 
1163 #endif // TINYFORMAT_H_INCLUDED
std::unique_ptr< interfaces::Init > init
ArgsManager & args
Definition: bitcoind.cpp:270
List of template arguments format(), held in a type-opaque way.
Definition: tinyformat.h:943
friend void vformat(std::ostream &out, const char *fmt, const FormatList &list)
FormatList(detail::FormatArg *args, int N)
Definition: tinyformat.h:945
const detail::FormatArg * m_args
Definition: tinyformat.h:952
static TINYFORMAT_HIDDEN void formatImpl(std::ostream &out, const char *fmtBegin, const char *fmtEnd, int ntrunc, const void *value)
Definition: tinyformat.h:536
void(* m_formatImpl)(std::ostream &out, const char *fmtBegin, const char *fmtEnd, int ntrunc, const void *value)
Definition: tinyformat.h:549
static TINYFORMAT_HIDDEN int toIntImpl(const void *value)
Definition: tinyformat.h:543
int(* m_toIntImpl)(const void *value)
Definition: tinyformat.h:551
void format(std::ostream &out, const char *fmtBegin, const char *fmtEnd, int ntrunc) const
Definition: tinyformat.h:519
FormatListN(const Args &... args)
Definition: tinyformat.h:969
FormatListN(const FormatListN &other)
Definition: tinyformat.h:992
format_error(const std::string &what)
Definition: tinyformat.h:185
#define T(expected, seed, data)
Definition: uint256.h:94
void formatTruncated(std::ostream &out, const T &value, int ntrunc)
Definition: tinyformat.h:288
int parseIntAndAdvance(const char *&c)
Definition: tinyformat.h:557
void formatImpl(std::ostream &out, const char *fmt, const detail::FormatArg *args, int numArgs)
Definition: tinyformat.h:868
const char * streamStateFromFormat(std::ostream &out, bool &positionalMode, bool &spacePadPositive, int &ntrunc, const char *fmtStart, const detail::FormatArg *args, int &argIndex, int numArgs)
Definition: tinyformat.h:660
const char * printFormatStringLiteral(std::ostream &out, const char *fmt)
Definition: tinyformat.h:608
bool parseWidthOrPrecision(int &n, const char *&c, bool positionalMode, const detail::FormatArg *args, int &argIndex, int numArgs)
Definition: tinyformat.h:570
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1059
void printfln(const char *fmt, const Args &... args)
Definition: tinyformat.h:1082
void printf(const char *fmt, const Args &... args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1076
void vformat(std::ostream &out, const char *fmt, FormatListRef list)
Format list of arguments to the stream according to the given format string.
Definition: tinyformat.h:1049
void formatValue(std::ostream &out, const char *, const char *fmtEnd, int ntrunc, const T &value)
Format a value into a stream, delegating to operator<< by default.
Definition: tinyformat.h:329
detail::FormatListN< sizeof...(Args)> makeFormatList(const Args &... args)
Make type-agnostic format list from list of template arguments.
Definition: tinyformat.h:1023
const FormatList & FormatListRef
Reference to type-opaque format list for passing to vformat()
Definition: tinyformat.h:957
static int invoke(const T &value)
Definition: tinyformat.h:283
static int invoke(const T &)
Definition: tinyformat.h:272
static void invoke(std::ostream &out, const T &value)
Definition: tinyformat.h:243
static void invoke(std::ostream &, const T &)
Definition: tinyformat.h:236
static succeed tryConvert(const T2 &)
#define TINYFORMAT_HIDDEN
Definition: tinyformat.h:176
#define TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(type)
Definition: tinyformat.h:295
#define TINYFORMAT_ASSERT(cond)
Definition: tinyformat.h:151
#define TINYFORMAT_ERROR(reasonString)
Definition: tinyformat.h:135
#define TINYFORMAT_DEFINE_FORMATVALUE_CHAR(charType)
Definition: tinyformat.h:363
#define TINYFORMAT_FOREACH_ARGNUM(m)
Definition: tinyformat.h:495