From 4b1ba7c7928b4b326eb83e6df6f0b8fb93f2edf0 Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Wed, 1 Feb 2012 21:08:30 +0100 Subject: [PATCH] Fix QString::operator=(QLatin1String) for substrings QLatin1String now has a constructor that takes explicit length, which makes it possible to create a QLatin1String that isn't null-terminated. Made QString::operator=(QLatin1String) work in that case. Change-Id: Ie77eabd2f8f036531d67cd8051a7b6305b386ccf Reviewed-by: Lars Knoll --- src/corelib/tools/qstring.h | 2 +- .../corelib/tools/qstring/tst_qstring.cpp | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 285369a736..f1bad5c028 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -711,7 +711,7 @@ inline bool QString::isDetached() const { return d->ref == 1; } inline QString &QString::operator=(const QLatin1String &s) { - *this = fromLatin1(s.latin1()); + *this = fromLatin1(s.latin1(), s.size()); return *this; } inline void QString::clear() diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp index 781377f574..dae1dc45ba 100644 --- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp @@ -224,6 +224,7 @@ private slots: void operatorGreaterWithQLatin1String(); void compareQLatin1Strings(); void fromQLatin1StringWithLength(); + void assignQLatin1String(); }; typedef QList IntList; @@ -5286,6 +5287,28 @@ void tst_QString::fromQLatin1StringWithLength() QCOMPARE(foo, QString::fromLatin1("foo")); } +void tst_QString::assignQLatin1String() +{ + QString empty = QLatin1String(""); + QVERIFY(empty.isEmpty()); + QVERIFY(!empty.isNull()); + + QString null = QLatin1String(0); + QVERIFY(null.isEmpty()); + QVERIFY(null.isNull()); + + QLatin1String latin1foo("foo"); + QString foo = latin1foo; + QCOMPARE(foo.size(), latin1foo.size()); + QCOMPARE(foo, QString::fromLatin1("foo")); + + QLatin1String latin1subfoo("foobar", 3); + foo = latin1subfoo; + QCOMPARE(foo.size(), latin1subfoo.size()); + QCOMPARE(foo, QString::fromLatin1("foo")); + +} + QTEST_APPLESS_MAIN(tst_QString) #include "tst_qstring.moc"