Coverage Report

Created: 2026-06-09 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/stringlib/fastsearch.h
Line
Count
Source
1
/* stringlib: fastsearch implementation */
2
3
#define STRINGLIB_FASTSEARCH_H
4
5
/* fast search/count implementation, based on a mix between boyer-
6
   moore and horspool, with a few more bells and whistles on the top.
7
   for some more background, see:
8
   https://web.archive.org/web/20201107074620/http://effbot.org/zone/stringlib.htm */
9
10
/* note: fastsearch may access s[n], which isn't a problem when using
11
   Python's ordinary string types, but may cause problems if you're
12
   using this code in other contexts.  also, the count mode returns -1
13
   if there cannot possibly be a match in the target string, and 0 if
14
   it has actually checked for matches, but didn't find any.  callers
15
   beware! */
16
17
/* If the strings are long enough, use Crochemore and Perrin's Two-Way
18
   algorithm, which has worst-case O(n) runtime and best-case O(n/k).
19
   Also compute a table of shifts to achieve O(n/k) in more cases,
20
   and often (data dependent) deduce larger shifts than pure C&P can
21
   deduce. See stringlib_find_two_way_notes.txt in this folder for a
22
   detailed explanation. */
23
24
280M
#define FAST_COUNT 0
25
113M
#define FAST_SEARCH 1
26
73.9M
#define FAST_RSEARCH 2
27
28
#if LONG_BIT >= 128
29
#define STRINGLIB_BLOOM_WIDTH 128
30
#elif LONG_BIT >= 64
31
1.15G
#define STRINGLIB_BLOOM_WIDTH 64
32
#elif LONG_BIT >= 32
33
#define STRINGLIB_BLOOM_WIDTH 32
34
#else
35
#error "LONG_BIT is smaller than 32"
36
#endif
37
38
#define STRINGLIB_BLOOM_ADD(mask, ch) \
39
215M
    ((mask |= (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
40
#define STRINGLIB_BLOOM(mask, ch)     \
41
935M
    ((mask &  (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
42
43
#ifdef STRINGLIB_FAST_MEMCHR
44
241M
#  define MEMCHR_CUT_OFF 15
45
#else
46
9.09M
#  define MEMCHR_CUT_OFF 40
47
#endif
48
49
Py_LOCAL_INLINE(Py_ssize_t)
50
STRINGLIB(find_char)(const STRINGLIB_CHAR* s, Py_ssize_t n, STRINGLIB_CHAR ch)
51
250M
{
52
250M
    const STRINGLIB_CHAR *p, *e;
53
54
250M
    p = s;
55
250M
    e = s + n;
56
250M
    if (n > MEMCHR_CUT_OFF) {
57
#ifdef STRINGLIB_FAST_MEMCHR
58
36.2M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
36.2M
        if (p != NULL)
60
34.8M
            return (p - s);
61
1.41M
        return -1;
62
#else
63
        /* use memchr if we can choose a needle without too many likely
64
           false positives */
65
        const STRINGLIB_CHAR *s1, *e1;
66
        unsigned char needle = ch & 0xff;
67
        /* If looking for a multiple of 256, we'd have too
68
           many false positives looking for the '\0' byte in UCS2
69
           and UCS4 representations. */
70
8.56M
        if (needle != 0) {
71
8.58M
            do {
72
8.58M
                const void *candidate = memchr(p, needle,
73
8.58M
                                               (e - p) * sizeof(STRINGLIB_CHAR));
74
8.58M
                if (candidate == NULL)
75
71.2k
                    return -1;
76
8.51M
                s1 = p;
77
8.51M
                p = (const STRINGLIB_CHAR *)
78
8.51M
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
8.51M
                if (*p == ch)
80
8.47M
                    return (p - s);
81
                /* False positive */
82
35.5k
                p++;
83
35.5k
                if (p - s1 > MEMCHR_CUT_OFF)
84
13.0k
                    continue;
85
22.5k
                if (e - p <= MEMCHR_CUT_OFF)
86
2.81k
                    break;
87
19.7k
                e1 = p + MEMCHR_CUT_OFF;
88
674k
                while (p != e1) {
89
659k
                    if (*p == ch)
90
3.92k
                        return (p - s);
91
655k
                    p++;
92
655k
                }
93
19.7k
            }
94
8.55M
            while (e - p > MEMCHR_CUT_OFF);
95
8.55M
        }
96
#endif
97
44.7M
    }
98
1.07G
    while (p < e) {
99
888M
        if (*p == ch)
100
21.2M
            return (p - s);
101
867M
        p++;
102
867M
    }
103
184M
    return -1;
104
205M
}
bytesobject.c:stringlib_find_char
Line
Count
Source
51
387k
{
52
387k
    const STRINGLIB_CHAR *p, *e;
53
54
387k
    p = s;
55
387k
    e = s + n;
56
387k
    if (n > MEMCHR_CUT_OFF) {
57
387k
#ifdef STRINGLIB_FAST_MEMCHR
58
387k
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
387k
        if (p != NULL)
60
385k
            return (p - s);
61
1.81k
        return -1;
62
#else
63
        /* use memchr if we can choose a needle without too many likely
64
           false positives */
65
        const STRINGLIB_CHAR *s1, *e1;
66
        unsigned char needle = ch & 0xff;
67
        /* If looking for a multiple of 256, we'd have too
68
           many false positives looking for the '\0' byte in UCS2
69
           and UCS4 representations. */
70
        if (needle != 0) {
71
            do {
72
                const void *candidate = memchr(p, needle,
73
                                               (e - p) * sizeof(STRINGLIB_CHAR));
74
                if (candidate == NULL)
75
                    return -1;
76
                s1 = p;
77
                p = (const STRINGLIB_CHAR *)
78
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
                if (*p == ch)
80
                    return (p - s);
81
                /* False positive */
82
                p++;
83
                if (p - s1 > MEMCHR_CUT_OFF)
84
                    continue;
85
                if (e - p <= MEMCHR_CUT_OFF)
86
                    break;
87
                e1 = p + MEMCHR_CUT_OFF;
88
                while (p != e1) {
89
                    if (*p == ch)
90
                        return (p - s);
91
                    p++;
92
                }
93
            }
94
            while (e - p > MEMCHR_CUT_OFF);
95
        }
96
#endif
97
387k
    }
98
84
    while (p < e) {
99
84
        if (*p == ch)
100
14
            return (p - s);
101
70
        p++;
102
70
    }
103
0
    return -1;
104
14
}
unicodeobject.c:ucs1lib_find_char
Line
Count
Source
51
214M
{
52
214M
    const STRINGLIB_CHAR *p, *e;
53
54
214M
    p = s;
55
214M
    e = s + n;
56
214M
    if (n > MEMCHR_CUT_OFF) {
57
20.5M
#ifdef STRINGLIB_FAST_MEMCHR
58
20.5M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
20.5M
        if (p != NULL)
60
19.6M
            return (p - s);
61
879k
        return -1;
62
#else
63
        /* use memchr if we can choose a needle without too many likely
64
           false positives */
65
        const STRINGLIB_CHAR *s1, *e1;
66
        unsigned char needle = ch & 0xff;
67
        /* If looking for a multiple of 256, we'd have too
68
           many false positives looking for the '\0' byte in UCS2
69
           and UCS4 representations. */
70
        if (needle != 0) {
71
            do {
72
                const void *candidate = memchr(p, needle,
73
                                               (e - p) * sizeof(STRINGLIB_CHAR));
74
                if (candidate == NULL)
75
                    return -1;
76
                s1 = p;
77
                p = (const STRINGLIB_CHAR *)
78
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
                if (*p == ch)
80
                    return (p - s);
81
                /* False positive */
82
                p++;
83
                if (p - s1 > MEMCHR_CUT_OFF)
84
                    continue;
85
                if (e - p <= MEMCHR_CUT_OFF)
86
                    break;
87
                e1 = p + MEMCHR_CUT_OFF;
88
                while (p != e1) {
89
                    if (*p == ch)
90
                        return (p - s);
91
                    p++;
92
                }
93
            }
94
            while (e - p > MEMCHR_CUT_OFF);
95
        }
96
#endif
97
20.5M
    }
98
980M
    while (p < e) {
99
800M
        if (*p == ch)
100
13.6M
            return (p - s);
101
787M
        p++;
102
787M
    }
103
180M
    return -1;
104
193M
}
unicodeobject.c:ucs2lib_find_char
Line
Count
Source
51
8.99M
{
52
8.99M
    const STRINGLIB_CHAR *p, *e;
53
54
8.99M
    p = s;
55
8.99M
    e = s + n;
56
8.99M
    if (n > MEMCHR_CUT_OFF) {
57
#ifdef STRINGLIB_FAST_MEMCHR
58
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
        if (p != NULL)
60
            return (p - s);
61
        return -1;
62
#else
63
        /* use memchr if we can choose a needle without too many likely
64
           false positives */
65
8.56M
        const STRINGLIB_CHAR *s1, *e1;
66
8.56M
        unsigned char needle = ch & 0xff;
67
        /* If looking for a multiple of 256, we'd have too
68
           many false positives looking for the '\0' byte in UCS2
69
           and UCS4 representations. */
70
8.56M
        if (needle != 0) {
71
8.58M
            do {
72
8.58M
                const void *candidate = memchr(p, needle,
73
8.58M
                                               (e - p) * sizeof(STRINGLIB_CHAR));
74
8.58M
                if (candidate == NULL)
75
71.2k
                    return -1;
76
8.51M
                s1 = p;
77
8.51M
                p = (const STRINGLIB_CHAR *)
78
8.51M
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
8.51M
                if (*p == ch)
80
8.47M
                    return (p - s);
81
                /* False positive */
82
35.5k
                p++;
83
35.5k
                if (p - s1 > MEMCHR_CUT_OFF)
84
13.0k
                    continue;
85
22.5k
                if (e - p <= MEMCHR_CUT_OFF)
86
2.81k
                    break;
87
19.7k
                e1 = p + MEMCHR_CUT_OFF;
88
674k
                while (p != e1) {
89
659k
                    if (*p == ch)
90
3.92k
                        return (p - s);
91
655k
                    p++;
92
655k
                }
93
19.7k
            }
94
8.55M
            while (e - p > MEMCHR_CUT_OFF);
95
8.55M
        }
96
8.56M
#endif
97
8.56M
    }
98
6.56M
    while (p < e) {
99
6.30M
        if (*p == ch)
100
188k
            return (p - s);
101
6.11M
        p++;
102
6.11M
    }
103
253k
    return -1;
104
441k
}
unicodeobject.c:ucs4lib_find_char
Line
Count
Source
51
6.26M
{
52
6.26M
    const STRINGLIB_CHAR *p, *e;
53
54
6.26M
    p = s;
55
6.26M
    e = s + n;
56
6.26M
    if (n > MEMCHR_CUT_OFF) {
57
6.21M
#ifdef STRINGLIB_FAST_MEMCHR
58
6.21M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
6.21M
        if (p != NULL)
60
6.19M
            return (p - s);
61
14.7k
        return -1;
62
#else
63
        /* use memchr if we can choose a needle without too many likely
64
           false positives */
65
        const STRINGLIB_CHAR *s1, *e1;
66
        unsigned char needle = ch & 0xff;
67
        /* If looking for a multiple of 256, we'd have too
68
           many false positives looking for the '\0' byte in UCS2
69
           and UCS4 representations. */
70
        if (needle != 0) {
71
            do {
72
                const void *candidate = memchr(p, needle,
73
                                               (e - p) * sizeof(STRINGLIB_CHAR));
74
                if (candidate == NULL)
75
                    return -1;
76
                s1 = p;
77
                p = (const STRINGLIB_CHAR *)
78
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
                if (*p == ch)
80
                    return (p - s);
81
                /* False positive */
82
                p++;
83
                if (p - s1 > MEMCHR_CUT_OFF)
84
                    continue;
85
                if (e - p <= MEMCHR_CUT_OFF)
86
                    break;
87
                e1 = p + MEMCHR_CUT_OFF;
88
                while (p != e1) {
89
                    if (*p == ch)
90
                        return (p - s);
91
                    p++;
92
                }
93
            }
94
            while (e - p > MEMCHR_CUT_OFF);
95
        }
96
#endif
97
6.21M
    }
98
198k
    while (p < e) {
99
166k
        if (*p == ch)
100
18.8k
            return (p - s);
101
147k
        p++;
102
147k
    }
103
31.3k
    return -1;
104
50.2k
}
unicodeobject.c:asciilib_find_char
Line
Count
Source
51
5.64M
{
52
5.64M
    const STRINGLIB_CHAR *p, *e;
53
54
5.64M
    p = s;
55
5.64M
    e = s + n;
56
5.64M
    if (n > MEMCHR_CUT_OFF) {
57
4.61M
#ifdef STRINGLIB_FAST_MEMCHR
58
4.61M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
4.61M
        if (p != NULL)
60
4.16M
            return (p - s);
61
452k
        return -1;
62
#else
63
        /* use memchr if we can choose a needle without too many likely
64
           false positives */
65
        const STRINGLIB_CHAR *s1, *e1;
66
        unsigned char needle = ch & 0xff;
67
        /* If looking for a multiple of 256, we'd have too
68
           many false positives looking for the '\0' byte in UCS2
69
           and UCS4 representations. */
70
        if (needle != 0) {
71
            do {
72
                const void *candidate = memchr(p, needle,
73
                                               (e - p) * sizeof(STRINGLIB_CHAR));
74
                if (candidate == NULL)
75
                    return -1;
76
                s1 = p;
77
                p = (const STRINGLIB_CHAR *)
78
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
                if (*p == ch)
80
                    return (p - s);
81
                /* False positive */
82
                p++;
83
                if (p - s1 > MEMCHR_CUT_OFF)
84
                    continue;
85
                if (e - p <= MEMCHR_CUT_OFF)
86
                    break;
87
                e1 = p + MEMCHR_CUT_OFF;
88
                while (p != e1) {
89
                    if (*p == ch)
90
                        return (p - s);
91
                    p++;
92
                }
93
            }
94
            while (e - p > MEMCHR_CUT_OFF);
95
        }
96
#endif
97
4.61M
    }
98
3.32M
    while (p < e) {
99
3.25M
        if (*p == ch)
100
959k
            return (p - s);
101
2.29M
        p++;
102
2.29M
    }
103
72.5k
    return -1;
104
1.03M
}
bytes_methods.c:stringlib_find_char
Line
Count
Source
51
14.7M
{
52
14.7M
    const STRINGLIB_CHAR *p, *e;
53
54
14.7M
    p = s;
55
14.7M
    e = s + n;
56
14.7M
    if (n > MEMCHR_CUT_OFF) {
57
4.50M
#ifdef STRINGLIB_FAST_MEMCHR
58
4.50M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
4.50M
        if (p != NULL)
60
4.43M
            return (p - s);
61
65.2k
        return -1;
62
#else
63
        /* use memchr if we can choose a needle without too many likely
64
           false positives */
65
        const STRINGLIB_CHAR *s1, *e1;
66
        unsigned char needle = ch & 0xff;
67
        /* If looking for a multiple of 256, we'd have too
68
           many false positives looking for the '\0' byte in UCS2
69
           and UCS4 representations. */
70
        if (needle != 0) {
71
            do {
72
                const void *candidate = memchr(p, needle,
73
                                               (e - p) * sizeof(STRINGLIB_CHAR));
74
                if (candidate == NULL)
75
                    return -1;
76
                s1 = p;
77
                p = (const STRINGLIB_CHAR *)
78
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
                if (*p == ch)
80
                    return (p - s);
81
                /* False positive */
82
                p++;
83
                if (p - s1 > MEMCHR_CUT_OFF)
84
                    continue;
85
                if (e - p <= MEMCHR_CUT_OFF)
86
                    break;
87
                e1 = p + MEMCHR_CUT_OFF;
88
                while (p != e1) {
89
                    if (*p == ch)
90
                        return (p - s);
91
                    p++;
92
                }
93
            }
94
            while (e - p > MEMCHR_CUT_OFF);
95
        }
96
#endif
97
4.50M
    }
98
81.7M
    while (p < e) {
99
77.9M
        if (*p == ch)
100
6.44M
            return (p - s);
101
71.5M
        p++;
102
71.5M
    }
103
3.81M
    return -1;
104
10.2M
}
Unexecuted instantiation: bytearrayobject.c:stringlib_find_char
105
106
#undef MEMCHR_CUT_OFF
107
108
#if STRINGLIB_SIZEOF_CHAR == 1
109
396k
#  define MEMRCHR_CUT_OFF 15
110
#else
111
384k
#  define MEMRCHR_CUT_OFF 40
112
#endif
113
114
115
Py_LOCAL_INLINE(Py_ssize_t)
116
STRINGLIB(rfind_char)(const STRINGLIB_CHAR* s, Py_ssize_t n, STRINGLIB_CHAR ch)
117
740k
{
118
740k
    const STRINGLIB_CHAR *p;
119
740k
#ifdef HAVE_MEMRCHR
120
    /* memrchr() is a GNU extension, available since glibc 2.1.91.  it
121
       doesn't seem as optimized as memchr(), but is still quite
122
       faster than our hand-written loop below. There is no wmemrchr
123
       for 4-byte chars. */
124
125
740k
    if (n > MEMRCHR_CUT_OFF) {
126
#if STRINGLIB_SIZEOF_CHAR == 1
127
        p = memrchr(s, ch, n);
128
104k
        if (p != NULL)
129
98.3k
            return (p - s);
130
6.25k
        return -1;
131
#else
132
        /* use memrchr if we can choose a needle without too many likely
133
           false positives */
134
        const STRINGLIB_CHAR *s1;
135
        Py_ssize_t n1;
136
        unsigned char needle = ch & 0xff;
137
        /* If looking for a multiple of 256, we'd have too
138
           many false positives looking for the '\0' byte in UCS2
139
           and UCS4 representations. */
140
266k
        if (needle != 0) {
141
277k
            do {
142
277k
                void *candidate = memrchr(s, needle,
143
277k
                                          n * sizeof(STRINGLIB_CHAR));
144
277k
                if (candidate == NULL)
145
1.38k
                    return -1;
146
276k
                n1 = n;
147
276k
                p = (const STRINGLIB_CHAR *)
148
276k
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
276k
                n = p - s;
150
276k
                if (*p == ch)
151
262k
                    return n;
152
                /* False positive */
153
13.8k
                if (n1 - n > MEMRCHR_CUT_OFF)
154
6.13k
                    continue;
155
7.74k
                if (n <= MEMRCHR_CUT_OFF)
156
933
                    break;
157
6.80k
                s1 = p - MEMRCHR_CUT_OFF;
158
263k
                while (p > s1) {
159
257k
                    p--;
160
257k
                    if (*p == ch)
161
634
                        return (p - s);
162
257k
                }
163
6.17k
                n = p - s;
164
6.17k
            }
165
266k
            while (n > MEMRCHR_CUT_OFF);
166
266k
        }
167
#endif
168
370k
    }
169
370k
#endif  /* HAVE_MEMRCHR */
170
370k
    p = s + n;
171
2.41M
    while (p > s) {
172
2.22M
        p--;
173
2.22M
        if (*p == ch)
174
179k
            return (p - s);
175
2.22M
    }
176
191k
    return -1;
177
370k
}
Unexecuted instantiation: bytesobject.c:stringlib_rfind_char
unicodeobject.c:ucs1lib_rfind_char
Line
Count
Source
117
68.8k
{
118
68.8k
    const STRINGLIB_CHAR *p;
119
68.8k
#ifdef HAVE_MEMRCHR
120
    /* memrchr() is a GNU extension, available since glibc 2.1.91.  it
121
       doesn't seem as optimized as memchr(), but is still quite
122
       faster than our hand-written loop below. There is no wmemrchr
123
       for 4-byte chars. */
124
125
68.8k
    if (n > MEMRCHR_CUT_OFF) {
126
63.9k
#if STRINGLIB_SIZEOF_CHAR == 1
127
63.9k
        p = memrchr(s, ch, n);
128
63.9k
        if (p != NULL)
129
61.9k
            return (p - s);
130
1.92k
        return -1;
131
#else
132
        /* use memrchr if we can choose a needle without too many likely
133
           false positives */
134
        const STRINGLIB_CHAR *s1;
135
        Py_ssize_t n1;
136
        unsigned char needle = ch & 0xff;
137
        /* If looking for a multiple of 256, we'd have too
138
           many false positives looking for the '\0' byte in UCS2
139
           and UCS4 representations. */
140
        if (needle != 0) {
141
            do {
142
                void *candidate = memrchr(s, needle,
143
                                          n * sizeof(STRINGLIB_CHAR));
144
                if (candidate == NULL)
145
                    return -1;
146
                n1 = n;
147
                p = (const STRINGLIB_CHAR *)
148
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
                n = p - s;
150
                if (*p == ch)
151
                    return n;
152
                /* False positive */
153
                if (n1 - n > MEMRCHR_CUT_OFF)
154
                    continue;
155
                if (n <= MEMRCHR_CUT_OFF)
156
                    break;
157
                s1 = p - MEMRCHR_CUT_OFF;
158
                while (p > s1) {
159
                    p--;
160
                    if (*p == ch)
161
                        return (p - s);
162
                }
163
                n = p - s;
164
            }
165
            while (n > MEMRCHR_CUT_OFF);
166
        }
167
#endif
168
63.9k
    }
169
4.92k
#endif  /* HAVE_MEMRCHR */
170
4.92k
    p = s + n;
171
20.7k
    while (p > s) {
172
18.8k
        p--;
173
18.8k
        if (*p == ch)
174
3.08k
            return (p - s);
175
18.8k
    }
176
1.84k
    return -1;
177
4.92k
}
unicodeobject.c:ucs2lib_rfind_char
Line
Count
Source
117
214k
{
118
214k
    const STRINGLIB_CHAR *p;
119
214k
#ifdef HAVE_MEMRCHR
120
    /* memrchr() is a GNU extension, available since glibc 2.1.91.  it
121
       doesn't seem as optimized as memchr(), but is still quite
122
       faster than our hand-written loop below. There is no wmemrchr
123
       for 4-byte chars. */
124
125
214k
    if (n > MEMRCHR_CUT_OFF) {
126
#if STRINGLIB_SIZEOF_CHAR == 1
127
        p = memrchr(s, ch, n);
128
        if (p != NULL)
129
            return (p - s);
130
        return -1;
131
#else
132
        /* use memrchr if we can choose a needle without too many likely
133
           false positives */
134
181k
        const STRINGLIB_CHAR *s1;
135
181k
        Py_ssize_t n1;
136
181k
        unsigned char needle = ch & 0xff;
137
        /* If looking for a multiple of 256, we'd have too
138
           many false positives looking for the '\0' byte in UCS2
139
           and UCS4 representations. */
140
181k
        if (needle != 0) {
141
184k
            do {
142
184k
                void *candidate = memrchr(s, needle,
143
184k
                                          n * sizeof(STRINGLIB_CHAR));
144
184k
                if (candidate == NULL)
145
801
                    return -1;
146
183k
                n1 = n;
147
183k
                p = (const STRINGLIB_CHAR *)
148
183k
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
183k
                n = p - s;
150
183k
                if (*p == ch)
151
179k
                    return n;
152
                /* False positive */
153
4.06k
                if (n1 - n > MEMRCHR_CUT_OFF)
154
1.72k
                    continue;
155
2.34k
                if (n <= MEMRCHR_CUT_OFF)
156
565
                    break;
157
1.77k
                s1 = p - MEMRCHR_CUT_OFF;
158
66.4k
                while (p > s1) {
159
64.9k
                    p--;
160
64.9k
                    if (*p == ch)
161
254
                        return (p - s);
162
64.9k
                }
163
1.52k
                n = p - s;
164
1.52k
            }
165
181k
            while (n > MEMRCHR_CUT_OFF);
166
181k
        }
167
181k
#endif
168
181k
    }
169
34.3k
#endif  /* HAVE_MEMRCHR */
170
34.3k
    p = s + n;
171
127k
    while (p > s) {
172
125k
        p--;
173
125k
        if (*p == ch)
174
31.9k
            return (p - s);
175
125k
    }
176
2.34k
    return -1;
177
34.3k
}
unicodeobject.c:ucs4lib_rfind_char
Line
Count
Source
117
129k
{
118
129k
    const STRINGLIB_CHAR *p;
119
129k
#ifdef HAVE_MEMRCHR
120
    /* memrchr() is a GNU extension, available since glibc 2.1.91.  it
121
       doesn't seem as optimized as memchr(), but is still quite
122
       faster than our hand-written loop below. There is no wmemrchr
123
       for 4-byte chars. */
124
125
129k
    if (n > MEMRCHR_CUT_OFF) {
126
#if STRINGLIB_SIZEOF_CHAR == 1
127
        p = memrchr(s, ch, n);
128
        if (p != NULL)
129
            return (p - s);
130
        return -1;
131
#else
132
        /* use memrchr if we can choose a needle without too many likely
133
           false positives */
134
84.7k
        const STRINGLIB_CHAR *s1;
135
84.7k
        Py_ssize_t n1;
136
84.7k
        unsigned char needle = ch & 0xff;
137
        /* If looking for a multiple of 256, we'd have too
138
           many false positives looking for the '\0' byte in UCS2
139
           and UCS4 representations. */
140
84.7k
        if (needle != 0) {
141
93.5k
            do {
142
93.5k
                void *candidate = memrchr(s, needle,
143
93.5k
                                          n * sizeof(STRINGLIB_CHAR));
144
93.5k
                if (candidate == NULL)
145
584
                    return -1;
146
92.9k
                n1 = n;
147
92.9k
                p = (const STRINGLIB_CHAR *)
148
92.9k
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
92.9k
                n = p - s;
150
92.9k
                if (*p == ch)
151
83.1k
                    return n;
152
                /* False positive */
153
9.81k
                if (n1 - n > MEMRCHR_CUT_OFF)
154
4.41k
                    continue;
155
5.40k
                if (n <= MEMRCHR_CUT_OFF)
156
368
                    break;
157
5.03k
                s1 = p - MEMRCHR_CUT_OFF;
158
196k
                while (p > s1) {
159
192k
                    p--;
160
192k
                    if (*p == ch)
161
380
                        return (p - s);
162
192k
                }
163
4.65k
                n = p - s;
164
4.65k
            }
165
84.7k
            while (n > MEMRCHR_CUT_OFF);
166
84.7k
        }
167
84.7k
#endif
168
84.7k
    }
169
45.1k
#endif  /* HAVE_MEMRCHR */
170
45.1k
    p = s + n;
171
286k
    while (p > s) {
172
284k
        p--;
173
284k
        if (*p == ch)
174
43.3k
            return (p - s);
175
284k
    }
176
1.83k
    return -1;
177
45.1k
}
unicodeobject.c:asciilib_rfind_char
Line
Count
Source
117
56.4k
{
118
56.4k
    const STRINGLIB_CHAR *p;
119
56.4k
#ifdef HAVE_MEMRCHR
120
    /* memrchr() is a GNU extension, available since glibc 2.1.91.  it
121
       doesn't seem as optimized as memchr(), but is still quite
122
       faster than our hand-written loop below. There is no wmemrchr
123
       for 4-byte chars. */
124
125
56.4k
    if (n > MEMRCHR_CUT_OFF) {
126
18.5k
#if STRINGLIB_SIZEOF_CHAR == 1
127
18.5k
        p = memrchr(s, ch, n);
128
18.5k
        if (p != NULL)
129
18.2k
            return (p - s);
130
286
        return -1;
131
#else
132
        /* use memrchr if we can choose a needle without too many likely
133
           false positives */
134
        const STRINGLIB_CHAR *s1;
135
        Py_ssize_t n1;
136
        unsigned char needle = ch & 0xff;
137
        /* If looking for a multiple of 256, we'd have too
138
           many false positives looking for the '\0' byte in UCS2
139
           and UCS4 representations. */
140
        if (needle != 0) {
141
            do {
142
                void *candidate = memrchr(s, needle,
143
                                          n * sizeof(STRINGLIB_CHAR));
144
                if (candidate == NULL)
145
                    return -1;
146
                n1 = n;
147
                p = (const STRINGLIB_CHAR *)
148
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
                n = p - s;
150
                if (*p == ch)
151
                    return n;
152
                /* False positive */
153
                if (n1 - n > MEMRCHR_CUT_OFF)
154
                    continue;
155
                if (n <= MEMRCHR_CUT_OFF)
156
                    break;
157
                s1 = p - MEMRCHR_CUT_OFF;
158
                while (p > s1) {
159
                    p--;
160
                    if (*p == ch)
161
                        return (p - s);
162
                }
163
                n = p - s;
164
            }
165
            while (n > MEMRCHR_CUT_OFF);
166
        }
167
#endif
168
18.5k
    }
169
37.8k
#endif  /* HAVE_MEMRCHR */
170
37.8k
    p = s + n;
171
217k
    while (p > s) {
172
190k
        p--;
173
190k
        if (*p == ch)
174
10.5k
            return (p - s);
175
190k
    }
176
27.3k
    return -1;
177
37.8k
}
bytes_methods.c:stringlib_rfind_char
Line
Count
Source
117
270k
{
118
270k
    const STRINGLIB_CHAR *p;
119
270k
#ifdef HAVE_MEMRCHR
120
    /* memrchr() is a GNU extension, available since glibc 2.1.91.  it
121
       doesn't seem as optimized as memchr(), but is still quite
122
       faster than our hand-written loop below. There is no wmemrchr
123
       for 4-byte chars. */
124
125
270k
    if (n > MEMRCHR_CUT_OFF) {
126
22.1k
#if STRINGLIB_SIZEOF_CHAR == 1
127
22.1k
        p = memrchr(s, ch, n);
128
22.1k
        if (p != NULL)
129
18.1k
            return (p - s);
130
4.04k
        return -1;
131
#else
132
        /* use memrchr if we can choose a needle without too many likely
133
           false positives */
134
        const STRINGLIB_CHAR *s1;
135
        Py_ssize_t n1;
136
        unsigned char needle = ch & 0xff;
137
        /* If looking for a multiple of 256, we'd have too
138
           many false positives looking for the '\0' byte in UCS2
139
           and UCS4 representations. */
140
        if (needle != 0) {
141
            do {
142
                void *candidate = memrchr(s, needle,
143
                                          n * sizeof(STRINGLIB_CHAR));
144
                if (candidate == NULL)
145
                    return -1;
146
                n1 = n;
147
                p = (const STRINGLIB_CHAR *)
148
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
                n = p - s;
150
                if (*p == ch)
151
                    return n;
152
                /* False positive */
153
                if (n1 - n > MEMRCHR_CUT_OFF)
154
                    continue;
155
                if (n <= MEMRCHR_CUT_OFF)
156
                    break;
157
                s1 = p - MEMRCHR_CUT_OFF;
158
                while (p > s1) {
159
                    p--;
160
                    if (*p == ch)
161
                        return (p - s);
162
                }
163
                n = p - s;
164
            }
165
            while (n > MEMRCHR_CUT_OFF);
166
        }
167
#endif
168
22.1k
    }
169
248k
#endif  /* HAVE_MEMRCHR */
170
248k
    p = s + n;
171
1.76M
    while (p > s) {
172
1.60M
        p--;
173
1.60M
        if (*p == ch)
174
90.4k
            return (p - s);
175
1.60M
    }
176
158k
    return -1;
177
248k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_rfind_char
178
179
#undef MEMRCHR_CUT_OFF
180
181
/* Change to a 1 to see logging comments walk through the algorithm. */
182
#if 0 && STRINGLIB_SIZEOF_CHAR == 1
183
# define LOG(...) printf(__VA_ARGS__)
184
# define LOG_STRING(s, n) printf("\"%.*s\"", (int)(n), s)
185
# define LOG_LINEUP() do {                                         \
186
    LOG("> "); LOG_STRING(haystack, len_haystack); LOG("\n> ");    \
187
    LOG("%*s",(int)(window_last - haystack + 1 - len_needle), ""); \
188
    LOG_STRING(needle, len_needle); LOG("\n");                     \
189
} while(0)
190
#else
191
# define LOG(...)
192
# define LOG_STRING(s, n)
193
# define LOG_LINEUP()
194
#endif
195
196
Py_LOCAL_INLINE(Py_ssize_t)
197
STRINGLIB(_lex_search)(const STRINGLIB_CHAR *needle, Py_ssize_t len_needle,
198
                       Py_ssize_t *return_period, int invert_alphabet)
199
44
{
200
    /* Do a lexicographic search. Essentially this:
201
           >>> max(needle[i:] for i in range(len(needle)+1))
202
       Also find the period of the right half.   */
203
44
    Py_ssize_t max_suffix = 0;
204
44
    Py_ssize_t candidate = 1;
205
44
    Py_ssize_t k = 0;
206
    // The period of the right half.
207
44
    Py_ssize_t period = 1;
208
209
440
    while (candidate + k < len_needle) {
210
        // each loop increases candidate + k + max_suffix
211
396
        STRINGLIB_CHAR a = needle[candidate + k];
212
396
        STRINGLIB_CHAR b = needle[max_suffix + k];
213
        // check if the suffix at candidate is better than max_suffix
214
396
        if (invert_alphabet ? (b < a) : (a < b)) {
215
            // Fell short of max_suffix.
216
            // The next k + 1 characters are non-increasing
217
            // from candidate, so they won't start a maximal suffix.
218
286
            candidate += k + 1;
219
286
            k = 0;
220
            // We've ruled out any period smaller than what's
221
            // been scanned since max_suffix.
222
286
            period = candidate - max_suffix;
223
286
        }
224
110
        else if (a == b) {
225
22
            if (k + 1 != period) {
226
                // Keep scanning the equal strings
227
0
                k++;
228
0
            }
229
22
            else {
230
                // Matched a whole period.
231
                // Start matching the next period.
232
22
                candidate += period;
233
22
                k = 0;
234
22
            }
235
22
        }
236
88
        else {
237
            // Did better than max_suffix, so replace it.
238
88
            max_suffix = candidate;
239
88
            candidate++;
240
88
            k = 0;
241
88
            period = 1;
242
88
        }
243
396
    }
244
44
    *return_period = period;
245
44
    return max_suffix;
246
44
}
Unexecuted instantiation: bytesobject.c:stringlib__lex_search
Unexecuted instantiation: unicodeobject.c:asciilib__lex_search
unicodeobject.c:ucs1lib__lex_search
Line
Count
Source
199
44
{
200
    /* Do a lexicographic search. Essentially this:
201
           >>> max(needle[i:] for i in range(len(needle)+1))
202
       Also find the period of the right half.   */
203
44
    Py_ssize_t max_suffix = 0;
204
44
    Py_ssize_t candidate = 1;
205
44
    Py_ssize_t k = 0;
206
    // The period of the right half.
207
44
    Py_ssize_t period = 1;
208
209
440
    while (candidate + k < len_needle) {
210
        // each loop increases candidate + k + max_suffix
211
396
        STRINGLIB_CHAR a = needle[candidate + k];
212
396
        STRINGLIB_CHAR b = needle[max_suffix + k];
213
        // check if the suffix at candidate is better than max_suffix
214
396
        if (invert_alphabet ? (b < a) : (a < b)) {
215
            // Fell short of max_suffix.
216
            // The next k + 1 characters are non-increasing
217
            // from candidate, so they won't start a maximal suffix.
218
286
            candidate += k + 1;
219
286
            k = 0;
220
            // We've ruled out any period smaller than what's
221
            // been scanned since max_suffix.
222
286
            period = candidate - max_suffix;
223
286
        }
224
110
        else if (a == b) {
225
22
            if (k + 1 != period) {
226
                // Keep scanning the equal strings
227
0
                k++;
228
0
            }
229
22
            else {
230
                // Matched a whole period.
231
                // Start matching the next period.
232
22
                candidate += period;
233
22
                k = 0;
234
22
            }
235
22
        }
236
88
        else {
237
            // Did better than max_suffix, so replace it.
238
88
            max_suffix = candidate;
239
88
            candidate++;
240
88
            k = 0;
241
88
            period = 1;
242
88
        }
243
396
    }
244
44
    *return_period = period;
245
44
    return max_suffix;
246
44
}
Unexecuted instantiation: unicodeobject.c:ucs2lib__lex_search
Unexecuted instantiation: unicodeobject.c:ucs4lib__lex_search
Unexecuted instantiation: bytes_methods.c:stringlib__lex_search
Unexecuted instantiation: bytearrayobject.c:stringlib__lex_search
247
248
Py_LOCAL_INLINE(Py_ssize_t)
249
STRINGLIB(_factorize)(const STRINGLIB_CHAR *needle,
250
                      Py_ssize_t len_needle,
251
                      Py_ssize_t *return_period)
252
22
{
253
    /* Do a "critical factorization", making it so that:
254
       >>> needle = (left := needle[:cut]) + (right := needle[cut:])
255
       where the "local period" of the cut is maximal.
256
257
       The local period of the cut is the minimal length of a string w
258
       such that (left endswith w or w endswith left)
259
       and (right startswith w or w startswith right).
260
261
       The Critical Factorization Theorem says that this maximal local
262
       period is the global period of the string.
263
264
       Crochemore and Perrin (1991) show that this cut can be computed
265
       as the later of two cuts: one that gives a lexicographically
266
       maximal right half, and one that gives the same with the
267
       with respect to a reversed alphabet-ordering.
268
269
       This is what we want to happen:
270
           >>> x = "GCAGAGAG"
271
           >>> cut, period = factorize(x)
272
           >>> x[:cut], (right := x[cut:])
273
           ('GC', 'AGAGAG')
274
           >>> period  # right half period
275
           2
276
           >>> right[period:] == right[:-period]
277
           True
278
279
       This is how the local period lines up in the above example:
280
                GC | AGAGAG
281
           AGAGAGC = AGAGAGC
282
       The length of this minimal repetition is 7, which is indeed the
283
       period of the original string. */
284
285
22
    Py_ssize_t cut1, period1, cut2, period2, cut, period;
286
22
    cut1 = STRINGLIB(_lex_search)(needle, len_needle, &period1, 0);
287
22
    cut2 = STRINGLIB(_lex_search)(needle, len_needle, &period2, 1);
288
289
    // Take the later cut.
290
22
    if (cut1 > cut2) {
291
22
        period = period1;
292
22
        cut = cut1;
293
22
    }
294
0
    else {
295
0
        period = period2;
296
0
        cut = cut2;
297
0
    }
298
299
22
    LOG("split: "); LOG_STRING(needle, cut);
300
22
    LOG(" + "); LOG_STRING(needle + cut, len_needle - cut);
301
22
    LOG("\n");
302
303
22
    *return_period = period;
304
22
    return cut;
305
22
}
Unexecuted instantiation: bytesobject.c:stringlib__factorize
Unexecuted instantiation: unicodeobject.c:asciilib__factorize
unicodeobject.c:ucs1lib__factorize
Line
Count
Source
252
22
{
253
    /* Do a "critical factorization", making it so that:
254
       >>> needle = (left := needle[:cut]) + (right := needle[cut:])
255
       where the "local period" of the cut is maximal.
256
257
       The local period of the cut is the minimal length of a string w
258
       such that (left endswith w or w endswith left)
259
       and (right startswith w or w startswith right).
260
261
       The Critical Factorization Theorem says that this maximal local
262
       period is the global period of the string.
263
264
       Crochemore and Perrin (1991) show that this cut can be computed
265
       as the later of two cuts: one that gives a lexicographically
266
       maximal right half, and one that gives the same with the
267
       with respect to a reversed alphabet-ordering.
268
269
       This is what we want to happen:
270
           >>> x = "GCAGAGAG"
271
           >>> cut, period = factorize(x)
272
           >>> x[:cut], (right := x[cut:])
273
           ('GC', 'AGAGAG')
274
           >>> period  # right half period
275
           2
276
           >>> right[period:] == right[:-period]
277
           True
278
279
       This is how the local period lines up in the above example:
280
                GC | AGAGAG
281
           AGAGAGC = AGAGAGC
282
       The length of this minimal repetition is 7, which is indeed the
283
       period of the original string. */
284
285
22
    Py_ssize_t cut1, period1, cut2, period2, cut, period;
286
22
    cut1 = STRINGLIB(_lex_search)(needle, len_needle, &period1, 0);
287
22
    cut2 = STRINGLIB(_lex_search)(needle, len_needle, &period2, 1);
288
289
    // Take the later cut.
290
22
    if (cut1 > cut2) {
291
22
        period = period1;
292
22
        cut = cut1;
293
22
    }
294
0
    else {
295
0
        period = period2;
296
0
        cut = cut2;
297
0
    }
298
299
22
    LOG("split: "); LOG_STRING(needle, cut);
300
22
    LOG(" + "); LOG_STRING(needle + cut, len_needle - cut);
301
22
    LOG("\n");
302
303
22
    *return_period = period;
304
22
    return cut;
305
22
}
Unexecuted instantiation: unicodeobject.c:ucs2lib__factorize
Unexecuted instantiation: unicodeobject.c:ucs4lib__factorize
Unexecuted instantiation: bytes_methods.c:stringlib__factorize
Unexecuted instantiation: bytearrayobject.c:stringlib__factorize
306
307
308
242
#define SHIFT_TYPE uint8_t
309
#define MAX_SHIFT UINT8_MAX
310
311
81.4k
#define TABLE_SIZE_BITS 6u
312
81.4k
#define TABLE_SIZE (1U << TABLE_SIZE_BITS)
313
79.9k
#define TABLE_MASK (TABLE_SIZE - 1U)
314
315
typedef struct STRINGLIB(_pre) {
316
    const STRINGLIB_CHAR *needle;
317
    Py_ssize_t len_needle;
318
    Py_ssize_t cut;
319
    Py_ssize_t period;
320
    Py_ssize_t gap;
321
    int is_periodic;
322
    SHIFT_TYPE table[TABLE_SIZE];
323
} STRINGLIB(prework);
324
325
326
static void
327
STRINGLIB(_preprocess)(const STRINGLIB_CHAR *needle, Py_ssize_t len_needle,
328
                       STRINGLIB(prework) *p)
329
22
{
330
22
    p->needle = needle;
331
22
    p->len_needle = len_needle;
332
22
    p->cut = STRINGLIB(_factorize)(needle, len_needle, &(p->period));
333
22
    assert(p->period + p->cut <= len_needle);
334
22
    p->is_periodic = (0 == memcmp(needle,
335
22
                                  needle + p->period,
336
22
                                  p->cut * STRINGLIB_SIZEOF_CHAR));
337
22
    if (p->is_periodic) {
338
0
        assert(p->cut <= len_needle/2);
339
0
        assert(p->cut < p->period);
340
0
    }
341
22
    else {
342
        // A lower bound on the period
343
22
        p->period = Py_MAX(p->cut, len_needle - p->cut) + 1;
344
22
    }
345
    // The gap between the last character and the previous
346
    // occurrence of an equivalent character (modulo TABLE_SIZE)
347
22
    p->gap = len_needle;
348
22
    STRINGLIB_CHAR last = needle[len_needle - 1] & TABLE_MASK;
349
154
    for (Py_ssize_t i = len_needle - 2; i >= 0; i--) {
350
154
        STRINGLIB_CHAR x = needle[i] & TABLE_MASK;
351
154
        if (x == last) {
352
22
            p->gap = len_needle - 1 - i;
353
22
            break;
354
22
        }
355
154
    }
356
    // Fill up a compressed Boyer-Moore "Bad Character" table
357
22
    Py_ssize_t not_found_shift = Py_MIN(len_needle, MAX_SHIFT);
358
1.43k
    for (Py_ssize_t i = 0; i < (Py_ssize_t)TABLE_SIZE; i++) {
359
1.40k
        p->table[i] = Py_SAFE_DOWNCAST(not_found_shift,
360
1.40k
                                       Py_ssize_t, SHIFT_TYPE);
361
1.40k
    }
362
242
    for (Py_ssize_t i = len_needle - not_found_shift; i < len_needle; i++) {
363
220
        SHIFT_TYPE shift = Py_SAFE_DOWNCAST(len_needle - 1 - i,
364
220
                                            Py_ssize_t, SHIFT_TYPE);
365
220
        p->table[needle[i] & TABLE_MASK] = shift;
366
220
    }
367
22
}
Unexecuted instantiation: bytesobject.c:stringlib__preprocess
Unexecuted instantiation: unicodeobject.c:asciilib__preprocess
unicodeobject.c:ucs1lib__preprocess
Line
Count
Source
329
22
{
330
22
    p->needle = needle;
331
22
    p->len_needle = len_needle;
332
22
    p->cut = STRINGLIB(_factorize)(needle, len_needle, &(p->period));
333
22
    assert(p->period + p->cut <= len_needle);
334
22
    p->is_periodic = (0 == memcmp(needle,
335
22
                                  needle + p->period,
336
22
                                  p->cut * STRINGLIB_SIZEOF_CHAR));
337
22
    if (p->is_periodic) {
338
0
        assert(p->cut <= len_needle/2);
339
0
        assert(p->cut < p->period);
340
0
    }
341
22
    else {
342
        // A lower bound on the period
343
22
        p->period = Py_MAX(p->cut, len_needle - p->cut) + 1;
344
22
    }
345
    // The gap between the last character and the previous
346
    // occurrence of an equivalent character (modulo TABLE_SIZE)
347
22
    p->gap = len_needle;
348
22
    STRINGLIB_CHAR last = needle[len_needle - 1] & TABLE_MASK;
349
154
    for (Py_ssize_t i = len_needle - 2; i >= 0; i--) {
350
154
        STRINGLIB_CHAR x = needle[i] & TABLE_MASK;
351
154
        if (x == last) {
352
22
            p->gap = len_needle - 1 - i;
353
22
            break;
354
22
        }
355
154
    }
356
    // Fill up a compressed Boyer-Moore "Bad Character" table
357
22
    Py_ssize_t not_found_shift = Py_MIN(len_needle, MAX_SHIFT);
358
1.43k
    for (Py_ssize_t i = 0; i < (Py_ssize_t)TABLE_SIZE; i++) {
359
1.40k
        p->table[i] = Py_SAFE_DOWNCAST(not_found_shift,
360
1.40k
                                       Py_ssize_t, SHIFT_TYPE);
361
1.40k
    }
362
242
    for (Py_ssize_t i = len_needle - not_found_shift; i < len_needle; i++) {
363
220
        SHIFT_TYPE shift = Py_SAFE_DOWNCAST(len_needle - 1 - i,
364
220
                                            Py_ssize_t, SHIFT_TYPE);
365
220
        p->table[needle[i] & TABLE_MASK] = shift;
366
220
    }
367
22
}
Unexecuted instantiation: unicodeobject.c:ucs2lib__preprocess
Unexecuted instantiation: unicodeobject.c:ucs4lib__preprocess
Unexecuted instantiation: bytes_methods.c:stringlib__preprocess
Unexecuted instantiation: bytearrayobject.c:stringlib__preprocess
368
369
static Py_ssize_t
370
STRINGLIB(_two_way)(const STRINGLIB_CHAR *haystack, Py_ssize_t len_haystack,
371
                    STRINGLIB(prework) *p)
372
22
{
373
    // Crochemore and Perrin's (1991) Two-Way algorithm.
374
    // See http://www-igm.univ-mlv.fr/~lecroq/string/node26.html#SECTION00260
375
22
    const Py_ssize_t len_needle = p->len_needle;
376
22
    const Py_ssize_t cut = p->cut;
377
22
    Py_ssize_t period = p->period;
378
22
    const STRINGLIB_CHAR *const needle = p->needle;
379
22
    const STRINGLIB_CHAR *window_last = haystack + len_needle - 1;
380
22
    const STRINGLIB_CHAR *const haystack_end = haystack + len_haystack;
381
22
    SHIFT_TYPE *table = p->table;
382
22
    const STRINGLIB_CHAR *window;
383
22
    LOG("===== Two-way: \"%s\" in \"%s\". =====\n", needle, haystack);
384
385
22
    Py_ssize_t gap = p->gap;
386
22
    Py_ssize_t gap_jump_end = Py_MIN(len_needle, cut + gap);
387
22
    if (p->is_periodic) {
388
0
        LOG("Needle is periodic.\n");
389
0
        Py_ssize_t memory = 0;
390
0
      periodicwindowloop:
391
0
        while (window_last < haystack_end) {
392
0
            assert(memory == 0);
393
0
            for (;;) {
394
0
                LOG_LINEUP();
395
0
                Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
396
0
                window_last += shift;
397
0
                if (shift == 0) {
398
0
                    break;
399
0
                }
400
0
                if (window_last >= haystack_end) {
401
0
                    return -1;
402
0
                }
403
0
                LOG("Horspool skip\n");
404
0
            }
405
0
          no_shift:
406
0
            window = window_last - len_needle + 1;
407
0
            assert((window[len_needle - 1] & TABLE_MASK) ==
408
0
                   (needle[len_needle - 1] & TABLE_MASK));
409
0
            Py_ssize_t i = Py_MAX(cut, memory);
410
0
            for (; i < len_needle; i++) {
411
0
                if (needle[i] != window[i]) {
412
0
                    if (i < gap_jump_end) {
413
0
                        LOG("Early right half mismatch: jump by gap.\n");
414
0
                        assert(gap >= i - cut + 1);
415
0
                        window_last += gap;
416
0
                    }
417
0
                    else {
418
0
                        LOG("Late right half mismatch: jump by n (>gap)\n");
419
0
                        assert(i - cut + 1 > gap);
420
0
                        window_last += i - cut + 1;
421
0
                    }
422
0
                    memory = 0;
423
0
                    goto periodicwindowloop;
424
0
                }
425
0
            }
426
0
            for (i = memory; i < cut; i++) {
427
0
                if (needle[i] != window[i]) {
428
0
                    LOG("Left half does not match.\n");
429
0
                    window_last += period;
430
0
                    memory = len_needle - period;
431
0
                    if (window_last >= haystack_end) {
432
0
                        return -1;
433
0
                    }
434
0
                    Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
435
0
                    if (shift) {
436
                        // A mismatch has been identified to the right
437
                        // of where i will next start, so we can jump
438
                        // at least as far as if the mismatch occurred
439
                        // on the first comparison.
440
0
                        Py_ssize_t mem_jump = Py_MAX(cut, memory) - cut + 1;
441
0
                        LOG("Skip with Memory.\n");
442
0
                        memory = 0;
443
0
                        window_last += Py_MAX(shift, mem_jump);
444
0
                        goto periodicwindowloop;
445
0
                    }
446
0
                    goto no_shift;
447
0
                }
448
0
            }
449
0
            LOG("Found a match!\n");
450
0
            return window - haystack;
451
0
        }
452
0
    }
453
22
    else {
454
22
        period = Py_MAX(gap, period);
455
22
        LOG("Needle is not periodic.\n");
456
15.1k
      windowloop:
457
15.1k
        while (window_last < haystack_end) {
458
79.5k
            for (;;) {
459
79.5k
                LOG_LINEUP();
460
79.5k
                Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
461
79.5k
                window_last += shift;
462
79.5k
                if (shift == 0) {
463
15.1k
                    break;
464
15.1k
                }
465
64.4k
                if (window_last >= haystack_end) {
466
17
                    return -1;
467
17
                }
468
64.4k
                LOG("Horspool skip\n");
469
64.4k
            }
470
15.1k
            window = window_last - len_needle + 1;
471
15.1k
            assert((window[len_needle - 1] & TABLE_MASK) ==
472
15.1k
                   (needle[len_needle - 1] & TABLE_MASK));
473
15.1k
            Py_ssize_t i = cut;
474
15.3k
            for (; i < len_needle; i++) {
475
15.2k
                if (needle[i] != window[i]) {
476
15.0k
                    if (i < gap_jump_end) {
477
15.0k
                        LOG("Early right half mismatch: jump by gap.\n");
478
15.0k
                        assert(gap >= i - cut + 1);
479
15.0k
                        window_last += gap;
480
15.0k
                    }
481
0
                    else {
482
0
                        LOG("Late right half mismatch: jump by n (>gap)\n");
483
0
                        assert(i - cut + 1 > gap);
484
0
                        window_last += i - cut + 1;
485
0
                    }
486
15.0k
                    goto windowloop;
487
15.0k
                }
488
15.2k
            }
489
199
            for (Py_ssize_t i = 0; i < cut; i++) {
490
196
                if (needle[i] != window[i]) {
491
92
                    LOG("Left half does not match.\n");
492
92
                    window_last += period;
493
92
                    goto windowloop;
494
92
                }
495
196
            }
496
3
            LOG("Found a match!\n");
497
3
            return window - haystack;
498
95
        }
499
15.1k
    }
500
2
    LOG("Not found. Returning -1.\n");
501
2
    return -1;
502
22
}
Unexecuted instantiation: bytesobject.c:stringlib__two_way
Unexecuted instantiation: unicodeobject.c:asciilib__two_way
unicodeobject.c:ucs1lib__two_way
Line
Count
Source
372
22
{
373
    // Crochemore and Perrin's (1991) Two-Way algorithm.
374
    // See http://www-igm.univ-mlv.fr/~lecroq/string/node26.html#SECTION00260
375
22
    const Py_ssize_t len_needle = p->len_needle;
376
22
    const Py_ssize_t cut = p->cut;
377
22
    Py_ssize_t period = p->period;
378
22
    const STRINGLIB_CHAR *const needle = p->needle;
379
22
    const STRINGLIB_CHAR *window_last = haystack + len_needle - 1;
380
22
    const STRINGLIB_CHAR *const haystack_end = haystack + len_haystack;
381
22
    SHIFT_TYPE *table = p->table;
382
22
    const STRINGLIB_CHAR *window;
383
22
    LOG("===== Two-way: \"%s\" in \"%s\". =====\n", needle, haystack);
384
385
22
    Py_ssize_t gap = p->gap;
386
22
    Py_ssize_t gap_jump_end = Py_MIN(len_needle, cut + gap);
387
22
    if (p->is_periodic) {
388
0
        LOG("Needle is periodic.\n");
389
0
        Py_ssize_t memory = 0;
390
0
      periodicwindowloop:
391
0
        while (window_last < haystack_end) {
392
0
            assert(memory == 0);
393
0
            for (;;) {
394
0
                LOG_LINEUP();
395
0
                Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
396
0
                window_last += shift;
397
0
                if (shift == 0) {
398
0
                    break;
399
0
                }
400
0
                if (window_last >= haystack_end) {
401
0
                    return -1;
402
0
                }
403
0
                LOG("Horspool skip\n");
404
0
            }
405
0
          no_shift:
406
0
            window = window_last - len_needle + 1;
407
0
            assert((window[len_needle - 1] & TABLE_MASK) ==
408
0
                   (needle[len_needle - 1] & TABLE_MASK));
409
0
            Py_ssize_t i = Py_MAX(cut, memory);
410
0
            for (; i < len_needle; i++) {
411
0
                if (needle[i] != window[i]) {
412
0
                    if (i < gap_jump_end) {
413
0
                        LOG("Early right half mismatch: jump by gap.\n");
414
0
                        assert(gap >= i - cut + 1);
415
0
                        window_last += gap;
416
0
                    }
417
0
                    else {
418
0
                        LOG("Late right half mismatch: jump by n (>gap)\n");
419
0
                        assert(i - cut + 1 > gap);
420
0
                        window_last += i - cut + 1;
421
0
                    }
422
0
                    memory = 0;
423
0
                    goto periodicwindowloop;
424
0
                }
425
0
            }
426
0
            for (i = memory; i < cut; i++) {
427
0
                if (needle[i] != window[i]) {
428
0
                    LOG("Left half does not match.\n");
429
0
                    window_last += period;
430
0
                    memory = len_needle - period;
431
0
                    if (window_last >= haystack_end) {
432
0
                        return -1;
433
0
                    }
434
0
                    Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
435
0
                    if (shift) {
436
                        // A mismatch has been identified to the right
437
                        // of where i will next start, so we can jump
438
                        // at least as far as if the mismatch occurred
439
                        // on the first comparison.
440
0
                        Py_ssize_t mem_jump = Py_MAX(cut, memory) - cut + 1;
441
0
                        LOG("Skip with Memory.\n");
442
0
                        memory = 0;
443
0
                        window_last += Py_MAX(shift, mem_jump);
444
0
                        goto periodicwindowloop;
445
0
                    }
446
0
                    goto no_shift;
447
0
                }
448
0
            }
449
0
            LOG("Found a match!\n");
450
0
            return window - haystack;
451
0
        }
452
0
    }
453
22
    else {
454
22
        period = Py_MAX(gap, period);
455
22
        LOG("Needle is not periodic.\n");
456
15.1k
      windowloop:
457
15.1k
        while (window_last < haystack_end) {
458
79.5k
            for (;;) {
459
79.5k
                LOG_LINEUP();
460
79.5k
                Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
461
79.5k
                window_last += shift;
462
79.5k
                if (shift == 0) {
463
15.1k
                    break;
464
15.1k
                }
465
64.4k
                if (window_last >= haystack_end) {
466
17
                    return -1;
467
17
                }
468
64.4k
                LOG("Horspool skip\n");
469
64.4k
            }
470
15.1k
            window = window_last - len_needle + 1;
471
15.1k
            assert((window[len_needle - 1] & TABLE_MASK) ==
472
15.1k
                   (needle[len_needle - 1] & TABLE_MASK));
473
15.1k
            Py_ssize_t i = cut;
474
15.3k
            for (; i < len_needle; i++) {
475
15.2k
                if (needle[i] != window[i]) {
476
15.0k
                    if (i < gap_jump_end) {
477
15.0k
                        LOG("Early right half mismatch: jump by gap.\n");
478
15.0k
                        assert(gap >= i - cut + 1);
479
15.0k
                        window_last += gap;
480
15.0k
                    }
481
0
                    else {
482
0
                        LOG("Late right half mismatch: jump by n (>gap)\n");
483
0
                        assert(i - cut + 1 > gap);
484
0
                        window_last += i - cut + 1;
485
0
                    }
486
15.0k
                    goto windowloop;
487
15.0k
                }
488
15.2k
            }
489
199
            for (Py_ssize_t i = 0; i < cut; i++) {
490
196
                if (needle[i] != window[i]) {
491
92
                    LOG("Left half does not match.\n");
492
92
                    window_last += period;
493
92
                    goto windowloop;
494
92
                }
495
196
            }
496
3
            LOG("Found a match!\n");
497
3
            return window - haystack;
498
95
        }
499
15.1k
    }
500
2
    LOG("Not found. Returning -1.\n");
501
2
    return -1;
502
22
}
Unexecuted instantiation: unicodeobject.c:ucs2lib__two_way
Unexecuted instantiation: unicodeobject.c:ucs4lib__two_way
Unexecuted instantiation: bytes_methods.c:stringlib__two_way
Unexecuted instantiation: bytearrayobject.c:stringlib__two_way
503
504
505
static Py_ssize_t
506
STRINGLIB(_two_way_find)(const STRINGLIB_CHAR *haystack,
507
                         Py_ssize_t len_haystack,
508
                         const STRINGLIB_CHAR *needle,
509
                         Py_ssize_t len_needle)
510
22
{
511
22
    LOG("###### Finding \"%s\" in \"%s\".\n", needle, haystack);
512
22
    STRINGLIB(prework) p;
513
22
    STRINGLIB(_preprocess)(needle, len_needle, &p);
514
22
    return STRINGLIB(_two_way)(haystack, len_haystack, &p);
515
22
}
Unexecuted instantiation: bytesobject.c:stringlib__two_way_find
Unexecuted instantiation: unicodeobject.c:asciilib__two_way_find
unicodeobject.c:ucs1lib__two_way_find
Line
Count
Source
510
22
{
511
22
    LOG("###### Finding \"%s\" in \"%s\".\n", needle, haystack);
512
22
    STRINGLIB(prework) p;
513
22
    STRINGLIB(_preprocess)(needle, len_needle, &p);
514
22
    return STRINGLIB(_two_way)(haystack, len_haystack, &p);
515
22
}
Unexecuted instantiation: unicodeobject.c:ucs2lib__two_way_find
Unexecuted instantiation: unicodeobject.c:ucs4lib__two_way_find
Unexecuted instantiation: bytes_methods.c:stringlib__two_way_find
Unexecuted instantiation: bytearrayobject.c:stringlib__two_way_find
516
517
518
static Py_ssize_t
519
STRINGLIB(_two_way_count)(const STRINGLIB_CHAR *haystack,
520
                          Py_ssize_t len_haystack,
521
                          const STRINGLIB_CHAR *needle,
522
                          Py_ssize_t len_needle,
523
                          Py_ssize_t maxcount)
524
0
{
525
0
    LOG("###### Counting \"%s\" in \"%s\".\n", needle, haystack);
526
0
    STRINGLIB(prework) p;
527
0
    STRINGLIB(_preprocess)(needle, len_needle, &p);
528
0
    Py_ssize_t index = 0, count = 0;
529
0
    while (1) {
530
0
        Py_ssize_t result;
531
0
        result = STRINGLIB(_two_way)(haystack + index,
532
0
                                     len_haystack - index, &p);
533
0
        if (result == -1) {
534
0
            return count;
535
0
        }
536
0
        count++;
537
0
        if (count == maxcount) {
538
0
            return maxcount;
539
0
        }
540
0
        index += result + len_needle;
541
0
    }
542
0
    return count;
543
0
}
Unexecuted instantiation: bytesobject.c:stringlib__two_way_count
Unexecuted instantiation: unicodeobject.c:asciilib__two_way_count
Unexecuted instantiation: unicodeobject.c:ucs1lib__two_way_count
Unexecuted instantiation: unicodeobject.c:ucs2lib__two_way_count
Unexecuted instantiation: unicodeobject.c:ucs4lib__two_way_count
Unexecuted instantiation: bytes_methods.c:stringlib__two_way_count
Unexecuted instantiation: bytearrayobject.c:stringlib__two_way_count
544
545
#undef SHIFT_TYPE
546
#undef NOT_FOUND
547
#undef SHIFT_OVERFLOW
548
#undef TABLE_SIZE_BITS
549
#undef TABLE_SIZE
550
#undef TABLE_MASK
551
552
#undef LOG
553
#undef LOG_STRING
554
#undef LOG_LINEUP
555
556
static inline Py_ssize_t
557
STRINGLIB(default_find)(const STRINGLIB_CHAR* s, Py_ssize_t n,
558
                        const STRINGLIB_CHAR* p, Py_ssize_t m,
559
                        Py_ssize_t maxcount, int mode)
560
37.9M
{
561
37.9M
    const Py_ssize_t w = n - m;
562
37.9M
    Py_ssize_t mlast = m - 1, count = 0;
563
37.9M
    Py_ssize_t gap = mlast;
564
37.9M
    const STRINGLIB_CHAR last = p[mlast];
565
37.9M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
37.9M
    unsigned long mask = 0;
568
215M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
177M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
177M
        if (p[i] == last) {
571
1.25M
            gap = mlast - i - 1;
572
1.25M
        }
573
177M
    }
574
37.9M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
985M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
959M
        if (ss[i] == last) {
578
            /* candidate match */
579
43.5M
            Py_ssize_t j;
580
67.6M
            for (j = 0; j < mlast; j++) {
581
44.7M
                if (s[i+j] != p[j]) {
582
20.7M
                    break;
583
20.7M
                }
584
44.7M
            }
585
43.5M
            if (j == mlast) {
586
                /* got a match! */
587
22.8M
                if (mode != FAST_COUNT) {
588
11.9M
                    return i;
589
11.9M
                }
590
10.8M
                count++;
591
10.8M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
10.8M
                i = i + mlast;
595
10.8M
                continue;
596
10.8M
            }
597
            /* miss: check if next character is part of pattern */
598
20.7M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
5.24M
                i = i + m;
600
5.24M
            }
601
15.5M
            else {
602
15.5M
                i = i + gap;
603
15.5M
            }
604
20.7M
        }
605
915M
        else {
606
            /* skip: check if next character is part of pattern */
607
915M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
790M
                i = i + m;
609
790M
            }
610
915M
        }
611
959M
    }
612
26.0M
    return mode == FAST_COUNT ? count : -1;
613
37.9M
}
Unexecuted instantiation: bytesobject.c:stringlib_default_find
unicodeobject.c:asciilib_default_find
Line
Count
Source
560
2.33M
{
561
2.33M
    const Py_ssize_t w = n - m;
562
2.33M
    Py_ssize_t mlast = m - 1, count = 0;
563
2.33M
    Py_ssize_t gap = mlast;
564
2.33M
    const STRINGLIB_CHAR last = p[mlast];
565
2.33M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
2.33M
    unsigned long mask = 0;
568
5.08M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
2.74M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
2.74M
        if (p[i] == last) {
571
35.8k
            gap = mlast - i - 1;
572
35.8k
        }
573
2.74M
    }
574
2.33M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
41.9M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
41.9M
        if (ss[i] == last) {
578
            /* candidate match */
579
4.08M
            Py_ssize_t j;
580
6.76M
            for (j = 0; j < mlast; j++) {
581
4.47M
                if (s[i+j] != p[j]) {
582
1.80M
                    break;
583
1.80M
                }
584
4.47M
            }
585
4.08M
            if (j == mlast) {
586
                /* got a match! */
587
2.28M
                if (mode != FAST_COUNT) {
588
2.28M
                    return i;
589
2.28M
                }
590
0
                count++;
591
0
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
0
                i = i + mlast;
595
0
                continue;
596
0
            }
597
            /* miss: check if next character is part of pattern */
598
1.80M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
112k
                i = i + m;
600
112k
            }
601
1.69M
            else {
602
1.69M
                i = i + gap;
603
1.69M
            }
604
1.80M
        }
605
37.8M
        else {
606
            /* skip: check if next character is part of pattern */
607
37.8M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
34.3M
                i = i + m;
609
34.3M
            }
610
37.8M
        }
