From b1debc11c118bb1e49a9e92a77194140255854fc Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 3 May 2017 19:52:56 +0200 Subject: [PATCH] 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 Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/corelib/tools/qstringalgorithms_p.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/corelib/tools/qstringalgorithms_p.h b/src/corelib/tools/qstringalgorithms_p.h index c5470bc7ad..4bcf697f78 100644 --- a/src/corelib/tools/qstringalgorithms_p.h +++ b/src/corelib/tools/qstringalgorithms_p.h @@ -89,14 +89,12 @@ template 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)