summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qlatin1stringmatcher.cpp
blob: 68bf97db5c3dbf887e2892d4705c187475e3c8e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qlatin1stringmatcher.h"
#include <limits.h>

QT_BEGIN_NAMESPACE

/*! \class QLatin1StringMatcher
    \inmodule QtCore
    \brief Optimized search for substring in Latin-1 text.

    A QLatin1StringMatcher can search for one QLatin1StringView
    as a substring of another, either ignoring case or taking it into
    account.

    \since 6.5
    \ingroup tools
    \ingroup string-processing

    This class is useful when you have a Latin-1 encoded string that
    you want to repeatedly search for in some QLatin1StringViews
    (perhaps in a loop), or when you want to search for all
    instances of it in a given QLatin1StringView. Using a matcher
    object and indexIn() is faster than matching a plain
    QLatin1StringView with QLatin1StringView::indexOf() if repeated
    matching takes place. This class offers no benefit if you are
    doing one-off matches. The string to be searched for must not
    be destroyed or changed before the matcher object is destroyed,
    as the matcher accesses the string when searching for it.

    Create a QLatin1StringMatcher for the QLatin1StringView
    you want to search for and the case sensitivity. Then call
    indexIn() with the QLatin1StringView that you want to search
    within.

    \sa QLatin1StringView, QStringMatcher, QByteArrayMatcher
*/

/*!
    Construct an empty Latin-1 string matcher.
    This will match at each position in any string.
    \sa setPattern(), setCaseSensitivity(), indexIn()
*/
QLatin1StringMatcher::QLatin1StringMatcher() noexcept
    : m_pattern(),
      m_cs(Qt::CaseSensitive),
      m_caseSensitiveSearcher(m_pattern.data(), m_pattern.data())
{
}

/*!
    Constructs a Latin-1 string matcher that searches for the given \a pattern
    with given case sensitivity \a cs. The \a pattern argument must
    not be destroyed before this matcher object. Call indexIn()
    to find the \a pattern in the given QLatin1StringView.
*/
QLatin1StringMatcher::QLatin1StringMatcher(QLatin1StringView pattern,
                                           Qt::CaseSensitivity cs) noexcept
    : m_pattern(pattern), m_cs(cs)
{
    setSearcher();
}

/*!
    Destroys the Latin-1 string matcher.
*/
QLatin1StringMatcher::~QLatin1StringMatcher() noexcept
{
    freeSearcher();
}

/*!
    \internal
*/
void QLatin1StringMatcher::setSearcher() noexcept
{
    if (m_cs == Qt::CaseSensitive) {
        new (&m_caseSensitiveSearcher) CaseSensitiveSearcher(m_pattern.data(), m_pattern.end());
    } else {
        QtPrivate::QCaseInsensitiveLatin1Hash foldCase;
        qsizetype bufferSize = std::min(m_pattern.size(), qsizetype(sizeof m_foldBuffer));
        for (qsizetype i = 0; i < bufferSize; ++i)
            m_foldBuffer[i] = static_cast<char>(foldCase(m_pattern[i].toLatin1()));

        new (&m_caseInsensitiveSearcher)
                CaseInsensitiveSearcher(m_foldBuffer, &m_foldBuffer[bufferSize]);
    }
}

/*!
    \internal
*/
void QLatin1StringMatcher::freeSearcher() noexcept
{
    if (m_cs == Qt::CaseSensitive)
        m_caseSensitiveSearcher.~CaseSensitiveSearcher();
    else
        m_caseInsensitiveSearcher.~CaseInsensitiveSearcher();
}

/*!
    Sets the \a pattern to search for. The string pointed to by the
    QLatin1StringView must not be destroyed before the matcher is
    destroyed, unless it is set to point to a different \a pattern
    with longer lifetime first.

    \sa pattern(), indexIn()
*/
void QLatin1StringMatcher::setPattern(QLatin1StringView pattern) noexcept
{
    if (m_pattern.latin1() == pattern.latin1() && m_pattern.size() == pattern.size())
        return; // Same address and size

    freeSearcher();
    m_pattern = pattern;
    setSearcher();
}

/*!
    Returns the Latin-1 pattern that the matcher searches for.

    \sa setPattern(), indexIn()
*/
QLatin1StringView QLatin1StringMatcher::pattern() const noexcept
{
    return m_pattern;
}

/*!
    Sets the case sensitivity to \a cs.

    \sa caseSensitivity(), indexIn()
*/
void QLatin1StringMatcher::setCaseSensitivity(Qt::CaseSensitivity cs) noexcept
{
    if (m_cs == cs)
        return;

    freeSearcher();
    m_cs = cs;
    setSearcher();
}

/*!
    Returns the case sensitivity the matcher uses.

    \sa setCaseSensitivity(), indexIn()
*/
Qt::CaseSensitivity QLatin1StringMatcher::caseSensitivity() const noexcept
{
    return m_cs;
}

/*!
    Searches for the pattern in the given \a haystack starting from
    \a from.

    \sa caseSensitivity(), pattern()
*/
qsizetype QLatin1StringMatcher::indexIn(QLatin1StringView haystack, qsizetype from) const noexcept
{
    if (m_pattern.isEmpty() && from == haystack.size())
        return from;
    if (from < 0) // Historical behavior (see QString::indexOf and co.)
        from += haystack.size();
    if (from >= haystack.size())
        return -1;

    auto begin = haystack.begin() + from;
    auto end = haystack.end();
    auto found = begin;
    if (m_cs == Qt::CaseSensitive) {
        found = m_caseSensitiveSearcher(begin, end, m_pattern.begin(), m_pattern.end()).begin;
        if (found == end)
            return -1;
    } else {
        const qsizetype bufferSize = std::min(m_pattern.size(), qsizetype(sizeof m_foldBuffer));
        const QLatin1StringView restNeedle = m_pattern.sliced(bufferSize);
        const bool needleLongerThanBuffer = restNeedle.size() > 0;
        QLatin1StringView restHaystack = haystack;
        do {
            found = m_caseInsensitiveSearcher(found, end, m_foldBuffer, &m_foldBuffer[bufferSize])
                            .begin;
            if (found == end) {
                return -1;
            } else if (!needleLongerThanBuffer) {
                break;
            }
            restHaystack = haystack.sliced(
                    qMin(haystack.size(),
                         bufferSize + qsizetype(std::distance(haystack.begin(), found))));
            if (restHaystack.startsWith(restNeedle, Qt::CaseInsensitive))
                break;
            ++found;
        } while (true);
    }
    return std::distance(haystack.begin(), found);
}

QT_END_NAMESPACE