611
41.9M
    }
612
53.0k
    return mode == FAST_COUNT ? count : -1;
613
2.33M
}
unicodeobject.c:ucs1lib_default_find
Line
Count
Source
560
27.9M
{
561
27.9M
    const Py_ssize_t w = n - m;
562
27.9M
    Py_ssize_t mlast = m - 1, count = 0;
563
27.9M
    Py_ssize_t gap = mlast;
564
27.9M
    const STRINGLIB_CHAR last = p[mlast];
565
27.9M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
27.9M
    unsigned long mask = 0;
568
194M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
166M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
166M
        if (p[i] == last) {
571
1.15M
            gap = mlast - i - 1;
572
1.15M
        }
573
166M
    }
574
27.9M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
442M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
417M
        if (ss[i] == last) {
578
            /* candidate match */
579
14.7M
            Py_ssize_t j;
580
21.0M
            for (j = 0; j < mlast; j++) {
581
15.2M
                if (s[i+j] != p[j]) {
582
8.96M
                    break;
583
8.96M
                }
584
15.2M
            }
585
14.7M
            if (j == mlast) {
586
                /* got a match! */
587
5.73M
                if (mode != FAST_COUNT) {
588
2.09M
                    return i;
589
2.09M
                }
590
3.63M
                count++;
591
3.63M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
3.63M
                i = i + mlast;
595
3.63M
                continue;
596
3.63M
            }
597
            /* miss: check if next character is part of pattern */
598
8.96M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
2.04M
                i = i + m;
600
2.04M
            }
