Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Modules/_sre/sre_lib.h
Line
Count
Source
1
/*
2
 * Secret Labs' Regular Expression Engine
3
 *
4
 * regular expression matching engine
5
 *
6
 * Copyright (c) 1997-2001 by Secret Labs AB.  All rights reserved.
7
 *
8
 * See the sre.c file for information on usage and redistribution.
9
 */
10
11
/* String matching engine */
12
13
/* This file is included three times, with different character settings */
14
15
LOCAL(int)
16
SRE(at)(SRE_STATE* state, const SRE_CHAR* ptr, SRE_CODE at)
17
30.6M
{
18
    /* check if pointer is at given position */
19
20
30.6M
    Py_ssize_t thisp, thatp;
21
22
30.6M
    switch (at) {
23
24
15.3M
    case SRE_AT_BEGINNING:
25
15.3M
    case SRE_AT_BEGINNING_STRING:
26
15.3M
        return ((void*) ptr == state->beginning);
27
28
0
    case SRE_AT_BEGINNING_LINE:
29
0
        return ((void*) ptr == state->beginning ||
30
0
                SRE_IS_LINEBREAK((int) ptr[-1]));
31
32
12.2M
    case SRE_AT_END:
33
12.2M
        return (((SRE_CHAR *)state->end - ptr == 1 &&
34
24.4k
                 SRE_IS_LINEBREAK((int) ptr[0])) ||
35
12.2M
                ((void*) ptr == state->end));
36
37
0
    case SRE_AT_END_LINE:
38
0
        return ((void*) ptr == state->end ||
39
0
                SRE_IS_LINEBREAK((int) ptr[0]));
40
41
3.03M
    case SRE_AT_END_STRING:
42
3.03M
        return ((void*) ptr == state->end);
43
44
0
    case SRE_AT_BOUNDARY:
45
0
        thatp = ((void*) ptr > state->beginning) ?
46
0
            SRE_IS_WORD((int) ptr[-1]) : 0;
47
0
        thisp = ((void*) ptr < state->end) ?
48
0
            SRE_IS_WORD((int) ptr[0]) : 0;
49
0
        return thisp != thatp;
50
51
0
    case SRE_AT_NON_BOUNDARY:
52
0
        thatp = ((void*) ptr > state->beginning) ?
53
0
            SRE_IS_WORD((int) ptr[-1]) : 0;
54
0
        thisp = ((void*) ptr < state->end) ?
55
0
            SRE_IS_WORD((int) ptr[0]) : 0;
56
0
        return thisp == thatp;
57
58
0
    case SRE_AT_LOC_BOUNDARY:
59
0
        thatp = ((void*) ptr > state->beginning) ?
60
0
            SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
61
0
        thisp = ((void*) ptr < state->end) ?
62
0
            SRE_LOC_IS_WORD((int) ptr[0]) : 0;
63
0
        return thisp != thatp;
64
65
0
    case SRE_AT_LOC_NON_BOUNDARY:
66
0
        thatp = ((void*) ptr > state->beginning) ?
67
0
            SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
68
0
        thisp = ((void*) ptr < state->end) ?
69
0
            SRE_LOC_IS_WORD((int) ptr[0]) : 0;
70
0
        return thisp == thatp;
71
72
0
    case SRE_AT_UNI_BOUNDARY:
73
0
        thatp = ((void*) ptr > state->beginning) ?
74
0
            SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
75
0
        thisp = ((void*) ptr < state->end) ?
76
0
            SRE_UNI_IS_WORD((int) ptr[0]) : 0;
77
0
        return thisp != thatp;
78
79
0
    case SRE_AT_UNI_NON_BOUNDARY:
80
0
        thatp = ((void*) ptr > state->beginning) ?
81
0
            SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
82
0
        thisp = ((void*) ptr < state->end) ?
83
0
            SRE_UNI_IS_WORD((int) ptr[0]) : 0;
84
0
        return thisp == thatp;
85
86
30.6M
    }
87
88
0
    return 0;
89
30.6M
}
sre.c:sre_ucs1_at
Line
Count
Source
17
25.3M
{
18
    /* check if pointer is at given position */
19
20
25.3M
    Py_ssize_t thisp, thatp;
21
22
25.3M
    switch (at) {
23
24
13.1M
    case SRE_AT_BEGINNING:
25
13.1M
    case SRE_AT_BEGINNING_STRING:
26
13.1M
        return ((void*) ptr == state->beginning);
27
28
0
    case SRE_AT_BEGINNING_LINE:
29
0
        return ((void*) ptr == state->beginning ||
30
0
                SRE_IS_LINEBREAK((int) ptr[-1]));
31
32
10.1M
    case SRE_AT_END:
33
10.1M
        return (((SRE_CHAR *)state->end - ptr == 1 &&
34
24.2k
                 SRE_IS_LINEBREAK((int) ptr[0])) ||
35
10.1M
                ((void*) ptr == state->end));
36
37
0
    case SRE_AT_END_LINE:
38
0
        return ((void*) ptr == state->end ||
39
0
                SRE_IS_LINEBREAK((int) ptr[0]));
40
41
2.09M
    case SRE_AT_END_STRING:
42
2.09M
        return ((void*) ptr == state->end);
43
44
0
    case SRE_AT_BOUNDARY:
45
0
        thatp = ((void*) ptr > state->beginning) ?
46
0
            SRE_IS_WORD((int) ptr[-1]) : 0;
47
0
        thisp = ((void*) ptr < state->end) ?
48
0
            SRE_IS_WORD((int) ptr[0]) : 0;
49
0
        return thisp != thatp;
50
51
0
    case SRE_AT_NON_BOUNDARY:
52
0
        thatp = ((void*) ptr > state->beginning) ?
53
0
            SRE_IS_WORD((int) ptr[-1]) : 0;
54
0
        thisp = ((void*) ptr < state->end) ?
55
0
            SRE_IS_WORD((int) ptr[0]) : 0;
56
0
        return thisp == thatp;
57
58
0
    case SRE_AT_LOC_BOUNDARY:
59
0
        thatp = ((void*) ptr > state->beginning) ?
60
0
            SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
61
0
        thisp = ((void*) ptr < state->end) ?
62
0
            SRE_LOC_IS_WORD((int) ptr[0]) : 0;
63
0
        return thisp != thatp;
64
65
0
    case SRE_AT_LOC_NON_BOUNDARY:
66
0
        thatp = ((void*) ptr > state->beginning) ?
67
0
            SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
68
0
        thisp = ((void*) ptr < state->end) ?
69
0
            SRE_LOC_IS_WORD((int) ptr[0]) : 0;
70
0
        return thisp == thatp;
71
72
0
    case SRE_AT_UNI_BOUNDARY:
73
0
        thatp = ((void*) ptr > state->beginning) ?
74
0
            SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
75
0
        thisp = ((void*) ptr < state->end) ?
76
0
            SRE_UNI_IS_WORD((int) ptr[0]) : 0;
77
0
        return thisp != thatp;
78
79
0
    case SRE_AT_UNI_NON_BOUNDARY:
80
0
        thatp = ((void*) ptr > state->beginning) ?
81
0
            SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
82
0
        thisp = ((void*) ptr < state->end) ?
83
0
            SRE_UNI_IS_WORD((int) ptr[0]) : 0;
84
0
        return thisp == thatp;
85
86
25.3M
    }
87
88
0
    return 0;
89
25.3M
}
sre.c:sre_ucs2_at
Line
Count
Source
17
4.76M
{
18
    /* check if pointer is at given position */
19
20
4.76M
    Py_ssize_t thisp, thatp;
21
22
4.76M
    switch (at) {
23
24
2.18M
    case SRE_AT_BEGINNING:
25
2.18M
    case SRE_AT_BEGINNING_STRING:
26
2.18M
        return ((void*) ptr == state->beginning);
27
28
0
    case SRE_AT_BEGINNING_LINE:
29
0
        return ((void*) ptr == state->beginning ||
30
0
                SRE_IS_LINEBREAK((int) ptr[-1]));
31
32
2.11M
    case SRE_AT_END:
33
2.11M
        return (((SRE_CHAR *)state->end - ptr == 1 &&
34
119
                 SRE_IS_LINEBREAK((int) ptr[0])) ||
35
2.11M
                ((void*) ptr == state->end));
36
37
0
    case SRE_AT_END_LINE:
38
0
        return ((void*) ptr == state->end ||
39
0
                SRE_IS_LINEBREAK((int) ptr[0]));
40
41
468k
    case SRE_AT_END_STRING:
42
468k
        return ((void*) ptr == state->end);
43
44
0
    case SRE_AT_BOUNDARY:
45
0
        thatp = ((void*) ptr > state->beginning) ?
46
0
            SRE_IS_WORD((int) ptr[-1]) : 0;
47
0
        thisp = ((void*) ptr < state->end) ?
48
0
            SRE_IS_WORD((int) ptr[0]) : 0;
49
0
        return thisp != thatp;
50
51
0
    case SRE_AT_NON_BOUNDARY:
52
0
        thatp = ((void*) ptr > state->beginning) ?
53
0
            SRE_IS_WORD((int) ptr[-1]) : 0;
54
0
        thisp = ((void*) ptr < state->end) ?
55
0
            SRE_IS_WORD((int) ptr[0]) : 0;
56
0
        return thisp == thatp;
57
58
0
    case SRE_AT_LOC_BOUNDARY:
59
0
        thatp = ((void*) ptr > state->beginning) ?
60
0
            SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
61
0
        thisp = ((void*) ptr < state->end) ?
62
0
            SRE_LOC_IS_WORD((int) ptr[0]) : 0;
63
0
        return thisp != thatp;
64
65
0
    case SRE_AT_LOC_NON_BOUNDARY:
66
0
        thatp = ((void*) ptr > state->beginning) ?
67
0
            SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
68
0
        thisp = ((void*) ptr < state->end) ?
69
0
            SRE_LOC_IS_WORD((int) ptr[0]) : 0;
70
0
        return thisp == thatp;
71
72
0
    case SRE_AT_UNI_BOUNDARY:
73
0
        thatp = ((void*) ptr > state->beginning) ?
74
0
            SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
75
0
        thisp = ((void*) ptr < state->end) ?
76
0
            SRE_UNI_IS_WORD((int) ptr[0]) : 0;
77
0
        return thisp != thatp;
78
79
0
    case SRE_AT_UNI_NON_BOUNDARY:
80
0
        thatp = ((void*) ptr > state->beginning) ?
81
0
            SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
82
0
        thisp = ((void*) ptr < state->end) ?
83
0
            SRE_UNI_IS_WORD((int) ptr[0]) : 0;
84
0
        return thisp == thatp;
85
86
4.76M
    }
87
88
0
    return 0;
89
4.76M
}
sre.c:sre_ucs4_at
Line
Count
Source
17
539k
{
18
    /* check if pointer is at given position */
19
20
539k
    Py_ssize_t thisp, thatp;
21
22
539k
    switch (at) {
23
24
21.9k
    case SRE_AT_BEGINNING:
25
21.9k
    case SRE_AT_BEGINNING_STRING:
26
21.9k
        return ((void*) ptr == state->beginning);
27
28
0
    case SRE_AT_BEGINNING_LINE:
29
0
        return ((void*) ptr == state->beginning ||
30
0
                SRE_IS_LINEBREAK((int) ptr[-1]));
31
32
43.3k
    case SRE_AT_END:
33
43.3k
        return (((SRE_CHAR *)state->end - ptr == 1 &&
34
102
                 SRE_IS_LINEBREAK((int) ptr[0])) ||
35
43.3k
                ((void*) ptr == state->end));
36
37
0
    case SRE_AT_END_LINE:
38
0
        return ((void*) ptr == state->end ||
39
0
                SRE_IS_LINEBREAK((int) ptr[0]));
40
41
474k
    case SRE_AT_END_STRING:
42
474k
        return ((void*) ptr == state->end);
43
44
0
    case SRE_AT_BOUNDARY:
45
0
        thatp = ((void*) ptr > state->beginning) ?
46
0
            SRE_IS_WORD((int) ptr[-1]) : 0;
47
0
        thisp = ((void*) ptr < state->end) ?
48
0
            SRE_IS_WORD((int) ptr[0]) : 0;
49
0
        return thisp != thatp;
50
51
0
    case SRE_AT_NON_BOUNDARY:
52
0
        thatp = ((void*) ptr > state->beginning) ?
53
0
            SRE_IS_WORD((int) ptr[-1]) : 0;
54
0
        thisp = ((void*) ptr < state->end) ?
55
0
            SRE_IS_WORD((int) ptr[0]) : 0;
56
0
        return thisp == thatp;
57
58
0
    case SRE_AT_LOC_BOUNDARY:
59
0
        thatp = ((void*) ptr > state->beginning) ?
60
0
            SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
61
0
        thisp = ((void*) ptr < state->end) ?
62
0
            SRE_LOC_IS_WORD((int) ptr[0]) : 0;
63
0
        return thisp != thatp;
64
65
0
    case SRE_AT_LOC_NON_BOUNDARY:
66
0
        thatp = ((void*) ptr > state->beginning) ?
67
0
            SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
68
0
        thisp = ((void*) ptr < state->end) ?
69
0
            SRE_LOC_IS_WORD((int) ptr[0]) : 0;
70
0
        return thisp == thatp;
71
72
0
    case SRE_AT_UNI_BOUNDARY:
73
0
        thatp = ((void*) ptr > state->beginning) ?
74
0
            SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
75
0
        thisp = ((void*) ptr < state->end) ?
76
0
            SRE_UNI_IS_WORD((int) ptr[0]) : 0;
77
0
        return thisp != thatp;
78
79
0
    case SRE_AT_UNI_NON_BOUNDARY:
80
0
        thatp = ((void*) ptr > state->beginning) ?
81
0
            SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
82
0
        thisp = ((void*) ptr < state->end) ?
83
0
            SRE_UNI_IS_WORD((int) ptr[0]) : 0;
84
0
        return thisp == thatp;
85
86
539k
    }
87
88
0
    return 0;
89
539k
}
90
91
LOCAL(int)
92
SRE(charset)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch)
93
1.16G
{
94
    /* check if character is a member of the given set */
95
96
1.16G
    int ok = 1;
97
98
2.78G
    for (;;) {
99
2.78G
        switch (*set++) {
100
101
809M
        case SRE_OP_FAILURE:
102
809M
            return !ok;
103
104
1.24G
        case SRE_OP_LITERAL:
105
            /* <LITERAL> <code> */
106
1.24G
            if (ch == set[0])
107
25.9M
                return ok;
108
1.22G
            set++;
109
1.22G
            break;
110
111
13.6M
        case SRE_OP_CATEGORY:
112
            /* <CATEGORY> <code> */
113
13.6M
            if (sre_category(set[0], (int) ch))
114
8.96M
                return ok;
115
4.73M
            set++;
116
4.73M
            break;
117
118
143M
        case SRE_OP_CHARSET:
119
            /* <CHARSET> <bitmap> */
120
143M
            if (ch < 256 &&
121
137M
                (set[ch/SRE_CODE_BITS] & (1u << (ch & (SRE_CODE_BITS-1)))))
122
23.9M
                return ok;
123
119M
            set += 256/SRE_CODE_BITS;
124
119M
            break;
125
126
444M
        case SRE_OP_RANGE:
127
            /* <RANGE> <lower> <upper> */
128
444M
            if (set[0] <= ch && ch <= set[1])
129
297M
                return ok;
130
146M
            set += 2;
131
146M
            break;
132
133
0
        case SRE_OP_RANGE_UNI_IGNORE:
134
            /* <RANGE_UNI_IGNORE> <lower> <upper> */
135
0
        {
136
0
            SRE_CODE uch;
137
            /* ch is already lower cased */
138
0
            if (set[0] <= ch && ch <= set[1])
139
0
                return ok;
140
0
            uch = sre_upper_unicode(ch);
141
0
            if (set[0] <= uch && uch <= set[1])
142
0
                return ok;
143
0
            set += 2;
144
0
            break;
145
0
        }
146
147
122M
        case SRE_OP_NEGATE:
148
122M
            ok = !ok;
149
122M
            break;
150
151
0
        case SRE_OP_BIGCHARSET:
152
            /* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */
153
0
        {
154
0
            Py_ssize_t count, block;
155
0
            count = *(set++);
156
157
0
            if (ch < 0x10000u)
158
0
                block = ((unsigned char*)set)[ch >> 8];
159
0
            else
160
0
                block = -1;
161
0
            set += 256/sizeof(SRE_CODE);
162
0
            if (block >=0 &&
163
0
                (set[(block * 256 + (ch & 255))/SRE_CODE_BITS] &
164
0
                    (1u << (ch & (SRE_CODE_BITS-1)))))
165
0
                return ok;
166
0
            set += count * (256/SRE_CODE_BITS);
167
0
            break;
168
0
        }
169
170
0
        default:
171
            /* internal error -- there's not much we can do about it
172
               here, so let's just pretend it didn't match... */
173
0
            return 0;
174
2.78G
        }
175
2.78G
    }
176
1.16G
}
sre.c:sre_ucs1_charset
Line
Count
Source
93
413M
{
94
    /* check if character is a member of the given set */
95
96
413M
    int ok = 1;
97
98
828M
    for (;;) {
99
828M
        switch (*set++) {
100
101
212M
        case SRE_OP_FAILURE:
102
212M
            return !ok;
103
104
263M
        case SRE_OP_LITERAL:
105
            /* <LITERAL> <code> */
106
263M
            if (ch == set[0])
107
21.4M
                return ok;
108
242M
            set++;
109
242M
            break;
110
111
9.84M
        case SRE_OP_CATEGORY:
112
            /* <CATEGORY> <code> */
113
9.84M
            if (sre_category(set[0], (int) ch))
114
6.79M
                return ok;
115
3.05M
            set++;
116
3.05M
            break;
117
118
58.2M
        case SRE_OP_CHARSET:
119
            /* <CHARSET> <bitmap> */
120
58.2M
            if (ch < 256 &&
121
58.2M
                (set[ch/SRE_CODE_BITS] & (1u << (ch & (SRE_CODE_BITS-1)))))
122
10.7M
                return ok;
123
47.4M
            set += 256/SRE_CODE_BITS;
124
47.4M
            break;
125
126
236M
        case SRE_OP_RANGE:
127
            /* <RANGE> <lower> <upper> */
128
236M
            if (set[0] <= ch && ch <= set[1])
129
161M
                return ok;
130
74.7M
            set += 2;
131
74.7M
            break;
132
133
0
        case SRE_OP_RANGE_UNI_IGNORE:
134
            /* <RANGE_UNI_IGNORE> <lower> <upper> */
135
0
        {
136
0
            SRE_CODE uch;
137
            /* ch is already lower cased */
138
0
            if (set[0] <= ch && ch <= set[1])
139
0
                return ok;
140
0
            uch = sre_upper_unicode(ch);
141
0
            if (set[0] <= uch && uch <= set[1])
142
0
                return ok;
143
0
            set += 2;
144
0
            break;
145
0
        }
146
147
47.8M
        case SRE_OP_NEGATE:
148
47.8M
            ok = !ok;
149
47.8M
            break;
150
151
0
        case SRE_OP_BIGCHARSET:
152
            /* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */
153
0
        {
154
0
            Py_ssize_t count, block;
155
0
            count = *(set++);
156
157
0
            if (ch < 0x10000u)
158
0
                block = ((unsigned char*)set)[ch >> 8];
159
0
            else
160
0
                block = -1;
161
0
            set += 256/sizeof(SRE_CODE);
162
0
            if (block >=0 &&
163
0
                (set[(block * 256 + (ch & 255))/SRE_CODE_BITS] &
164
0
                    (1u << (ch & (SRE_CODE_BITS-1)))))
165
0
                return ok;
166
0
            set += count * (256/SRE_CODE_BITS);
167
0
            break;
168
0
        }
169
170
0
        default:
171
            /* internal error -- there's not much we can do about it
172
               here, so let's just pretend it didn't match... */
173
0
            return 0;
174
828M
        }
175
828M
    }
176
413M
}
sre.c:sre_ucs2_charset
Line
Count
Source
93
574M
{
94
    /* check if character is a member of the given set */
95
96
574M
    int ok = 1;
97
98
1.49G
    for (;;) {
99
1.49G
        switch (*set++) {
100
101
456M
        case SRE_OP_FAILURE:
102
456M
            return !ok;
103
104
739M
        case SRE_OP_LITERAL:
105
            /* <LITERAL> <code> */
106
739M
            if (ch == set[0])
107
3.85M
                return ok;
108
735M
            set++;
109
735M
            break;
110
111
3.61M
        case SRE_OP_CATEGORY:
112
            /* <CATEGORY> <code> */
113
3.61M
            if (sre_category(set[0], (int) ch))
114
2.14M
                return ok;
115
1.46M
            set++;
116
1.46M
            break;
117
118
64.2M
        case SRE_OP_CHARSET:
119
            /* <CHARSET> <bitmap> */
120
64.2M
            if (ch < 256 &&
121
59.1M
                (set[ch/SRE_CODE_BITS] & (1u << (ch & (SRE_CODE_BITS-1)))))
122
7.84M
                return ok;
123
56.3M
            set += 256/SRE_CODE_BITS;
124
56.3M
            break;
125
126
169M
        case SRE_OP_RANGE:
127
            /* <RANGE> <lower> <upper> */
128
169M
            if (set[0] <= ch && ch <= set[1])
129
104M
                return ok;
130
65.0M
            set += 2;
131
65.0M
            break;
132
133
0
        case SRE_OP_RANGE_UNI_IGNORE:
134
            /* <RANGE_UNI_IGNORE> <lower> <upper> */
135
0
        {
136
0
            SRE_CODE uch;
137
            /* ch is already lower cased */
138
0
            if (set[0] <= ch && ch <= set[1])
139
0
                return ok;
140
0
            uch = sre_upper_unicode(ch);
141
0
            if (set[0] <= uch && uch <= set[1])
142
0
                return ok;
143
0
            set += 2;
144
0
            break;
145
0
        }
146
147
58.2M
        case SRE_OP_NEGATE:
148
58.2M
            ok = !ok;
149
58.2M
            break;
150
151
0
        case SRE_OP_BIGCHARSET:
152
            /* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */
153
0
        {
154
0
            Py_ssize_t count, block;
155
0
            count = *(set++);
156
157
0
            if (ch < 0x10000u)
158
0
                block = ((unsigned char*)set)[ch >> 8];
159
0
            else
160
0
                block = -1;
161
0
            set += 256/sizeof(SRE_CODE);
162
0
            if (block >=0 &&
163
0
                (set[(block * 256 + (ch & 255))/SRE_CODE_BITS] &
164
0
                    (1u << (ch & (SRE_CODE_BITS-1)))))
165
0
                return ok;
166
0
            set += count * (256/SRE_CODE_BITS);
167
0
            break;
168
0
        }
169
170
0
        default:
171
            /* internal error -- there's not much we can do about it
172
               here, so let's just pretend it didn't match... */
173
0
            return 0;
174
1.49G
        }
175
1.49G
    }
176
574M
}
sre.c:sre_ucs4_charset
Line
Count
Source
93
177M
{
94
    /* check if character is a member of the given set */
95
96
177M
    int ok = 1;
97
98
459M
    for (;;) {
99
459M
        switch (*set++) {
100
101
139M
        case SRE_OP_FAILURE:
102
139M
            return !ok;
103
104
243M
        case SRE_OP_LITERAL:
105
            /* <LITERAL> <code> */
106
243M
            if (ch == set[0])
107
674k
                return ok;
108
242M
            set++;
109
242M
            break;
110
111
232k
        case SRE_OP_CATEGORY:
112
            /* <CATEGORY> <code> */
113
232k
            if (sre_category(set[0], (int) ch))
114
16.5k
                return ok;
115
215k
            set++;
116
215k
            break;
117
118
20.9M
        case SRE_OP_CHARSET:
119
            /* <CHARSET> <bitmap> */
120
20.9M
            if (ch < 256 &&
121
20.2M
                (set[ch/SRE_CODE_BITS] & (1u << (ch & (SRE_CODE_BITS-1)))))
122
5.32M
                return ok;
123
15.6M
            set += 256/SRE_CODE_BITS;
124
15.6M
            break;
125
126
39.3M
        case SRE_OP_RANGE:
127
            /* <RANGE> <lower> <upper> */
128
39.3M
            if (set[0] <= ch && ch <= set[1])
129
32.1M
                return ok;
130
7.16M
            set += 2;
131
7.16M
            break;
132
133
0
        case SRE_OP_RANGE_UNI_IGNORE:
134
            /* <RANGE_UNI_IGNORE> <lower> <upper> */
135
0
        {
136
0
            SRE_CODE uch;
137
            /* ch is already lower cased */
138
0
            if (set[0] <= ch && ch <= set[1])
139
0
                return ok;
140
0
            uch = sre_upper_unicode(ch);
141
0
            if (set[0] <= uch && uch <= set[1])
142
0
                return ok;
143
0
            set += 2;
144
0
            break;
145
0
        }
146
147
15.9M
        case SRE_OP_NEGATE:
148
15.9M
            ok = !ok;
149
15.9M
            break;
150
151
0
        case SRE_OP_BIGCHARSET:
152
            /* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */
153
0
        {
154
0
            Py_ssize_t count, block;
155
0
            count = *(set++);
156
157
0
            if (ch < 0x10000u)
158
0
                block = ((unsigned char*)set)[ch >> 8];
159
0
            else
160
0
                block = -1;
161
0
            set += 256/sizeof(SRE_CODE);
162
0
            if (block >=0 &&
163
0
                (set[(block * 256 + (ch & 255))/SRE_CODE_BITS] &
164
0
                    (1u << (ch & (SRE_CODE_BITS-1)))))
165
0
                return ok;
166
0
            set += count * (256/SRE_CODE_BITS);
167
0
            break;
168
0
        }
169
170
0
        default:
171
            /* internal error -- there's not much we can do about it
172
               here, so let's just pretend it didn't match... */
173
0
            return 0;
174
459M
        }
175
459M
    }
176
177M
}
177
178
/* Like SRE(charset), but matches both locale cases of ch against every set
179
   member.  Testing the whole set once per case would complement it before
180
   closing it under case instead of after, so that [^bc] matched b'B'.
181
   BIGCHARSET and RANGE_UNI_IGNORE are not handled: they never occur in a
182
   set of a bytes pattern. */
183
LOCAL(int)
184
SRE(charset_loc_ignore)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch)
185
0
{
186
0
    SRE_CODE lo, up;
187
0
    int ok = 1;
188
189
0
    lo = sre_lower_locale(ch);
190
0
    up = sre_upper_locale(ch);
191
0
    if (up == lo)
192
0
        return SRE(charset)(state, set, lo);
193
194
0
    for (;;) {
195
0
        switch (*set++) {
196
197
0
        case SRE_OP_FAILURE:
198
0
            return !ok;
199
200
0
        case SRE_OP_LITERAL:
201
            /* <LITERAL> <code> */
202
0
            if (lo == set[0] || up == set[0])
203
0
                return ok;
204
0
            set++;
205
0
            break;
206
207
0
        case SRE_OP_CATEGORY:
208
            /* <CATEGORY> <code> */
209
0
            if (sre_category(set[0], (int) lo) ||
210
0
                sre_category(set[0], (int) up))
211
0
                return ok;
212
0
            set++;
213
0
            break;
214
215
0
        case SRE_OP_CHARSET:
216
            /* <CHARSET> <bitmap> */
217
0
            if ((lo < 256 && (set[lo/SRE_CODE_BITS]
218
0
                              & (1u << (lo & (SRE_CODE_BITS-1))))) ||
219
0
                (up < 256 && (set[up/SRE_CODE_BITS]
220
0
                              & (1u << (up & (SRE_CODE_BITS-1))))))
221
0
                return ok;
222
0
            set += 256/SRE_CODE_BITS;
223
0
            break;
224
225
0
        case SRE_OP_RANGE:
226
            /* <RANGE> <lower> <upper> */
227
0
            if ((set[0] <= lo && lo <= set[1]) ||
228
0
                (set[0] <= up && up <= set[1]))
229
0
                return ok;
230
0
            set += 2;
231
0
            break;
232
233
0
        case SRE_OP_NEGATE:
234
0
            ok = !ok;
235
0
            break;
236
237
0
        default:
238
            /* internal error -- there's not much we can do about it
239
               here, so let's just pretend it didn't match... */
240
0
            return 0;
241
0
        }
242
0
    }
243
0
}
Unexecuted instantiation: sre.c:sre_ucs1_charset_loc_ignore
Unexecuted instantiation: sre.c:sre_ucs2_charset_loc_ignore
Unexecuted instantiation: sre.c:sre_ucs4_charset_loc_ignore
244
245
LOCAL(Py_ssize_t) SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel);
246
247
LOCAL(Py_ssize_t)
248
SRE(count)(SRE_STATE* state, const SRE_CODE* pattern, Py_ssize_t maxcount)
249
314M
{
250
314M
    SRE_CODE chr;
251
314M
    SRE_CODE arg;
252
314M
    SRE_CHAR c;
253
314M
    const SRE_CHAR* ptr = (const SRE_CHAR *)state->ptr;
254
314M
    const SRE_CHAR* end = (const SRE_CHAR *)state->end;
255
314M
    Py_ssize_t i;
256
314M
    INIT_TRACE(state);
257
258
    /* adjust end */
259
314M
    if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT)
260
56.5M
        end = ptr + maxcount;
261
262
314M
    switch (pattern[0]) {
263
264
228M
    case SRE_OP_IN:
265
        /* repeated set */
266
228M
        TRACE(("|%p|%p|COUNT IN\n", pattern, ptr));
267
424M
        while (ptr < end && SRE(charset)(state, pattern + 2, *ptr))
268
196M
            ptr++;
269
228M
        break;
270
271
6.61M
    case SRE_OP_ANY:
272
        /* repeated dot wildcard. */
273
6.61M
        TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr));
274
48.1M
        while (ptr < end && !SRE_IS_LINEBREAK(*ptr))
275
41.5M
            ptr++;
276
6.61M
        break;
277
278
0
    case SRE_OP_ANY_ALL:
279
        /* repeated dot wildcard.  skip to the end of the target
280
           string, and backtrack from there */
281
0
        TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr));
282
0
        ptr = end;
283
0
        break;
284
285
61.3M
    case SRE_OP_LITERAL:
286
        /* repeated literal */
287
61.3M
        chr = pattern[1];
288
61.3M
        TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr));
289
61.3M
        c = (SRE_CHAR) chr;
290
#if SIZEOF_SRE_CHAR < 4
291
45.6M
        if ((SRE_CODE) c != chr)
292
0
            ; /* literal can't match: doesn't fit in char width */
293
45.6M
        else
294
45.6M
#endif
295
61.6M
        while (ptr < end && *ptr == c)
296
319k
            ptr++;
297
61.3M
        break;
298
299
0
    case SRE_OP_LITERAL_IGNORE:
300
        /* repeated literal */
301
0
        chr = pattern[1];
302
0
        TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr));
303
0
        while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) == chr)
304
0
            ptr++;
305
0
        break;
306
307
0
    case SRE_OP_LITERAL_UNI_IGNORE:
308
        /* repeated literal */
309
0
        chr = pattern[1];
310
0
        TRACE(("|%p|%p|COUNT LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr));
311
0
        while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) == chr)
312
0
            ptr++;
313
0
        break;
314
315
0
    case SRE_OP_LITERAL_LOC_IGNORE:
316
        /* repeated literal */
317
0
        chr = pattern[1];
318
0
        TRACE(("|%p|%p|COUNT LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr));
319
0
        while (ptr < end && char_loc_ignore(chr, *ptr))
320
0
            ptr++;
321
0
        break;
322
323
8
    case SRE_OP_NOT_LITERAL:
324
        /* repeated non-literal */
325
8
        chr = pattern[1];
326
8
        TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr));
327
8
        c = (SRE_CHAR) chr;
328
#if SIZEOF_SRE_CHAR < 4
329
8
        if ((SRE_CODE) c != chr)
330
0
            ptr = end; /* literal can't match: doesn't fit in char width */
331
8
        else
332
8
#endif
333
420
        while (ptr < end && *ptr != c)
334
412
            ptr++;
335
8
        break;
336
337
0
    case SRE_OP_NOT_LITERAL_IGNORE:
338
        /* repeated non-literal */
339
0
        chr = pattern[1];
340
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr));
341
0
        while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) != chr)
342
0
            ptr++;
343
0
        break;
344
345
0
    case SRE_OP_NOT_LITERAL_UNI_IGNORE:
346
        /* repeated non-literal */
347
0
        chr = pattern[1];
348
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr));
349
0
        while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) != chr)
350
0
            ptr++;
351
0
        break;
352
353
0
    case SRE_OP_NOT_LITERAL_LOC_IGNORE:
354
        /* repeated non-literal */
355
0
        chr = pattern[1];
356
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr));
357
0
        while (ptr < end && !char_loc_ignore(chr, *ptr))
358
0
            ptr++;
359
0
        break;
360
361
18.4M
    case SRE_OP_CATEGORY:
362
18.4M
        arg = pattern[1];
363
18.4M
        TRACE(("|%p|%p|COUNT CATEGORY %d\n", pattern, ptr, arg));
364
27.8M
        while (ptr < end && sre_category(arg, *ptr))
365
9.39M
            ptr++;
366
18.4M
        break;
367
368
0
    default:
369
        /* repeated single character pattern */
370
0
        TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr));
371
0
        while ((SRE_CHAR*) state->ptr < end) {
372
0
            i = SRE(match)(state, pattern, 0);
373
0
            if (i < 0)
374
0
                return i;
375
0
            if (!i)
376
0
                break;
377
0
        }
378
0
        TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr,
379
0
               (SRE_CHAR*) state->ptr - ptr));
380
0
        return (SRE_CHAR*) state->ptr - ptr;
381
314M
    }
382
383
314M
    TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr,
384
314M
           ptr - (SRE_CHAR*) state->ptr));
385
314M
    return ptr - (SRE_CHAR*) state->ptr;
386
314M
}
sre.c:sre_ucs1_count
Line
Count
Source
249
115M
{
250
115M
    SRE_CODE chr;
251
115M
    SRE_CODE arg;
252
115M
    SRE_CHAR c;
253
115M
    const SRE_CHAR* ptr = (const SRE_CHAR *)state->ptr;
254
115M
    const SRE_CHAR* end = (const SRE_CHAR *)state->end;
255
115M
    Py_ssize_t i;
256
115M
    INIT_TRACE(state);
257
258
    /* adjust end */
259
115M
    if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT)
260
25.2M
        end = ptr + maxcount;
261
262
115M
    switch (pattern[0]) {
263
264
62.8M
    case SRE_OP_IN:
265
        /* repeated set */
266
62.8M
        TRACE(("|%p|%p|COUNT IN\n", pattern, ptr));
267
168M
        while (ptr < end && SRE(charset)(state, pattern + 2, *ptr))
268
105M
            ptr++;
269
62.8M
        break;
270
271
6.29M
    case SRE_OP_ANY:
272
        /* repeated dot wildcard. */
273
6.29M
        TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr));
274
16.9M
        while (ptr < end && !SRE_IS_LINEBREAK(*ptr))
275
10.6M
            ptr++;
276
6.29M
        break;
277
278
0
    case SRE_OP_ANY_ALL:
279
        /* repeated dot wildcard.  skip to the end of the target
280
           string, and backtrack from there */
281
0
        TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr));
282
0
        ptr = end;
283
0
        break;
284
285
30.1M
    case SRE_OP_LITERAL:
286
        /* repeated literal */
287
30.1M
        chr = pattern[1];
288
30.1M
        TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr));
289
30.1M
        c = (SRE_CHAR) chr;
