Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Speed up matching of regular expression alternations whose alternatives
start with a capturing group or with a repeat with a nonzero minimum:
such alternatives can now be skipped without entering them when their
first character cannot match.
36 changes: 36 additions & 0 deletions Modules/_sre/sre_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,42 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
!SRE(charset)(state, pattern + 3,
(SRE_CODE) *ptr)))
continue;
if (pattern[1] == SRE_OP_MARK) {
/* the quick checks above also apply behind group marks
and to a repeat of one item with a nonzero minimum */
const SRE_CODE *alt = pattern + 1;
do {
alt += 2;
} while (alt[0] == SRE_OP_MARK);
if ((alt[0] == SRE_OP_REPEAT_ONE ||
alt[0] == SRE_OP_MIN_REPEAT_ONE ||
alt[0] == SRE_OP_POSSESSIVE_REPEAT_ONE) &&
alt[2] > 0)
alt += 4;
if (alt[0] == SRE_OP_LITERAL &&
(ptr >= end ||
(SRE_CODE) *ptr != alt[1]))
continue;
if (alt[0] == SRE_OP_IN &&
(ptr >= end ||
!SRE(charset)(state, alt + 2,
(SRE_CODE) *ptr)))
continue;
}
else if ((pattern[1] == SRE_OP_REPEAT_ONE ||
pattern[1] == SRE_OP_MIN_REPEAT_ONE ||
pattern[1] == SRE_OP_POSSESSIVE_REPEAT_ONE) &&
pattern[3] > 0) {
if (pattern[5] == SRE_OP_LITERAL &&
(ptr >= end ||
(SRE_CODE) *ptr != pattern[6]))
continue;
if (pattern[5] == SRE_OP_IN &&
(ptr >= end ||
!SRE(charset)(state, pattern + 7,
(SRE_CODE) *ptr)))
continue;
}
state->ptr = ptr;
DO_JUMP(JUMP_BRANCH, jump_branch, pattern+1);
if (ret) {
Expand Down
Loading