Skip to content

Commit 0825070

Browse files
committed
fixes #526
1 parent 2e3de94 commit 0825070

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/canmatrix/formats/dbc.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from builtins import *
3636

3737
import canmatrix
38-
38+
import canmatrix.utils
3939
logger = logging.getLogger(__name__)
4040

4141

@@ -755,7 +755,8 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
755755
if temp:
756756
frame_id = temp.group(1)
757757
signal_name = temp.group(2)
758-
temp_list = temp.group(3).split('"')
758+
temp_list = list(canmatrix.utils.escape_aware_split(temp.group(3), '"'))
759+
759760
if frame_id.isnumeric(): # value for Frame
760761
try:
761762
frame = get_frame_by_id(canmatrix.ArbitrationId.from_compound_integer(int(frame_id)))

src/canmatrix/tests/test_dbc.py

+10
Original file line numberDiff line numberDiff line change
@@ -478,4 +478,14 @@ def test_missing_space():
478478
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
479479
assert matrix.frames[0].signals[0].name == "sig1"
480480

481+
def test_escaped_quotes():
482+
dbc = io.BytesIO(textwrap.dedent(u'''\
483+
BO_ 17 Frame_1: 8 Vector__XXX
484+
SG_ Signal : 0|8@1-(1,0)[0|0] "" Vector__XXX
485+
486+
VAL_ 17 Signal 0 "zero" 1 "one " 2 "string with \\"escaped\\" double quotes";
487+
''').encode('utf-8'))
488+
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
489+
assert matrix.frames[0].signals[0].values[2] == 'string with \\"escaped\\" double quotes'
490+
481491

src/canmatrix/utils.py

+20
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ def quote_aware_space_split(in_line): # type: (str) -> typing.List[str]
2020
return [item.decode('utf-8') for item in shlex.split(in_line.strip().encode('utf-8'))]
2121

2222

23+
# https://stackoverflow.com/questions/18092354/python-split-string-without-splitting-escaped-character
24+
def escape_aware_split(string, delimiter):
25+
if len(delimiter) != 1:
26+
raise ValueError('Invalid delimiter: ' + delimiter)
27+
ln = len(string)
28+
i = 0
29+
j = 0
30+
while j < ln:
31+
if string[j] == '\\':
32+
if j + 1 >= ln:
33+
yield string[i:j]
34+
return
35+
j += 1
36+
elif string[j] == delimiter:
37+
yield string[i:j]
38+
i = j + 1
39+
j += 1
40+
yield string[i:j]
41+
42+
2343
def quote_aware_comma_split(string): # type: (str) -> typing.List[str]
2444
"""
2545
Split a string containing comma separated list of fields.

0 commit comments

Comments
 (0)