From 7fc3203062f27f582de84420fa0b519c58b5d1ff Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Thu, 21 Jul 2011 16:39:55 +0200 Subject: QAbstractConcatenable::convertFromAscii: make len the actual length Before, it was the length + 1, to include the ending \0 (for historical reasons) Having it the actual length is more intuitive and less error prone Also added QT_ASCII_CAST_WARN to QConcatenable::appendTo to show the warnig that convertion from ascii to qstring occurs. Change-Id: Ie7c8552b6b4e7ccb393cb09f5f0ca9b00336c714 Reviewed-by: thiago Reviewed-on: http://codereview.qt.nokia.com/1988 Reviewed-by: Qt Sanity Bot Reviewed-by: Olivier Goffart --- src/corelib/tools/qstringbuilder.cpp | 5 ++--- src/corelib/tools/qstringbuilder.h | 12 +++++------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/corelib/tools/qstringbuilder.cpp b/src/corelib/tools/qstringbuilder.cpp index 4c6848498b..7c1bde4ac7 100644 --- a/src/corelib/tools/qstringbuilder.cpp +++ b/src/corelib/tools/qstringbuilder.cpp @@ -108,13 +108,12 @@ QT_BEGIN_NAMESPACE */ /*! \internal - Note: The len contains the ending \0 */ void QAbstractConcatenable::convertFromAscii(const char *a, int len, QChar *&out) { #ifndef QT_NO_TEXTCODEC if (QString::codecForCStrings && len) { - QString tmp = QString::fromAscii(a, len > 0 ? len - 1 : -1); + QString tmp = QString::fromAscii(a, len > 0 ? len : -1); memcpy(out, reinterpret_cast(tmp.constData()), sizeof(QChar) * tmp.size()); out += tmp.length(); return; @@ -126,7 +125,7 @@ void QAbstractConcatenable::convertFromAscii(const char *a, int len, QChar *&out while (*a) *out++ = QLatin1Char(*a++); } else { - for (int i = 0; i < len - 1; ++i) + for (int i = 0; i < len; ++i) *out++ = QLatin1Char(a[i]); } } diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h index 63c487ef56..30b81c42f4 100644 --- a/src/corelib/tools/qstringbuilder.h +++ b/src/corelib/tools/qstringbuilder.h @@ -294,7 +294,7 @@ template struct QConcatenable : private QAbstractConcatenable #ifndef QT_NO_CAST_FROM_ASCII static inline void QT_ASCII_CAST_WARN appendTo(const char a[N], QChar *&out) { - QAbstractConcatenable::convertFromAscii(a, N, out); + QAbstractConcatenable::convertFromAscii(a, N - 1, out); } #endif static inline void appendTo(const char a[N], char *&out) @@ -313,7 +313,7 @@ template struct QConcatenable : private QAbstractConcaten #ifndef QT_NO_CAST_FROM_ASCII static inline void QT_ASCII_CAST_WARN appendTo(const char a[N], QChar *&out) { - QAbstractConcatenable::convertFromAscii(a, N, out); + QAbstractConcatenable::convertFromAscii(a, N - 1, out); } #endif static inline void appendTo(const char a[N], char *&out) @@ -349,10 +349,9 @@ template <> struct QConcatenable : private QAbstractConcatenable enum { ExactSize = false }; static int size(const QByteArray &ba) { return ba.size(); } #ifndef QT_NO_CAST_FROM_ASCII - static inline void appendTo(const QByteArray &ba, QChar *&out) + static inline QT_ASCII_CAST_WARN void appendTo(const QByteArray &ba, QChar *&out) { - // adding 1 because convertFromAscii expects the size including the null-termination - QAbstractConcatenable::convertFromAscii(ba.constData(), ba.size() + 1, out); + QAbstractConcatenable::convertFromAscii(ba.constData(), ba.size(), out); } #endif static inline void appendTo(const QByteArray &ba, char *&out) @@ -373,8 +372,7 @@ template struct QConcatenable > : private QAbs #ifndef QT_NO_CAST_FROM_ASCII static inline QT_ASCII_CAST_WARN void appendTo(const type &a, QChar *&out) { - // adding 1 because convertFromAscii expects the size including the null-termination - QAbstractConcatenable::convertFromAscii(a.ptr->data, N + 1, out); + QAbstractConcatenable::convertFromAscii(a.ptr->data, N, out); } #endif static inline void appendTo(const type &ba, char *&out) -- cgit v1.2.3