summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/tools/qbytearray.cpp24
-rw-r--r--src/corelib/tools/qbytearray.h25
-rw-r--r--src/corelib/tools/qstring_compat.cpp14
-rw-r--r--tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp27
4 files changed, 83 insertions, 7 deletions
diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp
index 6b6a9d2c1a..8b2d48df66 100644
--- a/src/corelib/tools/qbytearray.cpp
+++ b/src/corelib/tools/qbytearray.cpp
@@ -2777,10 +2777,10 @@ static QByteArray toCase_template(T &input, const uchar * table)
}
if (firstBad == e)
- return input;
+ return qMove(input);
// transform the rest
- QByteArray s = input;
+ QByteArray s = qMove(input); // will copy if T is const QByteArray
char *b = s.begin(); // will detach if necessary
char *p = b + (firstBad - orig_begin);
e = b + s.size();
@@ -2790,13 +2790,19 @@ static QByteArray toCase_template(T &input, const uchar * table)
return s;
}
+QByteArray QByteArray::toLower_helper(const QByteArray &a)
+{
+ return toCase_template(a, latin1_lowercased);
+}
-QByteArray QByteArray::toLower() const
+QByteArray QByteArray::toLower_helper(QByteArray &a)
{
- return toCase_template(*this, latin1_lowercased);
+ return toCase_template(a, latin1_lowercased);
}
/*!
+ \fn QByteArray QByteArray::toUpper() const
+
Returns an uppercase copy of the byte array. The bytearray is
interpreted as a Latin-1 encoded string.
@@ -2805,9 +2811,15 @@ QByteArray QByteArray::toLower() const
\sa toLower(), {8-bit Character Comparisons}
*/
-QByteArray QByteArray::toUpper() const
+
+QByteArray QByteArray::toUpper_helper(const QByteArray &a)
+{
+ return toCase_template(a, latin1_uppercased);
+}
+
+QByteArray QByteArray::toUpper_helper(QByteArray &a)
{
- return toCase_template(*this, latin1_uppercased);
+ return toCase_template(a, latin1_uppercased);
}
/*! \fn void QByteArray::clear()
diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h
index ba7fdbb10a..cb1ebf8493 100644
--- a/src/corelib/tools/qbytearray.h
+++ b/src/corelib/tools/qbytearray.h
@@ -264,8 +264,29 @@ public:
void truncate(int pos);
void chop(int n);
+#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
+ QByteArray toLower() const & Q_REQUIRED_RESULT
+ { return toLower_helper(*this); }
+ QByteArray toLower() && Q_REQUIRED_RESULT
+ { return toLower_helper(*this); }
+ QByteArray toUpper() const & Q_REQUIRED_RESULT
+ { return toUpper_helper(*this); }
+ QByteArray toUpper() && Q_REQUIRED_RESULT
+ { return toUpper_helper(*this); }
+# ifdef Q_REQUIRED_RESULT_pushed
+# pragma pop_macro("Q_REQUIRED_RESULT")
+# endif
+#else
QByteArray toLower() const Q_REQUIRED_RESULT;
QByteArray toUpper() const Q_REQUIRED_RESULT;
+#endif
QByteArray trimmed() const Q_REQUIRED_RESULT;
QByteArray simplified() const Q_REQUIRED_RESULT;
@@ -422,6 +443,10 @@ private:
void expand(int i);
QByteArray nulTerminated() const;
+ static QByteArray toLower_helper(const QByteArray &a);
+ static QByteArray toLower_helper(QByteArray &a);
+ static QByteArray toUpper_helper(const QByteArray &a);
+ static QByteArray toUpper_helper(QByteArray &a);
friend class QByteRef;
friend class QString;
friend Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes);
diff --git a/src/corelib/tools/qstring_compat.cpp b/src/corelib/tools/qstring_compat.cpp
index 6613dfde8e..be74775c1a 100644
--- a/src/corelib/tools/qstring_compat.cpp
+++ b/src/corelib/tools/qstring_compat.cpp
@@ -39,11 +39,12 @@
**
****************************************************************************/
-#if defined(QSTRING_H)
+#if defined(QSTRING_H) || defined(QBYTEARRAY_H)
# error "This file cannot be compiled with pre-compiled headers"
#endif
#define QT_COMPILING_QSTRING_COMPAT_CPP
+#include "qbytearray.h"
#include "qstring.h"
QT_BEGIN_NAMESPACE
@@ -64,4 +65,15 @@ QByteArray QString::toUtf8() const
return toUtf8_helper(*this);
}
+// ditto, for qbytearray.h (because we're lazy)
+QByteArray QByteArray::toLower() const
+{
+ return toLower_helper(*this);
+}
+
+QByteArray QByteArray::toUpper() const
+{
+ return toUpper_helper(*this);
+}
+
QT_END_NAMESPACE
diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp
index 8670e4b3ef..a60c3a5d9f 100644
--- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp
+++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp
@@ -2008,6 +2008,9 @@ void tst_QByteArray::toUpperLower_data()
QTest::addColumn<QByteArray>("lower");
QTest::newRow("empty") << QByteArray() << QByteArray() << QByteArray();
+ QTest::newRow("literal") << QByteArrayLiteral("Hello World")
+ << QByteArrayLiteral("HELLO WORLD")
+ << QByteArrayLiteral("hello world");
QTest::newRow("ascii") << QByteArray("Hello World, this is a STRING")
<< QByteArray("HELLO WORLD, THIS IS A STRING")
<< QByteArray("hello world, this is a string");
@@ -2026,6 +2029,30 @@ void tst_QByteArray::toUpperLower()
QCOMPARE(upper.toUpper(), upper);
QCOMPARE(input.toUpper(), upper);
QCOMPARE(input.toLower(), lower);
+
+ QByteArray copy = input;
+ QCOMPARE(qMove(copy).toUpper(), upper);
+ copy = input;
+ copy.detach();
+ QCOMPARE(qMove(copy).toUpper(), upper);
+
+ copy = input;
+ QCOMPARE(qMove(copy).toLower(), lower);
+ copy = input;
+ copy.detach();
+ QCOMPARE(qMove(copy).toLower(), lower);
+
+ copy = lower;
+ QCOMPARE(qMove(copy).toLower(), lower);
+ copy = lower;
+ copy.detach();
+ QCOMPARE(qMove(copy).toLower(), lower);
+
+ copy = upper;
+ QCOMPARE(qMove(copy).toUpper(), upper);
+ copy = upper;
+ copy.detach();
+ QCOMPARE(qMove(copy).toUpper(), upper);
}
void tst_QByteArray::macTypes()