From 8d127c57376e48dd49afe62cfb8e714d26759a69 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Mon, 5 Jun 2023 17:11:10 +0300 Subject: [PATCH] QMimeData: de-duplicate some code - Fix integral narrowing conversion issues by using range-for - Use QByteArrayView Change-Id: Ib45c7d6d8ca27c347324b4b259f25282bc84ff3f Reviewed-by: Thiago Macieira Reviewed-by: David Faure --- src/corelib/kernel/qmimedata.cpp | 43 +++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/corelib/kernel/qmimedata.cpp b/src/corelib/kernel/qmimedata.cpp index 9a3ec434c5..8f5be6a266 100644 --- a/src/corelib/kernel/qmimedata.cpp +++ b/src/corelib/kernel/qmimedata.cpp @@ -76,6 +76,24 @@ QVariant QMimeDataPrivate::getData(const QString &format) const return it->data; } +static QList dataToUrls(QByteArrayView text) +{ + QList list; + qsizetype newLineIndex = -1; + qsizetype from = 0; + const char *begin = text.data(); + while ((newLineIndex = text.indexOf('\n', from)) != -1) { + QByteArrayView bav(begin + from, begin + newLineIndex); + bav = bav.trimmed(); + if (!bav.isEmpty()) + list.push_back(QUrl::fromEncoded(bav)); + from = newLineIndex + 1; + if (from >= text.size()) + break; + } + return list; +} + QVariant QMimeDataPrivate::retrieveTypedData(const QString &format, QMetaType type) const { Q_Q(const QMimeData); @@ -146,21 +164,13 @@ QVariant QMimeDataPrivate::retrieveTypedData(const QString &format, QMetaType ty Q_FALLTHROUGH(); } case QMetaType::QUrl: { - QByteArray ba = data.toByteArray(); + auto bav = data.view(); // Qt 3.x will send text/uri-list with a trailing // null-terminator (that is *not* sent for any other // text/* mime-type), so chop it off - if (ba.endsWith('\0')) - ba.chop(1); - - QList urls = ba.split('\n'); - QList list; - for (int i = 0; i < urls.size(); ++i) { - QByteArray ba = urls.at(i).trimmed(); - if (!ba.isEmpty()) - list.append(QUrl::fromEncoded(ba)); - } - return list; + if (bav.endsWith('\0')) + bav.chop(1); + return dataToUrls(bav); } default: break; @@ -557,14 +567,7 @@ void QMimeData::setData(const QString &mimeType, const QByteArray &data) QByteArray ba = data; if (ba.endsWith('\0')) ba.chop(1); - QList urls = ba.split('\n'); - QList list; - for (int i = 0; i < urls.size(); ++i) { - QByteArray ba = urls.at(i).trimmed(); - if (!ba.isEmpty()) - list.append(QUrl::fromEncoded(ba)); - } - d->setData(mimeType, list); + d->setData(mimeType, dataToUrls(ba)); } else { d->setData(mimeType, QVariant(data)); }