From d4960f380e718bbc3df3fe33c68ba8c693394892 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sat, 26 Apr 2025 15:12:06 +0300 Subject: [PATCH] qdataurl: use QLatin1StringView instead of QByteArrayView This is URL data and we use toLatin1() to convert from QString to QByteArray. QL1SV is more suitable here because we need to do case-insensitive look ups into the (see https://www.rfc-editor.org/rfc/rfc2397.html) part of the URL. Drive by, use QL1SV::sliced() instead of mid(), the precondition has already been checked. Change-Id: I670c41fdb6728f6420b1a4e2046357013ea210e0 Reviewed-by: Thiago Macieira --- src/corelib/io/qdataurl.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/corelib/io/qdataurl.cpp b/src/corelib/io/qdataurl.cpp index b946f2bae44..ef468f2ea16 100644 --- a/src/corelib/io/qdataurl.cpp +++ b/src/corelib/io/qdataurl.cpp @@ -26,18 +26,18 @@ Q_CORE_EXPORT bool qDecodeDataUrl(const QUrl &uri, QString &mimeType, QByteArray //QByteArray data = QByteArray::fromPercentEncoding(uri.path(QUrl::FullyEncoded).toLatin1()); const QByteArray dataArray = QByteArray::fromPercentEncoding(uri.url(QUrl::FullyEncoded | QUrl::RemoveScheme).toLatin1()); - QByteArrayView data = dataArray; + auto data = QLatin1StringView{dataArray}; // parse it: - const qsizetype pos = data.indexOf(','); + const qsizetype pos = data.indexOf(u','); if (pos != -1) { - payload = data.mid(pos + 1).toByteArray(); + payload = QByteArray{data.sliced(pos + 1)}; data.truncate(pos); data = data.trimmed(); // find out if the payload is encoded in Base64 constexpr auto base64 = ";base64"_L1; // per the RFC, at the end of `data` - if (QLatin1StringView{data}.endsWith(base64, Qt::CaseInsensitive)) { + if (data.endsWith(base64, Qt::CaseInsensitive)) { auto r = QByteArray::fromBase64Encoding(std::move(payload)); if (!r) { // just in case someone uses `payload` without checking the returned bool @@ -50,16 +50,16 @@ Q_CORE_EXPORT bool qDecodeDataUrl(const QUrl &uri, QString &mimeType, QByteArray QLatin1StringView textPlain; constexpr auto charset = "charset"_L1; - if (QLatin1StringView{data}.startsWith(charset, Qt::CaseInsensitive)) { - QByteArrayView copy = data.sliced(charset.size()); - while (copy.startsWith(' ')) + if (data.startsWith(charset, Qt::CaseInsensitive)) { + QLatin1StringView copy = data.sliced(charset.size()); + while (copy.startsWith(u' ')) copy.slice(1); - if (copy.startsWith('=')) + if (copy.startsWith(u'=')) textPlain = "text/plain;"_L1; } if (!data.isEmpty()) - mimeType = textPlain + QLatin1StringView(data.trimmed()); + mimeType = textPlain + data.trimmed(); else mimeType = QStringLiteral("text/plain;charset=US-ASCII"); return true;