Coverage Report

Created: 2026-01-09 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/stringlib/fastsearch.h
Line
Count
Source
1
/* stringlib: fastsearch implementation */
2
3
#define STRINGLIB_FASTSEARCH_H
4
5
/* fast search/count implementation, based on a mix between boyer-
6
   moore and horspool, with a few more bells and whistles on the top.
7
   for some more background, see:
8
   https://web.archive.org/web/20201107074620/http://effbot.org/zone/stringlib.htm */
9
10
/* note: fastsearch may access s[n], which isn't a problem when using
11
   Python's ordinary string types, but may cause problems if you're
12
   using this code in other contexts.  also, the count mode returns -1
13
   if there cannot possibly be a match in the target string, and 0 if
14
   it has actually checked for matches, but didn't find any.  callers
15
   beware! */
16
17
/* If the strings are long enough, use Crochemore and Perrin's Two-Way
18
   algorithm, which has worst-case O(n) runtime and best-case O(n/k).
19
   Also compute a table of shifts to achieve O(n/k) in more cases,
20
   and often (data dependent) deduce larger shifts than pure C&P can
21
   deduce. See stringlib_find_two_way_notes.txt in this folder for a
22
   detailed explanation. */
23
24
646M
#define FAST_COUNT 0
25
407M
#define FAST_SEARCH 1
26
108M
#define FAST_RSEARCH 2
27
28
#if LONG_BIT >= 128
29
#define STRINGLIB_BLOOM_WIDTH 128
30
#elif LONG_BIT >= 64
31
3.72G
#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
208M
    ((mask |= (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
40
#define STRINGLIB_BLOOM(mask, ch)     \
41
3.51G
    ((mask &  (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
42
43
#ifdef STRINGLIB_FAST_MEMCHR
44
326M
#  define MEMCHR_CUT_OFF 15
45
#else
46
75.4M
#  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
401M
{
52
401M
    const STRINGLIB_CHAR *p, *e;
53
54
401M
    p = s;
55
401M
    e = s + n;
56
401M
    if (n > MEMCHR_CUT_OFF) {
57
#ifdef STRINGLIB_FAST_MEMCHR
58
101M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
101M
        if (p != NULL)
60
99.1M
            return (p - s);
61
2.56M
        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.1M
        if (needle != 0) {
71
63.5M
            do {
72
63.5M
                void *candidate = memchr(p, needle,
73
63.5M
                                         (e - p) * sizeof(STRINGLIB_CHAR));
74
63.5M
                if (candidate == NULL)
75
616k
                    return -1;
76
62.9M
                s1 = p;
77
62.9M
                p = (const STRINGLIB_CHAR *)
78
62.9M
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
62.9M
                if (*p == ch)
80
62.8M
                    return (p - s);
81
                /* False positive */
82
105k
                p++;
83
105k
                if (p - s1 > MEMCHR_CUT_OFF)
84
48.1k
                    continue;
85
56.9k
                if (e - p <= MEMCHR_CUT_OFF)
86
3.98k
                    break;
87
53.0k
                e1 = p + MEMCHR_CUT_OFF;
88
1.56M
                while (p != e1) {
89
1.53M
                    if (*p == ch)
90
23.1k
                        return (p - s);
91
1.51M
                    p++;
92
1.51M
                }
93
53.0k
            }
94
63.5M
            while (e - p > MEMCHR_CUT_OFF);
95
63.5M
        }
96
#endif
97
165M
    }
98
1.33G
    while (p < e) {
99
1.13G
        if (*p == ch)
100
34.0M
            return (p - s);
101
1.10G
        p++;
102
1.10G
    }
103
202M
    return -1;
104
236M
}
bytesobject.c:stringlib_find_char
Line
Count
Source
51
515k
{
52
515k
    const STRINGLIB_CHAR *p, *e;
53
54
515k
    p = s;
55
515k
    e = s + n;
56
515k
    if (n > MEMCHR_CUT_OFF) {
57
515k
#ifdef STRINGLIB_FAST_MEMCHR
58
515k
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
515k
        if (p != NULL)
60
508k
            return (p - s);
61
7.52k
        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
515k
    }
98
30
    while (p < e) {
99
30
        if (*p == ch)
100
5
            return (p - s);
101
25
        p++;
102
25
    }
103
0
    return -1;
104
5
}
unicodeobject.c:ucs1lib_find_char
Line
Count
Source
51
228M
{
52
228M
    const STRINGLIB_CHAR *p, *e;
53
54
228M
    p = s;
55
228M
    e = s + n;
56
228M
    if (n > MEMCHR_CUT_OFF) {
57
20.7M
#ifdef STRINGLIB_FAST_MEMCHR
58
20.7M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
20.7M
        if (p != NULL)
60
19.1M
            return (p - s);
61
1.59M
        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.7M
    }
98
1.07G
    while (p < e) {
99
878M
        if (*p == ch)
100
13.6M
            return (p - s);
101
865M
        p++;
102
865M
    }
103
194M
    return -1;
104
208M
}
unicodeobject.c:ucs2lib_find_char
Line
Count
Source
51
75.1M
{
52
75.1M
    const STRINGLIB_CHAR *p, *e;
53
54
75.1M
    p = s;
55
75.1M
    e = s + n;
56
75.1M
    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.1M
        const STRINGLIB_CHAR *s1, *e1;
66
64.1M
        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.1M
        if (needle != 0) {
71
63.5M
            do {
72
63.5M
                void *candidate = memchr(p, needle,
73
63.5M
                                         (e - p) * sizeof(STRINGLIB_CHAR));
74
63.5M
                if (candidate == NULL)
75
616k
                    return -1;
76
62.9M
                s1 = p;
77
62.9M
                p = (const STRINGLIB_CHAR *)
78
62.9M
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
79
62.9M
                if (*p == ch)
80
62.8M
                    return (p - s);
81
                /* False positive */
82
105k
                p++;
83
105k
                if (p - s1 > MEMCHR_CUT_OFF)
84
48.1k
                    continue;
85
56.9k
                if (e - p <= MEMCHR_CUT_OFF)
86
3.98k
                    break;
87
53.0k
                e1 = p + MEMCHR_CUT_OFF;
88
1.56M
                while (p != e1) {
89
1.53M
                    if (*p == ch)
90
23.1k
                        return (p - s);
91
1.51M
                    p++;
92
1.51M
                }
93
53.0k
            }
94
63.5M
            while (e - p > MEMCHR_CUT_OFF);
95
63.5M
        }
96
64.1M
#endif
97
64.1M
    }
98
158M
    while (p < e) {
99
155M
        if (*p == ch)
100
8.30M
            return (p - s);
101
147M
        p++;
102
147M
    }
103
3.39M
    return -1;
104
11.7M
}
unicodeobject.c:ucs4lib_find_char
Line
Count
Source
51
53.5M
{
52
53.5M
    const STRINGLIB_CHAR *p, *e;
53
54
53.5M
    p = s;
55
53.5M
    e = s + n;
56
53.5M
    if (n > MEMCHR_CUT_OFF) {
57
53.4M
#ifdef STRINGLIB_FAST_MEMCHR
58
53.4M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
53.4M
        if (p != NULL)
60
53.4M
            return (p - s);
61
31.9k
        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
53.4M
    }
98
327k
    while (p < e) {
99
290k
        if (*p == ch)
100
47.6k
            return (p - s);
101
243k
        p++;
102
243k
    }
103
36.4k
    return -1;
104
84.1k
}
unicodeobject.c:asciilib_find_char
Line
Count
Source
51
25.6M
{
52
25.6M
    const STRINGLIB_CHAR *p, *e;
53
54
25.6M
    p = s;
55
25.6M
    e = s + n;
56
25.6M
    if (n > MEMCHR_CUT_OFF) {
57
21.4M
#ifdef STRINGLIB_FAST_MEMCHR
58
21.4M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
21.4M
        if (p != NULL)
60
20.6M
            return (p - s);
61
788k
        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
21.4M
    }
98
11.5M
    while (p < e) {
99
11.4M
        if (*p == ch)
100
4.16M
            return (p - s);
101
7.30M
        p++;
102
7.30M
    }
103
60.1k
    return -1;
104
4.22M
}
bytes_methods.c:stringlib_find_char
Line
Count
Source
51
17.7M
{
52
17.7M
    const STRINGLIB_CHAR *p, *e;
53
54
17.7M
    p = s;
55
17.7M
    e = s + n;
56
17.7M
    if (n > MEMCHR_CUT_OFF) {
57
5.56M
#ifdef STRINGLIB_FAST_MEMCHR
58
5.56M
        p = STRINGLIB_FAST_MEMCHR(s, ch, n);
59
5.56M
        if (p != NULL)
60
5.42M
            return (p - s);
61
134k
        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
5.56M
    }
98
95.8M
    while (p < e) {
99
91.4M
        if (*p == ch)
100
7.79M
            return (p - s);
101
83.6M
        p++;
102
83.6M
    }
103
4.39M
    return -1;
104
12.1M
}
Unexecuted instantiation: bytearrayobject.c:stringlib_find_char
105
106
#undef MEMCHR_CUT_OFF
107
108
#if STRINGLIB_SIZEOF_CHAR == 1
109
151k
#  define MEMRCHR_CUT_OFF 15
110
#else
111
380k
#  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
499k
{
118
499k
    const STRINGLIB_CHAR *p;
119
499k
#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
499k
    if (n > MEMRCHR_CUT_OFF) {
126
#if STRINGLIB_SIZEOF_CHAR == 1
127
        p = memrchr(s, ch, n);
128
93.3k
        if (p != NULL)
129
89.9k
            return (p - s);
130
3.37k
        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
302k
        if (needle != 0) {
141
310k
            do {
142
310k
                void *candidate = memrchr(s, needle,
143
310k
                                          n * sizeof(STRINGLIB_CHAR));
144
310k
                if (candidate == NULL)
145
1.35k
                    return -1;
146
309k
                n1 = n;
147
309k
                p = (const STRINGLIB_CHAR *)
148
309k
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
309k
                n = p - s;
150
309k
                if (*p == ch)
151
297k
                    return n;
152
                /* False positive */
153
11.5k
                if (n1 - n > MEMRCHR_CUT_OFF)
154
5.36k
                    continue;
155
6.21k
                if (n <= MEMRCHR_CUT_OFF)
156
891
                    break;
157
5.32k
                s1 = p - MEMRCHR_CUT_OFF;
158
197k
                while (p > s1) {
159
193k
                    p--;
160
193k
                    if (*p == ch)
161
848
                        return (p - s);
162
193k
                }
163
4.48k
                n = p - s;
164
4.48k
            }
165
302k
            while (n > MEMRCHR_CUT_OFF);
166
302k
        }
167
#endif
168
395k
    }
169
105k
#endif  /* HAVE_MEMRCHR */
170
105k
    p = s + n;
171
640k
    while (p > s) {
172
596k
        p--;
173
596k
        if (*p == ch)
174
61.0k
            return (p - s);
175
596k
    }
176
44.7k
    return -1;
177
105k
}
Unexecuted instantiation: bytesobject.c:stringlib_rfind_char
unicodeobject.c:ucs1lib_rfind_char
Line
Count
Source
117
83.9k
{
118
83.9k
    const STRINGLIB_CHAR *p;
119
83.9k
#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
83.9k
    if (n > MEMRCHR_CUT_OFF) {
126
80.3k
#if STRINGLIB_SIZEOF_CHAR == 1
127
80.3k
        p = memrchr(s, ch, n);
128
80.3k
        if (p != NULL)
129
79.1k
            return (p - s);
130
1.11k
        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
80.3k
    }
169
3.59k
#endif  /* HAVE_MEMRCHR */
170
3.59k
    p = s + n;
171
11.6k
    while (p > s) {
172
10.5k
        p--;
173
10.5k
        if (*p == ch)
174
2.56k
            return (p - s);
175
10.5k
    }
176
1.02k
    return -1;
177
3.59k
}
unicodeobject.c:ucs2lib_rfind_char
Line
Count
Source
117
241k
{
118
241k
    const STRINGLIB_CHAR *p;
119
241k
#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
241k
    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
229k
        const STRINGLIB_CHAR *s1;
135
229k
        Py_ssize_t n1;
136
229k
        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
229k
        if (needle != 0) {
141
232k
            do {
142
232k
                void *candidate = memrchr(s, needle,
143
232k
                                          n * sizeof(STRINGLIB_CHAR));
144
232k
                if (candidate == NULL)
145
700
                    return -1;
146
232k
                n1 = n;
147
232k
                p = (const STRINGLIB_CHAR *)
148
232k
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
232k
                n = p - s;
150
232k
                if (*p == ch)
151
227k
                    return n;
152
                /* False positive */
153
4.60k
                if (n1 - n > MEMRCHR_CUT_OFF)
154
1.57k
                    continue;
155
3.03k
                if (n <= MEMRCHR_CUT_OFF)
156
418
                    break;
157
2.61k
                s1 = p - MEMRCHR_CUT_OFF;
158
96.6k
                while (p > s1) {
159
94.4k
                    p--;
160
94.4k
                    if (*p == ch)
161
421
                        return (p - s);
162
94.4k
                }
163
2.19k
                n = p - s;
164
2.19k
            }
165
229k
            while (n > MEMRCHR_CUT_OFF);
166
229k
        }
167
229k
#endif
168
229k
    }
169
12.5k
#endif  /* HAVE_MEMRCHR */
170
12.5k
    p = s + n;
171
64.6k
    while (p > s) {
172
62.4k
        p--;
173
62.4k
        if (*p == ch)
174
10.3k
            return (p - s);
175
62.4k
    }
176
2.18k
    return -1;
177
12.5k
}
unicodeobject.c:ucs4lib_rfind_char
Line
Count
Source
117
106k
{
118
106k
    const STRINGLIB_CHAR *p;
119
106k
#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
106k
    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
72.5k
        const STRINGLIB_CHAR *s1;
135
72.5k
        Py_ssize_t n1;
136
72.5k
        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
72.5k
        if (needle != 0) {
141
78.0k
            do {
142
78.0k
                void *candidate = memrchr(s, needle,
143
78.0k
                                          n * sizeof(STRINGLIB_CHAR));
144
78.0k
                if (candidate == NULL)
145
653
                    return -1;
146
77.4k
                n1 = n;
147
77.4k
                p = (const STRINGLIB_CHAR *)
148
77.4k
                        _Py_ALIGN_DOWN(candidate, sizeof(STRINGLIB_CHAR));
149
77.4k
                n = p - s;
150
77.4k
                if (*p == ch)
151
70.4k
                    return n;
152
                /* False positive */
153
6.97k
                if (n1 - n > MEMRCHR_CUT_OFF)
154
3.78k
                    continue;
155
3.18k
                if (n <= MEMRCHR_CUT_OFF)
156
473
                    break;
157
2.71k
                s1 = p - MEMRCHR_CUT_OFF;
158
100k
                while (p > s1) {
159
98.6k
                    p--;
160
98.6k
                    if (*p == ch)
161
427
                        return (p - s);
162
98.6k
                }
163
2.28k
                n = p - s;
164
2.28k
            }
165
72.5k
            while (n > MEMRCHR_CUT_OFF);
166
72.5k
        }
167
72.5k
#endif
168
72.5k
    }
169
34.7k
#endif  /* HAVE_MEMRCHR */
170
34.7k
    p = s + n;
171
272k
    while (p > s) {
172
270k
        p--;
173
270k
        if (*p == ch)
174
33.0k
            return (p - s);
175
270k
    }
176
1.73k
    return -1;
177
34.7k
}
unicodeobject.c:asciilib_rfind_char
Line
Count
Source
117
48.0k
{
118
48.0k
    const STRINGLIB_CHAR *p;
119
48.0k
#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
48.0k
    if (n > MEMRCHR_CUT_OFF) {
126
10.0k
#if STRINGLIB_SIZEOF_CHAR == 1
127
10.0k
        p = memrchr(s, ch, n);
128
10.0k
        if (p != NULL)
129
9.86k
            return (p - s);
130
146
        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
10.0k
    }
169
38.0k
#endif  /* HAVE_MEMRCHR */
170
38.0k
    p = s + n;
171
207k
    while (p > s) {
172
179k
        p--;
173
179k
        if (*p == ch)
174
9.95k
            return (p - s);
175
179k
    }
176
28.0k
    return -1;
177
38.0k
}
bytes_methods.c:stringlib_rfind_char
Line
Count
Source
117
19.8k
{
118
19.8k
    const STRINGLIB_CHAR *p;
119
19.8k
#ifdef HAVE_MEMRCHR
120
    /* memrchr() is a GNU extension, available since glibc 2.1.91.  it
121
       doesn't seem as optimized as memchr(), but is still quite
122
       faster than our hand-written loop below. There is no wmemrchr
123
       for 4-byte chars. */
124
125
19.8k
    if (n > MEMRCHR_CUT_OFF) {
126
2.98k
#if STRINGLIB_SIZEOF_CHAR == 1
127
2.98k
        p = memrchr(s, ch, n);
128
2.98k
        if (p != NULL)
129
863
            return (p - s);
130
2.11k
        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
2.98k
    }
169
16.8k
#endif  /* HAVE_MEMRCHR */
170
16.8k
    p = s + n;
171
85.3k
    while (p > s) {
172
73.6k
        p--;
173
73.6k
        if (*p == ch)
174
5.17k
            return (p - s);
175
73.6k
    }
176
11.7k
    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
44
{
200
    /* Do a lexicographic search. Essentially this:
201
           >>> max(needle[i:] for i in range(len(needle)+1))
202
       Also find the period of the right half.   */
203
44
    Py_ssize_t max_suffix = 0;
204
44
    Py_ssize_t candidate = 1;
205
44
    Py_ssize_t k = 0;
206
    // The period of the right half.
207
44
    Py_ssize_t period = 1;
208
209
440
    while (candidate + k < len_needle) {
210
        // each loop increases candidate + k + max_suffix
211
396
        STRINGLIB_CHAR a = needle[candidate + k];
212
396
        STRINGLIB_CHAR b = needle[max_suffix + k];
213
        // check if the suffix at candidate is better than max_suffix
214
396
        if (invert_alphabet ? (b < a) : (a < b)) {
215
            // Fell short of max_suffix.
216
            // The next k + 1 characters are non-increasing
217
            // from candidate, so they won't start a maximal suffix.
218
286
            candidate += k + 1;
219
286
            k = 0;
220
            // We've ruled out any period smaller than what's
221
            // been scanned since max_suffix.
222
286
            period = candidate - max_suffix;
223
286
        }
224
110
        else if (a == b) {
225
22
            if (k + 1 != period) {
226
                // Keep scanning the equal strings
227
0
                k++;
228
0
            }
229
22
            else {
230
                // Matched a whole period.
231
                // Start matching the next period.
232
22
                candidate += period;
233
22
                k = 0;
234
22
            }
235
22
        }
236
88
        else {
237
            // Did better than max_suffix, so replace it.
238
88
            max_suffix = candidate;
239
88
            candidate++;
240
88
            k = 0;
241
88
            period = 1;
242
88
        }
243
396
    }
244
44
    *return_period = period;
245
44
    return max_suffix;
246
44
}
Unexecuted instantiation: bytesobject.c:stringlib__lex_search
Unexecuted instantiation: unicodeobject.c:asciilib__lex_search
unicodeobject.c:ucs1lib__lex_search
Line
Count
Source
199
44
{
200
    /* Do a lexicographic search. Essentially this:
201
           >>> max(needle[i:] for i in range(len(needle)+1))
202
       Also find the period of the right half.   */
203
44
    Py_ssize_t max_suffix = 0;
204
44
    Py_ssize_t candidate = 1;
205
44
    Py_ssize_t k = 0;
206
    // The period of the right half.
207
44
    Py_ssize_t period = 1;
208
209
440
    while (candidate + k < len_needle) {
210
        // each loop increases candidate + k + max_suffix
211
396
        STRINGLIB_CHAR a = needle[candidate + k];
212
396
        STRINGLIB_CHAR b = needle[max_suffix + k];
213
        // check if the suffix at candidate is better than max_suffix
214
396
        if (invert_alphabet ? (b < a) : (a < b)) {
215
            // Fell short of max_suffix.
216
            // The next k + 1 characters are non-increasing
217
            // from candidate, so they won't start a maximal suffix.
218
286
            candidate += k + 1;
219
286
            k = 0;
220
            // We've ruled out any period smaller than what's
221
            // been scanned since max_suffix.
222
286
            period = candidate - max_suffix;
223
286
        }
224
110
        else if (a == b) {
225
22
            if (k + 1 != period) {
226
                // Keep scanning the equal strings
227
0
                k++;
228
0
            }
229
22
            else {
230
                // Matched a whole period.
231
                // Start matching the next period.
232
22
                candidate += period;
233
22
                k = 0;
234
22
            }
235
22
        }
236
88
        else {
237
            // Did better than max_suffix, so replace it.
238
88
            max_suffix = candidate;
239
88
            candidate++;
240
88
            k = 0;
241
88
            period = 1;
242
88
        }
243
396
    }
244
44
    *return_period = period;
245
44
    return max_suffix;
246
44
}
Unexecuted instantiation: unicodeobject.c:ucs2lib__lex_search
Unexecuted instantiation: unicodeobject.c:ucs4lib__lex_search
Unexecuted instantiation: bytes_methods.c:stringlib__lex_search
Unexecuted instantiation: bytearrayobject.c:stringlib__lex_search
247
248
Py_LOCAL_INLINE(Py_ssize_t)
249
STRINGLIB(_factorize)(const STRINGLIB_CHAR *needle,
250
                      Py_ssize_t len_needle,
251
                      Py_ssize_t *return_period)
252
22
{
253
    /* Do a "critical factorization", making it so that:
254
       >>> needle = (left := needle[:cut]) + (right := needle[cut:])
255
       where the "local period" of the cut is maximal.
256
257
       The local period of the cut is the minimal length of a string w
258
       such that (left endswith w or w endswith left)
259
       and (right startswith w or w startswith right).
260
261
       The Critical Factorization Theorem says that this maximal local
262
       period is the global period of the string.
263
264
       Crochemore and Perrin (1991) show that this cut can be computed
265
       as the later of two cuts: one that gives a lexicographically
266
       maximal right half, and one that gives the same with the
267
       with respect to a reversed alphabet-ordering.
268
269
       This is what we want to happen:
270
           >>> x = "GCAGAGAG"
271
           >>> cut, period = factorize(x)
272
           >>> x[:cut], (right := x[cut:])
273
           ('GC', 'AGAGAG')
274
           >>> period  # right half period
275
           2
276
           >>> right[period:] == right[:-period]
277
           True
278
279
       This is how the local period lines up in the above example:
280
                GC | AGAGAG
281
           AGAGAGC = AGAGAGC
282
       The length of this minimal repetition is 7, which is indeed the
283
       period of the original string. */
284
285
22
    Py_ssize_t cut1, period1, cut2, period2, cut, period;
286
22
    cut1 = STRINGLIB(_lex_search)(needle, len_needle, &period1, 0);
287
22
    cut2 = STRINGLIB(_lex_search)(needle, len_needle, &period2, 1);
288
289
    // Take the later cut.
290
22
    if (cut1 > cut2) {
291
22
        period = period1;
292
22
        cut = cut1;
293
22
    }
294
0
    else {
295
0
        period = period2;
296
0
        cut = cut2;
297
0
    }
298
299
22
    LOG("split: "); LOG_STRING(needle, cut);
300
22
    LOG(" + "); LOG_STRING(needle + cut, len_needle - cut);
301
22
    LOG("\n");
302
303
22
    *return_period = period;
304
22
    return cut;
305
22
}
Unexecuted instantiation: bytesobject.c:stringlib__factorize
Unexecuted instantiation: unicodeobject.c:asciilib__factorize
unicodeobject.c:ucs1lib__factorize
Line
Count
Source
252
22
{
253
    /* Do a "critical factorization", making it so that:
254
       >>> needle = (left := needle[:cut]) + (right := needle[cut:])
255
       where the "local period" of the cut is maximal.
256
257
       The local period of the cut is the minimal length of a string w
258
       such that (left endswith w or w endswith left)
259
       and (right startswith w or w startswith right).
260
261
       The Critical Factorization Theorem says that this maximal local
262
       period is the global period of the string.
263
264
       Crochemore and Perrin (1991) show that this cut can be computed
265
       as the later of two cuts: one that gives a lexicographically
266
       maximal right half, and one that gives the same with the
267
       with respect to a reversed alphabet-ordering.
268
269
       This is what we want to happen:
270
           >>> x = "GCAGAGAG"
271
           >>> cut, period = factorize(x)
272
           >>> x[:cut], (right := x[cut:])
273
           ('GC', 'AGAGAG')
274
           >>> period  # right half period
275
           2
276
           >>> right[period:] == right[:-period]
277
           True
278
279
       This is how the local period lines up in the above example:
280
                GC | AGAGAG
281
           AGAGAGC = AGAGAGC
282
       The length of this minimal repetition is 7, which is indeed the
283
       period of the original string. */
284
285
22
    Py_ssize_t cut1, period1, cut2, period2, cut, period;
286
22
    cut1 = STRINGLIB(_lex_search)(needle, len_needle, &period1, 0);
287
22
    cut2 = STRINGLIB(_lex_search)(needle, len_needle, &period2, 1);
288
289
    // Take the later cut.
290
22
    if (cut1 > cut2) {
291
22
        period = period1;
292
22
        cut = cut1;
293
22
    }
294
0
    else {
295
0
        period = period2;
296
0
        cut = cut2;
297
0
    }
298
299
22
    LOG("split: "); LOG_STRING(needle, cut);
300
22
    LOG(" + "); LOG_STRING(needle + cut, len_needle - cut);
301
22
    LOG("\n");
302
303
22
    *return_period = period;
304
22
    return cut;
305
22
}
Unexecuted instantiation: unicodeobject.c:ucs2lib__factorize
Unexecuted instantiation: unicodeobject.c:ucs4lib__factorize
Unexecuted instantiation: bytes_methods.c:stringlib__factorize
Unexecuted instantiation: bytearrayobject.c:stringlib__factorize
306
307
308
242
#define SHIFT_TYPE uint8_t
309
#define MAX_SHIFT UINT8_MAX
310
311
75.9k
#define TABLE_SIZE_BITS 6u
312
75.9k
#define TABLE_SIZE (1U << TABLE_SIZE_BITS)
313
74.4k
#define TABLE_MASK (TABLE_SIZE - 1U)
314
315
typedef struct STRINGLIB(_pre) {
316
    const STRINGLIB_CHAR *needle;
317
    Py_ssize_t len_needle;
318
    Py_ssize_t cut;
319
    Py_ssize_t period;
320
    Py_ssize_t gap;
321
    int is_periodic;
322
    SHIFT_TYPE table[TABLE_SIZE];
323
} STRINGLIB(prework);
324
325
326
static void
327
STRINGLIB(_preprocess)(const STRINGLIB_CHAR *needle, Py_ssize_t len_needle,
328
                       STRINGLIB(prework) *p)
329
22
{
330
22
    p->needle = needle;
331
22
    p->len_needle = len_needle;
332
22
    p->cut = STRINGLIB(_factorize)(needle, len_needle, &(p->period));
333
22
    assert(p->period + p->cut <= len_needle);
334
22
    p->is_periodic = (0 == memcmp(needle,
335
22
                                  needle + p->period,
336
22
                                  p->cut * STRINGLIB_SIZEOF_CHAR));
337
22
    if (p->is_periodic) {
338
0
        assert(p->cut <= len_needle/2);
339
0
        assert(p->cut < p->period);
340
0
    }
341
22
    else {
342
        // A lower bound on the period
343
22
        p->period = Py_MAX(p->cut, len_needle - p->cut) + 1;
344
22
    }
345
    // The gap between the last character and the previous
346
    // occurrence of an equivalent character (modulo TABLE_SIZE)
347
22
    p->gap = len_needle;
348
22
    STRINGLIB_CHAR last = needle[len_needle - 1] & TABLE_MASK;
349
154
    for (Py_ssize_t i = len_needle - 2; i >= 0; i--) {
350
154
        STRINGLIB_CHAR x = needle[i] & TABLE_MASK;
351
154
        if (x == last) {
352
22
            p->gap = len_needle - 1 - i;
353
22
            break;
354
22
        }
355
154
    }
356
    // Fill up a compressed Boyer-Moore "Bad Character" table
357
22
    Py_ssize_t not_found_shift = Py_MIN(len_needle, MAX_SHIFT);
358
1.43k
    for (Py_ssize_t i = 0; i < (Py_ssize_t)TABLE_SIZE; i++) {
359
1.40k
        p->table[i] = Py_SAFE_DOWNCAST(not_found_shift,
360
1.40k
                                       Py_ssize_t, SHIFT_TYPE);
361
1.40k
    }
362
242
    for (Py_ssize_t i = len_needle - not_found_shift; i < len_needle; i++) {
363
220
        SHIFT_TYPE shift = Py_SAFE_DOWNCAST(len_needle - 1 - i,
364
220
                                            Py_ssize_t, SHIFT_TYPE);
365
220
        p->table[needle[i] & TABLE_MASK] = shift;
366
220
    }
367
22
}
Unexecuted instantiation: bytesobject.c:stringlib__preprocess
Unexecuted instantiation: unicodeobject.c:asciilib__preprocess
unicodeobject.c:ucs1lib__preprocess
Line
Count
Source
329
22
{
330
22
    p->needle = needle;
331
22
    p->len_needle = len_needle;
332
22
    p->cut = STRINGLIB(_factorize)(needle, len_needle, &(p->period));
333
22
    assert(p->period + p->cut <= len_needle);
334
22
    p->is_periodic = (0 == memcmp(needle,
335
22
                                  needle + p->period,
336
22
                                  p->cut * STRINGLIB_SIZEOF_CHAR));
337
22
    if (p->is_periodic) {
338
0
        assert(p->cut <= len_needle/2);
339
0
        assert(p->cut < p->period);
340
0
    }
341
22
    else {
342
        // A lower bound on the period
343
22
        p->period = Py_MAX(p->cut, len_needle - p->cut) + 1;
344
22
    }
345
    // The gap between the last character and the previous
346
    // occurrence of an equivalent character (modulo TABLE_SIZE)
347
22
    p->gap = len_needle;
348
22
    STRINGLIB_CHAR last = needle[len_needle - 1] & TABLE_MASK;
349
154
    for (Py_ssize_t i = len_needle - 2; i >= 0; i--) {
350
154
        STRINGLIB_CHAR x = needle[i] & TABLE_MASK;
351
154
        if (x == last) {
352
22
            p->gap = len_needle - 1 - i;
353
22
            break;
354
22
        }
355
154
    }
356
    // Fill up a compressed Boyer-Moore "Bad Character" table
357
22
    Py_ssize_t not_found_shift = Py_MIN(len_needle, MAX_SHIFT);
358
1.43k
    for (Py_ssize_t i = 0; i < (Py_ssize_t)TABLE_SIZE; i++) {
359
1.40k
        p->table[i] = Py_SAFE_DOWNCAST(not_found_shift,
360
1.40k
                                       Py_ssize_t, SHIFT_TYPE);
361
1.40k
    }
362
242
    for (Py_ssize_t i = len_needle - not_found_shift; i < len_needle; i++) {
363
220
        SHIFT_TYPE shift = Py_SAFE_DOWNCAST(len_needle - 1 - i,
364
220
                                            Py_ssize_t, SHIFT_TYPE);
365
220
        p->table[needle[i] & TABLE_MASK] = shift;
366
220
    }
367
22
}
Unexecuted instantiation: unicodeobject.c:ucs2lib__preprocess
Unexecuted instantiation: unicodeobject.c:ucs4lib__preprocess
Unexecuted instantiation: bytes_methods.c:stringlib__preprocess
Unexecuted instantiation: bytearrayobject.c:stringlib__preprocess
368
369
static Py_ssize_t
370
STRINGLIB(_two_way)(const STRINGLIB_CHAR *haystack, Py_ssize_t len_haystack,
371
                    STRINGLIB(prework) *p)
372
22
{
373
    // Crochemore and Perrin's (1991) Two-Way algorithm.
374
    // See http://www-igm.univ-mlv.fr/~lecroq/string/node26.html#SECTION00260
375
22
    const Py_ssize_t len_needle = p->len_needle;
376
22
    const Py_ssize_t cut = p->cut;
377
22
    Py_ssize_t period = p->period;
378
22
    const STRINGLIB_CHAR *const needle = p->needle;
379
22
    const STRINGLIB_CHAR *window_last = haystack + len_needle - 1;
380
22
    const STRINGLIB_CHAR *const haystack_end = haystack + len_haystack;
381
22
    SHIFT_TYPE *table = p->table;
382
22
    const STRINGLIB_CHAR *window;
383
22
    LOG("===== Two-way: \"%s\" in \"%s\". =====\n", needle, haystack);
384
385
22
    Py_ssize_t gap = p->gap;
386
22
    Py_ssize_t gap_jump_end = Py_MIN(len_needle, cut + gap);
387
22
    if (p->is_periodic) {
388
0
        LOG("Needle is periodic.\n");
389
0
        Py_ssize_t memory = 0;
390
0
      periodicwindowloop:
391
0
        while (window_last < haystack_end) {
392
0
            assert(memory == 0);
393
0
            for (;;) {
394
0
                LOG_LINEUP();
395
0
                Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
396
0
                window_last += shift;
397
0
                if (shift == 0) {
398
0
                    break;
399
0
                }
400
0
                if (window_last >= haystack_end) {
401
0
                    return -1;
402
0
                }
403
0
                LOG("Horspool skip\n");
404
0
            }
405
0
          no_shift:
406
0
            window = window_last - len_needle + 1;
407
0
            assert((window[len_needle - 1] & TABLE_MASK) ==
408
0
                   (needle[len_needle - 1] & TABLE_MASK));
409
0
            Py_ssize_t i = Py_MAX(cut, memory);
410
0
            for (; i < len_needle; i++) {
411
0
                if (needle[i] != window[i]) {
412
0
                    if (i < gap_jump_end) {
413
0
                        LOG("Early right half mismatch: jump by gap.\n");
414
0
                        assert(gap >= i - cut + 1);
415
0
                        window_last += gap;
416
0
                    }
417
0
                    else {
418
0
                        LOG("Late right half mismatch: jump by n (>gap)\n");
419
0
                        assert(i - cut + 1 > gap);
420
0
                        window_last += i - cut + 1;
421
0
                    }
422
0
                    memory = 0;
423
0
                    goto periodicwindowloop;
424
0
                }
425
0
            }
426
0
            for (i = memory; i < cut; i++) {
427
0
                if (needle[i] != window[i]) {
428
0
                    LOG("Left half does not match.\n");
429
0
                    window_last += period;
430
0
                    memory = len_needle - period;
431
0
                    if (window_last >= haystack_end) {
432
0
                        return -1;
433
0
                    }
434
0
                    Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
435
0
                    if (shift) {
436
                        // A mismatch has been identified to the right
437
                        // of where i will next start, so we can jump
438
                        // at least as far as if the mismatch occurred
439
                        // on the first comparison.
440
0
                        Py_ssize_t mem_jump = Py_MAX(cut, memory) - cut + 1;
441
0
                        LOG("Skip with Memory.\n");
442
0
                        memory = 0;
443
0
                        window_last += Py_MAX(shift, mem_jump);
444
0
                        goto periodicwindowloop;
445
0
                    }
446
0
                    goto no_shift;
447
0
                }
448
0
            }
449
0
            LOG("Found a match!\n");
450
0
            return window - haystack;
451
0
        }
452
0
    }
453
22
    else {
454
22
        period = Py_MAX(gap, period);
455
22
        LOG("Needle is not periodic.\n");
456
16.3k
      windowloop:
457
16.3k
        while (window_last < haystack_end) {
458
74.0k
            for (;;) {
459
74.0k
                LOG_LINEUP();
460
74.0k
                Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
461
74.0k
                window_last += shift;
462
74.0k
                if (shift == 0) {
463
16.3k
                    break;
464
16.3k
                }
465
57.7k
                if (window_last >= haystack_end) {
466
17
                    return -1;
467
17
                }
468
57.7k
                LOG("Horspool skip\n");
469
57.7k
            }
470
16.3k
            window = window_last - len_needle + 1;
471
16.3k
            assert((window[len_needle - 1] & TABLE_MASK) ==
472
16.3k
                   (needle[len_needle - 1] & TABLE_MASK));
473
16.3k
            Py_ssize_t i = cut;
474
16.5k
            for (; i < len_needle; i++) {
475
16.4k
                if (needle[i] != window[i]) {
476
16.2k
                    if (i < gap_jump_end) {
477
16.2k
                        LOG("Early right half mismatch: jump by gap.\n");
478
16.2k
                        assert(gap >= i - cut + 1);
479
16.2k
                        window_last += gap;
480
16.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
16.2k
                    goto windowloop;
487
16.2k
                }
488
16.4k
            }
489
115
            for (Py_ssize_t i = 0; i < cut; i++) {
490
111
                if (needle[i] != window[i]) {
491
79
                    LOG("Left half does not match.\n");
492
79
                    window_last += period;
493
79
                    goto windowloop;
494
79
                }
495
111
            }
496
4
            LOG("Found a match!\n");
497
4
            return window - haystack;
498
83
        }
499
16.3k
    }
500
1
    LOG("Not found. Returning -1.\n");
501
1
    return -1;
502
22
}
Unexecuted instantiation: bytesobject.c:stringlib__two_way
Unexecuted instantiation: unicodeobject.c:asciilib__two_way
unicodeobject.c:ucs1lib__two_way
Line
Count
Source
372
22
{
373
    // Crochemore and Perrin's (1991) Two-Way algorithm.
374
    // See http://www-igm.univ-mlv.fr/~lecroq/string/node26.html#SECTION00260
375
22
    const Py_ssize_t len_needle = p->len_needle;
376
22
    const Py_ssize_t cut = p->cut;
377
22
    Py_ssize_t period = p->period;
378
22
    const STRINGLIB_CHAR *const needle = p->needle;
379
22
    const STRINGLIB_CHAR *window_last = haystack + len_needle - 1;
380
22
    const STRINGLIB_CHAR *const haystack_end = haystack + len_haystack;
381
22
    SHIFT_TYPE *table = p->table;
382
22
    const STRINGLIB_CHAR *window;
383
22
    LOG("===== Two-way: \"%s\" in \"%s\". =====\n", needle, haystack);
384
385
22
    Py_ssize_t gap = p->gap;
386
22
    Py_ssize_t gap_jump_end = Py_MIN(len_needle, cut + gap);
387
22
    if (p->is_periodic) {
388
0
        LOG("Needle is periodic.\n");
389
0
        Py_ssize_t memory = 0;
390
0
      periodicwindowloop:
391
0
        while (window_last < haystack_end) {
392
0
            assert(memory == 0);
393
0
            for (;;) {
394
0
                LOG_LINEUP();
395
0
                Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
396
0
                window_last += shift;
397
0
                if (shift == 0) {
398
0
                    break;
399
0
                }
400
0
                if (window_last >= haystack_end) {
401
0
                    return -1;
402
0
                }
403
0
                LOG("Horspool skip\n");
404
0
            }
405
0
          no_shift:
406
0
            window = window_last - len_needle + 1;
407
0
            assert((window[len_needle - 1] & TABLE_MASK) ==
408
0
                   (needle[len_needle - 1] & TABLE_MASK));
409
0
            Py_ssize_t i = Py_MAX(cut, memory);
410
0
            for (; i < len_needle; i++) {
411
0
                if (needle[i] != window[i]) {
412
0
                    if (i < gap_jump_end) {
413
0
                        LOG("Early right half mismatch: jump by gap.\n");
414
0
                        assert(gap >= i - cut + 1);
415
0
                        window_last += gap;
416
0
                    }
417
0
                    else {
418
0
                        LOG("Late right half mismatch: jump by n (>gap)\n");
419
0
                        assert(i - cut + 1 > gap);
420
0
                        window_last += i - cut + 1;
421
0
                    }
422
0
                    memory = 0;
423
0
                    goto periodicwindowloop;
424
0
                }
425
0
            }
426
0
            for (i = memory; i < cut; i++) {
427
0
                if (needle[i] != window[i]) {
428
0
                    LOG("Left half does not match.\n");
429
0
                    window_last += period;
430
0
                    memory = len_needle - period;
431
0
                    if (window_last >= haystack_end) {
432
0
                        return -1;
433
0
                    }
434
0
                    Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
435
0
                    if (shift) {
436
                        // A mismatch has been identified to the right
437
                        // of where i will next start, so we can jump
438
                        // at least as far as if the mismatch occurred
439
                        // on the first comparison.
440
0
                        Py_ssize_t mem_jump = Py_MAX(cut, memory) - cut + 1;
441
0
                        LOG("Skip with Memory.\n");
442
0
                        memory = 0;
443
0
                        window_last += Py_MAX(shift, mem_jump);
444
0
                        goto periodicwindowloop;
445
0
                    }
446
0
                    goto no_shift;
447
0
                }
448
0
            }
449
0
            LOG("Found a match!\n");
450
0
            return window - haystack;
451
0
        }
452
0
    }
453
22
    else {
454
22
        period = Py_MAX(gap, period);
455
22
        LOG("Needle is not periodic.\n");
456
16.3k
      windowloop:
457
16.3k
        while (window_last < haystack_end) {
458
74.0k
            for (;;) {
459
74.0k
                LOG_LINEUP();
460
74.0k
                Py_ssize_t shift = table[(*window_last) & TABLE_MASK];
461
74.0k
                window_last += shift;
462
74.0k
                if (shift == 0) {
463
16.3k
                    break;
464
16.3k
                }
465
57.7k
                if (window_last >= haystack_end) {
466
17
                    return -1;
467
17
                }
468
57.7k
                LOG("Horspool skip\n");
469
57.7k
            }
470
16.3k
            window = window_last - len_needle + 1;
471
16.3k
            assert((window[len_needle - 1] & TABLE_MASK) ==
472
16.3k
                   (needle[len_needle - 1] & TABLE_MASK));
473
16.3k
            Py_ssize_t i = cut;
474
16.5k
            for (; i < len_needle; i++) {
475
16.4k
                if (needle[i] != window[i]) {
476
16.2k
                    if (i < gap_jump_end) {
477
16.2k
                        LOG("Early right half mismatch: jump by gap.\n");
478
16.2k
                        assert(gap >= i - cut + 1);
479
16.2k
                        window_last += gap;
480
16.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
16.2k
                    goto windowloop;
487
16.2k
                }
488
16.4k
            }
489
115
            for (Py_ssize_t i = 0; i < cut; i++) {
490
111
                if (needle[i] != window[i]) {
491
79
                    LOG("Left half does not match.\n");
492
79
                    window_last += period;
493
79
                    goto windowloop;
494
79
                }
495
111
            }
496
4
            LOG("Found a match!\n");
497
4
            return window - haystack;
498
83
        }
499
16.3k
    }
500
1
    LOG("Not found. Returning -1.\n");
501
1
    return -1;
502
22
}
Unexecuted instantiation: unicodeobject.c:ucs2lib__two_way
Unexecuted instantiation: unicodeobject.c:ucs4lib__two_way
Unexecuted instantiation: bytes_methods.c:stringlib__two_way
Unexecuted instantiation: bytearrayobject.c:stringlib__two_way
503
504
505
static Py_ssize_t
506
STRINGLIB(_two_way_find)(const STRINGLIB_CHAR *haystack,
507
                         Py_ssize_t len_haystack,
508
                         const STRINGLIB_CHAR *needle,
509
                         Py_ssize_t len_needle)
510
22
{
511
22
    LOG("###### Finding \"%s\" in \"%s\".\n", needle, haystack);
512
22
    STRINGLIB(prework) p;
513
22
    STRINGLIB(_preprocess)(needle, len_needle, &p);
514
22
    return STRINGLIB(_two_way)(haystack, len_haystack, &p);
515
22
}
Unexecuted instantiation: bytesobject.c:stringlib__two_way_find
Unexecuted instantiation: unicodeobject.c:asciilib__two_way_find
unicodeobject.c:ucs1lib__two_way_find
Line
Count
Source
510
22
{
511
22
    LOG("###### Finding \"%s\" in \"%s\".\n", needle, haystack);
512
22
    STRINGLIB(prework) p;
513
22
    STRINGLIB(_preprocess)(needle, len_needle, &p);
514
22
    return STRINGLIB(_two_way)(haystack, len_haystack, &p);
515
22
}
Unexecuted instantiation: unicodeobject.c:ucs2lib__two_way_find
Unexecuted instantiation: unicodeobject.c:ucs4lib__two_way_find
Unexecuted instantiation: bytes_methods.c:stringlib__two_way_find
Unexecuted instantiation: bytearrayobject.c:stringlib__two_way_find
516
517
518
static Py_ssize_t
519
STRINGLIB(_two_way_count)(const STRINGLIB_CHAR *haystack,
520
                          Py_ssize_t len_haystack,
521
                          const STRINGLIB_CHAR *needle,
522
                          Py_ssize_t len_needle,
523
                          Py_ssize_t maxcount)
524
0
{
525
0
    LOG("###### Counting \"%s\" in \"%s\".\n", needle, haystack);
526
0
    STRINGLIB(prework) p;
527
0
    STRINGLIB(_preprocess)(needle, len_needle, &p);
528
0
    Py_ssize_t index = 0, count = 0;
529
0
    while (1) {
530
0
        Py_ssize_t result;
531
0
        result = STRINGLIB(_two_way)(haystack + index,
532
0
                                     len_haystack - index, &p);
533
0
        if (result == -1) {
534
0
            return count;
535
0
        }
536
0
        count++;
537
0
        if (count == maxcount) {
538
0
            return maxcount;
539
0
        }
540
0
        index += result + len_needle;
541
0
    }
542
0
    return count;
543
0
}
Unexecuted instantiation: bytesobject.c:stringlib__two_way_count
Unexecuted instantiation: unicodeobject.c:asciilib__two_way_count
Unexecuted instantiation: unicodeobject.c:ucs1lib__two_way_count
Unexecuted instantiation: unicodeobject.c:ucs2lib__two_way_count
Unexecuted instantiation: unicodeobject.c:ucs4lib__two_way_count
Unexecuted instantiation: bytes_methods.c:stringlib__two_way_count
Unexecuted instantiation: bytearrayobject.c:stringlib__two_way_count
544
545
#undef SHIFT_TYPE
546
#undef NOT_FOUND
547
#undef SHIFT_OVERFLOW
548
#undef TABLE_SIZE_BITS
549
#undef TABLE_SIZE
550
#undef TABLE_MASK
551
552
#undef LOG
553
#undef LOG_STRING
554
#undef LOG_LINEUP
555
556
static inline Py_ssize_t
557
STRINGLIB(default_find)(const STRINGLIB_CHAR* s, Py_ssize_t n,
558
                        const STRINGLIB_CHAR* p, Py_ssize_t m,
559
                        Py_ssize_t maxcount, int mode)
560
39.1M
{
561
39.1M
    const Py_ssize_t w = n - m;
562
39.1M
    Py_ssize_t mlast = m - 1, count = 0;
563
39.1M
    Py_ssize_t gap = mlast;
564
39.1M
    const STRINGLIB_CHAR last = p[mlast];
565
39.1M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
39.1M
    unsigned long mask = 0;
568
208M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
169M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
169M
        if (p[i] == last) {
571
587k
            gap = mlast - i - 1;
572
587k
        }
573
169M
    }
574
39.1M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
3.56G
    for (Py_ssize_t i = 0; i <= w; i++) {
577
3.54G
        if (ss[i] == last) {
578
            /* candidate match */
579
48.0M
            Py_ssize_t j;
580
76.2M
            for (j = 0; j < mlast; j++) {
581
48.8M
                if (s[i+j] != p[j]) {
582
20.5M
                    break;
583
20.5M
                }
584
48.8M
            }
585
48.0M
            if (j == mlast) {
586
                /* got a match! */
587
27.4M
                if (mode != FAST_COUNT) {
588
14.1M
                    return i;
589
14.1M
                }
590
13.2M
                count++;
591
13.2M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
13.2M
                i = i + mlast;
595
13.2M
                continue;
596
13.2M
            }
597
            /* miss: check if next character is part of pattern */
598
20.5M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
4.85M
                i = i + m;
600
4.85M
            }
601
15.7M
            else {
602
15.7M
                i = i + gap;
603
15.7M
            }
604
20.5M
        }
605
3.49G
        else {
606
            /* skip: check if next character is part of pattern */
607
3.49G
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
3.35G
                i = i + m;
609
3.35G
            }
610
3.49G
        }
611
3.54G
    }
612
24.9M
    return mode == FAST_COUNT ? count : -1;
613
39.1M
}
Unexecuted instantiation: bytesobject.c:stringlib_default_find
unicodeobject.c:asciilib_default_find
Line
Count
Source
560
2.29M
{
561
2.29M
    const Py_ssize_t w = n - m;
562
2.29M
    Py_ssize_t mlast = m - 1, count = 0;
563
2.29M
    Py_ssize_t gap = mlast;
564
2.29M
    const STRINGLIB_CHAR last = p[mlast];
565
2.29M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
2.29M
    unsigned long mask = 0;
568
4.86M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
2.56M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
2.56M
        if (p[i] == last) {
571
23.1k
            gap = mlast - i - 1;
572
23.1k
        }
573
2.56M
    }
574
2.29M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
153M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
153M
        if (ss[i] == last) {
578
            /* candidate match */
579
4.06M
            Py_ssize_t j;
580
6.61M
            for (j = 0; j < mlast; j++) {
581
4.35M
                if (s[i+j] != p[j]) {
582
1.80M
                    break;
583
1.80M
                }
584
4.35M
            }
585
4.06M
            if (j == mlast) {
586
                /* got a match! */
587
2.25M
                if (mode != FAST_COUNT) {
588
2.25M
                    return i;
589
2.25M
                }
590
0
                count++;
591
0
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
0
                i = i + mlast;
595
0
                continue;
596
0
            }
597
            /* miss: check if next character is part of pattern */
598
1.80M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
62.4k
                i = i + m;
600
62.4k
            }
601
1.74M
            else {
602
1.74M
                i = i + gap;
603
1.74M
            }
604
1.80M
        }
605
149M
        else {
606
            /* skip: check if next character is part of pattern */
607
149M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
144M
                i = i + m;
609
144M
            }
610
149M
        }
611
153M
    }
