From f325bdacb6d236f419f9a8f76f60cea2b58f5fb5 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sun, 29 Jan 2023 11:12:12 +0200 Subject: [PATCH] Replace ushort*/uint* with char16_t*/char32_t* in private API [1] Task-number: QTBUG-110403 Pick-to: 6.5 Change-Id: Ie20a831f22212d56659cf3c6940d17134ab5f2c5 Reviewed-by: Thiago Macieira --- src/corelib/io/qdebug.cpp | 16 +++++++--------- src/corelib/io/qdebug.h | 2 +- src/corelib/io/qdir.cpp | 8 ++++---- src/corelib/io/qipaddress.cpp | 6 +++--- src/corelib/io/qresource.cpp | 2 +- src/corelib/io/qurlidna.cpp | 4 ++-- src/corelib/kernel/qtranslator.cpp | 2 +- src/corelib/serialization/qxmlstream.cpp | 2 +- src/corelib/text/qchar.cpp | 2 +- src/widgets/styles/qstylesheetstyle.cpp | 4 ++-- .../tools/collections/tst_collections.cpp | 2 +- .../benchmarks/corelib/tools/qhash/outofline.cpp | 2 +- 12 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp index 16bb1402283..22f2c0ce8b1 100644 --- a/src/corelib/io/qdebug.cpp +++ b/src/corelib/io/qdebug.cpp @@ -195,10 +195,8 @@ void QDebug::putUcs4(uint ucs4) // These two functions return true if the character should be printed by QDebug. // For QByteArray, this is technically identical to US-ASCII isprint(); // for QString, we use QChar::isPrint, which requires a full UCS-4 decode. -static inline bool isPrintable(uint ucs4) -{ return QChar::isPrint(ucs4); } -static inline bool isPrintable(ushort uc) -{ return QChar::isPrint(uc); } +static inline bool isPrintable(char32_t ucs4) { return QChar::isPrint(ucs4); } +static inline bool isPrintable(char16_t uc) { return QChar::isPrint(uc); } static inline bool isPrintable(uchar c) { return c >= ' ' && c < 0x7f; } @@ -240,7 +238,7 @@ static inline void putEscapedString(QTextStreamPrivate *d, const Char *begin, si // print as an escape sequence (maybe, see below for surrogate pairs) qsizetype buflen = 2; - ushort buf[sizeof "\\U12345678" - 1]; + char16_t buf[std::char_traits::length("\\U12345678")]; buf[0] = '\\'; switch (*p) { @@ -276,7 +274,7 @@ static inline void putEscapedString(QTextStreamPrivate *d, const Char *begin, si if (QChar::isHighSurrogate(*p)) { if ((p + 1) != end && QChar::isLowSurrogate(p[1])) { // properly-paired surrogates - uint ucs4 = QChar::surrogateToUcs4(*p, p[1]); + char32_t ucs4 = QChar::surrogateToUcs4(*p, p[1]); if (isPrintable(ucs4)) { buf[0] = *p; buf[1] = p[1]; @@ -299,8 +297,8 @@ static inline void putEscapedString(QTextStreamPrivate *d, const Char *begin, si // improperly-paired surrogates, fall through } buf[1] = 'u'; - buf[2] = toHexUpper(ushort(*p) >> 12); - buf[3] = toHexUpper(ushort(*p) >> 8); + buf[2] = toHexUpper(char16_t(*p) >> 12); + buf[3] = toHexUpper(char16_t(*p) >> 8); buf[4] = toHexUpper(*p >> 4); buf[5] = toHexUpper(*p); buflen = 6; @@ -325,7 +323,7 @@ void QDebug::putString(const QChar *begin, size_t length) // we'll reset the QTextStream formatting mechanisms, so save the state QDebugStateSaver saver(*this); stream->ts.d_ptr->params.reset(); - putEscapedString(stream->ts.d_ptr.data(), reinterpret_cast(begin), length); + putEscapedString(stream->ts.d_ptr.data(), reinterpret_cast(begin), length); } } diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index d8db9a81620..a55a6ad100d 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -100,7 +100,7 @@ public: inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(signed short t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(unsigned short t) { stream->ts << t; return maybeSpace(); } - inline QDebug &operator<<(char16_t t) { return *this << QChar(ushort(t)); } + inline QDebug &operator<<(char16_t t) { return *this << QChar(t); } inline QDebug &operator<<(char32_t t) { putUcs4(t); return maybeSpace(); } inline QDebug &operator<<(signed int t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(unsigned int t) { stream->ts << t; return maybeSpace(); } diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index 2591670fb0e..3ad804b9e88 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -2217,8 +2217,8 @@ QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormaliza QVarLengthArray outVector(len); qsizetype used = len; char16_t *out = outVector.data(); - const ushort *p = reinterpret_cast(name.data()); - const ushort *prefix = p; + const char16_t *p = reinterpret_cast(name.data()); + const char16_t *prefix = p; qsizetype up = 0; const qsizetype prefixLength = rootLength(name, allowUncPaths); @@ -2232,10 +2232,10 @@ QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormaliza --i; } - auto isDot = [](const ushort *p, qsizetype i) { + auto isDot = [](const char16_t *p, qsizetype i) { return i > 1 && p[i - 1] == '.' && p[i - 2] == '/'; }; - auto isDotDot = [](const ushort *p, qsizetype i) { + auto isDotDot = [](const char16_t *p, qsizetype i) { return i > 2 && p[i - 1] == '.' && p[i - 2] == '.' && p[i - 3] == '/'; }; diff --git a/src/corelib/io/qipaddress.cpp b/src/corelib/io/qipaddress.cpp index 444a38a319d..9956f03738a 100644 --- a/src/corelib/io/qipaddress.cpp +++ b/src/corelib/io/qipaddress.cpp @@ -22,9 +22,9 @@ static QString number(quint8 val) typedef QVarLengthArray Buffer; static const QChar *checkedToAscii(Buffer &buffer, const QChar *begin, const QChar *end) { - const ushort *const ubegin = reinterpret_cast(begin); - const ushort *const uend = reinterpret_cast(end); - const ushort *src = ubegin; + const auto *const ubegin = reinterpret_cast(begin); + const auto *const uend = reinterpret_cast(end); + auto *src = ubegin; buffer.resize(uend - ubegin + 1); char *dst = buffer.data(); diff --git a/src/corelib/io/qresource.cpp b/src/corelib/io/qresource.cpp index a0759bd1b5c..a3237f25bc0 100644 --- a/src/corelib/io/qresource.cpp +++ b/src/corelib/io/qresource.cpp @@ -753,7 +753,7 @@ inline QString QResourceRoot::name(int node) const ret.resize(name_length); QChar *strData = ret.data(); - qFromBigEndian(names + name_offset, name_length, strData); + qFromBigEndian(names + name_offset, name_length, strData); return ret; } diff --git a/src/corelib/io/qurlidna.cpp b/src/corelib/io/qurlidna.cpp index 5fc6006ef4d..620034b5265 100644 --- a/src/corelib/io/qurlidna.cpp +++ b/src/corelib/io/qurlidna.cpp @@ -320,8 +320,8 @@ Q_CONSTINIT static QStringList *user_idn_whitelist = nullptr; static bool lessThan(const QChar *a, int l, const char *c) { - const ushort *uc = (const ushort *)a; - const ushort *e = uc + l; + const auto *uc = reinterpret_cast(a); + const char16_t *e = uc + l; if (!c || *c == 0) return false; diff --git a/src/corelib/kernel/qtranslator.cpp b/src/corelib/kernel/qtranslator.cpp index 521503b96bc..9f9575f5177 100644 --- a/src/corelib/kernel/qtranslator.cpp +++ b/src/corelib/kernel/qtranslator.cpp @@ -914,7 +914,7 @@ end: if (!tn) return QString(); QString str(tn_length / 2, Qt::Uninitialized); - qFromBigEndian(tn, str.size(), str.data()); + qFromBigEndian(tn, str.size(), str.data()); return str; } diff --git a/src/corelib/serialization/qxmlstream.cpp b/src/corelib/serialization/qxmlstream.cpp index e03b1a2c673..c6d085714fb 100644 --- a/src/corelib/serialization/qxmlstream.cpp +++ b/src/corelib/serialization/qxmlstream.cpp @@ -1721,7 +1721,7 @@ void QXmlStreamReaderPrivate::checkPublicLiteral(QStringView publicId) { //#x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%] - const ushort *data = reinterpret_cast(publicId.constData()); + const char16_t *data = publicId.utf16(); uchar c = 0; qsizetype i; for (i = publicId.size() - 1; i >= 0; --i) { diff --git a/src/corelib/text/qchar.cpp b/src/corelib/text/qchar.cpp index 272de292c5e..4175b6c8494 100644 --- a/src/corelib/text/qchar.cpp +++ b/src/corelib/text/qchar.cpp @@ -2060,7 +2060,7 @@ static bool normalizationQuickCheckHelper(QString *str, QString::NormalizationFo enum { NFQC_YES = 0, NFQC_NO = 1, NFQC_MAYBE = 3 }; - const ushort *string = reinterpret_cast(str->constData()); + const auto *string = reinterpret_cast(str->constData()); qsizetype length = str->size(); // this avoids one out of bounds check in the loop diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp index 00adefdf4f1..70217127c35 100644 --- a/src/widgets/styles/qstylesheetstyle.cpp +++ b/src/widgets/styles/qstylesheetstyle.cpp @@ -1603,8 +1603,8 @@ public: return nodeName == "QToolTip"_L1; #endif do { - const ushort *uc = (const ushort *)nodeName.constData(); - const ushort *e = uc + nodeName.size(); + const auto *uc = reinterpret_cast(nodeName.constData()); + const auto *e = uc + nodeName.size(); const uchar *c = (const uchar *)metaObject->className(); while (*c && uc != e && (*uc == *c || (*c == ':' && *uc == '-'))) { ++uc; diff --git a/tests/auto/corelib/tools/collections/tst_collections.cpp b/tests/auto/corelib/tools/collections/tst_collections.cpp index 2750b298548..af7a7ec0dfa 100644 --- a/tests/auto/corelib/tools/collections/tst_collections.cpp +++ b/tests/auto/corelib/tools/collections/tst_collections.cpp @@ -2018,7 +2018,7 @@ void tst_Collections::qstring() s = "ascii"; s += QChar((uchar) 0xb0); QVERIFY(s.toUtf8() != s.toLatin1()); - QCOMPARE(s[s.size()-1].unicode(), (ushort)0xb0); + QCOMPARE(s[s.size()-1].unicode(), char16_t(0xb0)); QCOMPARE(s.left(s.size()-1), QLatin1String("ascii")); QVERIFY(s == QString::fromUtf8(s.toUtf8().constData())); diff --git a/tests/benchmarks/corelib/tools/qhash/outofline.cpp b/tests/benchmarks/corelib/tools/qhash/outofline.cpp index 6ad1a4c52a9..aa861d2e42a 100644 --- a/tests/benchmarks/corelib/tools/qhash/outofline.cpp +++ b/tests/benchmarks/corelib/tools/qhash/outofline.cpp @@ -42,7 +42,7 @@ size_t qHash(const Qt50String &key, size_t seed) // (for instance, gcc 4.4 does that even at -O0). size_t qHash(const JavaString &str) { - const unsigned short *p = (unsigned short *)str.constData(); + const auto *p = reinterpret_cast(str.constData()); const qsizetype len = str.size(); uint h = 0;