summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJędrzej Nowacki <jedrzej.nowacki@digia.com>2013-07-19 12:27:56 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-09-11 08:06:21 +0200
commitec846146b443bfb75a86023e2235ab7851dfd67c (patch)
treef6129de85bce932cabdb2335db1a99d5c0669bd4 /src
parentaba336c2b4ad8926dc8a000718bbb7f8a6d5a72d (diff)
New QStringRef methods.
New functions left, right, mid were missing in the api. Change-Id: I3590a84431555d009d5012b204c111385bdceed3 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/tools/qstring.cpp73
-rw-r--r--src/corelib/tools/qstring.h4
2 files changed, 76 insertions, 1 deletions
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
@@ -8477,6 +8477,25 @@ QString &QString::append(const QStringRef &str)
}
/*!
+ \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
Returns a substring reference to the \a n leftmost characters
@@ -8497,6 +8516,25 @@ QStringRef QString::leftRef(int n) const
}
/*!
+ \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
Returns a substring reference to the \a n rightmost characters
@@ -8517,6 +8555,40 @@ QStringRef QString::rightRef(int n) const
}
/*!
+ \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
Returns a substring reference to \a n characters of this string,
@@ -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;