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 <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2023-07-17 17:51:51 +03:00
parent 358e13a5e1
commit 9d4cd4a63e
3 changed files with 16 additions and 11 deletions

View File

@ -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<QByteArrayView>::trimmed_helper_positions(start, stop);
const auto [start, stop] = QStringAlgorithms<QByteArrayView>::trimmed_helper_positions(view);
return QByteArrayView(start, stop);
}

View File

@ -5995,9 +5995,7 @@ namespace {
template <typename StringView>
StringView qt_trimmed(StringView s) noexcept
{
auto begin = s.begin();
auto end = s.end();
QStringAlgorithms<const StringView>::trimmed_helper_positions(begin, end);
const auto [begin, end] = QStringAlgorithms<const StringView>::trimmed_helper_positions(s);
return StringView{begin, end};
}
}

View File

@ -50,22 +50,31 @@ template <typename StringType> 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())