Coverage Report

Created: 2026-10-17 06:15

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
253M
#define FAST_COUNT 0
25
103M
#define FAST_SEARCH 1
26
65.9M
#define FAST_RSEARCH 2
27
28
#if LONG_BIT >= 128
29
#define STRINGLIB_BLOOM_WIDTH 128
30
#elif LONG_BIT >= 64
31
1.06G
#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
212M
    ((mask |= (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
40
#define STRINGLIB_BLOOM(mask, ch)     \
41
852M
    ((mask &  (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
42
43
#ifdef STRINGLIB_FAST_MEMCHR
44
219M
#  define MEMCHR_CUT_OFF 15
45
#else
46
4.99M
#  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
224M
{
52
224M
    const STRINGLIB_CHAR *p, *e;
53
54
224M
    p = s;
55
224M
    e = s + n;
56
224M
    if (n > MEMCHR_CUT_OFF) {
57
#ifdef STRINGLIB_FAST_MEMCHR
58
33.5M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
33.5M
        if (p != NULL)
60
32.2M
            return (p - s);
61
1.28M
        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
4.57M
        if (needle != 0) {
71
4.58M
            do {
72
4.58M
                const void *candidate = memchr(p, needle,
73
4.58M
                                               (e - p) * sizeof(STRINGLIB_CHAR));
74
4.58M
                if (candidate == NULL)
75
17.9k
                    return -1;
76
4.57M
                s1 = p;
77
4.57M
                p = (const STRINGLIB_CHAR *)
78
4.57M
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
4.57M
                if (*p == ch)
80
4.54M
                    return (p - s);
81
                /* False positive */
82
27.0k
                p++;
83
27.0k
                if (p - s1 > MEMCHR_CUT_OFF)
84
10.9k
                    continue;
85
16.1k
                if (e - p <= MEMCHR_CUT_OFF)
86
2.12k
                    break;
87
13.9k
                e1 = p + MEMCHR_CUT_OFF;
88
472k
                while (p != e1) {
89
461k
                    if (*p == ch)
90
2.88k
                        return (p - s);
91
458k
                    p++;
92
458k
                }
93
13.9k
            }
94
4.56M
            while (e - p > MEMCHR_CUT_OFF);
95
4.56M
        }
96
#endif
97
38.1M
    }
98
954M
    while (p < e) {
99
785M
        if (*p == ch)
100
17.3M
            return (p - s);
101
768M
        p++;
102
768M
    }
103
168M
    return -1;
104
186M
}
bytesobject.c:stringlib_find_char
Line
Count
Source
51
281k
{
52
281k
    const STRINGLIB_CHAR *p, *e;
53
54
281k
    p = s;
55
281k
    e = s + n;
56
281k
    if (n > MEMCHR_CUT_OFF) {
57
281k
#ifdef STRINGLIB_FAST_MEMCHR
58
281k
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
281k
        if (p != NULL)
60
278k
            return (p - s);
61
3.18k
        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
281k
    }
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
198M
{
52
198M
    const STRINGLIB_CHAR *p, *e;
53
54
198M
    p = s;
55
198M
    e = s + n;
56
198M
    if (n > MEMCHR_CUT_OFF) {
57
21.9M
#ifdef STRINGLIB_FAST_MEMCHR
58
21.9M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
21.9M
        if (p != NULL)
60
21.1M
            return (p - s);
61
829k
        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
21.9M
    }
98
877M
    while (p < e) {
99
713M
        if (*p == ch)
100
11.9M
            return (p - s);
101
701M
        p++;
102
701M
    }
103
164M
    return -1;
104
176M
}
unicodeobject.c:ucs2lib_find_char
Line
Count
Source
51
4.92M
{
52
4.92M
    const STRINGLIB_CHAR *p, *e;
53
54
4.92M
    p = s;
55
4.92M
    e = s + n;
56
4.92M
    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
4.57M
        const STRINGLIB_CHAR *s1, *e1;
66
4.57M
        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
4.57M
        if (needle != 0) {
71
4.58M
            do {
72
4.58M
                const void *candidate = memchr(p, needle,
73
4.58M
                                               (e - p) * sizeof(STRINGLIB_CHAR));
74
4.58M
                if (candidate == NULL)
75
17.9k
                    return -1;
76
4.57M
                s1 = p;
77
4.57M
                p = (const STRINGLIB_CHAR *)
78
4.57M
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
4.57M
                if (*p == ch)
80
4.54M
                    return (p - s);
81
                /* False positive */
82
27.0k
                p++;
83
27.0k
                if (p - s1 > MEMCHR_CUT_OFF)
84
10.9k
                    continue;
85
16.1k
                if (e - p <= MEMCHR_CUT_OFF)
86
2.12k
                    break;
87
13.9k
                e1 = p + MEMCHR_CUT_OFF;
88
472k
                while (p != e1) {
89
461k
                    if (*p == ch)
90
2.88k
                        return (p - s);
91
458k
                    p++;
92
458k
                }
93
13.9k
            }
94
4.56M
            while (e - p > MEMCHR_CUT_OFF);
95
4.56M
        }
96
4.57M
#endif
97
4.57M
    }
98
4.88M
    while (p < e) {
99
4.68M
        if (*p == ch)
100
159k
            return (p - s);
101
4.52M
        p++;
102
4.52M
    }
103
195k
    return -1;
104
355k
}
unicodeobject.c:ucs4lib_find_char
Line
Count
Source
51
4.07M
{
52
4.07M
    const STRINGLIB_CHAR *p, *e;
53
54
4.07M
    p = s;
55
4.07M
    e = s + n;
56
4.07M
    if (n > MEMCHR_CUT_OFF) {
57
4.02M
#ifdef STRINGLIB_FAST_MEMCHR
58
4.02M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
4.02M
        if (p != NULL)
60
4.01M
            return (p - s);
61
11.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.02M
    }
98
166k
    while (p < e) {
99
139k
        if (*p == ch)
100
16.4k
            return (p - s);
101
122k
        p++;
102
122k
    }
103
27.2k
    return -1;
104
43.7k
}
unicodeobject.c:asciilib_find_char
Line
Count
Source
51
4.83M
{
52
4.83M
    const STRINGLIB_CHAR *p, *e;
53
54
4.83M
    p = s;
55
4.83M
    e = s + n;
56
4.83M
    if (n > MEMCHR_CUT_OFF) {
57
4.01M
#ifdef STRINGLIB_FAST_MEMCHR
58
4.01M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
4.01M
        if (p != NULL)
60
3.62M
            return (p - s);
61
390k
        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.01M
    }
98
2.63M
    while (p < e) {
99
2.58M
        if (*p == ch)
100
773k
            return (p - s);
101
1.81M
        p++;
102
1.81M
    }
103
49.9k
    return -1;
104
823k
}
bytes_methods.c:stringlib_find_char
Line
Count
Source
51
11.7M
{
52
11.7M
    const STRINGLIB_CHAR *p, *e;
53
54
11.7M
    p = s;
55
11.7M
    e = s + n;
56
11.7M
    if (n > MEMCHR_CUT_OFF) {
57
3.27M
#ifdef STRINGLIB_FAST_MEMCHR
58
3.27M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
3.27M
        if (p != NULL)
60
3.22M
            return (p - s);
61
53.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
3.27M
    }
98
68.8M
    while (p < e) {
99
64.8M
        if (*p == ch)
100
4.42M
            return (p - s);
101
60.4M
        p++;
102
60.4M
    }
103
4.04M
    return -1;
104
8.47M
}
Unexecuted instantiation: bytearrayobject.c:stringlib_find_char
105
106
#undef MEMCHR_CUT_OFF
107
108
#if STRINGLIB_SIZEOF_CHAR == 1
109
1.29M
#  define MEMRCHR_CUT_OFF 15
110
#else
111
156k
#  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
1.43M
{
118
1.43M
    const STRINGLIB_CHAR *p;
119
1.43M
#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
1.43M
    if (n > MEMRCHR_CUT_OFF) {
126
#if STRINGLIB_SIZEOF_CHAR == 1
127
        p = memrchr(s, ch, n);
128
163k
        if (p != NULL)
129
115k
            return (p - s);
130
48.0k
        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
130k
        if (needle != 0) {
141
136k
            do {
142
136k
                void *candidate = memrchr(s, needle,
143
136k
                                          n * sizeof(STRINGLIB_CHAR));
144
136k
                if (candidate == NULL)
145
1.27k
                    return -1;
146
134k
                n1 = n;
147
134k
                p = (const STRINGLIB_CHAR *)
148
134k
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
134k
                n = p - s;
150
134k
                if (*p == ch)
151
128k
                    return n;
152
                /* False positive */
153
6.03k
                if (n1 - n > MEMRCHR_CUT_OFF)
154
3.04k
                    continue;
155
2.98k
                if (n <= MEMRCHR_CUT_OFF)
156
83
                    break;
157
2.90k
                s1 = p - MEMRCHR_CUT_OFF;
158
113k
                while (p > s1) {
159
110k
                    p--;
160
110k
                    if (*p == ch)
161
207
                        return (p - s);
162
110k
                }
163
2.69k
                n = p - s;
164
2.69k
            }
165
130k
            while (n > MEMRCHR_CUT_OFF);
166
130k
        }
167
#endif
168
294k
    }
169
1.14M
#endif  /* HAVE_MEMRCHR */
170
1.14M
    p = s + n;
171
6.05M
    while (p > s) {
172
5.24M
        p--;
173
5.24M
        if (*p == ch)
174
335k
            return (p - s);
175
5.24M
    }
176
809k
    return -1;
177
1.14M
}
Unexecuted instantiation: bytesobject.c:stringlib_rfind_char
unicodeobject.c:ucs1lib_rfind_char
Line
Count
Source
117
56.7k
{
118
56.7k
    const STRINGLIB_CHAR *p;
119
56.7k
#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.7k
    if (n > MEMRCHR_CUT_OFF) {
126
49.2k
#if STRINGLIB_SIZEOF_CHAR == 1
127
49.2k
        p = memrchr(s, ch, n);
128
49.2k
        if (p != NULL)
129
47.5k
            return (p - s);
130
1.72k
        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
49.2k
    }
169
7.52k
#endif  /* HAVE_MEMRCHR */
170
7.52k
    p = s + n;
171
41.5k
    while (p > s) {
172
34.7k
        p--;
173
34.7k
        if (*p == ch)
174
719
            return (p - s);
175
34.7k
    }
176
6.80k
    return -1;
177
7.52k
}
unicodeobject.c:ucs2lib_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
123k
        const STRINGLIB_CHAR *s1;
135
123k
        Py_ssize_t n1;
136
123k
        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
123k
        if (needle != 0) {
141
125k
            do {
142
125k
                void *candidate = memrchr(s, needle,
143
125k
                                          n * sizeof(STRINGLIB_CHAR));
144
125k
                if (candidate == NULL)
145
765
                    return -1;
146
124k
                n1 = n;
147
124k
                p = (const STRINGLIB_CHAR *)
148
124k
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
124k
                n = p - s;
150
124k
                if (*p == ch)
151
122k
                    return n;
152
                /* False positive */
153
2.30k
                if (n1 - n > MEMRCHR_CUT_OFF)
154
1.08k
                    continue;
155
1.22k
                if (n <= MEMRCHR_CUT_OFF)
156
45
                    break;
157
1.18k
                s1 = p - MEMRCHR_CUT_OFF;
158
45.8k
                while (p > s1) {
159
44.7k
                    p--;
160
44.7k
                    if (*p == ch)
161
91
                        return (p - s);
162
44.7k
                }
163
1.08k
                n = p - s;
164
1.08k
            }
165
123k
            while (n > MEMRCHR_CUT_OFF);
166
123k
        }
167
123k
#endif
168
123k
    }
169
6.44k
#endif  /* HAVE_MEMRCHR */
170
6.44k
    p = s + n;
171
31.6k
    while (p > s) {
172
30.3k
        p--;
173
30.3k
        if (*p == ch)
174
5.17k
            return (p - s);
175
30.3k
    }
176
1.26k
    return -1;
177
6.44k
}
unicodeobject.c:ucs4lib_rfind_char
Line
Count
Source
117
9.56k
{
118
9.56k
    const STRINGLIB_CHAR *p;
119
9.56k
#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
9.56k
    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
7.36k
        const STRINGLIB_CHAR *s1;
135
7.36k
        Py_ssize_t n1;
136
7.36k
        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
7.36k
        if (needle != 0) {
141
10.8k
            do {
142
10.8k
                void *candidate = memrchr(s, needle,
143
10.8k
                                          n * sizeof(STRINGLIB_CHAR));
144
10.8k
                if (candidate == NULL)
145
508
                    return -1;
146
10.3k
                n1 = n;
147
10.3k
                p = (const STRINGLIB_CHAR *)
148
10.3k
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
10.3k
                n = p - s;
150
10.3k
                if (*p == ch)
151
6.62k
                    return n;
152
                /* False positive */
153
3.72k
                if (n1 - n > MEMRCHR_CUT_OFF)
154
1.96k
                    continue;
155
1.75k
                if (n <= MEMRCHR_CUT_OFF)
156
38
                    break;
157
1.72k
                s1 = p - MEMRCHR_CUT_OFF;
158
67.6k
                while (p > s1) {
159
65.9k
                    p--;
160
65.9k
                    if (*p == ch)
161
116
                        return (p - s);
162
65.9k
                }
163
1.60k
                n = p - s;
164
1.60k
            }
165
7.36k
            while (n > MEMRCHR_CUT_OFF);
166
7.36k
        }
167
7.36k
#endif
168
7.36k
    }
169
2.31k
#endif  /* HAVE_MEMRCHR */
170
2.31k
    p = s + n;
171
19.3k
    while (p > s) {
172
18.2k
        p--;
173
18.2k
        if (*p == ch)
174
1.22k
            return (p - s);
175
18.2k
    }
176
1.09k
    return -1;
177
2.31k
}
unicodeobject.c:asciilib_rfind_char
Line
Count
Source
117
51.6k
{
118
51.6k
    const STRINGLIB_CHAR *p;
119
51.6k
#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
51.6k
    if (n > MEMRCHR_CUT_OFF) {
126
16.4k
#if STRINGLIB_SIZEOF_CHAR == 1
127
16.4k
        p = memrchr(s, ch, n);
128
16.4k
        if (p != NULL)
129
16.1k
            return (p - s);
130
297
        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
16.4k
    }
169
35.1k
#endif  /* HAVE_MEMRCHR */
170
35.1k
    p = s + n;
171
207k
    while (p > s) {
172
182k
        p--;
173
182k
        if (*p == ch)
174
11.0k
            return (p - s);
175
182k
    }
176
24.1k
    return -1;
177
35.1k
}
bytes_methods.c:stringlib_rfind_char
Line
Count
Source
117
1.19M
{
118
1.19M
    const STRINGLIB_CHAR *p;
119
1.19M
#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
1.19M
    if (n > MEMRCHR_CUT_OFF) {
126
98.1k
#if STRINGLIB_SIZEOF_CHAR == 1
127
98.1k
        p = memrchr(s, ch, n);
128
98.1k
        if (p != NULL)
129
52.1k
            return (p - s);
130
46.0k
        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
98.1k
    }
169
1.09M
#endif  /* HAVE_MEMRCHR */
170
1.09M
    p = s + n;
171
5.75M
    while (p > s) {
172
4.98M
        p--;
173
4.98M
        if (*p == ch)
174
316k
            return (p - s);
175
4.98M
    }
176
776k
    return -1;
177
1.09M
}
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
78.0k
#define TABLE_SIZE_BITS 6u
312
78.0k
#define TABLE_SIZE (1U << TABLE_SIZE_BITS)
313
76.5k
#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
76.1k
            for (;;) {
459
76.1k
                LOG_LINEUP();
460
76.1k
                Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
461
76.1k
                window_last += shift;
462
76.1k
                if (shift == 0) {
463
15.1k
                    break;
464
15.1k
                }
465
61.0k
                if (window_last >= haystack_end) {
466
18
                    return -1;
467
18
                }
468
60.9k
                LOG("Horspool skip\n");
469
60.9k
            }
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.4k
            for (; i < len_needle; i++) {
475
15.3k
                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.3k
            }
489
245
            for (Py_ssize_t i = 0; i < cut; i++) {
490
242
                if (needle[i] != window[i]) {
491
102
                    LOG("Left half does not match.\n");
492
102
                    window_last += period;
493
102
                    goto windowloop;
494
102
                }
495
242
            }
496
3
            LOG("Found a match!\n");
497
3
            return window - haystack;
498
105
        }
499
15.1k
    }
500
1
    LOG("Not found. Returning -1.\n");
501
1
    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
76.1k
            for (;;) {
459
76.1k
                LOG_LINEUP();
460
76.1k
                Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
461
76.1k
                window_last += shift;
462
76.1k
                if (shift == 0) {
463
15.1k
                    break;
464
15.1k
                }
465
61.0k
                if (window_last >= haystack_end) {
466
18
                    return -1;
467
18
                }
468
60.9k
                LOG("Horspool skip\n");
469
60.9k
            }
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.4k
            for (; i < len_needle; i++) {
475
15.3k
                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.3k
            }
489
245
            for (Py_ssize_t i = 0; i < cut; i++) {
490
242
                if (needle[i] != window[i]) {
491
102
                    LOG("Left half does not match.\n");
492
102
                    window_last += period;
493
102
                    goto windowloop;
494
102
                }
495
242
            }
496
3
            LOG("Found a match!\n");
497
3
            return window - haystack;
498
105
        }
