core: add compile time format check to format_string_std

Had to rewrite the thing, because gcc's warnings don't work
with templatized var-args. Since there is no string-format.cpp
and I didn't want to inline it, moved it to format.cpp.

String formatting is distributed around at least four
headers: membuffer.h, subsurface-string.h, format.h
and format-string.h. This really should be unified!

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-03-13 09:41:11 +01:00 committed by bstoeger
parent a123589efb
commit 322c3b55e6
8 changed files with 27 additions and 14 deletions

View file

@ -62,19 +62,6 @@ extern double strtod_flags(const char *str, const char **ptr, unsigned int flags
}
#include <string>
template <class... Args>
std::string format_string_std(const char *fmt, Args&&... args)
{
size_t stringsize = snprintf(NULL, 0, fmt, std::forward<Args>(args)...);
if (stringsize == 0)
return std::string();
std::string res;
res.resize(stringsize); // Pointless clearing, oh my.
// This overwrites the terminal null-byte of std::string.
// That's probably "undefined behavior". Oh my.
snprintf(res.data(), stringsize + 1, fmt, std::forward<Args>(args)...);
return res;
}
// Sadly, starts_with only with C++20!
inline bool starts_with(const std::string &s, const char *s2)
@ -83,4 +70,5 @@ inline bool starts_with(const std::string &s, const char *s2)
}
#endif
#endif // SUBSURFACE_STRING_H