Use QString::compare with Qt::CaseInsensitive to reduce allocations

This commit is contained in:
Jonas Kvinge
2021-07-13 23:18:12 +02:00
parent a87863229f
commit 68dbc29f2c
16 changed files with 192 additions and 181 deletions

View File

@@ -127,7 +127,7 @@ void AuddLyricsProvider::HandleSearchReply(QNetworkReply *reply, const quint64 i
LyricsSearchResult result;
result.artist = json_obj["artist"].toString();
result.title = json_obj["title"].toString();
if (result.artist.toLower() != artist.toLower() && result.title.toLower() != title.toLower()) continue;
if (result.artist.compare(artist, Qt::CaseInsensitive) != 0 && result.title.compare(title, Qt::CaseInsensitive) != 0) continue;
result.lyrics = json_obj["lyrics"].toString();
if (result.lyrics.isEmpty() || result.lyrics.length() > kMaxLength || result.lyrics == "error") continue;
result.lyrics = Utilities::DecodeHtmlEntities(result.lyrics);

View File

@@ -126,7 +126,7 @@ void ChartLyricsProvider::HandleSearchReply(QNetworkReply *reply, const quint64
}
else if (type == QXmlStreamReader::EndElement) {
if (name == "GetLyricResult") {
if (!result.artist.isEmpty() && !result.title.isEmpty() && !result.lyrics.isEmpty() && (result.artist.toLower() == artist.toLower() || result.title.toLower() == title.toLower())) {
if (!result.artist.isEmpty() && !result.title.isEmpty() && !result.lyrics.isEmpty() && (result.artist.compare(artist, Qt::CaseInsensitive) == 0 || result.title.compare(title, Qt::CaseInsensitive) == 0)) {
result.lyrics = Utilities::DecodeHtmlEntities(result.lyrics);
results << result;
}

View File

@@ -432,7 +432,7 @@ void GeniusLyricsProvider::HandleSearchReply(QNetworkReply *reply, const quint64
QString title = obj_result["title"].toString();
// Ignore results where both the artist and title don't match.
if (artist.toLower() != search->artist.toLower() && title.toLower() != search->title.toLower()) continue;
if (artist.compare(search->artist, Qt::CaseInsensitive) != 0 && title.compare(search->title, Qt::CaseInsensitive) != 0) continue;
QUrl url(obj_result["url"].toString());
if (!url.isValid()) continue;

View File

@@ -58,7 +58,7 @@ void LyricsFetcherSearch::TerminateSearch() {
void LyricsFetcherSearch::Start(LyricsProviders *lyrics_providers) {
// Ignore Radio Paradise "commercial" break.
if (request_.artist.toLower() == "commercial-free" && request_.title.toLower() == "listener-supported") {
if (request_.artist.compare("commercial-free", Qt::CaseInsensitive) == 0 && request_.title.compare("listener-supported", Qt::CaseInsensitive) == 0) {
TerminateSearch();
return;
}
@@ -88,16 +88,16 @@ void LyricsFetcherSearch::ProviderSearchFinished(const quint64 id, const LyricsS
for (int i = 0 ; i < results_copy.count() ; ++i) {
results_copy[i].provider = provider->name();
results_copy[i].score = 0.0;
if (results_copy[i].artist.toLower() == request_.artist.toLower()) {
if (results_copy[i].artist.compare(request_.artist, Qt::CaseInsensitive) == 0) {
results_copy[i].score += 0.5;
}
if (results_copy[i].album.toLower() == request_.album.toLower()) {
if (results_copy[i].album.compare(request_.album, Qt::CaseInsensitive) == 0) {
results_copy[i].score += 0.5;
}
if (results_copy[i].title.toLower() == request_.title.toLower()) {
if (results_copy[i].title.compare(request_.title, Qt::CaseInsensitive) == 0) {
results_copy[i].score += 0.5;
}
if (results_copy[i].artist.toLower() != request_.artist.toLower() && results_copy[i].title.toLower() != request_.title.toLower()) {
if (results_copy[i].artist.compare(request_.artist, Qt::CaseInsensitive) != 0 && results_copy[i].title.compare(request_.title, Qt::CaseInsensitive) != 0) {
results_copy[i].score -= 1.5;
}
if (results_copy[i].lyrics.length() > kGoodLyricsLength) results_copy[i].score += 1.0;

View File

@@ -58,19 +58,19 @@ bool MusixmatchLyricsProvider::StartSearch(const QString &artist, const QString
QString artist_stripped = artist;
QString title_stripped = title;
artist_stripped = artist_stripped.replace('/', '-');
artist_stripped = artist_stripped.remove(QRegularExpression("[^\\w0-9\\- ]", QRegularExpression::UseUnicodePropertiesOption));
artist_stripped = artist_stripped.simplified();
artist_stripped = artist_stripped.replace(' ', '-');
artist_stripped = artist_stripped.replace(QRegularExpression("(-)\\1+"), "-");
artist_stripped = artist_stripped.toLower();
artist_stripped = artist_stripped.replace('/', '-')
.remove(QRegularExpression("[^\\w0-9\\- ]", QRegularExpression::UseUnicodePropertiesOption))
.simplified()
.replace(' ', '-')
.replace(QRegularExpression("(-)\\1+"), "-")
.toLower();
title_stripped = title_stripped.replace('/', '-');
title_stripped = title_stripped.remove(QRegularExpression("[^\\w0-9\\- ]", QRegularExpression::UseUnicodePropertiesOption));
title_stripped = title_stripped.simplified();
title_stripped = title_stripped.replace(' ', '-').toLower();
title_stripped = title_stripped.replace(QRegularExpression("(-)\\1+"), "-");
title_stripped = title_stripped.toLower();
title_stripped = title_stripped.replace('/', '-')
.remove(QRegularExpression("[^\\w0-9\\- ]", QRegularExpression::UseUnicodePropertiesOption))
.simplified()
.replace(' ', '-')
.replace(QRegularExpression("(-)\\1+"), "-")
.toLower();
if (artist_stripped.isEmpty() || title_stripped.isEmpty()) return false;
@@ -199,7 +199,7 @@ void MusixmatchLyricsProvider::HandleSearchReply(QNetworkReply *reply, const qui
result.title = obj_track["name"].toString();
result.lyrics = obj_lyrics["body"].toString();
if (!result.lyrics.isEmpty() && (artist.toLower() == result.artist.toLower() || title.toLower() == result.title.toLower())) {
if (!result.lyrics.isEmpty() && (result.artist.compare(artist, Qt::CaseInsensitive) == 0 || result.title.compare(title, Qt::CaseInsensitive) == 0)) {
result.lyrics = Utilities::DecodeHtmlEntities(result.lyrics);
results.append(result);
}