Coverage Report

Created: 2025-08-24 07:03

/src/cpython/Objects/stringlib/fastsearch.h
Line
Count
Source (jump to first uncovered line)
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
678M
#define FAST_COUNT 0
25
487M
#define FAST_SEARCH 1
26
84.4M
#define FAST_RSEARCH 2
27
28
#if LONG_BIT >= 128
29
#define STRINGLIB_BLOOM_WIDTH 128
30
#elif LONG_BIT >= 64
31
6.15G
#define STRINGLIB_BLOOM_WIDTH 64
32
#elif LONG_BIT >= 32
33
#define STRINGLIB_BLOOM_WIDTH 32
34
#else
35
#error "LONG_BIT is smaller than 32"
36
#endif
37
38
#define STRINGLIB_BLOOM_ADD(mask, ch) \
39
33.6M
    ((mask |= (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
40
#define STRINGLIB_BLOOM(mask, ch)     \
41
6.12G
    ((mask &  (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
42
43
#ifdef STRINGLIB_FAST_MEMCHR
44
244M
#  define MEMCHR_CUT_OFF 15
45
#else
46
74.6M
#  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
318M
{
52
318M
    const STRINGLIB_CHAR *p, *e;
53
54
318M
    p = s;
55
318M
    e = s + n;
56
318M
    if (n > MEMCHR_CUT_OFF) {
57
#ifdef STRINGLIB_FAST_MEMCHR
58
146M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
146M
        if (p != NULL)
60
144M
            return (p - s);
61
1.82M
        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
64.4M
        if (needle != 0) {
71
64.1M
            do {
72
64.1M
                void *candidate = memchr(p, needle,
73
64.1M
                                         (e - p) * sizeof(STRINGLIB_CHAR));
74
64.1M
                if (candidate == NULL)
75
441k
                    return -1;
76
63.6M
                s1 = p;
77
63.6M
                p = (const STRINGLIB_CHAR *)
78
63.6M
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
63.6M
                if (*p == ch)
80
63.5M
                    return (p - s);
81
                /* False positive */
82
119k
                p++;
83
119k
                if (p - s1 > MEMCHR_CUT_OFF)
84
55.9k
                    continue;
85
63.0k
                if (e - p <= MEMCHR_CUT_OFF)
86
3.54k
                    break;
87
59.5k
                e1 = p + MEMCHR_CUT_OFF;
88
1.77M
                while (p != e1) {
89
1.74M
                    if (*p == ch)
90
26.0k
                        return (p - s);
91
1.71M
                    p++;
92
1.71M
                }
93
59.5k
            }
94
64.0M
            while (e - p > MEMCHR_CUT_OFF);
95
64.0M
        }
96
#endif
97
210M
    }
98
438M
    while (p < e) {
99
348M
        if (*p == ch)
100
18.0M
            return (p - s);
101
330M
        p++;
102
330M
    }
103
90.3M
    return -1;
104
108M
}
Unexecuted instantiation: bytesobject.c:stringlib_find_char
unicodeobject.c:ucs1lib_find_char
Line
Count
Source
51
112M
{
52
112M
    const STRINGLIB_CHAR *p, *e;
53
54
112M
    p = s;
55
112M
    e = s + n;
56
112M
    if (n > MEMCHR_CUT_OFF) {
57
20.0M
#ifdef STRINGLIB_FAST_MEMCHR
58
20.0M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
20.0M
        if (p != NULL)
60
19.1M
            return (p - s);
61
908k
        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
                void *candidate = memchr(p, needle,
73
                                         (e - p) * sizeof(STRINGLIB_CHAR));
74
                if (candidate == NULL)
75
                    return -1;
76
                s1 = p;
77
                p = (const STRINGLIB_CHAR *)
78
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
                if (*p == ch)
80
                    return (p - s);
81
                /* False positive */
82
                p++;
83
                if (p - s1 > MEMCHR_CUT_OFF)
84
                    continue;
85
                if (e - p <= MEMCHR_CUT_OFF)
86
                    break;
87
                e1 = p + MEMCHR_CUT_OFF;
88
                while (p != e1) {
89
                    if (*p == ch)
90
                        return (p - s);
91
                    p++;
92
                }
93
            }
94
            while (e - p > MEMCHR_CUT_OFF);
95
        }
96
#endif
97
20.0M
    }
98
260M
    while (p < e) {
99
173M
        if (*p == ch)
100
4.73M
            return (p - s);
101
168M
        p++;
102
168M
    }
103
87.6M
    return -1;
104
92.3M
}
unicodeobject.c:ucs2lib_find_char
Line
Count
Source
51
74.2M
{
52
74.2M
    const STRINGLIB_CHAR *p, *e;
53
54
74.2M
    p = s;
55
74.2M
    e = s + n;
56
74.2M
    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
64.4M
        const STRINGLIB_CHAR *s1, *e1;
66
64.4M
        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
64.4M
        if (needle != 0) {
71
64.1M
            do {
72
64.1M
                void *candidate = memchr(p, needle,
73
64.1M
                                         (e - p) * sizeof(STRINGLIB_CHAR));
74
64.1M
                if (candidate == NULL)
75
441k
                    return -1;
76
63.6M
                s1 = p;
77
63.6M
                p = (const STRINGLIB_CHAR *)
78
63.6M
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
63.6M
                if (*p == ch)
80
63.5M
                    return (p - s);
81
                /* False positive */
82
119k
                p++;
83
119k
                if (p - s1 > MEMCHR_CUT_OFF)
84
55.9k
                    continue;
85
63.0k
                if (e - p <= MEMCHR_CUT_OFF)
86
3.54k
                    break;
87
59.5k
                e1 = p + MEMCHR_CUT_OFF;
88
1.77M
                while (p != e1) {
89
1.74M
                    if (*p == ch)
90
26.0k
                        return (p - s);
91
1.71M
                    p++;
92
1.71M
                }
93
59.5k
            }
94
64.0M
            while (e - p > MEMCHR_CUT_OFF);
95
64.0M
        }
96
64.4M
#endif
97
64.4M
    }
98
161M
    while (p < e) {
99
158M
        if (*p == ch)
100
7.61M
            return (p - s);
101
151M
        p++;
102
151M
    }
103
2.64M
    return -1;
104
10.2M
}
unicodeobject.c:ucs4lib_find_char
Line
Count
Source
51
102M
{
52
102M
    const STRINGLIB_CHAR *p, *e;
53
54
102M
    p = s;
55
102M
    e = s + n;
56
102M
    if (n > MEMCHR_CUT_OFF) {
57
102M
#ifdef STRINGLIB_FAST_MEMCHR
58
102M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
102M
        if (p != NULL)
60
102M
            return (p - s);
61
31.4k
        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
                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
102M
    }
98
262k
    while (p < e) {
99
232k
        if (*p == ch)
100
33.8k
            return (p - s);
101
198k
        p++;
102
198k
    }
103
29.7k
    return -1;
104
63.5k
}
unicodeobject.c:asciilib_find_char
Line
Count
Source
51
28.8M
{
52
28.8M
    const STRINGLIB_CHAR *p, *e;
53
54
28.8M
    p = s;
55
28.8M
    e = s + n;
56
28.8M
    if (n > MEMCHR_CUT_OFF) {
57
23.1M
#ifdef STRINGLIB_FAST_MEMCHR
58
23.1M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
23.1M
        if (p != NULL)
60
22.2M
            return (p - s);
61
881k
        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
                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
23.1M
    }
98
16.3M
    while (p < e) {
99
16.2M
        if (*p == ch)
100
5.70M
            return (p - s);
101
10.5M
        p++;
102
10.5M
    }
103
51.3k
    return -1;
104
5.75M
}
bytes_methods.c:stringlib_find_char
Line
Count
Source
51
1.71k
{
52
1.71k
    const STRINGLIB_CHAR *p, *e;
53
54
1.71k
    p = s;
55
1.71k
    e = s + n;
56
1.71k
    if (n > MEMCHR_CUT_OFF) {
57
1.71k
#ifdef STRINGLIB_FAST_MEMCHR
58
1.71k
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
1.71k
        if (p != NULL)
60
1.45k
            return (p - s);
61
258
        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
                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
1.71k
    }
98
0
    while (p < e) {
99
0
        if (*p == ch)
100
0
            return (p - s);
101
0
        p++;
102
0
    }
103
0
    return -1;
104
0
}
Unexecuted instantiation: bytearrayobject.c:stringlib_find_char
105
106
#undef MEMCHR_CUT_OFF
107
108
#if STRINGLIB_SIZEOF_CHAR == 1
109
37.6k
#  define MEMRCHR_CUT_OFF 15
110
#else
111
176k
#  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
194k
{
118
194k
    const STRINGLIB_CHAR *p;
119
194k
#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
194k
    if (n > MEMRCHR_CUT_OFF) {
126
#if STRINGLIB_SIZEOF_CHAR == 1
127
        p = memrchr(s, ch, n);
128
10.7k
        if (p != NULL)
129
6.94k
            return (p - s);
130
3.79k
        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
89.3k
        if (needle != 0) {
141
93.8k
            do {
142
93.8k
                void *candidate = memrchr(s, needle,
143
93.8k
                                          n * sizeof(STRINGLIB_CHAR));
144
93.8k
                if (candidate == NULL)
145
655
                    return -1;
146
93.2k
                n1 = n;
147
93.2k
                p = (const STRINGLIB_CHAR *)
148
93.2k
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
93.2k
                n = p - s;
150
93.2k
                if (*p == ch)
151
86.3k
                    return n;
152
                /* False positive */
153
6.86k
                if (n1 - n > MEMRCHR_CUT_OFF)
154
3.00k
                    continue;
155
3.86k
                if (n <= MEMRCHR_CUT_OFF)
156
816
                    break;
157
3.04k
                s1 = p - MEMRCHR_CUT_OFF;
158
110k
                while (p > s1) {
159
108k
                    p--;
160
108k
                    if (*p == ch)
161
594
                        return (p - s);
162
108k
                }
163
2.45k
                n = p - s;
164
2.45k
            }
165
89.3k
            while (n > MEMRCHR_CUT_OFF);
166
89.3k
        }
167
#endif
168
100k
    }
169
96.2k
#endif  /* HAVE_MEMRCHR */
170
96.2k
    p = s + n;
171
816k
    while (p > s) {
172
798k
        p--;
173
798k
        if (*p == ch)
174
78.5k
            return (p - s);
175
798k
    }
176
17.7k
    return -1;
177
96.2k
}
Unexecuted instantiation: bytesobject.c:stringlib_rfind_char
unicodeobject.c:ucs1lib_rfind_char
Line
Count
Source
117
7.59k
{
118
7.59k
    const STRINGLIB_CHAR *p;
119
7.59k
#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
7.59k
    if (n > MEMRCHR_CUT_OFF) {
126
4.04k
#if STRINGLIB_SIZEOF_CHAR == 1
127
4.04k
        p = memrchr(s, ch, n);
128
4.04k
        if (p != NULL)
129
3.04k
            return (p - s);
130
994
        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
4.04k
    }
169
3.54k
#endif  /* HAVE_MEMRCHR */
170
3.54k
    p = s + n;
171
10.2k
    while (p > s) {
172
9.51k
        p--;
173
9.51k
        if (*p == ch)
174
2.77k
            return (p - s);
175
9.51k
    }
176
779
    return -1;
177
3.54k
}
unicodeobject.c:ucs2lib_rfind_char
Line
Count
Source
117
33.4k
{
118
33.4k
    const STRINGLIB_CHAR *p;
119
33.4k
#ifdef HAVE_MEMRCHR
120
    /* memrchr() is a GNU extension, available since glibc 2.1.91.  it
121
       doesn't seem as optimized as memchr(), but is still quite
122
       faster than our hand-written loop below. There is no wmemrchr
123
       for 4-byte chars. */
124
125
33.4k
    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
13.8k
        const STRINGLIB_CHAR *s1;
135
13.8k
        Py_ssize_t n1;
136
13.8k
        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
13.8k
        if (needle != 0) {
141
16.3k
            do {
142
16.3k
                void *candidate = memrchr(s, needle,
143
16.3k
                                          n * sizeof(STRINGLIB_CHAR));
144
16.3k
                if (candidate == NULL)
145
386
                    return -1;
146
15.9k
                n1 = n;
147
15.9k
                p = (const STRINGLIB_CHAR *)
148
15.9k
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
15.9k
                n = p - s;
150
15.9k
                if (*p == ch)
151
12.5k
                    return n;
152
                /* False positive */
153
3.36k
                if (n1 - n > MEMRCHR_CUT_OFF)
154
1.07k
                    continue;
155
2.29k
                if (n <= MEMRCHR_CUT_OFF)
156
421
                    break;
157
1.87k
                s1 = p - MEMRCHR_CUT_OFF;
158
71.3k
                while (p > s1) {
159
69.7k
                    p--;
160
69.7k
                    if (*p == ch)
161
220
                        return (p - s);
162
69.7k
                }
163
1.65k
                n = p - s;
164
1.65k
            }
165
13.8k
            while (n > MEMRCHR_CUT_OFF);
166
13.8k
        }
167
13.8k
#endif
168
13.8k
    }
169
20.2k
#endif  /* HAVE_MEMRCHR */
170
20.2k
    p = s + n;
171
186k
    while (p > s) {
172
185k
        p--;
173
185k
        if (*p == ch)
174
18.6k
            return (p - s);
175
185k
    }
176
1.68k
    return -1;
177
20.2k
}
unicodeobject.c:ucs4lib_rfind_char
Line
Count
Source
117
123k
{
118
123k
    const STRINGLIB_CHAR *p;
119
123k
#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
123k
    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
75.4k
        const STRINGLIB_CHAR *s1;
135
75.4k
        Py_ssize_t n1;
136
75.4k
        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
75.4k
        if (needle != 0) {
141
77.5k
            do {
142
77.5k
                void *candidate = memrchr(s, needle,
143
77.5k
                                          n * sizeof(STRINGLIB_CHAR));
144
77.5k
                if (candidate == NULL)
145
269
                    return -1;
146
77.2k
                n1 = n;
147
77.2k
                p = (const STRINGLIB_CHAR *)
148
77.2k
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
77.2k
                n = p - s;
150
77.2k
                if (*p == ch)
151
73.7k
                    return n;
152
                /* False positive */
153
3.50k
                if (n1 - n > MEMRCHR_CUT_OFF)
154
1.93k
                    continue;
155
1.56k
                if (n <= MEMRCHR_CUT_OFF)
156
395
                    break;
157
1.17k
                s1 = p - MEMRCHR_CUT_OFF;
158
39.3k
                while (p > s1) {
159
38.5k
                    p--;
160
38.5k
                    if (*p == ch)
161
374
                        return (p - s);
162
38.5k
                }
163
800
                n = p - s;
164
800
            }
165
75.4k
            while (n > MEMRCHR_CUT_OFF);
166
75.4k
        }
167
75.4k
#endif
168
75.4k
    }
169
49.1k
#endif  /* HAVE_MEMRCHR */
170
49.1k
    p = s + n;
171
495k
    while (p > s) {
172
494k
        p--;
173
494k
        if (*p == ch)
174
47.8k
            return (p - s);
175
494k
    }
176
1.30k
    return -1;
177
49.1k
}
unicodeobject.c:asciilib_rfind_char
Line
Count
Source
117
9.59k
{
118
9.59k
    const STRINGLIB_CHAR *p;
119
9.59k
#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.59k
    if (n > MEMRCHR_CUT_OFF) {
126
3.15k
#if STRINGLIB_SIZEOF_CHAR == 1
127
3.15k
        p = memrchr(s, ch, n);
128
3.15k
        if (p != NULL)
129
3.06k
            return (p - s);
130
86
        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
3.15k
    }
169
6.44k
#endif  /* HAVE_MEMRCHR */
170
6.44k
    p = s + n;
171
36.0k
    while (p > s) {
172
33.9k
        p--;
173
33.9k
        if (*p == ch)
174
4.34k
            return (p - s);
175
33.9k
    }
176
2.10k
    return -1;
177
6.44k
}
bytes_methods.c:stringlib_rfind_char
Line
Count
Source
117
20.4k
{
118
20.4k
    const STRINGLIB_CHAR *p;
119
20.4k
#ifdef HAVE_MEMRCHR
120
    /* memrchr() is a GNU extension, available since glibc 2.1.91.  it
121
       doesn't seem as optimized as memchr(), but is still quite
122
       faster than our hand-written loop below. There is no wmemrchr
123
       for 4-byte chars. */
124
125
20.4k
    if (n > MEMRCHR_CUT_OFF) {
126
3.54k
#if STRINGLIB_SIZEOF_CHAR == 1
127
3.54k
        p = memrchr(s, ch, n);
128
3.54k
        if (p != NULL)
129
834
            return (p - s);
130
2.71k
        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
3.54k
    }
169
16.8k
#endif  /* HAVE_MEMRCHR */
170
16.8k
    p = s + n;
171
87.4k
    while (p > s) {
172
75.6k
        p--;
173
75.6k
        if (*p == ch)
174
5.02k
            return (p - s);
175
75.6k
    }
176
11.8k
    return -1;
177
16.8k
}
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
40
{
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
40
    Py_ssize_t max_suffix = 0;
204
40
    Py_ssize_t candidate = 1;
205
40
    Py_ssize_t k = 0;
206
    // The period of the right half.
207
40
    Py_ssize_t period = 1;
208
209
400
    while (candidate + k < len_needle) {
210
        // each loop increases candidate + k + max_suffix
211
360
        STRINGLIB_CHAR a = needle[candidate + k];
212
360
        STRINGLIB_CHAR b = needle[max_suffix + k];
213
        // check if the suffix at candidate is better than max_suffix
214
360
        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
260
            candidate += k + 1;
219
260
            k = 0;
220
            // We've ruled out any period smaller than what's
221
            // been scanned since max_suffix.
222
260
            period = candidate - max_suffix;
223
260
        }
224
100
        else if (a == b) {
225
20
            if (k + 1 != period) {
226
                // Keep scanning the equal strings
227
0
                k++;
228
0
            }
229
20
            else {
230
                // Matched a whole period.
231
                // Start matching the next period.
232
20
                candidate += period;
233
20
                k = 0;
234
20
            }
235
20
        }
236
80
        else {
237
            // Did better than max_suffix, so replace it.
238
80
            max_suffix = candidate;
239
80
            candidate++;
240
80
            k = 0;
241
80
            period = 1;
242
80
        }
243
360
    }
244
40
    *return_period = period;
245
40
    return max_suffix;
246
40
}
Unexecuted instantiation: bytesobject.c:stringlib__lex_search
Unexecuted instantiation: unicodeobject.c:asciilib__lex_search
unicodeobject.c:ucs1lib__lex_search
Line
Count
Source
199
40
{
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
40
    Py_ssize_t max_suffix = 0;
204
40
    Py_ssize_t candidate = 1;
205
40
    Py_ssize_t k = 0;
206
    // The period of the right half.
207
40
    Py_ssize_t period = 1;
208
209
400
    while (candidate + k < len_needle) {
210
        // each loop increases candidate + k + max_suffix
211
360
        STRINGLIB_CHAR a = needle[candidate + k];
212
360
        STRINGLIB_CHAR b = needle[max_suffix + k];
213
        // check if the suffix at candidate is better than max_suffix
214
360
        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
260
            candidate += k + 1;
219
260
            k = 0;
220
            // We've ruled out any period smaller than what's
221
            // been scanned since max_suffix.
222
260
            period = candidate - max_suffix;
223
260
        }
224
100
        else if (a == b) {
225
20
            if (k + 1 != period) {
226
                // Keep scanning the equal strings
227
0
                k++;
228
0
            }
229
20
            else {
230
                // Matched a whole period.
231
                // Start matching the next period.
232
20
                candidate += period;
233
20
                k = 0;
234
20
            }
235
20
        }
236
80
        else {
237
            // Did better than max_suffix, so replace it.
238
80
            max_suffix = candidate;
239
80
            candidate++;
240
80
            k = 0;
241
80
            period = 1;
242
80
        }
243
360
    }
244
40
    *return_period = period;
245
40
    return max_suffix;
246
40
}
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
20
{
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
20
    Py_ssize_t cut1, period1, cut2, period2, cut, period;
286
20
    cut1 = STRINGLIB(_lex_search)(needle, len_needle, &period1, 0);
287
20
    cut2 = STRINGLIB(_lex_search)(needle, len_needle, &period2, 1);
288
289
    // Take the later cut.
290
20
    if (cut1 > cut2) {
291
20
        period = period1;
292
20
        cut = cut1;
293
20
    }
294
0
    else {
295
0
        period = period2;
296
0
        cut = cut2;
297
0
    }
298
299
20
    LOG("split: "); LOG_STRING(needle, cut);
300
20
    LOG(" + "); LOG_STRING(needle + cut, len_needle - cut);
301
20
    LOG("\n");
302
303
20
    *return_period = period;
304
20
    return cut;
305
20
}
Unexecuted instantiation: bytesobject.c:stringlib__factorize
Unexecuted instantiation: unicodeobject.c:asciilib__factorize
unicodeobject.c:ucs1lib__factorize
Line
Count
Source
252
20
{
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
20
    Py_ssize_t cut1, period1, cut2, period2, cut, period;
286
20
    cut1 = STRINGLIB(_lex_search)(needle, len_needle, &period1, 0);
287
20
    cut2 = STRINGLIB(_lex_search)(needle, len_needle, &period2, 1);
288
289
    // Take the later cut.
290
20
    if (cut1 > cut2) {
291
20
        period = period1;
292
20
        cut = cut1;
293
20
    }
294
0
    else {
295
0
        period = period2;
296
0
        cut = cut2;
297
0
    }
298
299
20
    LOG("split: "); LOG_STRING(needle, cut);
300
20
    LOG(" + "); LOG_STRING(needle + cut, len_needle - cut);
301
20
    LOG("\n");
302
303
20
    *return_period = period;
304
20
    return cut;
305
20
}
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
220
#define SHIFT_TYPE uint8_t
309
#define MAX_SHIFT UINT8_MAX
310
311
69.2k
#define TABLE_SIZE_BITS 6u
312
69.2k
#define TABLE_SIZE (1U << TABLE_SIZE_BITS)
313
67.9k
#define TABLE_MASK (TABLE_SIZE - 1U)
314
315
typedef struct STRINGLIB(_pre) {
316
    const STRINGLIB_CHAR *needle;
317
    Py_ssize_t len_needle;
318
    Py_ssize_t cut;
319
    Py_ssize_t period;
320
    Py_ssize_t gap;
321
    int is_periodic;
322
    SHIFT_TYPE table[TABLE_SIZE];
323
} STRINGLIB(prework);
324
325
326
static void
327
STRINGLIB(_preprocess)(const STRINGLIB_CHAR *needle, Py_ssize_t len_needle,
328
                       STRINGLIB(prework) *p)
329
20
{
330
20
    p->needle = needle;
331
20
    p->len_needle = len_needle;
332
20
    p->cut = STRINGLIB(_factorize)(needle, len_needle, &(p->period));
333
20
    assert(p->period + p->cut <= len_needle);
334
20
    p->is_periodic = (0 == memcmp(needle,
335
20
                                  needle + p->period,
336
20
                                  p->cut * STRINGLIB_SIZEOF_CHAR));
337
20
    if (p->is_periodic) {
338
0
        assert(p->cut <= len_needle/2);
339
0
        assert(p->cut < p->period);
340
0
    }
341
20
    else {
342
        // A lower bound on the period
343
20
        p->period = Py_MAX(p->cut, len_needle - p->cut) + 1;
344
20
    }
345
    // The gap between the last character and the previous
346
    // occurrence of an equivalent character (modulo TABLE_SIZE)
347
20
    p->gap = len_needle;
348
20
    STRINGLIB_CHAR last = needle[len_needle - 1] & TABLE_MASK;
349
140
    for (Py_ssize_t i = len_needle - 2; i >= 0; i--) {
350
140
        STRINGLIB_CHAR x = needle[i] & TABLE_MASK;
351
140
        if (x == last) {
352
20
            p->gap = len_needle - 1 - i;
353
20
            break;
354
20
        }
355
140
    }
356
    // Fill up a compressed Boyer-Moore "Bad Character" table
357
20
    Py_ssize_t not_found_shift = Py_MIN(len_needle, MAX_SHIFT);
358
1.30k
    for (Py_ssize_t i = 0; i < (Py_ssize_t)TABLE_SIZE; i++) {
359
1.28k
        p->table[i] = Py_SAFE_DOWNCAST(not_found_shift,
360
1.28k
                                       Py_ssize_t, SHIFT_TYPE);
361
1.28k
    }
362
220
    for (Py_ssize_t i = len_needle - not_found_shift; i < len_needle; i++) {
363
200
        SHIFT_TYPE shift = Py_SAFE_DOWNCAST(len_needle - 1 - i,
364
200
                                            Py_ssize_t, SHIFT_TYPE);
365
200
        p->table[needle[i] & TABLE_MASK] = shift;
366
200
    }
367
20
}
Unexecuted instantiation: bytesobject.c:stringlib__preprocess
Unexecuted instantiation: unicodeobject.c:asciilib__preprocess
unicodeobject.c:ucs1lib__preprocess
Line
Count
Source
329
20
{
330
20
    p->needle = needle;
331
20
    p->len_needle = len_needle;
332
20
    p->cut = STRINGLIB(_factorize)(needle, len_needle, &(p->period));
333
20
    assert(p->period + p->cut <= len_needle);
334
20
    p->is_periodic = (0 == memcmp(needle,
335
20
                                  needle + p->period,
336
20
                                  p->cut * STRINGLIB_SIZEOF_CHAR));
337
20
    if (p->is_periodic) {
338
0
        assert(p->cut <= len_needle/2);
339
0
        assert(p->cut < p->period);
340
0
    }
341
20
    else {
342
        // A lower bound on the period
343
20
        p->period = Py_MAX(p->cut, len_needle - p->cut) + 1;
344
20
    }
345
    // The gap between the last character and the previous
346
    // occurrence of an equivalent character (modulo TABLE_SIZE)
347
20
    p->gap = len_needle;
348
20
    STRINGLIB_CHAR last = needle[len_needle - 1] & TABLE_MASK;
349
140
    for (Py_ssize_t i = len_needle - 2; i >= 0; i--) {
350
140
        STRINGLIB_CHAR x = needle[i] & TABLE_MASK;
351
140
        if (x == last) {
352
20
            p->gap = len_needle - 1 - i;
353
20
            break;
354
20
        }
355
140
    }
356
    // Fill up a compressed Boyer-Moore "Bad Character" table
357
20
    Py_ssize_t not_found_shift = Py_MIN(len_needle, MAX_SHIFT);
358
1.30k
    for (Py_ssize_t i = 0; i < (Py_ssize_t)TABLE_SIZE; i++) {
359
1.28k
        p->table[i] = Py_SAFE_DOWNCAST(not_found_shift,
360
1.28k
                                       Py_ssize_t, SHIFT_TYPE);
361
1.28k
    }
362
220
    for (Py_ssize_t i = len_needle - not_found_shift; i < len_needle; i++) {
363
200
        SHIFT_TYPE shift = Py_SAFE_DOWNCAST(len_needle - 1 - i,
364
200
                                            Py_ssize_t, SHIFT_TYPE);
365
200
        p->table[needle[i] & TABLE_MASK] = shift;
366
200
    }
367
20
}
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
20
{
373
    // Crochemore and Perrin's (1991) Two-Way algorithm.
374
    // See http://www-igm.univ-mlv.fr/~lecroq/string/node26.html#SECTION00260
375
20
    const Py_ssize_t len_needle = p->len_needle;
376
20
    const Py_ssize_t cut = p->cut;
377
20
    Py_ssize_t period = p->period;
378
20
    const STRINGLIB_CHAR *const needle = p->needle;
379
20
    const STRINGLIB_CHAR *window_last = haystack + len_needle - 1;
380
20
    const STRINGLIB_CHAR *const haystack_end = haystack + len_haystack;
381
20
    SHIFT_TYPE *table = p->table;
382
20
    const STRINGLIB_CHAR *window;
383
20
    LOG("===== Two-way: \"%s\" in \"%s\". =====\n", needle, haystack);
384
385
20
    Py_ssize_t gap = p->gap;
386
20
    Py_ssize_t gap_jump_end = Py_MIN(len_needle, cut + gap);
387
20
    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
20
    else {
454
20
        period = Py_MAX(gap, period);
455
20
        LOG("Needle is not periodic.\n");
456
12.3k
      windowloop:
457
12.3k
        while (window_last < haystack_end) {
458
67.6k
            for (;;) {
459
67.6k
                LOG_LINEUP();
460
67.6k
                Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
461
67.6k
                window_last += shift;
462
67.6k
                if (shift == 0) {
463
12.3k
                    break;
464
12.3k
                }
465
55.2k
                if (window_last >= haystack_end) {
466
17
                    return -1;
467
17
                }
468
55.2k
                LOG("Horspool skip\n");
469
55.2k
            }
470
12.3k
            window = window_last - len_needle + 1;
471
12.3k
            assert((window[len_needle - 1] & TABLE_MASK) ==
472
12.3k
                   (needle[len_needle - 1] & TABLE_MASK));
473
12.3k
            Py_ssize_t i = cut;
474
12.5k
            for (; i < len_needle; i++) {
475
12.4k
                if (needle[i] != window[i]) {
476
12.2k
                    if (i < gap_jump_end) {
477
12.2k
                        LOG("Early right half mismatch: jump by gap.\n");
478
12.2k
                        assert(gap >= i - cut + 1);
479
12.2k
                        window_last += gap;
480
12.2k
                    }
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
12.2k
                    goto windowloop;
487
12.2k
                }
488
12.4k
            }
489
104
            for (Py_ssize_t i = 0; i < cut; i++) {
490
102
                if (needle[i] != window[i]) {
491
86
                    LOG("Left half does not match.\n");
492
86
                    window_last += period;
493
86
                    goto windowloop;
494
86
                }
495
102
            }
496
2
            LOG("Found a match!\n");
497
2
            return window - haystack;
498
88
        }
499
12.3k
    }
500
1
    LOG("Not found. Returning -1.\n");
501
1
    return -1;
502
20
}
Unexecuted instantiation: bytesobject.c:stringlib__two_way
Unexecuted instantiation: unicodeobject.c:asciilib__two_way
unicodeobject.c:ucs1lib__two_way
Line
Count
Source
372
20
{
373
    // Crochemore and Perrin's (1991) Two-Way algorithm.
374
    // See http://www-igm.univ-mlv.fr/~lecroq/string/node26.html#SECTION00260
375
20
    const Py_ssize_t len_needle = p->len_needle;
376
20
    const Py_ssize_t cut = p->cut;
377
20
    Py_ssize_t period = p->period;
378
20
    const STRINGLIB_CHAR *const needle = p->needle;
379
20
    const STRINGLIB_CHAR *window_last = haystack + len_needle - 1;
380
20
    const STRINGLIB_CHAR *const haystack_end = haystack + len_haystack;
381
20
    SHIFT_TYPE *table = p->table;
382
20
    const STRINGLIB_CHAR *window;
383
20
    LOG("===== Two-way: \"%s\" in \"%s\". =====\n", needle, haystack);
384
385
20
    Py_ssize_t gap = p->gap;
386
20
    Py_ssize_t gap_jump_end = Py_MIN(len_needle, cut + gap);
387
20
    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
20
    else {
454
20
        period = Py_MAX(gap, period);
455
20
        LOG("Needle is not periodic.\n");
456
12.3k
      windowloop:
457
12.3k
        while (window_last < haystack_end) {
458
67.6k
            for (;;) {
459
67.6k
                LOG_LINEUP();
460
67.6k
                Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
461
67.6k
                window_last += shift;
462
67.6k
                if (shift == 0) {
463
12.3k
                    break;
464
12.3k
                }
465
55.2k
                if (window_last >= haystack_end) {
466
17
                    return -1;
467
17
                }
468
55.2k
                LOG("Horspool skip\n");
469
55.2k
            }
470
12.3k
            window = window_last - len_needle + 1;
471
12.3k
            assert((window[len_needle - 1] & TABLE_MASK) ==
472
12.3k
                   (needle[len_needle - 1] & TABLE_MASK));
473
12.3k
            Py_ssize_t i = cut;
474
12.5k
            for (; i < len_needle; i++) {
475
12.4k
                if (needle[i] != window[i]) {
476
12.2k
                    if (i < gap_jump_end) {
477
12.2k
                        LOG("Early right half mismatch: jump by gap.\n");
478
12.2k
                        assert(gap >= i - cut + 1);
479
12.2k
                        window_last += gap;
480
12.2k
                    }
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
12.2k
                    goto windowloop;
487
12.2k
                }
488
12.4k
            }
489
104
            for (Py_ssize_t i = 0; i < cut; i++) {
490
102
                if (needle[i] != window[i]) {
491
86
                    LOG("Left half does not match.\n");
492
86
                    window_last += period;
493
86
                    goto windowloop;
494
86
                }
495
102
            }
496
2
            LOG("Found a match!\n");
497
2
            return window - haystack;
498
88
        }
499
12.3k
    }
500
1
    LOG("Not found. Returning -1.\n");
501
1
    return -1;
502
20
}
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
20
{
511
20
    LOG("###### Finding \"%s\" in \"%s\".\n", needle, haystack);
512
20
    STRINGLIB(prework) p;
513
20
    STRINGLIB(_preprocess)(needle, len_needle, &p);
514
20
    return STRINGLIB(_two_way)(haystack, len_haystack, &p);
515
20
}
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
20
{
511
20
    LOG("###### Finding \"%s\" in \"%s\".\n", needle, haystack);
512
20
    STRINGLIB(prework) p;
513
20
    STRINGLIB(_preprocess)(needle, len_needle, &p);
514
20
    return STRINGLIB(_two_way)(haystack, len_haystack, &p);
515
20
}
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
16.7M
{
561
16.7M
    const Py_ssize_t w = n - m;
562
16.7M
    Py_ssize_t mlast = m - 1, count = 0;
563
16.7M
    Py_ssize_t gap = mlast;
564
16.7M
    const STRINGLIB_CHAR last = p[mlast];
565
16.7M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
16.7M
    unsigned long mask = 0;
568
33.6M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
16.8M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
16.8M
        if (p[i] == last) {
571
355k
            gap = mlast - i - 1;
572
355k
        }
573
16.8M
    }
574
16.7M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
6.15G
    for (Py_ssize_t i = 0; i <= w; i++) {
577
6.14G
        if (ss[i] == last) {
578
            /* candidate match */
579
67.2M
            Py_ssize_t j;
580
91.0M
            for (j = 0; j < mlast; j++) {
581
67.3M
                if (s[i+j] != p[j]) {
582
43.5M
                    break;
583
43.5M
                }
584
67.3M
            }
585
67.2M
            if (j == mlast) {
586
                /* got a match! */
587
23.7M
                if (mode != FAST_COUNT) {
588
11.9M
                    return i;
589
11.9M
                }
590
11.8M
                count++;
591
11.8M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
11.8M
                i = i + mlast;
595
11.8M
                continue;
596
11.8M
            }
597
            /* miss: check if next character is part of pattern */
598
43.5M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
8.24M
                i = i + m;
600
8.24M
            }
601
35.2M
            else {
602
35.2M
                i = i + gap;
603
35.2M
            }
604
43.5M
        }
605
6.08G
        else {
606
            /* skip: check if next character is part of pattern */
607
6.08G
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
6.03G
                i = i + m;
609
6.03G
            }
610
6.08G
        }
611
6.14G
    }
612
4.85M
    return mode == FAST_COUNT ? count : -1;
613
16.7M
}
Unexecuted instantiation: bytesobject.c:stringlib_default_find
unicodeobject.c:asciilib_default_find
Line
Count
Source
560
2.21M
{
561
2.21M
    const Py_ssize_t w = n - m;
562
2.21M
    Py_ssize_t mlast = m - 1, count = 0;
563
2.21M
    Py_ssize_t gap = mlast;
564
2.21M
    const STRINGLIB_CHAR last = p[mlast];
565
2.21M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
2.21M
    unsigned long mask = 0;
568
4.42M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
2.21M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
2.21M
        if (p[i] == last) {
571
17.3k
            gap = mlast - i - 1;
572
17.3k
        }
573
2.21M
    }
574
2.21M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
207M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
207M
        if (ss[i] == last) {
578
            /* candidate match */
579
3.84M
            Py_ssize_t j;
580
6.02M
            for (j = 0; j < mlast; j++) {
581
3.84M
                if (s[i+j] != p[j]) {
582
1.65M
                    break;
583
1.65M
                }
584
3.84M
            }
585
3.84M
            if (j == mlast) {
586
                /* got a match! */
587
2.18M
                if (mode != FAST_COUNT) {
588
2.18M
                    return i;
589
2.18M
                }
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
27.1k
                i = i + m;
600
27.1k
            }
601
1.63M
            else {
602
1.63M
                i = i + gap;
603
1.63M
            }
604
1.65M
        }
605
203M
        else {
606
            /* skip: check if next character is part of pattern */
607
203M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
197M
                i = i + m;
609
197M
            }
610
203M
        }
611
207M
    }
612
28.9k
    return mode == FAST_COUNT ? count : -1;
613
2.21M
}
unicodeobject.c:ucs1lib_default_find
Line
Count
Source
560
6.38M
{
561
6.38M
    const Py_ssize_t w = n - m;
562
6.38M
    Py_ssize_t mlast = m - 1, count = 0;
563
6.38M
    Py_ssize_t gap = mlast;
564
6.38M
    const STRINGLIB_CHAR last = p[mlast];
565
6.38M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
6.38M
    unsigned long mask = 0;
568
12.8M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
6.43M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
6.43M
        if (p[i] == last) {
571
275k
            gap = mlast - i - 1;
572
275k
        }
573
6.43M
    }
574
6.38M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
3.23G
    for (Py_ssize_t i = 0; i <= w; i++) {
577
3.23G
        if (ss[i] == last) {
578
            /* candidate match */
579
27.9M
            Py_ssize_t j;
580
33.5M
            for (j = 0; j < mlast; j++) {
581
27.9M
                if (s[i+j] != p[j]) {
582
22.3M
                    break;
583
22.3M
                }
584
27.9M
            }
585
27.9M
            if (j == mlast) {
586
                /* got a match! */
587
5.58M
                if (mode != FAST_COUNT) {
588
1.71M
                    return i;
589
1.71M
                }
590
3.87M
                count++;
591
3.87M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
3.87M
                i = i + mlast;
595
3.87M
                continue;
596
3.87M
            }
597
            /* miss: check if next character is part of pattern */
598
22.3M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
5.10M
                i = i + m;
600
5.10M
            }
601
17.2M
            else {
602
17.2M
                i = i + gap;
603
17.2M
            }
604
22.3M
        }
605
3.20G
        else {
606
            /* skip: check if next character is part of pattern */
607
3.20G
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
3.18G
                i = i + m;
609
3.18G
            }
610
3.20G
        }
611
3.23G
    }
612
4.67M
    return mode == FAST_COUNT ? count : -1;
613
6.38M
}
unicodeobject.c:ucs2lib_default_find
Line
Count
Source
560
3.40M
{
561
3.40M
    const Py_ssize_t w = n - m;
562
3.40M
    Py_ssize_t mlast = m - 1, count = 0;
563
3.40M
    Py_ssize_t gap = mlast;
564
3.40M
    const STRINGLIB_CHAR last = p[mlast];
565
3.40M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
3.40M
    unsigned long mask = 0;
568
6.83M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
3.42M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
3.42M
        if (p[i] == last) {
571
36.0k
            gap = mlast - i - 1;
572
36.0k
        }
573
3.42M
    }