612
34.6k
    return mode == FAST_COUNT ? count : -1;
613
2.29M
}
unicodeobject.c:ucs1lib_default_find
Line
Count
Source
560
27.0M
{
561
27.0M
    const Py_ssize_t w = n - m;
562
27.0M
    Py_ssize_t mlast = m - 1, count = 0;
563
27.0M
    Py_ssize_t gap = mlast;
564
27.0M
    const STRINGLIB_CHAR last = p[mlast];
565
27.0M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
27.0M
    unsigned long mask = 0;
568
184M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
157M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
157M
        if (p[i] == last) {
571
506k
            gap = mlast - i - 1;
572
506k
        }
573
157M
    }
574
27.0M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
741M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
717M
        if (ss[i] == last) {
578
            /* candidate match */
579
14.0M
            Py_ssize_t j;
580
20.4M
            for (j = 0; j < mlast; j++) {
581
14.4M
                if (s[i+j] != p[j]) {
582
7.97M
                    break;
583
7.97M
                }
584
14.4M
            }
585
14.0M
            if (j == mlast) {
586
                /* got a match! */
587
6.07M
                if (mode != FAST_COUNT) {
588
2.25M
                    return i;
589
2.25M
                }
590
3.82M
                count++;
591
3.82M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
3.82M
                i = i + mlast;
595
3.82M
                continue;
596
3.82M
            }
597
            /* miss: check if next character is part of pattern */
598
7.97M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
1.85M
                i = i + m;
600
1.85M
            }
