diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2023-07-17 17:51:51 +0300 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2023-07-22 20:48:11 +0300 |
commit | 9d4cd4a63e073a4662ebb189e4d3a0fe6225fb49 (patch) | |
tree | 2350d007468a6889e77e85ce90a6b6bc4dc1291b /src/corelib/text/qstringalgorithms_p.h | |
parent | 358e13a5e1829afdb65c27fd5b2157df5ad222c6 (diff) |
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>
Diffstat (limited to 'src/corelib/text/qstringalgorithms_p.h')
-rw-r--r-- | src/corelib/text/qstringalgorithms_p.h | 19 |
1 files changed, 14 insertions, 5 deletions
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 <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()) |