VSRonin / QtXlsxWriter

.xlsx file reader and writer for Qt5
http://qtxlsx.debao.me
209 stars 73 forks source link

Use QString::asprintf() instead of sprintf() #42

Closed hasselmm closed 5 years ago

hasselmm commented 5 years ago

sprintf() causes redudant allocations and therefore got deprecated.

hasselmm commented 5 years ago

The documentation discourages its use because the format specifiers are not safe (on ancient compilers), but it isn't deprecated and actually the perfect fit for the intended use. QString::arg() also would have unnecessary allocations, which is exactly the reason why QString::sprintf() got deprecated in favor of QString::asprintf().

#if QT_DEPRECATED_SINCE(5, 14)
    QT_DEPRECATED_X("Use vasprintf(), arg() or QTextStream instead")
    QString &vsprintf(const char *format, va_list ap) Q_ATTRIBUTE_FORMAT_PRINTF(2, 0);
    QT_DEPRECATED_X("Use asprintf(), arg() or QTextStream instead")
    QString &sprintf(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(2, 3);
#endif
    static QString vasprintf(const char *format, va_list ap) Q_ATTRIBUTE_FORMAT_PRINTF(1, 0);
    static QString asprintf(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2);
VSRonin commented 5 years ago

I guess it's better than the gurrent code anyway