Skip to content

Commit 8a9fd5b

Browse files
committed
Speed up repeat expansion
1 parent 334888b commit 8a9fd5b

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

src/openmc_mcnp_adapter/parse.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
_COMPLEMENT_RE = re.compile(r'(#)[ ]*(\d+)')
4747
_NUM_RE = re.compile(r'(\d)([+-])(\d)')
4848

49+
_HAS_REPEAT_RE = re.compile(r'\b\d+[rR]\b')
50+
4951
_REPEAT_RE = re.compile(r"""
5052
(?P<value> # The numeric value to be repeated
5153
[+-]? # Optional sign
@@ -364,10 +366,11 @@ def sanitize(section: str) -> str:
364366
section = re.sub('\n {5}', ' ', section)
365367

366368
# Expand repeated numbers
367-
section = _REPEAT_RE.sub(
368-
lambda m: ' '.join([m.group('value')] * (int(m.group('count')) + 1)),
369-
section,
370-
)
369+
if _HAS_REPEAT_RE.search(section):
370+
section = _REPEAT_RE.sub(
371+
lambda m: ' '.join([m.group('value')] * (int(m.group('count')) + 1)),
372+
section,
373+
)
371374

372375
return section
373376

0 commit comments

Comments
 (0)