Fixed QString::operator<(QLatin1String)

QLatin1String now has a constructor that takes explicit length, which makes it
possible to create QLatin1String that do not have null-termination character.
Fixed QString::operator> and < to be safe in that case.

In the same patch fixed qtjson which had operator< implemented in the same way.

QString::compare(QLatin1String) is still broken and will be fixed separately.

Change-Id: I48ec1183a6f44034129cc17312af854795085408
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Denis Dzyubenko 2012-01-26 11:13:41 +01:00 committed by Qt by Nokia
parent 68e5fd9ebc
commit e62fe9ed4f
4 changed files with 33 additions and 4 deletions

View File

@ -469,7 +469,7 @@ inline bool String::operator<(const Latin1String &str) const
++uc;
++c;
}
return (uc == (d->utf16 + d->length) ? *c : (ushort)*uc < *c);
return (uc == e ? (int)d->length < (int)str.d->length : (ushort)*uc < *c);
}

View File

@ -2232,7 +2232,7 @@ bool QString::operator<(const QLatin1String &other) const
++uc;
++c;
}
return (uc == (d->data() + d->size) ? *c : *uc < *c);
return (uc == e ? d->size < other.size() : *uc < *c);
}
/*! \fn bool QString::operator<(const QByteArray &other) const
@ -2325,7 +2325,7 @@ bool QString::operator>(const QLatin1String &other) const
if (!c || *c == '\0')
return !isEmpty();
const ushort *uc = d->data();;
const ushort *uc = d->data();
const ushort *e = uc + qMin(d->size, other.size());
while (uc < e) {
@ -2334,7 +2334,7 @@ bool QString::operator>(const QLatin1String &other) const
++uc;
++c;
}
return (uc == (d->data() + d->size) ? false : *uc > *c);
return (uc == e) ? d->size > other.size() : *uc > *c;
}
/*! \fn bool QString::operator>(const QByteArray &other) const

View File

@ -61,6 +61,7 @@ private Q_SLOTS:
void testNumbers();
void testObjectSimple();
void testObjectSmallKeys();
void testArraySimple();
void testValueObject();
void testValueArray();
@ -277,6 +278,22 @@ void TestQtJson::testObjectSimple()
QVERIFY2(object.value("string").toString() != before, "value should have been updated");
}
void TestQtJson::testObjectSmallKeys()
{
QJsonObject data1;
data1.insert(QStringLiteral("1"), 123);
QVERIFY(data1.contains(QStringLiteral("1")));
QCOMPARE(data1.value(QStringLiteral("1")).toDouble(), (double)123);
data1.insert(QStringLiteral("12"), 133);
QCOMPARE(data1.value(QStringLiteral("12")).toDouble(), (double)133);
QVERIFY(data1.contains(QStringLiteral("12")));
data1.insert(QStringLiteral("123"), 323);
QCOMPARE(data1.value(QStringLiteral("12")).toDouble(), (double)133);
QVERIFY(data1.contains(QStringLiteral("123")));
QCOMPARE(data1.value(QStringLiteral("123")).type(), QJsonValue::Double);
QCOMPARE(data1.value(QStringLiteral("123")).toDouble(), (double)323);
}
void TestQtJson::testArraySimple()
{
QJsonArray array;

View File

@ -220,6 +220,8 @@ private slots:
void reserve();
void toHtmlEscaped_data();
void toHtmlEscaped();
void operatorGreaterWithQLatin1String();
};
typedef QList<int> IntList;
@ -5175,6 +5177,16 @@ void tst_QString::toHtmlEscaped()
QCOMPARE(original.toHtmlEscaped(), expected);
}
void tst_QString::operatorGreaterWithQLatin1String()
{
QLatin1String latin1foo("fooZZ", 3);
QString stringfoo = QString::fromLatin1("foo");
QVERIFY(stringfoo >= latin1foo);
QVERIFY(!(stringfoo > latin1foo));
QVERIFY(stringfoo <= latin1foo);
QVERIFY(!(stringfoo < latin1foo));
}
QTEST_APPLESS_MAIN(tst_QString)
#include "tst_qstring.moc"