Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional comment stripping. #1024

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions include/nlohmann/detail/input/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1185,12 +1185,54 @@ class lexer

token_type scan()
{
#ifndef JSON_STRIP_COMMENTS
// read next character and ignore whitespace
do
{
get();
}
while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
#else
// skip white space and comments if comment stripping enabled
while (true)
{
// first skip any whitespace
do
{
get();
}
while (current == ' ' || current == '\t' || current == '\n' || current == '\r');

// next, skip any comment
if (current == '/')
{
get();
// really a comment?
if (current == '/')
{
// skip to the end of line/file
do
{
get();
}
while (! (current == '\n' ||
current == '\r' ||
current == std::char_traits<char>::eof()));
}
else
{
// not a comment, leave it up to the rest of the code to deal with the '/'
unget();
break;
}
}
else
{
// not a comment at all, break out of the loop
break;
}
}
#endif

switch (current)
{
Expand Down
42 changes: 42 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3024,12 +3024,54 @@ class lexer

token_type scan()
{
#ifndef JSON_STRIP_COMMENTS
// read next character and ignore whitespace
do
{
get();
}
while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
#else
// skip white space and comments if comment stripping enabled
while (true)
{
// first skip any whitespace
do
{
get();
}
while (current == ' ' || current == '\t' || current == '\n' || current == '\r');

// next, skip any comment
if (current == '/')
{
get();
// really a comment?
if (current == '/')
{
// skip to the end of line/file
do
{
get();
}
while (! (current == '\n' ||
current == '\r' ||
current == std::char_traits<char>::eof()));
}
else
{
// not a comment, leave it up to the rest of the code to deal with the '/'
unget();
break;
}
}
else
{
// not a comment at all, break out of the loop
break;
}
}
#endif

switch (current)
{
Expand Down