574
3.40M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
1.20G
    for (Py_ssize_t i = 0; i <= w; i++) {
577
1.20G
        if (ss[i] == last) {
578
            /* candidate match */
579
12.0M
            Py_ssize_t j;
580
18.5M
            for (j = 0; j < mlast; j++) {
581
12.0M
                if (s[i+j] != p[j]) {
582
5.43M
                    break;
583
5.43M
                }
584
12.0M
            }
585
12.0M
            if (j == mlast) {
586
                /* got a match! */
587
6.57M
                if (mode != FAST_COUNT) {
588
3.32M
                    return i;
589
3.32M
                }
590
3.24M
                count++;
591
3.24M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
3.24M
                i = i + mlast;
595
3.24M
                continue;
596
3.24M
            }
597
            /* miss: check if next character is part of pattern */
598
5.43M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
1.29M
                i = i + m;
600
1.29M
            }
601
4.13M
            else {
602
4.13M
                i = i + gap;
603
4.13M
            }
604
5.43M
        }
605
1.19G
        else {
606
            /* skip: check if next character is part of pattern */
607
1.19G
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
1.18G
                i = i + m;
609
1.18G
            }
610
1.19G
        }
611
1.20G
    }
612
87.1k
    return mode == FAST_COUNT ? count : -1;
613
3.40M
}
unicodeobject.c:ucs4lib_default_find
Line
Count
Source
560
4.78M
{
561
4.78M
    const Py_ssize_t w = n - m;
562
4.78M
    Py_ssize_t mlast = m - 1, count = 0;
563
4.78M
    Py_ssize_t gap = mlast;
564
4.78M
    const STRINGLIB_CHAR last = p[mlast];
565
4.78M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
4.78M
    unsigned long mask = 0;
568
9.58M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
4.79M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
4.79M
        if (p[i] == last) {
571
23.9k
            gap = mlast - i - 1;
572
23.9k
        }
573
4.79M
    }
574
4.78M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
1.50G
    for (Py_ssize_t i = 0; i <= w; i++) {
577
1.50G
        if (ss[i] == last) {
578
            /* candidate match */
579
23.5M
            Py_ssize_t j;
580
32.9M
            for (j = 0; j < mlast; j++) {
581
23.5M
                if (s[i+j] != p[j]) {
582
14.0M
                    break;
583
14.0M
                }
584
23.5M
            }
585
23.5M
            if (j == mlast) {
586
                /* got a match! */
587
9.42M
                if (mode != FAST_COUNT) {
588
4.72M
                    return i;
589
4.72M
                }
590
4.69M
                count++;
591
4.69M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
4.69M
                i = i + mlast;
595
4.69M
                continue;
596
4.69M
            }
597
            /* miss: check if next character is part of pattern */
598
14.0M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
1.81M
                i = i + m;
600
1.81M
            }
601
12.2M
            else {
602
12.2M
                i = i + gap;
603
12.2M
            }
604
14.0M
        }
605
1.48G
        else {
606
            /* skip: check if next character is part of pattern */
607
1.48G
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
1.46G
                i = i + m;
609
1.46G
            }
610
1.48G
        }
611
1.50G
    }
612
64.4k
    return mode == FAST_COUNT ? count : -1;
613
4.78M
}
bytes_methods.c:stringlib_default_find
Line
Count
Source
560
3.14k
{
561
3.14k
    const Py_ssize_t w = n - m;
562
3.14k
    Py_ssize_t mlast = m - 1, count = 0;
563
3.14k
    Py_ssize_t gap = mlast;
564
3.14k
    const STRINGLIB_CHAR last = p[mlast];
565
3.14k
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
3.14k
    unsigned long mask = 0;
568
12.5k
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
9.43k
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
9.43k
        if (p[i] == last) {
571
3.14k
            gap = mlast - i - 1;
572
3.14k
        }
573
9.43k
    }
574
3.14k
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
938k
    for (Py_ssize_t i = 0; i <= w; i++) {
577
938k
        if (ss[i] == last) {
578
            /* candidate match */
579
8.59k
            Py_ssize_t j;
580
17.5k
            for (j = 0; j < mlast; j++) {
581
14.6k
                if (s[i+j] != p[j]) {
582
5.70k
                    break;
583
5.70k
                }
584
14.6k
            }
585
8.59k
            if (j == mlast) {
586
                /* got a match! */
587
2.89k
                if (mode != FAST_COUNT) {
588
2.89k
                    return i;
589
2.89k
                }
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
5.70k
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
634
                i = i + m;
600
634
            }
601
5.07k
            else {
602
5.07k
                i = i + gap;
603
5.07k
            }
604
5.70k
        }
605
929k
        else {
606
            /* skip: check if next character is part of pattern */
607
929k
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
218k
                i = i + m;
609
218k
            }
610
929k
        }
611
938k
    }
612
251
    return mode == FAST_COUNT ? count : -1;
613
3.14k
}
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
4
{
694
    /* create compressed boyer-moore delta 1 table */
695
4
    unsigned long mask = 0;
696
4
    Py_ssize_t i, j, mlast = m - 1, skip = m - 1, w = n - m;
697
698
    /* process pattern[0] outside the loop */
699
4
    STRINGLIB_BLOOM_ADD(mask, p[0]);
700
    /* process pattern[:0:-1] */
701
16
    for (i = mlast; i > 0; i--) {
702
12
        STRINGLIB_BLOOM_ADD(mask, p[i]);
703
12
        if (p[i] == p[0]) {
704
0
            skip = i - 1;
705
0
        }
706
12
    }
707
708
356
    for (i = w; i >= 0; i--) {
709
352
        if (s[i] == p[0]) {
710
            /* candidate match */
711
8
            for (j = mlast; j > 0; j--) {
712
8
                if (s[i+j] != p[j]) {
713
8
                    break;
714
8
                }
715
8
            }
716
8
            if (j == 0) {
717
                /* got a match! */
718
0
                return i;
719
0
            }
720
            /* miss: check if previous character is part of pattern */
721
8
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
722
8
                i = i - m;
723
8
            }
724
0
            else {
725
0
                i = i - skip;
726
0
            }
727
8
        }
728
344
        else {
729
            /* skip: check if previous character is part of pattern */
730
344
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
731
336
                i = i - m;
732
336
            }
733
344
        }
734
352
    }
735
4
    return -1;
736
4
}
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
4
{
694
    /* create compressed boyer-moore delta 1 table */
695
4
    unsigned long mask = 0;
696
4
    Py_ssize_t i, j, mlast = m - 1, skip = m - 1, w = n - m;
697
698
    /* process pattern[0] outside the loop */
699
4
    STRINGLIB_BLOOM_ADD(mask, p[0]);
700
    /* process pattern[:0:-1] */
701
16
    for (i = mlast; i > 0; i--) {
702
12
        STRINGLIB_BLOOM_ADD(mask, p[i]);
703
12
        if (p[i] == p[0]) {
704
0
            skip = i - 1;
705
0
        }
706
12
    }
707
708
356
    for (i = w; i >= 0; i--) {
709
352
        if (s[i] == p[0]) {
710
            /* candidate match */
711
8
            for (j = mlast; j > 0; j--) {
712
8
                if (s[i+j] != p[j]) {
713
8
                    break;
714
8
                }
715
8
            }
716
8
            if (j == 0) {
717
                /* got a match! */
718
0
                return i;
719
0
            }
720
            /* miss: check if previous character is part of pattern */
721
8
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
722
8
                i = i - m;
723
8
            }
724
0
            else {
725
0
                i = i - skip;
726
0
            }
727
8
        }
728
344
        else {
729
            /* skip: check if previous character is part of pattern */
730
344
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
731
336
                i = i - m;
732
336
            }
733
344
        }
