13 const char16_t *uc = needle.utf16();
15 cs == Qt::CaseSensitive ? needle.size() : qMin(needle.size(), FoldBufferCapacity);
16 int l = qMin(
int(len), 255);
17 memset(skiptable, l, 256 *
sizeof(uchar));
19 if (cs == Qt::CaseSensitive) {
21 skiptable[*uc & 0xff] = l;
25 const char16_t *start = uc;
27 skiptable[foldCase(uc, start) & 0xff] = l;
33static inline qsizetype bm_find(QStringView haystack, qsizetype index, QStringView needle,
34 const uchar *skiptable, Qt::CaseSensitivity cs)
36 const char16_t *uc = haystack.utf16();
37 const qsizetype l = haystack.size();
38 const char16_t *puc = needle.utf16();
39 const qsizetype pl = needle.size();
42 return index > l ? -1 : index;
44 if (cs == Qt::CaseSensitive) {
45 const qsizetype pl_minus_one = pl - 1;
46 const char16_t *current = uc + index + pl_minus_one;
47 const char16_t *end = uc + l;
49 while (current < end) {
50 qsizetype skip = skiptable[*current & 0xff];
54 if (*(current - skip) != puc[pl_minus_one-skip])
58 if (skip > pl_minus_one)
59 return (current - uc) - pl_minus_one;
63 if (skiptable[*(current - skip) & 0xff] == pl)
68 if (current > end - skip)
73 char16_t foldBuffer[FoldBufferCapacity];
74 const qsizetype foldBufferLength = qMin(FoldBufferCapacity, pl);
75 const char16_t *start = puc;
76 for (qsizetype i = 0; i < foldBufferLength; ++i)
77 foldBuffer[i] = foldCase(&puc[i], start);
78 QStringView restNeedle = needle.sliced(foldBufferLength);
79 const qsizetype foldBufferEnd = foldBufferLength - 1;
80 const char16_t *current = uc + index + foldBufferEnd;
81 const char16_t *end = uc + l;
83 while (current < end) {
84 qsizetype skip = skiptable[foldCase(current, uc) & 0xff];
87 while (skip < foldBufferLength) {
88 if (foldCase(current - skip, uc) != foldBuffer[foldBufferEnd - skip])
92 if (skip > foldBufferEnd) {
93 qsizetype candidatePos = (current - uc) - foldBufferEnd;
94 QStringView restHaystack =
95 haystack.sliced(qMin(haystack.size(), candidatePos + foldBufferLength));
96 if (restNeedle.size() == 0
97 || restHaystack.startsWith(
98 restNeedle, Qt::CaseInsensitive))
103 if (skiptable[foldCase(current - skip, uc) & 0xff] == foldBufferLength)
104 skip = foldBufferLength - skip;
108 if (current > end - skip)
323 QStringView haystack, qsizetype haystackOffset,
324 QStringView needle, Qt::CaseSensitivity cs)
326 uchar skiptable[256];
327 bm_init_skiptable(needle, skiptable, cs);
328 if (haystackOffset < 0)
330 return bm_find(haystack, haystackOffset, needle, skiptable, cs);
static qsizetype bm_find(QStringView haystack, qsizetype index, QStringView needle, const uchar *skiptable, Qt::CaseSensitivity cs)