Fix mime type parsing with Qt 5

The behavior of QStringView::mid is slightly different between Qt 5 and
Qt 6, so use Utils::midView which was created exactly to cover that.

Change-Id: Ia084e93c36fb152ac725f1b8eaa38a07dfb939e1
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Eike Ziller
2022-04-29 16:03:02 +02:00
parent 317452ee8c
commit 15f3b46133
2 changed files with 11 additions and 5 deletions

View File

@@ -40,8 +40,11 @@
#include "mimemagicrule_p.h"
#include "mimetypeparser_p.h"
#include <QtCore/QList>
#include "porting.h"
#include <QtCore/QDebug>
#include <QtCore/QList>
#include <qendian.h>
namespace Utils {
@@ -260,8 +263,11 @@ MimeMagicRule::MimeMagicRule(const QString &type,
// Parse for offset as "1" or "1:10"
const int colonIndex = offsets.indexOf(QLatin1Char(':'));
const QStringView startPosStr = QStringView{offsets}.mid(0, colonIndex); // \ These decay to returning 'offsets'
const QStringView endPosStr = QStringView{offsets}.mid(colonIndex + 1);// / unchanged when colonIndex == -1
const QStringView startPosStr
= Utils::midView(offsets, 0, colonIndex); // \ These decay to returning 'offsets'
const QStringView endPosStr = Utils::midView(offsets,
colonIndex
+ 1); // / unchanged when colonIndex == -1
if (Q_UNLIKELY(!MimeTypeParserBase::parseNumber(startPosStr, &m_startPos, errorString)) ||
Q_UNLIKELY(!MimeTypeParserBase::parseNumber(endPosStr, &m_endPos, errorString))) {
m_type = Invalid;

View File

@@ -65,7 +65,7 @@ inline StringView make_stringview(const QString &s)
}
// QStringView::mid in Qt5 does not do bounds checking, in Qt6 it does
inline QStringView midView(const QString &s, int offset, int length)
inline QStringView midView(const QString &s, int offset, int length = -1)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const int size = s.size();
@@ -77,7 +77,7 @@ inline QStringView midView(const QString &s, int offset, int length)
if (length + offset <= 0)
return {};
return QStringView(s).left(length + offset);
} else if (length > size - offset)
} else if (length < 0 || length > size - offset)
return QStringView(s).mid(offset);
return QStringView(s).mid(offset, length);
#else