File tree 3 files changed +34
-2
lines changed
3 files changed +34
-2
lines changed Original file line number Diff line number Diff line change 35
35
from builtins import *
36
36
37
37
import canmatrix
38
-
38
+ import canmatrix . utils
39
39
logger = logging .getLogger (__name__ )
40
40
41
41
@@ -755,13 +755,15 @@ def add_frame_by_id(new_frame): # type: (canmatrix.Frame) -> None
755
755
if temp :
756
756
frame_id = temp .group (1 )
757
757
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
+
759
760
if frame_id .isnumeric (): # value for Frame
760
761
try :
761
762
frame = get_frame_by_id (canmatrix .ArbitrationId .from_compound_integer (int (frame_id )))
762
763
sg = frame .signal_by_name (signal_name )
763
764
for i in range (math .floor (len (temp_list ) / 2 )):
764
765
val = temp_list [i * 2 + 1 ]
766
+ val = val .replace ('\\ "' , '"' )
765
767
if sg :
766
768
sg .add_values (temp_list [i * 2 ], val )
767
769
except :
Original file line number Diff line number Diff line change @@ -478,4 +478,14 @@ def test_missing_space():
478
478
matrix = canmatrix .formats .dbc .load (dbc , dbcImportEncoding = "utf8" )
479
479
assert matrix .frames [0 ].signals [0 ].name == "sig1"
480
480
481
+ def test_escaped_quotes ():
482
+ dbc = io .BytesIO (textwrap .dedent (r'''
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 ] == r'string with "escaped" double quotes'
490
+
481
491
Original file line number Diff line number Diff line change @@ -20,6 +20,26 @@ def quote_aware_space_split(in_line): # type: (str) -> typing.List[str]
20
20
return [item .decode ('utf-8' ) for item in shlex .split (in_line .strip ().encode ('utf-8' ))]
21
21
22
22
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
+
23
43
def quote_aware_comma_split (string ): # type: (str) -> typing.List[str]
24
44
"""
25
45
Split a string containing comma separated list of fields.
You can’t perform that action at this time.
0 commit comments