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,2 @@
Multiline regexes starting with a caret, such as ``re.compile("^foo",
re.MULTILINE)``, now run significantly faster.
41 changes: 35 additions & 6 deletions Modules/_sre/sre_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1854,12 +1854,41 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
state->start = state->ptr = ptr = end;
return 0;
}
while (status == 0 && ptr < end) {
ptr++;
RESET_CAPTURE_GROUP();
TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
state->start = state->ptr = ptr;
status = SRE(match)(state, pattern, 0);
if (pattern[0] == SRE_OP_AT && pattern[1] == SRE_AT_BEGINNING_LINE) {
/* skip to line boundaries */
while (status == 0 && ptr < end) {
ptr++;
if (!SRE_IS_LINEBREAK((int) ptr[-1])) {
#if SIZEOF_SRE_CHAR == 1
ptr = (SRE_CHAR *)memchr(ptr, '\n', end - ptr);
if (!ptr) {
break;
}
#else
while (ptr < end && !SRE_IS_LINEBREAK((int) *ptr)) {
ptr++;
}
if (ptr >= end) {
break;
}
#endif
/* advance to after the new line character */
ptr++;
}
RESET_CAPTURE_GROUP();
TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
state->start = state->ptr = ptr;
status = SRE(match)(state, pattern, 0);
}
}
else {
while (status == 0 && ptr < end) {
ptr++;
RESET_CAPTURE_GROUP();
TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
state->start = state->ptr = ptr;
status = SRE(match)(state, pattern, 0);
}
}
}

Expand Down
Loading