499
15.1k
    }
500
1
    LOG("Not found. Returning -1.\n");
501
1
    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
38.5M
{
561
38.5M
    const Py_ssize_t w = n - m;
562
38.5M
    Py_ssize_t mlast = m - 1, count = 0;
563
38.5M
    Py_ssize_t gap = mlast;
564
38.5M
    const STRINGLIB_CHAR last = p[mlast];
565
38.5M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
38.5M
    unsigned long mask = 0;
568
212M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
174M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
174M
        if (p[i] == last) {
571
2.17M
            gap = mlast - i - 1;
572
2.17M
        }
573
174M
    }
574
38.5M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
904M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
876M
        if (ss[i] == last) {
578
            /* candidate match */
579
40.5M
            Py_ssize_t j;
580
62.9M
            for (j = 0; j < mlast; j++) {
581
40.7M
                if (s[i+j] != p[j]) {
582
18.3M
                    break;
583
18.3M
                }
584
40.7M
            }
585
40.5M
            if (j == mlast) {
586
                /* got a match! */
587
22.2M
                if (mode != FAST_COUNT) {
588
11.2M
                    return i;
589
11.2M
                }
590
11.0M
                count++;
591
11.0M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
11.0M
                i = i + mlast;
595
11.0M
                continue;
596
11.0M
            }
597
            /* miss: check if next character is part of pattern */
598
18.3M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
4.52M
                i = i + m;
600
4.52M
            }