601
6.92M
            else {
602
6.92M
                i = i + gap;
603
6.92M
            }
604
8.96M
        }
605
402M
        else {
606
            /* skip: check if next character is part of pattern */
607
402M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
294M
                i = i + m;
609
294M
            }
610
402M
        }
611
417M
    }
612
25.8M
    return mode == FAST_COUNT ? count : -1;
613
27.9M
}
unicodeobject.c:ucs2lib_default_find
Line
Count
Source
560
3.26M
{
561
3.26M
    const Py_ssize_t w = n - m;
562
3.26M
    Py_ssize_t mlast = m - 1, count = 0;
563
3.26M
    Py_ssize_t gap = mlast;
564
3.26M
    const STRINGLIB_CHAR last = p[mlast];
565
3.26M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
3.26M
    unsigned long mask = 0;
568
6.61M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
3.35M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
3.35M
        if (p[i] == last) {
571
37.3k
            gap = mlast - i - 1;
572
37.3k
        }
573
3.35M
    }
574
3.26M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
296M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
296M
        if (ss[i] == last) {
578
            /* candidate match */
579
10.9M
            Py_ssize_t j;
580
17.2M
            for (j = 0; j < mlast; j++) {
581
11.0M
                if (s[i+j] != p[j]) {
582
4.75M
                    break;
583
4.75M
                }
584
11.0M
            }
585
10.9M
            if (j == mlast) {
586
                /* got a match! */
587
6.18M
                if (mode != FAST_COUNT) {
588
3.16M
                    return i;
589
3.16M
                }
590
3.01M
                count++;
591
3.01M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
3.01M
                i = i + mlast;
595
3.01M
                continue;
596
3.01M
            }
597
            /* miss: check if next character is part of pattern */
598
4.75M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
1.32M
                i = i + m;
600
1.32M
            }
