Skip to content

Commit

Permalink
📝 cleanup after the last PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Oct 22, 2017
1 parent be4fba7 commit b0c380b
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 91 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,12 @@ I deeply appreciate the help of the following people.
- [WebProdPP](https://github.com/WebProdPP) fixed a subtle error in a precondition check.
- [Alex](https://github.com/leha-bot) noted an error in a code sample.
- [Tom de Geus](https://github.com/tdegeus) reported some warnings with ICC and helped fixing them.

- [Perry Kundert](https://github.com/pjkundert) simplified reading from input streams.
- [Sonu Lohani](https://github.com/sonulohani) fixed a small compilation error.
- [Jamie Seward](https://github.com/jseward) fixed all MSVC warnings.
- [Nate Vargas](https://github.com/eld00d) added a Doxygen tag file.
- [pvleuven](https://github.com/pvleuven) helped fixing a warning in ICC.
- [Pavel](https://github.com/crea7or) helped fixing some warnings in MSVC.

Thanks a lot for helping out! Please [let me know](mailto:mail@nlohmann.me) if I forgot someone.

Expand Down
61 changes: 32 additions & 29 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1404,63 +1404,66 @@ consist of all valid char values as positive values (typically unsigned char),
plus an EOF value outside that range, specified by the value of the function
std::char_traits<char>::eof(). This value is typically -1, but could be any
arbitrary value which is not a valid char value.
@return Typically [0,255] plus std::char_traits<char>::eof().
*/
struct input_adapter_protocol
{
/// get a character [0,255] or std::char_traits<char>::eof().
virtual std::char_traits<char>::int_type get_character() = 0;
virtual void unget_character() = 0; // restore the last non-eof() character to input
/// restore the last non-eof() character to input
virtual void unget_character() = 0;
virtual ~input_adapter_protocol() = default;
};

/// a type to simplify interfaces
using input_adapter_t = std::shared_ptr<input_adapter_protocol>;

/// input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
/// beginning of input. Does not support changing the underlying std::streambuf
/// in mid-input. Maintains underlying std::istream and std::streambuf to
/// support subsequent use of standard std::istream operations to process any
/// input characters following those used in parsing the JSON input. Clears the
/// std::istream flags; any input errors (eg. EOF) will be detected by the first
/// subsequent call for input from the std::istream.
/*!
Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
beginning of input. Does not support changing the underlying std::streambuf
in mid-input. Maintains underlying std::istream and std::streambuf to support
subsequent use of standard std::istream operations to process any input
characters following those used in parsing the JSON input. Clears the
std::istream flags; any input errors (e.g., EOF) will be detected by the first
subsequent call for input from the std::istream.
*/
class input_stream_adapter : public input_adapter_protocol
{
public:
~input_stream_adapter() override
{
// clear stream flags; we use underlying streambuf I/O, do not maintain ifstream flags
// clear stream flags; we use underlying streambuf I/O, do not
// maintain ifstream flags
is.clear();
}

explicit input_stream_adapter(std::istream& i)
: is(i)
, sb(*i.rdbuf())
: is(i), sb(*i.rdbuf())
{
// Ignore Byte Order Mark at start of input
// ignore Byte Order Mark at start of input
std::char_traits<char>::int_type c;
if (( c = get_character() ) == 0xEF )
if ((c = get_character()) == 0xEF)
{
if (( c = get_character() ) == 0xBB )
if ((c = get_character()) == 0xBB)
{
if (( c = get_character() ) == 0xBF )
if ((c = get_character()) == 0xBF)
{
return; // Ignore BOM
}
else if ( c != std::char_traits<char>::eof() )
else if (c != std::char_traits<char>::eof())
{
is.unget();
}
is.putback( '\xBB' );
is.putback('\xBB');
}
else if ( c != std::char_traits<char>::eof() )
else if (c != std::char_traits<char>::eof())
{
is.unget();
}
is.putback( '\xEF' );
is.putback('\xEF');
}
else if ( c != std::char_traits<char>::eof() )
else if (c != std::char_traits<char>::eof())
{
is.unget(); // Not BOM. Process as usual.
is.unget(); // Not BOM. Process as usual.
}
}

Expand All @@ -1478,13 +1481,13 @@ class input_stream_adapter : public input_adapter_protocol

void unget_character() override
{
sb.sungetc(); // Avoided for performance: is.unget();
sb.sungetc(); // is.unget() avoided for performance
}
private:

private:
/// the associated input stream
std::istream& is;
std::streambuf &sb;
std::streambuf& sb;
};

/// input adapter for buffer input
Expand Down Expand Up @@ -2691,7 +2694,7 @@ class lexer
{
++chars_read;
current = ia->get_character();
if (JSON_LIKELY( current != std::char_traits<char>::eof()))
if (JSON_LIKELY(current != std::char_traits<char>::eof()))
{
token_string.push_back(std::char_traits<char>::to_char_type(current));
}
Expand All @@ -2709,7 +2712,7 @@ class lexer
token_string.pop_back();
}
}

/// add a character to yytext
void add(int c)
{
Expand Down Expand Up @@ -2742,7 +2745,7 @@ class lexer
/// return current string value (implicitly resets the token; useful only once)
std::string move_string()
{
return std::move( yytext );
return std::move(yytext);
}

/////////////////////
Expand Down
2 changes: 1 addition & 1 deletion test/src/unit-modifiers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ TEST_CASE("modifiers")
// invalid values (no string/val pair)
CHECK_THROWS_AS(j.push_back({1}), json::type_error&);
CHECK_THROWS_WITH(j.push_back({1}), "[json.exception.type_error.308] cannot use push_back() with object");
CHECK_THROWS_AS(j.push_back({1,2}), json::type_error&);
CHECK_THROWS_AS(j.push_back({1, 2}), json::type_error&);
CHECK_THROWS_WITH(j.push_back({1, 2}), "[json.exception.type_error.308] cannot use push_back() with object");
CHECK_THROWS_AS(j.push_back({1, 2, 3, 4}), json::type_error&);
CHECK_THROWS_WITH(j.push_back({1, 2, 3, 4}), "[json.exception.type_error.308] cannot use push_back() with object");
Expand Down
6 changes: 3 additions & 3 deletions test/src/unit-readme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ using nlohmann::json;
#include <iostream>

#if defined(_MSC_VER)
#pragma warning (push)
#pragma warning (disable : 4189) // local variable is initialized but not referenced
#pragma warning (push)
#pragma warning (disable : 4189) // local variable is initialized but not referenced
#endif

TEST_CASE("README", "[hide]")
Expand Down Expand Up @@ -305,5 +305,5 @@ TEST_CASE("README", "[hide]")
}

#if defined(_MSC_VER)
#pragma warning (pop)
#pragma warning (pop)
#endif
Loading

0 comments on commit b0c380b

Please sign in to comment.