Skip to content

Commit e9b5874

Browse files
authored
Merge pull request #731 from gerlero/parser
Improve ASCII numeric parsing performance
2 parents cf5643d + 7f16ce5 commit e9b5874

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/foamlib/_files/_parsing/_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def _expect(contents: bytes | bytearray, pos: int, expected: bytes | bytearray)
122122

123123
_COMMENTS = re.compile(rb"(?:(?:/\*(?:[^*]|\*(?!/))*\*/)|(?://(?:\\\n|[^\n])*))+")
124124
_SKIP = re.compile(rb"(?:\s+|" + _COMMENTS.pattern + rb")+")
125-
_ITEM = re.compile(rb"[\w\.\-\+]+")
125+
_ITEM = re.compile(rb"[0-9.\-+einfnatyEINFNATY]+", re.ASCII)
126126

127127

128128
class _ASCIINumericListParser(Generic[_NT, _ElShape]):

tests/test_files/test_parsing/test_basic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ def test_parse_value() -> None:
4646
assert isinstance(field, np.ndarray)
4747
assert field.dtype == float
4848
assert np.array_equal(field, [1.0, 1.0])
49+
field = ParsedFile(b"nonuniform List<vector> 2((nan InFiNiTy -NaN)(1e3 .1 0.))")[()]
50+
assert isinstance(field, np.ndarray)
51+
assert field.dtype == float
52+
assert field.shape == (2, 3)
53+
assert np.array_equal(field[0], [np.nan, np.inf, -np.nan], equal_nan=True)
54+
assert np.array_equal(field[1], [1e3, 0.1, 0.0])
4955
field = ParsedFile(b"nonuniform List<symmTensor> 0()")[()]
5056
assert isinstance(field, np.ndarray)
5157
assert field.dtype == float

0 commit comments

Comments
 (0)