From ec846146b443bfb75a86023e2235ab7851dfd67c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Fri, 19 Jul 2013 12:27:56 +0200 Subject: New QStringRef methods. New functions left, right, mid were missing in the api. Change-Id: I3590a84431555d009d5012b204c111385bdceed3 Reviewed-by: Thiago Macieira --- src/corelib/tools/qstring.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++- src/corelib/tools/qstring.h | 4 +++ 2 files changed, 76 insertions(+), 1 deletion(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 936a5d68fa..b9b261e27c 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -8476,6 +8476,25 @@ QString &QString::append(const QStringRef &str) return *this; } +/*! + \fn QStringRef::left(int n) const + \since 5.2 + + Returns a substring reference to the \a n leftmost characters + of the string. + + If \a n is greater than size() or less than zero, a reference to the entire + string is returned. + + \sa right(), mid(), startsWith() +*/ +QStringRef QStringRef::left(int n) const +{ + if (n >= m_size || n < 0) + return *this; + return QStringRef(m_string, m_position, n); +} + /*! \since 4.4 @@ -8496,6 +8515,25 @@ QStringRef QString::leftRef(int n) const return QStringRef(this, 0, n); } +/*! + \fn QStringRef::right(int n) const + \since 5.2 + + Returns a substring reference to the \a n rightmost characters + of the string. + + If \a n is greater than size() or less than zero, a reference to the entire + string is returned. + + \sa left(), mid(), endsWith() +*/ +QStringRef QStringRef::right(int n) const +{ + if (n >= m_size || n < 0) + return *this; + return QStringRef(m_string, n + m_position, m_size - n); +} + /*! \since 4.4 @@ -8516,6 +8554,40 @@ QStringRef QString::rightRef(int n) const return QStringRef(this, d->size - n, n); } +/*! + \fn QStringRef::mid(int position, int n = -1) const + \since 5.2 + + Returns a substring reference to \a n characters of this string, + starting at the specified \a position. + + If the \a position exceeds the length of the string, a null + reference is returned. + + If there are less than \a n characters available in the string, + starting at the given \a position, or if \a n is -1 (default), the + function returns all characters from the specified \a position + onwards. + + \sa left(), right() +*/ +QStringRef QStringRef::mid(int pos, int n) const +{ + if (pos > m_size) + return QStringRef(); + if (pos < 0) { + if (n < 0 || n + pos >= m_size) + return QStringRef(m_string, m_position, m_size); + if (n + pos <= 0) + return QStringRef(); + n += pos; + pos = 0; + } else if (n < 0 || n > m_size - pos) { + n = m_size - pos; + } + return QStringRef(m_string, pos + m_position, n); +} + /*! \since 4.4 @@ -8536,7 +8608,6 @@ QStringRef QString::rightRef(int n) const \sa mid(), leftRef(), rightRef() */ - QStringRef QString::midRef(int position, int n) const { if (position > d->size) diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 7010a954b4..d20b3e227d 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -1226,6 +1226,10 @@ public: int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + QStringRef left(int n) const Q_REQUIRED_RESULT; + QStringRef right(int n) const Q_REQUIRED_RESULT; + QStringRef mid(int pos, int n = -1) const Q_REQUIRED_RESULT; + bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; -- cgit v1.2.3