diff --git a/src/tinyformat.h b/src/tinyformat.h index f536306375e59..0d0e9149ccd55 100644 --- a/src/tinyformat.h +++ b/src/tinyformat.h @@ -145,6 +145,7 @@ namespace tfm = tinyformat; #include #include #include // Added for Bitcoin Core +#include // Added for Bitcoin Core #ifndef TINYFORMAT_ASSERT # include @@ -178,6 +179,18 @@ namespace tfm = tinyformat; namespace tinyformat { +// Added for Bitcoin Core. Wrapper for checking format strings at compile time. +// Unlike ConstevalFormatString this supports std::string for runtime string +// formatting without compile time checks. +template +struct FormatStringCheck { + consteval FormatStringCheck(const char* str) : fmt{util::ConstevalFormatString{str}.fmt} {} + FormatStringCheck(const std::string& str) : fmt{str.c_str()} {} + FormatStringCheck(util::ConstevalFormatString str) : fmt{str.fmt} {} + operator const char*() { return fmt; } + const char* fmt; +}; + // Added for Bitcoin Core class format_error: public std::runtime_error { @@ -1056,7 +1069,7 @@ inline void vformat(std::ostream& out, const char* fmt, FormatListRef list) /// Format list of arguments to the stream according to given format string. template -void format(std::ostream& out, const char* fmt, const Args&... args) +void format(std::ostream& out, FormatStringCheck fmt, const Args&... args) { vformat(out, fmt, makeFormatList(args...)); } @@ -1064,7 +1077,7 @@ void format(std::ostream& out, const char* fmt, const Args&... args) /// Format list of arguments according to the given format string and return /// the result as a string. template -std::string format(const char* fmt, const Args&... args) +std::string format(FormatStringCheck fmt, const Args&... args) { std::ostringstream oss; format(oss, fmt, args...); @@ -1073,13 +1086,13 @@ std::string format(const char* fmt, const Args&... args) /// Format list of arguments to std::cout, according to the given format string template -void printf(const char* fmt, const Args&... args) +void printf(FormatStringCheck fmt, const Args&... args) { format(std::cout, fmt, args...); } template -void printfln(const char* fmt, const Args&... args) +void printfln(FormatStringCheck fmt, const Args&... args) { format(std::cout, fmt, args...); std::cout << '\n'; @@ -1145,15 +1158,6 @@ TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMAT_FUNCS) #endif -// Added for Bitcoin Core -template -std::string format(const std::string &fmt, const Args&... args) -{ - std::ostringstream oss; - format(oss, fmt.c_str(), args...); - return oss.str(); -} - } // namespace tinyformat // Added for Bitcoin Core: diff --git a/src/util/string.h b/src/util/string.h index c6c077f77b8a8..9cf2ecb768297 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -6,7 +6,6 @@ #define BITCOIN_UTIL_STRING_H #include -#include #include #include @@ -256,12 +255,4 @@ template } } // namespace util -namespace tinyformat { -template -std::string format(util::ConstevalFormatString fmt, const Args&... args) -{ - return format(fmt.fmt, args...); -} -} // namespace tinyformat - #endif // BITCOIN_UTIL_STRING_H