14 const char16_t *uc = needle.utf16();
16 cs == Qt::CaseSensitive ? needle.size() : qMin(needle.size(), FoldBufferCapacity);
17 int l = qMin(
int(len), 255);
18 memset(skiptable, l, 256 *
sizeof(uchar));
20 if (cs == Qt::CaseSensitive) {
22 skiptable[*uc & 0xff] = l;
26 const char16_t *start = uc;
28 skiptable[foldCase(uc, start) & 0xff] = l;
34static inline qsizetype bm_find(QStringView haystack, qsizetype index, QStringView needle,
35 const uchar *skiptable, Qt::CaseSensitivity cs)
37 const char16_t *uc = haystack.utf16();
38 const qsizetype l = haystack.size();
39 const char16_t *puc = needle.utf16();
40 const qsizetype pl = needle.size();
43 return index > l ? -1 : index;
45 if (cs == Qt::CaseSensitive) {
46 const qsizetype pl_minus_one = pl - 1;
47 const char16_t *current = uc + index + pl_minus_one;
48 const char16_t *end = uc + l;
50 while (current < end) {
51 qsizetype skip = skiptable[*current & 0xff];
55 if (*(current - skip) != puc[pl_minus_one-skip])
59 if (skip > pl_minus_one)
60 return (current - uc) - pl_minus_one;
64 if (skiptable[*(current - skip) & 0xff] == pl)
69 if (current > end - skip)
74 char16_t foldBuffer[FoldBufferCapacity];
75 const qsizetype foldBufferLength = qMin(FoldBufferCapacity, pl);
76 const char16_t *start = puc;
77 for (qsizetype i = 0; i < foldBufferLength; ++i)
78 foldBuffer[i] = foldCase(&puc[i], start);
79 QStringView restNeedle = needle.sliced(foldBufferLength);
80 const qsizetype foldBufferEnd = foldBufferLength - 1;
81 const char16_t *current = uc + index + foldBufferEnd;
82 const char16_t *end = uc + l;
84 while (current < end) {
85 qsizetype skip = skiptable[foldCase(current, uc) & 0xff];
88 while (skip < foldBufferLength) {
89 if (foldCase(current - skip, uc) != foldBuffer[foldBufferEnd - skip])
93 if (skip > foldBufferEnd) {
94 qsizetype candidatePos = (current - uc) - foldBufferEnd;
95 QStringView restHaystack =
96 haystack.sliced(qMin(haystack.size(), candidatePos + foldBufferLength));
97 if (restNeedle.size() == 0
98 || restHaystack.startsWith(
99 restNeedle, Qt::CaseInsensitive))
104 if (skiptable[foldCase(current - skip, uc) & 0xff] == foldBufferLength)
105 skip = foldBufferLength - skip;
109 if (current > end - skip)
324 QStringView haystack, qsizetype haystackOffset,
325 QStringView needle, Qt::CaseSensitivity cs)
327 uchar skiptable[256];
328 bm_init_skiptable(needle, skiptable, cs);
329 if (haystackOffset < 0)
331 return bm_find(haystack, haystackOffset, needle, skiptable, cs);
static qsizetype bm_find(QStringView haystack, qsizetype index, QStringView needle, const uchar *skiptable, Qt::CaseSensitivity cs)