601
13.7M
            else {
602
13.7M
                i = i + gap;
603
13.7M
            }
604
18.3M
        }
605
836M
        else {
606
            /* skip: check if next character is part of pattern */
607
836M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
711M
                i = i + m;
609
711M
            }
610
836M
        }
611
876M
    }
612
27.3M
    return mode == FAST_COUNT ? count : -1;
613
38.5M
}
Unexecuted instantiation: bytesobject.c:stringlib_default_find
unicodeobject.c:asciilib_default_find
Line
Count
Source
560
2.29M
{
561
2.29M
    const Py_ssize_t w = n - m;
562
2.29M
    Py_ssize_t mlast = m - 1, count = 0;
563
2.29M
    Py_ssize_t gap = mlast;
564
2.29M
    const STRINGLIB_CHAR last = p[mlast];
565
2.29M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
2.29M
    unsigned long mask = 0;
568
4.61M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
2.32M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
2.32M
        if (p[i] == last) {
571
8.46k
            gap = mlast - i - 1;
572
8.46k
        }
573
2.32M
    }
574
2.29M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
41.1M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
41.1M
        if (ss[i] == last) {
578
            /* candidate match */
579
3.91M
            Py_ssize_t j;
580
6.17M
            for (j = 0; j < mlast; j++) {
581
3.91M
                if (s[i+j] != p[j]) {
582
1.65M
                    break;
583
1.65M
                }
584
3.91M
            }
585
3.91M
            if (j == mlast) {
586
                /* got a match! */
587
2.25M
                if (mode != FAST_COUNT) {
588
2.25M
                    return i;
589
2.25M
                }
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.65M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
52.6k
                i = i + m;
600
52.6k
            }
601
1.60M
            else {
602
1.60M
                i = i + gap;
603
1.60M
            }
604
1.65M
        }
605
37.2M
        else {
606
            /* skip: check if next character is part of pattern */
607
37.2M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
33.6M
                i = i + m;
609
33.6M
            }
610
37.2M
        }
611
41.1M
    }
612
34.5k
    return mode == FAST_COUNT ? count : -1;
613
2.29M
}
unicodeobject.c:ucs1lib_default_find
Line
Count
Source
560
29.1M
{
561
29.1M
    const Py_ssize_t w = n - m;
562
29.1M
    Py_ssize_t mlast = m - 1, count = 0;
563
29.1M
    Py_ssize_t gap = mlast;
564
29.1M
    const STRINGLIB_CHAR last = p[mlast];
565
29.1M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
29.1M
    unsigned long mask = 0;
568
194M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
164M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
164M
        if (p[i] == last) {
571
2.11M
            gap = mlast - i - 1;
572
2.11M
        }
573
164M
    }
574
29.1M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
440M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
413M
        if (ss[i] == last) {
578
            /* candidate match */
579
15.8M
            Py_ssize_t j;
580
22.1M
            for (j = 0; j < mlast; j++) {
581
16.0M
                if (s[i+j] != p[j]) {
582
9.75M
                    break;
583
9.75M
                }
584
16.0M
            }
585
15.8M
            if (j == mlast) {
586
                /* got a match! */
587
6.13M
                if (mode != FAST_COUNT) {
588
1.97M
                    return i;
589
1.97M
                }
590
4.15M
                count++;
591
4.15M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
4.15M
                i = i + mlast;
595
4.15M
                continue;
596
4.15M
            }
597
            /* miss: check if next character is part of pattern */
598
9.75M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
1.99M
                i = i + m;
600
1.99M
            }
601
7.75M
            else {
602
7.75M
                i = i + gap;
603
7.75M
            }
604
9.75M
        }
605
397M
        else {
606
            /* skip: check if next character is part of pattern */
607
397M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
290M
                i = i + m;
609
290M
            }
610
397M
        }
611
413M
    }
612
27.1M
    return mode == FAST_COUNT ? count : -1;
613
29.1M
}
unicodeobject.c:ucs2lib_default_find
Line
Count
Source
560
3.25M
{
561
3.25M
    const Py_ssize_t w = n - m;
562
3.25M
    Py_ssize_t mlast = m - 1, count = 0;
563
3.25M
    Py_ssize_t gap = mlast;
564
3.25M
    const STRINGLIB_CHAR last = p[mlast];
565
3.25M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
3.25M
    unsigned long mask = 0;
568
6.52M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
3.27M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
3.27M
        if (p[i] == last) {
571
36.6k
            gap = mlast - i - 1;
572
36.6k
        }
573
3.27M
    }
574
3.25M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
185M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
185M
        if (ss[i] == last) {
578
            /* candidate match */
579
10.1M
            Py_ssize_t j;
580
16.3M
            for (j = 0; j < mlast; j++) {
581
10.1M
                if (s[i+j] != p[j]) {
582
3.89M
                    break;
583
3.89M
                }
584
10.1M
            }
585
10.1M
            if (j == mlast) {
586
                /* got a match! */
587
6.22M
                if (mode != FAST_COUNT) {
588
3.16M
                    return i;
589
3.16M
                }
590
3.06M
                count++;
591
3.06M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
3.06M
                i = i + mlast;
595
3.06M
                continue;
596
3.06M
            }
597
            /* miss: check if next character is part of pattern */
598
3.89M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
1.06M
                i = i + m;
600
1.06M
            }