601
6.11M
            else {
602
6.11M
                i = i + gap;
603
6.11M
            }
604
7.97M
        }
605
703M
        else {
606
            /* skip: check if next character is part of pattern */
607
703M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
599M
                i = i + m;
609
599M
            }
610
703M
        }
611
717M
    }
612
24.7M
    return mode == FAST_COUNT ? count : -1;
613
27.0M
}
unicodeobject.c:ucs2lib_default_find
Line
Count
Source
560
3.91M
{
561
3.91M
    const Py_ssize_t w = n - m;
562
3.91M
    Py_ssize_t mlast = m - 1, count = 0;
563
3.91M
    Py_ssize_t gap = mlast;
564
3.91M
    const STRINGLIB_CHAR last = p[mlast];
565
3.91M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
3.91M
    unsigned long mask = 0;
568
7.96M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
4.05M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
4.05M
        if (p[i] == last) {
571
32.8k
            gap = mlast - i - 1;
572
32.8k
        }
573
4.05M
    }
574
3.91M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
969M
    for (Py_ssize_t i = 0; i <= w; i++) {
577
969M
        if (ss[i] == last) {
578
            /* candidate match */
579
13.0M
            Py_ssize_t j;
580
20.5M
            for (j = 0; j < mlast; j++) {
581
13.1M
                if (s[i+j] != p[j]) {
582
5.65M
                    break;
583
5.65M
                }
584
13.1M
            }
585
13.0M
            if (j == mlast) {
586
                /* got a match! */
587
7.40M
                if (mode != FAST_COUNT) {
588
3.80M
                    return i;
589
3.80M
                }
590
3.60M
                count++;
591
3.60M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
3.60M
                i = i + mlast;
595
3.60M
                continue;
596
3.60M
            }
597
            /* miss: check if next character is part of pattern */
598
5.65M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
1.10M
                i = i + m;
600
1.10M
            }
601
4.54M
            else {
602
4.54M
                i = i + gap;
603
4.54M
            }
604
5.65M
        }
605
956M
        else {
606
            /* skip: check if next character is part of pattern */
607
956M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
943M
                i = i + m;
609
943M
            }
610
956M
        }
611
969M
    }
612
115k
    return mode == FAST_COUNT ? count : -1;
613
3.91M
}
unicodeobject.c:ucs4lib_default_find
Line
Count
Source
560
5.89M
{
561
5.89M
    const Py_ssize_t w = n - m;
562
5.89M
    Py_ssize_t mlast = m - 1, count = 0;
563
5.89M
    Py_ssize_t gap = mlast;
564
5.89M
    const STRINGLIB_CHAR last = p[mlast];
565
5.89M
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
5.89M
    unsigned long mask = 0;
568
11.8M
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
5.92M
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
5.92M
        if (p[i] == last) {
571
22.1k
            gap = mlast - i - 1;
572
22.1k
        }
573
5.92M
    }
574
5.89M
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
1.70G
    for (Py_ssize_t i = 0; i <= w; i++) {
577
1.70G
        if (ss[i] == last) {
578
            /* candidate match */
579
16.8M
            Py_ssize_t j;
580
28.5M
            for (j = 0; j < mlast; j++) {
581
16.8M
                if (s[i+j] != p[j]) {
582
5.12M
                    break;
583
5.12M
                }
584
16.8M
            }
585
16.8M
            if (j == mlast) {
586
                /* got a match! */
587
11.6M
                if (mode != FAST_COUNT) {
588
5.86M
                    return i;
589
5.86M
                }
590
5.82M
                count++;
591
5.82M
                if (count == maxcount) {
592
0
                    return maxcount;
593
0
                }
594
5.82M
                i = i + mlast;
595
5.82M
                continue;
596
5.82M
            }
597
            /* miss: check if next character is part of pattern */
598
5.12M
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
1.82M
                i = i + m;
600
1.82M
            }
601
3.30M
            else {
602
3.30M
                i = i + gap;
603
3.30M
            }
604
5.12M
        }
