QStringAlgorithms/private: prefer to trim whitespace from the end

When calculating what to trim in trimmed_helper_positions(), first
trim the end, then the front.

This way, a string that consists of just whitespace will remain
anchored at its front, and we do not run into the memmove case in
trimmed_helper_inplace, which, even though there's zero elements to
move, still calls a potentially-out-of-line function (memmove()).

Change-Id: I7024ffa1f7ae2effb9c5166ec8f30a42b8d51079
Reviewed-by: Anton Kudryavtsev <antkudr@mail.ru>
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2017-05-03 19:52:56 +02:00
parent b53f8bf040
commit b1debc11c1

View File

@ -89,14 +89,12 @@ template <typename StringType> struct QStringAlgorithms
static inline void trimmed_helper_positions(const Char *&begin, const Char *&end)
{
// skip white space from end
while (begin < end && isSpace(end[-1]))
--end;
// skip white space from start
while (begin < end && isSpace(*begin))
begin++;
// skip white space from end
if (begin < end) {
while (begin < end && isSpace(end[-1]))
end--;
}
}
static inline StringType trimmed_helper(StringType &str)