Skip to content

Commit f2d2966

Browse files
authored
Merge pull request #2 from ACRIOS-Systems/feature/support-comments-parsing
Add support for special characters in comment blocks
2 parents 3683d1a + f38bdda commit f2d2966

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

dissect/cstruct/cstruct.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def _read(self, stream):
8181
8282
return Instance(self, r, sizes)
8383
84-
def add_fields(self, name, type_, offset=None, commentAttributes={}):
84+
def add_fields(self, name, type_, offset=None, commentAttributes={{}}):
8585
raise NotImplementedError("Can't add fields to a compiled structure")
8686
8787
def __repr__(self):
@@ -417,7 +417,7 @@ def _parse_fields(self, s):
417417
#parse the comment header
418418
if d['commentBlock'] is not None and d['commentBlock'].startswith('/*'):
419419
commentfields = re.finditer(
420-
r'@(?P<commentType>[a-zA-Z0-9_\-\/,.]+):[ \t]*(?P<commentVal>[a-zA-Z0-9_\-,.]+)',
420+
r'@(?P<commentType>[^@,;:\\]+):[ \t]*(?P<commentVal>[^@,;:\s\\]+)',
421421
d['commentBlock'],
422422
)
423423
for cf in commentfields:

tests/test_basic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -779,13 +779,13 @@ def test_commentfieldparse(capsys):
779779
struct test{
780780
/*
781781
* @scale: 0.001
782-
* @unit: testUnit1
782+
* @unit: µtestUnit1
783783
*/
784784
int testVar1;
785785
int testVar2;
786786
/* dicardedCom2
787787
* @scale: 5
788-
* @unit: testUnit2
788+
* @unit: %testUnit2
789789
*/
790790
int testVar3;
791791
};
@@ -798,9 +798,9 @@ def test_commentfieldparse(capsys):
798798
assert 'testVar2' in c.test.lookup
799799

800800
assert c.test.lookup['testVar1'].commentAttributes['scale'] == '0.001'
801-
assert c.test.lookup['testVar1'].commentAttributes['unit'] == 'testUnit1'
801+
assert c.test.lookup['testVar1'].commentAttributes['unit'] == 'µtestUnit1'
802802

803803
assert c.test.lookup['testVar2'].commentAttributes == {}
804804

805805
assert c.test.lookup['testVar3'].commentAttributes['scale'] == '5'
806-
assert c.test.lookup['testVar3'].commentAttributes['unit'] == 'testUnit2'
806+
assert c.test.lookup['testVar3'].commentAttributes['unit'] == '%testUnit2'

0 commit comments

Comments
 (0)