734
352
    }
735
4
    return -1;
736
4
}
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
67.6M
{
762
67.6M
    Py_ssize_t count = 0;
763
14.7G
    for (Py_ssize_t i = 0; i < n; i++) {
764
14.6G
        if (s[i] == p0) {
765
309M
            count++;
766
309M
        }
767
14.6G
    }
768
67.6M
    return count;
769
67.6M
}
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
58.2M
{
762
58.2M
    Py_ssize_t count = 0;
763
9.81G
    for (Py_ssize_t i = 0; i < n; i++) {
764
9.76G
        if (s[i] == p0) {
765
86.7M
            count++;
766
86.7M
        }
767
9.76G
    }
768
58.2M
    return count;
769
58.2M
}
unicodeobject.c:ucs2lib_count_char_no_maxcount
Line
Count
Source
761
8.66M
{
762
8.66M
    Py_ssize_t count = 0;
763
2.46G
    for (Py_ssize_t i = 0; i < n; i++) {
764
2.45G
        if (s[i] == p0) {
765
80.8M
            count++;
766
80.8M
        }
767
2.45G
    }
768
8.66M
    return count;
769
8.66M
}
unicodeobject.c:ucs4lib_count_char_no_maxcount
Line
Count
Source
761
775k
{
762
775k
    Py_ssize_t count = 0;
763
2.45G
    for (Py_ssize_t i = 0; i < n; i++) {
764
2.45G
        if (s[i] == p0) {
765
141M
            count++;
766
141M
        }
767
2.45G
    }
768
775k
    return count;
769
775k
}
Unexecuted instantiation: bytes_methods.c:stringlib_count_char_no_maxcount
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
288M
{
777
288M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
11.1k
        return -1;
779
11.1k
    }
780
781
    /* look for special cases */
782
288M
    if (m <= 1) {
783
271M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
271M
        if (mode == FAST_SEARCH)
788
204M
            return STRINGLIB(find_char)(s, n, p[0]);
789
67.6M
        else if (mode == FAST_RSEARCH)
790
9.59k
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
67.6M
        else {
792
67.6M
            if (maxcount == PY_SSIZE_T_MAX) {
793
67.6M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
67.6M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
67.6M
        }
797
271M
    }
798
799
16.7M
    if (mode != FAST_RSEARCH) {
800
16.7M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
16.7M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
16.7M
        }
803
20
        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
20
            if (mode == FAST_SEARCH) {
810
20
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
20
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
20
        }
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
16.7M
    }
825
4
    else {
826
        /* FAST_RSEARCH */
827
4
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
4
    }
829
16.7M
}
Unexecuted instantiation: bytesobject.c:fastsearch
unicodeobject.c:asciilib_fastsearch
Line
Count
Source
776
31.0M
{
777
31.0M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
31.0M
    if (m <= 1) {
783
28.8M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
28.8M
        if (mode == FAST_SEARCH)
788
28.8M
            return STRINGLIB(find_char)(s, n, p[0]);
789
9.59k
        else if (mode == FAST_RSEARCH)
790
9.59k
            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
28.8M
    }
798
799
2.21M
    if (mode != FAST_RSEARCH) {
800
2.21M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
2.21M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
2.21M
        }
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.21M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
2.21M
}
unicodeobject.c:ucs1lib_fastsearch
Line
Count
Source
776
71.0M
{
777
71.0M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
71.0M
    if (m <= 1) {
783
64.6M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
64.6M
        if (mode == FAST_SEARCH)
788
6.47M
            return STRINGLIB(find_char)(s, n, p[0]);
789
58.2M
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
58.2M
        else {
792
58.2M
            if (maxcount == PY_SSIZE_T_MAX) {
793
58.2M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
58.2M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
58.2M
        }
797
64.6M
    }
798
799
6.38M
    if (mode != FAST_RSEARCH) {
800
6.38M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
6.38M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
6.38M
        }
803
20
        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
20
            if (mode == FAST_SEARCH) {
810
20
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
20
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
20
        }
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
6.38M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
6.38M
}
unicodeobject.c:ucs2lib_fastsearch
Line
Count
Source
776
78.6M
{
777
78.6M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
11.1k
        return -1;
779
11.1k
    }
780
781
    /* look for special cases */
782
78.6M
    if (m <= 1) {
783
75.2M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
75.2M
        if (mode == FAST_SEARCH)
788
66.5M
            return STRINGLIB(find_char)(s, n, p[0]);
789
8.66M
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
8.66M
        else {
792
8.66M
            if (maxcount == PY_SSIZE_T_MAX) {
793
8.66M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
8.66M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
8.66M
        }
797
75.2M
    }
798
799
3.40M
    if (mode != FAST_RSEARCH) {
800
3.40M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
3.40M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
3.40M
        }
803
0
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
0
            if (mode == FAST_SEARCH) {
810
0
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
0
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
0
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
3.40M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
3.40M
}
unicodeobject.c:ucs4lib_fastsearch
Line
Count
Source
776
107M
{
777
107M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
107M
    if (m <= 1) {
783
103M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
103M
        if (mode == FAST_SEARCH)
788
102M
            return STRINGLIB(find_char)(s, n, p[0]);
789
775k
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
775k
        else {
792
775k
            if (maxcount == PY_SSIZE_T_MAX) {
793
775k
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
775k
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
775k
        }
797
103M
    }
798
799
4.78M
    if (mode != FAST_RSEARCH) {
800
4.78M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
4.78M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
4.78M
        }
803
0
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
0
            if (mode == FAST_SEARCH) {
810
0
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
0
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
0
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
4.78M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
4.78M
}
bytes_methods.c:fastsearch
Line
Count
Source
776
3.16k
{
777
3.16k
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
12
        return -1;
779
12
    }
780
781
    /* look for special cases */
782
3.14k
    if (m <= 1) {
783
0
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
0
        if (mode == FAST_SEARCH)
788
0
            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
0
    }
798
799
3.14k
    if (mode != FAST_RSEARCH) {
800
3.14k
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
3.14k
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
3.14k
        }
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.14k
    }
825
4
    else {
826
        /* FAST_RSEARCH */
827
4
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
4
    }
829
3.14k
}
Unexecuted instantiation: bytearrayobject.c:fastsearch
830