290
30.1M
#if SIZEOF_SRE_CHAR < 4
291
30.1M
        if ((SRE_CODE) c != chr)
292
0
            ; /* literal can't match: doesn't fit in char width */
293
30.1M
        else
294
30.1M
#endif
295
30.4M
        while (ptr < end && *ptr == c)
296
265k
            ptr++;
297
30.1M
        break;
298
299
0
    case SRE_OP_LITERAL_IGNORE:
300
        /* repeated literal */
301
0
        chr = pattern[1];
302
0
        TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr));
303
0
        while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) == chr)
304
0
            ptr++;
305
0
        break;
306
307
0
    case SRE_OP_LITERAL_UNI_IGNORE:
308
        /* repeated literal */
309
0
        chr = pattern[1];
310
0
        TRACE(("|%p|%p|COUNT LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr));
311
0
        while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) == chr)
312
0
            ptr++;
313
0
        break;
314
315
0
    case SRE_OP_LITERAL_LOC_IGNORE:
316
        /* repeated literal */
317
0
        chr = pattern[1];
318
0
        TRACE(("|%p|%p|COUNT LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr));
319
0
        while (ptr < end && char_loc_ignore(chr, *ptr))
320
0
            ptr++;
321
0
        break;
322
323
8
    case SRE_OP_NOT_LITERAL:
324
        /* repeated non-literal */
325
8
        chr = pattern[1];
326
8
        TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr));
327
8
        c = (SRE_CHAR) chr;
328
8
#if SIZEOF_SRE_CHAR < 4
329
8
        if ((SRE_CODE) c != chr)
330
0
            ptr = end; /* literal can't match: doesn't fit in char width */
331
8
        else
332
8
#endif
333
420
        while (ptr < end && *ptr != c)
334
412
            ptr++;
335
8
        break;
336
337
0
    case SRE_OP_NOT_LITERAL_IGNORE:
338
        /* repeated non-literal */
339
0
        chr = pattern[1];
340
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr));
341
0
        while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) != chr)
342
0
            ptr++;
343
0
        break;
344
345
0
    case SRE_OP_NOT_LITERAL_UNI_IGNORE:
346
        /* repeated non-literal */
347
0
        chr = pattern[1];
348
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr));
349
0
        while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) != chr)
350
0
            ptr++;
351
0
        break;
352
353
0
    case SRE_OP_NOT_LITERAL_LOC_IGNORE:
354
        /* repeated non-literal */
355
0
        chr = pattern[1];
356
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr));
357
0
        while (ptr < end && !char_loc_ignore(chr, *ptr))
358
0
            ptr++;
359
0
        break;
360
361
16.1M
    case SRE_OP_CATEGORY:
362
16.1M
        arg = pattern[1];
363
16.1M
        TRACE(("|%p|%p|COUNT CATEGORY %d\n", pattern, ptr, arg));
364
24.5M
        while (ptr < end && sre_category(arg, *ptr))
365
8.39M
            ptr++;
366
16.1M
        break;
367
368
0
    default:
369
        /* repeated single character pattern */
370
0
        TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr));
371
0
        while ((SRE_CHAR*) state->ptr < end) {
372
0
            i = SRE(match)(state, pattern, 0);
373
0
            if (i < 0)
374
0
                return i;
375
0
            if (!i)
376
0
                break;
377
0
        }
378
0
        TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr,
379
0
               (SRE_CHAR*) state->ptr - ptr));
380
0
        return (SRE_CHAR*) state->ptr - ptr;
381
115M
    }
382
383
115M
    TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr,
384
115M
           ptr - (SRE_CHAR*) state->ptr));
385
115M
    return ptr - (SRE_CHAR*) state->ptr;
386
115M
}
sre.c:sre_ucs2_count
Line
Count
Source
249
152M
{
250
152M
    SRE_CODE chr;
251
152M
    SRE_CODE arg;
252
152M
    SRE_CHAR c;
253
152M
    const SRE_CHAR* ptr = (const SRE_CHAR *)state->ptr;
254
152M
    const SRE_CHAR* end = (const SRE_CHAR *)state->end;
255
152M
    Py_ssize_t i;
256
152M
    INIT_TRACE(state);
257
258
    /* adjust end */
259
152M
    if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT)
260
15.4M
        end = ptr + maxcount;
261
262
152M
    switch (pattern[0]) {
263
264
134M
    case SRE_OP_IN:
265
        /* repeated set */
266
134M
        TRACE(("|%p|%p|COUNT IN\n", pattern, ptr));
267
202M
        while (ptr < end && SRE(charset)(state, pattern + 2, *ptr))
268
67.4M
            ptr++;
269
134M
        break;
270
271
305k
    case SRE_OP_ANY:
272
        /* repeated dot wildcard. */
273
305k
        TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr));
274
18.2M
        while (ptr < end && !SRE_IS_LINEBREAK(*ptr))
275
17.9M
            ptr++;
276
305k
        break;
277
278
0
    case SRE_OP_ANY_ALL:
279
        /* repeated dot wildcard.  skip to the end of the target
280
           string, and backtrack from there */
281
0
        TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr));
282
0
        ptr = end;
283
0
        break;
284
285
15.4M
    case SRE_OP_LITERAL:
286
        /* repeated literal */
287
15.4M
        chr = pattern[1];
288
15.4M
        TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr));
289
15.4M
        c = (SRE_CHAR) chr;
290
15.4M
#if SIZEOF_SRE_CHAR < 4
291
15.4M
        if ((SRE_CODE) c != chr)
292
0
            ; /* literal can't match: doesn't fit in char width */
293
15.4M
        else
294
15.4M
#endif
295
15.5M
        while (ptr < end && *ptr == c)
296
53.0k
            ptr++;
297
15.4M
        break;
298
299
0
    case SRE_OP_LITERAL_IGNORE:
300
        /* repeated literal */
301
0
        chr = pattern[1];
302
0
        TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr));
303
0
        while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) == chr)
304
0
            ptr++;
305
0
        break;
306
307
0
    case SRE_OP_LITERAL_UNI_IGNORE:
308
        /* repeated literal */
309
0
        chr = pattern[1];
310
0
        TRACE(("|%p|%p|COUNT LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr));
311
0
        while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) == chr)
312
0
            ptr++;
313
0
        break;
314
315
0
    case SRE_OP_LITERAL_LOC_IGNORE:
316
        /* repeated literal */
317
0
        chr = pattern[1];
318
0
        TRACE(("|%p|%p|COUNT LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr));
319
0
        while (ptr < end && char_loc_ignore(chr, *ptr))
320
0
            ptr++;
321
0
        break;
322
323
0
    case SRE_OP_NOT_LITERAL:
324
        /* repeated non-literal */
325
0
        chr = pattern[1];
326
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr));
327
0
        c = (SRE_CHAR) chr;
328
0
#if SIZEOF_SRE_CHAR < 4
329
0
        if ((SRE_CODE) c != chr)
330
0
            ptr = end; /* literal can't match: doesn't fit in char width */
331
0
        else
332
0
#endif
333
0
        while (ptr < end && *ptr != c)
334
0
            ptr++;
335
0
        break;
336
337
0
    case SRE_OP_NOT_LITERAL_IGNORE:
338
        /* repeated non-literal */
339
0
        chr = pattern[1];
340
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr));
341
0
        while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) != chr)
342
0
            ptr++;
343
0
        break;
344
345
0
    case SRE_OP_NOT_LITERAL_UNI_IGNORE:
346
        /* repeated non-literal */
347
0
        chr = pattern[1];
348
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr));
349
0
        while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) != chr)
350
0
            ptr++;
351
0
        break;
352
353
0
    case SRE_OP_NOT_LITERAL_LOC_IGNORE:
354
        /* repeated non-literal */
355
0
        chr = pattern[1];
356
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr));
357
0
        while (ptr < end && !char_loc_ignore(chr, *ptr))
358
0
            ptr++;
359
0
        break;
360
361
2.27M
    case SRE_OP_CATEGORY:
362
2.27M
        arg = pattern[1];
363
2.27M
        TRACE(("|%p|%p|COUNT CATEGORY %d\n", pattern, ptr, arg));
364
2.85M
        while (ptr < end && sre_category(arg, *ptr))
365
579k
            ptr++;
366
2.27M
        break;
367
368
0
    default:
369
        /* repeated single character pattern */
370
0
        TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr));
371
0
        while ((SRE_CHAR*) state->ptr < end) {
372
0
            i = SRE(match)(state, pattern, 0);
373
0
            if (i < 0)
374
0
                return i;
375
0
            if (!i)
376
0
                break;
377
0
        }
378
0
        TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr,
379
0
               (SRE_CHAR*) state->ptr - ptr));
380
0
        return (SRE_CHAR*) state->ptr - ptr;
381
152M
    }
382
383
152M
    TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr,
384
152M
           ptr - (SRE_CHAR*) state->ptr));
385
152M
    return ptr - (SRE_CHAR*) state->ptr;
386
152M
}
sre.c:sre_ucs4_count
Line
Count
Source
249
46.2M
{
250
46.2M
    SRE_CODE chr;
251
46.2M
    SRE_CODE arg;
252
46.2M
    SRE_CHAR c;
253
46.2M
    const SRE_CHAR* ptr = (const SRE_CHAR *)state->ptr;
254
46.2M
    const SRE_CHAR* end = (const SRE_CHAR *)state->end;
255
46.2M
    Py_ssize_t i;
256
46.2M
    INIT_TRACE(state);
257
258
    /* adjust end */
259
46.2M
    if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT)
260
15.8M
        end = ptr + maxcount;
261
262
46.2M
    switch (pattern[0]) {
263
264
30.4M
    case SRE_OP_IN:
265
        /* repeated set */
266
30.4M
        TRACE(("|%p|%p|COUNT IN\n", pattern, ptr));
267
54.2M
        while (ptr < end && SRE(charset)(state, pattern + 2, *ptr))
268
23.8M
            ptr++;
269
30.4M
        break;
270
271
12.8k
    case SRE_OP_ANY:
272
        /* repeated dot wildcard. */
273
12.8k
        TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr));
274
13.0M
        while (ptr < end && !SRE_IS_LINEBREAK(*ptr))
275
12.9M
            ptr++;
276
12.8k
        break;
277
278
0
    case SRE_OP_ANY_ALL:
279
        /* repeated dot wildcard.  skip to the end of the target
280
           string, and backtrack from there */
281
0
        TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr));
282
0
        ptr = end;
283
0
        break;
284
285
15.7M
    case SRE_OP_LITERAL:
286
        /* repeated literal */
287
15.7M
        chr = pattern[1];
288
15.7M
        TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr));
289
15.7M
        c = (SRE_CHAR) chr;
290
#if SIZEOF_SRE_CHAR < 4
291
        if ((SRE_CODE) c != chr)
292
            ; /* literal can't match: doesn't fit in char width */
293
        else
294
#endif
295
15.7M
        while (ptr < end && *ptr == c)
296
897
            ptr++;
297
15.7M
        break;
298
299
0
    case SRE_OP_LITERAL_IGNORE:
300
        /* repeated literal */
301
0
        chr = pattern[1];
302
0
        TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr));
303
0
        while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) == chr)
304
0
            ptr++;
305
0
        break;
306
307
0
    case SRE_OP_LITERAL_UNI_IGNORE:
308
        /* repeated literal */
309
0
        chr = pattern[1];
310
0
        TRACE(("|%p|%p|COUNT LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr));
311
0
        while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) == chr)
312
0
            ptr++;
313
0
        break;
314
315
0
    case SRE_OP_LITERAL_LOC_IGNORE:
316
        /* repeated literal */
317
0
        chr = pattern[1];
318
0
        TRACE(("|%p|%p|COUNT LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr));
319
0
        while (ptr < end && char_loc_ignore(chr, *ptr))
320
0
            ptr++;
321
0
        break;
322
323
0
    case SRE_OP_NOT_LITERAL:
324
        /* repeated non-literal */
325
0
        chr = pattern[1];
326
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr));
327
0
        c = (SRE_CHAR) chr;
328
#if SIZEOF_SRE_CHAR < 4
329
        if ((SRE_CODE) c != chr)
330
            ptr = end; /* literal can't match: doesn't fit in char width */
331
        else
332
#endif
333
0
        while (ptr < end && *ptr != c)
334
0
            ptr++;
335
0
        break;
336
337
0
    case SRE_OP_NOT_LITERAL_IGNORE:
338
        /* repeated non-literal */
339
0
        chr = pattern[1];
340
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr));
341
0
        while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) != chr)
342
0
            ptr++;
343
0
        break;
344
345
0
    case SRE_OP_NOT_LITERAL_UNI_IGNORE:
346
        /* repeated non-literal */
347
0
        chr = pattern[1];
348
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr));
349
0
        while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) != chr)
350
0
            ptr++;
351
0
        break;
352
353
0
    case SRE_OP_NOT_LITERAL_LOC_IGNORE:
354
        /* repeated non-literal */
355
0
        chr = pattern[1];
356
0
        TRACE(("|%p|%p|COUNT NOT_LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr));
357
0
        while (ptr < end && !char_loc_ignore(chr, *ptr))
358
0
            ptr++;
359
0
        break;
360
361
24.4k
    case SRE_OP_CATEGORY:
362
24.4k
        arg = pattern[1];
363
24.4k
        TRACE(("|%p|%p|COUNT CATEGORY %d\n", pattern, ptr, arg));
364
449k
        while (ptr < end && sre_category(arg, *ptr))
365
424k
            ptr++;
366
24.4k
        break;
367
368
0
    default:
369
        /* repeated single character pattern */
370
0
        TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr));
371
0
        while ((SRE_CHAR*) state->ptr < end) {
372
0
            i = SRE(match)(state, pattern, 0);
373
0
            if (i < 0)
374
0
                return i;
375
0
            if (!i)
376
0
                break;
377
0
        }
378
0
        TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr,
379
0
               (SRE_CHAR*) state->ptr - ptr));
380
0
        return (SRE_CHAR*) state->ptr - ptr;
381
46.2M
    }
382
383
46.2M
    TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr,
384
46.2M
           ptr - (SRE_CHAR*) state->ptr));
385
46.2M
    return ptr - (SRE_CHAR*) state->ptr;
386
46.2M
}
387
388
/* The macros below should be used to protect recursive SRE(match)()
389
 * calls that *failed* and do *not* return immediately (IOW, those
390
 * that will backtrack). Explaining:
391
 *
392
 * - Recursive SRE(match)() returned true: that's usually a success
393
 *   (besides atypical cases like ASSERT_NOT), therefore there's no
394
 *   reason to restore lastmark;
395
 *
396
 * - Recursive SRE(match)() returned false but the current SRE(match)()
397
 *   is returning to the caller: If the current SRE(match)() is the
398
 *   top function of the recursion, returning false will be a matching
399
 *   failure, and it doesn't matter where lastmark is pointing to.
400
 *   If it's *not* the top function, it will be a recursive SRE(match)()
401
 *   failure by itself, and the calling SRE(match)() will have to deal
402
 *   with the failure by the same rules explained here (it will restore
403
 *   lastmark by itself if necessary);
404
 *
405
 * - Recursive SRE(match)() returned false, and will continue the
406
 *   outside 'for' loop: must be protected when breaking, since the next
407
 *   OP could potentially depend on lastmark;
408
 *
409
 * - Recursive SRE(match)() returned false, and will be called again
410
 *   inside a local for/while loop: must be protected between each
411
 *   loop iteration, since the recursive SRE(match)() could do anything,
412
 *   and could potentially depend on lastmark.
413
 *
414
 * For more information, check the discussion at SF patch #712900.
415
 */
416
#define LASTMARK_SAVE()     \
417
505M
    do { \
418
505M
        ctx->lastmark = state->lastmark; \
419
505M
        ctx->lastindex = state->lastindex; \
420
505M
    } while (0)
421
#define LASTMARK_RESTORE()  \
422
245M
    do { \
423
245M
        state->lastmark = ctx->lastmark; \
424
245M
        state->lastindex = ctx->lastindex; \
425
245M
    } while (0)
426
427
#define LAST_PTR_PUSH()     \
428
217M
    do { \
429
217M
        TRACE(("push last_ptr: %zd", \
430
217M
                PTR_TO_INDEX(ctx->u.rep->last_ptr))); \
431
217M
        DATA_PUSH(&ctx->u.rep->last_ptr); \
432
217M
    } while (0)
433
#define LAST_PTR_POP()  \
434
217M
    do { \
435
217M
        DATA_POP(&ctx->u.rep->last_ptr); \
436
217M
        TRACE(("pop last_ptr: %zd", \
437
217M
                PTR_TO_INDEX(ctx->u.rep->last_ptr))); \
438
217M
    } while (0)
439
440
0
#define RETURN_ERROR(i) do { return i; } while(0)
441
512M
#define RETURN_FAILURE do { ret = 0; goto exit; } while(0)
442
538M
#define RETURN_SUCCESS do { ret = 1; goto exit; } while(0)
443
444
#define RETURN_ON_ERROR(i) \
445
712M
    do { if (i < 0) RETURN_ERROR(i); } while (0)
446
#define RETURN_ON_SUCCESS(i) \
447
40.2M
    do { RETURN_ON_ERROR(i); if (i > 0) RETURN_SUCCESS; } while (0)
448
#define RETURN_ON_FAILURE(i) \
449
12
    do { RETURN_ON_ERROR(i); if (i == 0) RETURN_FAILURE; } while (0)
450
451
1.05G
#define DATA_STACK_ALLOC(state, type, ptr) \
452
1.05G
do { \
453
1.05G
    alloc_pos = state->data_stack_base; \
454
1.05G
    TRACE(("allocating %s in %zd (%zd)\n", \
455
1.05G
           Py_STRINGIFY(type), alloc_pos, sizeof(type))); \
456
1.05G
    if (sizeof(type) > state->data_stack_size - alloc_pos) { \
457
175M
        int j = data_stack_grow(state, sizeof(type)); \
458
175M
        if (j < 0) return j; \
459
175M
        if (ctx_pos != -1) \
460
175M
            DATA_STACK_LOOKUP_AT(state, SRE(match_context), ctx, ctx_pos); \
461
175M
    } \
462
1.05G
    ptr = (type*)(state->data_stack+alloc_pos); \
463
1.05G
    state->data_stack_base += sizeof(type); \
464
1.05G
} while (0)
465
466
979M
#define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \
467
979M
do { \
468
979M
    TRACE(("looking up %s at %zd\n", Py_STRINGIFY(type), pos)); \
469
979M
    ptr = (type*)(state->data_stack+pos); \
470
979M
} while (0)
471
472
629M
#define DATA_STACK_PUSH(state, data, size) \
473
629M
do { \
474
629M
    TRACE(("copy data in %p to %zd (%zd)\n", \
475
629M
           data, state->data_stack_base, size)); \
476
629M
    if (size > state->data_stack_size - state->data_stack_base) { \
477
99.0k
        int j = data_stack_grow(state, size); \
478
99.0k
        if (j < 0) return j; \
479
99.0k
        if (ctx_pos != -1) \
480
99.0k
            DATA_STACK_LOOKUP_AT(state, SRE(match_context), ctx, ctx_pos); \
481
99.0k
    } \
482
629M
    memcpy(state->data_stack+state->data_stack_base, data, size); \
483
629M
    state->data_stack_base += size; \
484
629M
} while (0)
485
486
/* We add an explicit cast to memcpy here because MSVC has a bug when
487
   compiling C code where it believes that `const void**` cannot be
488
   safely casted to `void*`, see bpo-39943 for details. */
489
389M
#define DATA_STACK_POP(state, data, size, discard) \
490
389M
do { \
491
389M
    TRACE(("copy data to %p from %zd (%zd)\n", \
492
389M
           data, state->data_stack_base-size, size)); \
493
389M
    memcpy((void*) data, state->data_stack+state->data_stack_base-size, size); \
494
389M
    if (discard) \
495
389M
        state->data_stack_base -= size; \
496
389M
} while (0)
497
498
1.29G
#define DATA_STACK_POP_DISCARD(state, size) \
499
1.29G
do { \
500
1.29G
    TRACE(("discard data from %zd (%zd)\n", \
501
1.29G
           state->data_stack_base-size, size)); \
502
1.29G
    state->data_stack_base -= size; \
503
1.29G
} while(0)
504
505
#define DATA_PUSH(x) \
506
217M
    DATA_STACK_PUSH(state, (x), sizeof(*(x)))
507
#define DATA_POP(x) \
508
217M
    DATA_STACK_POP(state, (x), sizeof(*(x)), 1)
509
#define DATA_POP_DISCARD(x) \
510
1.05G
    DATA_STACK_POP_DISCARD(state, sizeof(*(x)))
511
#define DATA_ALLOC(t,p) \
512
1.05G
    DATA_STACK_ALLOC(state, t, p)
513
#define DATA_LOOKUP_AT(t,p,pos) \
514
975M
    DATA_STACK_LOOKUP_AT(state,t,p,pos)
515
516
#define PTR_TO_INDEX(ptr) \
517
    ((ptr) ? ((char*)(ptr) - (char*)state->beginning) / state->charsize : -1)
518
519
#if VERBOSE
520
#  define MARK_TRACE(label, lastmark) \
521
    do if (DO_TRACE) { \
522
        TRACE(("%s %d marks:", (label), (lastmark)+1)); \
523
        for (int j = 0; j <= (lastmark); j++) { \
524
            if (j && (j & 1) == 0) { \
525
                TRACE((" ")); \
526
            } \
527
            TRACE((" %zd", PTR_TO_INDEX(state->mark[j]))); \
528
        } \
529
        TRACE(("\n")); \
530
    } while (0)
531
#else
532
#  define MARK_TRACE(label, lastmark)
533
#endif
534
#define MARK_PUSH(lastmark) \
535
442M
    do if (lastmark >= 0) { \
536
411M
        MARK_TRACE("push", (lastmark)); \
537
411M
        size_t _marks_size = (lastmark+1) * sizeof(void*); \
538
411M
        DATA_STACK_PUSH(state, state->mark, _marks_size); \
539
442M
    } while (0)
540
#define MARK_POP(lastmark) \
541
171M
    do if (lastmark >= 0) { \
542
170M
        size_t _marks_size = (lastmark+1) * sizeof(void*); \
543
170M
        DATA_STACK_POP(state, state->mark, _marks_size, 1); \
544
170M
        MARK_TRACE("pop", (lastmark)); \
545
171M
    } while (0)
546
#define MARK_POP_KEEP(lastmark) \
547
1.88M
    do if (lastmark >= 0) { \
548
1.02M
        size_t _marks_size = (lastmark+1) * sizeof(void*); \
549
1.02M
        DATA_STACK_POP(state, state->mark, _marks_size, 0); \
550
1.02M
        MARK_TRACE("pop keep", (lastmark)); \
551
1.88M
    } while (0)
552
#define MARK_POP_DISCARD(lastmark) \
553
271M
    do if (lastmark >= 0) { \
554
241M
        size_t _marks_size = (lastmark+1) * sizeof(void*); \
555
241M
        DATA_STACK_POP_DISCARD(state, _marks_size); \
556
241M
        MARK_TRACE("pop discard", (lastmark)); \
557
271M
    } while (0)
558
559
389M
#define JUMP_NONE            0
560
15.9M
#define JUMP_MAX_UNTIL_1     1
561
217M
#define JUMP_MAX_UNTIL_2     2
562
40.2M
#define JUMP_MAX_UNTIL_3     3
563
0
#define JUMP_MIN_UNTIL_1     4
564
0
#define JUMP_MIN_UNTIL_2     5
565
0
#define JUMP_MIN_UNTIL_3     6
566
48.7M
#define JUMP_REPEAT          7
567
1.18M
#define JUMP_REPEAT_ONE_1    8
568
119M
#define JUMP_REPEAT_ONE_2    9
569
832
#define JUMP_MIN_REPEAT_ONE  10
570
64.6M
#define JUMP_BRANCH          11
571
12
#define JUMP_ASSERT          12
572
153M
#define JUMP_ASSERT_NOT      13
573
0
#define JUMP_POSS_REPEAT_1   14
574
0
#define JUMP_POSS_REPEAT_2   15
575
0
#define JUMP_ATOMIC_GROUP    16
576
577
#define DO_JUMPX(jumpvalue, jumplabel, nextpattern, toplevel_) \
578
661M
    ctx->pattern = pattern; \
579
661M
    ctx->ptr = ptr; \
580
661M
    DATA_ALLOC(SRE(match_context), nextctx); \
581
661M
    nextctx->pattern = nextpattern; \
582
661M
    nextctx->toplevel = toplevel_; \
583
661M
    nextctx->jump = jumpvalue; \
584
661M
    nextctx->last_ctx_pos = ctx_pos; \
585
661M
    pattern = nextpattern; \
586
661M
    ctx_pos = alloc_pos; \
587
661M
    ctx = nextctx; \
588
661M
    goto entrance; \
589
661M
    jumplabel: \
590
661M
    pattern = ctx->pattern; \
591
661M
    ptr = ctx->ptr;
592
593
#define DO_JUMP(jumpvalue, jumplabel, nextpattern) \
594
507M
    DO_JUMPX(jumpvalue, jumplabel, nextpattern, ctx->toplevel)
595
596
#define DO_JUMP0(jumpvalue, jumplabel, nextpattern) \
597
153M
    DO_JUMPX(jumpvalue, jumplabel, nextpattern, 0)
598
599
typedef struct {
600
    Py_ssize_t count;
601
    union {
602
        SRE_CODE chr;
603
        SRE_REPEAT* rep;
604
    } u;
605
    int lastmark;
606
    int lastindex;
607
    const SRE_CODE* pattern;
608
    const SRE_CHAR* ptr;
609
    int toplevel;
610
    int jump;
611
    Py_ssize_t last_ctx_pos;
612
} SRE(match_context);
613
614
#define _MAYBE_CHECK_SIGNALS                                       \
615
1.92G
    do {                                                           \
616
1.92G
        if ((0 == (++sigcount & 0xfff)) && PyErr_CheckSignals()) { \
617
0
            RETURN_ERROR(SRE_ERROR_INTERRUPTED);                   \
618
0
        }                                                          \
619
1.92G
    } while (0)
620
621
#ifdef Py_DEBUG
622
# define MAYBE_CHECK_SIGNALS                                       \
623
    do {                                                           \
624
        _MAYBE_CHECK_SIGNALS;                                      \
625
        if (state->fail_after_count >= 0) {                        \
626
            if (state->fail_after_count-- == 0) {                  \
627
                PyErr_SetNone(state->fail_after_exc);              \
628
                RETURN_ERROR(SRE_ERROR_INTERRUPTED);               \
629
            }                                                      \
630
        }                                                          \
631
    } while (0)
632
#else
633
1.92G
# define MAYBE_CHECK_SIGNALS _MAYBE_CHECK_SIGNALS
634
#endif /* Py_DEBUG */
635
636
#ifdef HAVE_COMPUTED_GOTOS
637
    #ifndef USE_COMPUTED_GOTOS
638
    #define USE_COMPUTED_GOTOS 1
639
    #endif
640
#elif defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
641
    #error "Computed gotos are not supported on this compiler."
642
#else
643
    #undef USE_COMPUTED_GOTOS
644
    #define USE_COMPUTED_GOTOS 0
645
#endif
646
647
#if USE_COMPUTED_GOTOS
648
1.94G
    #define TARGET(OP) TARGET_ ## OP
649
    #define DISPATCH                       \
650
1.92G
        do {                               \
651
1.92G
            MAYBE_CHECK_SIGNALS;           \
652
1.92G
            goto *sre_targets[*pattern++]; \
653
1.92G
        } while (0)
654
#else
655
    #define TARGET(OP) case OP
656
    #define DISPATCH goto dispatch
657
#endif
658
659
/* check if string matches the given pattern.  returns <0 for
660
   error, 0 for failure, and 1 for success */
661
LOCAL(Py_ssize_t)
662
SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
663
389M
{
664
389M
    const SRE_CHAR* end = (const SRE_CHAR *)state->end;
665
389M
    Py_ssize_t alloc_pos, ctx_pos = -1;
666
389M
    Py_ssize_t ret = 0;
667
389M
    int jump;
668
389M
    unsigned int sigcount = state->sigcount;
669
670
389M
    SRE(match_context)* ctx;
671
389M
    SRE(match_context)* nextctx;
672
389M
    INIT_TRACE(state);
673
674
389M
    TRACE(("|%p|%p|ENTER\n", pattern, state->ptr));
675
676
389M
    DATA_ALLOC(SRE(match_context), ctx);
677
389M
    ctx->last_ctx_pos = -1;
678
389M
    ctx->jump = JUMP_NONE;
679
389M
    ctx->toplevel = toplevel;
680
389M
    ctx_pos = alloc_pos;
681
682
389M
#if USE_COMPUTED_GOTOS
683
389M
#include "sre_targets.h"
684
389M
#endif
685
686
1.05G
entrance:
687
688
1.05G
    ;  // Fashion statement.
689
1.05G
    const SRE_CHAR *ptr = (SRE_CHAR *)state->ptr;
690
691
1.05G
    if (pattern[0] == SRE_OP_INFO) {
692
        /* optimization info block */
693
        /* <INFO> <1=skip> <2=flags> <3=min> ... */
694
53.9M
        if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) {
695
10.6M
            TRACE(("reject (got %tu chars, need %zu)\n",
696
10.6M
                   end - ptr, (size_t) pattern[3]));
697
10.6M
            RETURN_FAILURE;
698
10.6M
        }
699
43.3M
        pattern += pattern[1] + 1;
700
43.3M
    }
701
702
1.04G
#if USE_COMPUTED_GOTOS
703
1.04G
    DISPATCH;
704
#else
705
dispatch:
706
    MAYBE_CHECK_SIGNALS;
707
    switch (*pattern++)
708
#endif
709
1.04G
    {
710
711
1.04G
        TARGET(SRE_OP_MARK):
712
            /* set mark */
713
            /* <MARK> <gid> */
714
348M
            TRACE(("|%p|%p|MARK %d\n", pattern,
715
348M
                   ptr, pattern[0]));
716
348M
            {
717
348M
                int i = pattern[0];
718
348M
                if (i & 1)
719
67.7M
                    state->lastindex = i/2 + 1;
720
348M
                if (i > state->lastmark) {
721
                    /* state->lastmark is the highest valid index in the
722
                       state->mark array.  If it is increased by more than 1,
723
                       the intervening marks must be set to NULL to signal
724
                       that these marks have not been encountered. */
725
345M
                    int j = state->lastmark + 1;
726
366M
                    while (j < i)
727
21.3M
                        state->mark[j++] = NULL;
728
345M
                    state->lastmark = i;
729
345M
                }
730
348M
                state->mark[i] = ptr;
731
348M
            }
732
348M
            pattern++;
733
348M
            DISPATCH;
734
735
348M
        TARGET(SRE_OP_LITERAL):
736
            /* match literal string */
737
            /* <LITERAL> <code> */
738
108M
            TRACE(("|%p|%p|LITERAL %d\n", pattern,
739
108M
                   ptr, *pattern));
740
108M
            if (ptr >= end || (SRE_CODE) ptr[0] != pattern[0])
741
66.2M
                RETURN_FAILURE;
742
42.1M
            pattern++;
743
42.1M
            ptr++;
744
42.1M
            DISPATCH;
745
746
42.1M
        TARGET(SRE_OP_NOT_LITERAL):
747
            /* match anything that is not literal character */
748
            /* <NOT_LITERAL> <code> */
749
0
            TRACE(("|%p|%p|NOT_LITERAL %d\n", pattern,
750
0
                   ptr, *pattern));
751
0
            if (ptr >= end || (SRE_CODE) ptr[0] == pattern[0])
752
0
                RETURN_FAILURE;
753
0
            pattern++;
754
0
            ptr++;
755
0
            DISPATCH;
756
757
153M
        TARGET(SRE_OP_SUCCESS):
758
            /* end of pattern */
759
153M
            TRACE(("|%p|%p|SUCCESS\n", pattern, ptr));
760
153M
            if (ctx->toplevel &&
761
33.5M
                ((state->match_all && ptr != state->end) ||
762
33.5M
                 (state->must_advance && ptr == state->start)))
763
200
            {
764
200
                RETURN_FAILURE;
765
200
            }
766
153M
            state->ptr = ptr;
767
153M
            RETURN_SUCCESS;
768
769
30.6M
        TARGET(SRE_OP_AT):
770
            /* match at given position */
771
            /* <AT> <code> */
772
30.6M
            TRACE(("|%p|%p|AT %d\n", pattern, ptr, *pattern));
773
30.6M
            if (!SRE(at)(state, ptr, *pattern))
774
3.41M
                RETURN_FAILURE;
775
27.2M
            pattern++;
776
27.2M
            DISPATCH;
777
778
149M
        TARGET(SRE_OP_CATEGORY):
779
            /* match at given category */
780
            /* <CATEGORY> <code> */
781
149M
            TRACE(("|%p|%p|CATEGORY %d\n", pattern,
782
149M
                   ptr, *pattern));
783
149M
            if (ptr >= end || !sre_category(pattern[0], ptr[0]))
784
11.9M
                RETURN_FAILURE;
785
137M
            pattern++;
786
137M
            ptr++;
787
137M
            DISPATCH;
788
789
137M
        TARGET(SRE_OP_ANY):
790
            /* match anything (except a newline) */
791
            /* <ANY> */
792
0
            TRACE(("|%p|%p|ANY\n", pattern, ptr));
793
0
            if (ptr >= end || SRE_IS_LINEBREAK(ptr[0]))
794
0
                RETURN_FAILURE;
795
0
            ptr++;
796
0
            DISPATCH;
797
798
0
        TARGET(SRE_OP_ANY_ALL):
799
            /* match anything */
800
            /* <ANY_ALL> */
801
0
            TRACE(("|%p|%p|ANY_ALL\n", pattern, ptr));
802
0
            if (ptr >= end)
803
0
                RETURN_FAILURE;
804
0
            ptr++;
805
0
            DISPATCH;
806
807
314M
        TARGET(SRE_OP_IN):
808
            /* match set member (or non_member) */
809
            /* <IN> <skip> <set> */
810
314M
            TRACE(("|%p|%p|IN\n", pattern, ptr));
811
314M
            if (ptr >= end ||
812
307M
                !SRE(charset)(state, pattern + 1, *ptr))
813
145M
                RETURN_FAILURE;
814
168M
            pattern += pattern[0];
815
168M
            ptr++;
816
168M
            DISPATCH;
817
818
168M
        TARGET(SRE_OP_LITERAL_IGNORE):
819
0
            TRACE(("|%p|%p|LITERAL_IGNORE %d\n",
820
0
                   pattern, ptr, pattern[0]));
821
0
            if (ptr >= end ||
822
0
                sre_lower_ascii(*ptr) != *pattern)
823
0
                RETURN_FAILURE;
824
0
            pattern++;
825
0
            ptr++;
826
0
            DISPATCH;
827
828
28
        TARGET(SRE_OP_LITERAL_UNI_IGNORE):
829
28
            TRACE(("|%p|%p|LITERAL_UNI_IGNORE %d\n",
830
28
                   pattern, ptr, pattern[0]));
831
28
            if (ptr >= end ||
832
28
                sre_lower_unicode(*ptr) != *pattern)
833
0
                RETURN_FAILURE;
834
28
            pattern++;
835
28
            ptr++;
836
28
            DISPATCH;
837
838
28
        TARGET(SRE_OP_LITERAL_LOC_IGNORE):
839
0
            TRACE(("|%p|%p|LITERAL_LOC_IGNORE %d\n",
840
0
                   pattern, ptr, pattern[0]));
841
0
            if (ptr >= end
842
0
                || !char_loc_ignore(*pattern, *ptr))
843
0
                RETURN_FAILURE;
844
0
            pattern++;
845
0
            ptr++;
846
0
            DISPATCH;
847
848
0
        TARGET(SRE_OP_NOT_LITERAL_IGNORE):
849
0
            TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n",
850
0
                   pattern, ptr, *pattern));
