Fixed Bug #43: jkqtp_format() had undefined behaviour, because va_start was called with a ref-parameter, which does not work. Now there are 4 overloaded template variants. See https://github.com/jkriege2/JKQtPlotter/issues/43

This commit is contained in:
jkriege2 2020-06-27 14:35:45 +02:00
parent 2b942e1e2d
commit b22b4ca935
2 changed files with 37 additions and 11 deletions

View File

@ -81,19 +81,10 @@ std::string jkqtp_tolower(const std::string& s){
return d; return d;
}; };
std::string jkqtp_format(const std::string& templ, ...){
va_list ap;
char buffer[4096];
va_start (ap, templ);
vsnprintf(buffer, 4096, templ.c_str(), ap);
va_end (ap);
std::string ret(buffer);
return ret;
};
std::string jkqtp_bytestostr(double bytes){ std::string jkqtp_bytestostr(double bytes){
double data=bytes; double data=bytes;
std::string form="%.0lf"; std::string form="%.0lf";
std::string res=jkqtp_format(form,data); std::string res=jkqtp_format(form,data);
form="%.3lf"; form="%.3lf";

View File

@ -89,7 +89,42 @@ JKQTCOMMON_LIB_EXPORT std::string jkqtp_toupper(const std::string& s);
/** \brief std::string wrapper around sprintf() /** \brief std::string wrapper around sprintf()
* \ingroup jkqtptools_string * \ingroup jkqtptools_string
*/ */
JKQTCOMMON_LIB_EXPORT std::string jkqtp_format(const std::string& templ, ...); template <class T1>
inline std::string jkqtp_format(const std::string& templ, T1 d1) {
char buffer[4096];
snprintf(buffer, 4096, templ.c_str(), d1);
return std::string(buffer);
};
/** \brief std::string wrapper around sprintf()
* \ingroup jkqtptools_string
*/
template <class T1, class T2>
inline std::string jkqtp_format(const std::string& templ, T1 d1, T2 d2) {
char buffer[4096];
snprintf(buffer, 4096, templ.c_str(), d1, d2);
return std::string(buffer);
};
/** \brief std::string wrapper around sprintf()
* \ingroup jkqtptools_string
*/
template <class T1, class T2, class T3>
inline std::string jkqtp_format(const std::string& templ, T1 d1, T2 d2, T3 d3) {
char buffer[4096];
snprintf(buffer, 4096, templ.c_str(), d1, d2, d3);
return std::string(buffer);
};
/** \brief std::string wrapper around sprintf()
* \ingroup jkqtptools_string
*/
template <class T1, class T2, class T3, class T4>
inline std::string jkqtp_format(const std::string& templ, T1 d1, T2 d2, T3 d3, T4 d4) {
char buffer[4096];
snprintf(buffer, 4096, templ.c_str(), d1, d2, d3, d4);
return std::string(buffer);
};
/** \brief convert a number of bytes to a string, formatting e.g. 1024 as 1kB, ... /** \brief convert a number of bytes to a string, formatting e.g. 1024 as 1kB, ...
* \ingroup jkqtptools_string * \ingroup jkqtptools_string