601
3.43M
            else {
602
3.43M
                i = i + gap;
603
3.43M
            }
604
4.75M
        }
605
285M
        else {
606
            /* skip: check if next character is part of pattern */
607
285M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
277M
                i = i + m;
609
277M
            }
610
285M
        }
611
296M
    }
612
97.2k
    return mode == FAST_COUNT ? count : -1;
613
3.26M
}
unicodeobject.c:ucs4lib_default_find
Line
Count
Source
560
4.40M
{
561
4.40M
    const Py_ssize_t w = n - m;
562
4.40M
    Py_ssize_t mlast = m - 1, count = 0;
563
4.40M
    Py_ssize_t gap = mlast;
564
4.40M
    const STRINGLIB_CHAR last = p[mlast];
565
4.40M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
4.40M
    unsigned long mask = 0;
568
8.94M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
4.53M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
4.53M
        if (p[i] == last) {
571
19.6k
            gap = mlast - i - 1;
572
19.6k
        }
573
4.53M
    }
574
4.40M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
202M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
202M
        if (ss[i] == last) {
578
            /* candidate match */
579
13.8M
            Py_ssize_t j;
580
22.5M
            for (j = 0; j < mlast; j++) {
581
13.9M
                if (s[i+j] != p[j]) {
582
5.21M
                    break;
583
5.21M
                }
584
13.9M
            }
585
13.8M
            if (j == mlast) {
586
                /* got a match! */
587
8.61M
                if (mode != FAST_COUNT) {
588
4.37M
                    return i;
589
4.37M
                }
590
4.23M
                count++;
591
4.23M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
4.23M
                i = i + mlast;
595
4.23M
                continue;
596
4.23M
            }
597
            /* miss: check if next character is part of pattern */
598
5.21M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
1.73M
                i = i + m;
600
1.73M
            }
601
3.48M
            else {
602
3.48M
                i = i + gap;
603
3.48M
            }
604
5.21M
        }
605
188M
        else {
606
            /* skip: check if next character is part of pattern */
607
188M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
182M
                i = i + m;
609
182M
            }
610
188M
        }
611
202M
    }
612
30.9k
    return mode == FAST_COUNT ? count : -1;
613
4.40M
}
bytes_methods.c:stringlib_default_find
Line
Count
Source
560
3.00k
{
561
3.00k
    const Py_ssize_t w = n - m;
562
3.00k
    Py_ssize_t mlast = m - 1, count = 0;
563
3.00k
    Py_ssize_t gap = mlast;
564
3.00k
    const STRINGLIB_CHAR last = p[mlast];
565
3.00k
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
3.00k
    unsigned long mask = 0;
568
12.0k
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
9.00k
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
9.00k
        if (p[i] == last) {
571
3.00k
            gap = mlast - i - 1;
572
3.00k
        }
573
9.00k
    }
574
3.00k
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
1.48M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
1.48M
        if (ss[i] == last) {
578
            /* candidate match */
579
35.6k
            Py_ssize_t j;
580
44.8k
            for (j = 0; j < mlast; j++) {
581
42.1k
                if (s[i+j] != p[j]) {
582
32.9k
                    break;
583
32.9k
                }
584
42.1k
            }
585
35.6k
            if (j == mlast) {
586
                /* got a match! */
587
2.76k
                if (mode != FAST_COUNT) {
588
2.76k
                    return i;
589
2.76k
                }
590
0
                count++;
591
0
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
0
                i = i + mlast;
595
0
                continue;
596
0
            }
597
            /* miss: check if next character is part of pattern */
598
32.9k
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
27.2k
                i = i + m;
600
27.2k
            }
601
5.66k
            else {
602
5.66k
                i = i + gap;
603
5.66k
            }
604
32.9k
        }
605
1.44M
        else {
606
            /* skip: check if next character is part of pattern */
607
1.44M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
1.11M
                i = i + m;
609
1.11M
            }
610
1.44M
        }
611
1.48M
    }
612
234
    return mode == FAST_COUNT ? count : -1;
613
3.00k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_default_find
614
615
616
static Py_ssize_t
617
STRINGLIB(adaptive_find)(const STRINGLIB_CHAR* s, Py_ssize_t n,
618
                         const STRINGLIB_CHAR* p, Py_ssize_t m,
619
                         Py_ssize_t maxcount, int mode)
620
0
{
621
0
    const Py_ssize_t w = n - m;
622
0
    Py_ssize_t mlast = m - 1, count = 0;
623
0
    Py_ssize_t gap = mlast;
624
0
    Py_ssize_t hits = 0, res;
625
0
    const STRINGLIB_CHAR last = p[mlast];
626
0
    const STRINGLIB_CHAR *const ss = &s[mlast];
627
628
0
    unsigned long mask = 0;
629
0
    for (Py_ssize_t i = 0; i < mlast; i++) {
630
0
        STRINGLIB_BLOOM_ADD(mask, p[i]);
631
0
        if (p[i] == last) {
632
0
            gap = mlast - i - 1;
633
0
        }
634
0
    }
635
0
    STRINGLIB_BLOOM_ADD(mask, last);
636
637
0
    for (Py_ssize_t i = 0; i <= w; i++) {
638
0
        if (ss[i] == last) {
639
            /* candidate match */
640
0
            Py_ssize_t j;
641
0
            for (j = 0; j < mlast; j++) {
642
0
                if (s[i+j] != p[j]) {
643
0
                    break;
644
0
                }
645
0
            }
646
0
            if (j == mlast) {
647
                /* got a match! */
648
0
                if (mode != FAST_COUNT) {
649
0
                    return i;
650
0
                }
651
0
                count++;
652
0
                if (count == maxcount) {
653
0
                    return maxcount;
654
0
                }
655
0
                i = i + mlast;
656
0
                continue;
657
0
            }
658
0
            hits += j + 1;
659
0
            if (hits > m / 4 && w - i > 2000) {
660
0
                if (mode == FAST_SEARCH) {
661
0
                    res = STRINGLIB(_two_way_find)(s + i, n - i, p, m);
662
0
                    return res == -1 ? -1 : res + i;
663
0
                }
664
0
                else {
665
0
                    res = STRINGLIB(_two_way_count)(s + i, n - i, p, m,
666
0
                                                    maxcount - count);
667
0
                    return res + count;
668
0
                }
669
0
            }
670
            /* miss: check if next character is part of pattern */
671
0
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
672
0
                i = i + m;
673
0
            }
674
0
            else {
675
0
                i = i + gap;
676
0
            }
677
0
        }
678
0
        else {
679
            /* skip: check if next character is part of pattern */
680
0
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
681
0
                i = i + m;
682
0
            }
683
0
        }
684
0
    }
685
0
    return mode == FAST_COUNT ? count : -1;
686
0
}
Unexecuted instantiation: bytesobject.c:stringlib_adaptive_find
Unexecuted instantiation: unicodeobject.c:asciilib_adaptive_find
Unexecuted instantiation: unicodeobject.c:ucs1lib_adaptive_find
Unexecuted instantiation: unicodeobject.c:ucs2lib_adaptive_find
Unexecuted instantiation: unicodeobject.c:ucs4lib_adaptive_find
Unexecuted instantiation: bytes_methods.c:stringlib_adaptive_find
Unexecuted instantiation: bytearrayobject.c:stringlib_adaptive_find
687
688
689
static Py_ssize_t
690
STRINGLIB(default_rfind)(const STRINGLIB_CHAR* s, Py_ssize_t n,
691
                         const STRINGLIB_CHAR* p, Py_ssize_t m,
692
                         Py_ssize_t maxcount, int mode)
693
8.18k
{
694
    /* create compressed boyer-moore delta 1 table */
695
8.18k
    unsigned long mask = 0;
696
8.18k
    Py_ssize_t i, j, mlast = m - 1, skip = m - 1, w = n - m;
697
698
    /* process pattern[0] outside the loop */
699
8.18k
    STRINGLIB_BLOOM_ADD(mask, p[0]);
700
    /* process pattern[:0:-1] */
701
32.7k
    for (i = mlast; i > 0; i--) {
702
24.5k
        STRINGLIB_BLOOM_ADD(mask, p[i]);
703
24.5k
        if (p[i] == p[0]) {
704
0
            skip = i - 1;
705
0
        }
706
24.5k
    }
707
708
1.77M
    for (i = w; i >= 0; i--) {
709
1.77M
        if (s[i] == p[0]) {
710
            /* candidate match */
711
88.9k
            for (j = mlast; j > 0; j--) {
712
80.8k
                if (s[i+j] != p[j]) {
713
56.0k
                    break;
714
56.0k
                }
715
80.8k
            }
716
64.0k
            if (j == 0) {
717
                /* got a match! */
718
8.08k
                return i;
719
8.08k
            }
720
            /* miss: check if previous character is part of pattern */
721
56.0k
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
722
54.7k
                i = i - m;
723
54.7k
            }
724
1.27k
            else {
725
1.27k
                i = i - skip;
726
1.27k
            }
727
56.0k
        }
728
1.71M
        else {
729
            /* skip: check if previous character is part of pattern */
730
1.71M
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
731
1.37M
                i = i - m;
732
1.37M
            }
733
1.71M
        }
734
1.77M
    }
735
104
    return -1;
