Switch to string view where applicable

PR #22438.
This commit is contained in:
Chocobo1 2025-03-17 19:28:38 +08:00 committed by GitHub
parent 5a4b3b25d3
commit 8d0870c953
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 24 additions and 27 deletions

View file

@ -204,7 +204,7 @@ bool RequestParser::parseRequestLine(const QString &line)
m_request.method = match.captured(1);
// Request Target
const QByteArray url {match.captured(2).toLatin1()};
const QByteArray url {match.capturedView(2).toLatin1()};
const int sepPos = url.indexOf('?');
const QByteArrayView pathComponent = ((sepPos == -1) ? url : QByteArrayView(url).first(sepPos));

View file

@ -96,9 +96,9 @@ void DNSUpdater::ipRequestFinished(const DownloadResult &result)
const QRegularExpressionMatch ipRegexMatch = QRegularExpression(u"Current IP Address:\\s+([^<]+)</body>"_s).match(QString::fromUtf8(result.data));
if (ipRegexMatch.hasMatch())
{
QString ipStr = ipRegexMatch.captured(1);
const QString ipStr = ipRegexMatch.captured(1);
qDebug() << Q_FUNC_INFO << "Regular expression captured the following IP:" << ipStr;
QHostAddress newIp(ipStr);
const QHostAddress newIp {ipStr};
if (!newIp.isNull())
{
if (m_lastIP != newIp)

View file

@ -184,14 +184,10 @@ QString computeEpisodeName(const QString &article)
for (int i = 1; i <= match.lastCapturedIndex(); ++i)
{
const QString cap = match.captured(i);
if (cap.isEmpty())
continue;
bool isInt = false;
const int x = cap.toInt(&isInt);
ret.append(isInt ? QString::number(x) : cap);
ret.append(cap);
}
return ret.join(u'x');
}
@ -293,11 +289,11 @@ bool AutoDownloadRule::matchesEpisodeFilterExpression(const QString &articleTitl
if (!matcher.hasMatch())
return false;
const QString season {matcher.captured(1)};
const QStringList episodes {matcher.captured(2).split(u';')};
const QStringView season {matcher.capturedView(1)};
const QList<QStringView> episodes {matcher.capturedView(2).split(u';')};
const int seasonOurs {season.toInt()};
for (QString episode : episodes)
for (QStringView episode : episodes)
{
if (episode.isEmpty())
continue;
@ -308,11 +304,11 @@ bool AutoDownloadRule::matchesEpisodeFilterExpression(const QString &articleTitl
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
episode.slice(1);
#else
episode.remove(0, 1);
episode = episode.sliced(1);
#endif
}
if (episode.indexOf(u'-') != -1)
if (episode.contains(u'-'))
{ // Range detected
const QString partialPattern1 {u"\\bs0?(\\d{1,4})[ -_\\.]?e(0?\\d{1,4})(?:\\D|\\b)"_s};
const QString partialPattern2 {u"\\b(\\d{1,4})x(0?\\d{1,4})(?:\\D|\\b)"_s};
@ -329,8 +325,8 @@ bool AutoDownloadRule::matchesEpisodeFilterExpression(const QString &articleTitl
if (matched)
{
const int seasonTheirs {matcher.captured(1).toInt()};
const int episodeTheirs {matcher.captured(2).toInt()};
const int seasonTheirs {matcher.capturedView(1).toInt()};
const int episodeTheirs {matcher.capturedView(2).toInt()};
if (episode.endsWith(u'-'))
{ // Infinite range
@ -340,13 +336,14 @@ bool AutoDownloadRule::matchesEpisodeFilterExpression(const QString &articleTitl
}
else
{ // Normal range
const QStringList range {episode.split(u'-')};
const QList<QStringView> range {episode.split(u'-')};
Q_ASSERT(range.size() == 2);
if (range.first().toInt() > range.last().toInt())
continue; // Ignore this subrule completely
const int episodeOursFirst {range.first().toInt()};
const int episodeOursLast {range.last().toInt()};
if (episodeOursFirst > episodeOursLast)
continue; // Ignore this subrule completely
if ((seasonTheirs == seasonOurs) && ((episodeOursFirst <= episodeTheirs) && (episodeOursLast >= episodeTheirs)))
return true;
}

View file

@ -69,8 +69,8 @@ namespace
while (iter.hasNext())
{
const QRegularExpressionMatch match = iter.next();
const QString scheme = match.captured(4);
const QString host = match.captured(5);
const QStringView scheme = match.capturedView(4);
const QStringView host = match.capturedView(5);
if (!scheme.isEmpty())
{
if (host.isEmpty())
@ -80,21 +80,21 @@ namespace
continue;
}
QString relativePath = match.captured(6);
QStringView relativePath = match.capturedView(6);
if (relativePath.startsWith(u'/'))
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
relativePath.slice(1);
#else
relativePath.remove(0, 1);
relativePath = relativePath.sliced(1);
#endif
}
const QString absoluteUrl = !host.isEmpty()
? QString(defaultScheme + u':' + host) : (normalizedBaseUrl + relativePath);
const QString fullMatch = match.captured(0);
const QString prefix = match.captured(1);
const QString suffix = match.captured(7);
const QStringView prefix = match.capturedView(1);
const QStringView suffix = match.capturedView(7);
html.replace(fullMatch, (prefix + absoluteUrl + suffix));
}

View file

@ -237,15 +237,15 @@ void WebApplication::translateDocument(QString &data) const
i = data.indexOf(regex, i, &regexMatch);
if (i >= 0)
{
const QString sourceText = regexMatch.captured(1);
const QString context = regexMatch.captured(3);
const QStringView sourceText = regexMatch.capturedView(1);
const QStringView context = regexMatch.capturedView(3);
const QString loadedText = m_translationFileLoaded
? m_translator.translate(context.toUtf8().constData(), sourceText.toUtf8().constData())
: QString();
// `loadedText` is empty when translation is not provided
// it should fallback to `sourceText`
QString translation = loadedText.isEmpty() ? sourceText : loadedText;
QString translation = loadedText.isEmpty() ? sourceText.toString() : loadedText;
// Escape quotes to workaround issues with HTML attributes
// FIXME: this is a dirty workaround to deal with broken translation strings: