Skip to content

Commit

Permalink
Fixed bug in 'JsonParser::skipWhitespace'
Browse files Browse the repository at this point in the history
Method read last char even if it was not a whitespace character. Was only noticeable when reading a stream containing UTF-8 sequences
  • Loading branch information
MichaelBrunn3r committed Apr 14, 2019
1 parent 7c05d5e commit 8f1b6bd
Show file tree
Hide file tree
Showing 8 changed files with 601 additions and 568 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ add_custom_target(updateMockArduino

COMMAND mv MockArduino/native MockArduino/MockArduinoNative
COMMAND mv MockArduino/MockArduinoNative ${CMAKE_SOURCE_DIR}/lib
)

add_custom_target(createRelease
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}

COMMAND rm -f ArduinoJStream.zip
COMMAND cp -R src ArduinoJStream
COMMAND cp LICENSE ArduinoJStream
COMMAND zip -r ArduinoJStream.zip ArduinoJStream
COMMAND rm -r ArduinoJStream
)
4 changes: 0 additions & 4 deletions src/Internals/JsonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ namespace JStream {
return c == '\t' || c == '\n' || c == '\r' || c == ' ';
}

inline bool isNotWhitespace(const char c) {
return c > 32;
}

/** @brief Returns true if a character is a valid json decimal digit **/
inline bool isDecDigit(const char c) {
return c >= 48 && c <= 57;
Expand Down
3 changes: 2 additions & 1 deletion src/JsonParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Stream.h>
#include <Path.h>
#include <limits>
#include <WString.h>

namespace JStream {
class JsonParser {
Expand Down Expand Up @@ -209,7 +210,7 @@ namespace JStream {
*/
bool parseNumArray(std::vector<double>& vec, bool inArray=false);
private:
Stream* mStream;
Stream* mStream = nullptr;

/**
* @brief Reads the stream until the start of the n-th succeeding key/value in the current object/array
Expand Down
2 changes: 1 addition & 1 deletion src/JsonParserNav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ namespace JStream {
int c;
do {
c = mStream->peek();
if(Internals::isNotWhitespace(c)) break;
if(!Internals::isWhitespace(c)) break;
mStream->read();
} while(c > 0);

Expand Down
13 changes: 9 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ target_include_directories(ArduinoJStream PUBLIC ${CMAKE_SOURCE_DIR}/test/MockAr
# Executables #
###############

add_executable(TestJsonParser TestJsonParser.cpp)
target_link_libraries(TestJsonParser MockArduino Catch2 ArduinoJStream)
add_executable(TestParserImpl TestParserImpl.cpp)
target_link_libraries(TestParserImpl MockArduino Catch2 ArduinoJStream)

add_executable(TestParserNav TestParserNav.cpp)
target_link_libraries(TestParserNav MockArduino Catch2 ArduinoJStream)

add_executable(TestJsonUtils TestJsonUtils.cpp)
target_link_libraries(TestJsonUtils MockArduino Catch2 ArduinoJStream)
Expand All @@ -30,5 +33,7 @@ target_link_libraries(TestJsonUtils MockArduino Catch2 ArduinoJStream)
# Targets #
###########

add_custom_target(testJsonParser ./TestJsonParser DEPENDS TestJsonParser)
add_custom_target(testJsonUtils ./TestJsonUtils DEPENDS TestJsonUtils)
add_custom_target(runTestParserImpl COMMAND ./TestParserImpl DEPENDS TestParserImpl)
add_custom_target(runTestParserNav COMMAND ./TestParserNav DEPENDS TestParserNav)
add_custom_target(runTestJsonUtils COMMAND ./TestJsonUtils DEPENDS TestJsonUtils)
add_custom_target(runTestAll COMMAND make runTestParserImpl && make runTestParserNav && make runTestJsonUtils)
14 changes: 7 additions & 7 deletions test/TestJsonUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

using namespace JStream;

SCENARIO("stol") {
JsonParser parser;
TEST_CASE("::stol") {
JsonParser parser();

GIVEN("valid longs") {
SECTION("valid longs") {
std::vector<std::tuple<const char*, long, long>> parse = {
{"0", -1, 0},
{"123", -1, 123},
Expand All @@ -49,7 +49,7 @@ SCENARIO("stol") {
}
}

GIVEN("invalid longs") {
SECTION("invalid longs") {
std::vector<std::tuple<const char*, long>> parse = {
{"", -1},

Expand All @@ -74,10 +74,10 @@ SCENARIO("stol") {
}
}

SCENARIO("stod") {
JsonParser parser;
TEST_CASE("::stod") {
JsonParser parser();

GIVEN("valid longs") {
SECTION("valid longs") {
std::vector<std::tuple<const char*, double, double>> parse = {
// Ints
{"1", -1, 1},
Expand Down
Loading

0 comments on commit 8f1b6bd

Please sign in to comment.