summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qstringalgorithms_p.h
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2014-07-28 19:50:35 -0700
committerThiago Macieira <thiago.macieira@intel.com>2014-11-02 20:51:57 +0100
commit294c914eb6bb5e0200480400ba31f2049edb9b86 (patch)
tree86fe3198ebc56f63efb2ea136be88e15a708368b /src/corelib/tools/qstringalgorithms_p.h
parent939197fd7c8b64256333d096f3b4ebbf066b2481 (diff)
Add rvalue-ref qualified {QString,QByteArray}::{simplified,trimmed}
Of the const overloads that return a QString or a QByteArray, this is one that gains the most benefit. It happens often in constructs like: QByteArray s = x.readLine().trimmed(); After this change, 41 out of 103 calls to trimmed become rvalue in Qt and 272 out of 441 in Qt Creator. For simplified, the numbers are 27 out of 69 in Qt and 10 out of 19 in Qt Creator. Other candidates are left, right, and mid, but there are exactly zero uses of left, right and mid on an xvalue QString or QByteArray in Qt. I'm being lazy and using qstring_compat.cpp to store the QByteArray compat methods. Change-Id: I4e410fc1adc4c761bb07cc3d43b348a65befa9f6 Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib/tools/qstringalgorithms_p.h')
-rw-r--r--src/corelib/tools/qstringalgorithms_p.h27
1 files changed, 24 insertions, 3 deletions
diff --git a/src/corelib/tools/qstringalgorithms_p.h b/src/corelib/tools/qstringalgorithms_p.h
index 5a68260c59..c3a87b2072 100644
--- a/src/corelib/tools/qstringalgorithms_p.h
+++ b/src/corelib/tools/qstringalgorithms_p.h
@@ -72,6 +72,23 @@ template <typename StringType> struct QStringAlgorithms
// not a problem because there are no space characters (Zs, Zl, Zp) outside the
// Basic Multilingual Plane.
+ static inline StringType trimmed_helper_inplace(NakedStringType &str, const Char *begin, const Char *end)
+ {
+ // in-place trimming:
+ Char *data = const_cast<Char *>(str.cbegin());
+ if (begin != data)
+ memmove(data, begin, (end - begin) * sizeof(Char));
+ str.resize(end - begin);
+ return qMove(str);
+ }
+
+ static inline StringType trimmed_helper_inplace(const NakedStringType &, const Char *, const Char *)
+ {
+ // can't happen
+ Q_UNREACHABLE();
+ return StringType();
+ }
+
static inline void trimmed_helper_positions(const Char *&begin, const Char *&end)
{
// skip white space from start
@@ -94,6 +111,8 @@ template <typename StringType> struct QStringAlgorithms
return str;
if (begin == end)
return StringType();
+ if (!isConst && str.isDetached())
+ return trimmed_helper_inplace(str, begin, end);
return StringType(begin, end - begin);
}
@@ -103,7 +122,9 @@ template <typename StringType> struct QStringAlgorithms
return str;
const Char *src = str.cbegin();
const Char *end = str.cend();
- NakedStringType result(str.size(), Qt::Uninitialized);
+ NakedStringType result = isConst ?
+ StringType(str.size(), Qt::Uninitialized) :
+ qMove(str);
Char *dst = const_cast<Char *>(result.cbegin());
Char *ptr = dst;
@@ -121,12 +142,12 @@ template <typename StringType> struct QStringAlgorithms
--ptr;
int newlen = ptr - dst;
- if (newlen == str.size()) {
+ if (isConst && newlen == str.size()) {
// nothing happened, return the original
return str;
}
result.resize(ptr - dst);
- return qMove(result);
+ return result;
}
};