diff --git a/core/checkcloudconnection.cpp b/core/checkcloudconnection.cpp index 854451eb7..c751bd538 100644 --- a/core/checkcloudconnection.cpp +++ b/core/checkcloudconnection.cpp @@ -10,6 +10,7 @@ #include "qthelper.h" #include "git-access.h" #include "errorhelper.h" +#include "core/format.h" #include "core/subsurface-string.h" #include "core/membuffer.h" #include "core/settings/qPrefCloudStorage.h" diff --git a/core/format.cpp b/core/format.cpp index b037361bb..345b564f9 100644 --- a/core/format.cpp +++ b/core/format.cpp @@ -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; +} diff --git a/core/format.h b/core/format.h index 511b319f8..71f8a7875 100644 --- a/core/format.h +++ b/core/format.h @@ -25,6 +25,9 @@ __printf(2, 0) int vasprintf_loc(char **dst, const char *cformat, va_list ap); #ifdef __cplusplus } + +__printf(1, 2) std::string format_string_std(const char *fmt, ...); + #endif #endif diff --git a/core/libdivecomputer.cpp b/core/libdivecomputer.cpp index 77d7b11c5..d67119339 100644 --- a/core/libdivecomputer.cpp +++ b/core/libdivecomputer.cpp @@ -19,6 +19,7 @@ #include "sample.h" #include "subsurface-float.h" #include "subsurface-string.h" +#include "format.h" #include "device.h" #include "dive.h" #include "errorhelper.h" diff --git a/core/load-git.cpp b/core/load-git.cpp index 9c12fdfe5..75af6cba7 100644 --- a/core/load-git.cpp +++ b/core/load-git.cpp @@ -23,6 +23,7 @@ #include "errorhelper.h" #include "sample.h" #include "subsurface-string.h" +#include "format.h" #include "trip.h" #include "device.h" #include "git-access.h" diff --git a/core/parse-xml.cpp b/core/parse-xml.cpp index ba1a5c26b..4b1cf9ca6 100644 --- a/core/parse-xml.cpp +++ b/core/parse-xml.cpp @@ -26,6 +26,7 @@ #include "divesite.h" #include "errorhelper.h" #include "parse.h" +#include "format.h" #include "subsurface-float.h" #include "subsurface-string.h" #include "subsurface-time.h" diff --git a/core/subsurface-string.h b/core/subsurface-string.h index 6f05063d5..4d4eda3f7 100644 --- a/core/subsurface-string.h +++ b/core/subsurface-string.h @@ -62,19 +62,6 @@ extern double strtod_flags(const char *str, const char **ptr, unsigned int flags } #include -template -std::string format_string_std(const char *fmt, Args&&... args) -{ - size_t stringsize = snprintf(NULL, 0, fmt, std::forward(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)...); - 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 diff --git a/core/worldmap-save.cpp b/core/worldmap-save.cpp index 5f3804a36..76826da66 100644 --- a/core/worldmap-save.cpp +++ b/core/worldmap-save.cpp @@ -15,7 +15,7 @@ #include "errorhelper.h" #include "file.h" #include "save-html.h" -#include "subsurface-string.h" +#include "format.h" #include "worldmap-save.h" #include "worldmap-options.h" #include "gettext.h"