From 61461ec456895dc9cd2afc685a1dd2544380b85b Mon Sep 17 00:00:00 2001 From: Niels Date: Fri, 5 Jul 2013 11:23:49 +0200 Subject: [PATCH] - fixes from Harro --- src/JSON.cc | 37 +++++++++++++++++++++---- src/JSON.h | 2 +- test/JSON_test.cc | 70 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 96 insertions(+), 13 deletions(-) diff --git a/src/JSON.cc b/src/JSON.cc index a8a402201..8095a0366 100644 --- a/src/JSON.cc +++ b/src/JSON.cc @@ -643,6 +643,7 @@ std::string JSON::parser::parseString() { const size_t length = p - _buffer - _pos; char* tmp = new char[length + 1]; std::strncpy(tmp, _buffer + _pos, length); + tmp[length] = 0; std::string result(tmp); delete [] tmp; @@ -838,11 +839,11 @@ JSON::iterator::iterator(JSON* j) : _object(j), _vi(nullptr), _oi(nullptr) { JSON::iterator::iterator(const JSON::iterator& o) : _object(o._object), _vi(nullptr), _oi(nullptr) { switch (_object->_type) { case (array): { - _vi = new std::vector::iterator(static_cast*>(_object->_payload)->begin()); + _vi = new std::vector::iterator(*(o._vi)); break; } case (object): { - _oi = new std::map::iterator(static_cast*>(_object->_payload)->begin()); + _oi = new std::map::iterator(*(o._oi)); break; } default: @@ -857,6 +858,18 @@ JSON::iterator::~iterator() { JSON::iterator& JSON::iterator::operator=(const JSON::iterator& o) { _object = o._object; + switch (_object->_type) { + case (array): { + _vi = new std::vector::iterator(*(o._vi)); + break; + } + case (object): { + _oi = new std::map::iterator(*(o._oi)); + break; + } + default: + break; + } return *this; } @@ -985,11 +998,11 @@ JSON::const_iterator::const_iterator(const JSON* j) : _object(j), _vi(nullptr), JSON::const_iterator::const_iterator(const JSON::const_iterator& o) : _object(o._object), _vi(nullptr), _oi(nullptr) { switch (_object->_type) { case (array): { - _vi = new std::vector::const_iterator(static_cast*>(_object->_payload)->begin()); + _vi = new std::vector::const_iterator(*(o._vi)); break; } case (object): { - _oi = new std::map::const_iterator(static_cast*>(_object->_payload)->begin()); + _oi = new std::map::const_iterator(*(o._oi)); break; } default: @@ -1000,11 +1013,11 @@ JSON::const_iterator::const_iterator(const JSON::const_iterator& o) : _object(o. JSON::const_iterator::const_iterator(const JSON::iterator& o) : _object(o._object), _vi(nullptr), _oi(nullptr) { switch (_object->_type) { case (array): { - _vi = new std::vector::const_iterator(static_cast*>(_object->_payload)->begin()); + _vi = new std::vector::const_iterator(*(o._vi)); break; } case (object): { - _oi = new std::map::const_iterator(static_cast*>(_object->_payload)->begin()); + _oi = new std::map::const_iterator(*(o._oi)); break; } default: @@ -1019,6 +1032,18 @@ JSON::const_iterator::~const_iterator() { JSON::const_iterator& JSON::const_iterator::operator=(const JSON::const_iterator& o) { _object = o._object; + switch (_object->_type) { + case (array): { + _vi = new std::vector::const_iterator(*(o._vi)); + break; + } + case (object): { + _oi = new std::map::const_iterator(*(o._oi)); + break; + } + default: + break; + } return *this; } diff --git a/src/JSON.h b/src/JSON.h index 3a715f70c..082a7fcda 100644 --- a/src/JSON.h +++ b/src/JSON.h @@ -6,8 +6,8 @@ #endif // allow us to use "nullptr" everywhere -#ifndef nullptr #include +#ifndef nullptr #define nullptr NULL #endif diff --git a/test/JSON_test.cc b/test/JSON_test.cc index 3836ba12d..f631cfca2 100644 --- a/test/JSON_test.cc +++ b/test/JSON_test.cc @@ -202,20 +202,78 @@ void test_array() { #endif // iterators - for (JSON::iterator i = a.begin(); i != a.end(); ++i) { - std::cerr << *i << '\n'; + { + size_t count = 0; + for (JSON::iterator i = a.begin(); i != a.end(); ++i) { + std::cerr << *i << '\n'; + count++; + } + assert(count == a.size()); } - for (JSON::const_iterator i = a.cbegin(); i != a.cend(); ++i) { - std::cerr << *i << '\n'; + { + /* + size_t count = 0; + for (JSON::const_iterator i = a.begin(); i != a.end(); ++i) { + std::cerr << *i << '\n'; + count++; + } + assert(count == a.size()); + */ + } + + { + size_t count = 0; + for (JSON::const_iterator i = a.cbegin(); i != a.cend(); ++i) { + std::cerr << *i << '\n'; + count++; + } + assert(count == a.size()); } #ifdef __cplusplus11 - for (auto element : a) { - std::cerr << element << '\n'; + { + size_t count = 0; + for (auto element : a) { + std::cerr << element << '\n'; + count++; + } + assert(count == a.size()); } #endif + { + JSON::iterator i; + size_t count = 0; + for (i = a.begin(); i != a.end(); ++i) { + std::cerr << *i << '\n'; + count++; + } + assert(count == a.size()); + } + + { + /* + JSON::const_iterator i; + size_t count = 0; + for (i = a.begin(); i != a.end(); ++i) { + std::cerr << *i << '\n'; + count++; + } + assert(count == a.size()); + */ + } + + { + JSON::const_iterator i; + size_t count = 0; + for (i = a.cbegin(); i != a.cend(); ++i) { + std::cerr << *i << '\n'; + count++; + } + assert(count == a.size()); + } + { // get payload std::vector* array = static_cast*>(a.data());