Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
fa0ee9c9
Commit
fa0ee9c9
authored
Jan 08, 2015
by
Raphael Isemann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed escaped quotes
parent
46474010
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
25 deletions
+38
-25
src/json.cc
src/json.cc
+37
-25
test/json_unit.cc
test/json_unit.cc
+1
-0
No files found.
src/json.cc
View file @
fa0ee9c9
...
@@ -2042,34 +2042,46 @@ Parses a string after opening quotes (\p ") where read.
...
@@ -2042,34 +2042,46 @@ Parses a string after opening quotes (\p ") where read.
*/
*/
std
::
string
json
::
parser
::
parseString
()
std
::
string
json
::
parser
::
parseString
()
{
{
// get position of closing quotes
// remember the position where the first character of the string was
auto
quotepos_
=
buffer_
.
find_first_of
(
"
\"
"
,
pos_
);
const
auto
startPos
=
pos_
;
// true if and only if the amount of backslashes before the current
// character is even
bool
evenAmountOfBackslashes
=
true
;
// iterate with pos_ over the whole string
for
(;
pos_
<
buffer_
.
size
();
pos_
++
)
{
char
currentChar
=
buffer_
[
pos_
];
// currentChar is a quote, so we might have found the end of the string
if
(
currentChar
==
'"'
)
{
// but only if the amount of backslashes before that quote is even
if
(
evenAmountOfBackslashes
)
{
const
auto
stringLength
=
pos_
-
startPos
;
// set pos_ behind the trailing quote
pos_
++
;
// find next char to parse
next
();
// if the closing quotes are escaped (character before the quotes is a
// return string inside the quotes
// backslash), we continue looking for the final quotes
return
buffer_
.
substr
(
startPos
,
stringLength
);
while
(
quotepos_
!=
std
::
string
::
npos
and
buffer_
[
quotepos_
-
1
]
==
'\\'
)
{
quotepos_
=
buffer_
.
find_first_of
(
"
\"
"
,
quotepos_
+
1
);
}
}
// check if closing quotes were found
if
(
quotepos_
==
std
::
string
::
npos
)
{
error
(
"expected '
\"
'"
);
}
}
// store the coordinates of the string for the later return value
// remember if we have a even amount of backslashes before the current character
const
auto
stringBegin
=
pos_
;
if
(
currentChar
==
'\\'
)
{
const
auto
stringLength
=
quotepos_
-
pos_
;
// jump between even/uneven for each backslash we encounter
evenAmountOfBackslashes
=
!
evenAmountOfBackslashes
;
// set buffer position to the position behind (+1) the closing quote
}
else
{
pos_
=
quotepos_
+
1
;
// zero backslashes are also a even number, so as soon as we encounter a non-backslash
// the chain of backslashes breaks and we start again from zero
// read next character
evenAmountOfBackslashes
=
true
;
next
();
}
}
// return the string value
// we iterated over the whole string without finding a unescaped quote
return
buffer_
.
substr
(
stringBegin
,
stringLength
);
// so the given string is malformed
error
(
"expected '
\"
'"
);
}
}
/*!
/*!
...
...
test/json_unit.cc
View file @
fa0ee9c9
...
@@ -1621,6 +1621,7 @@ TEST_CASE("Parser")
...
@@ -1621,6 +1621,7 @@ TEST_CASE("Parser")
// escape characters
// escape characters
CHECK_THROWS_AS
(
json
::
parse
(
"
\"\\\"
"
),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parse
(
"
\"\\\"
"
),
std
::
invalid_argument
);
CHECK_NOTHROW
(
json
::
parse
(
"
\"\\\"\"
"
));
CHECK_NOTHROW
(
json
::
parse
(
"
\"\\\"\"
"
));
CHECK_NOTHROW
(
json
::
parse
(
"
\"\\\\\"
"
));
// quotes must be closed
// quotes must be closed
CHECK_THROWS_AS
(
json
::
parse
(
"
\"
"
),
std
::
invalid_argument
);
CHECK_THROWS_AS
(
json
::
parse
(
"
\"
"
),
std
::
invalid_argument
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment