QStringRef: add missing op[]

[ChangeLog][QtCore][QStringRef] Added subscript operator.

Change-Id: Ia85d5efcb7747d2961ba55922ddabe6a46bdf20b
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
This commit is contained in:
Marc Mutz 2016-01-25 10:41:15 +01:00
parent 1c470f3af0
commit cf51e2f33f
3 changed files with 25 additions and 1 deletions

View File

@ -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()

View File

@ -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

View File

@ -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<QString>("s1");