Skip to content

Commit 22f498f

Browse files
committed
gh-148762: speed up caret match in regexes
Signed-off-by: Harmen Stoppels <harmenstoppels@gmail.com>
1 parent 7ce737e commit 22f498f

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Multiline regexes starting with a caret, such as ``re.compile("^foo",
2+
re.MULTILINE)``, now run significantly faster.

Modules/_sre/sre_lib.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,18 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
18551855
return 0;
18561856
}
18571857
while (status == 0 && ptr < end) {
1858+
if (pattern[0] == SRE_OP_AT &&
1859+
pattern[1] == SRE_AT_BEGINNING_LINE &&
1860+
!SRE_IS_LINEBREAK((int) ptr[-1]))
1861+
{
1862+
/* fast-forward to the next newline character */
1863+
while (ptr < end && !SRE_IS_LINEBREAK((int) *ptr)) {
1864+
ptr++;
1865+
}
1866+
if (ptr >= end) {
1867+
return 0;
1868+
}
1869+
}
18581870
ptr++;
18591871
RESET_CAPTURE_GROUP();
18601872
TRACE(("|%p|%p|SEARCH\n", pattern, ptr));

0 commit comments

Comments
 (0)