mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
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:
parent
a123589efb
commit
322c3b55e6
8 changed files with 27 additions and 14 deletions
|
@ -417,3 +417,21 @@ std::string casprintf_loc(const char *cformat, ...)
|
|||
va_end(ap);
|
||||
return std::string(utf8.constData(), utf8.size());
|
||||
}
|
||||
|
||||
std::string __printf(1, 2) format_string_std(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
size_t stringsize = vsnprintf(NULL, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
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.
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(res.data(), stringsize + 1, fmt, ap);
|
||||
va_end(ap);
|
||||
return res;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue