From cf51e2f33fa9ce28c3e822d75fcc87dfeaeed4b1 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 25 Jan 2016 10:41:15 +0100 Subject: [PATCH] QStringRef: add missing op[] [ChangeLog][QtCore][QStringRef] Added subscript operator. Change-Id: Ia85d5efcb7747d2961ba55922ddabe6a46bdf20b Reviewed-by: Lars Knoll --- src/corelib/tools/qstring.cpp | 14 +++++++++++++- src/corelib/tools/qstring.h | 1 + .../corelib/tools/qstringref/tst_qstringref.cpp | 11 +++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index d17dd32328..9b98f4322b 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -8800,7 +8800,6 @@ Since this class is only used to refer to string data, and does not take ownership of it, no memory is freed when instances are destroyed. */ - /*! \fn int QStringRef::position() const @@ -9038,6 +9037,19 @@ bool operator<(const QStringRef &s1,const QStringRef &s2) Q_DECL_NOTHROW (i.e., 0 <= \a position < size()). */ +/*! + \fn QChar QStringRef::operator[](int position) const + \since 5.7 + + Returns the character at the given index \a position in the + string reference. + + The \a position must be a valid index position in the string + reference (i.e., 0 <= \a position < size()). + + \sa at() +*/ + /*! \fn void QStringRef::clear() diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 6d57343356..da651f865e 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -1462,6 +1462,7 @@ public: inline const QChar at(int i) const { Q_ASSERT(uint(i) < uint(size())); return m_string->at(i + m_position); } + QChar operator[](int i) const { return at(i); } #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) // ASCII compatibility diff --git a/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp b/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp index b3a1558931..b98c2fb4de 100644 --- a/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp +++ b/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp @@ -41,6 +41,7 @@ class tst_QStringRef : public QObject public slots: void cleanup(); private slots: + void at(); void endsWith(); void startsWith(); void contains(); @@ -172,6 +173,16 @@ void tst_QStringRef::cleanup() QLocale::setDefault(QString(QLatin1Char('C'))); } +void tst_QStringRef::at() +{ + const QString hw = QStringLiteral("Hello World"); + const QStringRef ref = hw.midRef(6); + QCOMPARE(ref.at(0), QChar('W')); + QCOMPARE(ref.at(4), QChar('d')); + QCOMPARE(ref[0], QChar('W')); + QCOMPARE(ref[4], QChar('d')); +} + void tst_QStringRef::length_data() { QTest::addColumn("s1");