Bitcoin Core 28.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
127namespace tinyformat {}
128//------------------------------------------------------------------------------
129// Config section. Customize to your liking!
130
131// Namespace alias to encourage brevity
132namespace 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#include <util/string.h> // Added for Bitcoin Core
149
150#ifndef TINYFORMAT_ASSERT
151# include <cassert>
152# define TINYFORMAT_ASSERT(cond) assert(cond)
153#endif
154
155#ifndef TINYFORMAT_ERROR
156# include <cassert>
157# define TINYFORMAT_ERROR(reason) assert(0 && reason)
158#endif
159
160#if !defined(TINYFORMAT_USE_VARIADIC_TEMPLATES) && !defined(TINYFORMAT_NO_VARIADIC_TEMPLATES)
161# ifdef __GXX_EXPERIMENTAL_CXX0X__
162# define TINYFORMAT_USE_VARIADIC_TEMPLATES
163# endif
164#endif
165
166#if defined(__GLIBCXX__) && __GLIBCXX__ < 20080201
167// std::showpos is broken on old libstdc++ as provided with macOS. See
168// http://gcc.gnu.org/ml/libstdc++/2007-11/msg00075.html
169# define TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
170#endif
171
172#ifdef __APPLE__
173// Workaround macOS linker warning: Xcode uses different default symbol
174// visibilities for static libs vs executables (see issue #25)
175# define TINYFORMAT_HIDDEN __attribute__((visibility("hidden")))
176#else
177# define TINYFORMAT_HIDDEN
178#endif
179
180namespace tinyformat {
181
182// Added for Bitcoin Core. Wrapper for checking format strings at compile time.
183// Unlike ConstevalFormatString this supports std::string for runtime string
184// formatting without compile time checks.
185template <unsigned num_params>
187 consteval FormatStringCheck(const char* str) : fmt{util::ConstevalFormatString<num_params>{str}.fmt} {}
188 FormatStringCheck(const std::string& str) : fmt{str.c_str()} {}
190 operator const char*() { return fmt; }
191 const char* fmt;
192};
193
194// Added for Bitcoin Core
195class format_error: public std::runtime_error
196{
197public:
198 explicit format_error(const std::string &what): std::runtime_error(what) {
199 }
200};
201
202//------------------------------------------------------------------------------
203namespace detail {
204
205// Test whether type T1 is convertible to type T2
206template <typename T1, typename T2>
208{
209 private:
210 // two types of different size
211 struct fail { char dummy[2]; };
212 struct succeed { char dummy; };
213 // Try to convert a T1 to a T2 by plugging into tryConvert
214 static fail tryConvert(...);
215 static succeed tryConvert(const T2&);
216 static const T1& makeT1();
217 public:
218# ifdef _MSC_VER
219 // Disable spurious loss of precision warnings in tryConvert(makeT1())
220# pragma warning(push)
221# pragma warning(disable:4244)
222# pragma warning(disable:4267)
223# endif
224 // Standard trick: the (...) version of tryConvert will be chosen from
225 // the overload set only if the version taking a T2 doesn't match.
226 // Then we compare the sizes of the return types to check which
227 // function matched. Very neat, in a disgusting kind of way :)
228 static const bool value =
229 sizeof(tryConvert(makeT1())) == sizeof(succeed);
230# ifdef _MSC_VER
231# pragma warning(pop)
232# endif
233};
234
235
236// Detect when a type is not a wchar_t string
237template<typename T> struct is_wchar { typedef int tinyformat_wchar_is_not_supported; };
238template<> struct is_wchar<wchar_t*> {};
239template<> struct is_wchar<const wchar_t*> {};
240template<int n> struct is_wchar<const wchar_t[n]> {};
241template<int n> struct is_wchar<wchar_t[n]> {};
242
243
244// Format the value by casting to type fmtT. This default implementation
245// should never be called.
246template<typename T, typename fmtT, bool convertible = is_convertible<T, fmtT>::value>
248{
249 static void invoke(std::ostream& /*out*/, const T& /*value*/) { TINYFORMAT_ASSERT(0); }
250};
251// Specialized version for types that can actually be converted to fmtT, as
252// indicated by the "convertible" template parameter.
253template<typename T, typename fmtT>
254struct formatValueAsType<T,fmtT,true>
255{
256 static void invoke(std::ostream& out, const T& value)
257 { out << static_cast<fmtT>(value); }
258};
259
260#ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
261template<typename T, bool convertible = is_convertible<T, int>::value>
262struct formatZeroIntegerWorkaround
263{
264 static bool invoke(std::ostream& , const T& ) { return false; }
265};
266template<typename T>
267struct formatZeroIntegerWorkaround<T,true>
268{
269 static bool invoke(std::ostream& out, const T& value)
270 {
271 if (static_cast<int>(value) == 0 && out.flags() & std::ios::showpos) {
272 out << "+0";
273 return true;
274 }
275 return false;
276 }
277};
278#endif // TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
279
280// Convert an arbitrary type to integer. The version with convertible=false
281// throws an error.
282template<typename T, bool convertible = is_convertible<T,int>::value>
284{
285 static int invoke(const T& /*value*/)
286 {
287 TINYFORMAT_ERROR("tinyformat: Cannot convert from argument type to "
288 "integer for use as variable width or precision");
289 return 0;
290 }
291};
292// Specialization for convertToInt when conversion is possible
293template<typename T>
294struct convertToInt<T,true>
295{
296 static int invoke(const T& value) { return static_cast<int>(value); }
297};
298
299// Format at most ntrunc characters to the given stream.
300template<typename T>
301inline void formatTruncated(std::ostream& out, const T& value, int ntrunc)
302{
303 std::ostringstream tmp;
304 tmp << value;
305 std::string result = tmp.str();
306 out.write(result.c_str(), (std::min)(ntrunc, static_cast<int>(result.size())));
307}
308#define TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(type) \
309inline void formatTruncated(std::ostream& out, type* value, int ntrunc) \
310{ \
311 std::streamsize len = 0; \
312 while (len < ntrunc && value[len] != 0) \
313 ++len; \
314 out.write(value, len); \
315}
316// Overload for const char* and char*. Could overload for signed & unsigned
317// char too, but these are technically unneeded for printf compatibility.
320#undef TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR
321
322} // namespace detail
323
324
325//------------------------------------------------------------------------------
326// Variable formatting functions. May be overridden for user-defined types if
327// desired.
328
329
341template<typename T>
342inline void formatValue(std::ostream& out, const char* /*fmtBegin*/,
343 const char* fmtEnd, int ntrunc, const T& value)
344{
345#ifndef TINYFORMAT_ALLOW_WCHAR_STRINGS
346 // Since we don't support printing of wchar_t using "%ls", make it fail at
347 // compile time in preference to printing as a void* at runtime.
349 (void) DummyType(); // avoid unused type warning with gcc-4.8
350#endif
351 // The mess here is to support the %c and %p conversions: if these
352 // conversions are active we try to convert the type to a char or const
353 // void* respectively and format that instead of the value itself. For the
354 // %p conversion it's important to avoid dereferencing the pointer, which
355 // could otherwise lead to a crash when printing a dangling (const char*).
356 const bool canConvertToChar = detail::is_convertible<T,char>::value;
357 const bool canConvertToVoidPtr = detail::is_convertible<T, const void*>::value;
358 if (canConvertToChar && *(fmtEnd-1) == 'c')
360 else if (canConvertToVoidPtr && *(fmtEnd-1) == 'p')
362#ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
363 else if (detail::formatZeroIntegerWorkaround<T>::invoke(out, value)) ;
364#endif
365 else if (ntrunc >= 0) {
366 // Take care not to overread C strings in truncating conversions like
367 // "%.4s" where at most 4 characters may be read.
368 detail::formatTruncated(out, value, ntrunc);
369 }
370 else
371 out << value;
372}
373
374
375// Overloaded version for char types to support printing as an integer
376#define TINYFORMAT_DEFINE_FORMATVALUE_CHAR(charType) \
377inline void formatValue(std::ostream& out, const char* /*fmtBegin*/, \
378 const char* fmtEnd, int , charType value) \
379{ \
380 switch (*(fmtEnd-1)) { \
381 case 'u': case 'd': case 'i': case 'o': case 'X': case 'x': \
382 out << static_cast<int>(value); break; \
383 default: \
384 out << value; break; \
385 } \
386}
387// per 3.9.1: char, signed char and unsigned char are all distinct types
391#undef TINYFORMAT_DEFINE_FORMATVALUE_CHAR
392
393
394//------------------------------------------------------------------------------
395// Tools for emulating variadic templates in C++98. The basic idea here is
396// stolen from the boost preprocessor metaprogramming library and cut down to
397// be just general enough for what we need.
398
399#define TINYFORMAT_ARGTYPES(n) TINYFORMAT_ARGTYPES_ ## n
400#define TINYFORMAT_VARARGS(n) TINYFORMAT_VARARGS_ ## n
401#define TINYFORMAT_PASSARGS(n) TINYFORMAT_PASSARGS_ ## n
402#define TINYFORMAT_PASSARGS_TAIL(n) TINYFORMAT_PASSARGS_TAIL_ ## n
403
404// To keep it as transparent as possible, the macros below have been generated
405// using python via the excellent cog.py code generation script. This avoids
406// the need for a bunch of complex (but more general) preprocessor tricks as
407// used in boost.preprocessor.
408//
409// To rerun the code generation in place, use `cog.py -r tinyformat.h`
410// (see http://nedbatchelder.com/code/cog). Alternatively you can just create
411// extra versions by hand.
412
413/*[[[cog
414maxParams = 16
415
416def makeCommaSepLists(lineTemplate, elemTemplate, startInd=1):
417 for j in range(startInd,maxParams+1):
418 list = ', '.join([elemTemplate % {'i':i} for i in range(startInd,j+1)])
419 cog.outl(lineTemplate % {'j':j, 'list':list})
420
421makeCommaSepLists('#define TINYFORMAT_ARGTYPES_%(j)d %(list)s',
422 'class T%(i)d')
423
424cog.outl()
425makeCommaSepLists('#define TINYFORMAT_VARARGS_%(j)d %(list)s',
426 'const T%(i)d& v%(i)d')
427
428cog.outl()
429makeCommaSepLists('#define TINYFORMAT_PASSARGS_%(j)d %(list)s', 'v%(i)d')
430
431cog.outl()
432cog.outl('#define TINYFORMAT_PASSARGS_TAIL_1')
433makeCommaSepLists('#define TINYFORMAT_PASSARGS_TAIL_%(j)d , %(list)s',
434 'v%(i)d', startInd = 2)
435
436cog.outl()
437cog.outl('#define TINYFORMAT_FOREACH_ARGNUM(m) \\\n ' +
438 ' '.join(['m(%d)' % (j,) for j in range(1,maxParams+1)]))
439]]]*/
440#define TINYFORMAT_ARGTYPES_1 class T1
441#define TINYFORMAT_ARGTYPES_2 class T1, class T2
442#define TINYFORMAT_ARGTYPES_3 class T1, class T2, class T3
443#define TINYFORMAT_ARGTYPES_4 class T1, class T2, class T3, class T4
444#define TINYFORMAT_ARGTYPES_5 class T1, class T2, class T3, class T4, class T5
445#define TINYFORMAT_ARGTYPES_6 class T1, class T2, class T3, class T4, class T5, class T6
446#define TINYFORMAT_ARGTYPES_7 class T1, class T2, class T3, class T4, class T5, class T6, class T7
447#define TINYFORMAT_ARGTYPES_8 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8
448#define TINYFORMAT_ARGTYPES_9 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9
449#define TINYFORMAT_ARGTYPES_10 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10
450#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
451#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
452#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
453#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
454#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
455#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
456
457#define TINYFORMAT_VARARGS_1 const T1& v1
458#define TINYFORMAT_VARARGS_2 const T1& v1, const T2& v2
459#define TINYFORMAT_VARARGS_3 const T1& v1, const T2& v2, const T3& v3
460#define TINYFORMAT_VARARGS_4 const T1& v1, const T2& v2, const T3& v3, const T4& v4
461#define TINYFORMAT_VARARGS_5 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5
462#define TINYFORMAT_VARARGS_6 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6
463#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
464#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
465#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
466#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
467#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
468#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
469#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
470#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
471#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
472#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
473
474#define TINYFORMAT_PASSARGS_1 v1
475#define TINYFORMAT_PASSARGS_2 v1, v2
476#define TINYFORMAT_PASSARGS_3 v1, v2, v3
477#define TINYFORMAT_PASSARGS_4 v1, v2, v3, v4
478#define TINYFORMAT_PASSARGS_5 v1, v2, v3, v4, v5
479#define TINYFORMAT_PASSARGS_6 v1, v2, v3, v4, v5, v6
480#define TINYFORMAT_PASSARGS_7 v1, v2, v3, v4, v5, v6, v7
481#define TINYFORMAT_PASSARGS_8 v1, v2, v3, v4, v5, v6, v7, v8
482#define TINYFORMAT_PASSARGS_9 v1, v2, v3, v4, v5, v6, v7, v8, v9
483#define TINYFORMAT_PASSARGS_10 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10
484#define TINYFORMAT_PASSARGS_11 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11
485#define TINYFORMAT_PASSARGS_12 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12
486#define TINYFORMAT_PASSARGS_13 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13
487#define TINYFORMAT_PASSARGS_14 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14
488#define TINYFORMAT_PASSARGS_15 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15
489#define TINYFORMAT_PASSARGS_16 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16
490
491#define TINYFORMAT_PASSARGS_TAIL_1
492#define TINYFORMAT_PASSARGS_TAIL_2 , v2
493#define TINYFORMAT_PASSARGS_TAIL_3 , v2, v3
494#define TINYFORMAT_PASSARGS_TAIL_4 , v2, v3, v4
495#define TINYFORMAT_PASSARGS_TAIL_5 , v2, v3, v4, v5
496#define TINYFORMAT_PASSARGS_TAIL_6 , v2, v3, v4, v5, v6
497#define TINYFORMAT_PASSARGS_TAIL_7 , v2, v3, v4, v5, v6, v7
498#define TINYFORMAT_PASSARGS_TAIL_8 , v2, v3, v4, v5, v6, v7, v8
499#define TINYFORMAT_PASSARGS_TAIL_9 , v2, v3, v4, v5, v6, v7, v8, v9
500#define TINYFORMAT_PASSARGS_TAIL_10 , v2, v3, v4, v5, v6, v7, v8, v9, v10
501#define TINYFORMAT_PASSARGS_TAIL_11 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11
502#define TINYFORMAT_PASSARGS_TAIL_12 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12
503#define TINYFORMAT_PASSARGS_TAIL_13 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13
504#define TINYFORMAT_PASSARGS_TAIL_14 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14
505#define TINYFORMAT_PASSARGS_TAIL_15 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15
506#define TINYFORMAT_PASSARGS_TAIL_16 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16
507
508#define TINYFORMAT_FOREACH_ARGNUM(m) \
509 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)
510//[[[end]]]
511
512
513
514namespace detail {
515
516// Type-opaque holder for an argument to format(), with associated actions on
517// the type held as explicit function pointers. This allows FormatArg's for
518// each argument to be allocated as a homogeneous array inside FormatList
519// whereas a naive implementation based on inheritance does not.
521{
522 public:
523 FormatArg() = default;
524
525 template<typename T>
526 explicit FormatArg(const T& value)
527 : m_value(static_cast<const void*>(&value)),
528 m_formatImpl(&formatImpl<T>),
529 m_toIntImpl(&toIntImpl<T>)
530 { }
531
532 void format(std::ostream& out, const char* fmtBegin,
533 const char* fmtEnd, int ntrunc) const
534 {
535 TINYFORMAT_ASSERT(m_value);
536 TINYFORMAT_ASSERT(m_formatImpl);
537 m_formatImpl(out, fmtBegin, fmtEnd, ntrunc, m_value);
538 }
539
540 int toInt() const
541 {
542 TINYFORMAT_ASSERT(m_value);
543 TINYFORMAT_ASSERT(m_toIntImpl);
544 return m_toIntImpl(m_value);
545 }
546
547 private:
548 template<typename T>
549 TINYFORMAT_HIDDEN static void formatImpl(std::ostream& out, const char* fmtBegin,
550 const char* fmtEnd, int ntrunc, const void* value)
551 {
552 formatValue(out, fmtBegin, fmtEnd, ntrunc, *static_cast<const T*>(value));
553 }
554
555 template<typename T>
556 TINYFORMAT_HIDDEN static int toIntImpl(const void* value)
557 {
558 return convertToInt<T>::invoke(*static_cast<const T*>(value));
559 }
560
561 const void* m_value{nullptr};
562 void (*m_formatImpl)(std::ostream& out, const char* fmtBegin,
563 const char* fmtEnd, int ntrunc, const void* value){nullptr};
564 int (*m_toIntImpl)(const void* value){nullptr};
565};
566
567
568// Parse and return an integer from the string c, as atoi()
569// On return, c is set to one past the end of the integer.
570inline int parseIntAndAdvance(const char*& c)
571{
572 int i = 0;
573 for (;*c >= '0' && *c <= '9'; ++c)
574 i = 10*i + (*c - '0');
575 return i;
576}
577
578// Parse width or precision `n` from format string pointer `c`, and advance it
579// to the next character. If an indirection is requested with `*`, the argument
580// is read from `args[argIndex]` and `argIndex` is incremented (or read
581// from `args[n]` in positional mode). Returns true if one or more
582// characters were read.
583inline bool parseWidthOrPrecision(int& n, const char*& c, bool positionalMode,
584 const detail::FormatArg* args,
585 int& argIndex, int numArgs)
586{
587 if (*c >= '0' && *c <= '9') {
588 n = parseIntAndAdvance(c);
589 }
590 else if (*c == '*') {
591 ++c;
592 n = 0;
593 if (positionalMode) {
594 int pos = parseIntAndAdvance(c) - 1;
595 if (*c != '$')
596 TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one");
597 if (pos >= 0 && pos < numArgs)
598 n = args[pos].toInt();
599 else
600 TINYFORMAT_ERROR("tinyformat: Positional argument out of range");
601 ++c;
602 }
603 else {
604 if (argIndex < numArgs)
605 n = args[argIndex++].toInt();
606 else
607 TINYFORMAT_ERROR("tinyformat: Not enough arguments to read variable width or precision");
608 }
609 }
610 else {
611 return false;
612 }
613 return true;
614}
615
616// Print literal part of format string and return next format spec position.
617//
618// Skips over any occurrences of '%%', printing a literal '%' to the output.
619// The position of the first % character of the next nontrivial format spec is
620// returned, or the end of string.
621inline const char* printFormatStringLiteral(std::ostream& out, const char* fmt)
622{
623 const char* c = fmt;
624 for (;; ++c) {
625 if (*c == '\0') {
626 out.write(fmt, c - fmt);
627 return c;
628 }
629 else if (*c == '%') {
630 out.write(fmt, c - fmt);
631 if (*(c+1) != '%')
632 return c;
633 // for "%%", tack trailing % onto next literal section.
634 fmt = ++c;
635 }
636 }
637}
638
639
640// Parse a format string and set the stream state accordingly.
641//
642// The format mini-language recognized here is meant to be the one from C99,
643// with the form "%[flags][width][.precision][length]type" with POSIX
644// positional arguments extension.
645//
646// POSIX positional arguments extension:
647// Conversions can be applied to the nth argument after the format in
648// the argument list, rather than to the next unused argument. In this case,
649// the conversion specifier character % (see below) is replaced by the sequence
650// "%n$", where n is a decimal integer in the range [1,{NL_ARGMAX}],
651// giving the position of the argument in the argument list. This feature
652// provides for the definition of format strings that select arguments
653// in an order appropriate to specific languages.
654//
655// The format can contain either numbered argument conversion specifications
656// (that is, "%n$" and "*m$"), or unnumbered argument conversion specifications
657// (that is, % and * ), but not both. The only exception to this is that %%
658// can be mixed with the "%n$" form. The results of mixing numbered and
659// unnumbered argument specifications in a format string are undefined.
660// When numbered argument specifications are used, specifying the Nth argument
661// requires that all the leading arguments, from the first to the (N-1)th,
662// are specified in the format string.
663//
664// In format strings containing the "%n$" form of conversion specification,
665// numbered arguments in the argument list can be referenced from the format
666// string as many times as required.
667//
668// Formatting options which can't be natively represented using the ostream
669// state are returned in spacePadPositive (for space padded positive numbers)
670// and ntrunc (for truncating conversions). argIndex is incremented if
671// necessary to pull out variable width and precision. The function returns a
672// pointer to the character after the end of the current format spec.
673inline const char* streamStateFromFormat(std::ostream& out, bool& positionalMode,
674 bool& spacePadPositive,
675 int& ntrunc, const char* fmtStart,
676 const detail::FormatArg* args,
677 int& argIndex, int numArgs)
678{
679 TINYFORMAT_ASSERT(*fmtStart == '%');
680 // Reset stream state to defaults.
681 out.width(0);
682 out.precision(6);
683 out.fill(' ');
684 // Reset most flags; ignore irrelevant unitbuf & skipws.
685 out.unsetf(std::ios::adjustfield | std::ios::basefield |
686 std::ios::floatfield | std::ios::showbase | std::ios::boolalpha |
687 std::ios::showpoint | std::ios::showpos | std::ios::uppercase);
688 bool precisionSet = false;
689 bool widthSet = false;
690 int widthExtra = 0;
691 const char* c = fmtStart + 1;
692
693 // 1) Parse an argument index (if followed by '$') or a width possibly
694 // preceded with '0' flag.
695 if (*c >= '0' && *c <= '9') {
696 const char tmpc = *c;
697 int value = parseIntAndAdvance(c);
698 if (*c == '$') {
699 // value is an argument index
700 if (value > 0 && value <= numArgs)
701 argIndex = value - 1;
702 else
703 TINYFORMAT_ERROR("tinyformat: Positional argument out of range");
704 ++c;
705 positionalMode = true;
706 }
707 else if (positionalMode) {
708 TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one");
709 }
710 else {
711 if (tmpc == '0') {
712 // Use internal padding so that numeric values are
713 // formatted correctly, eg -00010 rather than 000-10
714 out.fill('0');
715 out.setf(std::ios::internal, std::ios::adjustfield);
716 }
717 if (value != 0) {
718 // Nonzero value means that we parsed width.
719 widthSet = true;
720 out.width(value);
721 }
722 }
723 }
724 else if (positionalMode) {
725 TINYFORMAT_ERROR("tinyformat: Non-positional argument used after a positional one");
726 }
727 // 2) Parse flags and width if we did not do it in previous step.
728 if (!widthSet) {
729 // Parse flags
730 for (;; ++c) {
731 switch (*c) {
732 case '#':
733 out.setf(std::ios::showpoint | std::ios::showbase);
734 continue;
735 case '0':
736 // overridden by left alignment ('-' flag)
737 if (!(out.flags() & std::ios::left)) {
738 // Use internal padding so that numeric values are
739 // formatted correctly, eg -00010 rather than 000-10
740 out.fill('0');
741 out.setf(std::ios::internal, std::ios::adjustfield);
742 }
743 continue;
744 case '-':
745 out.fill(' ');
746 out.setf(std::ios::left, std::ios::adjustfield);
747 continue;
748 case ' ':
749 // overridden by show positive sign, '+' flag.
750 if (!(out.flags() & std::ios::showpos))
751 spacePadPositive = true;
752 continue;
753 case '+':
754 out.setf(std::ios::showpos);
755 spacePadPositive = false;
756 widthExtra = 1;
757 continue;
758 default:
759 break;
760 }
761 break;
762 }
763 // Parse width
764 int width = 0;
765 widthSet = parseWidthOrPrecision(width, c, positionalMode,
766 args, argIndex, numArgs);
767 if (widthSet) {
768 if (width < 0) {
769 // negative widths correspond to '-' flag set
770 out.fill(' ');
771 out.setf(std::ios::left, std::ios::adjustfield);
772 width = -width;
773 }
774 out.width(width);
775 }
776 }
777 // 3) Parse precision
778 if (*c == '.') {
779 ++c;
780 int precision = 0;
781 parseWidthOrPrecision(precision, c, positionalMode,
782 args, argIndex, numArgs);
783 // Presence of `.` indicates precision set, unless the inferred value
784 // was negative in which case the default is used.
785 precisionSet = precision >= 0;
786 if (precisionSet)
787 out.precision(precision);
788 }
789 // 4) Ignore any C99 length modifier
790 while (*c == 'l' || *c == 'h' || *c == 'L' ||
791 *c == 'j' || *c == 'z' || *c == 't') {
792 ++c;
793 }
794 // 5) We're up to the conversion specifier character.
795 // Set stream flags based on conversion specifier (thanks to the
796 // boost::format class for forging the way here).
797 bool intConversion = false;
798 switch (*c) {
799 case 'u': case 'd': case 'i':
800 out.setf(std::ios::dec, std::ios::basefield);
801 intConversion = true;
802 break;
803 case 'o':
804 out.setf(std::ios::oct, std::ios::basefield);
805 intConversion = true;
806 break;
807 case 'X':
808 out.setf(std::ios::uppercase);
809 [[fallthrough]];
810 case 'x': case 'p':
811 out.setf(std::ios::hex, std::ios::basefield);
812 intConversion = true;
813 break;
814 case 'E':
815 out.setf(std::ios::uppercase);
816 [[fallthrough]];
817 case 'e':
818 out.setf(std::ios::scientific, std::ios::floatfield);
819 out.setf(std::ios::dec, std::ios::basefield);
820 break;
821 case 'F':
822 out.setf(std::ios::uppercase);
823 [[fallthrough]];
824 case 'f':
825 out.setf(std::ios::fixed, std::ios::floatfield);
826 break;
827 case 'A':
828 out.setf(std::ios::uppercase);
829 [[fallthrough]];
830 case 'a':
831# ifdef _MSC_VER
832 // Workaround https://developercommunity.visualstudio.com/content/problem/520472/hexfloat-stream-output-does-not-ignore-precision-a.html
833 // by always setting maximum precision on MSVC to avoid precision
834 // loss for doubles.
835 out.precision(13);
836# endif
837 out.setf(std::ios::fixed | std::ios::scientific, std::ios::floatfield);
838 break;
839 case 'G':
840 out.setf(std::ios::uppercase);
841 [[fallthrough]];
842 case 'g':
843 out.setf(std::ios::dec, std::ios::basefield);
844 // As in boost::format, let stream decide float format.
845 out.flags(out.flags() & ~std::ios::floatfield);
846 break;
847 case 'c':
848 // Handled as special case inside formatValue()
849 break;
850 case 's':
851 if (precisionSet)
852 ntrunc = static_cast<int>(out.precision());
853 // Make %s print Booleans as "true" and "false"
854 out.setf(std::ios::boolalpha);
855 break;
856 case 'n':
857 // Not supported - will cause problems!
858 TINYFORMAT_ERROR("tinyformat: %n conversion spec not supported");
859 break;
860 case '\0':
861 TINYFORMAT_ERROR("tinyformat: Conversion spec incorrectly "
862 "terminated by end of string");
863 return c;
864 default:
865 break;
866 }
867 if (intConversion && precisionSet && !widthSet) {
868 // "precision" for integers gives the minimum number of digits (to be
869 // padded with zeros on the left). This isn't really supported by the
870 // iostreams, but we can approximately simulate it with the width if
871 // the width isn't otherwise used.
872 out.width(out.precision() + widthExtra);
873 out.setf(std::ios::internal, std::ios::adjustfield);
874 out.fill('0');
875 }
876 return c+1;
877}
878
879
880//------------------------------------------------------------------------------
881inline void formatImpl(std::ostream& out, const char* fmt,
882 const detail::FormatArg* args,
883 int numArgs)
884{
885 // Saved stream state
886 std::streamsize origWidth = out.width();
887 std::streamsize origPrecision = out.precision();
888 std::ios::fmtflags origFlags = out.flags();
889 char origFill = out.fill();
890
891 // "Positional mode" means all format specs should be of the form "%n$..."
892 // with `n` an integer. We detect this in `streamStateFromFormat`.
893 bool positionalMode = false;
894 int argIndex = 0;
895 while (true) {
896 fmt = printFormatStringLiteral(out, fmt);
897 if (*fmt == '\0') {
898 if (!positionalMode && argIndex < numArgs) {
899 TINYFORMAT_ERROR("tinyformat: Not enough conversion specifiers in format string");
900 }
901 break;
902 }
903 bool spacePadPositive = false;
904 int ntrunc = -1;
905 const char* fmtEnd = streamStateFromFormat(out, positionalMode, spacePadPositive, ntrunc, fmt,
906 args, argIndex, numArgs);
907 // NB: argIndex may be incremented by reading variable width/precision
908 // in `streamStateFromFormat`, so do the bounds check here.
909 if (argIndex >= numArgs) {
910 TINYFORMAT_ERROR("tinyformat: Too many conversion specifiers in format string");
911 return;
912 }
913 const FormatArg& arg = args[argIndex];
914 // Format the arg into the stream.
915 if (!spacePadPositive) {
916 arg.format(out, fmt, fmtEnd, ntrunc);
917 }
918 else {
919 // The following is a special case with no direct correspondence
920 // between stream formatting and the printf() behaviour. Simulate
921 // it crudely by formatting into a temporary string stream and
922 // munging the resulting string.
923 std::ostringstream tmpStream;
924 tmpStream.copyfmt(out);
925 tmpStream.setf(std::ios::showpos);
926 arg.format(tmpStream, fmt, fmtEnd, ntrunc);
927 std::string result = tmpStream.str(); // allocates... yuck.
928 for (size_t i = 0, iend = result.size(); i < iend; ++i) {
929 if (result[i] == '+')
930 result[i] = ' ';
931 }
932 out << result;
933 }
934 if (!positionalMode)
935 ++argIndex;
936 fmt = fmtEnd;
937 }
938
939 // Restore stream state
940 out.width(origWidth);
941 out.precision(origPrecision);
942 out.flags(origFlags);
943 out.fill(origFill);
944}
945
946} // namespace detail
947
948
956{
957 public:
959 : m_args(args), m_N(N) { }
960
961 friend void vformat(std::ostream& out, const char* fmt,
962 const FormatList& list);
963
964 private:
966 int m_N;
967};
968
971
972
973namespace detail {
974
975// Format list subclass with fixed storage to avoid dynamic allocation
976template<int N>
978{
979 public:
980#ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
981 template<typename... Args>
982 explicit FormatListN(const Args&... args)
983 : FormatList(&m_formatterStore[0], N),
984 m_formatterStore { FormatArg(args)... }
985 { static_assert(sizeof...(args) == N, "Number of args must be N"); }
986#else // C++98 version
987 void init(int) {}
988# define TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR(n) \
989 \
990 template<TINYFORMAT_ARGTYPES(n)> \
991 FormatListN(TINYFORMAT_VARARGS(n)) \
992 : FormatList(&m_formatterStore[0], n) \
993 { TINYFORMAT_ASSERT(n == N); init(0, TINYFORMAT_PASSARGS(n)); } \
994 \
995 template<TINYFORMAT_ARGTYPES(n)> \
996 void init(int i, TINYFORMAT_VARARGS(n)) \
997 { \
998 m_formatterStore[i] = FormatArg(v1); \
999 init(i+1 TINYFORMAT_PASSARGS_TAIL(n)); \
1000 }
1001
1002 TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR)
1003# undef TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR
1004#endif
1006 : FormatList(&m_formatterStore[0], N)
1007 { std::copy(&other.m_formatterStore[0], &other.m_formatterStore[N],
1008 &m_formatterStore[0]); }
1009
1010 private:
1011 FormatArg m_formatterStore[N];
1012};
1013
1014// Special 0-arg version - MSVC says zero-sized C array in struct is nonstandard
1015template<> class FormatListN<0> : public FormatList
1016{
1017public:
1018 FormatListN() : FormatList(nullptr, 0) {}
1019};
1020
1021} // namespace detail
1022
1023
1024//------------------------------------------------------------------------------
1025// Primary API functions
1026
1027#ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
1028
1035template<typename... Args>
1036detail::FormatListN<sizeof...(Args)> makeFormatList(const Args&... args)
1037{
1038 return detail::FormatListN<sizeof...(args)>(args...);
1039}
1040
1041#else // C++98 version
1042
1043inline detail::FormatListN<0> makeFormatList()
1044{
1045 return detail::FormatListN<0>();
1046}
1047#define TINYFORMAT_MAKE_MAKEFORMATLIST(n) \
1048template<TINYFORMAT_ARGTYPES(n)> \
1049detail::FormatListN<n> makeFormatList(TINYFORMAT_VARARGS(n)) \
1050{ \
1051 return detail::FormatListN<n>(TINYFORMAT_PASSARGS(n)); \
1052}
1053TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_MAKEFORMATLIST)
1054#undef TINYFORMAT_MAKE_MAKEFORMATLIST
1055
1056#endif
1057
1062inline void vformat(std::ostream& out, const char* fmt, FormatListRef list)
1063{
1064 detail::formatImpl(out, fmt, list.m_args, list.m_N);
1065}
1066
1067
1068#ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
1069
1071template<typename... Args>
1072void format(std::ostream& out, FormatStringCheck<sizeof...(Args)> fmt, const Args&... args)
1073{
1074 vformat(out, fmt, makeFormatList(args...));
1075}
1076
1079template<typename... Args>
1080std::string format(FormatStringCheck<sizeof...(Args)> fmt, const Args&... args)
1081{
1082 std::ostringstream oss;
1083 format(oss, fmt, args...);
1084 return oss.str();
1085}
1086
1088template<typename... Args>
1089void printf(FormatStringCheck<sizeof...(Args)> fmt, const Args&... args)
1090{
1091 format(std::cout, fmt, args...);
1092}
1093
1094template<typename... Args>
1095void printfln(FormatStringCheck<sizeof...(Args)> fmt, const Args&... args)
1096{
1097 format(std::cout, fmt, args...);
1098 std::cout << '\n';
1099}
1100
1101
1102#else // C++98 version
1103
1104inline void format(std::ostream& out, const char* fmt)
1105{
1106 vformat(out, fmt, makeFormatList());
1107}
1108
1109inline std::string format(const char* fmt)
1110{
1111 std::ostringstream oss;
1112 format(oss, fmt);
1113 return oss.str();
1114}
1115
1116inline void printf(const char* fmt)
1117{
1118 format(std::cout, fmt);
1119}
1120
1121inline void printfln(const char* fmt)
1122{
1123 format(std::cout, fmt);
1124 std::cout << '\n';
1125}
1126
1127#define TINYFORMAT_MAKE_FORMAT_FUNCS(n) \
1128 \
1129template<TINYFORMAT_ARGTYPES(n)> \
1130void format(std::ostream& out, const char* fmt, TINYFORMAT_VARARGS(n)) \
1131{ \
1132 vformat(out, fmt, makeFormatList(TINYFORMAT_PASSARGS(n))); \
1133} \
1134 \
1135template<TINYFORMAT_ARGTYPES(n)> \
1136std::string format(const char* fmt, TINYFORMAT_VARARGS(n)) \
1137{ \
1138 std::ostringstream oss; \
1139 format(oss, fmt, TINYFORMAT_PASSARGS(n)); \
1140 return oss.str(); \
1141} \
1142 \
1143template<TINYFORMAT_ARGTYPES(n)> \
1144void printf(const char* fmt, TINYFORMAT_VARARGS(n)) \
1145{ \
1146 format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \
1147} \
1148 \
1149template<TINYFORMAT_ARGTYPES(n)> \
1150void printfln(const char* fmt, TINYFORMAT_VARARGS(n)) \
1151{ \
1152 format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \
1153 std::cout << '\n'; \
1154}
1155
1156TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMAT_FUNCS)
1157#undef TINYFORMAT_MAKE_FORMAT_FUNCS
1158
1159#endif
1160
1161} // namespace tinyformat
1162
1163// Added for Bitcoin Core:
1165#define strprintf tfm::format
1166
1167#endif // TINYFORMAT_H_INCLUDED
std::unique_ptr< interfaces::Init > init
ArgsManager & args
Definition: bitcoind.cpp:277
List of template arguments format(), held in a type-opaque way.
Definition: tinyformat.h:956
friend void vformat(std::ostream &out, const char *fmt, const FormatList &list)
FormatList(detail::FormatArg *args, int N)
Definition: tinyformat.h:958
const detail::FormatArg * m_args
Definition: tinyformat.h:965
static TINYFORMAT_HIDDEN void 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:556
void format(std::ostream &out, const char *fmtBegin, const char *fmtEnd, int ntrunc) const
Definition: tinyformat.h:532
FormatListN(const Args &... args)
Definition: tinyformat.h:982
FormatListN(const FormatListN &other)
Definition: tinyformat.h:1005
format_error(const std::string &what)
Definition: tinyformat.h:198
#define T(expected, seed, data)
void formatTruncated(std::ostream &out, const T &value, int ntrunc)
Definition: tinyformat.h:301
const char * printFormatStringLiteral(std::ostream &out, const char *fmt)
Definition: tinyformat.h:621
int parseIntAndAdvance(const char *&c)
Definition: tinyformat.h:570
void formatImpl(std::ostream &out, const char *fmt, const detail::FormatArg *args, int numArgs)
Definition: tinyformat.h:881
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:673
bool parseWidthOrPrecision(int &n, const char *&c, bool positionalMode, const detail::FormatArg *args, int &argIndex, int numArgs)
Definition: tinyformat.h:583
void printf(FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1089
std::string format(FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments according to the given format string and return the result as a string.
Definition: tinyformat.h:1080
void printfln(FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Definition: tinyformat.h:1095
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:1062
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:342
detail::FormatListN< sizeof...(Args)> makeFormatList(const Args &... args)
Make type-agnostic format list from list of template arguments.
Definition: tinyformat.h:1036
const FormatList & FormatListRef
Reference to type-opaque format list for passing to vformat()
Definition: tinyformat.h:970
FormatStringCheck(const std::string &str)
Definition: tinyformat.h:188
consteval FormatStringCheck(const char *str)
Definition: tinyformat.h:187
FormatStringCheck(util::ConstevalFormatString< num_params > str)
Definition: tinyformat.h:189
static int invoke(const T &value)
Definition: tinyformat.h:296
static int invoke(const T &)
Definition: tinyformat.h:285
static void invoke(std::ostream &out, const T &value)
Definition: tinyformat.h:256
static void invoke(std::ostream &, const T &)
Definition: tinyformat.h:249
static succeed tryConvert(const T2 &)
A wrapper for a compile-time partially validated format string.
Definition: string.h:92
#define TINYFORMAT_HIDDEN
Definition: tinyformat.h:177
#define TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(type)
Definition: tinyformat.h:308
#define TINYFORMAT_ASSERT(cond)
Definition: tinyformat.h:152
#define TINYFORMAT_ERROR(reasonString)
Definition: tinyformat.h:135
#define TINYFORMAT_DEFINE_FORMATVALUE_CHAR(charType)
Definition: tinyformat.h:376
#define TINYFORMAT_FOREACH_ARGNUM(m)
Definition: tinyformat.h:508