diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-04-06 19:36:47 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-04-07 08:32:32 +0000 |
commit | 399ab058c94978b40876e42b5b677d392f2e33f5 (patch) | |
tree | ae1d0920f45e63d9c65fb91c0a765c981c45cde9 /src/corelib/tools/qstring.h | |
parent | 5dc1e08c8c602738f6a827cea0de44683c7bbd0b (diff) |
Add front()/back() to QString, QStringRef, QByteArray and QLatin1String
These STL-compatibility functions are present on our generic
containers, but not on the string classes.
[ChangeLog][QtCore][QString/QStringRef/QByteArray/QLatin1String] Added
front() and back() for STL compatibility.
Change-Id: I536019396b319abd1e2daf9c64ebab4e7a35b334
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/corelib/tools/qstring.h')
-rw-r--r-- | src/corelib/tools/qstring.h | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 431e2672ff..c00208869c 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -120,6 +120,9 @@ public: { return Q_ASSERT(i >= 0), Q_ASSERT(i < size()), QLatin1Char(m_data[i]); } Q_DECL_CONSTEXPR QLatin1Char operator[](int i) const { return at(i); } + Q_DECL_CONSTEXPR QLatin1Char front() const Q_REQUIRED_RESULT { return at(0); } + Q_DECL_CONSTEXPR QLatin1Char back() const Q_REQUIRED_RESULT { return at(size() - 1); } + using value_type = const char; using reference = value_type&; using const_reference = reference; @@ -302,6 +305,11 @@ public: const QChar operator[](uint i) const; QCharRef operator[](uint i); + inline QChar front() const Q_REQUIRED_RESULT { return at(0); } + inline QCharRef front() Q_REQUIRED_RESULT; + inline QChar back() const Q_REQUIRED_RESULT { return at(size() - 1); } + inline QCharRef back() Q_REQUIRED_RESULT; + QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar = QLatin1Char(' ')) const Q_REQUIRED_RESULT; QString arg(qulonglong a, int fieldwidth=0, int base=10, @@ -1160,6 +1168,8 @@ inline QCharRef QString::operator[](int i) { Q_ASSERT(i >= 0); return QCharRef(*this, i); } inline QCharRef QString::operator[](uint i) { return QCharRef(*this, i); } +inline QCharRef QString::front() { return operator[](0); } +inline QCharRef QString::back() { return operator[](size() - 1); } inline QString::iterator QString::begin() { detach(); return reinterpret_cast<QChar*>(d->data()); } inline QString::const_iterator QString::begin() const @@ -1544,6 +1554,8 @@ 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); } + QChar front() const Q_REQUIRED_RESULT { return at(0); } + QChar back() const Q_REQUIRED_RESULT { return at(size() - 1); } #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) // ASCII compatibility |