diff --git a/src/corelib/io/qurl_p.h b/src/corelib/io/qurl_p.h index c352399505..9a063a37e6 100644 --- a/src/corelib/io/qurl_p.h +++ b/src/corelib/io/qurl_p.h @@ -68,7 +68,7 @@ enum AceOperation { ToAceOnly, NormalizeAce }; extern QString qt_ACE_do(QStringView domain, AceOperation op, AceLeadingDot dot); extern Q_AUTOTEST_EXPORT bool qt_nameprep(QString *source, int from); extern Q_AUTOTEST_EXPORT bool qt_check_std3rules(QStringView in); -extern Q_AUTOTEST_EXPORT void qt_punycodeEncoder(const QChar *s, int ucLength, QString *output); +extern Q_AUTOTEST_EXPORT void qt_punycodeEncoder(QStringView in, QString *output); extern Q_AUTOTEST_EXPORT QString qt_punycodeDecoder(const QString &pc); extern Q_AUTOTEST_EXPORT QString qt_urlRecodeByteArray(const QByteArray &ba); diff --git a/src/corelib/io/qurlidna.cpp b/src/corelib/io/qurlidna.cpp index b06ab08910..08c5d1759c 100644 --- a/src/corelib/io/qurlidna.cpp +++ b/src/corelib/io/qurlidna.cpp @@ -2209,21 +2209,21 @@ static inline void appendEncode(QString* output, uint& delta, uint& bias, uint& ++h; } -Q_AUTOTEST_EXPORT void qt_punycodeEncoder(const QChar *s, int ucLength, QString *output) +Q_AUTOTEST_EXPORT void qt_punycodeEncoder(QStringView in, QString *output) { uint n = initial_n; uint delta = 0; uint bias = initial_bias; int outLen = output->length(); - output->resize(outLen + ucLength); + output->resize(outLen + in.length()); QChar *d = output->data() + outLen; bool skipped = false; // copy all basic code points verbatim to output. - for (uint j = 0; j < (uint) ucLength; ++j) { - if (s[j].unicode() < 0x80) - *d++ = s[j]; + for (QChar c : in) { + if (c.unicode() < 0x80) + *d++ = c; else skipped = true; } @@ -2246,14 +2246,13 @@ Q_AUTOTEST_EXPORT void qt_punycodeEncoder(const QChar *s, int ucLength, QString // while there are still unprocessed non-basic code points left in // the input string... - while (h < (uint) ucLength) { + while (h < (uint) in.length()) { // find the character in the input string with the lowest // unicode value. uint m = Q_MAXINT; - uint j; - for (j = 0; j < (uint) ucLength; ++j) { - if (s[j].unicode() >= n && s[j].unicode() < m) - m = (uint) s[j].unicode(); + for (QChar c : in) { + if (c.unicode() >= n && c.unicode() < m) + m = (uint) c.unicode(); } // reject out-of-bounds unicode characters @@ -2265,12 +2264,11 @@ Q_AUTOTEST_EXPORT void qt_punycodeEncoder(const QChar *s, int ucLength, QString delta += (m - n) * (h + 1); n = m; - // for each code point in the input string - for (j = 0; j < (uint) ucLength; ++j) { + for (QChar c : in) { // increase delta until we reach the character with the // lowest unicode code. fail if delta overflows. - if (s[j].unicode() < n) { + if (c.unicode() < n) { ++delta; if (!delta) { output->truncate(outLen); @@ -2280,7 +2278,7 @@ Q_AUTOTEST_EXPORT void qt_punycodeEncoder(const QChar *s, int ucLength, QString // if j is the index of the character with the lowest // unicode code... - if (s[j].unicode() == n) { + if (c.unicode() == n) { appendEncode(output, delta, bias, b, h); } } @@ -2561,7 +2559,7 @@ QString qt_ACE_do(QStringView domain, AceOperation op, AceLeadingDot dot) aceForm.resize(0); if (toReserve > aceForm.capacity()) aceForm.reserve(toReserve); - qt_punycodeEncoder(result.constData() + prevLen, result.size() - prevLen, &aceForm); + qt_punycodeEncoder(QStringView{result}.mid(prevLen), &aceForm); // We use resize()+memcpy() here because we're overwriting the data we've copied bool appended = false; diff --git a/tests/auto/corelib/io/qurlinternal/tst_qurlinternal.cpp b/tests/auto/corelib/io/qurlinternal/tst_qurlinternal.cpp index edee5e5723..452f9417e1 100644 --- a/tests/auto/corelib/io/qurlinternal/tst_qurlinternal.cpp +++ b/tests/auto/corelib/io/qurlinternal/tst_qurlinternal.cpp @@ -244,7 +244,7 @@ void tst_QUrlInternal::idna_testsuite() QFETCH(QByteArray, punycode); QString result; - qt_punycodeEncoder((QChar*)unicode.points, numchars, &result); + qt_punycodeEncoder(QStringView{unicode.points, numchars}, &result); QCOMPARE(result.toLatin1(), punycode); QCOMPARE(qt_punycodeDecoder(result), QString::fromUtf16(unicode.points, numchars)); }