851
0
            if (ptr >= end ||
852
0
                sre_lower_ascii(*ptr) == *pattern)
853
0
                RETURN_FAILURE;
854
0
            pattern++;
855
0
            ptr++;
856
0
            DISPATCH;
857
858
0
        TARGET(SRE_OP_NOT_LITERAL_UNI_IGNORE):
859
0
            TRACE(("|%p|%p|NOT_LITERAL_UNI_IGNORE %d\n",
860
0
                   pattern, ptr, *pattern));
861
0
            if (ptr >= end ||
862
0
                sre_lower_unicode(*ptr) == *pattern)
863
0
                RETURN_FAILURE;
864
0
            pattern++;
865
0
            ptr++;
866
0
            DISPATCH;
867
868
0
        TARGET(SRE_OP_NOT_LITERAL_LOC_IGNORE):
869
0
            TRACE(("|%p|%p|NOT_LITERAL_LOC_IGNORE %d\n",
870
0
                   pattern, ptr, *pattern));
871
0
            if (ptr >= end
872
0
                || char_loc_ignore(*pattern, *ptr))
873
0
                RETURN_FAILURE;
874
0
            pattern++;
875
0
            ptr++;
876
0
            DISPATCH;
877
878
0
        TARGET(SRE_OP_IN_IGNORE):
879
0
            TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr));
880
0
            if (ptr >= end
881
0
                || !SRE(charset)(state, pattern+1,
882
0
                                 (SRE_CODE)sre_lower_ascii(*ptr)))
883
0
                RETURN_FAILURE;
884
0
            pattern += pattern[0];
885
0
            ptr++;
886
0
            DISPATCH;
887
888
24
        TARGET(SRE_OP_IN_UNI_IGNORE):
889
24
            TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ptr));
890
24
            if (ptr >= end
891
16
                || !SRE(charset)(state, pattern+1,
892
16
                                 (SRE_CODE)sre_lower_unicode(*ptr)))
893
16
                RETURN_FAILURE;
894
8
            pattern += pattern[0];
895
8
            ptr++;
896
8
            DISPATCH;
897
898
8
        TARGET(SRE_OP_IN_LOC_IGNORE):
899
0
            TRACE(("|%p|%p|IN_LOC_IGNORE\n", pattern, ptr));
900
0
            if (ptr >= end
901
0
                || !SRE(charset_loc_ignore)(state, pattern+1, *ptr))
902
0
                RETURN_FAILURE;
903
0
            pattern += pattern[0];
904
0
            ptr++;
905
0
            DISPATCH;
906
907
22.4M
        TARGET(SRE_OP_JUMP):
908
22.4M
        TARGET(SRE_OP_INFO):
909
            /* jump forward */
910
            /* <JUMP> <offset> */
911
22.4M
            TRACE(("|%p|%p|JUMP %d\n", pattern,
912
22.4M
                   ptr, pattern[0]));
913
22.4M
            pattern += pattern[0];
914
22.4M
            DISPATCH;
915
916
39.0M
        TARGET(SRE_OP_BRANCH):
917
            /* alternation */
918
            /* <BRANCH> <0=skip> code <JUMP> ... <NULL> */
919
39.0M
            TRACE(("|%p|%p|BRANCH\n", pattern, ptr));
920
39.0M
            LASTMARK_SAVE();
921
39.0M
            if (state->save_marks)
922
71.7k
                MARK_PUSH(ctx->lastmark);
923
107M
            for (; pattern[0]; pattern += pattern[0]) {
924
87.8M
                if (pattern[1] == SRE_OP_LITERAL &&
925
58.2M
                    (ptr >= end ||
926
58.2M
                     (SRE_CODE) *ptr != pattern[2]))
927
21.5M
                    continue;
928
66.2M
                if (pattern[1] == SRE_OP_IN &&
929
3.05M
                    (ptr >= end ||
930
3.05M
                     !SRE(charset)(state, pattern + 3,
931
3.05M
                                   (SRE_CODE) *ptr)))
932
1.59M
                    continue;
933
64.6M
                state->ptr = ptr;
934
64.6M
                DO_JUMP(JUMP_BRANCH, jump_branch, pattern+1);
935
64.6M
                if (ret) {
936
19.2M
                    if (state->save_marks)
937
53.5k
                        MARK_POP_DISCARD(ctx->lastmark);
938
19.2M
                    RETURN_ON_ERROR(ret);
939
19.2M
                    RETURN_SUCCESS;
940
19.2M
                }
941
45.4M
                if (state->save_marks)
942
40.2k
                    MARK_POP_KEEP(ctx->lastmark);
943
45.4M
                LASTMARK_RESTORE();
944
45.4M
            }
945
19.7M
            if (state->save_marks)
946
18.2k
                MARK_POP_DISCARD(ctx->lastmark);
947
19.7M
            RETURN_FAILURE;
948
949
315M
        TARGET(SRE_OP_REPEAT_ONE):
950
            /* match repeated sequence (maximizing regexp) */
951
952
            /* this operator only works if the repeated item is
953
               exactly one character wide, and we're not already
954
               collecting backtracking points.  for other cases,
955
               use the MAX_REPEAT operator */
956
957
            /* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
958
959
315M
            TRACE(("|%p|%p|REPEAT_ONE %d %d\n", pattern, ptr,
960
315M
                   pattern[1], pattern[2]));
961
962
315M
            if ((Py_ssize_t) pattern[1] > end - ptr)
963
970k
                RETURN_FAILURE; /* cannot match */
964
965
314M
            state->ptr = ptr;
966
967
314M
            ret = SRE(count)(state, pattern+3, pattern[2]);
968
314M
            RETURN_ON_ERROR(ret);
969
314M
            DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
970
314M
            ctx->count = ret;
971
314M
            ptr += ctx->count;
972
973
            /* when we arrive here, count contains the number of
974
               matches, and ptr points to the tail of the target
975
               string.  check if the rest of the pattern matches,
976
               and backtrack if not. */
977
978
314M
            if (ctx->count < (Py_ssize_t) pattern[1])
979
219M
                RETURN_FAILURE;
980
981
95.4M
            if (pattern[pattern[0]] == SRE_OP_SUCCESS &&
982
2.81M
                ptr == state->end &&
983
99.0k
                !(ctx->toplevel && state->must_advance && ptr == state->start))
984
99.0k
            {
985
                /* tail is empty.  we're finished */
986
99.0k
                state->ptr = ptr;
987
99.0k
                RETURN_SUCCESS;
988
99.0k
            }
989
990
95.3M
            LASTMARK_SAVE();
991
95.3M
            if (state->save_marks)
992
71.8M
                MARK_PUSH(ctx->lastmark);
993
994
95.3M
            if (pattern[pattern[0]] == SRE_OP_LITERAL) {
995
                /* tail starts with a literal. skip positions where
996
                   the rest of the pattern cannot possibly match */
997
2.37M
                ctx->u.chr = pattern[pattern[0]+1];
998
2.37M
                for (;;) {
999
9.84M
                    while (ctx->count >= (Py_ssize_t) pattern[1] &&
1000
8.65M
                           (ptr >= end || *ptr != ctx->u.chr)) {
1001
7.46M
                        ptr--;
1002
7.46M
                        ctx->count--;
1003
7.46M
                    }
1004
2.37M
                    if (ctx->count < (Py_ssize_t) pattern[1])
1005
1.19M
                        break;
1006
1.18M
                    state->ptr = ptr;
1007
1.18M
                    DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1,
1008
1.18M
                            pattern+pattern[0]);
1009
1.18M
                    if (ret) {
1010
1.18M
                        if (state->save_marks)
1011
0
                            MARK_POP_DISCARD(ctx->lastmark);
1012
1.18M
                        RETURN_ON_ERROR(ret);
1013
1.18M
                        RETURN_SUCCESS;
1014
1.18M
                    }
1015
16
                    if (state->save_marks)
1016
0
                        MARK_POP_KEEP(ctx->lastmark);
1017
16
                    LASTMARK_RESTORE();
1018
1019
16
                    ptr--;
1020
16
                    ctx->count--;
1021
16
                }
1022
1.19M
                if (state->save_marks)
1023
32
                    MARK_POP_DISCARD(ctx->lastmark);
1024
92.9M
            } else {
1025
                /* general case */
1026
121M
                while (ctx->count >= (Py_ssize_t) pattern[1]) {
1027
119M
                    state->ptr = ptr;
1028
119M
                    DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2,
1029
119M
                            pattern+pattern[0]);
1030
119M
                    if (ret) {
1031
90.8M
                        if (state->save_marks)
1032
70.2M
                            MARK_POP_DISCARD(ctx->lastmark);
1033
90.8M
                        RETURN_ON_ERROR(ret);
1034
90.8M
                        RETURN_SUCCESS;
1035
90.8M
                    }
1036
28.7M
                    if (state->save_marks)
1037
1.84M
                        MARK_POP_KEEP(ctx->lastmark);
1038
28.7M
                    LASTMARK_RESTORE();
1039
1040
28.7M
                    ptr--;
1041
28.7M
                    ctx->count--;
1042
28.7M
                }
1043
2.15M
                if (state->save_marks)
1044
1.61M
                    MARK_POP_DISCARD(ctx->lastmark);
1045
2.15M
            }
1046
3.34M
            RETURN_FAILURE;
1047
1048
16
        TARGET(SRE_OP_MIN_REPEAT_ONE):
1049
            /* match repeated sequence (minimizing regexp) */
1050
1051
            /* this operator only works if the repeated item is
1052
               exactly one character wide, and we're not already
1053
               collecting backtracking points.  for other cases,
1054
               use the MIN_REPEAT operator */
1055
1056
            /* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
1057
1058
16
            TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", pattern, ptr,
1059
16
                   pattern[1], pattern[2]));
1060
1061
16
            if ((Py_ssize_t) pattern[1] > end - ptr)
1062
0
                RETURN_FAILURE; /* cannot match */
1063
1064
16
            state->ptr = ptr;
1065
1066
16
            if (pattern[1] == 0)
1067
16
                ctx->count = 0;
1068
0
            else {
1069
                /* count using pattern min as the maximum */
1070
0
                ret = SRE(count)(state, pattern+3, pattern[1]);
1071
0
                RETURN_ON_ERROR(ret);
1072
0
                DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1073
0
                if (ret < (Py_ssize_t) pattern[1])
1074
                    /* didn't match minimum number of times */
1075
0
                    RETURN_FAILURE;
1076
                /* advance past minimum matches of repeat */
1077
0
                ctx->count = ret;
1078
0
                ptr += ctx->count;
1079
0
            }
1080
1081
16
            if (pattern[pattern[0]] == SRE_OP_SUCCESS &&
1082
0
                !(ctx->toplevel &&
1083
0
                  ((state->match_all && ptr != state->end) ||
1084
0
                   (state->must_advance && ptr == state->start))))
1085
0
            {
1086
                /* tail is empty.  we're finished */
1087
0
                state->ptr = ptr;
1088
0
                RETURN_SUCCESS;
1089
1090
16
            } else {
1091
                /* general case */
1092
16
                LASTMARK_SAVE();
1093
16
                if (state->save_marks)
1094
0
                    MARK_PUSH(ctx->lastmark);
1095
1096
832
                while ((Py_ssize_t)pattern[2] == SRE_MAXREPEAT
1097
832
                       || ctx->count <= (Py_ssize_t)pattern[2]) {
1098
832
                    state->ptr = ptr;
1099
832
                    DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one,
1100
832
                            pattern+pattern[0]);
1101
832
                    if (ret) {
1102
0
                        if (state->save_marks)
1103
0
                            MARK_POP_DISCARD(ctx->lastmark);
1104
0
                        RETURN_ON_ERROR(ret);
1105
0
                        RETURN_SUCCESS;
1106
0
                    }
1107
832
                    if (state->save_marks)
1108
0
                        MARK_POP_KEEP(ctx->lastmark);
1109
832
                    LASTMARK_RESTORE();
1110
1111
832
                    state->ptr = ptr;
1112
832
                    ret = SRE(count)(state, pattern+3, 1);
1113
832
                    RETURN_ON_ERROR(ret);
1114
832
                    DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1115
832
                    if (ret == 0)
1116
16
                        break;
1117
832
                    assert(ret == 1);
1118
816
                    ptr++;
1119
816
                    ctx->count++;
1120
816
                }
1121
16
                if (state->save_marks)
1122
0
                    MARK_POP_DISCARD(ctx->lastmark);
1123
16
            }
1124
16
            RETURN_FAILURE;
1125
1126
0
        TARGET(SRE_OP_POSSESSIVE_REPEAT_ONE):
1127
            /* match repeated sequence (maximizing regexp) without
1128
               backtracking */
1129
1130
            /* this operator only works if the repeated item is
1131
               exactly one character wide, and we're not already
1132
               collecting backtracking points.  for other cases,
1133
               use the MAX_REPEAT operator */
1134
1135
            /* <POSSESSIVE_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS>
1136
               tail */
1137
1138
0
            TRACE(("|%p|%p|POSSESSIVE_REPEAT_ONE %d %d\n", pattern,
1139
0
                   ptr, pattern[1], pattern[2]));
1140
1141
0
            if (ptr + pattern[1] > end) {
1142
0
                RETURN_FAILURE; /* cannot match */
1143
0
            }
1144
1145
0
            state->ptr = ptr;
1146
1147
0
            ret = SRE(count)(state, pattern + 3, pattern[2]);
1148
0
            RETURN_ON_ERROR(ret);
1149
0
            DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1150
0
            ctx->count = ret;
1151
0
            ptr += ctx->count;
1152
1153
            /* when we arrive here, count contains the number of
1154
               matches, and ptr points to the tail of the target
1155
               string.  check if the rest of the pattern matches,
1156
               and fail if not. */
1157
1158
            /* Test for not enough repetitions in match */
1159
0
            if (ctx->count < (Py_ssize_t) pattern[1]) {
1160
0
                RETURN_FAILURE;
1161
0
            }
1162
1163
            /* Update the pattern to point to the next op code */
1164
0
            pattern += pattern[0];
1165
1166
            /* Let the tail be evaluated separately and consider this
1167
               match successful. */
1168
0
            if (*pattern == SRE_OP_SUCCESS &&
1169
0
                ptr == state->end &&
1170
0
                !(ctx->toplevel && state->must_advance && ptr == state->start))
1171
0
            {
1172
                /* tail is empty.  we're finished */
1173
0
                state->ptr = ptr;
1174
0
                RETURN_SUCCESS;
1175
0
            }
1176
1177
            /* Attempt to match the rest of the string */
1178
0
            DISPATCH;
1179
1180
48.7M
        TARGET(SRE_OP_REPEAT):
1181
            /* create repeat context.  all the hard work is done
1182
               by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */
1183
            /* <REPEAT> <skip> <1=min> <2=max>
1184
               <3=repeat_index> item <UNTIL> tail */
1185
48.7M
            TRACE(("|%p|%p|REPEAT %d %d\n", pattern, ptr,
1186
48.7M
                   pattern[1], pattern[2]));
1187
1188
            /* install new repeat context */
1189
48.7M
            ctx->u.rep = repeat_pool_malloc(state);
1190
48.7M
            if (!ctx->u.rep) {
1191
0
                RETURN_ERROR(SRE_ERROR_MEMORY);
1192
0
            }
1193
48.7M
            ctx->u.rep->count = -1;
1194
48.7M
            ctx->u.rep->pattern = pattern;
1195
48.7M
            ctx->u.rep->prev = state->repeat;
1196
48.7M
            ctx->u.rep->last_ptr = NULL;
1197
48.7M
            state->repeat = ctx->u.rep;
1198
48.7M
            state->save_marks++;
1199
1200
48.7M
            state->ptr = ptr;
1201
48.7M
            DO_JUMP(JUMP_REPEAT, jump_repeat, pattern+pattern[0]);
1202
48.7M
            state->repeat = ctx->u.rep->prev;
1203
48.7M
            state->save_marks--;
1204
48.7M
            repeat_pool_free(state, ctx->u.rep);
1205
1206
48.7M
            if (ret) {
1207
39.6M
                RETURN_ON_ERROR(ret);
1208
39.6M
                RETURN_SUCCESS;
1209
39.6M
            }
1210
9.08M
            RETURN_FAILURE;
1211
1212
242M
        TARGET(SRE_OP_MAX_UNTIL):
1213
            /* maximizing repeat */
1214
            /* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */
1215
1216
            /* FIXME: we probably need to deal with zero-width
1217
               matches in here... */
1218
1219
242M
            ctx->u.rep = state->repeat;
1220
242M
            if (!ctx->u.rep)
1221
0
                RETURN_ERROR(SRE_ERROR_STATE);
1222
1223
242M
            state->ptr = ptr;
1224
1225
242M
            ctx->count = ctx->u.rep->count+1;
1226
1227
242M
            TRACE(("|%p|%p|MAX_UNTIL %zd\n", pattern,
1228
242M
                   ptr, ctx->count));
1229
1230
242M
            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
1231
                /* not enough matches */
1232
15.9M
                ctx->u.rep->count = ctx->count;
1233
15.9M
                DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
1234
15.9M
                        ctx->u.rep->pattern+3);
1235
15.9M
                if (ret) {
1236
6.93M
                    RETURN_ON_ERROR(ret);
1237
6.93M
                    RETURN_SUCCESS;
1238
6.93M
                }
1239
8.98M
                ctx->u.rep->count = ctx->count-1;
1240
8.98M
                state->ptr = ptr;
1241
8.98M
                RETURN_FAILURE;
1242
8.98M
            }
1243
1244
227M
            if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] ||
1245
9.63M
                ctx->u.rep->pattern[2] == SRE_MAXREPEAT) &&
1246
217M
                state->ptr != ctx->u.rep->last_ptr) {
1247
                /* we may have enough matches, but if we can
1248
                   match another item, do so */
1249
217M
                ctx->u.rep->count = ctx->count;
1250
217M
                LASTMARK_SAVE();
1251
217M
                MARK_PUSH(ctx->lastmark);
1252
                /* zero-width match protection */
1253
217M
                LAST_PTR_PUSH();
1254
217M
                ctx->u.rep->last_ptr = state->ptr;
1255
217M
                DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
1256
217M
                        ctx->u.rep->pattern+3);
1257
217M
                LAST_PTR_POP();
1258
217M
                if (ret) {
1259
186M
                    MARK_POP_DISCARD(ctx->lastmark);
1260
186M
                    RETURN_ON_ERROR(ret);
1261
186M
                    RETURN_SUCCESS;
1262
186M
                }
1263
30.6M
                MARK_POP(ctx->lastmark);
1264
30.6M
                LASTMARK_RESTORE();
1265
30.6M
                ctx->u.rep->count = ctx->count-1;
1266
30.6M
                state->ptr = ptr;
1267
30.6M
            }
1268
1269
            /* cannot match more repeated items here.  make sure the
1270
               tail matches */
1271
40.2M
            state->repeat = ctx->u.rep->prev;
1272
40.2M
            state->save_marks--;
1273
40.2M
            DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, pattern);
1274
40.2M
            state->repeat = ctx->u.rep; // restore repeat before return
1275
40.2M
            state->save_marks++;
1276
1277
40.2M
            RETURN_ON_SUCCESS(ret);
1278
625k
            state->ptr = ptr;
1279
625k
            RETURN_FAILURE;
1280
1281
0
        TARGET(SRE_OP_MIN_UNTIL):
1282
            /* minimizing repeat */
1283
            /* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */
1284
1285
0
            ctx->u.rep = state->repeat;
1286
0
            if (!ctx->u.rep)
1287
0
                RETURN_ERROR(SRE_ERROR_STATE);
1288
1289
0
            state->ptr = ptr;
1290
1291
0
            ctx->count = ctx->u.rep->count+1;
1292
1293
0
            TRACE(("|%p|%p|MIN_UNTIL %zd %p\n", pattern,
1294
0
                   ptr, ctx->count, ctx->u.rep->pattern));
1295
1296
0
            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
1297
                /* not enough matches */
1298
0
                ctx->u.rep->count = ctx->count;
1299
0
                DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
1300
0
                        ctx->u.rep->pattern+3);
1301
0
                if (ret) {
1302
0
                    RETURN_ON_ERROR(ret);
1303
0
                    RETURN_SUCCESS;
1304
0
                }
1305
0
                ctx->u.rep->count = ctx->count-1;
1306
0
                state->ptr = ptr;
1307
0
                RETURN_FAILURE;
1308
0
            }
1309
1310
            /* see if the tail matches */
1311
0
            state->repeat = ctx->u.rep->prev;
1312
0
            state->save_marks--;
1313
1314
0
            LASTMARK_SAVE();
1315
0
            if (state->save_marks)
1316
0
                MARK_PUSH(ctx->lastmark);
1317
1318
0
            DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, pattern);
1319
            /* save_marks is balanced across the jump, so this equals the
1320
               value tested for MARK_PUSH above */
1321
0
            int pushed = state->save_marks != 0;
1322
0
            state->repeat = ctx->u.rep; // restore repeat before return
1323
0
            state->save_marks++;
1324
1325
0
            if (ret) {
1326
0
                if (pushed)
1327
0
                    MARK_POP_DISCARD(ctx->lastmark);
1328
0
                RETURN_ON_ERROR(ret);
1329
0
                RETURN_SUCCESS;
1330
0
            }
1331
0
            if (pushed)
1332
0
                MARK_POP(ctx->lastmark);
1333
0
            LASTMARK_RESTORE();
1334
1335
0
            state->ptr = ptr;
1336
1337
0
            if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2]
1338
0
                && ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
1339
0
                state->ptr == ctx->u.rep->last_ptr)
1340
0
                RETURN_FAILURE;
1341
1342
0
            ctx->u.rep->count = ctx->count;
1343
            /* zero-width match protection */
1344
0
            LAST_PTR_PUSH();
1345
0
            ctx->u.rep->last_ptr = state->ptr;
1346
0
            DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
1347
0
                    ctx->u.rep->pattern+3);
1348
0
            LAST_PTR_POP();
1349
0
            if (ret) {
1350
0
                RETURN_ON_ERROR(ret);
1351
0
                RETURN_SUCCESS;
1352
0
            }
1353
0
            ctx->u.rep->count = ctx->count-1;
1354
0
            state->ptr = ptr;
1355
0
            RETURN_FAILURE;
1356
1357
0
        TARGET(SRE_OP_POSSESSIVE_REPEAT):
1358
            /* create possessive repeat contexts. */
1359
            /* <POSSESSIVE_REPEAT> <skip> <1=min> <2=max> pattern
1360
               <SUCCESS> tail */
1361
0
            TRACE(("|%p|%p|POSSESSIVE_REPEAT %d %d\n", pattern,
1362
0
                   ptr, pattern[1], pattern[2]));
1363
1364
            /* Set the global Input pointer to this context's Input
1365
               pointer */
1366
0
            state->ptr = ptr;
1367
1368
            /* Capture groups in the body can be revisited on backtracking
1369
               between iterations, so their marks must be saved and restored,
1370
               as is done inside a repeat. */
1371
0
            state->save_marks++;
1372
1373
            /* Initialize Count to 0 */
1374
0
            ctx->count = 0;
1375
1376
            /* Check for minimum required matches. */
1377
0
            while (ctx->count < (Py_ssize_t)pattern[1]) {
1378
                /* not enough matches */
1379
0
                DO_JUMP0(JUMP_POSS_REPEAT_1, jump_poss_repeat_1,
1380
0
                         &pattern[3]);
1381
0
                if (ret) {
1382
0
                    RETURN_ON_ERROR(ret);
1383
0
                    ctx->count++;
1384
0
                }
1385
0
                else {
1386
0
                    state->ptr = ptr;
1387
0
                    state->save_marks--;
1388
0
                    RETURN_FAILURE;
1389
0
                }
1390
0
            }
1391
1392
            /* Clear the context's Input stream pointer so that it
1393
               doesn't match the global state so that the while loop can
1394
               be entered. */
1395
0
            ptr = NULL;
1396
1397
            /* Keep trying to parse the <pattern> sub-pattern until the
1398
               end is reached, creating a new context each time. */
1399
0
            while ((ctx->count < (Py_ssize_t)pattern[2] ||
1400
0
                    (Py_ssize_t)pattern[2] == SRE_MAXREPEAT) &&
1401
0
                   state->ptr != ptr) {
1402
                /* Save the Capture Group Marker state into the current
1403
                   Context and back up the current highest number
1404
                   Capture Group marker. */
1405
0
                LASTMARK_SAVE();
1406
0
                MARK_PUSH(ctx->lastmark);
1407
1408
                /* zero-width match protection */
1409
                /* Set the context's Input Stream pointer to be the
1410
                   current Input Stream pointer from the global
1411
                   state.  When the loop reaches the next iteration,
1412
                   the context will then store the last known good
1413
                   position with the global state holding the Input
1414
                   Input Stream position that has been updated with
1415
                   the most recent match.  Thus, if state's Input
1416
                   stream remains the same as the one stored in the
1417
                   current Context, we know we have successfully
1418
                   matched an empty string and that all subsequent
1419
                   matches will also be the empty string until the
1420
                   maximum number of matches are counted, and because
1421
                   of this, we could immediately stop at that point and
1422
                   consider this match successful. */
1423
0
                ptr = state->ptr;
1424
1425
                /* We have not reached the maximin matches, so try to
1426
                   match once more. */
1427
0
                DO_JUMP0(JUMP_POSS_REPEAT_2, jump_poss_repeat_2,
1428
0
                         &pattern[3]);
1429
1430
                /* Check to see if the last attempted match
1431
                   succeeded. */
1432
0
                if (ret) {
1433
                    /* Drop the saved highest number Capture Group
1434
                       marker saved above and use the newly updated
1435
                       value. */
1436
0
                    MARK_POP_DISCARD(ctx->lastmark);
1437
0
                    RETURN_ON_ERROR(ret);
1438
1439
                    /* Success, increment the count. */
1440
0
                    ctx->count++;
1441
0
                }
1442
                /* Last attempted match failed. */
1443
0
                else {
1444
                    /* Restore the previously saved highest number
1445
                       Capture Group marker since the last iteration
1446
                       did not match, then restore that to the global
1447
                       state. */
1448
0
                    MARK_POP(ctx->lastmark);
1449
0
                    LASTMARK_RESTORE();
1450
1451
                    /* Restore the global Input Stream pointer
1452
                       since it can change after jumps. */
1453
0
                    state->ptr = ptr;
1454
1455
                    /* We have sufficient matches, so exit loop. */
1456
0
                    break;
1457
0
                }
1458
0
            }
1459
1460
0
            state->save_marks--;
1461
1462
            /* Evaluate Tail */
1463
            /* Jump to end of pattern indicated by skip, and then skip
1464
               the SUCCESS op code that follows it. */
1465
0
            pattern += pattern[0] + 1;
1466
0
            ptr = state->ptr;
1467
0
            DISPATCH;
1468
1469
0
        TARGET(SRE_OP_ATOMIC_GROUP):
1470
            /* Atomic Group Sub Pattern */
1471
            /* <ATOMIC_GROUP> <skip> pattern <SUCCESS> tail */
1472
0
            TRACE(("|%p|%p|ATOMIC_GROUP\n", pattern, ptr));
1473
1474
            /* Set the global Input pointer to this context's Input
1475
               pointer */
1476
0
            state->ptr = ptr;
1477
1478
            /* Evaluate the Atomic Group in a new context, terminating
1479
               when the end of the group, represented by a SUCCESS op
1480
               code, is reached. */
1481
            /* Group Pattern begins at an offset of 1 code. */
1482
0
            DO_JUMP0(JUMP_ATOMIC_GROUP, jump_atomic_group,
1483
0
                     &pattern[1]);
1484
1485
            /* Test Exit Condition */
1486
0
            RETURN_ON_ERROR(ret);
1487
1488
0
            if (ret == 0) {
1489
                /* Atomic Group failed to Match. */
1490
0
                state->ptr = ptr;
1491
0
                RETURN_FAILURE;
1492
0
            }
1493
1494
            /* Evaluate Tail */
1495
            /* Jump to end of pattern indicated by skip, and then skip
1496
               the SUCCESS op code that follows it. */
1497
0
            pattern += pattern[0];
1498
0
            ptr = state->ptr;
1499
0
            DISPATCH;
1500
1501
0
        TARGET(SRE_OP_GROUPREF):
1502
            /* match backreference */
1503
0
            TRACE(("|%p|%p|GROUPREF %d\n", pattern,
1504
0
                   ptr, pattern[0]));
1505
0
            {
1506
0
                int groupref = pattern[0] * 2;
1507
0
                if (groupref >= state->lastmark) {
1508
0
                    RETURN_FAILURE;
1509
0
                } else {
1510
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1511
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1512
0
                    if (!p || !e || e < p)
1513
0
                        RETURN_FAILURE;
1514
0
                    while (p < e) {
1515
0
                        if (ptr >= end || *ptr != *p)
1516
0
                            RETURN_FAILURE;
1517
0
                        p++;
1518
0
                        ptr++;
1519
0
                    }
1520
0
                }
1521
0
            }
1522
0
            pattern++;
1523
0
            DISPATCH;
1524
1525
0
        TARGET(SRE_OP_GROUPREF_IGNORE):
1526
            /* match backreference */
1527
0
            TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", pattern,
1528
0
                   ptr, pattern[0]));
1529
0
            {
1530
0
                int groupref = pattern[0] * 2;
1531
0
                if (groupref >= state->lastmark) {
1532
0
                    RETURN_FAILURE;
1533
0
                } else {
1534
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1535
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1536
0
                    if (!p || !e || e < p)
1537
0
                        RETURN_FAILURE;
1538
0
                    while (p < e) {
1539
0
                        if (ptr >= end ||
1540
0
                            sre_lower_ascii(*ptr) != sre_lower_ascii(*p))
1541
0
                            RETURN_FAILURE;
1542
0
                        p++;
1543
0
                        ptr++;
1544
0
                    }
1545
0
                }
1546
0
            }
1547
0
            pattern++;
1548
0
            DISPATCH;
1549
1550
0
        TARGET(SRE_OP_GROUPREF_UNI_IGNORE):
1551
            /* match backreference */
1552
0
            TRACE(("|%p|%p|GROUPREF_UNI_IGNORE %d\n", pattern,
1553
0
                   ptr, pattern[0]));
1554
0
            {
1555
0
                int groupref = pattern[0] * 2;
1556
0
                if (groupref >= state->lastmark) {
1557
0
                    RETURN_FAILURE;
1558
0
                } else {
1559
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1560
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1561
0
                    if (!p || !e || e < p)
1562
0
                        RETURN_FAILURE;
1563
0
                    while (p < e) {
1564
0
                        if (ptr >= end ||
1565
0
                            sre_lower_unicode(*ptr) != sre_lower_unicode(*p))
1566
0
                            RETURN_FAILURE;
1567
0
                        p++;
1568
0
                        ptr++;
1569
0
                    }
1570
0
                }
1571
0
            }
1572
0
            pattern++;
1573
0
            DISPATCH;
1574
1575
0
        TARGET(SRE_OP_GROUPREF_LOC_IGNORE):
1576
            /* match backreference */
1577
0
            TRACE(("|%p|%p|GROUPREF_LOC_IGNORE %d\n", pattern,
1578
0
                   ptr, pattern[0]));
1579
0
            {
1580
0
                int groupref = pattern[0] * 2;
1581
0
                if (groupref >= state->lastmark) {
1582
0
                    RETURN_FAILURE;
1583
0
                } else {
1584
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1585
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1586
0
                    if (!p || !e || e < p)
1587
0
                        RETURN_FAILURE;
1588
0
                    while (p < e) {
1589
0
                        if (ptr >= end ||
1590
0
                            sre_lower_locale(*ptr) != sre_lower_locale(*p))
1591
0
                            RETURN_FAILURE;
1592
0
                        p++;
1593
0
                        ptr++;
1594
0
                    }
1595
0
                }
1596
0
            }
1597
0
            pattern++;
1598
0
            DISPATCH;
1599
1600
0
        TARGET(SRE_OP_GROUPREF_EXISTS):
1601
0
            TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", pattern,
1602
0
                   ptr, pattern[0]));
1603
            /* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */
1604
0
            {
1605
0
                int groupref = pattern[0] * 2;
1606
0
                if (groupref >= state->lastmark) {
1607
0
                    pattern += pattern[1];
1608
0
                    DISPATCH;
1609
0
                } else {
1610
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1611
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1612
0
                    if (!p || !e || e < p) {
1613
0
                        pattern += pattern[1];
1614
0
                        DISPATCH;
1615
0
                    }
1616
0
                }
1617
0
            }
1618
0
            pattern += 2;
1619
0
            DISPATCH;
1620
1621
12
        TARGET(SRE_OP_ASSERT):
1622
            /* assert subpattern */
1623
            /* <ASSERT> <skip> <back> <pattern> */
1624
12
            TRACE(("|%p|%p|ASSERT %d\n", pattern,
1625
12
                   ptr, pattern[1]));
1626
12
            if ((uintptr_t)(ptr - (SRE_CHAR *)state->beginning) < pattern[1])
1627
0
                RETURN_FAILURE;
1628
12
            state->ptr = ptr - pattern[1];
1629
12
            DO_JUMP0(JUMP_ASSERT, jump_assert, pattern+2);
1630
12
            RETURN_ON_FAILURE(ret);
1631
0
            pattern += pattern[0];
1632
0
            DISPATCH;
1633
1634
153M
        TARGET(SRE_OP_ASSERT_NOT):
1635
            /* assert not subpattern */
1636
            /* <ASSERT_NOT> <skip> <back> <pattern> */
1637
153M
            TRACE(("|%p|%p|ASSERT_NOT %d\n", pattern,
1638
153M
                   ptr, pattern[1]));
1639
153M
            if ((uintptr_t)(ptr - (SRE_CHAR *)state->beginning) >= pattern[1]) {
1640
153M
                state->ptr = ptr - pattern[1];
1641
153M
                LASTMARK_SAVE();
1642
153M
                if (state->save_marks)
1643
153M
                    MARK_PUSH(ctx->lastmark);
1644
1645
307M
                DO_JUMP0(JUMP_ASSERT_NOT, jump_assert_not, pattern+2);
1646
307M
                if (ret) {
1647
12.7M
                    if (state->save_marks)
1648
12.7M
                        MARK_POP_DISCARD(ctx->lastmark);
1649
12.7M
                    RETURN_ON_ERROR(ret);
1650
12.7M
                    RETURN_FAILURE;
1651
12.7M
                }
1652
140M
                if (state->save_marks)
1653
140M
                    MARK_POP(ctx->lastmark);
1654
140M
                LASTMARK_RESTORE();
1655
140M
            }
1656
140M
            pattern += pattern[0];
1657
140M
            DISPATCH;
1658
1659
140M
        TARGET(SRE_OP_FAILURE):
1660
            /* immediate failure */
1661
0
            TRACE(("|%p|%p|FAILURE\n", pattern, ptr));
1662
0
            RETURN_FAILURE;
1663
1664
#if !USE_COMPUTED_GOTOS
1665
        default:
1666
#endif
1667
        // Also any unused opcodes:
1668
0
        TARGET(SRE_OP_RANGE_UNI_IGNORE):
1669
0
        TARGET(SRE_OP_SUBPATTERN):
1670
0
        TARGET(SRE_OP_RANGE):
1671
0
        TARGET(SRE_OP_NEGATE):
1672
0
        TARGET(SRE_OP_BIGCHARSET):
1673
0
        TARGET(SRE_OP_CHARSET):
