summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Kudryavtsev <antkudr@mail.ru>2016-11-13 16:19:55 +0300
committerAnton Kudryavtsev <antkudr@mail.ru>2016-11-28 13:18:45 +0000
commit7eb4be9db89d40ea2cc090e7a562bdd588708607 (patch)
tree63bf6d7c18ab0d1685f985aa129af4722716abac
parent49cdf51ac97d12749e770d792124b7f06d5fd9ca (diff)
QStringRef: de-duplicate lastIndexOf code
Change-Id: Id6d804b2ab4c9c763d7ec9cb66c255ed0b4f785d Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/tools/qstring.cpp77
1 files changed, 40 insertions, 37 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index 4262899e02..eef375fe72 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -3260,6 +3260,23 @@ static int lastIndexOfHelper(const ushort *haystack, int from, const ushort *nee
return -1;
}
+static inline int lastIndexOfHelper(
+ const QStringRef &haystack, int from, const QStringRef &needle, Qt::CaseSensitivity cs)
+{
+ return lastIndexOfHelper(reinterpret_cast<const ushort*>(haystack.unicode()), from,
+ reinterpret_cast<const ushort*>(needle.unicode()), needle.size(), cs);
+}
+
+static inline int lastIndexOfHelper(
+ const QStringRef &haystack, int from, QLatin1String needle, Qt::CaseSensitivity cs)
+{
+ const int size = needle.size();
+ QVarLengthArray<ushort> s(size);
+ qt_from_latin1(s.data(), needle.latin1(), size);
+ return lastIndexOfHelper(reinterpret_cast<const ushort*>(haystack.unicode()), from,
+ s.data(), size, cs);
+}
+
/*!
Returns the index position of the last occurrence of the string \a
str in this string, searching backward from index position \a
@@ -9816,6 +9833,27 @@ int QStringRef::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
return qt_last_index_of(unicode(), size(), ch, from, cs);
}
+template<typename T>
+static int last_index_of_impl(const QStringRef &haystack, int from, const T &needle, Qt::CaseSensitivity cs)
+{
+ const int sl = needle.size();
+ if (sl == 1)
+ return haystack.lastIndexOf(needle.at(0), from, cs);
+
+ const int l = haystack.size();
+ if (from < 0)
+ from += l;
+ int delta = l - sl;
+ if (from == l && sl == 0)
+ return from;
+ if (uint(from) >= uint(l) || delta < 0)
+ return -1;
+ if (from > delta)
+ from = delta;
+
+ return lastIndexOfHelper(haystack, from, needle, cs);
+}
+
/*!
\since 4.8
\overload lastIndexOf()
@@ -9833,25 +9871,7 @@ int QStringRef::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
*/
int QStringRef::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const
{
- const int sl = str.size();
- if (sl == 1)
- return lastIndexOf(str.at(0), from, cs);
-
- const int l = size();
- if (from < 0)
- from += l;
- int delta = l - sl;
- if (from == l && sl == 0)
- return from;
- if (uint(from) >= uint(l) || delta < 0)
- return -1;
- if (from > delta)
- from = delta;
-
- QVarLengthArray<ushort> s(sl);
- qt_from_latin1(s.data(), str.latin1(), sl);
-
- return lastIndexOfHelper(reinterpret_cast<const ushort*>(unicode()), from, s.data(), sl, cs);
+ return last_index_of_impl(*this, from, str, cs);
}
/*!
@@ -9871,24 +9891,7 @@ int QStringRef::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs)
*/
int QStringRef::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const
{
- const int sl = str.size();
- if (sl == 1)
- return lastIndexOf(str.at(0), from, cs);
-
- const int l = size();
- if (from < 0)
- from += l;
- int delta = l - sl;
- if (from == l && sl == 0)
- return from;
- if (uint(from) >= uint(l) || delta < 0)
- return -1;
- if (from > delta)
- from = delta;
-
- return lastIndexOfHelper(reinterpret_cast<const ushort*>(unicode()), from,
- reinterpret_cast<const ushort*>(str.unicode()),
- str.size(), cs);
+ return last_index_of_impl(*this, from, str, cs);
}
/*!