Skip to content

Commit a814e7d

Browse files
committed
Allow escaped double quotes in DBC strings
1 parent 33fb7e5 commit a814e7d

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

src/parser/dbc/DbcTokens.cpp

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,48 @@ bool DbcIdentifierToken::acceptsChar(QChar ch)
8484

8585

8686
DbcStringToken::DbcStringToken(int line, int column)
87-
: DbcToken(line, column, dbc_tok_string)
87+
: DbcToken(line, column, dbc_tok_string),
88+
_done(false),
89+
_escape(false)
8890
{
8991
}
9092

93+
// Store string including quotes, as:
94+
// "some string"
95+
// which may include '\'-escaped double quotes, leading to something like
96+
// "some string is \"this\""
97+
// and '\' can be escaped as well
98+
// "this is a valid \\ acceptable string"
9199
bool DbcStringToken::acceptsChar(QChar ch)
92100
{
93-
if (_data.isEmpty()) {
94-
return (ch=='"');
95-
} else if (_data.length()<2) {
96-
return true;
97-
} else {
98-
return !_data.endsWith('"');
99-
}
101+
if (_done) {
102+
return false;
103+
}
104+
105+
switch (_data.length()) {
106+
// Start of string must be '"'
107+
case 0:
108+
return (ch == '"');
109+
// Can't look to index -1, so accept anything
110+
case 1:
111+
_escape = (ch == '\\');
112+
_done = (ch == '"');
113+
return true;
114+
// Accept anything until an unescaped '"'
115+
default:
116+
if (_escape) {
117+
_escape = false;
118+
} else {
119+
if (ch == '\\') {
120+
_escape = true;
121+
}
122+
if (ch == '"') {
123+
_data.replace(QRegExp("\\\\(.)"), "\\1");
124+
_done = true;
125+
}
126+
}
127+
return true;
128+
}
100129
}
101130

102131

src/parser/dbc/DbcTokens.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class DbcStringToken : public DbcToken {
8383
public:
8484
DbcStringToken(int line, int column);
8585
virtual bool acceptsChar(QChar ch);
86+
private:
87+
bool _done;
88+
bool _escape;
8689
};
8790

8891
class DbcRegExpToken : public DbcToken {

0 commit comments

Comments
 (0)