1674
0
            TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ptr,
1675
0
                   pattern[-1]));
1676
0
            RETURN_ERROR(SRE_ERROR_ILLEGAL);
1677
1678
0
    }
1679
1680
1.05G
exit:
1681
1.05G
    ctx_pos = ctx->last_ctx_pos;
1682
1.05G
    jump = ctx->jump;
1683
1.05G
    DATA_POP_DISCARD(ctx);
1684
1.05G
    if (ctx_pos == -1) {
1685
389M
        state->sigcount = sigcount;
1686
389M
        return ret;
1687
389M
    }
1688
661M
    DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1689
1690
661M
    switch (jump) {
1691
217M
        case JUMP_MAX_UNTIL_2:
1692
217M
            TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", pattern, ptr));
1693
217M
            goto jump_max_until_2;
1694
40.2M
        case JUMP_MAX_UNTIL_3:
1695
40.2M
            TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", pattern, ptr));
1696
40.2M
            goto jump_max_until_3;
1697
0
        case JUMP_MIN_UNTIL_2:
1698
0
            TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", pattern, ptr));
1699
0
            goto jump_min_until_2;
1700
0
        case JUMP_MIN_UNTIL_3:
1701
0
            TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", pattern, ptr));
1702
0
            goto jump_min_until_3;
1703
64.6M
        case JUMP_BRANCH:
1704
64.6M
            TRACE(("|%p|%p|JUMP_BRANCH\n", pattern, ptr));
1705
64.6M
            goto jump_branch;
1706
15.9M
        case JUMP_MAX_UNTIL_1:
1707
15.9M
            TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", pattern, ptr));
1708
15.9M
            goto jump_max_until_1;
1709
0
        case JUMP_MIN_UNTIL_1:
1710
0
            TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", pattern, ptr));
1711
0
            goto jump_min_until_1;
1712
0
        case JUMP_POSS_REPEAT_1:
1713
0
            TRACE(("|%p|%p|JUMP_POSS_REPEAT_1\n", pattern, ptr));
1714
0
            goto jump_poss_repeat_1;
1715
0
        case JUMP_POSS_REPEAT_2:
1716
0
            TRACE(("|%p|%p|JUMP_POSS_REPEAT_2\n", pattern, ptr));
1717
0
            goto jump_poss_repeat_2;
1718
48.7M
        case JUMP_REPEAT:
1719
48.7M
            TRACE(("|%p|%p|JUMP_REPEAT\n", pattern, ptr));
1720
48.7M
            goto jump_repeat;
1721
1.18M
        case JUMP_REPEAT_ONE_1:
1722
1.18M
            TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", pattern, ptr));
1723
1.18M
            goto jump_repeat_one_1;
1724
119M
        case JUMP_REPEAT_ONE_2:
1725
119M
            TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", pattern, ptr));
1726
119M
            goto jump_repeat_one_2;
1727
832
        case JUMP_MIN_REPEAT_ONE:
1728
832
            TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", pattern, ptr));
1729
832
            goto jump_min_repeat_one;
1730
0
        case JUMP_ATOMIC_GROUP:
1731
0
            TRACE(("|%p|%p|JUMP_ATOMIC_GROUP\n", pattern, ptr));
1732
0
            goto jump_atomic_group;
1733
12
        case JUMP_ASSERT:
1734
12
            TRACE(("|%p|%p|JUMP_ASSERT\n", pattern, ptr));
1735
12
            goto jump_assert;
1736
153M
        case JUMP_ASSERT_NOT:
1737
153M
            TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", pattern, ptr));
1738
153M
            goto jump_assert_not;
1739
0
        case JUMP_NONE:
1740
0
            TRACE(("|%p|%p|RETURN %zd\n", pattern,
1741
0
                   ptr, ret));
1742
0
            break;
1743
661M
    }
1744
1745
0
    return ret; /* should never get here */
1746
661M
}
sre.c:sre_ucs1_match
Line
Count
Source
663
163M
{
664
163M
    const SRE_CHAR* end = (const SRE_CHAR *)state->end;
665
163M
    Py_ssize_t alloc_pos, ctx_pos = -1;
666
163M
    Py_ssize_t ret = 0;
667
163M
    int jump;
668
163M
    unsigned int sigcount = state->sigcount;
669
670
163M
    SRE(match_context)* ctx;
671
163M
    SRE(match_context)* nextctx;
672
163M
    INIT_TRACE(state);
673
674
163M
    TRACE(("|%p|%p|ENTER\n", pattern, state->ptr));
675
676
163M
    DATA_ALLOC(SRE(match_context), ctx);
677
163M
    ctx->last_ctx_pos = -1;
678
163M
    ctx->jump = JUMP_NONE;
679
163M
    ctx->toplevel = toplevel;
680
163M
    ctx_pos = alloc_pos;
681
682
163M
#if USE_COMPUTED_GOTOS
683
163M
#include "sre_targets.h"
684
163M
#endif
685
686
457M
entrance:
687
688
457M
    ;  // Fashion statement.
689
457M
    const SRE_CHAR *ptr = (SRE_CHAR *)state->ptr;
690
691
457M
    if (pattern[0] == SRE_OP_INFO) {
692
        /* optimization info block */
693
        /* <INFO> <1=skip> <2=flags> <3=min> ... */
694
46.4M
        if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) {
695
10.4M
            TRACE(("reject (got %tu chars, need %zu)\n",
696
10.4M
                   end - ptr, (size_t) pattern[3]));
697
10.4M
            RETURN_FAILURE;
698
10.4M
        }
699
36.0M
        pattern += pattern[1] + 1;
700
36.0M
    }
701
702
447M
#if USE_COMPUTED_GOTOS
703
447M
    DISPATCH;
704
#else
705
dispatch:
706
    MAYBE_CHECK_SIGNALS;
707
    switch (*pattern++)
708
#endif
709
447M
    {
710
711
447M
        TARGET(SRE_OP_MARK):
712
            /* set mark */
713
            /* <MARK> <gid> */
714
150M
            TRACE(("|%p|%p|MARK %d\n", pattern,
715
150M
                   ptr, pattern[0]));
716
150M
            {
717
150M
                int i = pattern[0];
718
150M
                if (i & 1)
719
43.1M
                    state->lastindex = i/2 + 1;
720
150M
                if (i > state->lastmark) {
721
                    /* state->lastmark is the highest valid index in the
722
                       state->mark array.  If it is increased by more than 1,
723
                       the intervening marks must be set to NULL to signal
724
                       that these marks have not been encountered. */
725
147M
                    int j = state->lastmark + 1;
726
164M
                    while (j < i)
727
16.9M
                        state->mark[j++] = NULL;
728
147M
                    state->lastmark = i;
729
147M
                }
730
150M
                state->mark[i] = ptr;
731
150M
            }
732
150M
            pattern++;
733
150M
            DISPATCH;
734
735
150M
        TARGET(SRE_OP_LITERAL):
736
            /* match literal string */
737
            /* <LITERAL> <code> */
738
80.0M
            TRACE(("|%p|%p|LITERAL %d\n", pattern,
739
80.0M
                   ptr, *pattern));
740
80.0M
            if (ptr >= end || (SRE_CODE) ptr[0] != pattern[0])
741
39.8M
                RETURN_FAILURE;
742
40.2M
            pattern++;
743
40.2M
            ptr++;
744
40.2M
            DISPATCH;
745
746
40.2M
        TARGET(SRE_OP_NOT_LITERAL):
747
            /* match anything that is not literal character */
748
            /* <NOT_LITERAL> <code> */
749
0
            TRACE(("|%p|%p|NOT_LITERAL %d\n", pattern,
750
0
                   ptr, *pattern));
751
0
            if (ptr >= end || (SRE_CODE) ptr[0] == pattern[0])
752
0
                RETURN_FAILURE;
753
0
            pattern++;
754
0
            ptr++;
755
0
            DISPATCH;
756
757
95.8M
        TARGET(SRE_OP_SUCCESS):
758
            /* end of pattern */
759
95.8M
            TRACE(("|%p|%p|SUCCESS\n", pattern, ptr));
760
95.8M
            if (ctx->toplevel &&
761
28.7M
                ((state->match_all && ptr != state->end) ||
762
28.7M
                 (state->must_advance && ptr == state->start)))
763
200
            {
764
200
                RETURN_FAILURE;
765
200
            }
766
95.8M
            state->ptr = ptr;
767
95.8M
            RETURN_SUCCESS;
768
769
25.3M
        TARGET(SRE_OP_AT):
770
            /* match at given position */
771
            /* <AT> <code> */
772
25.3M
            TRACE(("|%p|%p|AT %d\n", pattern, ptr, *pattern));
773
25.3M
            if (!SRE(at)(state, ptr, *pattern))
774
2.38M
                RETURN_FAILURE;
775
22.9M
            pattern++;
776
22.9M
            DISPATCH;
777
778
30.0M
        TARGET(SRE_OP_CATEGORY):
779
            /* match at given category */
780
            /* <CATEGORY> <code> */
781
30.0M
            TRACE(("|%p|%p|CATEGORY %d\n", pattern,
782
30.0M
                   ptr, *pattern));
783
30.0M
            if (ptr >= end || !sre_category(pattern[0], ptr[0]))
784
1.91M
                RETURN_FAILURE;
785
28.0M
            pattern++;
786
28.0M
            ptr++;
787
28.0M
            DISPATCH;
788
789
28.0M
        TARGET(SRE_OP_ANY):
790
            /* match anything (except a newline) */
791
            /* <ANY> */
792
0
            TRACE(("|%p|%p|ANY\n", pattern, ptr));
793
0
            if (ptr >= end || SRE_IS_LINEBREAK(ptr[0]))
794
0
                RETURN_FAILURE;
795
0
            ptr++;
796
0
            DISPATCH;
797
798
0
        TARGET(SRE_OP_ANY_ALL):
799
            /* match anything */
800
            /* <ANY_ALL> */
801
0
            TRACE(("|%p|%p|ANY_ALL\n", pattern, ptr));
802
0
            if (ptr >= end)
803
0
                RETURN_FAILURE;
804
0
            ptr++;
805
0
            DISPATCH;
806
807
112M
        TARGET(SRE_OP_IN):
808
            /* match set member (or non_member) */
809
            /* <IN> <skip> <set> */
810
112M
            TRACE(("|%p|%p|IN\n", pattern, ptr));
811
112M
            if (ptr >= end ||
812
110M
                !SRE(charset)(state, pattern + 1, *ptr))
813
25.2M
                RETURN_FAILURE;
814
87.2M
            pattern += pattern[0];
815
87.2M
            ptr++;
816
87.2M
            DISPATCH;
817
818
87.2M
        TARGET(SRE_OP_LITERAL_IGNORE):
819
0
            TRACE(("|%p|%p|LITERAL_IGNORE %d\n",
820
0
                   pattern, ptr, pattern[0]));
821
0
            if (ptr >= end ||
822
0
                sre_lower_ascii(*ptr) != *pattern)
823
0
                RETURN_FAILURE;
824
0
            pattern++;
825
0
            ptr++;
826
0
            DISPATCH;
827
828
28
        TARGET(SRE_OP_LITERAL_UNI_IGNORE):
829
28
            TRACE(("|%p|%p|LITERAL_UNI_IGNORE %d\n",
830
28
                   pattern, ptr, pattern[0]));
831
28
            if (ptr >= end ||
832
28
                sre_lower_unicode(*ptr) != *pattern)
833
0
                RETURN_FAILURE;
834
28
            pattern++;
835
28
            ptr++;
836
28
            DISPATCH;
837
838
28
        TARGET(SRE_OP_LITERAL_LOC_IGNORE):
839
0
            TRACE(("|%p|%p|LITERAL_LOC_IGNORE %d\n",
840
0
                   pattern, ptr, pattern[0]));
841
0
            if (ptr >= end
842
0
                || !char_loc_ignore(*pattern, *ptr))
843
0
                RETURN_FAILURE;
844
0
            pattern++;
845
0
            ptr++;
846
0
            DISPATCH;
847
848
0
        TARGET(SRE_OP_NOT_LITERAL_IGNORE):
849
0
            TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n",
850
0
                   pattern, ptr, *pattern));
851
0
            if (ptr >= end ||
852
0
                sre_lower_ascii(*ptr) == *pattern)
853
0
                RETURN_FAILURE;
854
0
            pattern++;
855
0
            ptr++;
856
0
            DISPATCH;
857
858
0
        TARGET(SRE_OP_NOT_LITERAL_UNI_IGNORE):
859
0
            TRACE(("|%p|%p|NOT_LITERAL_UNI_IGNORE %d\n",
860
0
                   pattern, ptr, *pattern));
861
0
            if (ptr >= end ||
862
0
                sre_lower_unicode(*ptr) == *pattern)
863
0
                RETURN_FAILURE;
864
0
            pattern++;
865
0
            ptr++;
866
0
            DISPATCH;
867
868
0
        TARGET(SRE_OP_NOT_LITERAL_LOC_IGNORE):
869
0
            TRACE(("|%p|%p|NOT_LITERAL_LOC_IGNORE %d\n",
870
0
                   pattern, ptr, *pattern));
871
0
            if (ptr >= end
872
0
                || char_loc_ignore(*pattern, *ptr))
873
0
                RETURN_FAILURE;
874
0
            pattern++;
875
0
            ptr++;
876
0
            DISPATCH;
877
878
0
        TARGET(SRE_OP_IN_IGNORE):
879
0
            TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr));
880
0
            if (ptr >= end
881
0
                || !SRE(charset)(state, pattern+1,
882
0
                                 (SRE_CODE)sre_lower_ascii(*ptr)))
883
0
                RETURN_FAILURE;
884
0
            pattern += pattern[0];
885
0
            ptr++;
886
0
            DISPATCH;
887
888
24
        TARGET(SRE_OP_IN_UNI_IGNORE):
889
24
            TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ptr));
890
24
            if (ptr >= end
891
16
                || !SRE(charset)(state, pattern+1,
892
16
                                 (SRE_CODE)sre_lower_unicode(*ptr)))
893
16
                RETURN_FAILURE;
894
8
            pattern += pattern[0];
895
8
            ptr++;
896
8
            DISPATCH;
897
898
8
        TARGET(SRE_OP_IN_LOC_IGNORE):
899
0
            TRACE(("|%p|%p|IN_LOC_IGNORE\n", pattern, ptr));
900
0
            if (ptr >= end
901
0
                || !SRE(charset_loc_ignore)(state, pattern+1, *ptr))
902
0
                RETURN_FAILURE;
903
0
            pattern += pattern[0];
904
0
            ptr++;
905
0
            DISPATCH;
906
907
20.6M
        TARGET(SRE_OP_JUMP):
908
20.6M
        TARGET(SRE_OP_INFO):
909
            /* jump forward */
910
            /* <JUMP> <offset> */
911
20.6M
            TRACE(("|%p|%p|JUMP %d\n", pattern,
912
20.6M
                   ptr, pattern[0]));
913
20.6M
            pattern += pattern[0];
914
20.6M
            DISPATCH;
915
916
34.4M
        TARGET(SRE_OP_BRANCH):
917
            /* alternation */
918
            /* <BRANCH> <0=skip> code <JUMP> ... <NULL> */
919
34.4M
            TRACE(("|%p|%p|BRANCH\n", pattern, ptr));
920
34.4M
            LASTMARK_SAVE();
921
34.4M
            if (state->save_marks)
922
51.0k
                MARK_PUSH(ctx->lastmark);
923
93.7M
            for (; pattern[0]; pattern += pattern[0]) {
924
77.6M
                if (pattern[1] == SRE_OP_LITERAL &&
925
54.1M
                    (ptr >= end ||
926
54.1M
                     (SRE_CODE) *ptr != pattern[2]))
927
18.9M
                    continue;
928
58.7M
                if (pattern[1] == SRE_OP_IN &&
929
2.46M
                    (ptr >= end ||
930
2.46M
                     !SRE(charset)(state, pattern + 3,
931
2.46M
                                   (SRE_CODE) *ptr)))
932
1.42M
                    continue;
933
57.2M
                state->ptr = ptr;
934
57.2M
                DO_JUMP(JUMP_BRANCH, jump_branch, pattern+1);
935
57.2M
                if (ret) {
936
18.4M
                    if (state->save_marks)
937
39.9k
                        MARK_POP_DISCARD(ctx->lastmark);
938
18.4M
                    RETURN_ON_ERROR(ret);
939
18.4M
                    RETURN_SUCCESS;
940
18.4M
                }
941
38.8M
                if (state->save_marks)
942
24.9k
                    MARK_POP_KEEP(ctx->lastmark);
943
38.8M
                LASTMARK_RESTORE();
944
38.8M
            }
945
16.0M
            if (state->save_marks)
946
11.1k
                MARK_POP_DISCARD(ctx->lastmark);
947
16.0M
            RETURN_FAILURE;
948
949
116M
        TARGET(SRE_OP_REPEAT_ONE):
950
            /* match repeated sequence (maximizing regexp) */
951
952
            /* this operator only works if the repeated item is
953
               exactly one character wide, and we're not already
954
               collecting backtracking points.  for other cases,
955
               use the MAX_REPEAT operator */
956
957
            /* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
958
959
116M
            TRACE(("|%p|%p|REPEAT_ONE %d %d\n", pattern, ptr,
960
116M
                   pattern[1], pattern[2]));
961
962
116M
            if ((Py_ssize_t) pattern[1] > end - ptr)
963
816k
                RETURN_FAILURE; /* cannot match */
964
965
115M
            state->ptr = ptr;
966
967
115M
            ret = SRE(count)(state, pattern+3, pattern[2]);
968
115M
            RETURN_ON_ERROR(ret);
969
115M
            DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
970
115M
            ctx->count = ret;
971
115M
            ptr += ctx->count;
972
973
            /* when we arrive here, count contains the number of
974
               matches, and ptr points to the tail of the target
975
               string.  check if the rest of the pattern matches,
976
               and backtrack if not. */
977
978
115M
            if (ctx->count < (Py_ssize_t) pattern[1])
979
56.7M
                RETURN_FAILURE;
980
981
58.7M
            if (pattern[pattern[0]] == SRE_OP_SUCCESS &&
982
564k
                ptr == state->end &&
983
74.1k
                !(ctx->toplevel && state->must_advance && ptr == state->start))
984
74.1k
            {
985
                /* tail is empty.  we're finished */
986
74.1k
                state->ptr = ptr;
987
74.1k
                RETURN_SUCCESS;
988
74.1k
            }
989
990
58.6M
            LASTMARK_SAVE();
991
58.6M
            if (state->save_marks)
992
40.8M
                MARK_PUSH(ctx->lastmark);
993
994
58.6M
            if (pattern[pattern[0]] == SRE_OP_LITERAL) {
995
                /* tail starts with a literal. skip positions where
996
                   the rest of the pattern cannot possibly match */
997
2.34M
                ctx->u.chr = pattern[pattern[0]+1];
998
2.34M
                for (;;) {
999
6.99M
                    while (ctx->count >= (Py_ssize_t) pattern[1] &&
1000
5.81M
                           (ptr >= end || *ptr != ctx->u.chr)) {
1001
4.64M
                        ptr--;
1002
4.64M
                        ctx->count--;
1003
4.64M
                    }
1004
2.34M
                    if (ctx->count < (Py_ssize_t) pattern[1])
1005
1.17M
                        break;
1006
1.16M
                    state->ptr = ptr;
1007
1.16M
                    DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1,
1008
1.16M
                            pattern+pattern[0]);
1009
1.16M
                    if (ret) {
1010
1.16M
                        if (state->save_marks)
1011
0
                            MARK_POP_DISCARD(ctx->lastmark);
1012
1.16M
                        RETURN_ON_ERROR(ret);
1013
1.16M
                        RETURN_SUCCESS;
1014
1.16M
                    }
1015
16
                    if (state->save_marks)
1016
0
                        MARK_POP_KEEP(ctx->lastmark);
1017
16
                    LASTMARK_RESTORE();
1018
1019
16
                    ptr--;
1020
16
                    ctx->count--;
1021
16
                }
1022
1.17M
                if (state->save_marks)
1023
32
                    MARK_POP_DISCARD(ctx->lastmark);
1024
56.3M
            } else {
1025
                /* general case */
1026
64.9M
                while (ctx->count >= (Py_ssize_t) pattern[1]) {
1027
63.3M
                    state->ptr = ptr;
1028
63.3M
                    DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2,
1029
63.3M
                            pattern+pattern[0]);
1030
63.3M
                    if (ret) {
1031
54.7M
                        if (state->save_marks)
1032
39.7M
                            MARK_POP_DISCARD(ctx->lastmark);
1033
54.7M
                        RETURN_ON_ERROR(ret);
1034
54.7M
                        RETURN_SUCCESS;
1035
54.7M
                    }
1036
8.62M
                    if (state->save_marks)
1037
1.28M
                        MARK_POP_KEEP(ctx->lastmark);
1038
8.62M
                    LASTMARK_RESTORE();
1039
1040
8.62M
                    ptr--;
1041
8.62M
                    ctx->count--;
1042
8.62M
                }
1043
1.57M
                if (state->save_marks)
1044
1.13M
                    MARK_POP_DISCARD(ctx->lastmark);
1045
1.57M
            }
1046
2.75M
            RETURN_FAILURE;
1047
1048
16
        TARGET(SRE_OP_MIN_REPEAT_ONE):
1049
            /* match repeated sequence (minimizing regexp) */
1050
1051
            /* this operator only works if the repeated item is
1052
               exactly one character wide, and we're not already
1053
               collecting backtracking points.  for other cases,
1054
               use the MIN_REPEAT operator */
1055
1056
            /* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
1057
1058
16
            TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", pattern, ptr,
1059
16
                   pattern[1], pattern[2]));
1060
1061
16
            if ((Py_ssize_t) pattern[1] > end - ptr)
1062
0
                RETURN_FAILURE; /* cannot match */
1063
1064
16
            state->ptr = ptr;
1065
1066
16
            if (pattern[1] == 0)
1067
16
                ctx->count = 0;
1068
0
            else {
1069
                /* count using pattern min as the maximum */
1070
0
                ret = SRE(count)(state, pattern+3, pattern[1]);
1071
0
                RETURN_ON_ERROR(ret);
1072
0
                DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1073
0
                if (ret < (Py_ssize_t) pattern[1])
1074
                    /* didn't match minimum number of times */
1075
0
                    RETURN_FAILURE;
1076
                /* advance past minimum matches of repeat */
1077
0
                ctx->count = ret;
1078
0
                ptr += ctx->count;
1079
0
            }
1080
1081
16
            if (pattern[pattern[0]] == SRE_OP_SUCCESS &&
1082
0
                !(ctx->toplevel &&
1083
0
                  ((state->match_all && ptr != state->end) ||
1084
0
                   (state->must_advance && ptr == state->start))))
1085
0
            {
1086
                /* tail is empty.  we're finished */
1087
0
                state->ptr = ptr;
1088
0
                RETURN_SUCCESS;
1089
1090
16
            } else {
1091
                /* general case */
1092
16
                LASTMARK_SAVE();
1093
16
                if (state->save_marks)
1094
0
                    MARK_PUSH(ctx->lastmark);
1095
1096
832
                while ((Py_ssize_t)pattern[2] == SRE_MAXREPEAT
1097
832
                       || ctx->count <= (Py_ssize_t)pattern[2]) {
1098
832
                    state->ptr = ptr;
1099
832
                    DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one,
1100
832
                            pattern+pattern[0]);
1101
832
                    if (ret) {
1102
0
                        if (state->save_marks)
1103
0
                            MARK_POP_DISCARD(ctx->lastmark);
1104
0
                        RETURN_ON_ERROR(ret);
1105
0
                        RETURN_SUCCESS;
1106
0
                    }
1107
832
                    if (state->save_marks)
1108
0
                        MARK_POP_KEEP(ctx->lastmark);
1109
832
                    LASTMARK_RESTORE();
1110
1111
832
                    state->ptr = ptr;
1112
832
                    ret = SRE(count)(state, pattern+3, 1);
1113
832
                    RETURN_ON_ERROR(ret);
1114
832
                    DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1115
832
                    if (ret == 0)
1116
16
                        break;
1117
832
                    assert(ret == 1);
1118
816
                    ptr++;
1119
816
                    ctx->count++;
1120
816
                }
1121
16
                if (state->save_marks)
1122
0
                    MARK_POP_DISCARD(ctx->lastmark);
1123
16
            }
1124
16
            RETURN_FAILURE;
1125
1126
0
        TARGET(SRE_OP_POSSESSIVE_REPEAT_ONE):
1127
            /* match repeated sequence (maximizing regexp) without
1128
               backtracking */
1129
1130
            /* this operator only works if the repeated item is
1131
               exactly one character wide, and we're not already
1132
               collecting backtracking points.  for other cases,
1133
               use the MAX_REPEAT operator */
1134
1135
            /* <POSSESSIVE_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS>
1136
               tail */
1137
1138
0
            TRACE(("|%p|%p|POSSESSIVE_REPEAT_ONE %d %d\n", pattern,
1139
0
                   ptr, pattern[1], pattern[2]));
1140
1141
0
            if (ptr + pattern[1] > end) {
1142
0
                RETURN_FAILURE; /* cannot match */
1143
0
            }
1144
1145
0
            state->ptr = ptr;
1146
1147
0
            ret = SRE(count)(state, pattern + 3, pattern[2]);
1148
0
            RETURN_ON_ERROR(ret);
1149
0
            DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1150
0
            ctx->count = ret;
1151
0
            ptr += ctx->count;
1152
1153
            /* when we arrive here, count contains the number of
1154
               matches, and ptr points to the tail of the target
1155
               string.  check if the rest of the pattern matches,
1156
               and fail if not. */
1157
1158
            /* Test for not enough repetitions in match */
1159
0
            if (ctx->count < (Py_ssize_t) pattern[1]) {
1160
0
                RETURN_FAILURE;
1161
0
            }
1162
1163
            /* Update the pattern to point to the next op code */
1164
0
            pattern += pattern[0];
1165
1166
            /* Let the tail be evaluated separately and consider this
1167
               match successful. */
1168
0
            if (*pattern == SRE_OP_SUCCESS &&
1169
0
                ptr == state->end &&
1170
0
                !(ctx->toplevel && state->must_advance && ptr == state->start))
1171
0
            {
1172
                /* tail is empty.  we're finished */
1173
0
                state->ptr = ptr;
1174
0
                RETURN_SUCCESS;
1175
0
            }
1176
1177
            /* Attempt to match the rest of the string */
1178
0
            DISPATCH;
1179
1180
33.0M
        TARGET(SRE_OP_REPEAT):
1181
            /* create repeat context.  all the hard work is done
1182
               by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */
1183
            /* <REPEAT> <skip> <1=min> <2=max>
1184
               <3=repeat_index> item <UNTIL> tail */
1185
33.0M
            TRACE(("|%p|%p|REPEAT %d %d\n", pattern, ptr,
1186
33.0M
                   pattern[1], pattern[2]));
1187
1188
            /* install new repeat context */
1189
33.0M
            ctx->u.rep = repeat_pool_malloc(state);
1190
33.0M
            if (!ctx->u.rep) {
1191
0
                RETURN_ERROR(SRE_ERROR_MEMORY);
1192
0
            }
1193
33.0M
            ctx->u.rep->count = -1;
1194
33.0M
            ctx->u.rep->pattern = pattern;
1195
33.0M
            ctx->u.rep->prev = state->repeat;
1196
33.0M
            ctx->u.rep->last_ptr = NULL;
1197
33.0M
            state->repeat = ctx->u.rep;
1198
33.0M
            state->save_marks++;
1199
1200
33.0M
            state->ptr = ptr;
1201
33.0M
            DO_JUMP(JUMP_REPEAT, jump_repeat, pattern+pattern[0]);
1202
33.0M
            state->repeat = ctx->u.rep->prev;
1203
33.0M
            state->save_marks--;
1204
33.0M
            repeat_pool_free(state, ctx->u.rep);
1205
1206
33.0M
            if (ret) {
1207
26.1M
                RETURN_ON_ERROR(ret);
1208
26.1M
                RETURN_SUCCESS;
1209
26.1M
            }
1210
6.90M
            RETURN_FAILURE;
1211
1212
86.1M
        TARGET(SRE_OP_MAX_UNTIL):
1213
            /* maximizing repeat */
1214
            /* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */
1215
1216
            /* FIXME: we probably need to deal with zero-width
1217
               matches in here... */
1218
1219
86.1M
            ctx->u.rep = state->repeat;
1220
86.1M
            if (!ctx->u.rep)
1221
0
                RETURN_ERROR(SRE_ERROR_STATE);
1222
1223
86.1M
            state->ptr = ptr;
1224
1225
86.1M
            ctx->count = ctx->u.rep->count+1;
1226
1227
86.1M
            TRACE(("|%p|%p|MAX_UNTIL %zd\n", pattern,
1228
86.1M
                   ptr, ctx->count));
1229
1230
86.1M
            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
1231
                /* not enough matches */
1232
7.53M
                ctx->u.rep->count = ctx->count;
1233
7.53M
                DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
1234
7.53M
                        ctx->u.rep->pattern+3);
1235
7.53M
                if (ret) {
1236
730k
                    RETURN_ON_ERROR(ret);
1237
730k
                    RETURN_SUCCESS;
1238
730k
                }
1239
6.80M
                ctx->u.rep->count = ctx->count-1;
1240
6.80M
                state->ptr = ptr;
1241
6.80M
                RETURN_FAILURE;
1242
6.80M
            }
1243
1244
78.5M
            if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] ||
1245
9.17M
                ctx->u.rep->pattern[2] == SRE_MAXREPEAT) &&
1246
69.4M
                state->ptr != ctx->u.rep->last_ptr) {
1247
                /* we may have enough matches, but if we can
1248
                   match another item, do so */
1249
69.4M
                ctx->u.rep->count = ctx->count;
1250
69.4M
                LASTMARK_SAVE();
1251
69.4M
                MARK_PUSH(ctx->lastmark);
1252
                /* zero-width match protection */
1253
69.4M
                LAST_PTR_PUSH();
1254
69.4M
                ctx->u.rep->last_ptr = state->ptr;
1255
69.4M
                DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
1256
69.4M
                        ctx->u.rep->pattern+3);
1257
69.4M
                LAST_PTR_POP();
1258
69.4M
                if (ret) {
1259
51.9M
                    MARK_POP_DISCARD(ctx->lastmark);
1260
51.9M
                    RETURN_ON_ERROR(ret);
1261
51.9M
                    RETURN_SUCCESS;
1262
51.9M
                }
1263
17.4M
                MARK_POP(ctx->lastmark);
1264
17.4M
                LASTMARK_RESTORE();
1265
17.4M
                ctx->u.rep->count = ctx->count-1;
1266
17.4M
                state->ptr = ptr;
1267
17.4M
            }
1268
1269
            /* cannot match more repeated items here.  make sure the
1270
               tail matches */
1271
26.6M
            state->repeat = ctx->u.rep->prev;
1272
26.6M
            state->save_marks--;
1273
26.6M
            DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, pattern);
1274
26.6M
            state->repeat = ctx->u.rep; // restore repeat before return
1275
26.6M
            state->save_marks++;
1276
1277
26.6M
            RETURN_ON_SUCCESS(ret);
1278
433k
            state->ptr = ptr;
1279
433k
            RETURN_FAILURE;
1280
1281
0
        TARGET(SRE_OP_MIN_UNTIL):
1282
            /* minimizing repeat */
1283
            /* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */
1284
1285
0
            ctx->u.rep = state->repeat;
1286
0
            if (!ctx->u.rep)
1287
0
                RETURN_ERROR(SRE_ERROR_STATE);
1288
1289
0
            state->ptr = ptr;
1290
1291
0
            ctx->count = ctx->u.rep->count+1;
1292
1293
0
            TRACE(("|%p|%p|MIN_UNTIL %zd %p\n", pattern,
1294
0
                   ptr, ctx->count, ctx->u.rep->pattern));
1295
1296
0
            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
1297
                /* not enough matches */
1298
0
                ctx->u.rep->count = ctx->count;
1299
0
                DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
1300
0
                        ctx->u.rep->pattern+3);
1301
0
                if (ret) {
1302
0
                    RETURN_ON_ERROR(ret);
1303
0
                    RETURN_SUCCESS;
1304
0
                }
1305
0
                ctx->u.rep->count = ctx->count-1;
1306
0
                state->ptr = ptr;
1307
0
                RETURN_FAILURE;
1308
0
            }
1309
1310
            /* see if the tail matches */
1311
0
            state->repeat = ctx->u.rep->prev;
1312
0
            state->save_marks--;
1313
1314
0
            LASTMARK_SAVE();
1315
0
            if (state->save_marks)
1316
0
                MARK_PUSH(ctx->lastmark);
1317
1318
0
            DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, pattern);
1319
            /* save_marks is balanced across the jump, so this equals the
1320
               value tested for MARK_PUSH above */
1321
0
            int pushed = state->save_marks != 0;
1322
0
            state->repeat = ctx->u.rep; // restore repeat before return
1323
0
            state->save_marks++;
1324
1325
0
            if (ret) {
1326
0
                if (pushed)
1327
0
                    MARK_POP_DISCARD(ctx->lastmark);
1328
0
                RETURN_ON_ERROR(ret);
1329
0
                RETURN_SUCCESS;
1330
0
            }
1331
0
            if (pushed)
1332
0
                MARK_POP(ctx->lastmark);
1333
0
            LASTMARK_RESTORE();
1334
1335
0
            state->ptr = ptr;
1336
1337
0
            if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2]
1338
0
                && ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
1339
0
                state->ptr == ctx->u.rep->last_ptr)
1340
0
                RETURN_FAILURE;
1341
1342
0
            ctx->u.rep->count = ctx->count;
1343
            /* zero-width match protection */
1344
0
            LAST_PTR_PUSH();
1345
0
            ctx->u.rep->last_ptr = state->ptr;
1346
0
            DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
1347
0
                    ctx->u.rep->pattern+3);
1348
0
            LAST_PTR_POP();
1349
0
            if (ret) {
1350
0
                RETURN_ON_ERROR(ret);
1351
0
                RETURN_SUCCESS;
1352
0
            }
1353
0
            ctx->u.rep->count = ctx->count-1;
1354
0
            state->ptr = ptr;
1355
0
            RETURN_FAILURE;
1356
1357
0
        TARGET(SRE_OP_POSSESSIVE_REPEAT):
1358
            /* create possessive repeat contexts. */
1359
            /* <POSSESSIVE_REPEAT> <skip> <1=min> <2=max> pattern
1360
               <SUCCESS> tail */
1361
0
            TRACE(("|%p|%p|POSSESSIVE_REPEAT %d %d\n", pattern,
1362
0
                   ptr, pattern[1], pattern[2]));
1363
1364
            /* Set the global Input pointer to this context's Input
1365
               pointer */
1366
0
            state->ptr = ptr;
1367
1368
            /* Capture groups in the body can be revisited on backtracking
1369
               between iterations, so their marks must be saved and restored,
1370
               as is done inside a repeat. */
1371
0
            state->save_marks++;
1372
1373
            /* Initialize Count to 0 */
1374
0
            ctx->count = 0;
1375
1376
            /* Check for minimum required matches. */
1377
0
            while (ctx->count < (Py_ssize_t)pattern[1]) {
1378
                /* not enough matches */
1379
0
                DO_JUMP0(JUMP_POSS_REPEAT_1, jump_poss_repeat_1,
1380
0
                         &pattern[3]);
1381
0
                if (ret) {
1382
0
                    RETURN_ON_ERROR(ret);
1383
0
                    ctx->count++;
1384
0
                }
1385
0
                else {
1386
0
                    state->ptr = ptr;
1387
0
                    state->save_marks--;
1388
0
                    RETURN_FAILURE;
1389
0
                }
1390
0
            }
1391
1392
            /* Clear the context's Input stream pointer so that it
1393
               doesn't match the global state so that the while loop can
1394
               be entered. */
1395
0
            ptr = NULL;
1396
1397
            /* Keep trying to parse the <pattern> sub-pattern until the
1398
               end is reached, creating a new context each time. */
1399
0
            while ((ctx->count < (Py_ssize_t)pattern[2] ||
1400
0
                    (Py_ssize_t)pattern[2] == SRE_MAXREPEAT) &&
1401
0
                   state->ptr != ptr) {
1402
                /* Save the Capture Group Marker state into the current
1403
                   Context and back up the current highest number
1404
                   Capture Group marker. */
1405
0
                LASTMARK_SAVE();
1406
0
                MARK_PUSH(ctx->lastmark);
1407
1408
                /* zero-width match protection */
1409
                /* Set the context's Input Stream pointer to be the
1410
                   current Input Stream pointer from the global
1411
                   state.  When the loop reaches the next iteration,
1412
                   the context will then store the last known good
1413
                   position with the global state holding the Input
1414
                   Input Stream position that has been updated with
1415
                   the most recent match.  Thus, if state's Input
1416
                   stream remains the same as the one stored in the
1417
                   current Context, we know we have successfully
1418
                   matched an empty string and that all subsequent
1419
                   matches will also be the empty string until the
1420
                   maximum number of matches are counted, and because
1421
                   of this, we could immediately stop at that point and
1422
                   consider this match successful. */
1423
0
                ptr = state->ptr;
1424
1425
                /* We have not reached the maximin matches, so try to
1426
                   match once more. */
1427
0
                DO_JUMP0(JUMP_POSS_REPEAT_2, jump_poss_repeat_2,
1428
0
                         &pattern[3]);
1429
1430
                /* Check to see if the last attempted match
1431
                   succeeded. */
1432
0
                if (ret) {
1433
                    /* Drop the saved highest number Capture Group
1434
                       marker saved above and use the newly updated
1435
                       value. */
1436
0
                    MARK_POP_DISCARD(ctx->lastmark);
1437
0
                    RETURN_ON_ERROR(ret);
1438
1439
                    /* Success, increment the count. */
1440
0
                    ctx->count++;
1441
0
                }
1442
                /* Last attempted match failed. */
1443
0
                else {
1444
                    /* Restore the previously saved highest number
1445
                       Capture Group marker since the last iteration
1446
                       did not match, then restore that to the global
1447
                       state. */
1448
0
                    MARK_POP(ctx->lastmark);
1449
0
                    LASTMARK_RESTORE();
1450
1451
                    /* Restore the global Input Stream pointer
1452
                       since it can change after jumps. */
1453
0
                    state->ptr = ptr;
1454
1455
                    /* We have sufficient matches, so exit loop. */
1456
0
                    break;
1457
0
                }
1458
0
            }
1459
1460
0
            state->save_marks--;
1461
1462
            /* Evaluate Tail */
1463
            /* Jump to end of pattern indicated by skip, and then skip
1464
               the SUCCESS op code that follows it. */
1465
0
            pattern += pattern[0] + 1;
1466
0
            ptr = state->ptr;
1467
0
            DISPATCH;
1468
1469
0
        TARGET(SRE_OP_ATOMIC_GROUP):
1470
            /* Atomic Group Sub Pattern */
1471
            /* <ATOMIC_GROUP> <skip> pattern <SUCCESS> tail */
1472
0
            TRACE(("|%p|%p|ATOMIC_GROUP\n", pattern, ptr));
1473
1474
            /* Set the global Input pointer to this context's Input
1475
               pointer */
1476
0
            state->ptr = ptr;
1477
1478
            /* Evaluate the Atomic Group in a new context, terminating
1479
               when the end of the group, represented by a SUCCESS op
1480
               code, is reached. */
1481
            /* Group Pattern begins at an offset of 1 code. */
1482
0
            DO_JUMP0(JUMP_ATOMIC_GROUP, jump_atomic_group,
1483
0
                     &pattern[1]);
1484
1485
            /* Test Exit Condition */
1486
0
            RETURN_ON_ERROR(ret);
1487
1488
0
            if (ret == 0) {
1489
                /* Atomic Group failed to Match. */
1490
0
                state->ptr = ptr;
1491
0
                RETURN_FAILURE;
1492
0
            }
1493
1494
            /* Evaluate Tail */
1495
            /* Jump to end of pattern indicated by skip, and then skip
1496
               the SUCCESS op code that follows it. */
1497
0
            pattern += pattern[0];
1498
0
            ptr = state->ptr;
1499
0
            DISPATCH;
1500
1501
0
        TARGET(SRE_OP_GROUPREF):
1502
            /* match backreference */
1503
0
            TRACE(("|%p|%p|GROUPREF %d\n", pattern,
1504
0
                   ptr, pattern[0]));
1505
0
            {
1506
0
                int groupref = pattern[0] * 2;
1507
0
                if (groupref >= state->lastmark) {
1508
0
                    RETURN_FAILURE;
1509
0
                } else {
1510
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1511
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1512
0
                    if (!p || !e || e < p)
1513
0
                        RETURN_FAILURE;
1514
0
                    while (p < e) {
1515
0
                        if (ptr >= end || *ptr != *p)
1516
0
                            RETURN_FAILURE;
1517
0
                        p++;
1518
0
                        ptr++;
1519
0
                    }
1520
0
                }
1521
0
            }
1522
0
            pattern++;
1523
0
            DISPATCH;
1524
1525
0
        TARGET(SRE_OP_GROUPREF_IGNORE):
1526
            /* match backreference */
1527
0
            TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", pattern,
1528
0
                   ptr, pattern[0]));
1529
0
            {
1530
0
                int groupref = pattern[0] * 2;
1531
0
                if (groupref >= state->lastmark) {
1532
0
                    RETURN_FAILURE;
1533
0
                } else {
1534
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1535
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1536
0
                    if (!p || !e || e < p)
1537
0
                        RETURN_FAILURE;
1538
0
                    while (p < e) {
1539
0
                        if (ptr >= end ||
1540
0
                            sre_lower_ascii(*ptr) != sre_lower_ascii(*p))
1541
0
                            RETURN_FAILURE;
1542
0
                        p++;
1543
0
                        ptr++;
1544
0
                    }
1545
0
                }
1546
0
            }
1547
0
            pattern++;
1548
0
            DISPATCH;
1549
1550
0
        TARGET(SRE_OP_GROUPREF_UNI_IGNORE):
1551
            /* match backreference */
1552
0
            TRACE(("|%p|%p|GROUPREF_UNI_IGNORE %d\n", pattern,
1553
0
                   ptr, pattern[0]));
1554
0
            {
1555
0
                int groupref = pattern[0] * 2;
1556
0
                if (groupref >= state->lastmark) {
1557
0
                    RETURN_FAILURE;
1558
0
                } else {
1559
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1560
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1561
0
                    if (!p || !e || e < p)
1562
0
                        RETURN_FAILURE;
1563
0
                    while (p < e) {
1564
0
                        if (ptr >= end ||
1565
0
                            sre_lower_unicode(*ptr) != sre_lower_unicode(*p))
1566
0
                            RETURN_FAILURE;
1567
0
                        p++;
1568
0
                        ptr++;
1569
0
                    }
1570
0
                }
1571
0
            }
1572
0
            pattern++;
1573
0
            DISPATCH;
1574
1575
0
        TARGET(SRE_OP_GROUPREF_LOC_IGNORE):
1576
            /* match backreference */
1577
0
            TRACE(("|%p|%p|GROUPREF_LOC_IGNORE %d\n", pattern,
1578
0
                   ptr, pattern[0]));
1579
0
            {
1580
0
                int groupref = pattern[0] * 2;
1581
0
                if (groupref >= state->lastmark) {
1582
0
                    RETURN_FAILURE;
1583
0
                } else {
1584
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1585
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1586
0
                    if (!p || !e || e < p)
1587
0
                        RETURN_FAILURE;
1588
0
                    while (p < e) {
1589
0
                        if (ptr >= end ||
1590
0
                            sre_lower_locale(*ptr) != sre_lower_locale(*p))
1591
0
                            RETURN_FAILURE;
1592
0
                        p++;
1593
0
                        ptr++;
1594
0
                    }
1595
0
                }
1596
0
            }
1597
0
            pattern++;
1598
0
            DISPATCH;
1599
1600
0
        TARGET(SRE_OP_GROUPREF_EXISTS):
1601
0
            TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", pattern,
1602
0
                   ptr, pattern[0]));
1603
            /* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */
1604
0
            {
1605
0
                int groupref = pattern[0] * 2;
1606
0
                if (groupref >= state->lastmark) {
1607
0
                    pattern += pattern[1];
1608
0
                    DISPATCH;
1609
0
                } else {
1610
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1611
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1612
0
                    if (!p || !e || e < p) {
1613
0
                        pattern += pattern[1];
1614
0
                        DISPATCH;
1615
0
                    }
1616
0
                }
1617
0
            }
1618
0
            pattern += 2;
1619
0
            DISPATCH;
1620
1621
12
        TARGET(SRE_OP_ASSERT):
1622
            /* assert subpattern */
1623
            /* <ASSERT> <skip> <back> <pattern> */
1624
12
            TRACE(("|%p|%p|ASSERT %d\n", pattern,
1625
12
                   ptr, pattern[1]));
1626
12
            if ((uintptr_t)(ptr - (SRE_CHAR *)state->beginning) < pattern[1])
1627
0
                RETURN_FAILURE;
1628
12
            state->ptr = ptr - pattern[1];
1629
12
            DO_JUMP0(JUMP_ASSERT, jump_assert, pattern+2);
1630
12
            RETURN_ON_FAILURE(ret);
1631
0
            pattern += pattern[0];
1632
0
            DISPATCH;
1633
1634
35.5M
        TARGET(SRE_OP_ASSERT_NOT):
1635
            /* assert not subpattern */
1636
            /* <ASSERT_NOT> <skip> <back> <pattern> */
1637
35.5M
            TRACE(("|%p|%p|ASSERT_NOT %d\n", pattern,
1638
35.5M
                   ptr, pattern[1]));
1639
35.5M
            if ((uintptr_t)(ptr - (SRE_CHAR *)state->beginning) >= pattern[1]) {
1640
35.5M
                state->ptr = ptr - pattern[1];
1641
35.5M
                LASTMARK_SAVE();
1642
35.5M
                if (state->save_marks)
1643
35.5M
                    MARK_PUSH(ctx->lastmark);
1644
1645
71.0M
                DO_JUMP0(JUMP_ASSERT_NOT, jump_assert_not, pattern+2);
1646
71.0M
                if (ret) {
1647
12.2M
                    if (state->save_marks)
1648
12.2M
                        MARK_POP_DISCARD(ctx->lastmark);
1649
12.2M
                    RETURN_ON_ERROR(ret);
1650
12.2M
                    RETURN_FAILURE;
1651
12.2M
                }
1652
23.2M
                if (state->save_marks)
1653
23.2M
                    MARK_POP(ctx->lastmark);
1654
23.2M
                LASTMARK_RESTORE();
1655
23.2M
            }
1656
23.2M
            pattern += pattern[0];
1657
23.2M
            DISPATCH;
1658
1659
23.2M
        TARGET(SRE_OP_FAILURE):
1660
            /* immediate failure */
1661
0
            TRACE(("|%p|%p|FAILURE\n", pattern, ptr));
1662
0
            RETURN_FAILURE;
1663
1664
#if !USE_COMPUTED_GOTOS
1665
        default:
1666
#endif
1667
        // Also any unused opcodes:
1668
0
        TARGET(SRE_OP_RANGE_UNI_IGNORE):
1669
0
        TARGET(SRE_OP_SUBPATTERN):
1670
0
        TARGET(SRE_OP_RANGE):
1671
0
        TARGET(SRE_OP_NEGATE):
1672
0
        TARGET(SRE_OP_BIGCHARSET):
1673
0
        TARGET(SRE_OP_CHARSET):
1674
0
            TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ptr,
1675
0
                   pattern[-1]));
1676
0
            RETURN_ERROR(SRE_ERROR_ILLEGAL);
1677
1678
0
    }
1679
1680
457M
exit:
1681
457M
    ctx_pos = ctx->last_ctx_pos;
1682
457M
    jump = ctx->jump;
1683
457M
    DATA_POP_DISCARD(ctx);
1684
457M
    if (ctx_pos == -1) {
1685
163M
        state->sigcount = sigcount;
1686
163M
        return ret;
1687
163M
    }
1688
293M
    DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1689
1690
293M
    switch (jump) {
1691
69.4M
        case JUMP_MAX_UNTIL_2:
1692
69.4M
            TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", pattern, ptr));
1693
69.4M
            goto jump_max_until_2;
1694
26.6M
        case JUMP_MAX_UNTIL_3:
1695
26.6M
            TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", pattern, ptr));
1696
26.6M
            goto jump_max_until_3;
1697
0
        case JUMP_MIN_UNTIL_2:
1698
0
            TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", pattern, ptr));
1699
0
            goto jump_min_until_2;
1700
0
        case JUMP_MIN_UNTIL_3:
1701
0
            TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", pattern, ptr));
1702
0
            goto jump_min_until_3;
1703
57.2M
        case JUMP_BRANCH:
1704
57.2M
            TRACE(("|%p|%p|JUMP_BRANCH\n", pattern, ptr));
1705
57.2M
            goto jump_branch;
1706
7.53M
        case JUMP_MAX_UNTIL_1:
1707
7.53M
            TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", pattern, ptr));
1708
7.53M
            goto jump_max_until_1;
1709
0
        case JUMP_MIN_UNTIL_1:
1710
0
            TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", pattern, ptr));
1711
0
            goto jump_min_until_1;
1712
0
        case JUMP_POSS_REPEAT_1:
1713
0
            TRACE(("|%p|%p|JUMP_POSS_REPEAT_1\n", pattern, ptr));
1714
0
            goto jump_poss_repeat_1;
1715
0
        case JUMP_POSS_REPEAT_2:
1716
0
            TRACE(("|%p|%p|JUMP_POSS_REPEAT_2\n", pattern, ptr));
1717
0
            goto jump_poss_repeat_2;
1718
33.0M
        case JUMP_REPEAT:
1719
33.0M
            TRACE(("|%p|%p|JUMP_REPEAT\n", pattern, ptr));
1720
33.0M
            goto jump_repeat;
1721
1.16M
        case JUMP_REPEAT_ONE_1:
1722
1.16M
            TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", pattern, ptr));
1723
1.16M
            goto jump_repeat_one_1;
1724
63.3M
        case JUMP_REPEAT_ONE_2:
1725
63.3M
            TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", pattern, ptr));
1726
63.3M
            goto jump_repeat_one_2;
1727
832
        case JUMP_MIN_REPEAT_ONE:
1728
832
            TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", pattern, ptr));
1729
832
            goto jump_min_repeat_one;
1730
0
        case JUMP_ATOMIC_GROUP:
1731
0
            TRACE(("|%p|%p|JUMP_ATOMIC_GROUP\n", pattern, ptr));
1732
0
            goto jump_atomic_group;
1733
12
        case JUMP_ASSERT:
1734
12
            TRACE(("|%p|%p|JUMP_ASSERT\n", pattern, ptr));
1735
12
            goto jump_assert;
1736
35.5M
        case JUMP_ASSERT_NOT:
1737
35.5M
            TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", pattern, ptr));
1738
35.5M
            goto jump_assert_not;
1739
0
        case JUMP_NONE:
1740
0
            TRACE(("|%p|%p|RETURN %zd\n", pattern,
1741
0
                   ptr, ret));
1742
0
            break;
1743
293M
    }
1744
1745
0
    return ret; /* should never get here */
1746
293M
}
sre.c:sre_ucs2_match
Line
Count
Source
663
187M
{
664
187M
    const SRE_CHAR* end = (const SRE_CHAR *)state->end;
665
187M
    Py_ssize_t alloc_pos, ctx_pos = -1;
666
187M
    Py_ssize_t ret = 0;
667
187M
    int jump;
668
187M
    unsigned int sigcount = state->sigcount;
669
670
187M
    SRE(match_context)* ctx;
671
187M
    SRE(match_context)* nextctx;
672
187M
    INIT_TRACE(state);
673
674
187M
    TRACE(("|%p|%p|ENTER\n", pattern, state->ptr));
675
676
187M
    DATA_ALLOC(SRE(match_context), ctx);
677
187M
    ctx->last_ctx_pos = -1;
678
187M
    ctx->jump = JUMP_NONE;
679
187M
    ctx->toplevel = toplevel;
680
187M
    ctx_pos = alloc_pos;
681
682
187M
#if USE_COMPUTED_GOTOS
683
187M
#include "sre_targets.h"
684
187M
#endif
685
686
444M
entrance:
687
688
444M
    ;  // Fashion statement.
689
444M
    const SRE_CHAR *ptr = (SRE_CHAR *)state->ptr;
690
691
444M
    if (pattern[0] == SRE_OP_INFO) {
692
        /* optimization info block */
693
        /* <INFO> <1=skip> <2=flags> <3=min> ... */
694
7.05M
        if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) {
695
144k
            TRACE(("reject (got %tu chars, need %zu)\n",
696
144k
                   end - ptr, (size_t) pattern[3]));
697
144k
            RETURN_FAILURE;
698
144k
        }
699
6.91M
        pattern += pattern[1] + 1;
700
6.91M
    }
701
702
444M
#if USE_COMPUTED_GOTOS
703
444M
    DISPATCH;
704
#else
705
dispatch:
706
    MAYBE_CHECK_SIGNALS;
707
    switch (*pattern++)
708
#endif
709
444M
    {
710
711
444M
        TARGET(SRE_OP_MARK):
712
            /* set mark */
713
            /* <MARK> <gid> */
714
158M
            TRACE(("|%p|%p|MARK %d\n", pattern,
715
158M
                   ptr, pattern[0]));
716
158M
            {
717
158M
                int i = pattern[0];
718
158M
                if (i & 1)
719
16.0M
                    state->lastindex = i/2 + 1;
720
158M
                if (i > state->lastmark) {
721
                    /* state->lastmark is the highest valid index in the
722
                       state->mark array.  If it is increased by more than 1,
723
                       the intervening marks must be set to NULL to signal
724
                       that these marks have not been encountered. */
725
158M
                    int j = state->lastmark + 1;
726
163M
                    while (j < i)
727
4.33M
                        state->mark[j++] = NULL;
728
158M
                    state->lastmark = i;
729
158M
                }
730
158M
                state->mark[i] = ptr;
731
158M
            }
732
158M
            pattern++;
733
158M
            DISPATCH;
734
735
158M
        TARGET(SRE_OP_LITERAL):
736
            /* match literal string */
737
            /* <LITERAL> <code> */
738
19.7M
            TRACE(("|%p|%p|LITERAL %d\n", pattern,
739
19.7M
                   ptr, *pattern));
740
19.7M
            if (ptr >= end || (SRE_CODE) ptr[0] != pattern[0])
741
18.6M
                RETURN_FAILURE;
742
1.14M
            pattern++;
743
1.14M
            ptr++;
744
1.14M
            DISPATCH;
745
746
1.14M
        TARGET(SRE_OP_NOT_LITERAL):
747
            /* match anything that is not literal character */
748
            /* <NOT_LITERAL> <code> */
749
0
            TRACE(("|%p|%p|NOT_LITERAL %d\n", pattern,
750
0
                   ptr, *pattern));
751
0
            if (ptr >= end || (SRE_CODE) ptr[0] == pattern[0])
752
0
                RETURN_FAILURE;
753
0
            pattern++;
754
0
            ptr++;
755
0
            DISPATCH;
756
757
50.8M
        TARGET(SRE_OP_SUCCESS):
758
            /* end of pattern */
759
50.8M
            TRACE(("|%p|%p|SUCCESS\n", pattern, ptr));
760
50.8M
            if (ctx->toplevel &&
761
4.38M
                ((state->match_all && ptr != state->end) ||
762
4.38M
                 (state->must_advance && ptr == state->start)))
763
0
            {
764
0
                RETURN_FAILURE;
765
0
            }
766
50.8M
            state->ptr = ptr;
767
50.8M
            RETURN_SUCCESS;
768
769
4.76M
        TARGET(SRE_OP_AT):
770
            /* match at given position */
771
            /* <AT> <code> */
772
4.76M
            TRACE(("|%p|%p|AT %d\n", pattern, ptr, *pattern));
773
4.76M
            if (!SRE(at)(state, ptr, *pattern))
774
522k
                RETURN_FAILURE;
775
4.23M
            pattern++;
776
4.23M
            DISPATCH;
777
778
89.7M
        TARGET(SRE_OP_CATEGORY):
779
            /* match at given category */
780
            /* <CATEGORY> <code> */
781
89.7M
            TRACE(("|%p|%p|CATEGORY %d\n", pattern,
782
89.7M
                   ptr, *pattern));
783
89.7M
            if (ptr >= end || !sre_category(pattern[0], ptr[0]))
784
6.30M
                RETURN_FAILURE;
785
83.4M
            pattern++;
786
83.4M
            ptr++;
787
83.4M
            DISPATCH;
788
789
83.4M
        TARGET(SRE_OP_ANY):
790
            /* match anything (except a newline) */
791
            /* <ANY> */
792
0
            TRACE(("|%p|%p|ANY\n", pattern, ptr));
793
0
            if (ptr >= end || SRE_IS_LINEBREAK(ptr[0]))
794
0
                RETURN_FAILURE;
795
0
            ptr++;
796
0
            DISPATCH;
797
798
0
        TARGET(SRE_OP_ANY_ALL):
799
            /* match anything */
800
            /* <ANY_ALL> */
801
0
            TRACE(("|%p|%p|ANY_ALL\n", pattern, ptr));
802
0
            if (ptr >= end)
803
0
                RETURN_FAILURE;
804
0
            ptr++;
805
0
            DISPATCH;
806
807
148M
        TARGET(SRE_OP_IN):
808
            /* match set member (or non_member) */
809
            /* <IN> <skip> <set> */
810
148M
            TRACE(("|%p|%p|IN\n", pattern, ptr));
811
148M
            if (ptr >= end ||
812
143M
                !SRE(charset)(state, pattern + 1, *ptr))
813
90.0M
                RETURN_FAILURE;
814
58.8M
            pattern += pattern[0];
815
58.8M
            ptr++;
816
58.8M
            DISPATCH;
817
818
58.8M
        TARGET(SRE_OP_LITERAL_IGNORE):
819
0
            TRACE(("|%p|%p|LITERAL_IGNORE %d\n",
820
0
                   pattern, ptr, pattern[0]));
821
0
            if (ptr >= end ||
822
0
                sre_lower_ascii(*ptr) != *pattern)
823
0
                RETURN_FAILURE;
824
0
            pattern++;
825
0
            ptr++;
826
0
            DISPATCH;
827
828
0
        TARGET(SRE_OP_LITERAL_UNI_IGNORE):
829
0
            TRACE(("|%p|%p|LITERAL_UNI_IGNORE %d\n",
830
0
                   pattern, ptr, pattern[0]));
831
0
            if (ptr >= end ||
832
0
                sre_lower_unicode(*ptr) != *pattern)
833
0
                RETURN_FAILURE;
834
0
            pattern++;
835
0
            ptr++;
836
0
            DISPATCH;
837
838
0
        TARGET(SRE_OP_LITERAL_LOC_IGNORE):
839
0
            TRACE(("|%p|%p|LITERAL_LOC_IGNORE %d\n",
840
0
                   pattern, ptr, pattern[0]));
841
0
            if (ptr >= end
842
0
                || !char_loc_ignore(*pattern, *ptr))
843
0
                RETURN_FAILURE;
844
0
            pattern++;
845
0
            ptr++;
846
0
            DISPATCH;
847
848
0
        TARGET(SRE_OP_NOT_LITERAL_IGNORE):
849
0
            TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n",
850
0
                   pattern, ptr, *pattern));
851
0
            if (ptr >= end ||
852
0
                sre_lower_ascii(*ptr) == *pattern)
853
0
                RETURN_FAILURE;
854
0
            pattern++;
855
0
            ptr++;
856
0
            DISPATCH;
857
858
0
        TARGET(SRE_OP_NOT_LITERAL_UNI_IGNORE):
859
0
            TRACE(("|%p|%p|NOT_LITERAL_UNI_IGNORE %d\n",
860
0
                   pattern, ptr, *pattern));
861
0
            if (ptr >= end ||
862
0
                sre_lower_unicode(*ptr) == *pattern)
863
0
                RETURN_FAILURE;
864
0
            pattern++;
865
0
            ptr++;
866
0
            DISPATCH;
867
868
0
        TARGET(SRE_OP_NOT_LITERAL_LOC_IGNORE):
869
0
            TRACE(("|%p|%p|NOT_LITERAL_LOC_IGNORE %d\n",
870
0
                   pattern, ptr, *pattern));
871
0
            if (ptr >= end
872
0
                || char_loc_ignore(*pattern, *ptr))
873
0
                RETURN_FAILURE;
874
0
            pattern++;
875
0
            ptr++;
876
0
            DISPATCH;
877
878
0
        TARGET(SRE_OP_IN_IGNORE):
879
0
            TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr));
880
0
            if (ptr >= end
881
0
                || !SRE(charset)(state, pattern+1,
882
0
                                 (SRE_CODE)sre_lower_ascii(*ptr)))
883
0
                RETURN_FAILURE;
884
0
            pattern += pattern[0];
885
0
            ptr++;
886
0
            DISPATCH;
887
888
0
        TARGET(SRE_OP_IN_UNI_IGNORE):
889
0
            TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ptr));
890
0
            if (ptr >= end
891
0
                || !SRE(charset)(state, pattern+1,
892
0
                                 (SRE_CODE)sre_lower_unicode(*ptr)))
893
0
                RETURN_FAILURE;
894
0
            pattern += pattern[0];
895
0
            ptr++;
896
0
            DISPATCH;
897
898
0
        TARGET(SRE_OP_IN_LOC_IGNORE):
899
0
            TRACE(("|%p|%p|IN_LOC_IGNORE\n", pattern, ptr));
900
0
            if (ptr >= end
901
0
                || !SRE(charset_loc_ignore)(state, pattern+1, *ptr))
902
0
                RETURN_FAILURE;
903
0
            pattern += pattern[0];
904
0
            ptr++;
905
0
            DISPATCH;
906
907
1.19M
        TARGET(SRE_OP_JUMP):
908
1.19M
        TARGET(SRE_OP_INFO):
909
            /* jump forward */
910
            /* <JUMP> <offset> */
911
1.19M
            TRACE(("|%p|%p|JUMP %d\n", pattern,
912
1.19M
                   ptr, pattern[0]));
913
1.19M
            pattern += pattern[0];
914
1.19M
            DISPATCH;
915
916
3.87M
        TARGET(SRE_OP_BRANCH):
917
            /* alternation */
918
            /* <BRANCH> <0=skip> code <JUMP> ... <NULL> */
919
3.87M
            TRACE(("|%p|%p|BRANCH\n", pattern, ptr));
920
3.87M
            LASTMARK_SAVE();
921
3.87M
            if (state->save_marks)
922
12.9k
                MARK_PUSH(ctx->lastmark);
923
11.6M
            for (; pattern[0]; pattern += pattern[0]) {
924
8.41M
                if (pattern[1] == SRE_OP_LITERAL &&
925
2.53M
                    (ptr >= end ||
926
2.53M
                     (SRE_CODE) *ptr != pattern[2]))
927
1.62M
                    continue;
928
6.79M
                if (pattern[1] == SRE_OP_IN &&
929
538k
                    (ptr >= end ||
930
538k
                     !SRE(charset)(state, pattern + 3,
931
538k
                                   (SRE_CODE) *ptr)))
932
156k
                    continue;
933
6.63M
                state->ptr = ptr;
934
6.63M
                DO_JUMP(JUMP_BRANCH, jump_branch, pattern+1);
935
6.63M
                if (ret) {
936
662k
                    if (state->save_marks)
937
7.55k
                        MARK_POP_DISCARD(ctx->lastmark);
938
662k
                    RETURN_ON_ERROR(ret);
939
662k
                    RETURN_SUCCESS;
940
662k
                }
941
5.97M
                if (state->save_marks)
942
10.9k
                    MARK_POP_KEEP(ctx->lastmark);
943
5.97M
                LASTMARK_RESTORE();
944
5.97M
            }
945
3.21M
            if (state->save_marks)
946
5.39k
                MARK_POP_DISCARD(ctx->lastmark);
947
3.21M
            RETURN_FAILURE;
948
949
153M
        TARGET(SRE_OP_REPEAT_ONE):
950
            /* match repeated sequence (maximizing regexp) */
951
952
            /* this operator only works if the repeated item is
953
               exactly one character wide, and we're not already
954
               collecting backtracking points.  for other cases,
955
               use the MAX_REPEAT operator */
956
957
            /* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
958
959
153M
            TRACE(("|%p|%p|REPEAT_ONE %d %d\n", pattern, ptr,
960
153M
                   pattern[1], pattern[2]));
961
962
153M
            if ((Py_ssize_t) pattern[1] > end - ptr)
963
143k
                RETURN_FAILURE; /* cannot match */
964
965
152M
            state->ptr = ptr;
966
967
152M
            ret = SRE(count)(state, pattern+3, pattern[2]);
968
152M
            RETURN_ON_ERROR(ret);
969
152M
            DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
970
152M
            ctx->count = ret;
971
152M
            ptr += ctx->count;
972
973
            /* when we arrive here, count contains the number of
974
               matches, and ptr points to the tail of the target
975
               string.  check if the rest of the pattern matches,
976
               and backtrack if not. */
977
978
152M
            if (ctx->count < (Py_ssize_t) pattern[1])
979
132M
                RETURN_FAILURE;
980
981
20.4M
            if (pattern[pattern[0]] == SRE_OP_SUCCESS &&
982
1.94M
                ptr == state->end &&
983
22.5k
                !(ctx->toplevel && state->must_advance && ptr == state->start))
984
22.5k
            {
985
                /* tail is empty.  we're finished */
986
22.5k
                state->ptr = ptr;
987
22.5k
                RETURN_SUCCESS;
988
22.5k
            }
989
990
20.4M
            LASTMARK_SAVE();
991
20.4M
            if (state->save_marks)
992
15.2M
                MARK_PUSH(ctx->lastmark);
993
994
20.4M
            if (pattern[pattern[0]] == SRE_OP_LITERAL) {
995
                /* tail starts with a literal. skip positions where
996
                   the rest of the pattern cannot possibly match */
997
26.4k
                ctx->u.chr = pattern[pattern[0]+1];
998
26.4k
                for (;;) {
999
215k
                    while (ctx->count >= (Py_ssize_t) pattern[1] &&
1000
204k
                           (ptr >= end || *ptr != ctx->u.chr)) {
1001
189k
                        ptr--;
1002
189k
                        ctx->count--;
1003
189k
                    }
1004
26.4k
                    if (ctx->count < (Py_ssize_t) pattern[1])
1005
11.2k
                        break;
1006
15.1k
                    state->ptr = ptr;
1007
15.1k
                    DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1,
1008
15.1k
                            pattern+pattern[0]);
1009
15.1k
                    if (ret) {
1010
15.1k
                        if (state->save_marks)
1011
0
                            MARK_POP_DISCARD(ctx->lastmark);
1012
15.1k
                        RETURN_ON_ERROR(ret);
1013
15.1k
                        RETURN_SUCCESS;
1014
15.1k
                    }
1015
0
                    if (state->save_marks)
1016
0
                        MARK_POP_KEEP(ctx->lastmark);
1017
0
                    LASTMARK_RESTORE();
1018
1019
0
                    ptr--;
1020
0
                    ctx->count--;
1021
0
                }
1022
11.2k
                if (state->save_marks)
1023
0
                    MARK_POP_DISCARD(ctx->lastmark);
1024
20.4M
            } else {
1025
                /* general case */
1026
32.7M
                while (ctx->count >= (Py_ssize_t) pattern[1]) {
1027
32.1M
                    state->ptr = ptr;
1028
32.1M
                    DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2,
1029
32.1M
                            pattern+pattern[0]);
1030
32.1M
                    if (ret) {
1031
19.8M
                        if (state->save_marks)
1032
14.8M
                            MARK_POP_DISCARD(ctx->lastmark);
1033
19.8M
                        RETURN_ON_ERROR(ret);
1034
19.8M
                        RETURN_SUCCESS;
1035
19.8M
                    }
1036
12.3M
                    if (state->save_marks)
1037
478k
                        MARK_POP_KEEP(ctx->lastmark);
1038
12.3M
                    LASTMARK_RESTORE();
1039
1040
12.3M
                    ptr--;
1041
12.3M
                    ctx->count--;
1042
12.3M
                }
1043
525k
                if (state->save_marks)
1044
424k
                    MARK_POP_DISCARD(ctx->lastmark);
1045
525k
            }
1046
537k
            RETURN_FAILURE;
1047
1048
0
        TARGET(SRE_OP_MIN_REPEAT_ONE):
1049
            /* match repeated sequence (minimizing regexp) */
1050
1051
            /* this operator only works if the repeated item is
1052
               exactly one character wide, and we're not already
1053
               collecting backtracking points.  for other cases,
1054
               use the MIN_REPEAT operator */
1055
1056
            /* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
1057
1058
0
            TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", pattern, ptr,
1059
0
                   pattern[1], pattern[2]));
1060
1061
0
            if ((Py_ssize_t) pattern[1] > end - ptr)
1062
0
                RETURN_FAILURE; /* cannot match */
1063
1064
0
            state->ptr = ptr;
1065
1066
0
            if (pattern[1] == 0)
1067
0
                ctx->count = 0;
1068
0
            else {
1069
                /* count using pattern min as the maximum */
1070
0
                ret = SRE(count)(state, pattern+3, pattern[1]);
1071
0
                RETURN_ON_ERROR(ret);
1072
0
                DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1073
0
                if (ret < (Py_ssize_t) pattern[1])
1074
                    /* didn't match minimum number of times */
1075
0
                    RETURN_FAILURE;
1076
                /* advance past minimum matches of repeat */
1077
0
                ctx->count = ret;
1078
0
                ptr += ctx->count;
1079
0
            }
1080
1081
0
            if (pattern[pattern[0]] == SRE_OP_SUCCESS &&
1082
0
                !(ctx->toplevel &&
1083
0
                  ((state->match_all && ptr != state->end) ||
1084
0
                   (state->must_advance && ptr == state->start))))
1085
0
            {
1086
                /* tail is empty.  we're finished */
1087
0
                state->ptr = ptr;
1088
0
                RETURN_SUCCESS;
1089
1090
0
            } else {
1091
                /* general case */
1092
0
                LASTMARK_SAVE();
1093
0
                if (state->save_marks)
1094
0
                    MARK_PUSH(ctx->lastmark);
1095
1096
0
                while ((Py_ssize_t)pattern[2] == SRE_MAXREPEAT
1097
0
                       || ctx->count <= (Py_ssize_t)pattern[2]) {
1098
0
                    state->ptr = ptr;
1099
0
                    DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one,
1100
0
                            pattern+pattern[0]);
1101
0
                    if (ret) {
1102
0
                        if (state->save_marks)
1103
0
                            MARK_POP_DISCARD(ctx->lastmark);
1104
0
                        RETURN_ON_ERROR(ret);
1105
0
                        RETURN_SUCCESS;
1106
0
                    }
1107
0
                    if (state->save_marks)
1108
0
                        MARK_POP_KEEP(ctx->lastmark);
1109
0
                    LASTMARK_RESTORE();
1110
1111
0
                    state->ptr = ptr;
1112
0
                    ret = SRE(count)(state, pattern+3, 1);
1113
0
                    RETURN_ON_ERROR(ret);
1114
0
                    DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1115
0
                    if (ret == 0)
1116
0
                        break;
1117
0
                    assert(ret == 1);
1118
0
                    ptr++;
1119
0
                    ctx->count++;
1120
0
                }
