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())