X Tutup
Skip to content

Commit 1e8c893

Browse files
committed
Compile regex
Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
1 parent a902f10 commit 1e8c893

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

bpython/curtsiesfrontend/parse.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
from bpython.line import LazyReCompile
23
from bpython.formatter import BPythonFormatter
34
from bpython._py3compat import PythonLexer
45
from bpython.config import Struct, loadini, default_config_path
@@ -62,17 +63,21 @@ def fs_from_match(d):
6263
atts['bold'] = True
6364
return fmtstr(d['string'], **atts)
6465

66+
67+
peel_off_string_re = LazyReCompile(
68+
r"""(?P<colormarker>\x01
69+
(?P<fg>[krgybmcwdKRGYBMCWD]?)
70+
(?P<bg>[krgybmcwdKRGYBMCWDI]?)?)
71+
(?P<bold>\x02?)
72+
\x03
73+
(?P<string>[^\x04]*)
74+
\x04
75+
(?P<rest>.*)
76+
""", re.VERBOSE | re.DOTALL)
77+
78+
6579
def peel_off_string(s):
66-
p = r"""(?P<colormarker>\x01
67-
(?P<fg>[krgybmcwdKRGYBMCWD]?)
68-
(?P<bg>[krgybmcwdKRGYBMCWDI]?)?)
69-
(?P<bold>\x02?)
70-
\x03
71-
(?P<string>[^\x04]*)
72-
\x04
73-
(?P<rest>.*)
74-
"""
75-
m = re.match(p, s, re.VERBOSE | re.DOTALL)
80+
m = peel_off_string_re.match(s)
7681
assert m, repr(s)
7782
d = m.groupdict()
7883
rest = d['rest']

bpython/line.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010

1111
class LazyReCompile(object):
1212

13-
def __init__(self, regex):
13+
def __init__(self, regex, flags=0):
1414
self.regex = regex
15+
self.flags = flags
1516
self.compiled = None
1617

1718
def compile_regex(method):
1819
def _impl(self, *args, **kwargs):
1920
if self.compiled is None:
20-
self.compiled = re.compile(self.regex)
21+
self.compiled = re.compile(self.regex, self.flags)
2122
return method(self, *args, **kwargs)
2223
return _impl
2324

0 commit comments

Comments
 (0)
X Tutup