Skip to content

Commit

Permalink
Ignore comment markers within quotes
Browse files Browse the repository at this point in the history
Fixes #121
  • Loading branch information
coldfix committed Feb 21, 2023
1 parent c8156cb commit 002f176
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/cpymad/libmadx.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1229,24 +1229,31 @@ cdef clib.variable* _get_var(name) except NULL:


cdef void _strip_comments(char* text) nogil:
cdef int in_quotes = 0
cdef char* dest = text
cdef char c, d
while True:
c = text[0]
if c == 0:
break
d = text[1]
if c == b'!' or (c == b'/' and d == b'/'):
text = strchr(text+1, b'\n')
if text == NULL:
break
continue
if c == b'/' and d == b'*':
text = strstr(text+2, b"*/")
if text == NULL:
break
text += 2
continue
if in_quotes:
in_quotes = c != d
elif c == b'\'' or c == b'"':
in_quotes = 1
d = c
else:
d = text[1]
if c == b'!' or (c == b'/' and d == b'/'):
text = strchr(text+1, b'\n')
if text == NULL:
break
continue
if c == b'/' and d == b'*':
text = strstr(text+2, b"*/")
if text == NULL:
break
text += 2
continue
dest[0] = text[0]
text += 1
dest += 1
Expand Down
13 changes: 13 additions & 0 deletions test/test_madx.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,19 @@ def test_comments(mad):
assert var.x == 6


def test_quoted_comment_markers(mad):
"""Comment markers within quotes should be ignored."""
var = mad.globals
mad("print, text='//'; x = 7;")
assert var.x == 7
mad('print, text="//"; x = 8;')
assert var.x == 8
mad('print, text="!"; x = 9;')
assert var.x == 9
mad('print, text="/*"; x = 10; print, text="*/";')
assert var.x == 10


def test_multiline_input(mad):
var = mad.globals
mad('''
Expand Down

0 comments on commit 002f176

Please sign in to comment.