diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2020-04-29 11:36:19 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2020-05-09 06:26:08 +0000 |
commit | 928d57d8da5e89d2dd7fb3be8c992422538f47d0 (patch) | |
tree | 0915a02f6788300fd2d4d41bf1433ce7836021f8 /tests/auto | |
parent | 19e7c0d2b5a807e194be1d65b16041f48136c9be (diff) |
QChar: add fromUcs{2,4}()
The fromUcs2() named ctor is designed to replace all the non-char
integral-type constructors of QChar which make it very hard to control
the implicit QChar conversions, which have caused a few bugs in Qt
itself. As a classical named contructor, it simply returns QChar.
The fromUcs4() named "ctor", however, needs to expand surrogate pairs,
and thus can't just return QChar. Instead, it returns a small struct
that contains one or two char16_t's, can be iterated over and be
implicitly converted to QStringView. To avoid bikeshedding the name
(FromUcs4Result, of course :), it's defined inline and thus can't be
named outside the function. This function replaces most uses of
QChar::requiresSurrogates() in QtBase.
[ChangeLog][QtCore][QChar] Added fromUcs2(), fromUcs4().
Change-Id: I803708c14001040f75cb599e33c24a3fb8d2579c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/text/qchar/tst_qchar.cpp | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/tests/auto/corelib/text/qchar/tst_qchar.cpp b/tests/auto/corelib/text/qchar/tst_qchar.cpp index cf4f6d21e2..83a6765f2e 100644 --- a/tests/auto/corelib/text/qchar/tst_qchar.cpp +++ b/tests/auto/corelib/text/qchar/tst_qchar.cpp @@ -37,6 +37,8 @@ class tst_QChar : public QObject Q_OBJECT private slots: void fromChar16_t(); + void fromUcs4_data(); + void fromUcs4(); void fromWchar_t(); void operator_eqeq_null(); void operators_data(); @@ -89,6 +91,34 @@ void tst_QChar::fromChar16_t() #endif } +void tst_QChar::fromUcs4_data() +{ + QTest::addColumn<uint>("ucs4"); + auto row = [](uint ucs4) { + QTest::addRow("0x%08X", ucs4) << ucs4; + }; + + row(0x2f868); + row(0x1D157); + row(0x1D157); +} + +void tst_QChar::fromUcs4() +{ + QFETCH(const uint, ucs4); + + const auto result = QChar::fromUcs4(ucs4); + if (QChar::requiresSurrogates(ucs4)) { + QCOMPARE(result.chars[0], QChar::highSurrogate(ucs4)); + QCOMPARE(result.chars[1], QChar::lowSurrogate(ucs4)); + QCOMPARE(QStringView{result}.size(), 2); + } else { + QCOMPARE(result.chars[0], ucs4); + QCOMPARE(result.chars[1], 0u); + QCOMPARE(QStringView{result}.size(), 1); + } +} + void tst_QChar::fromWchar_t() { #if defined(Q_OS_WIN) @@ -835,13 +865,7 @@ void tst_QChar::normalization_data() for (int j = 0; j < c.size(); ++j) { bool ok; uint uc = c.at(j).toInt(&ok, 16); - if (!QChar::requiresSurrogates(uc)) { - columns[i].append(QChar(uc)); - } else { - // convert to utf16 - columns[i].append(QChar(QChar::highSurrogate(uc))); - columns[i].append(QChar(QChar::lowSurrogate(uc))); - } + columns[i].append(QChar::fromUcs4(uc)); } } |