summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2021-09-28 18:58:25 -0700
committerThiago Macieira <thiago.macieira@intel.com>2021-10-01 14:23:15 -0700
commitf6fa78fd600d0b78d279993f02d86b14aeb6fc22 (patch)
tree2dda0d730ab724d67fc2308dccc57c2a941301d3
parent90c54b02f83961b1c66f10fe57bcfcf4523ebba1 (diff)
QByteArrayMatcher: add QByteArrayView overloads
The modification to the existing constructor was necessary to avoid ambiguous overloads from plain strings. They'd previously only match QByteArray, but now they match QByteArrayView too. The QByteArray constructor must stay even in Qt 7 because the pattern is stored. The QByteArray indexIn also stays in because of ambiguous overloads. Unless we want to remove the const char* overloads. Change-Id: I2bbf422288924c198645fffd16a92859b67f3978 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
-rw-r--r--src/corelib/text/qbytearraymatcher.cpp26
-rw-r--r--src/corelib/text/qbytearraymatcher.h9
2 files changed, 33 insertions, 2 deletions
diff --git a/src/corelib/text/qbytearraymatcher.cpp b/src/corelib/text/qbytearraymatcher.cpp
index 4292064de2..ca1791c18b 100644
--- a/src/corelib/text/qbytearraymatcher.cpp
+++ b/src/corelib/text/qbytearraymatcher.cpp
@@ -131,7 +131,10 @@ QByteArrayMatcher::QByteArrayMatcher()
QByteArrayMatcher::QByteArrayMatcher(const char *pattern, qsizetype length) : d(nullptr)
{
p.p = reinterpret_cast<const uchar *>(pattern);
- p.l = length;
+ if (length < 0)
+ p.l = qstrlen(pattern);
+ else
+ p.l = length;
bm_init_skiptable(p.p, p.l, p.q_skiptable);
}
@@ -148,6 +151,15 @@ QByteArrayMatcher::QByteArrayMatcher(const QByteArray &pattern)
}
/*!
+ \fn QByteArrayMatcher::QByteArrayMatcher(QByteArrayView pattern)
+ \since 6.3
+ \overload
+
+ Constructs a byte array matcher that will search for \a pattern.
+ Call indexIn() to perform a search.
+*/
+
+/*!
Copies the \a other byte array matcher to this byte array matcher.
*/
QByteArrayMatcher::QByteArrayMatcher(const QByteArrayMatcher &other)
@@ -219,6 +231,18 @@ qsizetype QByteArrayMatcher::indexIn(const char *str, qsizetype len, qsizetype f
}
/*!
+ \fn qsizetype QByteArrayMatcher::indexIn(QByteArrayView data, qsizetype from) const
+ \since 6.3
+ \overload
+
+ Searches the byte array \a view, from byte position \a from (default
+ 0, i.e. from the first byte), for the byte array pattern() that
+ was set in the constructor or in the most recent call to
+ setPattern(). Returns the position where the pattern() matched in
+ \a data, or -1 if no match was found.
+*/
+
+/*!
\fn QByteArray QByteArrayMatcher::pattern() const
Returns the byte array pattern that this byte array matcher will
diff --git a/src/corelib/text/qbytearraymatcher.h b/src/corelib/text/qbytearraymatcher.h
index db6c06128c..d9ea638500 100644
--- a/src/corelib/text/qbytearraymatcher.h
+++ b/src/corelib/text/qbytearraymatcher.h
@@ -54,7 +54,10 @@ class Q_CORE_EXPORT QByteArrayMatcher
public:
QByteArrayMatcher();
explicit QByteArrayMatcher(const QByteArray &pattern);
- explicit QByteArrayMatcher(const char *pattern, qsizetype length);
+ explicit QByteArrayMatcher(QByteArrayView pattern)
+ : QByteArrayMatcher(pattern.data(), pattern.size())
+ {}
+ explicit QByteArrayMatcher(const char *pattern, qsizetype length = -1);
QByteArrayMatcher(const QByteArrayMatcher &other);
~QByteArrayMatcher();
@@ -64,6 +67,10 @@ public:
qsizetype indexIn(const QByteArray &ba, qsizetype from = 0) const;
qsizetype indexIn(const char *str, qsizetype len, qsizetype from = 0) const;
+ qsizetype indexIn(QByteArrayView data, qsizetype from = 0) const
+ {
+ return indexIn(data.data(), data.size(), from);
+ }
inline QByteArray pattern() const
{
if (q_pattern.isNull())