1121
0
                if (state->save_marks)
1122
0
                    MARK_POP_DISCARD(ctx->lastmark);
1123
0
            }
1124
0
            RETURN_FAILURE;
1125
1126
0
        TARGET(SRE_OP_POSSESSIVE_REPEAT_ONE):
1127
            /* match repeated sequence (maximizing regexp) without
1128
               backtracking */
1129
1130
            /* this operator only works if the repeated item is
1131
               exactly one character wide, and we're not already
1132
               collecting backtracking points.  for other cases,
1133
               use the MAX_REPEAT operator */
1134
1135
            /* <POSSESSIVE_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS>
1136
               tail */
1137
1138
0
            TRACE(("|%p|%p|POSSESSIVE_REPEAT_ONE %d %d\n", pattern,
1139
0
                   ptr, pattern[1], pattern[2]));
1140
1141
0
            if (ptr + pattern[1] > end) {
1142
0
                RETURN_FAILURE; /* cannot match */
1143
0
            }
1144
1145
0
            state->ptr = ptr;
1146
1147
0
            ret = SRE(count)(state, pattern + 3, pattern[2]);
1148
0
            RETURN_ON_ERROR(ret);
1149
0
            DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1150
0
            ctx->count = ret;
1151
0
            ptr += ctx->count;
1152
1153
            /* when we arrive here, count contains the number of
1154
               matches, and ptr points to the tail of the target
1155
               string.  check if the rest of the pattern matches,
1156
               and fail if not. */
1157
1158
            /* Test for not enough repetitions in match */
1159
0
            if (ctx->count < (Py_ssize_t) pattern[1]) {
1160
0
                RETURN_FAILURE;
1161
0
            }
1162
1163
            /* Update the pattern to point to the next op code */
1164
0
            pattern += pattern[0];
1165
1166
            /* Let the tail be evaluated separately and consider this
1167
               match successful. */
1168
0
            if (*pattern == SRE_OP_SUCCESS &&
1169
0
                ptr == state->end &&
1170
0
                !(ctx->toplevel && state->must_advance && ptr == state->start))
1171
0
            {
1172
                /* tail is empty.  we're finished */
1173
0
                state->ptr = ptr;
1174
0
                RETURN_SUCCESS;
1175
0
            }
1176
1177
            /* Attempt to match the rest of the string */
1178
0
            DISPATCH;
1179
1180
11.7M
        TARGET(SRE_OP_REPEAT):
1181
            /* create repeat context.  all the hard work is done
1182
               by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */
1183
            /* <REPEAT> <skip> <1=min> <2=max>
1184
               <3=repeat_index> item <UNTIL> tail */
1185
11.7M
            TRACE(("|%p|%p|REPEAT %d %d\n", pattern, ptr,
1186
11.7M
                   pattern[1], pattern[2]));
1187
1188
            /* install new repeat context */
1189
11.7M
            ctx->u.rep = repeat_pool_malloc(state);
1190
11.7M
            if (!ctx->u.rep) {
1191
0
                RETURN_ERROR(SRE_ERROR_MEMORY);
1192
0
            }
1193
11.7M
            ctx->u.rep->count = -1;
1194
11.7M
            ctx->u.rep->pattern = pattern;
1195
11.7M
            ctx->u.rep->prev = state->repeat;
1196
11.7M
            ctx->u.rep->last_ptr = NULL;
1197
11.7M
            state->repeat = ctx->u.rep;
1198
11.7M
            state->save_marks++;
1199
1200
11.7M
            state->ptr = ptr;
1201
11.7M
            DO_JUMP(JUMP_REPEAT, jump_repeat, pattern+pattern[0]);
1202
11.7M
            state->repeat = ctx->u.rep->prev;
1203
11.7M
            state->save_marks--;
1204
11.7M
            repeat_pool_free(state, ctx->u.rep);
1205
1206
11.7M
            if (ret) {
1207
9.67M
                RETURN_ON_ERROR(ret);
1208
9.67M
                RETURN_SUCCESS;
1209
9.67M
            }
1210
2.10M
            RETURN_FAILURE;
1211
1212
109M
        TARGET(SRE_OP_MAX_UNTIL):
1213
            /* maximizing repeat */
1214
            /* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */
1215
1216
            /* FIXME: we probably need to deal with zero-width
1217
               matches in here... */
1218
1219
109M
            ctx->u.rep = state->repeat;
1220
109M
            if (!ctx->u.rep)
1221
0
                RETURN_ERROR(SRE_ERROR_STATE);
1222
1223
109M
            state->ptr = ptr;
1224
1225
109M
            ctx->count = ctx->u.rep->count+1;
1226
1227
109M
            TRACE(("|%p|%p|MAX_UNTIL %zd\n", pattern,
1228
109M
                   ptr, ctx->count));
1229
1230
109M
            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
1231
                /* not enough matches */
1232
4.67M
                ctx->u.rep->count = ctx->count;
1233
4.67M
                DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
1234
4.67M
                        ctx->u.rep->pattern+3);
1235
4.67M
                if (ret) {
1236
2.56M
                    RETURN_ON_ERROR(ret);
1237
2.56M
                    RETURN_SUCCESS;
1238
2.56M
                }
1239
2.10M
                ctx->u.rep->count = ctx->count-1;
1240
2.10M
                state->ptr = ptr;
1241
2.10M
                RETURN_FAILURE;
1242
2.10M
            }
1243
1244
104M
            if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] ||
1245
405k
                ctx->u.rep->pattern[2] == SRE_MAXREPEAT) &&
1246
104M
                state->ptr != ctx->u.rep->last_ptr) {
1247
                /* we may have enough matches, but if we can
1248
                   match another item, do so */
1249
104M
                ctx->u.rep->count = ctx->count;
1250
104M
                LASTMARK_SAVE();
1251
104M
                MARK_PUSH(ctx->lastmark);
1252
                /* zero-width match protection */
1253
104M
                LAST_PTR_PUSH();
1254
104M
                ctx->u.rep->last_ptr = state->ptr;
1255
104M
                DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
1256
104M
                        ctx->u.rep->pattern+3);
1257
104M
                LAST_PTR_POP();
1258
104M
                if (ret) {
1259
94.6M
                    MARK_POP_DISCARD(ctx->lastmark);
1260
94.6M
                    RETURN_ON_ERROR(ret);
1261
94.6M
                    RETURN_SUCCESS;
1262
94.6M
                }
1263
9.37M
                MARK_POP(ctx->lastmark);
1264
9.37M
                LASTMARK_RESTORE();
1265
9.37M
                ctx->u.rep->count = ctx->count-1;
1266
9.37M
                state->ptr = ptr;
1267
9.37M
            }
1268
1269
            /* cannot match more repeated items here.  make sure the
1270
               tail matches */
1271
9.78M
            state->repeat = ctx->u.rep->prev;
1272
9.78M
            state->save_marks--;
1273
9.78M
            DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, pattern);
1274
9.78M
            state->repeat = ctx->u.rep; // restore repeat before return
1275
9.78M
            state->save_marks++;
1276
1277
9.78M
            RETURN_ON_SUCCESS(ret);
1278
106k
            state->ptr = ptr;
1279
106k
            RETURN_FAILURE;
1280
1281
0
        TARGET(SRE_OP_MIN_UNTIL):
1282
            /* minimizing repeat */
1283
            /* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */
1284
1285
0
            ctx->u.rep = state->repeat;
1286
0
            if (!ctx->u.rep)
1287
0
                RETURN_ERROR(SRE_ERROR_STATE);
1288
1289
0
            state->ptr = ptr;
1290
1291
0
            ctx->count = ctx->u.rep->count+1;
1292
1293
0
            TRACE(("|%p|%p|MIN_UNTIL %zd %p\n", pattern,
1294
0
                   ptr, ctx->count, ctx->u.rep->pattern));
1295
1296
0
            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
1297
                /* not enough matches */
1298
0
                ctx->u.rep->count = ctx->count;
1299
0
                DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
1300
0
                        ctx->u.rep->pattern+3);
1301
0
                if (ret) {
1302
0
                    RETURN_ON_ERROR(ret);
1303
0
                    RETURN_SUCCESS;
1304
0
                }
1305
0
                ctx->u.rep->count = ctx->count-1;
1306
0
                state->ptr = ptr;
1307
0
                RETURN_FAILURE;
1308
0
            }
1309
1310
            /* see if the tail matches */
1311
0
            state->repeat = ctx->u.rep->prev;
1312
0
            state->save_marks--;
1313
1314
0
            LASTMARK_SAVE();
1315
0
            if (state->save_marks)
1316
0
                MARK_PUSH(ctx->lastmark);
1317
1318
0
            DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, pattern);
1319
            /* save_marks is balanced across the jump, so this equals the
1320
               value tested for MARK_PUSH above */
1321
0
            int pushed = state->save_marks != 0;
1322
0
            state->repeat = ctx->u.rep; // restore repeat before return
1323
0
            state->save_marks++;
1324
1325
0
            if (ret) {
1326
0
                if (pushed)
1327
0
                    MARK_POP_DISCARD(ctx->lastmark);
1328
0
                RETURN_ON_ERROR(ret);
1329
0
                RETURN_SUCCESS;
1330
0
            }
1331
0
            if (pushed)
1332
0
                MARK_POP(ctx->lastmark);
1333
0
            LASTMARK_RESTORE();
1334
1335
0
            state->ptr = ptr;
1336
1337
0
            if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2]
1338
0
                && ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
1339
0
                state->ptr == ctx->u.rep->last_ptr)
1340
0
                RETURN_FAILURE;
1341
1342
0
            ctx->u.rep->count = ctx->count;
1343
            /* zero-width match protection */
1344
0
            LAST_PTR_PUSH();
1345
0
            ctx->u.rep->last_ptr = state->ptr;
1346
0
            DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
1347
0
                    ctx->u.rep->pattern+3);
1348
0
            LAST_PTR_POP();
1349
0
            if (ret) {
1350
0
                RETURN_ON_ERROR(ret);
1351
0
                RETURN_SUCCESS;
1352
0
            }
1353
0
            ctx->u.rep->count = ctx->count-1;
1354
0
            state->ptr = ptr;
1355
0
            RETURN_FAILURE;
1356
1357
0
        TARGET(SRE_OP_POSSESSIVE_REPEAT):
1358
            /* create possessive repeat contexts. */
1359
            /* <POSSESSIVE_REPEAT> <skip> <1=min> <2=max> pattern
1360
               <SUCCESS> tail */
1361
0
            TRACE(("|%p|%p|POSSESSIVE_REPEAT %d %d\n", pattern,
1362
0
                   ptr, pattern[1], pattern[2]));
1363
1364
            /* Set the global Input pointer to this context's Input
1365
               pointer */
1366
0
            state->ptr = ptr;
1367
1368
            /* Capture groups in the body can be revisited on backtracking
1369
               between iterations, so their marks must be saved and restored,
1370
               as is done inside a repeat. */
1371
0
            state->save_marks++;
1372
1373
            /* Initialize Count to 0 */
1374
0
            ctx->count = 0;
1375
1376
            /* Check for minimum required matches. */
1377
0
            while (ctx->count < (Py_ssize_t)pattern[1]) {
1378
                /* not enough matches */
1379
0
                DO_JUMP0(JUMP_POSS_REPEAT_1, jump_poss_repeat_1,
1380
0
                         &pattern[3]);
1381
0
                if (ret) {
1382
0
                    RETURN_ON_ERROR(ret);
1383
0
                    ctx->count++;
1384
0
                }
1385
0
                else {
1386
0
                    state->ptr = ptr;
1387
0
                    state->save_marks--;
1388
0
                    RETURN_FAILURE;
1389
0
                }
1390
0
            }
1391
1392
            /* Clear the context's Input stream pointer so that it
1393
               doesn't match the global state so that the while loop can
1394
               be entered. */
1395
0
            ptr = NULL;
1396
1397
            /* Keep trying to parse the <pattern> sub-pattern until the
1398
               end is reached, creating a new context each time. */
1399
0
            while ((ctx->count < (Py_ssize_t)pattern[2] ||
1400
0
                    (Py_ssize_t)pattern[2] == SRE_MAXREPEAT) &&
1401
0
                   state->ptr != ptr) {
1402
                /* Save the Capture Group Marker state into the current
1403
                   Context and back up the current highest number
1404
                   Capture Group marker. */
1405
0
                LASTMARK_SAVE();
1406
0
                MARK_PUSH(ctx->lastmark);
1407
1408
                /* zero-width match protection */
1409
                /* Set the context's Input Stream pointer to be the
1410
                   current Input Stream pointer from the global
1411
                   state.  When the loop reaches the next iteration,
1412
                   the context will then store the last known good
1413
                   position with the global state holding the Input
1414
                   Input Stream position that has been updated with
1415
                   the most recent match.  Thus, if state's Input
1416
                   stream remains the same as the one stored in the
1417
                   current Context, we know we have successfully
1418
                   matched an empty string and that all subsequent
1419
                   matches will also be the empty string until the
1420
                   maximum number of matches are counted, and because
1421
                   of this, we could immediately stop at that point and
1422
                   consider this match successful. */
1423
0
                ptr = state->ptr;
1424
1425
                /* We have not reached the maximin matches, so try to
1426
                   match once more. */
1427
0
                DO_JUMP0(JUMP_POSS_REPEAT_2, jump_poss_repeat_2,
1428
0
                         &pattern[3]);
1429
1430
                /* Check to see if the last attempted match
1431
                   succeeded. */
1432
0
                if (ret) {
1433
                    /* Drop the saved highest number Capture Group
1434
                       marker saved above and use the newly updated
1435
                       value. */
1436
0
                    MARK_POP_DISCARD(ctx->lastmark);
1437
0
                    RETURN_ON_ERROR(ret);
1438
1439
                    /* Success, increment the count. */
1440
0
                    ctx->count++;
1441
0
                }
1442
                /* Last attempted match failed. */
1443
0
                else {
1444
                    /* Restore the previously saved highest number
1445
                       Capture Group marker since the last iteration
1446
                       did not match, then restore that to the global
1447
                       state. */
1448
0
                    MARK_POP(ctx->lastmark);
1449
0
                    LASTMARK_RESTORE();
1450
1451
                    /* Restore the global Input Stream pointer
1452
                       since it can change after jumps. */
1453
0
                    state->ptr = ptr;
1454
1455
                    /* We have sufficient matches, so exit loop. */
1456
0
                    break;
1457
0
                }
1458
0
            }
1459
1460
0
            state->save_marks--;
1461
1462
            /* Evaluate Tail */
1463
            /* Jump to end of pattern indicated by skip, and then skip
1464
               the SUCCESS op code that follows it. */
1465
0
            pattern += pattern[0] + 1;
1466
0
            ptr = state->ptr;
1467
0
            DISPATCH;
1468
1469
0
        TARGET(SRE_OP_ATOMIC_GROUP):
1470
            /* Atomic Group Sub Pattern */
1471
            /* <ATOMIC_GROUP> <skip> pattern <SUCCESS> tail */
1472
0
            TRACE(("|%p|%p|ATOMIC_GROUP\n", pattern, ptr));
1473
1474
            /* Set the global Input pointer to this context's Input
1475
               pointer */
1476
0
            state->ptr = ptr;
1477
1478
            /* Evaluate the Atomic Group in a new context, terminating
1479
               when the end of the group, represented by a SUCCESS op
1480
               code, is reached. */
1481
            /* Group Pattern begins at an offset of 1 code. */
1482
0
            DO_JUMP0(JUMP_ATOMIC_GROUP, jump_atomic_group,
1483
0
                     &pattern[1]);
1484
1485
            /* Test Exit Condition */
1486
0
            RETURN_ON_ERROR(ret);
1487
1488
0
            if (ret == 0) {
1489
                /* Atomic Group failed to Match. */
1490
0
                state->ptr = ptr;
1491
0
                RETURN_FAILURE;
1492
0
            }
1493
1494
            /* Evaluate Tail */
1495
            /* Jump to end of pattern indicated by skip, and then skip
1496
               the SUCCESS op code that follows it. */
1497
0
            pattern += pattern[0];
1498
0
            ptr = state->ptr;
1499
0
            DISPATCH;
1500
1501
0
        TARGET(SRE_OP_GROUPREF):
1502
            /* match backreference */
1503
0
            TRACE(("|%p|%p|GROUPREF %d\n", pattern,
1504
0
                   ptr, pattern[0]));
1505
0
            {
1506
0
                int groupref = pattern[0] * 2;
1507
0
                if (groupref >= state->lastmark) {
1508
0
                    RETURN_FAILURE;
1509
0
                } else {
1510
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1511
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1512
0
                    if (!p || !e || e < p)
1513
0
                        RETURN_FAILURE;
1514
0
                    while (p < e) {
1515
0
                        if (ptr >= end || *ptr != *p)
1516
0
                            RETURN_FAILURE;
1517
0
                        p++;
1518
0
                        ptr++;
1519
0
                    }
1520
0
                }
1521
0
            }
1522
0
            pattern++;
1523
0
            DISPATCH;
1524
1525
0
        TARGET(SRE_OP_GROUPREF_IGNORE):
1526
            /* match backreference */
1527
0
            TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", pattern,
1528
0
                   ptr, pattern[0]));
1529
0
            {
1530
0
                int groupref = pattern[0] * 2;
1531
0
                if (groupref >= state->lastmark) {
1532
0
                    RETURN_FAILURE;
1533
0
                } else {
1534
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1535
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1536
0
                    if (!p || !e || e < p)
1537
0
                        RETURN_FAILURE;
1538
0
                    while (p < e) {
1539
0
                        if (ptr >= end ||
1540
0
                            sre_lower_ascii(*ptr) != sre_lower_ascii(*p))
1541
0
                            RETURN_FAILURE;
1542
0
                        p++;
1543
0
                        ptr++;
1544
0
                    }
1545
0
                }
1546
0
            }
1547
0
            pattern++;
1548
0
            DISPATCH;
1549
1550
0
        TARGET(SRE_OP_GROUPREF_UNI_IGNORE):
1551
            /* match backreference */
1552
0
            TRACE(("|%p|%p|GROUPREF_UNI_IGNORE %d\n", pattern,
1553
0
                   ptr, pattern[0]));
1554
0
            {
1555
0
                int groupref = pattern[0] * 2;
1556
0
                if (groupref >= state->lastmark) {
1557
0
                    RETURN_FAILURE;
1558
0
                } else {
1559
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1560
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1561
0
                    if (!p || !e || e < p)
1562
0
                        RETURN_FAILURE;
1563
0
                    while (p < e) {
1564
0
                        if (ptr >= end ||
1565
0
                            sre_lower_unicode(*ptr) != sre_lower_unicode(*p))
1566
0
                            RETURN_FAILURE;
1567
0
                        p++;
1568
0
                        ptr++;
1569
0
                    }
1570
0
                }
1571
0
            }
1572
0
            pattern++;
1573
0
            DISPATCH;
1574
1575
0
        TARGET(SRE_OP_GROUPREF_LOC_IGNORE):
1576
            /* match backreference */
1577
0
            TRACE(("|%p|%p|GROUPREF_LOC_IGNORE %d\n", pattern,
1578
0
                   ptr, pattern[0]));
1579
0
            {
1580
0
                int groupref = pattern[0] * 2;
1581
0
                if (groupref >= state->lastmark) {
1582
0
                    RETURN_FAILURE;
1583
0
                } else {
1584
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1585
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1586
0
                    if (!p || !e || e < p)
1587
0
                        RETURN_FAILURE;
1588
0
                    while (p < e) {
1589
0
                        if (ptr >= end ||
1590
0
                            sre_lower_locale(*ptr) != sre_lower_locale(*p))
1591
0
                            RETURN_FAILURE;
1592
0
                        p++;
1593
0
                        ptr++;
1594
0
                    }
1595
0
                }
1596
0
            }
1597
0
            pattern++;
1598
0
            DISPATCH;
1599
1600
0
        TARGET(SRE_OP_GROUPREF_EXISTS):
1601
0
            TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", pattern,
1602
0
                   ptr, pattern[0]));
1603
            /* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */
1604
0
            {
1605
0
                int groupref = pattern[0] * 2;
1606
0
                if (groupref >= state->lastmark) {
1607
0
                    pattern += pattern[1];
1608
0
                    DISPATCH;
1609
0
                } else {
1610
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1611
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1612
0
                    if (!p || !e || e < p) {
1613
0
                        pattern += pattern[1];
1614
0
                        DISPATCH;
1615
0
                    }
1616
0
                }
1617
0
            }
1618
0
            pattern += 2;
1619
0
            DISPATCH;
1620
1621
0
        TARGET(SRE_OP_ASSERT):
1622
            /* assert subpattern */
1623
            /* <ASSERT> <skip> <back> <pattern> */
1624
0
            TRACE(("|%p|%p|ASSERT %d\n", pattern,
1625
0
                   ptr, pattern[1]));
1626
0
            if ((uintptr_t)(ptr - (SRE_CHAR *)state->beginning) < pattern[1])
1627
0
                RETURN_FAILURE;
1628
0
            state->ptr = ptr - pattern[1];
1629
0
            DO_JUMP0(JUMP_ASSERT, jump_assert, pattern+2);
1630
0
            RETURN_ON_FAILURE(ret);
1631
0
            pattern += pattern[0];
1632
0
            DISPATCH;
1633
1634
88.0M
        TARGET(SRE_OP_ASSERT_NOT):
1635
            /* assert not subpattern */
1636
            /* <ASSERT_NOT> <skip> <back> <pattern> */
1637
88.0M
            TRACE(("|%p|%p|ASSERT_NOT %d\n", pattern,
1638
88.0M
                   ptr, pattern[1]));
1639
88.0M
            if ((uintptr_t)(ptr - (SRE_CHAR *)state->beginning) >= pattern[1]) {
1640
88.0M
                state->ptr = ptr - pattern[1];
1641
88.0M
                LASTMARK_SAVE();
1642
88.0M
                if (state->save_marks)
1643
88.0M
                    MARK_PUSH(ctx->lastmark);
1644
1645
176M
                DO_JUMP0(JUMP_ASSERT_NOT, jump_assert_not, pattern+2);
1646
176M
                if (ret) {
1647
418k
                    if (state->save_marks)
1648
418k
                        MARK_POP_DISCARD(ctx->lastmark);
1649
418k
                    RETURN_ON_ERROR(ret);
1650
418k
                    RETURN_FAILURE;
1651
418k
                }
1652
87.6M
                if (state->save_marks)
1653
87.6M
                    MARK_POP(ctx->lastmark);
1654
87.6M
                LASTMARK_RESTORE();
1655
87.6M
            }
1656
87.6M
            pattern += pattern[0];
1657
87.6M
            DISPATCH;
1658
1659
87.6M
        TARGET(SRE_OP_FAILURE):
1660
            /* immediate failure */
1661
0
            TRACE(("|%p|%p|FAILURE\n", pattern, ptr));
1662
0
            RETURN_FAILURE;
1663
1664
#if !USE_COMPUTED_GOTOS
1665
        default:
1666
#endif
1667
        // Also any unused opcodes:
1668
0
        TARGET(SRE_OP_RANGE_UNI_IGNORE):
1669
0
        TARGET(SRE_OP_SUBPATTERN):
1670
0
        TARGET(SRE_OP_RANGE):
1671
0
        TARGET(SRE_OP_NEGATE):
1672
0
        TARGET(SRE_OP_BIGCHARSET):
1673
0
        TARGET(SRE_OP_CHARSET):
1674
0
            TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ptr,
1675
0
                   pattern[-1]));
1676
0
            RETURN_ERROR(SRE_ERROR_ILLEGAL);
1677
1678
0
    }
1679
1680
444M
exit:
1681
444M
    ctx_pos = ctx->last_ctx_pos;
1682
444M
    jump = ctx->jump;
1683
444M
    DATA_POP_DISCARD(ctx);
1684
444M
    if (ctx_pos == -1) {
1685
187M
        state->sigcount = sigcount;
1686
187M
        return ret;
1687
187M
    }
1688
257M
    DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1689
1690
257M
    switch (jump) {
1691
104M
        case JUMP_MAX_UNTIL_2:
1692
104M
            TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", pattern, ptr));
1693
104M
            goto jump_max_until_2;
1694
9.78M
        case JUMP_MAX_UNTIL_3:
1695
9.78M
            TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", pattern, ptr));
1696
9.78M
            goto jump_max_until_3;
1697
0
        case JUMP_MIN_UNTIL_2:
1698
0
            TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", pattern, ptr));
1699
0
            goto jump_min_until_2;
1700
0
        case JUMP_MIN_UNTIL_3:
1701
0
            TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", pattern, ptr));
1702
0
            goto jump_min_until_3;
1703
6.63M
        case JUMP_BRANCH:
1704
6.63M
            TRACE(("|%p|%p|JUMP_BRANCH\n", pattern, ptr));
1705
6.63M
            goto jump_branch;
1706
4.67M
        case JUMP_MAX_UNTIL_1:
1707
4.67M
            TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", pattern, ptr));
1708
4.67M
            goto jump_max_until_1;
1709
0
        case JUMP_MIN_UNTIL_1:
1710
0
            TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", pattern, ptr));
1711
0
            goto jump_min_until_1;
1712
0
        case JUMP_POSS_REPEAT_1:
1713
0
            TRACE(("|%p|%p|JUMP_POSS_REPEAT_1\n", pattern, ptr));
1714
0
            goto jump_poss_repeat_1;
1715
0
        case JUMP_POSS_REPEAT_2:
1716
0
            TRACE(("|%p|%p|JUMP_POSS_REPEAT_2\n", pattern, ptr));
1717
0
            goto jump_poss_repeat_2;
1718
11.7M
        case JUMP_REPEAT:
1719
11.7M
            TRACE(("|%p|%p|JUMP_REPEAT\n", pattern, ptr));
1720
11.7M
            goto jump_repeat;
1721
15.1k
        case JUMP_REPEAT_ONE_1:
1722
15.1k
            TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", pattern, ptr));
1723
15.1k
            goto jump_repeat_one_1;
1724
32.1M
        case JUMP_REPEAT_ONE_2:
1725
32.1M
            TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", pattern, ptr));
1726
32.1M
            goto jump_repeat_one_2;
1727
0
        case JUMP_MIN_REPEAT_ONE:
1728
0
            TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", pattern, ptr));
1729
0
            goto jump_min_repeat_one;
1730
0
        case JUMP_ATOMIC_GROUP:
1731
0
            TRACE(("|%p|%p|JUMP_ATOMIC_GROUP\n", pattern, ptr));
1732
0
            goto jump_atomic_group;
1733
0
        case JUMP_ASSERT:
1734
0
            TRACE(("|%p|%p|JUMP_ASSERT\n", pattern, ptr));
1735
0
            goto jump_assert;
1736
88.0M
        case JUMP_ASSERT_NOT:
1737
88.0M
            TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", pattern, ptr));
1738
88.0M
            goto jump_assert_not;
1739
0
        case JUMP_NONE:
1740
0
            TRACE(("|%p|%p|RETURN %zd\n", pattern,
1741
0
                   ptr, ret));
1742
0
            break;
1743
257M
    }
1744
1745
0
    return ret; /* should never get here */
1746
257M
}
sre.c:sre_ucs4_match
Line
Count
Source
663
37.7M
{
664
37.7M
    const SRE_CHAR* end = (const SRE_CHAR *)state->end;
665
37.7M
    Py_ssize_t alloc_pos, ctx_pos = -1;
666
37.7M
    Py_ssize_t ret = 0;
667
37.7M
    int jump;
668
37.7M
    unsigned int sigcount = state->sigcount;
669
670
37.7M
    SRE(match_context)* ctx;
671
37.7M
    SRE(match_context)* nextctx;
672
37.7M
    INIT_TRACE(state);
673
674
37.7M
    TRACE(("|%p|%p|ENTER\n", pattern, state->ptr));
675
676
37.7M
    DATA_ALLOC(SRE(match_context), ctx);
677
37.7M
    ctx->last_ctx_pos = -1;
678
37.7M
    ctx->jump = JUMP_NONE;
679
37.7M
    ctx->toplevel = toplevel;
680
37.7M
    ctx_pos = alloc_pos;
681
682
37.7M
#if USE_COMPUTED_GOTOS
683
37.7M
#include "sre_targets.h"
684
37.7M
#endif
685
686
147M
entrance:
687
688
147M
    ;  // Fashion statement.
689
147M
    const SRE_CHAR *ptr = (SRE_CHAR *)state->ptr;
690
691
147M
    if (pattern[0] == SRE_OP_INFO) {
692
        /* optimization info block */
693
        /* <INFO> <1=skip> <2=flags> <3=min> ... */
694
451k
        if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) {
695
4.70k
            TRACE(("reject (got %tu chars, need %zu)\n",
696
4.70k
                   end - ptr, (size_t) pattern[3]));
697
4.70k
            RETURN_FAILURE;
698
4.70k
        }
699
447k
        pattern += pattern[1] + 1;
700
447k
    }
701
702
147M
#if USE_COMPUTED_GOTOS
703
147M
    DISPATCH;
704
#else
705
dispatch:
706
    MAYBE_CHECK_SIGNALS;
707
    switch (*pattern++)
708
#endif
709
147M
    {
710
711
147M
        TARGET(SRE_OP_MARK):
712
            /* set mark */
713
            /* <MARK> <gid> */
714
39.3M
            TRACE(("|%p|%p|MARK %d\n", pattern,
715
39.3M
                   ptr, pattern[0]));
716
39.3M
            {
717
39.3M
                int i = pattern[0];
718
39.3M
                if (i & 1)
719
8.51M
                    state->lastindex = i/2 + 1;
720
39.3M
                if (i > state->lastmark) {
721
                    /* state->lastmark is the highest valid index in the
722
                       state->mark array.  If it is increased by more than 1,
723
                       the intervening marks must be set to NULL to signal
724
                       that these marks have not been encountered. */
725
39.2M
                    int j = state->lastmark + 1;
726
39.3M
                    while (j < i)
727
57.7k
                        state->mark[j++] = NULL;
728
39.2M
                    state->lastmark = i;
729
39.2M
                }
730
39.3M
                state->mark[i] = ptr;
731
39.3M
            }
732
39.3M
            pattern++;
733
39.3M
            DISPATCH;
734
735
39.3M
        TARGET(SRE_OP_LITERAL):
736
            /* match literal string */
737
            /* <LITERAL> <code> */
738
8.57M
            TRACE(("|%p|%p|LITERAL %d\n", pattern,
739
8.57M
                   ptr, *pattern));
740
8.57M
            if (ptr >= end || (SRE_CODE) ptr[0] != pattern[0])
741
7.84M
                RETURN_FAILURE;
742
725k
            pattern++;
743
725k
            ptr++;
744
725k
            DISPATCH;
745
746
725k
        TARGET(SRE_OP_NOT_LITERAL):
747
            /* match anything that is not literal character */
748
            /* <NOT_LITERAL> <code> */
749
0
            TRACE(("|%p|%p|NOT_LITERAL %d\n", pattern,
750
0
                   ptr, *pattern));
751
0
            if (ptr >= end || (SRE_CODE) ptr[0] == pattern[0])
752
0
                RETURN_FAILURE;
753
0
            pattern++;
754
0
            ptr++;
755
0
            DISPATCH;
756
757
7.23M
        TARGET(SRE_OP_SUCCESS):
758
            /* end of pattern */
759
7.23M
            TRACE(("|%p|%p|SUCCESS\n", pattern, ptr));
760
7.23M
            if (ctx->toplevel &&
761
378k
                ((state->match_all && ptr != state->end) ||
762
378k
                 (state->must_advance && ptr == state->start)))
763
0
            {
764
0
                RETURN_FAILURE;
765
0
            }
766
7.23M
            state->ptr = ptr;
767
7.23M
            RETURN_SUCCESS;
768
769
539k
        TARGET(SRE_OP_AT):
770
            /* match at given position */
771
            /* <AT> <code> */
772
539k
            TRACE(("|%p|%p|AT %d\n", pattern, ptr, *pattern));
773
539k
            if (!SRE(at)(state, ptr, *pattern))
774
502k
                RETURN_FAILURE;
775
36.8k
            pattern++;
776
36.8k
            DISPATCH;
777
778
30.0M
        TARGET(SRE_OP_CATEGORY):
779
            /* match at given category */
780
            /* <CATEGORY> <code> */
781
30.0M
            TRACE(("|%p|%p|CATEGORY %d\n", pattern,
782
30.0M
                   ptr, *pattern));
783
30.0M
            if (ptr >= end || !sre_category(pattern[0], ptr[0]))
784
3.70M
                RETURN_FAILURE;
785
26.3M
            pattern++;
786
26.3M
            ptr++;
787
26.3M
            DISPATCH;
788
789
26.3M
        TARGET(SRE_OP_ANY):
790
            /* match anything (except a newline) */
791
            /* <ANY> */
792
0
            TRACE(("|%p|%p|ANY\n", pattern, ptr));
793
0
            if (ptr >= end || SRE_IS_LINEBREAK(ptr[0]))
794
0
                RETURN_FAILURE;
795
0
            ptr++;
796
0
            DISPATCH;
797
798
0
        TARGET(SRE_OP_ANY_ALL):
799
            /* match anything */
800
            /* <ANY_ALL> */
801
0
            TRACE(("|%p|%p|ANY_ALL\n", pattern, ptr));
802
0
            if (ptr >= end)
803
0
                RETURN_FAILURE;
804
0
            ptr++;
805
0
            DISPATCH;
806
807
52.5M
        TARGET(SRE_OP_IN):
808
            /* match set member (or non_member) */
809
            /* <IN> <skip> <set> */
810
52.5M
            TRACE(("|%p|%p|IN\n", pattern, ptr));
811
52.5M
            if (ptr >= end ||
812
52.5M
                !SRE(charset)(state, pattern + 1, *ptr))
813
30.1M
                RETURN_FAILURE;
814
22.4M
            pattern += pattern[0];
815
22.4M
            ptr++;
816
22.4M
            DISPATCH;
817
818
22.4M
        TARGET(SRE_OP_LITERAL_IGNORE):
819
0
            TRACE(("|%p|%p|LITERAL_IGNORE %d\n",
820
0
                   pattern, ptr, pattern[0]));
821
0
            if (ptr >= end ||
822
0
                sre_lower_ascii(*ptr) != *pattern)
823
0
                RETURN_FAILURE;
824
0
            pattern++;
825
0
            ptr++;
826
0
            DISPATCH;
827
828
0
        TARGET(SRE_OP_LITERAL_UNI_IGNORE):
829
0
            TRACE(("|%p|%p|LITERAL_UNI_IGNORE %d\n",
830
0
                   pattern, ptr, pattern[0]));
831
0
            if (ptr >= end ||
832
0
                sre_lower_unicode(*ptr) != *pattern)
833
0
                RETURN_FAILURE;
834
0
            pattern++;
835
0
            ptr++;
836
0
            DISPATCH;
837
838
0
        TARGET(SRE_OP_LITERAL_LOC_IGNORE):
839
0
            TRACE(("|%p|%p|LITERAL_LOC_IGNORE %d\n",
840
0
                   pattern, ptr, pattern[0]));
841
0
            if (ptr >= end
842
0
                || !char_loc_ignore(*pattern, *ptr))
843
0
                RETURN_FAILURE;
844
0
            pattern++;
845
0
            ptr++;
846
0
            DISPATCH;
847
848
0
        TARGET(SRE_OP_NOT_LITERAL_IGNORE):
849
0
            TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n",
850
0
                   pattern, ptr, *pattern));
