Skip to content

Commit

Permalink
Iss526 (#536)
Browse files Browse the repository at this point in the history
* fix ci

* fix ci

* fixes #526

* fix for #526escaped quotes

* #526 use raw strings for test
  • Loading branch information
ebroecker authored Dec 20, 2020
1 parent 5b2b43b commit 568ed7d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/canmatrix/formats/dbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from builtins import *

import canmatrix

import canmatrix.utils
logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -755,13 +755,15 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
if temp:
frame_id = temp.group(1)
signal_name = temp.group(2)
temp_list = temp.group(3).split('"')
temp_list = list(canmatrix.utils.escape_aware_split(temp.group(3), '"'))

if frame_id.isnumeric(): # value for Frame
try:
frame = get_frame_by_id(canmatrix.ArbitrationId.from_compound_integer(int(frame_id)))
sg = frame.signal_by_name(signal_name)
for i in range(math.floor(len(temp_list) / 2)):
val = temp_list[i * 2 + 1]
val = val.replace('\\"', '"')
if sg:
sg.add_values(temp_list[i * 2], val)
except:
Expand Down
10 changes: 10 additions & 0 deletions src/canmatrix/tests/test_dbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,14 @@ def test_missing_space():
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
assert matrix.frames[0].signals[0].name == "sig1"

def test_escaped_quotes():
dbc = io.BytesIO(textwrap.dedent(r'''
BO_ 17 Frame_1: 8 Vector__XXX
SG_ Signal : 0|8@1-(1,0)[0|0] "" Vector__XXX
VAL_ 17 Signal 0 "zero" 1 "one " 2 "string with \"escaped\" double quotes";
''').encode('utf-8'))
matrix = canmatrix.formats.dbc.load(dbc, dbcImportEncoding="utf8")
assert matrix.frames[0].signals[0].values[2] == r'string with "escaped" double quotes'


20 changes: 20 additions & 0 deletions src/canmatrix/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ def quote_aware_space_split(in_line): # type: (str) -> typing.List[str]
return [item.decode('utf-8') for item in shlex.split(in_line.strip().encode('utf-8'))]


# https://stackoverflow.com/questions/18092354/python-split-string-without-splitting-escaped-character
def escape_aware_split(string, delimiter):
if len(delimiter) != 1:
raise ValueError('Invalid delimiter: ' + delimiter)
ln = len(string)
i = 0
j = 0
while j < ln:
if string[j] == '\\':
if j + 1 >= ln:
yield string[i:j]
return
j += 1
elif string[j] == delimiter:
yield string[i:j]
i = j + 1
j += 1
yield string[i:j]


def quote_aware_comma_split(string): # type: (str) -> typing.List[str]
"""
Split a string containing comma separated list of fields.
Expand Down

0 comments on commit 568ed7d

Please sign in to comment.