736
8.18k
}
Unexecuted instantiation: bytesobject.c:stringlib_default_rfind
Unexecuted instantiation: unicodeobject.c:asciilib_default_rfind
Unexecuted instantiation: unicodeobject.c:ucs1lib_default_rfind
Unexecuted instantiation: unicodeobject.c:ucs2lib_default_rfind
Unexecuted instantiation: unicodeobject.c:ucs4lib_default_rfind
bytes_methods.c:stringlib_default_rfind
Line
Count
Source
693
8.18k
{
694
    /* create compressed boyer-moore delta 1 table */
695
8.18k
    unsigned long mask = 0;
696
8.18k
    Py_ssize_t i, j, mlast = m - 1, skip = m - 1, w = n - m;
697
698
    /* process pattern[0] outside the loop */
699
8.18k
    STRINGLIB_BLOOM_ADD(mask, p[0]);
700
    /* process pattern[:0:-1] */
701
32.7k
    for (i = mlast; i > 0; i--) {
702
24.5k
        STRINGLIB_BLOOM_ADD(mask, p[i]);
703
24.5k
        if (p[i] == p[0]) {
704
0
            skip = i - 1;
705
0
        }
706
24.5k
    }
707
708
1.77M
    for (i = w; i >= 0; i--) {
709
1.77M
        if (s[i] == p[0]) {
710
            /* candidate match */
711
88.9k
            for (j = mlast; j > 0; j--) {
712
80.8k
                if (s[i+j] != p[j]) {
713
56.0k
                    break;
714
56.0k
                }
715
80.8k
            }
716
64.0k
            if (j == 0) {
717
                /* got a match! */
718
8.08k
                return i;
719
8.08k
            }
720
            /* miss: check if previous character is part of pattern */
721
56.0k
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
722
54.7k
                i = i - m;
723
54.7k
            }
724
1.27k
            else {
725
1.27k
                i = i - skip;
726
1.27k
            }
727
56.0k
        }
728
1.71M
        else {
729
            /* skip: check if previous character is part of pattern */
730
1.71M
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
731
1.37M
                i = i - m;
732
1.37M
            }
733
1.71M
        }
734
1.77M
    }
735
104
    return -1;
736
8.18k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_default_rfind
737
738
739
static inline Py_ssize_t
740
STRINGLIB(count_char)(const STRINGLIB_CHAR *s, Py_ssize_t n,
741
                      const STRINGLIB_CHAR p0, Py_ssize_t maxcount)
742
0
{
743
0
    Py_ssize_t i, count = 0;
744
0
    for (i = 0; i < n; i++) {
745
0
        if (s[i] == p0) {
746
0
            count++;
747
0
            if (count == maxcount) {
748
0
                return maxcount;
749
0
            }
750
0
        }
751
0
    }
752
0
    return count;
753
0
}
Unexecuted instantiation: bytesobject.c:stringlib_count_char
Unexecuted instantiation: unicodeobject.c:asciilib_count_char
Unexecuted instantiation: unicodeobject.c:ucs1lib_count_char
Unexecuted instantiation: unicodeobject.c:ucs2lib_count_char
Unexecuted instantiation: unicodeobject.c:ucs4lib_count_char
Unexecuted instantiation: bytes_methods.c:stringlib_count_char
Unexecuted instantiation: bytearrayobject.c:stringlib_count_char
754
755
756
static inline Py_ssize_t
757
STRINGLIB(count_char_no_maxcount)(const STRINGLIB_CHAR *s, Py_ssize_t n,
758
                                  const STRINGLIB_CHAR p0)
759
/* A specialized function of count_char that does not cut off at a maximum.
760
   As a result, the compiler is able to vectorize the loop. */
761
35.8M
{
762
35.8M
    Py_ssize_t count = 0;
763
4.78G
    for (Py_ssize_t i = 0; i < n; i++) {
764
4.75G
        if (s[i] == p0) {
765
924M
            count++;
766
924M
        }
767
4.75G
    }
768
35.8M
    return count;
769
35.8M
}
Unexecuted instantiation: bytesobject.c:stringlib_count_char_no_maxcount
Unexecuted instantiation: unicodeobject.c:asciilib_count_char_no_maxcount
unicodeobject.c:ucs1lib_count_char_no_maxcount
Line
Count
Source
761
23.1M
{
762
23.1M
    Py_ssize_t count = 0;
763
654M
    for (Py_ssize_t i = 0; i < n; i++) {
764
631M
        if (s[i] == p0) {
765
37.9M
            count++;
766
37.9M
        }
767
631M
    }
768
23.1M
    return count;
769
23.1M
}
unicodeobject.c:ucs2lib_count_char_no_maxcount
Line
Count
Source
761
4.33M
{
762
4.33M
    Py_ssize_t count = 0;
763
637M
    for (Py_ssize_t i = 0; i < n; i++) {
764
633M
        if (s[i] == p0) {
765
18.6M
            count++;
766
18.6M
        }
767
633M
    }
768
4.33M
    return count;
769
4.33M
}
unicodeobject.c:ucs4lib_count_char_no_maxcount
Line
Count
Source
761
2.42M
{
762
2.42M
    Py_ssize_t count = 0;
763
448M
    for (Py_ssize_t i = 0; i < n; i++) {
764
445M
        if (s[i] == p0) {
765
15.5M
            count++;
766
15.5M
        }
767
445M
    }
768
2.42M
    return count;
769
2.42M
}
bytes_methods.c:stringlib_count_char_no_maxcount
Line
Count
Source
761
5.94M
{
762
5.94M
    Py_ssize_t count = 0;
763
3.04G
    for (Py_ssize_t i = 0; i < n; i++) {
764
3.04G
        if (s[i] == p0) {
765
852M
            count++;
766
852M
        }
767
3.04G
    }
768
5.94M
    return count;
769
5.94M
}
Unexecuted instantiation: bytearrayobject.c:stringlib_count_char_no_maxcount
770
771
772
Py_LOCAL_INLINE(Py_ssize_t)
773
FASTSEARCH(const STRINGLIB_CHAR* s, Py_ssize_t n,
774
           const STRINGLIB_CHAR* p, Py_ssize_t m,
775
           Py_ssize_t maxcount, int mode)
776
95.7M
{
777
95.7M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
717
        return -1;
779
717
    }
780
781
    /* look for special cases */
782
95.7M
    if (m <= 1) {
783
57.7M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
57.7M
        if (mode == FAST_SEARCH)
788
21.8M
            return STRINGLIB(find_char)(s, n, p[0]);
789
35.9M
        else if (mode == FAST_RSEARCH)
790
56.4k
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
35.8M
        else {
792
35.8M
            if (maxcount == PY_SSIZE_T_MAX) {
793
35.8M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
35.8M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
35.8M
        }
797
57.7M
    }
798
799
37.9M
    if (mode != FAST_RSEARCH) {
800
37.9M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
37.9M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
37.9M
        }
803
22
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
22
            if (mode == FAST_SEARCH) {
810
22
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
22
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
22
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
37.9M
    }
825
8.18k
    else {
826
        /* FAST_RSEARCH */
827
8.18k
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
8.18k
    }
829
37.9M
}
bytesobject.c:fastsearch
Line
Count
Source
776
387k
{
777
387k
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
387k
    if (m <= 1) {
783
387k
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
387k
        if (mode == FAST_SEARCH)
788
387k
            return STRINGLIB(find_char)(s, n, p[0]);
789
0
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
0
        else {
792
0
            if (maxcount == PY_SSIZE_T_MAX) {
793
0
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
0
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
0
        }
797
387k
    }
798
799
0
    if (mode != FAST_RSEARCH) {
800
0
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
0
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
0
        }
803
0
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
0
            if (mode == FAST_SEARCH) {
810
0
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
0
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
0
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
0
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
0
}
unicodeobject.c:asciilib_fastsearch
Line
Count
Source
776
8.04M
{
777
8.04M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
8.04M
    if (m <= 1) {
783
5.70M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
5.70M
        if (mode == FAST_SEARCH)
788
5.64M
            return STRINGLIB(find_char)(s, n, p[0]);
789
56.4k
        else if (mode == FAST_RSEARCH)
790
56.4k
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
0
        else {
792
0
            if (maxcount == PY_SSIZE_T_MAX) {
793
0
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
0
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
0
        }
797
5.70M
    }
798
799
2.33M
    if (mode != FAST_RSEARCH) {
800
2.33M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
2.33M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
2.33M
        }
803
0
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
0
            if (mode == FAST_SEARCH) {
810
0
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
0
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
0
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
2.33M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
2.33M
}
unicodeobject.c:ucs1lib_fastsearch
Line
Count
Source
776
59.2M
{
777
59.2M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
59.2M
    if (m <= 1) {
783
31.3M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
31.3M
        if (mode == FAST_SEARCH)
788
8.21M
            return STRINGLIB(find_char)(s, n, p[0]);
789
23.1M
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
23.1M
        else {
792
23.1M
            if (maxcount == PY_SSIZE_T_MAX) {
793
23.1M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
23.1M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
23.1M
        }
797
31.3M
    }
798
799
27.9M
    if (mode != FAST_RSEARCH) {
800
27.9M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
27.9M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
27.9M
        }
803
22
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
22
            if (mode == FAST_SEARCH) {
810
22
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
22
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
22
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
27.9M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
27.9M
}
unicodeobject.c:ucs2lib_fastsearch
Line
Count
Source
776
11.6M
{
777
11.6M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
703
        return -1;
779
703
    }
780
781
    /* look for special cases */
782
11.6M
    if (m <= 1) {
783
8.34M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
8.34M
        if (mode == FAST_SEARCH)
788
4.00M
            return STRINGLIB(find_char)(s, n, p[0]);
789
4.33M
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
4.33M
        else {
792
4.33M
            if (maxcount == PY_SSIZE_T_MAX) {
793
4.33M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
4.33M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
4.33M
        }
797
8.34M
    }
798
799
3.26M
    if (mode != FAST_RSEARCH) {
800
3.26M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
3.26M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
3.26M
        }
803
0
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
0
            if (mode == FAST_SEARCH) {
810
0
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
0
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
0
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
3.26M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
3.26M
}
unicodeobject.c:ucs4lib_fastsearch
Line
Count
Source
776
10.4M
{
777
10.4M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
10.4M
    if (m <= 1) {
783
6.02M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
6.02M
        if (mode == FAST_SEARCH)
788
3.60M
            return STRINGLIB(find_char)(s, n, p[0]);
789
2.42M
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
2.42M
        else {
792
2.42M
            if (maxcount == PY_SSIZE_T_MAX) {
793
2.42M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
2.42M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
2.42M
        }
797
6.02M
    }
798
799
4.40M
    if (mode != FAST_RSEARCH) {
800
4.40M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
4.40M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
4.40M
        }
803
0
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
0
            if (mode == FAST_SEARCH) {
810
0
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
0
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
0
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
4.40M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
4.40M
}
bytes_methods.c:fastsearch
Line
Count
Source
776
5.95M
{
777
5.95M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
14
        return -1;
779
14
    }
780
781
    /* look for special cases */
782
5.95M
    if (m <= 1) {
783
5.94M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
5.94M
        if (mode == FAST_SEARCH)
788
0
            return STRINGLIB(find_char)(s, n, p[0]);
789
5.94M
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
5.94M
        else {
792
5.94M
            if (maxcount == PY_SSIZE_T_MAX) {
793
5.94M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
5.94M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
5.94M
        }
797
5.94M
    }
798
799
11.1k
    if (mode != FAST_RSEARCH) {
800
3.00k
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
3.00k
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
3.00k
        }
803
0
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
0
            if (mode == FAST_SEARCH) {
810
0
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
0
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
0
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
3.00k
    }
825
8.18k
    else {
826
        /* FAST_RSEARCH */
827
8.18k
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
8.18k
    }
829
11.1k
}
Unexecuted instantiation: bytearrayobject.c:fastsearch
830