summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstringmatcher.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/text/qstringmatcher.cpp')
-rw-r--r--src/corelib/text/qstringmatcher.cpp139
1 files changed, 59 insertions, 80 deletions
diff --git a/src/corelib/text/qstringmatcher.cpp b/src/corelib/text/qstringmatcher.cpp
index 449f475e66..379d555e54 100644
--- a/src/corelib/text/qstringmatcher.cpp
+++ b/src/corelib/text/qstringmatcher.cpp
@@ -1,52 +1,19 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Copyright (C) 2019 Mail.ru Group.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2020 The Qt Company Ltd.
+// Copyright (C) 2019 Mail.ru Group.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qstringmatcher.h"
QT_BEGIN_NAMESPACE
+static constexpr qsizetype FoldBufferCapacity = 256;
+
static void bm_init_skiptable(QStringView needle, uchar *skiptable, Qt::CaseSensitivity cs)
{
const char16_t *uc = needle.utf16();
- const qsizetype len = needle.size();
- int l = int(qMin(len, qsizetype(255)));
+ const qsizetype len =
+ cs == Qt::CaseSensitive ? needle.size() : qMin(needle.size(), FoldBufferCapacity);
+ int l = qMin(int(len), 255);
memset(skiptable, l, 256 * sizeof(uchar));
uc += len - l;
if (cs == Qt::CaseSensitive) {
@@ -73,11 +40,12 @@ static inline qsizetype bm_find(QStringView haystack, qsizetype index, QStringVi
if (pl == 0)
return index > l ? -1 : index;
- const qsizetype pl_minus_one = pl - 1;
- const char16_t *current = uc + index + pl_minus_one;
- const char16_t *end = uc + l;
if (cs == Qt::CaseSensitive) {
+ const qsizetype pl_minus_one = pl - 1;
+ const char16_t *current = uc + index + pl_minus_one;
+ const char16_t *end = uc + l;
+
while (current < end) {
qsizetype skip = skiptable[*current & 0xff];
if (!skip) {
@@ -102,21 +70,38 @@ static inline qsizetype bm_find(QStringView haystack, qsizetype index, QStringVi
current += skip;
}
} else {
+ char16_t foldBuffer[FoldBufferCapacity];
+ const qsizetype foldBufferLength = qMin(FoldBufferCapacity, pl);
+ const char16_t *start = puc;
+ for (qsizetype i = 0; i < foldBufferLength; ++i)
+ foldBuffer[i] = foldCase(&puc[i], start);
+ QStringView restNeedle = needle.sliced(foldBufferLength);
+ const qsizetype foldBufferEnd = foldBufferLength - 1;
+ const char16_t *current = uc + index + foldBufferEnd;
+ const char16_t *end = uc + l;
+
while (current < end) {
qsizetype skip = skiptable[foldCase(current, uc) & 0xff];
if (!skip) {
// possible match
- while (skip < pl) {
- if (foldCase(current - skip, uc) != foldCase(puc + pl_minus_one - skip, puc))
+ while (skip < foldBufferLength) {
+ if (foldCase(current - skip, uc) != foldBuffer[foldBufferEnd - skip])
break;
++skip;
}
- if (skip > pl_minus_one) // we have a match
- return (current - uc) - pl_minus_one;
+ if (skip > foldBufferEnd) { // Matching foldBuffer
+ qsizetype candidatePos = (current - uc) - foldBufferEnd;
+ QStringView restHaystack =
+ haystack.sliced(qMin(haystack.size(), candidatePos + foldBufferLength));
+ if (restNeedle.size() == 0
+ || restHaystack.startsWith(
+ restNeedle, Qt::CaseInsensitive)) // Check the rest of the string
+ return candidatePos;
+ }
// in case we don't have a match we are a bit inefficient as we only skip by one
// when we have the non matching char in the string.
- if (skiptable[foldCase(current - skip, uc) & 0xff] == pl)
- skip = pl - skip;
+ if (skiptable[foldCase(current - skip, uc) & 0xff] == foldBufferLength)
+ skip = foldBufferLength - skip;
else
skip = 1;
}
@@ -130,7 +115,7 @@ static inline qsizetype bm_find(QStringView haystack, qsizetype index, QStringVi
void QStringMatcher::updateSkipTable()
{
- bm_init_skiptable(p.sv, p.q_skiptable, q_cs);
+ bm_init_skiptable(q_sv, q_skiptable, q_cs);
}
/*!
@@ -156,15 +141,11 @@ void QStringMatcher::updateSkipTable()
\sa QString, QByteArrayMatcher, QRegularExpression
*/
-/*!
+/*! \fn QStringMatcher::QStringMatcher()
+
Constructs an empty string matcher that won't match anything.
Call setPattern() to give it a pattern to match.
*/
-QStringMatcher::QStringMatcher()
- : d_ptr(nullptr), q_cs(Qt::CaseSensitive)
-{
- memset(q_data, 0, sizeof(q_data));
-}
/*!
Constructs a string matcher that will search for \a pattern, with
@@ -173,23 +154,19 @@ QStringMatcher::QStringMatcher()
Call indexIn() to perform a search.
*/
QStringMatcher::QStringMatcher(const QString &pattern, Qt::CaseSensitivity cs)
- : d_ptr(nullptr), q_pattern(pattern), q_cs(cs)
+ : d_ptr(nullptr), q_cs(cs), q_pattern(pattern)
{
- p.sv = q_pattern;
+ q_sv = q_pattern;
updateSkipTable();
}
/*!
- \fn QStringMatcher::QStringMatcher(const QChar *uc, int length, Qt::CaseSensitivity cs)
+ \fn QStringMatcher::QStringMatcher(const QChar *uc, qsizetype length, Qt::CaseSensitivity cs)
\since 4.5
Constructs a string matcher that will search for the pattern referred to
by \a uc with the given \a length and case sensitivity specified by \a cs.
*/
-QStringMatcher::QStringMatcher(const QChar *uc, int len, Qt::CaseSensitivity cs)
- : QStringMatcher(QStringView(uc, len), cs)
-{
-}
/*!
\fn QStringMatcher::QStringMatcher(QStringView pattern, Qt::CaseSensitivity cs = Qt::CaseSensitive)
@@ -201,9 +178,8 @@ QStringMatcher::QStringMatcher(const QChar *uc, int len, Qt::CaseSensitivity cs)
Call indexIn() to perform a search.
*/
QStringMatcher::QStringMatcher(QStringView str, Qt::CaseSensitivity cs)
- : d_ptr(nullptr), q_cs(cs)
+ : d_ptr(nullptr), q_cs(cs), q_sv(str)
{
- p.sv = str;
updateSkipTable();
}
/*!
@@ -231,7 +207,8 @@ QStringMatcher &QStringMatcher::operator=(const QStringMatcher &other)
if (this != &other) {
q_pattern = other.q_pattern;
q_cs = other.q_cs;
- memcpy(q_data, other.q_data, sizeof(q_data));
+ q_sv = other.q_sv;
+ memcpy(q_skiptable, other.q_skiptable, sizeof(q_skiptable));
}
return *this;
}
@@ -245,7 +222,7 @@ QStringMatcher &QStringMatcher::operator=(const QStringMatcher &other)
void QStringMatcher::setPattern(const QString &pattern)
{
q_pattern = pattern;
- p.sv = q_pattern;
+ q_sv = q_pattern;
updateSkipTable();
}
@@ -262,10 +239,19 @@ QString QStringMatcher::pattern() const
{
if (!q_pattern.isEmpty())
return q_pattern;
- return p.sv.toString();
+ return q_sv.toString();
}
/*!
+ \fn QStringView QStringMatcher::patternView() const noexcept
+ \since 6.7
+
+ Returns a string view of the pattern that this string matcher will search for.
+
+ \sa setPattern()
+*/
+
+/*!
Sets the case sensitivity setting of this string matcher to \a
cs.
@@ -279,7 +265,8 @@ void QStringMatcher::setCaseSensitivity(Qt::CaseSensitivity cs)
updateSkipTable();
}
-/*!
+/*! \fn qsizetype QStringMatcher::indexIn(const QString &str, qsizetype from) const
+
Searches the string \a str from character position \a from
(default 0, i.e. from the first character), for the string
pattern() that was set in the constructor or in the most recent
@@ -288,12 +275,8 @@ void QStringMatcher::setCaseSensitivity(Qt::CaseSensitivity cs)
\sa setPattern(), setCaseSensitivity()
*/
-int QStringMatcher::indexIn(const QString &str, qsizetype from) const
-{
- return int(indexIn(QStringView(str), from));
-}
-/*!
+/*! \fn qsizetype QStringMatcher::indexIn(const QChar *str, qsizetype length, qsizetype from) const
\since 4.5
Searches the string starting at \a str (of length \a length) from
@@ -305,10 +288,6 @@ int QStringMatcher::indexIn(const QString &str, qsizetype from) const
\sa setPattern(), setCaseSensitivity()
*/
-int QStringMatcher::indexIn(const QChar *str, qsizetype length, qsizetype from) const
-{
- return int(indexIn(QStringView(str, length), from));
-}
/*!
\since 5.14
@@ -325,7 +304,7 @@ qsizetype QStringMatcher::indexIn(QStringView str, qsizetype from) const
{
if (from < 0)
from = 0;
- return bm_find(str, from, p.sv, p.q_skiptable, q_cs);
+ return bm_find(str, from, q_sv, q_skiptable, q_cs);
}
/*!