851
0
            if (ptr >= end ||
852
0
                sre_lower_ascii(*ptr) == *pattern)
853
0
                RETURN_FAILURE;
854
0
            pattern++;
855
0
            ptr++;
856
0
            DISPATCH;
857
858
0
        TARGET(SRE_OP_NOT_LITERAL_UNI_IGNORE):
859
0
            TRACE(("|%p|%p|NOT_LITERAL_UNI_IGNORE %d\n",
860
0
                   pattern, ptr, *pattern));
861
0
            if (ptr >= end ||
862
0
                sre_lower_unicode(*ptr) == *pattern)
863
0
                RETURN_FAILURE;
864
0
            pattern++;
865
0
            ptr++;
866
0
            DISPATCH;
867
868
0
        TARGET(SRE_OP_NOT_LITERAL_LOC_IGNORE):
869
0
            TRACE(("|%p|%p|NOT_LITERAL_LOC_IGNORE %d\n",
870
0
                   pattern, ptr, *pattern));
871
0
            if (ptr >= end
872
0
                || char_loc_ignore(*pattern, *ptr))
873
0
                RETURN_FAILURE;
874
0
            pattern++;
875
0
            ptr++;
876
0
            DISPATCH;
877
878
0
        TARGET(SRE_OP_IN_IGNORE):
879
0
            TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr));
880
0
            if (ptr >= end
881
0
                || !SRE(charset)(state, pattern+1,
882
0
                                 (SRE_CODE)sre_lower_ascii(*ptr)))
883
0
                RETURN_FAILURE;
884
0
            pattern += pattern[0];
885
0
            ptr++;
886
0
            DISPATCH;
887
888
0
        TARGET(SRE_OP_IN_UNI_IGNORE):
889
0
            TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ptr));
890
0
            if (ptr >= end
891
0
                || !SRE(charset)(state, pattern+1,
892
0
                                 (SRE_CODE)sre_lower_unicode(*ptr)))
893
0
                RETURN_FAILURE;
894
0
            pattern += pattern[0];
895
0
            ptr++;
896
0
            DISPATCH;
897
898
0
        TARGET(SRE_OP_IN_LOC_IGNORE):
899
0
            TRACE(("|%p|%p|IN_LOC_IGNORE\n", pattern, ptr));
900
0
            if (ptr >= end
901
0
                || !SRE(charset_loc_ignore)(state, pattern+1, *ptr))
902
0
                RETURN_FAILURE;
903
0
            pattern += pattern[0];
904
0
            ptr++;
905
0
            DISPATCH;
906
907
615k
        TARGET(SRE_OP_JUMP):
908
615k
        TARGET(SRE_OP_INFO):
909
            /* jump forward */
910
            /* <JUMP> <offset> */
911
615k
            TRACE(("|%p|%p|JUMP %d\n", pattern,
912
615k
                   ptr, pattern[0]));
913
615k
            pattern += pattern[0];
914
615k
            DISPATCH;
915
916
670k
        TARGET(SRE_OP_BRANCH):
917
            /* alternation */
918
            /* <BRANCH> <0=skip> code <JUMP> ... <NULL> */
919
670k
            TRACE(("|%p|%p|BRANCH\n", pattern, ptr));
920
670k
            LASTMARK_SAVE();
921
670k
            if (state->save_marks)
922
7.72k
                MARK_PUSH(ctx->lastmark);
923
2.26M
            for (; pattern[0]; pattern += pattern[0]) {
924
1.73M
                if (pattern[1] == SRE_OP_LITERAL &&
925
1.61M
                    (ptr >= end ||
926
1.61M
                     (SRE_CODE) *ptr != pattern[2]))
927
960k
                    continue;
928
778k
                if (pattern[1] == SRE_OP_IN &&
929
51.4k
                    (ptr >= end ||
930
51.4k
                     !SRE(charset)(state, pattern + 3,
931
51.4k
                                   (SRE_CODE) *ptr)))
932
13.1k
                    continue;
933
765k
                state->ptr = ptr;
934
765k
                DO_JUMP(JUMP_BRANCH, jump_branch, pattern+1);
935
765k
                if (ret) {
936
140k
                    if (state->save_marks)
937
6.07k
                        MARK_POP_DISCARD(ctx->lastmark);
938
140k
                    RETURN_ON_ERROR(ret);
939
140k
                    RETURN_SUCCESS;
940
140k
                }
941
625k
                if (state->save_marks)
942
4.37k
                    MARK_POP_KEEP(ctx->lastmark);
943
625k
                LASTMARK_RESTORE();
944
625k
            }
945
530k
            if (state->save_marks)
946
1.64k
                MARK_POP_DISCARD(ctx->lastmark);
947
530k
            RETURN_FAILURE;
948
949
46.2M
        TARGET(SRE_OP_REPEAT_ONE):
950
            /* match repeated sequence (maximizing regexp) */
951
952
            /* this operator only works if the repeated item is
953
               exactly one character wide, and we're not already
954
               collecting backtracking points.  for other cases,
955
               use the MAX_REPEAT operator */
956
957
            /* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
958
959
46.2M
            TRACE(("|%p|%p|REPEAT_ONE %d %d\n", pattern, ptr,
960
46.2M
                   pattern[1], pattern[2]));
961
962
46.2M
            if ((Py_ssize_t) pattern[1] > end - ptr)
963
10.1k
                RETURN_FAILURE; /* cannot match */
964
965
46.2M
            state->ptr = ptr;
966
967
46.2M
            ret = SRE(count)(state, pattern+3, pattern[2]);
968
46.2M
            RETURN_ON_ERROR(ret);
969
46.2M
            DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
970
46.2M
            ctx->count = ret;
971
46.2M
            ptr += ctx->count;
972
973
            /* when we arrive here, count contains the number of
974
               matches, and ptr points to the tail of the target
975
               string.  check if the rest of the pattern matches,
976
               and backtrack if not. */
977
978
46.2M
            if (ctx->count < (Py_ssize_t) pattern[1])
979
29.9M
                RETURN_FAILURE;
980
981
16.2M
            if (pattern[pattern[0]] == SRE_OP_SUCCESS &&
982
307k
                ptr == state->end &&
983
2.27k
                !(ctx->toplevel && state->must_advance && ptr == state->start))
984
2.27k
            {
985
                /* tail is empty.  we're finished */
986
2.27k
                state->ptr = ptr;
987
2.27k
                RETURN_SUCCESS;
988
2.27k
            }
989
990
16.2M
            LASTMARK_SAVE();
991
16.2M
            if (state->save_marks)
992
15.7M
                MARK_PUSH(ctx->lastmark);
993
994
16.2M
            if (pattern[pattern[0]] == SRE_OP_LITERAL) {
995
                /* tail starts with a literal. skip positions where
996
                   the rest of the pattern cannot possibly match */
997
3.84k
                ctx->u.chr = pattern[pattern[0]+1];
998
3.84k
                for (;;) {
999
2.63M
                    while (ctx->count >= (Py_ssize_t) pattern[1] &&
1000
2.63M
                           (ptr >= end || *ptr != ctx->u.chr)) {
1001
2.62M
                        ptr--;
1002
2.62M
                        ctx->count--;
1003
2.62M
                    }
1004
3.84k
                    if (ctx->count < (Py_ssize_t) pattern[1])
1005
1.89k
                        break;
1006
1.94k
                    state->ptr = ptr;
1007
1.94k
                    DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1,
1008
1.94k
                            pattern+pattern[0]);
1009
1.94k
                    if (ret) {
1010
1.94k
                        if (state->save_marks)
1011
0
                            MARK_POP_DISCARD(ctx->lastmark);
1012
1.94k
                        RETURN_ON_ERROR(ret);
1013
1.94k
                        RETURN_SUCCESS;
1014
1.94k
                    }
1015
0
                    if (state->save_marks)
1016
0
                        MARK_POP_KEEP(ctx->lastmark);
1017
0
                    LASTMARK_RESTORE();
1018
1019
0
                    ptr--;
1020
0
                    ctx->count--;
1021
0
                }
1022
1.89k
                if (state->save_marks)
1023
0
                    MARK_POP_DISCARD(ctx->lastmark);
1024
16.2M
            } else {
1025
                /* general case */
1026
24.0M
                while (ctx->count >= (Py_ssize_t) pattern[1]) {
1027
23.9M
                    state->ptr = ptr;
1028
23.9M
                    DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2,
1029
23.9M
                            pattern+pattern[0]);
1030
23.9M
                    if (ret) {
1031
16.2M
                        if (state->save_marks)
1032
15.7M
                            MARK_POP_DISCARD(ctx->lastmark);
1033
16.2M
                        RETURN_ON_ERROR(ret);
1034
16.2M
                        RETURN_SUCCESS;
1035
16.2M
                    }
1036
7.77M
                    if (state->save_marks)
1037
80.8k
                        MARK_POP_KEEP(ctx->lastmark);
1038
7.77M
                    LASTMARK_RESTORE();
1039
1040
7.77M
                    ptr--;
1041
7.77M
                    ctx->count--;
1042
7.77M
                }
1043
56.0k
                if (state->save_marks)
1044
52.1k
                    MARK_POP_DISCARD(ctx->lastmark);
1045
56.0k
            }
1046
57.9k
            RETURN_FAILURE;
1047
1048
0
        TARGET(SRE_OP_MIN_REPEAT_ONE):
1049
            /* match repeated sequence (minimizing regexp) */
1050
1051
            /* this operator only works if the repeated item is
1052
               exactly one character wide, and we're not already
1053
               collecting backtracking points.  for other cases,
1054
               use the MIN_REPEAT operator */
1055
1056
            /* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
1057
1058
0
            TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", pattern, ptr,
1059
0
                   pattern[1], pattern[2]));
1060
1061
0
            if ((Py_ssize_t) pattern[1] > end - ptr)
1062
0
                RETURN_FAILURE; /* cannot match */
1063
1064
0
            state->ptr = ptr;
1065
1066
0
            if (pattern[1] == 0)
1067
0
                ctx->count = 0;
1068
0
            else {
1069
                /* count using pattern min as the maximum */
1070
0
                ret = SRE(count)(state, pattern+3, pattern[1]);
1071
0
                RETURN_ON_ERROR(ret);
1072
0
                DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1073
0
                if (ret < (Py_ssize_t) pattern[1])
1074
                    /* didn't match minimum number of times */
1075
0
                    RETURN_FAILURE;
1076
                /* advance past minimum matches of repeat */
1077
0
                ctx->count = ret;
1078
0
                ptr += ctx->count;
1079
0
            }
1080
1081
0
            if (pattern[pattern[0]] == SRE_OP_SUCCESS &&
1082
0
                !(ctx->toplevel &&
1083
0
                  ((state->match_all && ptr != state->end) ||
1084
0
                   (state->must_advance && ptr == state->start))))
1085
0
            {
1086
                /* tail is empty.  we're finished */
1087
0
                state->ptr = ptr;
1088
0
                RETURN_SUCCESS;
1089
1090
0
            } else {
1091
                /* general case */
1092
0
                LASTMARK_SAVE();
1093
0
                if (state->save_marks)
1094
0
                    MARK_PUSH(ctx->lastmark);
1095
1096
0
                while ((Py_ssize_t)pattern[2] == SRE_MAXREPEAT
1097
0
                       || ctx->count <= (Py_ssize_t)pattern[2]) {
1098
0
                    state->ptr = ptr;
1099
0
                    DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one,
1100
0
                            pattern+pattern[0]);
1101
0
                    if (ret) {
1102
0
                        if (state->save_marks)
1103
0
                            MARK_POP_DISCARD(ctx->lastmark);
1104
0
                        RETURN_ON_ERROR(ret);
1105
0
                        RETURN_SUCCESS;
1106
0
                    }
1107
0
                    if (state->save_marks)
1108
0
                        MARK_POP_KEEP(ctx->lastmark);
1109
0
                    LASTMARK_RESTORE();
1110
1111
0
                    state->ptr = ptr;
1112
0
                    ret = SRE(count)(state, pattern+3, 1);
1113
0
                    RETURN_ON_ERROR(ret);
1114
0
                    DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1115
0
                    if (ret == 0)
1116
0
                        break;
1117
0
                    assert(ret == 1);
1118
0
                    ptr++;
1119
0
                    ctx->count++;
1120
0
                }
1121
0
                if (state->save_marks)
1122
0
                    MARK_POP_DISCARD(ctx->lastmark);
1123
0
            }
1124
0
            RETURN_FAILURE;
1125
1126
0
        TARGET(SRE_OP_POSSESSIVE_REPEAT_ONE):
1127
            /* match repeated sequence (maximizing regexp) without
1128
               backtracking */
1129
1130
            /* this operator only works if the repeated item is
1131
               exactly one character wide, and we're not already
1132
               collecting backtracking points.  for other cases,
1133
               use the MAX_REPEAT operator */
1134
1135
            /* <POSSESSIVE_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS>
1136
               tail */
1137
1138
0
            TRACE(("|%p|%p|POSSESSIVE_REPEAT_ONE %d %d\n", pattern,
1139
0
                   ptr, pattern[1], pattern[2]));
1140
1141
0
            if (ptr + pattern[1] > end) {
1142
0
                RETURN_FAILURE; /* cannot match */
1143
0
            }
1144
1145
0
            state->ptr = ptr;
1146
1147
0
            ret = SRE(count)(state, pattern + 3, pattern[2]);
1148
0
            RETURN_ON_ERROR(ret);
1149
0
            DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1150
0
            ctx->count = ret;
1151
0
            ptr += ctx->count;
1152
1153
            /* when we arrive here, count contains the number of
1154
               matches, and ptr points to the tail of the target
1155
               string.  check if the rest of the pattern matches,
1156
               and fail if not. */
1157
1158
            /* Test for not enough repetitions in match */
1159
0
            if (ctx->count < (Py_ssize_t) pattern[1]) {
1160
0
                RETURN_FAILURE;
1161
0
            }
1162
1163
            /* Update the pattern to point to the next op code */
1164
0
            pattern += pattern[0];
1165
1166
            /* Let the tail be evaluated separately and consider this
1167
               match successful. */
1168
0
            if (*pattern == SRE_OP_SUCCESS &&
1169
0
                ptr == state->end &&
1170
0
                !(ctx->toplevel && state->must_advance && ptr == state->start))
1171
0
            {
1172
                /* tail is empty.  we're finished */
1173
0
                state->ptr = ptr;
1174
0
                RETURN_SUCCESS;
1175
0
            }
1176
1177
            /* Attempt to match the rest of the string */
1178
0
            DISPATCH;
1179
1180
3.85M
        TARGET(SRE_OP_REPEAT):
1181
            /* create repeat context.  all the hard work is done
1182
               by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */
1183
            /* <REPEAT> <skip> <1=min> <2=max>
1184
               <3=repeat_index> item <UNTIL> tail */
1185
3.85M
            TRACE(("|%p|%p|REPEAT %d %d\n", pattern, ptr,
1186
3.85M
                   pattern[1], pattern[2]));
1187
1188
            /* install new repeat context */
1189
3.85M
            ctx->u.rep = repeat_pool_malloc(state);
1190
3.85M
            if (!ctx->u.rep) {
1191
0
                RETURN_ERROR(SRE_ERROR_MEMORY);
1192
0
            }
1193
3.85M
            ctx->u.rep->count = -1;
1194
3.85M
            ctx->u.rep->pattern = pattern;
1195
3.85M
            ctx->u.rep->prev = state->repeat;
1196
3.85M
            ctx->u.rep->last_ptr = NULL;
1197
3.85M
            state->repeat = ctx->u.rep;
1198
3.85M
            state->save_marks++;
1199
1200
3.85M
            state->ptr = ptr;
1201
3.85M
            DO_JUMP(JUMP_REPEAT, jump_repeat, pattern+pattern[0]);
1202
3.85M
            state->repeat = ctx->u.rep->prev;
1203
3.85M
            state->save_marks--;
1204
3.85M
            repeat_pool_free(state, ctx->u.rep);
1205
1206
3.85M
            if (ret) {
1207
3.78M
                RETURN_ON_ERROR(ret);
1208
3.78M
                RETURN_SUCCESS;
1209
3.78M
            }
1210
71.0k
            RETURN_FAILURE;
1211
1212
47.7M
        TARGET(SRE_OP_MAX_UNTIL):
1213
            /* maximizing repeat */
1214
            /* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */
1215
1216
            /* FIXME: we probably need to deal with zero-width
1217
               matches in here... */
1218
1219
47.7M
            ctx->u.rep = state->repeat;
1220
47.7M
            if (!ctx->u.rep)
1221
0
                RETURN_ERROR(SRE_ERROR_STATE);
1222
1223
47.7M
            state->ptr = ptr;
1224
1225
47.7M
            ctx->count = ctx->u.rep->count+1;
1226
1227
47.7M
            TRACE(("|%p|%p|MAX_UNTIL %zd\n", pattern,
1228
47.7M
                   ptr, ctx->count));
1229
1230
47.7M
            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
1231
                /* not enough matches */
1232
3.70M
                ctx->u.rep->count = ctx->count;
1233
3.70M
                DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
1234
3.70M
                        ctx->u.rep->pattern+3);
1235
3.70M
                if (ret) {
1236
3.63M
                    RETURN_ON_ERROR(ret);
1237
3.63M
                    RETURN_SUCCESS;
1238
3.63M
                }
1239
70.5k
                ctx->u.rep->count = ctx->count-1;
1240
70.5k
                state->ptr = ptr;
1241
70.5k
                RETURN_FAILURE;
1242
70.5k
            }
1243
1244
44.0M
            if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] ||
1245
51.5k
                ctx->u.rep->pattern[2] == SRE_MAXREPEAT) &&
1246
43.9M
                state->ptr != ctx->u.rep->last_ptr) {
1247
                /* we may have enough matches, but if we can
1248
                   match another item, do so */
1249
43.9M
                ctx->u.rep->count = ctx->count;
1250
43.9M
                LASTMARK_SAVE();
1251
43.9M
                MARK_PUSH(ctx->lastmark);
1252
                /* zero-width match protection */
1253
43.9M
                LAST_PTR_PUSH();
1254
43.9M
                ctx->u.rep->last_ptr = state->ptr;
1255
43.9M
                DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
1256
43.9M
                        ctx->u.rep->pattern+3);
1257
43.9M
                LAST_PTR_POP();
1258
43.9M
                if (ret) {
1259
40.1M
                    MARK_POP_DISCARD(ctx->lastmark);
1260
40.1M
                    RETURN_ON_ERROR(ret);
1261
40.1M
                    RETURN_SUCCESS;
1262
40.1M
                }
1263
3.82M
                MARK_POP(ctx->lastmark);
1264
3.82M
                LASTMARK_RESTORE();
1265
3.82M
                ctx->u.rep->count = ctx->count-1;
1266
3.82M
                state->ptr = ptr;
1267
3.82M
            }
1268
1269
            /* cannot match more repeated items here.  make sure the
1270
               tail matches */
1271
3.87M
            state->repeat = ctx->u.rep->prev;
1272
3.87M
            state->save_marks--;
1273
3.87M
            DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, pattern);
1274
3.87M
            state->repeat = ctx->u.rep; // restore repeat before return
1275
3.87M
            state->save_marks++;
1276
1277
3.87M
            RETURN_ON_SUCCESS(ret);
1278
85.3k
            state->ptr = ptr;
1279
85.3k
            RETURN_FAILURE;
1280
1281
0
        TARGET(SRE_OP_MIN_UNTIL):
1282
            /* minimizing repeat */
1283
            /* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */
1284
1285
0
            ctx->u.rep = state->repeat;
1286
0
            if (!ctx->u.rep)
1287
0
                RETURN_ERROR(SRE_ERROR_STATE);
1288
1289
0
            state->ptr = ptr;
1290
1291
0
            ctx->count = ctx->u.rep->count+1;
1292
1293
0
            TRACE(("|%p|%p|MIN_UNTIL %zd %p\n", pattern,
1294
0
                   ptr, ctx->count, ctx->u.rep->pattern));
1295
1296
0
            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
1297
                /* not enough matches */
1298
0
                ctx->u.rep->count = ctx->count;
1299
0
                DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
1300
0
                        ctx->u.rep->pattern+3);
1301
0
                if (ret) {
1302
0
                    RETURN_ON_ERROR(ret);
1303
0
                    RETURN_SUCCESS;
1304
0
                }
1305
0
                ctx->u.rep->count = ctx->count-1;
1306
0
                state->ptr = ptr;
1307
0
                RETURN_FAILURE;
1308
0
            }
1309
1310
            /* see if the tail matches */
1311
0
            state->repeat = ctx->u.rep->prev;
1312
0
            state->save_marks--;
1313
1314
0
            LASTMARK_SAVE();
1315
0
            if (state->save_marks)
1316
0
                MARK_PUSH(ctx->lastmark);
1317
1318
0
            DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, pattern);
1319
            /* save_marks is balanced across the jump, so this equals the
1320
               value tested for MARK_PUSH above */
1321
0
            int pushed = state->save_marks != 0;
1322
0
            state->repeat = ctx->u.rep; // restore repeat before return
1323
0
            state->save_marks++;
1324
1325
0
            if (ret) {
1326
0
                if (pushed)
1327
0
                    MARK_POP_DISCARD(ctx->lastmark);
1328
0
                RETURN_ON_ERROR(ret);
1329
0
                RETURN_SUCCESS;
1330
0
            }
1331
0
            if (pushed)
1332
0
                MARK_POP(ctx->lastmark);
1333
0
            LASTMARK_RESTORE();
1334
1335
0
            state->ptr = ptr;
1336
1337
0
            if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2]
1338
0
                && ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
1339
0
                state->ptr == ctx->u.rep->last_ptr)
1340
0
                RETURN_FAILURE;
1341
1342
0
            ctx->u.rep->count = ctx->count;
1343
            /* zero-width match protection */
1344
0
            LAST_PTR_PUSH();
1345
0
            ctx->u.rep->last_ptr = state->ptr;
1346
0
            DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
1347
0
                    ctx->u.rep->pattern+3);
1348
0
            LAST_PTR_POP();
1349
0
            if (ret) {
1350
0
                RETURN_ON_ERROR(ret);
1351
0
                RETURN_SUCCESS;
1352
0
            }
1353
0
            ctx->u.rep->count = ctx->count-1;
1354
0
            state->ptr = ptr;
1355
0
            RETURN_FAILURE;
1356
1357
0
        TARGET(SRE_OP_POSSESSIVE_REPEAT):
1358
            /* create possessive repeat contexts. */
1359
            /* <POSSESSIVE_REPEAT> <skip> <1=min> <2=max> pattern
1360
               <SUCCESS> tail */
1361
0
            TRACE(("|%p|%p|POSSESSIVE_REPEAT %d %d\n", pattern,
1362
0
                   ptr, pattern[1], pattern[2]));
1363
1364
            /* Set the global Input pointer to this context's Input
1365
               pointer */
1366
0
            state->ptr = ptr;
1367
1368
            /* Capture groups in the body can be revisited on backtracking
1369
               between iterations, so their marks must be saved and restored,
1370
               as is done inside a repeat. */
1371
0
            state->save_marks++;
1372
1373
            /* Initialize Count to 0 */
1374
0
            ctx->count = 0;
1375
1376
            /* Check for minimum required matches. */
1377
0
            while (ctx->count < (Py_ssize_t)pattern[1]) {
1378
                /* not enough matches */
1379
0
                DO_JUMP0(JUMP_POSS_REPEAT_1, jump_poss_repeat_1,
1380
0
                         &pattern[3]);
1381
0
                if (ret) {
1382
0
                    RETURN_ON_ERROR(ret);
1383
0
                    ctx->count++;
1384
0
                }
1385
0
                else {
1386
0
                    state->ptr = ptr;
1387
0
                    state->save_marks--;
1388
0
                    RETURN_FAILURE;
1389
0
                }
1390
0
            }
1391
1392
            /* Clear the context's Input stream pointer so that it
1393
               doesn't match the global state so that the while loop can
1394
               be entered. */
1395
0
            ptr = NULL;
1396
1397
            /* Keep trying to parse the <pattern> sub-pattern until the
1398
               end is reached, creating a new context each time. */
1399
0
            while ((ctx->count < (Py_ssize_t)pattern[2] ||
1400
0
                    (Py_ssize_t)pattern[2] == SRE_MAXREPEAT) &&
1401
0
                   state->ptr != ptr) {
1402
                /* Save the Capture Group Marker state into the current
1403
                   Context and back up the current highest number
1404
                   Capture Group marker. */
1405
0
                LASTMARK_SAVE();
1406
0
                MARK_PUSH(ctx->lastmark);
1407
1408
                /* zero-width match protection */
1409
                /* Set the context's Input Stream pointer to be the
1410
                   current Input Stream pointer from the global
1411
                   state.  When the loop reaches the next iteration,
1412
                   the context will then store the last known good
1413
                   position with the global state holding the Input
1414
                   Input Stream position that has been updated with
1415
                   the most recent match.  Thus, if state's Input
1416
                   stream remains the same as the one stored in the
1417
                   current Context, we know we have successfully
1418
                   matched an empty string and that all subsequent
1419
                   matches will also be the empty string until the
1420
                   maximum number of matches are counted, and because
1421
                   of this, we could immediately stop at that point and
1422
                   consider this match successful. */
1423
0
                ptr = state->ptr;
1424
1425
                /* We have not reached the maximin matches, so try to
1426
                   match once more. */
1427
0
                DO_JUMP0(JUMP_POSS_REPEAT_2, jump_poss_repeat_2,
1428
0
                         &pattern[3]);
1429
1430
                /* Check to see if the last attempted match
1431
                   succeeded. */
1432
0
                if (ret) {
1433
                    /* Drop the saved highest number Capture Group
1434
                       marker saved above and use the newly updated
1435
                       value. */
1436
0
                    MARK_POP_DISCARD(ctx->lastmark);
1437
0
                    RETURN_ON_ERROR(ret);
1438
1439
                    /* Success, increment the count. */
1440
0
                    ctx->count++;
1441
0
                }
1442
                /* Last attempted match failed. */
1443
0
                else {
1444
                    /* Restore the previously saved highest number
1445
                       Capture Group marker since the last iteration
1446
                       did not match, then restore that to the global
1447
                       state. */
1448
0
                    MARK_POP(ctx->lastmark);
1449
0
                    LASTMARK_RESTORE();
1450
1451
                    /* Restore the global Input Stream pointer
1452
                       since it can change after jumps. */
1453
0
                    state->ptr = ptr;
1454
1455
                    /* We have sufficient matches, so exit loop. */
1456
0
                    break;
1457
0
                }
1458
0
            }
1459
1460
0
            state->save_marks--;
1461
1462
            /* Evaluate Tail */
1463
            /* Jump to end of pattern indicated by skip, and then skip
1464
               the SUCCESS op code that follows it. */
1465
0
            pattern += pattern[0] + 1;
1466
0
            ptr = state->ptr;
1467
0
            DISPATCH;
1468
1469
0
        TARGET(SRE_OP_ATOMIC_GROUP):
1470
            /* Atomic Group Sub Pattern */
1471
            /* <ATOMIC_GROUP> <skip> pattern <SUCCESS> tail */
1472
0
            TRACE(("|%p|%p|ATOMIC_GROUP\n", pattern, ptr));
1473
1474
            /* Set the global Input pointer to this context's Input
1475
               pointer */
1476
0
            state->ptr = ptr;
1477
1478
            /* Evaluate the Atomic Group in a new context, terminating
1479
               when the end of the group, represented by a SUCCESS op
1480
               code, is reached. */
1481
            /* Group Pattern begins at an offset of 1 code. */
1482
0
            DO_JUMP0(JUMP_ATOMIC_GROUP, jump_atomic_group,
1483
0
                     &pattern[1]);
1484
1485
            /* Test Exit Condition */
1486
0
            RETURN_ON_ERROR(ret);
1487
1488
0
            if (ret == 0) {
1489
                /* Atomic Group failed to Match. */
1490
0
                state->ptr = ptr;
1491
0
                RETURN_FAILURE;
1492
0
            }
1493
1494
            /* Evaluate Tail */
1495
            /* Jump to end of pattern indicated by skip, and then skip
1496
               the SUCCESS op code that follows it. */
1497
0
            pattern += pattern[0];
1498
0
            ptr = state->ptr;
1499
0
            DISPATCH;
1500
1501
0
        TARGET(SRE_OP_GROUPREF):
1502
            /* match backreference */
1503
0
            TRACE(("|%p|%p|GROUPREF %d\n", pattern,
1504
0
                   ptr, pattern[0]));
1505
0
            {
1506
0
                int groupref = pattern[0] * 2;
1507
0
                if (groupref >= state->lastmark) {
1508
0
                    RETURN_FAILURE;
1509
0
                } else {
1510
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1511
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1512
0
                    if (!p || !e || e < p)
1513
0
                        RETURN_FAILURE;
1514
0
                    while (p < e) {
1515
0
                        if (ptr >= end || *ptr != *p)
1516
0
                            RETURN_FAILURE;
1517
0
                        p++;
1518
0
                        ptr++;
1519
0
                    }
1520
0
                }
1521
0
            }
1522
0
            pattern++;
1523
0
            DISPATCH;
1524
1525
0
        TARGET(SRE_OP_GROUPREF_IGNORE):
1526
            /* match backreference */
1527
0
            TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", pattern,
1528
0
                   ptr, pattern[0]));
1529
0
            {
1530
0
                int groupref = pattern[0] * 2;
1531
0
                if (groupref >= state->lastmark) {
1532
0
                    RETURN_FAILURE;
1533
0
                } else {
1534
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1535
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1536
0
                    if (!p || !e || e < p)
1537
0
                        RETURN_FAILURE;
1538
0
                    while (p < e) {
1539
0
                        if (ptr >= end ||
1540
0
                            sre_lower_ascii(*ptr) != sre_lower_ascii(*p))
1541
0
                            RETURN_FAILURE;
1542
0
                        p++;
1543
0
                        ptr++;
1544
0
                    }
1545
0
                }
1546
0
            }
1547
0
            pattern++;
1548
0
            DISPATCH;
1549
1550
0
        TARGET(SRE_OP_GROUPREF_UNI_IGNORE):
1551
            /* match backreference */
1552
0
            TRACE(("|%p|%p|GROUPREF_UNI_IGNORE %d\n", pattern,
1553
0
                   ptr, pattern[0]));
1554
0
            {
1555
0
                int groupref = pattern[0] * 2;
1556
0
                if (groupref >= state->lastmark) {
1557
0
                    RETURN_FAILURE;
1558
0
                } else {
1559
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1560
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1561
0
                    if (!p || !e || e < p)
1562
0
                        RETURN_FAILURE;
1563
0
                    while (p < e) {
1564
0
                        if (ptr >= end ||
1565
0
                            sre_lower_unicode(*ptr) != sre_lower_unicode(*p))
1566
0
                            RETURN_FAILURE;
1567
0
                        p++;
1568
0
                        ptr++;
1569
0
                    }
1570
0
                }
1571
0
            }
1572
0
            pattern++;
1573
0
            DISPATCH;
1574
1575
0
        TARGET(SRE_OP_GROUPREF_LOC_IGNORE):
1576
            /* match backreference */
1577
0
            TRACE(("|%p|%p|GROUPREF_LOC_IGNORE %d\n", pattern,
1578
0
                   ptr, pattern[0]));
1579
0
            {
1580
0
                int groupref = pattern[0] * 2;
1581
0
                if (groupref >= state->lastmark) {
1582
0
                    RETURN_FAILURE;
1583
0
                } else {
1584
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1585
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1586
0
                    if (!p || !e || e < p)
1587
0
                        RETURN_FAILURE;
1588
0
                    while (p < e) {
1589
0
                        if (ptr >= end ||
1590
0
                            sre_lower_locale(*ptr) != sre_lower_locale(*p))
1591
0
                            RETURN_FAILURE;
1592
0
                        p++;
1593
0
                        ptr++;
1594
0
                    }
1595
0
                }
1596
0
            }
1597
0
            pattern++;
1598
0
            DISPATCH;
1599
1600
0
        TARGET(SRE_OP_GROUPREF_EXISTS):
1601
0
            TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", pattern,
1602
0
                   ptr, pattern[0]));
1603
            /* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */
1604
0
            {
1605
0
                int groupref = pattern[0] * 2;
1606
0
                if (groupref >= state->lastmark) {
1607
0
                    pattern += pattern[1];
1608
0
                    DISPATCH;
1609
0
                } else {
1610
0
                    SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1611
0
                    SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1612
0
                    if (!p || !e || e < p) {
1613
0
                        pattern += pattern[1];
1614
0
                        DISPATCH;
1615
0
                    }
1616
0
                }
1617
0
            }
1618
0
            pattern += 2;
1619
0
            DISPATCH;
1620
1621
0
        TARGET(SRE_OP_ASSERT):
1622
            /* assert subpattern */
1623
            /* <ASSERT> <skip> <back> <pattern> */
1624
0
            TRACE(("|%p|%p|ASSERT %d\n", pattern,
1625
0
                   ptr, pattern[1]));
1626
0
            if ((uintptr_t)(ptr - (SRE_CHAR *)state->beginning) < pattern[1])
1627
0
                RETURN_FAILURE;
1628
0
            state->ptr = ptr - pattern[1];
1629
0
            DO_JUMP0(JUMP_ASSERT, jump_assert, pattern+2);
1630
0
            RETURN_ON_FAILURE(ret);
1631
0
            pattern += pattern[0];
1632
0
            DISPATCH;
1633
1634
30.0M
        TARGET(SRE_OP_ASSERT_NOT):
1635
            /* assert not subpattern */
1636
            /* <ASSERT_NOT> <skip> <back> <pattern> */
1637
30.0M
            TRACE(("|%p|%p|ASSERT_NOT %d\n", pattern,
1638
30.0M
                   ptr, pattern[1]));
1639
30.0M
            if ((uintptr_t)(ptr - (SRE_CHAR *)state->beginning) >= pattern[1]) {
1640
30.0M
                state->ptr = ptr - pattern[1];
1641
30.0M
                LASTMARK_SAVE();
1642
30.0M
                if (state->save_marks)
1643
30.0M
                    MARK_PUSH(ctx->lastmark);
1644
1645
60.0M
                DO_JUMP0(JUMP_ASSERT_NOT, jump_assert_not, pattern+2);
1646
60.0M
                if (ret) {
1647
19.6k
                    if (state->save_marks)
1648
19.6k
                        MARK_POP_DISCARD(ctx->lastmark);
1649
19.6k
                    RETURN_ON_ERROR(ret);
1650
19.6k
                    RETURN_FAILURE;
1651
19.6k
                }
1652
30.0M
                if (state->save_marks)
1653
30.0M
                    MARK_POP(ctx->lastmark);
1654
30.0M
                LASTMARK_RESTORE();
1655
30.0M
            }
1656
30.0M
            pattern += pattern[0];
1657
30.0M
            DISPATCH;
1658
1659
30.0M
        TARGET(SRE_OP_FAILURE):
1660
            /* immediate failure */
1661
0
            TRACE(("|%p|%p|FAILURE\n", pattern, ptr));
1662
0
            RETURN_FAILURE;
1663
1664
#if !USE_COMPUTED_GOTOS
1665
        default:
1666
#endif
1667
        // Also any unused opcodes:
1668
0
        TARGET(SRE_OP_RANGE_UNI_IGNORE):
1669
0
        TARGET(SRE_OP_SUBPATTERN):
1670
0
        TARGET(SRE_OP_RANGE):
1671
0
        TARGET(SRE_OP_NEGATE):
