@@ -145,6 +145,7 @@ namespace tfm = tinyformat;
145
145
#include < iostream>
146
146
#include < sstream>
147
147
#include < stdexcept> // Added for Bitcoin Core
148
+ #include < util/string.h> // Added for Bitcoin Core
148
149
149
150
#ifndef TINYFORMAT_ASSERT
150
151
# include < cassert>
@@ -178,6 +179,18 @@ namespace tfm = tinyformat;
178
179
179
180
namespace tinyformat {
180
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.
185
+ template <unsigned num_params>
186
+ struct FormatStringCheck {
187
+ consteval FormatStringCheck (const char * str) : fmt{util::ConstevalFormatString<num_params>{str}.fmt } {}
188
+ FormatStringCheck (const std::string& str) : fmt{str.c_str ()} {}
189
+ FormatStringCheck (util::ConstevalFormatString<num_params> str) : fmt{str.fmt } {}
190
+ operator const char *() { return fmt; }
191
+ const char * fmt;
192
+ };
193
+
181
194
// Added for Bitcoin Core
182
195
class format_error : public std ::runtime_error
183
196
{
@@ -1056,15 +1069,15 @@ inline void vformat(std::ostream& out, const char* fmt, FormatListRef list)
1056
1069
1057
1070
// / Format list of arguments to the stream according to given format string.
1058
1071
template <typename ... Args>
1059
- void format (std::ostream& out, const char * fmt, const Args&... args)
1072
+ void format (std::ostream& out, FormatStringCheck< sizeof ...(Args)> fmt, const Args&... args)
1060
1073
{
1061
1074
vformat (out, fmt, makeFormatList (args...));
1062
1075
}
1063
1076
1064
1077
// / Format list of arguments according to the given format string and return
1065
1078
// / the result as a string.
1066
1079
template <typename ... Args>
1067
- std::string format (const char * fmt, const Args&... args)
1080
+ std::string format (FormatStringCheck< sizeof ...(Args)> fmt, const Args&... args)
1068
1081
{
1069
1082
std::ostringstream oss;
1070
1083
format (oss, fmt, args...);
@@ -1073,13 +1086,13 @@ std::string format(const char* fmt, const Args&... args)
1073
1086
1074
1087
// / Format list of arguments to std::cout, according to the given format string
1075
1088
template <typename ... Args>
1076
- void printf (const char * fmt, const Args&... args)
1089
+ void printf (FormatStringCheck< sizeof ...(Args)> fmt, const Args&... args)
1077
1090
{
1078
1091
format (std::cout, fmt, args...);
1079
1092
}
1080
1093
1081
1094
template <typename ... Args>
1082
- void printfln (const char * fmt, const Args&... args)
1095
+ void printfln (FormatStringCheck< sizeof ...(Args)> fmt, const Args&... args)
1083
1096
{
1084
1097
format (std::cout, fmt, args...);
1085
1098
std::cout << ' \n ' ;
@@ -1145,15 +1158,6 @@ TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMAT_FUNCS)
1145
1158
1146
1159
#endif
1147
1160
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
1161
} // namespace tinyformat
1158
1162
1159
1163
// Added for Bitcoin Core:
0 commit comments