From 74dc7931abae4f551b5276c7e6a530e121e0d6e4 Mon Sep 17 00:00:00 2001 From: ronso0 Date: Fri, 1 Nov 2024 22:32:16 +0100 Subject: [PATCH] util/string.h: add removeTrailingWhitespaces(), used in DlgTrackInfoMulti Co-authored-by: Lukas Waslowski --- src/library/dlgtrackinfomulti.cpp | 15 +++++---------- src/util/string.h | 10 ++++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/library/dlgtrackinfomulti.cpp b/src/library/dlgtrackinfomulti.cpp index 39c1bae3211..dc18bfd0d2d 100644 --- a/src/library/dlgtrackinfomulti.cpp +++ b/src/library/dlgtrackinfomulti.cpp @@ -18,6 +18,7 @@ #include "util/datetime.h" #include "util/desktophelper.h" #include "util/duration.h" +#include "util/string.h" #include "util/stringformat.h" #include "widget/wcoverartlabel.h" #include "widget/wcoverartmenu.h" @@ -53,18 +54,15 @@ QString validEditText(QComboBox* pBox) { // flag). For single-value boxes we compare with the original value. auto* pLine = pBox->lineEdit(); const QString origVal = pBox->property(kOrigValProp).toString(); - QString currVal = pLine->text(); + const QString currText = pLine->text(); if ((pBox->count() > 0 && !pLine->placeholderText().isNull()) || - (pBox->count() == 0 && currVal == origVal)) { + (pBox->count() == 0 && currText == origVal)) { return QString(); } // We have a new text. // Remove trailing whitespaces. Keep Leading whitespaces to be consistent // with the track table's inline editor. - while (currVal.endsWith(' ')) { - currVal.chop(1); - } - return currVal; + return mixxx::removeTrailingWhitespaces(currText); } /// Sets the text of a QLabel, either the only/common value or the 'various' string. @@ -622,10 +620,7 @@ void DlgTrackInfoMulti::saveTracks() { if ((txtCommentBox->count() > 0 && txtComment->placeholderText().isNull()) || (txtCommentBox->count() == 0 && currText != origText)) { // Remove trailing whitespaces. - comment = currText; - while (comment.endsWith(' ')) { - comment.chop(1); - } + comment = mixxx::removeTrailingWhitespaces(currText); } for (auto& rec : m_trackRecords) { diff --git a/src/util/string.h b/src/util/string.h index 8f439af46b1..ed1a94ac80d 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -84,6 +84,16 @@ inline QString convertWCStringToQString( return QString::fromWCharArray(wcs, static_cast(wcsnlen_s(wcs, maxLen))); } +/// Remove trailing spaces from the specified string. +/// Currently only handles simple ASCII spaces (" "). +inline QString removeTrailingWhitespaces(const QString& str) { + QString out = str; + while (out.endsWith(' ')) { + out.chop(1); + } + return out; +} + } // namespace mixxx // Helper to create html link strings to be used for ui files, mostly in