601
2.83M
            else {
602
2.83M
                i = i + gap;
603
2.83M
            }
604
3.89M
        }
605
175M
        else {
606
            /* skip: check if next character is part of pattern */
607
175M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
169M
                i = i + m;
609
169M
            }
610
175M
        }
611
185M
    }
612
89.9k
    return mode == FAST_COUNT ? count : -1;
613
3.25M
}
unicodeobject.c:ucs4lib_default_find
Line
Count
Source
560
3.83M
{
561
3.83M
    const Py_ssize_t w = n - m;
562
3.83M
    Py_ssize_t mlast = m - 1, count = 0;
563
3.83M
    Py_ssize_t gap = mlast;
564
3.83M
    const STRINGLIB_CHAR last = p[mlast];
565
3.83M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
3.83M
    unsigned long mask = 0;
568
7.67M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
3.84M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
3.84M
        if (p[i] == last) {
571
18.6k
            gap = mlast - i - 1;
572
18.6k
        }
573
3.84M
    }
574
3.83M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
233M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
233M
        if (ss[i] == last) {
578
            /* candidate match */
579
10.5M
            Py_ssize_t j;
580
18.1M
            for (j = 0; j < mlast; j++) {
581
10.5M
                if (s[i+j] != p[j]) {
582
2.97M
                    break;
583
2.97M
                }
584
10.5M
            }
585
10.5M
            if (j == mlast) {
586
                /* got a match! */
587
7.59M
                if (mode != FAST_COUNT) {
588
3.80M
                    return i;
589
3.80M
                }
590
3.78M
                count++;
591
3.78M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
3.78M
                i = i + mlast;
595
3.78M
                continue;
596
3.78M
            }
597
            /* miss: check if next character is part of pattern */
598
2.97M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
1.39M
                i = i + m;
600
1.39M
            }
601
1.58M
            else {
602
1.58M
                i = i + gap;
603
1.58M
            }
604
2.97M
        }
605
223M
        else {
606
            /* skip: check if next character is part of pattern */
607
223M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
216M
                i = i + m;
609
216M
            }
610
223M
        }
611
233M
    }
612
26.7k
    return mode == FAST_COUNT ? count : -1;
613
3.83M
}
bytes_methods.c:stringlib_default_find
Line
Count
Source
560
2.71k
{
561
2.71k
    const Py_ssize_t w = n - m;
562
2.71k
    Py_ssize_t mlast = m - 1, count = 0;
563
2.71k
    Py_ssize_t gap = mlast;
564
2.71k
    const STRINGLIB_CHAR last = p[mlast];
565
2.71k
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
2.71k
    unsigned long mask = 0;
568
10.8k
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
8.13k
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
8.13k
        if (p[i] == last) {
571
2.71k
            gap = mlast - i - 1;
572
2.71k
        }
573
8.13k
    }
574
2.71k
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
2.83M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
2.83M
        if (ss[i] == last) {
578
            /* candidate match */
579
31.0k
            Py_ssize_t j;
580
38.9k
            for (j = 0; j < mlast; j++) {
581
36.4k
                if (s[i+j] != p[j]) {
582
28.6k
                    break;
583
28.6k
                }
584
36.4k
            }
585
31.0k
            if (j == mlast) {
586
                /* got a match! */
587
2.47k
                if (mode != FAST_COUNT) {
588
2.47k
                    return i;
589
2.47k
                }
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
28.6k
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
23.0k
                i = i + m;
600
23.0k
            }
601
5.52k
            else {
602
5.52k
                i = i + gap;
603
5.52k
            }
604
28.6k
        }
605
2.80M
        else {
606
            /* skip: check if next character is part of pattern */
607
2.80M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
1.11M
                i = i + m;
609
1.11M
            }
610
2.80M
        }
611
2.83M
    }
612
238
    return mode == FAST_COUNT ? count : -1;