605
1.68G
        else {
606
            /* skip: check if next character is part of pattern */
607
1.68G
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
1.66G
                i = i + m;
609
1.66G
            }
610
1.68G
        }
611
1.70G
    }
612
30.3k
    return mode == FAST_COUNT ? count : -1;
613
5.89M
}
bytes_methods.c:stringlib_default_find
Line
Count
Source
560
2.93k
{
561
2.93k
    const Py_ssize_t w = n - m;
562
2.93k
    Py_ssize_t mlast = m - 1, count = 0;
563
2.93k
    Py_ssize_t gap = mlast;
564
2.93k
    const STRINGLIB_CHAR last = p[mlast];
565
2.93k
    const STRINGLIB_CHAR *const ss = &s[mlast];
566
567
2.93k
    unsigned long mask = 0;
568
11.7k
    for (Py_ssize_t i = 0; i < mlast; i++) {
569
8.79k
        STRINGLIB_BLOOM_ADD(mask, p[i]);
570
8.79k
        if (p[i] == last) {
571
2.93k
            gap = mlast - i - 1;
572
2.93k
        }
573
8.79k
    }
574
2.93k
    STRINGLIB_BLOOM_ADD(mask, last);
575
576
791k
    for (Py_ssize_t i = 0; i <= w; i++) {
577
790k
        if (ss[i] == last) {
578
            /* candidate match */
579
8.70k
            Py_ssize_t j;
580
17.1k
            for (j = 0; j < mlast; j++) {
581
14.3k
                if (s[i+j] != p[j]) {
582
5.99k
                    break;
583
5.99k
                }
584
14.3k
            }
585
8.70k
            if (j == mlast) {
586
                /* got a match! */
587
2.70k
                if (mode != FAST_COUNT) {
588
2.70k
                    return i;
589
2.70k
                }
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.99k
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
599
627
                i = i + m;
600
627
            }
601
5.36k
            else {
602
5.36k
                i = i + gap;
603
5.36k
            }
604
5.99k
        }
605
782k
        else {
606
            /* skip: check if next character is part of pattern */
607
782k
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
608
42.0k
                i = i + m;
609
42.0k
            }
610
782k
        }
611
790k
    }
612
223
    return mode == FAST_COUNT ? count : -1;
613
2.93k
}
Unexecuted instantiation: bytearrayobject.c:stringlib_default_find
614
615
616
static Py_ssize_t
617
STRINGLIB(adaptive_find)(const STRINGLIB_CHAR* s, Py_ssize_t n,
618
                         const STRINGLIB_CHAR* p, Py_ssize_t m,
619
                         Py_ssize_t maxcount, int mode)
620
0
{
621
0
    const Py_ssize_t w = n - m;
622
0
    Py_ssize_t mlast = m - 1, count = 0;
623
0
    Py_ssize_t gap = mlast;
624
0
    Py_ssize_t hits = 0, res;
625
0
    const STRINGLIB_CHAR last = p[mlast];
626
0
    const STRINGLIB_CHAR *const ss = &s[mlast];
627
628
0
    unsigned long mask = 0;
629
0
    for (Py_ssize_t i = 0; i < mlast; i++) {
630
0
        STRINGLIB_BLOOM_ADD(mask, p[i]);
631
0
        if (p[i] == last) {
632
0
            gap = mlast - i - 1;
633
0
        }
634
0
    }
635
0
    STRINGLIB_BLOOM_ADD(mask, last);
636
637
0
    for (Py_ssize_t i = 0; i <= w; i++) {
638
0
        if (ss[i] == last) {
639
            /* candidate match */
640
0
            Py_ssize_t j;
641
0
            for (j = 0; j < mlast; j++) {
642
0
                if (s[i+j] != p[j]) {
643
0
                    break;
644
0
                }
645
0
            }
646
0
            if (j == mlast) {
647
                /* got a match! */
648
0
                if (mode != FAST_COUNT) {
649
0
                    return i;
650
0
                }
651
0
                count++;
652
0
                if (count == maxcount) {
653
0
                    return maxcount;
654
0
                }
655
0
                i = i + mlast;
656
0
                continue;
657
0
            }
658
0
            hits += j + 1;
659
0
            if (hits > m / 4 && w - i > 2000) {
660
0
                if (mode == FAST_SEARCH) {
661
0
                    res = STRINGLIB(_two_way_find)(s + i, n - i, p, m);
662
0
                    return res == -1 ? -1 : res + i;
663
0
                }
664
0
                else {
665
0
                    res = STRINGLIB(_two_way_count)(s + i, n - i, p, m,
666
0
                                                    maxcount - count);
667
0
                    return res + count;
668
0
                }
669
0
            }
670
            /* miss: check if next character is part of pattern */
671
0
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
672
0
                i = i + m;
673
0
            }
674
0
            else {
675
0
                i = i + gap;
676
0
            }
677
0
        }
678
0
        else {
679
            /* skip: check if next character is part of pattern */
680
0
            if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
681
0
                i = i + m;
682
0
            }
683
0
        }
