summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2014-07-29 18:06:51 -0700
committerThiago Macieira <thiago.macieira@intel.com>2014-08-19 03:39:47 +0200
commit19dd9a0ebd570ac134e115f2339912a1015ecde5 (patch)
treea6eb385853c1c83e3e2e7269b38b118e8176af68 /src/corelib/tools
parent3888f5a251d6230cc290ec0ada211a6b45307615 (diff)
Add rvalue-ref qualified QString::to{Upper,Lower,CaseFolded}
This is even more common than the QByteArray equivalents. Qt Qt Creator const & && const & && toLower 71 50 45 26 toUpper 35 8 46 35 Change-Id: I8b797d2321b22ce414c23656c5f1709ac649c423 Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r--src/corelib/tools/qstring.cpp48
-rw-r--r--src/corelib/tools/qstring.h31
-rw-r--r--src/corelib/tools/qstring_compat.cpp15
3 files changed, 81 insertions, 13 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index 23329224e7..5ac11c71c6 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -5547,16 +5547,16 @@ struct CasefoldTraits
{ return prop->caseFoldSpecial; }
};
-template <typename Traits>
+template <typename Traits, typename T>
#ifdef Q_CC_MSVC
__declspec(noinline)
#elif defined(Q_CC_GNU)
__attribute__((noinline))
#endif
-static QString detachAndConvertCase(const QString &str, QStringIterator it)
+static QString detachAndConvertCase(T &str, QStringIterator it)
{
- QString s(str);
- QChar *pp = s.begin() + it.index();
+ QString s = qMove(str); // will copy if T is const QString
+ QChar *pp = s.begin() + it.index(); // will detach if necessary
uint uc = it.nextUnchecked();
forever {
const QUnicodeTables::Properties *prop = qGetProp(uc);
@@ -5583,8 +5583,8 @@ static QString detachAndConvertCase(const QString &str, QStringIterator it)
}
}
-template <typename Traits>
-static inline QString convertCase(const QString &str)
+template <typename Traits, typename T>
+static QString convertCase(T &str)
{
const QChar *p = str.constBegin();
const QChar *e = p + str.size();
@@ -5600,25 +5600,40 @@ static inline QString convertCase(const QString &str)
if (Traits::caseDiff(prop))
return detachAndConvertCase<Traits>(str, it);
}
- return str;
+ return qMove(str);
}
} // namespace QUnicodeTables
-QString QString::toLower() const
+QString QString::toLower_helper(const QString &str)
{
- return QUnicodeTables::convertCase<QUnicodeTables::LowercaseTraits>(*this);
+ return QUnicodeTables::convertCase<QUnicodeTables::LowercaseTraits>(str);
+}
+
+QString QString::toLower_helper(QString &str)
+{
+ return QUnicodeTables::convertCase<QUnicodeTables::LowercaseTraits>(str);
}
/*!
+ \fn QString QString::toCaseFolded() const
+
Returns the case folded equivalent of the string. For most Unicode
characters this is the same as toLower().
*/
-QString QString::toCaseFolded() const
+
+QString QString::toCaseFolded_helper(const QString &str)
+{
+ return QUnicodeTables::convertCase<QUnicodeTables::CasefoldTraits>(str);
+}
+
+QString QString::toCaseFolded_helper(QString &str)
{
- return QUnicodeTables::convertCase<QUnicodeTables::CasefoldTraits>(*this);
+ return QUnicodeTables::convertCase<QUnicodeTables::CasefoldTraits>(str);
}
/*!
+ \fn QString QString::toUpper() const
+
Returns an uppercase copy of the string.
\snippet qstring/main.cpp 81
@@ -5628,11 +5643,18 @@ QString QString::toCaseFolded() const
\sa toLower(), QLocale::toLower()
*/
-QString QString::toUpper() const
+
+QString QString::toUpper_helper(const QString &str)
{
- return QUnicodeTables::convertCase<QUnicodeTables::UppercaseTraits>(*this);
+ return QUnicodeTables::convertCase<QUnicodeTables::UppercaseTraits>(str);
}
+QString QString::toUpper_helper(QString &str)
+{
+ return QUnicodeTables::convertCase<QUnicodeTables::UppercaseTraits>(str);
+}
+
+
// ### Qt 6: Consider whether this function shouldn't be removed See task 202871.
/*!
Safely builds a formatted string from the format string \a cformat
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
index dfb98ffaa2..a6e13e37b5 100644
--- a/src/corelib/tools/qstring.h
+++ b/src/corelib/tools/qstring.h
@@ -377,9 +377,34 @@ public:
QString leftJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const Q_REQUIRED_RESULT;
QString rightJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const Q_REQUIRED_RESULT;
+#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP)
+# if defined(Q_CC_GNU)
+ // required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941
+# pragma push_macro("Q_REQUIRED_RESULT")
+# undef Q_REQUIRED_RESULT
+# define Q_REQUIRED_RESULT
+# define Q_REQUIRED_RESULT_pushed
+# endif
+ QString toLower() const & Q_REQUIRED_RESULT
+ { return toLower_helper(*this); }
+ QString toLower() && Q_REQUIRED_RESULT
+ { return toLower_helper(*this); }
+ QString toUpper() const & Q_REQUIRED_RESULT
+ { return toUpper_helper(*this); }
+ QString toUpper() && Q_REQUIRED_RESULT
+ { return toUpper_helper(*this); }
+ QString toCaseFolded() const & Q_REQUIRED_RESULT
+ { return toCaseFolded_helper(*this); }
+ QString toCaseFolded() && Q_REQUIRED_RESULT
+ { return toCaseFolded_helper(*this); }
+# ifdef Q_REQUIRED_RESULT_pushed
+# pragma pop_macro("Q_REQUIRED_RESULT")
+# endif
+#else
QString toLower() const Q_REQUIRED_RESULT;
QString toUpper() const Q_REQUIRED_RESULT;
QString toCaseFolded() const Q_REQUIRED_RESULT;
+#endif
QString trimmed() const Q_REQUIRED_RESULT;
QString simplified() const Q_REQUIRED_RESULT;
@@ -742,6 +767,12 @@ private:
Qt::CaseSensitivity cs = Qt::CaseSensitive);
static int localeAwareCompare_helper(const QChar *data1, int length1,
const QChar *data2, int length2);
+ static QString toLower_helper(const QString &str);
+ static QString toLower_helper(QString &str);
+ static QString toUpper_helper(const QString &str);
+ static QString toUpper_helper(QString &str);
+ static QString toCaseFolded_helper(const QString &str);
+ static QString toCaseFolded_helper(QString &str);
static Data *fromLatin1_helper(const char *str, int size = -1);
static Data *fromAscii_helper(const char *str, int size = -1);
static QString fromUtf8_helper(const char *str, int size);
diff --git a/src/corelib/tools/qstring_compat.cpp b/src/corelib/tools/qstring_compat.cpp
index be74775c1a..b42f49d86d 100644
--- a/src/corelib/tools/qstring_compat.cpp
+++ b/src/corelib/tools/qstring_compat.cpp
@@ -50,6 +50,21 @@
QT_BEGIN_NAMESPACE
// all these implementations must be the same as the inline versions in qstring.h
+QString QString::toLower() const
+{
+ return toLower_helper(*this);
+}
+
+QString QString::toCaseFolded() const
+{
+ return toCaseFolded_helper(*this);
+}
+
+QString QString::toUpper() const
+{
+ return toUpper_helper(*this);
+}
+
QByteArray QString::toLatin1() const
{
return toLatin1_helper(*this);