613
2.71k
}
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
7.00k
{
694
    /* create compressed boyer-moore delta 1 table */
695
7.00k
    unsigned long mask = 0;
696
7.00k
    Py_ssize_t i, j, mlast = m - 1, skip = m - 1, w = n - m;
697
698
    /* process pattern[0] outside the loop */
699
7.00k
    STRINGLIB_BLOOM_ADD(mask, p[0]);
700
    /* process pattern[:0:-1] */
701
28.0k
    for (i = mlast; i > 0; i--) {
702
21.0k
        STRINGLIB_BLOOM_ADD(mask, p[i]);
703
21.0k
        if (p[i] == p[0]) {
704
0
            skip = i - 1;
705
0
        }
706
21.0k
    }
707
708
1.47M
    for (i = w; i >= 0; i--) {
709
1.47M
        if (s[i] == p[0]) {
710
            /* candidate match */
711
85.0k
            for (j = mlast; j > 0; j--) {
712
78.1k
                if (s[i+j] != p[j]) {
713
56.9k
                    break;
714
56.9k
                }
715
78.1k
            }
716
63.8k
            if (j == 0) {
717
                /* got a match! */
718
6.91k
                return i;
719
6.91k
            }
720
            /* miss: check if previous character is part of pattern */
721
56.9k
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
722
55.7k
                i = i - m;
723
55.7k
            }
724
1.14k
            else {
725
1.14k
                i = i - skip;
726
1.14k
            }
727
56.9k
        }
728
1.41M
        else {
729
            /* skip: check if previous character is part of pattern */
730
1.41M
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
731
1.20M
                i = i - m;
732
1.20M
            }
733
1.41M
        }
734
1.47M
    }
735
91
    return -1;
736
7.00k
}
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
7.00k
{
694
    /* create compressed boyer-moore delta 1 table */
695
7.00k
    unsigned long mask = 0;
696
7.00k
    Py_ssize_t i, j, mlast = m - 1, skip = m - 1, w = n - m;
697
698
    /* process pattern[0] outside the loop */
699
7.00k
    STRINGLIB_BLOOM_ADD(mask, p[0]);
700
    /* process pattern[:0:-1] */
701
28.0k
    for (i = mlast; i > 0; i--) {
702
21.0k
        STRINGLIB_BLOOM_ADD(mask, p[i]);
703
21.0k
        if (p[i] == p[0]) {
704
0
            skip = i - 1;
705
0
        }
706
21.0k
    }
707
708
1.47M
    for (i = w; i >= 0; i--) {
709
1.47M
        if (s[i] == p[0]) {
710
            /* candidate match */
711
85.0k
            for (j = mlast; j > 0; j--) {
712
78.1k
                if (s[i+j] != p[j]) {
713
56.9k
                    break;
714
56.9k
                }
715
78.1k
            }
716
63.8k
            if (j == 0) {
717
                /* got a match! */
718
6.91k
                return i;
719
6.91k
            }
720
            /* miss: check if previous character is part of pattern */
721
56.9k
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
722
55.7k
                i = i - m;
723
55.7k
            }
724
1.14k
            else {
725
1.14k
                i = i - skip;
726
1.14k
            }
727
56.9k
        }
728
1.41M
        else {
729
            /* skip: check if previous character is part of pattern */
730
1.41M
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
731
1.20M
                i = i - m;
732
1.20M
            }
733
1.41M
        }
734
1.47M
    }
735
91
    return -1;
736
7.00k
}
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
27.2M
{
762
27.2M
    Py_ssize_t count = 0;
763
4.16G
    for (Py_ssize_t i = 0; i < n; i++) {
764
4.14G
        if (s[i] == p0) {
765
659M
            count++;
766
659M
        }
767
4.14G
    }
768
27.2M
    return count;
769
27.2M
}
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
20.8M
{
762
20.8M
    Py_ssize_t count = 0;
763
587M
    for (Py_ssize_t i = 0; i < n; i++) {
764
566M
        if (s[i] == p0) {
765
27.9M
            count++;
766
27.9M
        }
767
566M
    }
768
20.8M
    return count;
769
20.8M
}
unicodeobject.c:ucs2lib_count_char_no_maxcount
Line
Count
Source
761
748k
{
762
748k
    Py_ssize_t count = 0;
763
382M
    for (Py_ssize_t i = 0; i < n; i++) {
764
382M
        if (s[i] == p0) {
765
12.0M
            count++;
766
12.0M
        }
767
382M
    }
768
748k
    return count;
769
748k
}
unicodeobject.c:ucs4lib_count_char_no_maxcount
Line
Count
Source
761
98.2k
{
762
98.2k
    Py_ssize_t count = 0;
763
355M
    for (Py_ssize_t i = 0; i < n; i++) {
764
355M
        if (s[i] == p0) {
765
9.95M
            count++;
766
9.95M
        }
767
355M
    }
768
98.2k
    return count;
769
98.2k
}
bytes_methods.c:stringlib_count_char_no_maxcount
Line
Count
Source
761
5.54M
{
762
5.54M
    Py_ssize_t count = 0;
763
2.84G
    for (Py_ssize_t i = 0; i < n; i++) {
764
2.83G
        if (s[i] == p0) {
765
609M
            count++;
766
609M
        }
767
2.83G
    }
768
5.54M
    return count;
769
5.54M
}
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
86.5M
{
777
86.5M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
526
        return -1;
779
526
    }
