Skip to content

Commit

Permalink
Fixed parsing of comments (issue #421)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Jan 17, 2017
1 parent 2e7d498 commit 17a17c8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
ArduinoJson: change log
=======================

HEAD
----

* Fixed parsing of comments (issue #421)

v5.8.1
------

Expand Down
36 changes: 12 additions & 24 deletions include/ArduinoJson/Deserialization/Comments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,27 @@ void skipSpacesAndComments(TInput& input) {
// C-style block comment
case '*':
input.move(); // skip '/'
input.move(); // skip '*'
// no need to skip '*'
for (;;) {
switch (input.current()) {
case '\0':
return;
case '*':
input.move(); // skip '*'
if (input.current() == '/') {
input.move(); // skip '/'
return;
}
break;
default:
input.move();
input.move();
if (input.current() == '\0') return;
if (input.current() == '*' && input.next() == '/') {
input.move(); // skip '*'
input.move(); // skip '/'
break;
}
}
break;

// C++-style line comment
case '/':
input.move(); // skip '/'
// not need to skip "//"
for (;;) {
switch (input.current()) {
case '\0':
return;
case '\n':
input.move();
return;
default:
input.move();
}
input.move();
if (input.current() == '\0') return;
if (input.current() == '\n') break;
}
return;
break;

// not a comment, just a '/'
default:
Expand Down
10 changes: 5 additions & 5 deletions test/JsonParser_Array_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,15 @@ TEST_F(JsonParser_Array_Tests, StringWithUnterminatedEscapeSequence) {
}

TEST_F(JsonParser_Array_Tests, CCommentBeforeOpeningBracket) {
whenInputIs("/*COMMENT*/[\"hello\"]");
whenInputIs("/*COMMENT*/ [\"hello\"]");

parseMustSucceed();
sizeMustBe(1);
firstElementMustBe("hello");
}

TEST_F(JsonParser_Array_Tests, CCommentAfterOpeningBracket) {
whenInputIs("[/*COMMENT*/\"hello\"]");
whenInputIs("[/*COMMENT*/ \"hello\"]");

parseMustSucceed();
sizeMustBe(1);
Expand Down Expand Up @@ -278,7 +278,7 @@ TEST_F(JsonParser_Array_Tests, CCommentBeforeComma) {
}

TEST_F(JsonParser_Array_Tests, CCommentAfterComma) {
whenInputIs("[\"hello\",/*COMMENT*/\"world\"]");
whenInputIs("[\"hello\",/*COMMENT*/ \"world\"]");

parseMustSucceed();
sizeMustBe(2);
Expand All @@ -287,7 +287,7 @@ TEST_F(JsonParser_Array_Tests, CCommentAfterComma) {
}

TEST_F(JsonParser_Array_Tests, CppCommentBeforeOpeningBracket) {
whenInputIs("//COMMENT\n[\"hello\"]");
whenInputIs("//COMMENT\n\t[\"hello\"]");

parseMustSucceed();
sizeMustBe(1);
Expand All @@ -303,7 +303,7 @@ TEST_F(JsonParser_Array_Tests, CppCommentAfterOpeningBracket) {
}

TEST_F(JsonParser_Array_Tests, CppCommentBeforeClosingBracket) {
whenInputIs("[\"hello\"//COMMENT\n]");
whenInputIs("[\"hello\"//COMMENT\r\n]");

parseMustSucceed();
sizeMustBe(1);
Expand Down

0 comments on commit 17a17c8

Please sign in to comment.