summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qvsnprintf.cpp
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2012-02-20 13:54:26 +0100
committerMarc Mutz <marc.mutz@kdab.com>2015-02-12 09:58:00 +0000
commitd251cae7b7370af328307948aca9bb63def22fe4 (patch)
treee70c25f582e5e890e0886320d3899a8f460a156b /src/corelib/tools/qvsnprintf.cpp
parent63114f4d3ce568e05346f7dba815b567da47894d (diff)
Long live QString::asprintf()!
asprintf() is a GNU extension that prints into a string it allocates internally. Arguably, that's a better name for QString::sprintf() since it also allocates memory internally. The main problem with QString::sprintf() isn't that it's dangerous to use (it is), but that it's not static. It also returns a reference instead of by-value, breaking RVO. There is a comment about removing this function completely in Qt 6.0, but it remains the only printf-style function in Qt that can allocate the target string, so it's vital for logging, e.g., and the recommended replacement code (http://linux.die.net/man/3/vsnprintf) is a nightmare. So this patch adds static (v)asprintf() methods to replace it. Further patches will fix up all in-tree callers and finally deprecate the old (v)sprintf(). Test coverage is provided through the existing tests of sprintf(), which is implemented in terms of asprintf(). Arguably, the in-tree callers show that QByteArray would benefit from having an asprintf(), too, as most of the in-tree code works around its lack with calls to to{Latin1,Local8Bit}() after using the QString version. [ChangeLog][QtCore][QString] Added asprintf(), vasprintf(). Change-Id: I8510f8d67c22230653ec0f1c252c01bc95f3c386 Reviewed-by: Kai Koehne <kai.koehne@theqtcompany.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qvsnprintf.cpp')
-rw-r--r--src/corelib/tools/qvsnprintf.cpp8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/corelib/tools/qvsnprintf.cpp b/src/corelib/tools/qvsnprintf.cpp
index 2526d1cd77..e9590c3971 100644
--- a/src/corelib/tools/qvsnprintf.cpp
+++ b/src/corelib/tools/qvsnprintf.cpp
@@ -58,10 +58,10 @@ QT_BEGIN_NAMESPACE
platforms, you should not rely on the return value or on the fact
that you will always get a 0 terminated string back.
- Ideally, you should never call this function but use QString::sprintf()
+ Ideally, you should never call this function but use QString::asprintf()
instead.
- \sa qsnprintf(), QString::sprintf()
+ \sa qsnprintf(), QString::asprintf()
*/
int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap)
@@ -107,9 +107,9 @@ int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap)
\warning Call this function only when you know what you are doing
since it shows different behavior on certain platforms.
- Use QString::sprintf() to format a string instead.
+ Use QString::asprintf() to format a string instead.
- \sa qvsnprintf(), QString::sprintf()
+ \sa qvsnprintf(), QString::asprintf()
*/
int qsnprintf(char *str, size_t n, const char *fmt, ...)