684
0
    }
685
0
    return mode == FAST_COUNT ? count : -1;
686
0
}
Unexecuted instantiation: bytesobject.c:stringlib_adaptive_find
Unexecuted instantiation: unicodeobject.c:asciilib_adaptive_find
Unexecuted instantiation: unicodeobject.c:ucs1lib_adaptive_find
Unexecuted instantiation: unicodeobject.c:ucs2lib_adaptive_find
Unexecuted instantiation: unicodeobject.c:ucs4lib_adaptive_find
Unexecuted instantiation: bytes_methods.c:stringlib_adaptive_find
Unexecuted instantiation: bytearrayobject.c:stringlib_adaptive_find
687
688
689
static Py_ssize_t
690
STRINGLIB(default_rfind)(const STRINGLIB_CHAR* s, Py_ssize_t n,
691
                         const STRINGLIB_CHAR* p, Py_ssize_t m,
692
                         Py_ssize_t maxcount, int mode)
693
7.26k
{
694
    /* create compressed boyer-moore delta 1 table */
695
7.26k
    unsigned long mask = 0;
696
7.26k
    Py_ssize_t i, j, mlast = m - 1, skip = m - 1, w = n - m;
697
698
    /* process pattern[0] outside the loop */
699
7.26k
    STRINGLIB_BLOOM_ADD(mask, p[0]);
700
    /* process pattern[:0:-1] */
701
29.0k
    for (i = mlast; i > 0; i--) {
702
21.8k
        STRINGLIB_BLOOM_ADD(mask, p[i]);
703
21.8k
        if (p[i] == p[0]) {
704
0
            skip = i - 1;
705
0
        }
706
21.8k
    }
707
708
1.54M
    for (i = w; i >= 0; i--) {
709
1.54M
        if (s[i] == p[0]) {
710
            /* candidate match */
711
81.8k
            for (j = mlast; j > 0; j--) {
712
74.6k
                if (s[i+j] != p[j]) {
713
52.5k
                    break;
714
52.5k
                }
715
74.6k
            }
716
59.7k
            if (j == 0) {
717
                /* got a match! */
718
7.18k
                return i;
719
7.18k
            }
720
            /* miss: check if previous character is part of pattern */
721
52.5k
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
722
50.9k
                i = i - m;
723
50.9k
            }
724
1.62k
            else {
725
1.62k
                i = i - skip;
726
1.62k
            }
727
52.5k
        }
728
1.48M
        else {
729
            /* skip: check if previous character is part of pattern */
730
1.48M
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
731
1.35M
                i = i - m;
732
1.35M
            }
733
1.48M
        }
734
1.54M
    }
735
86
    return -1;
736
7.26k
}
Unexecuted instantiation: bytesobject.c:stringlib_default_rfind
Unexecuted instantiation: unicodeobject.c:asciilib_default_rfind
Unexecuted instantiation: unicodeobject.c:ucs1lib_default_rfind
Unexecuted instantiation: unicodeobject.c:ucs2lib_default_rfind
Unexecuted instantiation: unicodeobject.c:ucs4lib_default_rfind
bytes_methods.c:stringlib_default_rfind
Line
Count
Source
693
7.26k
{
694
    /* create compressed boyer-moore delta 1 table */
695
7.26k
    unsigned long mask = 0;
696
7.26k
    Py_ssize_t i, j, mlast = m - 1, skip = m - 1, w = n - m;
697
698
    /* process pattern[0] outside the loop */
699
7.26k
    STRINGLIB_BLOOM_ADD(mask, p[0]);
700
    /* process pattern[:0:-1] */
701
29.0k
    for (i = mlast; i > 0; i--) {
702
21.8k
        STRINGLIB_BLOOM_ADD(mask, p[i]);
703
21.8k
        if (p[i] == p[0]) {
704
0
            skip = i - 1;
705
0
        }
706
21.8k
    }
707
708
1.54M
    for (i = w; i >= 0; i--) {
709
1.54M
        if (s[i] == p[0]) {
710
            /* candidate match */
711
81.8k
            for (j = mlast; j > 0; j--) {
712
74.6k
                if (s[i+j] != p[j]) {
713
52.5k
                    break;
714
52.5k
                }
715
74.6k
            }
716
59.7k
            if (j == 0) {
717
                /* got a match! */
718
7.18k
                return i;
719
7.18k
            }
720
            /* miss: check if previous character is part of pattern */
721
52.5k
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
722
50.9k
                i = i - m;
723
50.9k
            }
724
1.62k
            else {
725
1.62k
                i = i - skip;
726
1.62k
            }
727
52.5k
        }
728
1.48M
        else {
729
            /* skip: check if previous character is part of pattern */
730
1.48M
            if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1])) {
731
1.35M
                i = i - m;
732
1.35M
            }
733
1.48M
        }
734
1.54M
    }
735
86
    return -1;
736
7.26k
}
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
69.3M
{
762
69.3M
    Py_ssize_t count = 0;
763
9.40G
    for (Py_ssize_t i = 0; i < n; i++) {
764
9.33G
        if (s[i] == p0) {
765
993M
            count++;
766
993M
        }
767
9.33G
    }
768
69.3M
    return count;
769
69.3M
}
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
50.1M
{
762
50.1M
    Py_ssize_t count = 0;
763
1.58G
    for (Py_ssize_t i = 0; i < n; i++) {
764
1.53G
        if (s[i] == p0) {
765
50.4M
            count++;
766
50.4M
        }
767
1.53G
    }
768
50.1M
    return count;
769
50.1M
}
unicodeobject.c:ucs2lib_count_char_no_maxcount
Line
Count
Source
761
10.6M
{
762
10.6M
    Py_ssize_t count = 0;
763
2.03G
    for (Py_ssize_t i = 0; i < n; i++) {
764
2.02G
        if (s[i] == p0) {
765
77.8M
            count++;
766
77.8M
        }
767
2.02G
    }
768
10.6M
    return count;
769
10.6M
}
unicodeobject.c:ucs4lib_count_char_no_maxcount
Line
Count
Source
761
1.96M
{
762
1.96M
    Py_ssize_t count = 0;
763
2.41G
    for (Py_ssize_t i = 0; i < n; i++) {
764
2.41G
        if (s[i] == p0) {
765
61.0M
            count++;
766
61.0M
        }
767
2.41G
    }
768
1.96M
    return count;
769
1.96M
}
bytes_methods.c:stringlib_count_char_no_maxcount
Line
Count
Source
761
6.55M
{
762
6.55M
    Py_ssize_t count = 0;
763
3.36G
    for (Py_ssize_t i = 0; i < n; i++) {
764
3.35G
        if (s[i] == p0) {
765
804M
            count++;
766
804M
        }
767
3.35G
    }
768
6.55M
    return count;
769
6.55M
}
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
260M
{
777
260M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
16.8k
        return -1;
779
16.8k
    }
780
781
    /* look for special cases */
782
260M
    if (m <= 1) {
783
221M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
221M
        if (mode == FAST_SEARCH)
788
151M
            return STRINGLIB(find_char)(s, n, p[0]);
789
69.4M
        else if (mode == FAST_RSEARCH)
790
48.0k
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
69.3M
        else {
792
69.3M
            if (maxcount == PY_SSIZE_T_MAX) {
793
69.3M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
69.3M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
69.3M
        }
797
221M
    }
798
799
39.1M
    if (mode != FAST_RSEARCH) {
800
39.1M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
39.1M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
39.1M
        }
803
22
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
22
            if (mode == FAST_SEARCH) {
810
22
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
22
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
22
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
39.1M
    }
825
7.26k
    else {
826
        /* FAST_RSEARCH */
827
7.26k
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
7.26k
    }
829
39.1M
}
bytesobject.c:fastsearch
Line
Count
Source
776
515k
{
777
515k
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
515k
    if (m <= 1) {
783
515k
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
515k
        if (mode == FAST_SEARCH)
788
515k
            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
515k
    }
798
799
0
    if (mode != FAST_RSEARCH) {
800
0
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
0
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
0
        }
803
0
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
0
            if (mode == FAST_SEARCH) {
810
0
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
0
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
0
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
0
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
0
}
unicodeobject.c:asciilib_fastsearch
Line
Count
Source
776
27.9M
{
777
27.9M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
27.9M
    if (m <= 1) {
783
25.6M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
25.6M
        if (mode == FAST_SEARCH)
788
25.6M
            return STRINGLIB(find_char)(s, n, p[0]);
789
48.0k
        else if (mode == FAST_RSEARCH)
790
48.0k
            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
25.6M
    }
798
799
2.29M
    if (mode != FAST_RSEARCH) {
800
2.29M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
2.29M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
2.29M
        }
803
0
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
0
            if (mode == FAST_SEARCH) {
810
0
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
0
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
0
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
2.29M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
2.29M
}
unicodeobject.c:ucs1lib_fastsearch
Line
Count
Source
776
85.6M
{
777
85.6M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
85.6M
    if (m <= 1) {
783
58.5M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
58.5M
        if (mode == FAST_SEARCH)
788
8.45M
            return STRINGLIB(find_char)(s, n, p[0]);
789
50.1M
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
50.1M
        else {
792
50.1M
            if (maxcount == PY_SSIZE_T_MAX) {
793
50.1M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
50.1M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
50.1M
        }
797
58.5M
    }
798
799
27.0M
    if (mode != FAST_RSEARCH) {
800
27.0M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
27.0M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
27.0M
        }
803
22
        else if ((m >> 2) * 3 < (n >> 2)) {
804
            /* 33% threshold, but don't overflow. */
805
            /* For larger problems where the needle isn't a huge
806
               percentage of the size of the haystack, the relatively
807
               expensive O(m) startup cost of the two-way algorithm
808
               will surely pay off. */
809
22
            if (mode == FAST_SEARCH) {
810
22
                return STRINGLIB(_two_way_find)(s, n, p, m);
811
22
            }
812
0
            else {
813
0
                return STRINGLIB(_two_way_count)(s, n, p, m, maxcount);
814
0
            }
815
22
        }
816
0
        else {
817
            /* To ensure that we have good worst-case behavior,
818
               here's an adaptive version of the algorithm, where if
819
               we match O(m) characters without any matches of the
820
               entire needle, then we predict that the startup cost of
821
               the two-way algorithm will probably be worth it. */
822
0
            return STRINGLIB(adaptive_find)(s, n, p, m, maxcount, mode);
823
0
        }
824
27.0M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
27.0M
}
unicodeobject.c:ucs2lib_fastsearch
Line
Count
Source
776
80.1M
{
777
80.1M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
16.8k
        return -1;
779
16.8k
    }
780
781
    /* look for special cases */
782
80.1M
    if (m <= 1) {
783
76.2M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
76.2M
        if (mode == FAST_SEARCH)
788
65.5M
            return STRINGLIB(find_char)(s, n, p[0]);
789
10.6M
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
10.6M
        else {
792
10.6M
            if (maxcount == PY_SSIZE_T_MAX) {
793
10.6M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
10.6M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
10.6M
        }
797
76.2M
    }
798
799
3.91M
    if (mode != FAST_RSEARCH) {
800
3.91M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
3.91M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
3.91M
        }
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.91M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
3.91M
}
unicodeobject.c:ucs4lib_fastsearch
Line
Count
Source
776
59.2M
{
777
59.2M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
0
        return -1;
779
0
    }
780
781
    /* look for special cases */
782
59.2M
    if (m <= 1) {
783
53.3M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
53.3M
        if (mode == FAST_SEARCH)
788
51.4M
            return STRINGLIB(find_char)(s, n, p[0]);
789
1.96M
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
1.96M
        else {
792
1.96M
            if (maxcount == PY_SSIZE_T_MAX) {
793
1.96M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
1.96M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
1.96M
        }
797
53.3M
    }
798
799
5.89M
    if (mode != FAST_RSEARCH) {
800
5.89M
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
5.89M
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
5.89M
        }
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
5.89M
    }
825
0
    else {
826
        /* FAST_RSEARCH */
827
0
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
0
    }
829
5.89M
}
bytes_methods.c:fastsearch
Line
Count
Source
776
6.56M
{
777
6.56M
    if (n < m || (mode == FAST_COUNT && maxcount == 0)) {
778
10
        return -1;
779
10
    }
780
781
    /* look for special cases */
782
6.56M
    if (m <= 1) {
783
6.55M
        if (m <= 0) {
784
0
            return -1;
785
0
        }
786
        /* use special case for 1-character strings */
787
6.55M
        if (mode == FAST_SEARCH)
788
0
            return STRINGLIB(find_char)(s, n, p[0]);
789
6.55M
        else if (mode == FAST_RSEARCH)
790
0
            return STRINGLIB(rfind_char)(s, n, p[0]);
791
6.55M
        else {
792
6.55M
            if (maxcount == PY_SSIZE_T_MAX) {
793
6.55M
                return STRINGLIB(count_char_no_maxcount)(s, n, p[0]);
794
6.55M
            }
795
0
            return STRINGLIB(count_char)(s, n, p[0], maxcount);
796
6.55M
        }
797
6.55M
    }
798
799
10.1k
    if (mode != FAST_RSEARCH) {
800
2.93k
        if (n < 2500 || (m < 100 && n < 30000) || m < 6) {
801
2.93k
            return STRINGLIB(default_find)(s, n, p, m, maxcount, mode);
802
2.93k
        }
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.93k
    }
825
7.26k
    else {
826
        /* FAST_RSEARCH */
827
7.26k
        return STRINGLIB(default_rfind)(s, n, p, m, maxcount, mode);
828
7.26k
    }
829
10.1k
}
Unexecuted instantiation: bytearrayobject.c:fastsearch
830