Skip to content

Commit 50c9f07

Browse files
committed
compile regex lazily
1 parent 9616588 commit 50c9f07

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

markdown_it/rules_inline/text.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import re
23

34
# Skip text characters for text token, place those to pending buffer
@@ -36,14 +37,18 @@
3637
"}",
3738
"~",
3839
}
39-
_RE_TERMINATOR_CHAR = re.compile("[" + re.escape("".join(_TerminatorChars)) + "]")
40+
41+
42+
@functools.cache
43+
def _terminator_char_regex() -> re.Pattern[str]:
44+
return re.compile("[" + re.escape("".join(_TerminatorChars)) + "]")
4045

4146

4247
def text(state: StateInline, silent: bool) -> bool:
4348
pos = state.pos
4449
posMax = state.posMax
4550

46-
terminator_char = _RE_TERMINATOR_CHAR.search(state.src, pos)
51+
terminator_char = _terminator_char_regex().search(state.src, pos)
4752
pos = terminator_char.start() if terminator_char else posMax
4853

4954
if pos == state.pos:

0 commit comments

Comments
 (0)