From f6fa78fd600d0b78d279993f02d86b14aeb6fc22 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 28 Sep 2021 18:58:25 -0700 Subject: 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 --- src/corelib/text/qbytearraymatcher.cpp | 26 +++++++++++++++++++++++++- src/corelib/text/qbytearraymatcher.h | 9 ++++++++- 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(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); } @@ -147,6 +150,15 @@ QByteArrayMatcher::QByteArrayMatcher(const QByteArray &pattern) bm_init_skiptable(p.p, p.l, p.q_skiptable); } +/*! + \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. */ @@ -218,6 +230,18 @@ qsizetype QByteArrayMatcher::indexIn(const char *str, qsizetype len, qsizetype f p.p, p.l, p.q_skiptable); } +/*! + \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 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()) -- cgit v1.2.3