From 9d4cd4a63e073a4662ebb189e4d3a0fe6225fb49 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Mon, 17 Jul 2023 17:51:51 +0300 Subject: [PATCH] qstringalgorithms: refactor trimmed_helper_positions Take by const Str&, trimming a container/view of characters means removing whitespace from the beginning and end, so the two args were always cbegin() and cend(). Change-Id: Iac0eda59341f1981204d11013d591013eae3c4e0 Reviewed-by: Thiago Macieira --- src/corelib/text/qbytearray.cpp | 4 +--- src/corelib/text/qstring.cpp | 4 +--- src/corelib/text/qstringalgorithms_p.h | 19 ++++++++++++++----- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 267d8a3371..3766d68adc 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -3657,9 +3657,7 @@ QByteArray QByteArray::trimmed_helper(QByteArray &a) QByteArrayView QtPrivate::trimmed(QByteArrayView view) noexcept { - auto start = view.begin(); - auto stop = view.end(); - QStringAlgorithms::trimmed_helper_positions(start, stop); + const auto [start, stop] = QStringAlgorithms::trimmed_helper_positions(view); return QByteArrayView(start, stop); } diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 5a0a9102b3..55bb99087b 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -5995,9 +5995,7 @@ namespace { template StringView qt_trimmed(StringView s) noexcept { - auto begin = s.begin(); - auto end = s.end(); - QStringAlgorithms::trimmed_helper_positions(begin, end); + const auto [begin, end] = QStringAlgorithms::trimmed_helper_positions(s); return StringView{begin, end}; } } diff --git a/src/corelib/text/qstringalgorithms_p.h b/src/corelib/text/qstringalgorithms_p.h index 0b8118a858..4a3bebaa28 100644 --- a/src/corelib/text/qstringalgorithms_p.h +++ b/src/corelib/text/qstringalgorithms_p.h @@ -50,22 +50,31 @@ template struct QStringAlgorithms Q_UNREACHABLE_RETURN(StringType()); } - static inline void trimmed_helper_positions(const Char *&begin, const Char *&end) + struct TrimPositions { + const Char *begin; + const Char *end; + }; + // Returns {begin, end} where: + // - "begin" refers to the first non-space character + // - if there is a sequence of one or more space chacaters at the end, + // "end" refers to the first character in that sequence, otherwise + // "end" is str.cend() + static TrimPositions trimmed_helper_positions(const StringType &str) { + const Char *begin = str.cbegin(); + const Char *end = str.cend(); // skip white space from end while (begin < end && isSpace(end[-1])) --end; // skip white space from start while (begin < end && isSpace(*begin)) begin++; + return {begin, end}; } static inline StringType trimmed_helper(StringType &str) { - const Char *begin = str.cbegin(); - const Char *end = str.cend(); - trimmed_helper_positions(begin, end); - + const auto [begin, end] = trimmed_helper_positions(str); if (begin == str.cbegin() && end == str.cend()) return str; if (!isConst && str.isDetached())