From a2ddd96ac8b7657c2ef64f2a8f51db5cd8a8d96a Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Wed, 18 Dec 2019 20:23:11 +0100 Subject: Introduce QString(View)::isValidUtf16 QString(View)s can be built or manipulated in ways that make them contain/refer to improperly encoded UTF-16 data. Problem is, we don't have public APIs to check whether a string contains valid UTF-16. This knowledge is precious if the string is to be fed in algorithms, regular expressions, etc. that expect validated input (e.g. QRegularExpression can be faster if it can assume valid UTF-16, otherwise it has to employ extra checks). Add a function that does the validation. [ChangeLog][QtCore][QStringView] Added QStringView::isValidUtf16. [ChangeLog][QtCore][QString] Added QString::isValidUtf16. Change-Id: Idd699183f6ec08013046c76c6a5a7c524b6c6fbc Reviewed-by: Thiago Macieira --- tests/auto/corelib/text/qstring/tst_qstring.cpp | 48 +++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'tests/auto/corelib/text') diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 2108e99f20..f86fbc5988 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -596,6 +596,8 @@ private slots: void assignQChar(); void isRightToLeft_data(); void isRightToLeft(); + void isValidUtf16_data(); + void isValidUtf16(); void unicodeStrings(); }; @@ -7025,6 +7027,52 @@ void tst_QString::isRightToLeft() QCOMPARE(unicode.isRightToLeft(), rtl); } +void tst_QString::isValidUtf16_data() +{ + QTest::addColumn("string"); + QTest::addColumn("valid"); + + int row = 0; + QTest::addRow("valid-%02d", row++) << QString() << true; + QTest::addRow("valid-%02d", row++) << QString("") << true; + QTest::addRow("valid-%02d", row++) << QString("abc def") << true; + QTest::addRow("valid-%02d", row++) << QString("Γ bΓ§") << true; + QTest::addRow("valid-%02d", row++) << QString("ßẞ") << true; + QTest::addRow("valid-%02d", row++) << QString("𝐀𝐁𝐂abc𝐃𝐄𝐅def") << true; + QTest::addRow("valid-%02d", row++) << QString("abc𝐀𝐁𝐂def𝐃𝐄𝐅") << true; + QTest::addRow("valid-%02d", row++) << (QString("abc") + QChar(0x0000) + QString("def")) << true; + QTest::addRow("valid-%02d", row++) << (QString("abc") + QChar(0xFFFF) + QString("def")) << true; + // check that BOM presence doesn't make any difference + QTest::addRow("valid-%02d", row++) << (QString() + QChar(0xFEFF) + QString("abc𝐀𝐁𝐂def𝐃𝐄𝐅")) << true; + QTest::addRow("valid-%02d", row++) << (QString() + QChar(0xFFFE) + QString("abc𝐀𝐁𝐂def𝐃𝐄𝐅")) << true; + + row = 0; + QTest::addRow("stray-high-%02d", row++) << (QString() + QChar(0xD800)) << false; + QTest::addRow("stray-high-%02d", row++) << (QString() + QString("abc") + QChar(0xD800)) << false; + QTest::addRow("stray-high-%02d", row++) << (QString() + QChar(0xD800) + QString("def")) << false; + QTest::addRow("stray-high-%02d", row++) << (QString() + QString("abc") + QChar(0xD800) + QString("def")) << false; + QTest::addRow("stray-high-%02d", row++) << (QString() + QChar(0xD800) + QChar(0xD800)) << false; + QTest::addRow("stray-high-%02d", row++) << (QString() + QString("abc") + QChar(0xD800) + QChar(0xD800)) << false; + QTest::addRow("stray-high-%02d", row++) << (QString() + QChar(0xD800) + QChar(0xD800) + QString("def")) << false; + QTest::addRow("stray-high-%02d", row++) << (QString() + QString("abc") + QChar(0xD800) + QChar(0xD800) + QString("def")) << false; + + row = 0; + QTest::addRow("stray-low-%02d", row++) << (QString() + QChar(0xDC00)) << false; + QTest::addRow("stray-low-%02d", row++) << (QString() + QString("abc") + QChar(0xDC00)) << false; + QTest::addRow("stray-low-%02d", row++) << (QString() + QChar(0xDC00) + QString("def")) << false; + QTest::addRow("stray-low-%02d", row++) << (QString() + QString("abc") + QChar(0xDC00) + QString("def")) << false; + QTest::addRow("stray-low-%02d", row++) << (QString() + QChar(0xDC00) + QChar(0xDC00)) << false; + QTest::addRow("stray-low-%02d", row++) << (QString() + QString("abc") + QChar(0xDC00) + QChar(0xDC00)) << false; + QTest::addRow("stray-low-%02d", row++) << (QString() + QChar(0xDC00) + QChar(0xDC00) + QString("def")) << false; + QTest::addRow("stray-low-%02d", row++) << (QString() + QString("abc") + QChar(0xDC00) + QChar(0xDC00) + QString("def")) << false; +} + +void tst_QString::isValidUtf16() +{ + QFETCH(QString, string); + QTEST(string.isValidUtf16(), "valid"); +} + QTEST_APPLESS_MAIN(tst_QString) #include "tst_qstring.moc" -- cgit v1.2.3 From 27d139128013c969a939779536485c1a80be977e Mon Sep 17 00:00:00 2001 From: Tuomas Heimonen Date: Fri, 29 Nov 2019 09:27:35 +0200 Subject: QLocale: Support Indian number formatting When QLocale::Country is set to QLocale::India numbers are written so that after first three from the right and then after every second will be comma. E.g. 10000000 is written as 1,00,00,000 Task-number: QTBUG-24301 Change-Id: Ic06241c127b0af1824104f94f7e2ce6e2058a070 Reviewed-by: Venugopal Shivashankar Reviewed-by: Teemu Holappa --- tests/auto/corelib/text/qlocale/tst_qlocale.cpp | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'tests/auto/corelib/text') diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index 20ed7500b5..8f434acb29 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -147,6 +147,8 @@ private slots: void systemLocale_data(); void systemLocale(); + void IndianNumberGrouping(); + // *** ORDER-DEPENDENCY *** (This Is Bad.) // Test order is determined by order of declaration here: *all* tests that // QLocale::setDefault() *must* appear *after* all other tests ! @@ -2991,5 +2993,56 @@ void tst_QLocale::systemLocale() QCOMPARE(QLocale::system(), originalSystemLocale); } +void tst_QLocale::IndianNumberGrouping() +{ + QLocale indian(QLocale::Hindi, QLocale::India); + + qint8 int8 = 100; + QString strResult8("100"); + QCOMPARE(indian.toString(int8), strResult8); + QCOMPARE(indian.toShort(strResult8), short(int8)); + + quint8 uint8 = 100; + QCOMPARE(indian.toString(uint8), strResult8); + QCOMPARE(indian.toShort(strResult8), short(uint8)); + + // Boundary case 1000 for short and ushort + short shortInt = 1000; + QString strResult16("1,000"); + QCOMPARE(indian.toString(shortInt), strResult16); + QCOMPARE(indian.toShort(strResult16), shortInt); + + ushort uShortInt = 1000; + QCOMPARE(indian.toString(uShortInt), strResult16); + QCOMPARE(indian.toUShort(strResult16), uShortInt); + + shortInt = 10000; + strResult16 = "10,000"; + QCOMPARE(indian.toString(shortInt), strResult16); + QCOMPARE(indian.toShort(strResult16), shortInt); + + uShortInt = 10000; + QCOMPARE(indian.toString(uShortInt), strResult16); + QCOMPARE(indian.toUShort(strResult16), uShortInt); + + int intInt = 1000000000; + QString strResult32("1,00,00,00,000"); + QCOMPARE(indian.toString(intInt), strResult32); + QCOMPARE(indian.toInt(strResult32), intInt); + + uint uIntInt = 1000000000; + QCOMPARE(indian.toString(uIntInt), strResult32); + QCOMPARE(indian.toUInt(strResult32), uIntInt); + + QString strResult64("10,00,00,00,00,00,00,00,000"); + qint64 int64 = Q_INT64_C(1000000000000000000); + QCOMPARE(indian.toString(int64), strResult64); + QCOMPARE(indian.toLongLong(strResult64), int64); + + quint64 uint64 = Q_UINT64_C(1000000000000000000); + QCOMPARE(indian.toString(uint64), strResult64); + QCOMPARE(indian.toULongLong(strResult64), uint64); +} + QTEST_MAIN(tst_QLocale) #include "tst_qlocale.moc" -- cgit v1.2.3