File indexing completed on 2025-01-18 10:07:37
0001
0002
0003
0004 #ifndef QSTATICLATIN1STRINGMATCHER_H
0005 #define QSTATICLATIN1STRINGMATCHER_H
0006
0007 #include <functional>
0008 #include <iterator>
0009 #include <limits>
0010
0011 #include <QtCore/q20algorithm.h>
0012 #include <QtCore/qlatin1stringmatcher.h>
0013 #include <QtCore/qstring.h>
0014
0015 QT_BEGIN_NAMESPACE
0016
0017 #ifdef Q_CC_GHS
0018 # define QT_STATIC_BOYER_MOORE_NOT_SUPPORTED
0019 #else
0020 namespace QtPrivate {
0021 template <class RandomIt1,
0022 class Hash = std::hash<typename std::iterator_traits<RandomIt1>::value_type>,
0023 class BinaryPredicate = std::equal_to<>>
0024 class q_boyer_moore_searcher
0025 {
0026 public:
0027 constexpr q_boyer_moore_searcher(RandomIt1 pat_first, RandomIt1 pat_last) : m_skiptable{}
0028 {
0029 const size_t n = std::distance(pat_first, pat_last);
0030 constexpr auto uchar_max = (std::numeric_limits<uchar>::max)();
0031 uchar max = n > uchar_max ? uchar_max : uchar(n);
0032 q20::fill(std::begin(m_skiptable), std::end(m_skiptable), max);
0033 Hash hf;
0034 RandomIt1 pattern = std::next(pat_first, n - max);
0035 while (max--)
0036 m_skiptable[hf(*pattern++)] = max;
0037 }
0038
0039 template <class RandomIt2>
0040 constexpr auto operator()(RandomIt2 first, RandomIt2 last, RandomIt1 pat_first,
0041 RandomIt1 pat_last) const
0042 {
0043 struct R
0044 {
0045 RandomIt2 begin, end;
0046 };
0047 Hash hf;
0048 BinaryPredicate pred;
0049 auto pat_length = std::distance(pat_first, pat_last);
0050 if (pat_length == 0)
0051 return R{ first, first };
0052
0053 auto haystack_length = std::distance(first, last);
0054 if (haystack_length < pat_length)
0055 return R{ last, last };
0056
0057 const qsizetype pl_minus_one = qsizetype(pat_length - 1);
0058 RandomIt2 current = first + pl_minus_one;
0059
0060 qsizetype skip = 0;
0061 while (current < last - skip) {
0062 current += skip;
0063 skip = m_skiptable[hf(*current)];
0064 if (!skip) {
0065
0066 while (skip < pat_length) {
0067 if (!pred(hf(*(current - skip)), hf(pat_first[pl_minus_one - skip])))
0068 break;
0069 skip++;
0070 }
0071 if (skip > pl_minus_one) {
0072 auto match = current + 1 - skip;
0073 return R{ match, match + pat_length };
0074 }
0075
0076
0077
0078 if (m_skiptable[hf(*(current - skip))] == pat_length)
0079 skip = pat_length - skip;
0080 else
0081 skip = 1;
0082 }
0083 }
0084
0085 return R{ last, last };
0086 }
0087
0088 private:
0089 alignas(16) uchar m_skiptable[256];
0090 };
0091 }
0092
0093 template <Qt::CaseSensitivity CS, size_t N>
0094 class QStaticLatin1StringMatcher
0095 {
0096 static_assert(N > 2,
0097 "QStaticLatin1StringMatcher makes no sense for finding a single-char pattern");
0098
0099 QLatin1StringView m_pattern;
0100 using Hasher = std::conditional_t<CS == Qt::CaseSensitive, QtPrivate::QCaseSensitiveLatin1Hash,
0101 QtPrivate::QCaseInsensitiveLatin1Hash>;
0102 QtPrivate::q_boyer_moore_searcher<const char *, Hasher> m_searcher;
0103
0104 public:
0105 explicit constexpr QStaticLatin1StringMatcher(QLatin1StringView patternToMatch) noexcept
0106 : m_pattern(patternToMatch),
0107 m_searcher(patternToMatch.begin(), patternToMatch.begin() + N - 1)
0108 {
0109 }
0110
0111 constexpr qsizetype indexIn(QLatin1StringView haystack, qsizetype from = 0) const noexcept
0112 {
0113 if (from >= haystack.size())
0114 return -1;
0115 const char *begin = haystack.begin() + from;
0116 const char *end = haystack.end();
0117 const auto r = m_searcher(begin, end, m_pattern.begin(), m_pattern.end());
0118 return r.begin == end ? -1 : std::distance(haystack.begin(), r.begin);
0119 }
0120 };
0121
0122 template <size_t N>
0123 constexpr auto qMakeStaticCaseSensitiveLatin1StringMatcher(const char (&patternToMatch)[N]) noexcept
0124 {
0125 return QStaticLatin1StringMatcher<Qt::CaseSensitive, N>(
0126 QLatin1StringView(patternToMatch, qsizetype(N) - 1));
0127 }
0128
0129 template <size_t N>
0130 constexpr auto
0131 qMakeStaticCaseInsensitiveLatin1StringMatcher(const char (&patternToMatch)[N]) noexcept
0132 {
0133 return QStaticLatin1StringMatcher<Qt::CaseInsensitive, N>(
0134 QLatin1StringView(patternToMatch, qsizetype(N) - 1));
0135 }
0136 #endif
0137
0138 QT_END_NAMESPACE
0139
0140 #endif