Optimize parsing of search results

PR #22906.
This commit is contained in:
Vladimir Golovnev
2025-06-26 08:49:58 +03:00
committed by GitHub
parent 71af105a89
commit 41d7d672ce
5 changed files with 92 additions and 48 deletions

View File

@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2023-2025 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2018 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
@@ -34,16 +34,32 @@
#include <QByteArrayView>
#include <QList>
QList<QByteArrayView> Utils::ByteArray::splitToViews(const QByteArrayView in, const QByteArrayView sep)
QList<QByteArrayView> Utils::ByteArray::splitToViews(const QByteArrayView in, const QByteArrayView sep, const Qt::SplitBehavior behavior)
{
if (in.isEmpty())
return {};
if (sep.isEmpty())
return {in};
if (behavior == Qt::SkipEmptyParts)
{
if (in.isEmpty())
return {};
if (sep.isEmpty())
return {in};
}
else
{
if (in.isEmpty())
{
if (sep.isEmpty())
return {{}, {}};
return {{}};
}
}
const QByteArrayMatcher matcher {sep};
QList<QByteArrayView> ret;
ret.reserve(1 + (in.size() / (sep.size() + 1)));
ret.reserve((behavior == Qt::SkipEmptyParts)
? (1 + (in.size() / (sep.size() + 1)))
: (1 + (in.size() / sep.size())));
qsizetype head = 0;
while (head < in.size())
{
@@ -51,14 +67,16 @@ QList<QByteArrayView> Utils::ByteArray::splitToViews(const QByteArrayView in, co
if (end < 0)
end = in.size();
// omit empty parts
const QByteArrayView part = in.sliced(head, (end - head));
if (!part.isEmpty())
if (!part.isEmpty() || (behavior == Qt::KeepEmptyParts))
ret += part;
head = end + sep.size();
}
if ((behavior == Qt::KeepEmptyParts) && (head == in.size()))
ret.emplaceBack();
return ret;
}

View File

@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2023-2025 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2018 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
@@ -37,8 +37,8 @@ class QByteArrayView;
namespace Utils::ByteArray
{
// Inspired by QStringView(in).split(sep, Qt::SkipEmptyParts)
QList<QByteArrayView> splitToViews(QByteArrayView in, QByteArrayView sep);
// Inspired by QStringView(in).split(sep, behavior)
QList<QByteArrayView> splitToViews(QByteArrayView in, QByteArrayView sep, Qt::SplitBehavior behavior = Qt::SkipEmptyParts);
QByteArray asQByteArray(QByteArrayView view);
QByteArray toBase32(const QByteArray &in);