1672
0
        TARGET(SRE_OP_BIGCHARSET):
1673
0
        TARGET(SRE_OP_CHARSET):
1674
0
            TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ptr,
1675
0
                   pattern[-1]));
1676
0
            RETURN_ERROR(SRE_ERROR_ILLEGAL);
1677
1678
0
    }
1679
1680
147M
exit:
1681
147M
    ctx_pos = ctx->last_ctx_pos;
1682
147M
    jump = ctx->jump;
1683
147M
    DATA_POP_DISCARD(ctx);
1684
147M
    if (ctx_pos == -1) {
1685
37.7M
        state->sigcount = sigcount;
1686
37.7M
        return ret;
1687
37.7M
    }
1688
110M
    DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
1689
1690
110M
    switch (jump) {
1691
43.9M
        case JUMP_MAX_UNTIL_2:
1692
43.9M
            TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", pattern, ptr));
1693
43.9M
            goto jump_max_until_2;
1694
3.87M
        case JUMP_MAX_UNTIL_3:
1695
3.87M
            TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", pattern, ptr));
1696
3.87M
            goto jump_max_until_3;
1697
0
        case JUMP_MIN_UNTIL_2:
1698
0
            TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", pattern, ptr));
1699
0
            goto jump_min_until_2;
1700
0
        case JUMP_MIN_UNTIL_3:
1701
0
            TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", pattern, ptr));
1702
0
            goto jump_min_until_3;
1703
765k
        case JUMP_BRANCH:
1704
765k
            TRACE(("|%p|%p|JUMP_BRANCH\n", pattern, ptr));
1705
765k
            goto jump_branch;
1706
3.70M
        case JUMP_MAX_UNTIL_1:
1707
3.70M
            TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", pattern, ptr));
1708
3.70M
            goto jump_max_until_1;
1709
0
        case JUMP_MIN_UNTIL_1:
1710
0
            TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", pattern, ptr));
1711
0
            goto jump_min_until_1;
1712
0
        case JUMP_POSS_REPEAT_1:
1713
0
            TRACE(("|%p|%p|JUMP_POSS_REPEAT_1\n", pattern, ptr));
1714
0
            goto jump_poss_repeat_1;
1715
0
        case JUMP_POSS_REPEAT_2:
1716
0
            TRACE(("|%p|%p|JUMP_POSS_REPEAT_2\n", pattern, ptr));
1717
0
            goto jump_poss_repeat_2;
1718
3.85M
        case JUMP_REPEAT:
1719
3.85M
            TRACE(("|%p|%p|JUMP_REPEAT\n", pattern, ptr));
1720
3.85M
            goto jump_repeat;
1721
1.94k
        case JUMP_REPEAT_ONE_1:
1722
1.94k
            TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", pattern, ptr));
1723
1.94k
            goto jump_repeat_one_1;
1724
23.9M
        case JUMP_REPEAT_ONE_2:
1725
23.9M
            TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", pattern, ptr));
1726
23.9M
            goto jump_repeat_one_2;
1727
0
        case JUMP_MIN_REPEAT_ONE:
1728
0
            TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", pattern, ptr));
1729
0
            goto jump_min_repeat_one;
1730
0
        case JUMP_ATOMIC_GROUP:
1731
0
            TRACE(("|%p|%p|JUMP_ATOMIC_GROUP\n", pattern, ptr));
1732
0
            goto jump_atomic_group;
1733
0
        case JUMP_ASSERT:
1734
0
            TRACE(("|%p|%p|JUMP_ASSERT\n", pattern, ptr));
1735
0
            goto jump_assert;
1736
30.0M
        case JUMP_ASSERT_NOT:
1737
30.0M
            TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", pattern, ptr));
1738
30.0M
            goto jump_assert_not;
1739
0
        case JUMP_NONE:
1740
0
            TRACE(("|%p|%p|RETURN %zd\n", pattern,
1741
0
                   ptr, ret));
1742
0
            break;
1743
110M
    }
1744
1745
0
    return ret; /* should never get here */
1746
110M
}
1747
1748
/* need to reset capturing groups between two SRE(match) callings in loops */
1749
#define RESET_CAPTURE_GROUP() \
1750
217M
    do { state->lastmark = state->lastindex = -1; } while (0)
1751
1752
LOCAL(Py_ssize_t)
1753
SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
1754
125M
{
1755
125M
    SRE_CHAR* ptr = (SRE_CHAR *)state->start;
1756
125M
    SRE_CHAR* end = (SRE_CHAR *)state->end;
1757
125M
    Py_ssize_t status = 0;
1758
125M
    Py_ssize_t prefix_len = 0;
1759
125M
    Py_ssize_t prefix_skip = 0;
1760
125M
    SRE_CODE* prefix = NULL;
1761
125M
    SRE_CODE* charset = NULL;
1762
125M
    SRE_CODE* overlap = NULL;
1763
125M
    int flags = 0;
1764
125M
    INIT_TRACE(state);
1765
1766
125M
    if (ptr > end)
1767
0
        return 0;
1768
1769
125M
    if (pattern[0] == SRE_OP_INFO) {
1770
        /* optimization info block */
1771
        /* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info>  */
1772
1773
125M
        flags = pattern[2];
1774
1775
125M
        if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) {
1776
3.88M
            TRACE(("reject (got %tu chars, need %zu)\n",
1777
3.88M
                   end - ptr, (size_t) pattern[3]));
1778
3.88M
            return 0;
1779
3.88M
        }
1780
121M
        if (pattern[3] > 1) {
1781
            /* adjust end point (but make sure we leave at least one
1782
               character in there, so literal search will work) */
1783
71.9k
            end -= pattern[3] - 1;
1784
71.9k
            if (end <= ptr)
1785
0
                end = ptr;
1786
71.9k
        }
1787
1788
121M
        if (flags & SRE_INFO_PREFIX) {
1789
            /* pattern starts with a known prefix */
1790
            /* <length> <skip> <prefix data> <overlap data> */
1791
71.9k
            prefix_len = pattern[5];
1792
71.9k
            prefix_skip = pattern[6];
1793
71.9k
            prefix = pattern + 7;
1794
71.9k
            overlap = prefix + prefix_len - 1;
1795
121M
        } else if (flags & SRE_INFO_CHARSET)
1796
            /* pattern starts with a character from a known set */
1797
            /* <charset> */
1798
107M
            charset = pattern + 5;
1799
1800
121M
        pattern += 1 + pattern[1];
1801
121M
    }
1802
1803
121M
    TRACE(("prefix = %p %zd %zd\n",
1804
121M
           prefix, prefix_len, prefix_skip));
1805
121M
    TRACE(("charset = %p\n", charset));
1806
1807
121M
    if (prefix_len == 1) {
1808
        /* pattern starts with a literal character */
1809
71.9k
        SRE_CHAR c = (SRE_CHAR) prefix[0];
1810
#if SIZEOF_SRE_CHAR < 4
1811
71.9k
        if ((SRE_CODE) c != prefix[0])
1812
0
            return 0; /* literal can't match: doesn't fit in char width */
1813
71.9k
#endif
1814
71.9k
        end = (SRE_CHAR *)state->end;
1815
71.9k
        state->must_advance = 0;
1816
115k
        while (ptr < end) {
1817
16.1M
            while (*ptr != c) {
1818
16.1M
                if (++ptr >= end)
1819
24.9k
                    return 0;
1820
16.1M
            }
1821
88.2k
            TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr));
1822
88.2k
            state->start = ptr;
1823
88.2k
            state->ptr = ptr + prefix_skip;
1824
88.2k
            if (flags & SRE_INFO_LITERAL)
1825
0
                return 1; /* we got all of it */
1826
88.2k
            status = SRE(match)(state, pattern + 2*prefix_skip, 0);
1827
88.2k
            if (status != 0)
1828
44.5k
                return status;
1829
43.6k
            ++ptr;
1830
43.6k
            RESET_CAPTURE_GROUP();
1831
43.6k
        }
1832
2.39k
        return 0;
1833
71.9k
    }
1834
1835
121M
    if (prefix_len > 1) {
1836
        /* pattern starts with a known prefix.  use the overlap
1837
           table to skip forward as fast as we possibly can */
1838
0
        Py_ssize_t i = 0;
1839
1840
0
        end = (SRE_CHAR *)state->end;
1841
0
        if (prefix_len > end - ptr)
1842
0
            return 0;
1843
#if SIZEOF_SRE_CHAR < 4
1844
0
        for (i = 0; i < prefix_len; i++)
1845
0
            if ((SRE_CODE)(SRE_CHAR) prefix[i] != prefix[i])
1846
0
                return 0; /* literal can't match: doesn't fit in char width */
1847
0
#endif
1848
0
        while (ptr < end) {
1849
0
            SRE_CHAR c = (SRE_CHAR) prefix[0];
1850
0
            while (*ptr++ != c) {
1851
0
                if (ptr >= end)
1852
0
                    return 0;
1853
0
            }
1854
0
            if (ptr >= end)
1855
0
                return 0;
1856
1857
0
            i = 1;
1858
0
            state->must_advance = 0;
1859
0
            do {
1860
0
                if (*ptr == (SRE_CHAR) prefix[i]) {
1861
0
                    if (++i != prefix_len) {
1862
0
                        if (++ptr >= end)
1863
0
                            return 0;
1864
0
                        continue;
1865
0
                    }
1866
                    /* found a potential match */
1867
0
                    TRACE(("|%p|%p|SEARCH SCAN\n", pattern, ptr));
1868
0
                    state->start = ptr - (prefix_len - 1);
1869
0
                    state->ptr = ptr - (prefix_len - prefix_skip - 1);
1870
0
                    if (flags & SRE_INFO_LITERAL)
1871
0
                        return 1; /* we got all of it */
1872
0
                    status = SRE(match)(state, pattern + 2*prefix_skip, 0);
1873
0
                    if (status != 0)
1874
0
                        return status;
1875
                    /* close but no cigar -- try again */
1876
0
                    if (++ptr >= end)
1877
0
                        return 0;
1878
0
                    RESET_CAPTURE_GROUP();
1879
0
                }
1880
0
                i = overlap[i];
1881
0
            } while (i != 0);
1882
0
        }
1883
0
        return 0;
1884
0
    }
1885
1886
121M
    if (charset) {
1887
        /* pattern starts with a character from a known set */
1888
107M
        end = (SRE_CHAR *)state->end;
1889
107M
        state->must_advance = 0;
1890
110M
        for (;;) {
1891
438M
            while (ptr < end && !SRE(charset)(state, charset, *ptr))
1892
327M
                ptr++;
1893
110M
            if (ptr >= end)
1894
3.85M
                return 0;
1895
106M
            TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr));
1896
106M
            state->start = ptr;
1897
106M
            state->ptr = ptr;
1898
106M
            status = SRE(match)(state, pattern, 0);
1899
106M
            if (status != 0)
1900
103M
                break;
1901
3.02M
            ptr++;
1902
3.02M
            RESET_CAPTURE_GROUP();
1903
3.02M
        }
1904
107M
    } else {
1905
        /* general case */
1906
13.7M
        assert(ptr <= end);
1907
13.7M
        TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
1908
13.7M
        state->start = state->ptr = ptr;
1909
13.7M
        status = SRE(match)(state, pattern, 1);
1910
13.7M
        state->must_advance = 0;
1911
13.7M
        if (status == 0 && pattern[0] == SRE_OP_AT &&
1912
8.96M
            (pattern[1] == SRE_AT_BEGINNING ||
1913
0
             pattern[1] == SRE_AT_BEGINNING_STRING))
1914
8.96M
        {
1915
8.96M
            state->start = state->ptr = ptr = end;
1916
8.96M
            return 0;
1917
8.96M
        }
1918
219M
        while (status == 0 && ptr < end) {
1919
214M
            ptr++;
1920
214M
            RESET_CAPTURE_GROUP();
1921
214M
            TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
1922
214M
            state->start = state->ptr = ptr;
1923
214M
            status = SRE(match)(state, pattern, 0);
1924
214M
        }
1925
4.74M
    }
1926
1927
108M
    return status;
1928
121M
}
sre.c:sre_ucs1_search
Line
Count
Source
1754
68.7M
{
1755
68.7M
    SRE_CHAR* ptr = (SRE_CHAR *)state->start;
1756
68.7M
    SRE_CHAR* end = (SRE_CHAR *)state->end;
1757
68.7M
    Py_ssize_t status = 0;
1758
68.7M
    Py_ssize_t prefix_len = 0;
1759
68.7M
    Py_ssize_t prefix_skip = 0;
1760
68.7M
    SRE_CODE* prefix = NULL;
1761
68.7M
    SRE_CODE* charset = NULL;
1762
68.7M
    SRE_CODE* overlap = NULL;
1763
68.7M
    int flags = 0;
1764
68.7M
    INIT_TRACE(state);
1765
1766
68.7M
    if (ptr > end)
1767
0
        return 0;
1768
1769
68.7M
    if (pattern[0] == SRE_OP_INFO) {
1770
        /* optimization info block */
1771
        /* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info>  */
1772
1773
68.7M
        flags = pattern[2];
1774
1775
68.7M
        if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) {
1776
3.78M
            TRACE(("reject (got %tu chars, need %zu)\n",
1777
3.78M
                   end - ptr, (size_t) pattern[3]));
1778
3.78M
            return 0;
1779
3.78M
        }
1780
64.9M
        if (pattern[3] > 1) {
1781
            /* adjust end point (but make sure we leave at least one
1782
               character in there, so literal search will work) */
1783
71.9k
            end -= pattern[3] - 1;
1784
71.9k
            if (end <= ptr)
1785
0
                end = ptr;
1786
71.9k
        }
1787
1788
64.9M
        if (flags & SRE_INFO_PREFIX) {
1789
            /* pattern starts with a known prefix */
1790
            /* <length> <skip> <prefix data> <overlap data> */
1791
71.9k
            prefix_len = pattern[5];
1792
71.9k
            prefix_skip = pattern[6];
1793
71.9k
            prefix = pattern + 7;
1794
71.9k
            overlap = prefix + prefix_len - 1;
1795
64.8M
        } else if (flags & SRE_INFO_CHARSET)
1796
            /* pattern starts with a character from a known set */
1797
            /* <charset> */
1798
54.3M
            charset = pattern + 5;
1799
1800
64.9M
        pattern += 1 + pattern[1];
1801
64.9M
    }
1802
1803
64.9M
    TRACE(("prefix = %p %zd %zd\n",
1804
64.9M
           prefix, prefix_len, prefix_skip));
1805
64.9M
    TRACE(("charset = %p\n", charset));
1806
1807
64.9M
    if (prefix_len == 1) {
1808
        /* pattern starts with a literal character */
1809
71.9k
        SRE_CHAR c = (SRE_CHAR) prefix[0];
1810
71.9k
#if SIZEOF_SRE_CHAR < 4
1811
71.9k
        if ((SRE_CODE) c != prefix[0])
1812
0
            return 0; /* literal can't match: doesn't fit in char width */
1813
71.9k
#endif
1814
71.9k
        end = (SRE_CHAR *)state->end;
1815
71.9k
        state->must_advance = 0;
1816
115k
        while (ptr < end) {
1817
16.1M
            while (*ptr != c) {
1818
16.1M
                if (++ptr >= end)
1819
24.9k
                    return 0;
1820
16.1M
            }
1821
88.2k
            TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr));
1822
88.2k
            state->start = ptr;
1823
88.2k
            state->ptr = ptr + prefix_skip;
1824
88.2k
            if (flags & SRE_INFO_LITERAL)
1825
0
                return 1; /* we got all of it */
1826
88.2k
            status = SRE(match)(state, pattern + 2*prefix_skip, 0);
1827
88.2k
            if (status != 0)
1828
44.5k
                return status;
1829
43.6k
            ++ptr;
1830
43.6k
            RESET_CAPTURE_GROUP();
1831
43.6k
        }
1832
2.39k
        return 0;
1833
71.9k
    }
1834
1835
64.8M
    if (prefix_len > 1) {
1836
        /* pattern starts with a known prefix.  use the overlap
1837
           table to skip forward as fast as we possibly can */
1838
0
        Py_ssize_t i = 0;
1839
1840
0
        end = (SRE_CHAR *)state->end;
1841
0
        if (prefix_len > end - ptr)
1842
0
            return 0;
1843
0
#if SIZEOF_SRE_CHAR < 4
1844
0
        for (i = 0; i < prefix_len; i++)
1845
0
            if ((SRE_CODE)(SRE_CHAR) prefix[i] != prefix[i])
1846
0
                return 0; /* literal can't match: doesn't fit in char width */
1847
0
#endif
1848
0
        while (ptr < end) {
1849
0
            SRE_CHAR c = (SRE_CHAR) prefix[0];
1850
0
            while (*ptr++ != c) {
1851
0
                if (ptr >= end)
1852
0
                    return 0;
1853
0
            }
1854
0
            if (ptr >= end)
1855
0
                return 0;
1856
1857
0
            i = 1;
1858
0
            state->must_advance = 0;
1859
0
            do {
1860
0
                if (*ptr == (SRE_CHAR) prefix[i]) {
1861
0
                    if (++i != prefix_len) {
1862
0
                        if (++ptr >= end)
1863
0
                            return 0;
1864
0
                        continue;
1865
0
                    }
1866
                    /* found a potential match */
1867
0
                    TRACE(("|%p|%p|SEARCH SCAN\n", pattern, ptr));
1868
0
                    state->start = ptr - (prefix_len - 1);
1869
0
                    state->ptr = ptr - (prefix_len - prefix_skip - 1);
1870
0
                    if (flags & SRE_INFO_LITERAL)
1871
0
                        return 1; /* we got all of it */
1872
0
                    status = SRE(match)(state, pattern + 2*prefix_skip, 0);
1873
0
                    if (status != 0)
1874
0
                        return status;
1875
                    /* close but no cigar -- try again */
1876
0
                    if (++ptr >= end)
1877
0
                        return 0;
1878
0
                    RESET_CAPTURE_GROUP();
1879
0
                }
1880
0
                i = overlap[i];
1881
0
            } while (i != 0);
1882
0
        }
1883
0
        return 0;
1884
0
    }
1885
1886
64.8M
    if (charset) {
1887
        /* pattern starts with a character from a known set */
1888
54.3M
        end = (SRE_CHAR *)state->end;
1889
54.3M
        state->must_advance = 0;
1890
56.4M
        for (;;) {
1891
137M
            while (ptr < end && !SRE(charset)(state, charset, *ptr))
1892
81.0M
                ptr++;
1893
56.4M
            if (ptr >= end)
1894
2.77M
                return 0;
1895
53.6M
            TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr));
1896
53.6M
            state->start = ptr;
1897
53.6M
            state->ptr = ptr;
1898
53.6M
            status = SRE(match)(state, pattern, 0);
1899
53.6M
            if (status != 0)
1900
51.5M
                break;
1901
2.08M
            ptr++;
1902
2.08M
            RESET_CAPTURE_GROUP();
1903
2.08M
        }
1904
54.3M
    } else {
1905
        /* general case */
1906
10.5M
        assert(ptr <= end);
1907
10.5M
        TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
1908
10.5M
        state->start = state->ptr = ptr;
1909
10.5M
        status = SRE(match)(state, pattern, 1);
1910
10.5M
        state->must_advance = 0;
1911
10.5M
        if (status == 0 && pattern[0] == SRE_OP_AT &&
1912
6.79M
            (pattern[1] == SRE_AT_BEGINNING ||
1913
0
             pattern[1] == SRE_AT_BEGINNING_STRING))
1914
6.79M
        {
1915
6.79M
            state->start = state->ptr = ptr = end;
1916
6.79M
            return 0;
1917
6.79M
        }
1918
56.9M
        while (status == 0 && ptr < end) {
1919
53.1M
            ptr++;
1920
53.1M
            RESET_CAPTURE_GROUP();
1921
53.1M
            TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
1922
53.1M
            state->start = state->ptr = ptr;
1923
53.1M
            status = SRE(match)(state, pattern, 0);
1924
53.1M
        }
1925
3.75M
    }
1926
1927
55.3M
    return status;
1928
64.8M
}
sre.c:sre_ucs2_search
Line
Count
Source
1754
49.5M
{
1755
49.5M
    SRE_CHAR* ptr = (SRE_CHAR *)state->start;
1756
49.5M
    SRE_CHAR* end = (SRE_CHAR *)state->end;
1757
49.5M
    Py_ssize_t status = 0;
1758
49.5M
    Py_ssize_t prefix_len = 0;
1759
49.5M
    Py_ssize_t prefix_skip = 0;
1760
49.5M
    SRE_CODE* prefix = NULL;
1761
49.5M
    SRE_CODE* charset = NULL;
1762
49.5M
    SRE_CODE* overlap = NULL;
1763
49.5M
    int flags = 0;
1764
49.5M
    INIT_TRACE(state);
1765
1766
49.5M
    if (ptr > end)
1767
0
        return 0;
1768
1769
49.5M
    if (pattern[0] == SRE_OP_INFO) {
1770
        /* optimization info block */
1771
        /* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info>  */
1772
1773
49.5M
        flags = pattern[2];
1774
1775
49.5M
        if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) {
1776
93.9k
            TRACE(("reject (got %tu chars, need %zu)\n",
1777
93.9k
                   end - ptr, (size_t) pattern[3]));
1778
93.9k
            return 0;
1779
93.9k
        }
1780
49.4M
        if (pattern[3] > 1) {
1781
            /* adjust end point (but make sure we leave at least one
1782
               character in there, so literal search will work) */
1783
0
            end -= pattern[3] - 1;
1784
0
            if (end <= ptr)
1785
0
                end = ptr;
1786
0
        }
1787
1788
49.4M
        if (flags & SRE_INFO_PREFIX) {
1789
            /* pattern starts with a known prefix */
1790
            /* <length> <skip> <prefix data> <overlap data> */
1791
0
            prefix_len = pattern[5];
1792
0
            prefix_skip = pattern[6];
1793
0
            prefix = pattern + 7;
1794
0
            overlap = prefix + prefix_len - 1;
1795
49.4M
        } else if (flags & SRE_INFO_CHARSET)
1796
            /* pattern starts with a character from a known set */
1797
            /* <charset> */
1798
46.4M
            charset = pattern + 5;
1799
1800
49.4M
        pattern += 1 + pattern[1];
1801
49.4M
    }
1802
1803
49.4M
    TRACE(("prefix = %p %zd %zd\n",
1804
49.4M
           prefix, prefix_len, prefix_skip));
1805
49.4M
    TRACE(("charset = %p\n", charset));
1806
1807
49.4M
    if (prefix_len == 1) {
1808
        /* pattern starts with a literal character */
1809
0
        SRE_CHAR c = (SRE_CHAR) prefix[0];
1810
0
#if SIZEOF_SRE_CHAR < 4
1811
0
        if ((SRE_CODE) c != prefix[0])
1812
0
            return 0; /* literal can't match: doesn't fit in char width */
1813
0
#endif
1814
0
        end = (SRE_CHAR *)state->end;
1815
0
        state->must_advance = 0;
1816
0
        while (ptr < end) {
1817
0
            while (*ptr != c) {
1818
0
                if (++ptr >= end)
1819
0
                    return 0;
1820
0
            }
1821
0
            TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr));
1822
0
            state->start = ptr;
1823
0
            state->ptr = ptr + prefix_skip;
1824
0
            if (flags & SRE_INFO_LITERAL)
1825
0
                return 1; /* we got all of it */
1826
0
            status = SRE(match)(state, pattern + 2*prefix_skip, 0);
1827
0
            if (status != 0)
1828
0
                return status;
1829
0
            ++ptr;
1830
0
            RESET_CAPTURE_GROUP();
1831
0
        }
1832
0
        return 0;
1833
0
    }
1834
1835
49.4M
    if (prefix_len > 1) {
1836
        /* pattern starts with a known prefix.  use the overlap
1837
           table to skip forward as fast as we possibly can */
1838
0
        Py_ssize_t i = 0;
1839
1840
0
        end = (SRE_CHAR *)state->end;
1841
0
        if (prefix_len > end - ptr)
1842
0
            return 0;
1843
0
#if SIZEOF_SRE_CHAR < 4
1844
0
        for (i = 0; i < prefix_len; i++)
1845
0
            if ((SRE_CODE)(SRE_CHAR) prefix[i] != prefix[i])
1846
0
                return 0; /* literal can't match: doesn't fit in char width */
1847
0
#endif
1848
0
        while (ptr < end) {
1849
0
            SRE_CHAR c = (SRE_CHAR) prefix[0];
1850
0
            while (*ptr++ != c) {
1851
0
                if (ptr >= end)
1852
0
                    return 0;
1853
0
            }
1854
0
            if (ptr >= end)
1855
0
                return 0;
1856
1857
0
            i = 1;
1858
0
            state->must_advance = 0;
1859
0
            do {
1860
0
                if (*ptr == (SRE_CHAR) prefix[i]) {
1861
0
                    if (++i != prefix_len) {
1862
0
                        if (++ptr >= end)
1863
0
                            return 0;
1864
0
                        continue;
1865
0
                    }
1866
                    /* found a potential match */
1867
0
                    TRACE(("|%p|%p|SEARCH SCAN\n", pattern, ptr));
1868
0
                    state->start = ptr - (prefix_len - 1);
1869
0
                    state->ptr = ptr - (prefix_len - prefix_skip - 1);
1870
0
                    if (flags & SRE_INFO_LITERAL)
1871
0
                        return 1; /* we got all of it */
1872
0
                    status = SRE(match)(state, pattern + 2*prefix_skip, 0);
1873
0
                    if (status != 0)
1874
0
                        return status;
1875
                    /* close but no cigar -- try again */
1876
0
                    if (++ptr >= end)
1877
0
                        return 0;
1878
0
                    RESET_CAPTURE_GROUP();
1879
0
                }
1880
0
                i = overlap[i];
1881
0
            } while (i != 0);
1882
0
        }
1883
0
        return 0;
1884
0
    }
1885
1886
49.4M
    if (charset) {
1887
        /* pattern starts with a character from a known set */
1888
46.4M
        end = (SRE_CHAR *)state->end;
1889
46.4M
        state->must_advance = 0;
1890
46.9M
        for (;;) {
1891
229M
            while (ptr < end && !SRE(charset)(state, charset, *ptr))
1892
182M
                ptr++;
1893
46.9M
            if (ptr >= end)
1894
1.02M
                return 0;
1895
45.8M
            TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr));
1896
45.8M
            state->start = ptr;
1897
45.8M
            state->ptr = ptr;
1898
45.8M
            status = SRE(match)(state, pattern, 0);
1899
45.8M
            if (status != 0)
1900
45.4M
                break;
1901
467k
            ptr++;
1902
467k
            RESET_CAPTURE_GROUP();
1903
467k
        }
1904
46.4M
    } else {
1905
        /* general case */
1906
2.98M
        assert(ptr <= end);
1907
2.98M
        TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
1908
2.98M
        state->start = state->ptr = ptr;
1909
2.98M
        status = SRE(match)(state, pattern, 1);
1910
2.98M
        state->must_advance = 0;
1911
2.98M
        if (status == 0 && pattern[0] == SRE_OP_AT &&
1912
2.14M
            (pattern[1] == SRE_AT_BEGINNING ||
1913
0
             pattern[1] == SRE_AT_BEGINNING_STRING))
1914
2.14M
        {
1915
2.14M
            state->start = state->ptr = ptr = end;
1916
2.14M
            return 0;
1917
2.14M
        }
1918
132M
        while (status == 0 && ptr < end) {
1919
131M
            ptr++;
1920
131M
            RESET_CAPTURE_GROUP();
1921
131M
            TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
1922
131M
            state->start = state->ptr = ptr;
1923
131M
            status = SRE(match)(state, pattern, 0);
1924
131M
        }
1925
835k
    }
1926
1927
46.2M
    return status;
1928
49.4M
}
sre.c:sre_ucs4_search
Line
Count
Source
1754
6.92M
{
1755
6.92M
    SRE_CHAR* ptr = (SRE_CHAR *)state->start;
1756
6.92M
    SRE_CHAR* end = (SRE_CHAR *)state->end;
1757
6.92M
    Py_ssize_t status = 0;
1758
6.92M
    Py_ssize_t prefix_len = 0;
1759
6.92M
    Py_ssize_t prefix_skip = 0;
1760
6.92M
    SRE_CODE* prefix = NULL;
1761
6.92M
    SRE_CODE* charset = NULL;
1762
6.92M
    SRE_CODE* overlap = NULL;
1763
6.92M
    int flags = 0;
1764
6.92M
    INIT_TRACE(state);
1765
1766
6.92M
    if (ptr > end)
1767
0
        return 0;
1768
1769
6.92M
    if (pattern[0] == SRE_OP_INFO) {
1770
        /* optimization info block */
1771
        /* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info>  */
1772
1773
6.92M
        flags = pattern[2];
1774
1775
6.92M
        if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) {
1776
5.27k
            TRACE(("reject (got %tu chars, need %zu)\n",
1777
5.27k
                   end - ptr, (size_t) pattern[3]));
1778
5.27k
            return 0;
1779
5.27k
        }
1780
6.92M
        if (pattern[3] > 1) {
1781
            /* adjust end point (but make sure we leave at least one
1782
               character in there, so literal search will work) */
1783
0
            end -= pattern[3] - 1;
1784
0
            if (end <= ptr)
1785
0
                end = ptr;
1786
0
        }
1787
1788
6.92M
        if (flags & SRE_INFO_PREFIX) {
1789
            /* pattern starts with a known prefix */
1790
            /* <length> <skip> <prefix data> <overlap data> */
1791
0
            prefix_len = pattern[5];
1792
0
            prefix_skip = pattern[6];
1793
0
            prefix = pattern + 7;
1794
0
            overlap = prefix + prefix_len - 1;
1795
6.92M
        } else if (flags & SRE_INFO_CHARSET)
1796
            /* pattern starts with a character from a known set */
1797
            /* <charset> */
1798
6.75M
            charset = pattern + 5;
1799
1800
6.92M
        pattern += 1 + pattern[1];
1801
6.92M
    }
1802
1803
6.92M
    TRACE(("prefix = %p %zd %zd\n",
1804
6.92M
           prefix, prefix_len, prefix_skip));
1805
6.92M
    TRACE(("charset = %p\n", charset));
1806
1807
6.92M
    if (prefix_len == 1) {
1808
        /* pattern starts with a literal character */
1809
0
        SRE_CHAR c = (SRE_CHAR) prefix[0];
1810
#if SIZEOF_SRE_CHAR < 4
1811
        if ((SRE_CODE) c != prefix[0])
1812
            return 0; /* literal can't match: doesn't fit in char width */
1813
#endif
1814
0
        end = (SRE_CHAR *)state->end;
1815
0
        state->must_advance = 0;
1816
0
        while (ptr < end) {
1817
0
            while (*ptr != c) {
1818
0
                if (++ptr >= end)
1819
0
                    return 0;
1820
0
            }
1821
0
            TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr));
1822
0
            state->start = ptr;
1823
0
            state->ptr = ptr + prefix_skip;
1824
0
            if (flags & SRE_INFO_LITERAL)
1825
0
                return 1; /* we got all of it */
1826
0
            status = SRE(match)(state, pattern + 2*prefix_skip, 0);
1827
0
            if (status != 0)
1828
0
                return status;
1829
0
            ++ptr;
1830
0
            RESET_CAPTURE_GROUP();
1831
0
        }
1832
0
        return 0;
1833
0
    }
1834
1835
6.92M
    if (prefix_len > 1) {
1836
        /* pattern starts with a known prefix.  use the overlap
1837
           table to skip forward as fast as we possibly can */
1838
0
        Py_ssize_t i = 0;
1839
1840
0
        end = (SRE_CHAR *)state->end;
1841
0
        if (prefix_len > end - ptr)
1842
0
            return 0;
1843
#if SIZEOF_SRE_CHAR < 4
1844
        for (i = 0; i < prefix_len; i++)
1845
            if ((SRE_CODE)(SRE_CHAR) prefix[i] != prefix[i])
1846
                return 0; /* literal can't match: doesn't fit in char width */
1847
#endif
1848
0
        while (ptr < end) {
1849
0
            SRE_CHAR c = (SRE_CHAR) prefix[0];
1850
0
            while (*ptr++ != c) {
1851
0
                if (ptr >= end)
1852
0
                    return 0;
1853
0
            }
1854
0
            if (ptr >= end)
1855
0
                return 0;
1856
1857
0
            i = 1;
1858
0
            state->must_advance = 0;
1859
0
            do {
1860
0
                if (*ptr == (SRE_CHAR) prefix[i]) {
1861
0
                    if (++i != prefix_len) {
1862
0
                        if (++ptr >= end)
1863
0
                            return 0;
1864
0
                        continue;
1865
0
                    }
1866
                    /* found a potential match */
1867
0
                    TRACE(("|%p|%p|SEARCH SCAN\n", pattern, ptr));
1868
0
                    state->start = ptr - (prefix_len - 1);
1869
0
                    state->ptr = ptr - (prefix_len - prefix_skip - 1);
1870
0
                    if (flags & SRE_INFO_LITERAL)
1871
0
                        return 1; /* we got all of it */
1872
0
                    status = SRE(match)(state, pattern + 2*prefix_skip, 0);
1873
0
                    if (status != 0)
1874
0
                        return status;
1875
                    /* close but no cigar -- try again */
1876
0
                    if (++ptr >= end)
1877
0
                        return 0;
1878
0
                    RESET_CAPTURE_GROUP();
1879
0
                }
1880
0
                i = overlap[i];
1881
0
            } while (i != 0);
1882
0
        }
1883
0
        return 0;
1884
0
    }
1885
1886
6.92M
    if (charset) {
1887
        /* pattern starts with a character from a known set */
1888
6.75M
        end = (SRE_CHAR *)state->end;
1889
6.75M
        state->must_advance = 0;
1890
7.22M
        for (;;) {
1891
70.9M
            while (ptr < end && !SRE(charset)(state, charset, *ptr))
1892
63.7M
                ptr++;
1893
7.22M
            if (ptr >= end)
1894
56.6k
                return 0;
1895
7.17M
            TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr));
1896
7.17M
            state->start = ptr;
1897
7.17M
            state->ptr = ptr;
1898
7.17M
            status = SRE(match)(state, pattern, 0);
1899
7.17M
            if (status != 0)
1900
6.69M
                break;
1901
473k
            ptr++;
1902
473k
            RESET_CAPTURE_GROUP();
1903
473k
        }
1904
6.75M
    } else {
1905
        /* general case */
1906
169k
        assert(ptr <= end);
1907
169k
        TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
1908
169k
        state->start = state->ptr = ptr;
1909
169k
        status = SRE(match)(state, pattern, 1);
1910
169k
        state->must_advance = 0;
1911
169k
        if (status == 0 && pattern[0] == SRE_OP_AT &&
1912
16.5k
            (pattern[1] == SRE_AT_BEGINNING ||
1913
0
             pattern[1] == SRE_AT_BEGINNING_STRING))
1914
16.5k
        {
1915
16.5k
            state->start = state->ptr = ptr = end;
1916
16.5k
            return 0;
1917
16.5k
        }
1918
30.0M
        while (status == 0 && ptr < end) {
1919
29.9M
            ptr++;
1920
29.9M
            RESET_CAPTURE_GROUP();
1921
29.9M
            TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
1922
29.9M
            state->start = state->ptr = ptr;
1923
29.9M
            status = SRE(match)(state, pattern, 0);
1924
29.9M
        }
1925
152k
    }
1926
1927
6.84M
    return status;
1928
6.92M
}
1929
1930
#undef SRE_CHAR
1931
#undef SIZEOF_SRE_CHAR
1932
#undef SRE
1933
1934
/* vim:ts=4:sw=4:et
1935
*/