summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Dzyubenko <denis.dzyubenko@nokia.com>2011-03-02 16:21:12 (GMT)
committerDenis Dzyubenko <denis.dzyubenko@nokia.com>2011-03-03 14:20:43 (GMT)
commit47e8fc1c237adb9742140f834226e9da9f60c2f2 (patch)
tree77e2d796664f763f8c4f4dc50aac027c21602bb5
parent61dfa74bb542f495eb5ff25e3f91b9065eb1cfdd (diff)
downloadQt-47e8fc1c237adb9742140f834226e9da9f60c2f2.zip
Qt-47e8fc1c237adb9742140f834226e9da9f60c2f2.tar.gz
Qt-47e8fc1c237adb9742140f834226e9da9f60c2f2.tar.bz2
Improve handling QByteArray with QStringBuilder
Instead of relying on the old behavior of the deprecated QString(QByteArray) constructor which stops copying the byte array at the null termination character if there is one embedded in the array, we copy the whole provided QByteArray object into the QString when QStringBuilder is used. Reviewed-by: Harald Fernengel
-rw-r--r--src/corelib/tools/qstringbuilder.cpp4
-rw-r--r--src/corelib/tools/qstringbuilder.h5
-rw-r--r--tests/auto/qstringbuilder1/stringbuilder.cpp26
3 files changed, 30 insertions, 5 deletions
diff --git a/src/corelib/tools/qstringbuilder.cpp b/src/corelib/tools/qstringbuilder.cpp
index 53368eb..7d75de7 100644
--- a/src/corelib/tools/qstringbuilder.cpp
+++ b/src/corelib/tools/qstringbuilder.cpp
@@ -150,8 +150,8 @@ QT_BEGIN_NAMESPACE
void QAbstractConcatenable::convertFromAscii(const char *a, int len, QChar *&out)
{
#ifndef QT_NO_TEXTCODEC
- if (QString::codecForCStrings) {
- QString tmp = QString::fromAscii(a);
+ if (QString::codecForCStrings && len) {
+ QString tmp = QString::fromAscii(a, len > 0 ? len - 1 : -1);
memcpy(out, reinterpret_cast<const char *>(tmp.constData()), sizeof(QChar) * tmp.size());
out += tmp.length();
return;
diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h
index 8d9537c..d230d67 100644
--- a/src/corelib/tools/qstringbuilder.h
+++ b/src/corelib/tools/qstringbuilder.h
@@ -270,10 +270,11 @@ template <> struct QConcatenable<QByteArray> : private QAbstractConcatenable
{
typedef QByteArray type;
enum { ExactSize = false };
- static int size(const QByteArray &ba) { return qstrnlen(ba.constData(), ba.size()); }
+ static int size(const QByteArray &ba) { return ba.size(); }
static inline void appendTo(const QByteArray &ba, QChar *&out)
{
- QAbstractConcatenable::convertFromAscii(ba.constData(), -1, out);
+ // adding 1 because convertFromAscii expects the size including the null-termination
+ QAbstractConcatenable::convertFromAscii(ba.constData(), ba.size() + 1, out);
}
};
#endif
diff --git a/tests/auto/qstringbuilder1/stringbuilder.cpp b/tests/auto/qstringbuilder1/stringbuilder.cpp
index 6faff35..f3c0ea9 100644
--- a/tests/auto/qstringbuilder1/stringbuilder.cpp
+++ b/tests/auto/qstringbuilder1/stringbuilder.cpp
@@ -40,9 +40,13 @@
****************************************************************************/
#define LITERAL "some literal"
+#define LITERAL_LEN (sizeof(LITERAL)-1)
+#define LITERAL_EXTRA "some literal" "EXTRA"
// "some literal", but replacing all vocals by their umlauted UTF-8 string :)
#define UTF8_LITERAL "s\xc3\xb6m\xc3\xab l\xc3\xaft\xc3\xabr\xc3\xa4l"
+#define UTF8_LITERAL_LEN (sizeof(UTF8_LITERAL)-1)
+#define UTF8_LITERAL_EXTRA "s\xc3\xb6m\xc3\xab l\xc3\xaft\xc3\xabr\xc3\xa4l" "EXTRA"
//fix for gcc4.0: if the operator+ does not exist without QT_USE_FAST_OPERATOR_PLUS
@@ -65,7 +69,6 @@ void runScenario()
QLatin1Char achar('c');
QString r2(QLatin1String(LITERAL LITERAL));
QString r;
- QByteArray ba(LITERAL);
r = l1literal Q l1literal;
QCOMPARE(r, r2);
@@ -86,6 +89,15 @@ void runScenario()
QCOMPARE(r, r2);
r = LITERAL P string;
QCOMPARE(r, r2);
+
+ QByteArray ba = QByteArray(LITERAL);
+ r = ba P string;
+ QCOMPARE(r, r2);
+ r = string P ba;
+ QCOMPARE(r, r2);
+
+ static const char badata[] = LITERAL_EXTRA;
+ ba = QByteArray::fromRawData(badata, LITERAL_LEN);
r = ba P string;
QCOMPARE(r, r2);
r = string P ba;
@@ -109,6 +121,18 @@ void runScenario()
QCOMPARE(r, r2);
r = string P ba;
QCOMPARE(r, r2);
+
+ ba = QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN);
+ r = ba P string;
+ QCOMPARE(r, r2);
+ r = string P ba;
+ QCOMPARE(r, r2);
+
+ ba = QByteArray(); // empty
+ r = ba P string;
+ QCOMPARE(r, string);
+ r = string P ba;
+ QCOMPARE(r, string);
#endif
string = QString::fromLatin1(LITERAL);