780
781
    /* look for special cases */
782
86.5M
    if (m <= 1) {
783
47.9M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
47.9M
        if (mode == FAST_SEARCH)
788
20.6M
            return STRINGLIB(find_char)(s, n, p[0]);
789
27.2M
        else if (mode == FAST_RSEARCH)
790
51.6k
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
27.2M
        else {
792
27.2M
            if (maxcount == PY_SSIZE_T_MAX) {
793
27.2M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
27.2M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
27.2M
        }
797
47.9M
    }
798
799
38.5M
    if (mode != FAST_RSEARCH) {
800
38.5M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
38.5M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
38.5M
        }
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
38.5M
    }
825
7.00k
    else {
826
        /* FAST_RSEARCH */
827
7.00k
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
7.00k
    }
829
38.5M
}
bytesobject.c:fastsearch
Line
Count
Source
776
281k
{
777
281k
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
281k
    if (m <= 1) {
783
281k
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
281k
        if (mode == FAST_SEARCH)
788
281k
            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
281k
    }
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
7.17M
{
777
7.17M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
7.17M
    if (m <= 1) {
783
4.88M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
4.88M
        if (mode == FAST_SEARCH)
788
4.83M
            return STRINGLIB(find_char)(s, n, p[0]);
789
51.6k
        else if (mode == FAST_RSEARCH)
790
51.6k
            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
4.88M
    }
798
799
2.29M
    if (mode != FAST_RSEARCH) {
800
2.29M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
2.29M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
2.29M
        }
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.29M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
2.29M
}
unicodeobject.c:ucs1lib_fastsearch
Line
Count
Source
776
58.4M
{
777
58.4M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
58.4M
    if (m <= 1) {
783
29.3M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
29.3M
        if (mode == FAST_SEARCH)
788
8.45M
            return STRINGLIB(find_char)(s, n, p[0]);
789
20.8M
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
20.8M
        else {
792
20.8M
            if (maxcount == PY_SSIZE_T_MAX) {
793
20.8M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
20.8M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
20.8M
        }
797
29.3M
    }
798
799
29.1M
    if (mode != FAST_RSEARCH) {
800
29.1M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
29.1M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
29.1M
        }
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
29.1M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
29.1M
}
unicodeobject.c:ucs2lib_fastsearch
Line
Count
Source
776
7.52M
{
777
7.52M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
512
        return -1;
779
512
    }
780
781
    /* look for special cases */
782
7.52M
    if (m <= 1) {
783
4.27M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
4.27M
        if (mode == FAST_SEARCH)
788
3.52M
            return STRINGLIB(find_char)(s, n, p[0]);
789
748k
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
748k
        else {
792
748k
            if (maxcount == PY_SSIZE_T_MAX) {
793
748k
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
748k
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
748k
        }
797
4.27M
    }
798
799
3.25M
    if (mode != FAST_RSEARCH) {
800
3.25M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
3.25M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
3.25M
        }
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.25M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
3.25M
}
unicodeobject.c:ucs4lib_fastsearch
Line
Count
Source
776
7.51M
{
777
7.51M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
7.51M
    if (m <= 1) {
783
3.68M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
3.68M
        if (mode == FAST_SEARCH)
788
3.58M
            return STRINGLIB(find_char)(s, n, p[0]);
789
98.2k
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
98.2k
        else {
792
98.2k
            if (maxcount == PY_SSIZE_T_MAX) {
793
98.2k
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
98.2k
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
98.2k
        }
797
3.68M
    }
798
799
3.83M
    if (mode != FAST_RSEARCH) {
800
3.83M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
3.83M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
3.83M
        }
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.83M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
3.83M
}
bytes_methods.c:fastsearch
Line
Count
Source
776
5.55M
{
777
5.55M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
14
        return -1;
779
14
    }
780
781
    /* look for special cases */
782
5.55M
    if (m <= 1) {
783
5.54M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
5.54M
        if (mode == FAST_SEARCH)
788
0
            return STRINGLIB(find_char)(s, n, p[0]);
789
5.54M
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
5.54M
        else {
792
5.54M
            if (maxcount == PY_SSIZE_T_MAX) {
793
5.54M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
5.54M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
5.54M
        }
797
5.54M
    }
798
799
9.71k
    if (mode != FAST_RSEARCH) {
800
2.71k
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
2.71k
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
2.71k
        }
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.71k
    }
825
7.00k
    else {
826
        /* FAST_RSEARCH */
827
7.00k
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
7.00k
    }
829
9.71k
}
Unexecuted instantiation: bytearrayobject.c:fastsearch
830