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
14 changes: 14 additions & 0 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -2712,6 +2712,20 @@ def test_search_anchor_at_beginning(self):
# With optimization -- 0.0003 seconds.
self.assertLess(stopwatch.seconds, 0.1)

def test_search_anchor_at_beginning_line(self):
# Test "^" anchor with UCS-1, UCS-2 and UCS-4 characters.
p = re.compile('^a', re.MULTILINE)
self.assertEqual([m.span() for m in p.finditer('a\n\xe0a\na')], [(0, 1), (5, 6)])
self.assertEqual([m.span() for m in p.finditer('a\n\u0430a\na')], [(0, 1), (5, 6)])
self.assertEqual([m.span() for m in p.finditer('a\n\U0001d49ca\na')], [(0, 1), (5, 6)])

def test_search_anchor_at_beginning_line_at_end(self):
# "^" matches the end of the string if the string ends with a newline.
p = re.compile('^', re.MULTILINE)
self.assertEqual([m.span() for m in p.finditer('\xe0\n')], [(0, 0), (2, 2)])
self.assertEqual([m.span() for m in p.finditer('\u0430\n')], [(0, 0), (2, 2)])
self.assertEqual([m.span() for m in p.finditer('\U0001d49c\n')], [(0, 0), (2, 2)])

def test_possessive_quantifiers(self):
"""Test Possessive Quantifiers
Test quantifiers of the form @+ for some repetition operator @,
Expand Down
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.
23 changes: 23 additions & 0 deletions Modules/_sre/sre_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,29 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
ptr++;
RESET_CAPTURE_GROUP();
}
} else if (pattern[0] == SRE_OP_AT &&
pattern[1] == SRE_AT_BEGINNING_LINE) {
/* pattern is anchored at the start of a line (MULTILINE "^").
Only the start of the string and the character after a linebreak
can match, so jump from one line start to the next instead of
trying SRE(match) at every position. */
end = (SRE_CHAR *)state->end;
TRACE(("|%p|%p|SEARCH AT_BEGINNING_LINE\n", pattern, ptr));
state->start = state->ptr = ptr;
status = SRE(match)(state, pattern, 1);
state->must_advance = 0;
while (status == 0) {
/* skip to the next linebreak ... */
while (ptr < end && !SRE_IS_LINEBREAK(*ptr))
ptr++;
if (ptr >= end)
return 0;
Comment thread
eendebakpt marked this conversation as resolved.
ptr++; /* ... and step past it, onto a line start */
RESET_CAPTURE_GROUP();
TRACE(("|%p|%p|SEARCH AT_BEGINNING_LINE\n", pattern, ptr));
state->start = state->ptr = ptr;
status = SRE(match)(state, pattern, 0);
}
} else {
/* general case */
assert(ptr <= end);
Expand Down
Loading