diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..20878fd --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +BasedOnStyle: "LLVM" +ColumnLimit: 132 +AllowShortFunctionsOnASingleLine: None +SortIncludes: false diff --git a/Makefile b/Makefile index 908e2a0..a03f4a0 100644 --- a/Makefile +++ b/Makefile @@ -23,4 +23,7 @@ install: uninstall: rm -f $(DESTDIR)$(includedir)/picojson.h -.PHONY: test check clean install uninstall +clang-format: picojson.h examples/github-issues.cc examples/iostream.cc examples/streaming.cc + clang-format -i $? + +.PHONY: test check clean install uninstall clang-format diff --git a/examples/github-issues.cc b/examples/github-issues.cc index 3e05afd..0235b96 100644 --- a/examples/github-issues.cc +++ b/examples/github-issues.cc @@ -29,32 +29,30 @@ #include "../picojson.h" typedef struct { - char* data; // response data from server - size_t size; // response size of data + char *data; // response data from server + size_t size; // response size of data } MEMFILE; -MEMFILE* -memfopen() { - MEMFILE* mf = (MEMFILE*) malloc(sizeof(MEMFILE)); +MEMFILE *memfopen() { + MEMFILE *mf = (MEMFILE *)malloc(sizeof(MEMFILE)); mf->data = NULL; mf->size = 0; return mf; } -void -memfclose(MEMFILE* mf) { - if (mf->data) free(mf->data); +void memfclose(MEMFILE *mf) { + if (mf->data) + free(mf->data); free(mf); } -size_t -memfwrite(char* ptr, size_t size, size_t nmemb, void* stream) { - MEMFILE* mf = (MEMFILE*) stream; +size_t memfwrite(char *ptr, size_t size, size_t nmemb, void *stream) { + MEMFILE *mf = (MEMFILE *)stream; int block = size * nmemb; if (!mf->data) - mf->data = (char*) malloc(block); + mf->data = (char *)malloc(block); else - mf->data = (char*) realloc(mf->data, mf->size + block); + mf->data = (char *)realloc(mf->data, mf->size + block); if (mf->data) { memcpy(mf->data + mf->size, ptr, block); mf->size += block; @@ -62,9 +60,8 @@ memfwrite(char* ptr, size_t size, size_t nmemb, void* stream) { return block; } -char* -memfstrdup(MEMFILE* mf) { - char* buf = (char*)malloc(mf->size + 1); +char *memfstrdup(MEMFILE *mf) { + char *buf = (char *)malloc(mf->size + 1); memcpy(buf, mf->data, mf->size); buf[mf->size] = 0; return buf; @@ -73,12 +70,11 @@ memfstrdup(MEMFILE* mf) { using namespace std; using namespace picojson; -int -main(int argc, char* argv[]) { +int main(int argc, char *argv[]) { char error[256]; - MEMFILE* mf = memfopen(); - CURL* curl = curl_easy_init(); + MEMFILE *mf = memfopen(); + CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, "https://api.github.com/repos/kazuho/picojson/issues"); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl"); diff --git a/examples/iostream.cc b/examples/iostream.cc index 30cc93f..b145cfd 100644 --- a/examples/iostream.cc +++ b/examples/iostream.cc @@ -27,17 +27,16 @@ */ #include "../picojson.h" -int main(void) -{ +int main(void) { picojson::value v; - + // read json value from stream std::cin >> v; if (std::cin.fail()) { std::cerr << picojson::get_last_error() << std::endl; return 1; } - + // dump json object std::cout << "---- dump input ----" << std::endl; std::cout << v << std::endl; @@ -54,17 +53,17 @@ int main(void) std::cout << "input is " << v.get() << std::endl; } else if (v.is()) { std::cout << "input is an array" << std::endl; - const picojson::array& a = v.get(); + const picojson::array &a = v.get(); for (picojson::array::const_iterator i = a.begin(); i != a.end(); ++i) { std::cout << " " << *i << std::endl; } } else if (v.is()) { std::cout << "input is an object" << std::endl; - const picojson::object& o = v.get(); + const picojson::object &o = v.get(); for (picojson::object::const_iterator i = o.begin(); i != o.end(); ++i) { std::cout << i->first << " " << i->second << std::endl; } } - + return 0; } diff --git a/examples/streaming.cc b/examples/streaming.cc index 8b1225f..bca0209 100644 --- a/examples/streaming.cc +++ b/examples/streaming.cc @@ -33,44 +33,40 @@ // and prints the x and y values to stdout namespace { - - class root_context : public picojson::deny_parse_context { - public: - bool parse_array_start() { - return true; // only allow array as root + +class root_context : public picojson::deny_parse_context { +public: + bool parse_array_start() { + return true; // only allow array as root + } + template bool parse_array_item(picojson::input &in, size_t) { + picojson::value item; + // parse the array item + picojson::default_parse_context ctx(&item); + if (!picojson::_parse(ctx, in)) { + return false; } - template bool parse_array_item(picojson::input& in, size_t) { - picojson::value item; - // parse the array item - picojson::default_parse_context ctx(&item); - if (! picojson::_parse(ctx, in)) { - return false; - } - // assert that the array item is a hash - if (! item.is()) { - return false; - } - // print x and y - std::cout << item.get("x") << ',' << item.get("y").to_str() - << std::endl; - return true; + // assert that the array item is a hash + if (!item.is()) { + return false; } - }; - + // print x and y + std::cout << item.get("x") << ',' << item.get("y").to_str() << std::endl; + return true; + } +}; } -int main(void) -{ +int main(void) { root_context ctx; std::string err; - - picojson::_parse(ctx, std::istream_iterator(std::cin), - std::istream_iterator(), &err); - - if (! err.empty()) { + + picojson::_parse(ctx, std::istream_iterator(std::cin), std::istream_iterator(), &err); + + if (!err.empty()) { std::cerr << err << std::endl; return 1; } - + return 0; } diff --git a/picojson.h b/picojson.h index bd5cd86..f75c399 100644 --- a/picojson.h +++ b/picojson.h @@ -43,1063 +43,1118 @@ #include // for isnan/isinf -#if __cplusplus>=201103L -# include +#if __cplusplus >= 201103L +#include #else extern "C" { -# ifdef _MSC_VER -# include -# elif defined(__INTEL_COMPILER) -# include -# else -# include -# endif +#ifdef _MSC_VER +#include +#elif defined(__INTEL_COMPILER) +#include +#else +#include +#endif } #endif #ifndef PICOJSON_USE_RVALUE_REFERENCE -# if (defined(__cpp_rvalue_references) && __cpp_rvalue_references >= 200610) || (defined(_MSC_VER) && _MSC_VER >= 1600) -# define PICOJSON_USE_RVALUE_REFERENCE 1 -# else -# define PICOJSON_USE_RVALUE_REFERENCE 0 -# endif -#endif//PICOJSON_USE_RVALUE_REFERENCE - +#if (defined(__cpp_rvalue_references) && __cpp_rvalue_references >= 200610) || (defined(_MSC_VER) && _MSC_VER >= 1600) +#define PICOJSON_USE_RVALUE_REFERENCE 1 +#else +#define PICOJSON_USE_RVALUE_REFERENCE 0 +#endif +#endif // PICOJSON_USE_RVALUE_REFERENCE // experimental support for int64_t (see README.mkdn for detail) #ifdef PICOJSON_USE_INT64 -# define __STDC_FORMAT_MACROS -# include -# include +#define __STDC_FORMAT_MACROS +#include +#include #endif // to disable the use of localeconv(3), set PICOJSON_USE_LOCALE to 0 #ifndef PICOJSON_USE_LOCALE -# define PICOJSON_USE_LOCALE 1 +#define PICOJSON_USE_LOCALE 1 #endif #if PICOJSON_USE_LOCALE extern "C" { -# include +#include } #endif #ifndef PICOJSON_ASSERT -# define PICOJSON_ASSERT(e) do { if (! (e)) throw std::runtime_error(#e); } while (0) +#define PICOJSON_ASSERT(e) \ + do { \ + if (!(e)) \ + throw std::runtime_error(#e); \ + } while (0) #endif #ifdef _MSC_VER - #define SNPRINTF _snprintf_s - #pragma warning(push) - #pragma warning(disable : 4244) // conversion from int to char - #pragma warning(disable : 4127) // conditional expression is constant - #pragma warning(disable : 4702) // unreachable code +#define SNPRINTF _snprintf_s +#pragma warning(push) +#pragma warning(disable : 4244) // conversion from int to char +#pragma warning(disable : 4127) // conditional expression is constant +#pragma warning(disable : 4702) // unreachable code #else - #define SNPRINTF snprintf +#define SNPRINTF snprintf #endif namespace picojson { - enum { - null_type, - boolean_type, - number_type, - string_type, - array_type, - object_type +enum { + null_type, + boolean_type, + number_type, + string_type, + array_type, + object_type #ifdef PICOJSON_USE_INT64 - , int64_type + , + int64_type #endif - }; +}; - enum { - INDENT_WIDTH = 2 - }; +enum { INDENT_WIDTH = 2 }; - struct null {}; +struct null {}; - class value { - public: - typedef std::vector array; - typedef std::map object; - union _storage { - bool boolean_; - double number_; +class value { +public: + typedef std::vector array; + typedef std::map object; + union _storage { + bool boolean_; + double number_; #ifdef PICOJSON_USE_INT64 - int64_t int64_; + int64_t int64_; #endif - std::string* string_; - array* array_; - object* object_; - }; - protected: - int type_; - _storage u_; - public: - value(); - value(int type, bool); - explicit value(bool b); + std::string *string_; + array *array_; + object *object_; + }; + +protected: + int type_; + _storage u_; + +public: + value(); + value(int type, bool); + explicit value(bool b); #ifdef PICOJSON_USE_INT64 - explicit value(int64_t i); + explicit value(int64_t i); #endif - explicit value(double n); - explicit value(const std::string& s); - explicit value(const array& a); - explicit value(const object& o); + explicit value(double n); + explicit value(const std::string &s); + explicit value(const array &a); + explicit value(const object &o); #if PICOJSON_USE_RVALUE_REFERENCE - explicit value(std::string&& s); - explicit value(array&& a); - explicit value(object&& o); + explicit value(std::string &&s); + explicit value(array &&a); + explicit value(object &&o); #endif - explicit value(const char* s); - value(const char* s, size_t len); - ~value(); - value(const value& x); - value& operator=(const value& x); + explicit value(const char *s); + value(const char *s, size_t len); + ~value(); + value(const value &x); + value &operator=(const value &x); #if PICOJSON_USE_RVALUE_REFERENCE - value(value&& x)throw(); - value& operator=(value&& x)throw(); + value(value &&x) throw(); + value &operator=(value &&x) throw(); #endif - void swap(value& x)throw(); - template bool is() const; - template const T& get() const; - template T& get(); - template void set(const T &); + void swap(value &x) throw(); + template bool is() const; + template const T &get() const; + template T &get(); + template void set(const T &); #if PICOJSON_USE_RVALUE_REFERENCE - template void set(T &&); + template void set(T &&); #endif - bool evaluate_as_boolean() const; - const value& get(const size_t idx) const; - const value& get(const std::string& key) const; - value& get(const size_t idx); - value& get(const std::string& key); - - bool contains(const size_t idx) const; - bool contains(const std::string& key) const; - std::string to_str() const; - template void serialize(Iter os, bool prettify = false) const; - std::string serialize(bool prettify = false) const; - private: - template value(const T*); // intentionally defined to block implicit conversion of pointer to bool - template static void _indent(Iter os, int indent); - template void _serialize(Iter os, int indent) const; - std::string _serialize(int indent) const; - void clear(); - }; - - typedef value::array array; - typedef value::object object; - - inline value::value() : type_(null_type), u_() {} + bool evaluate_as_boolean() const; + const value &get(const size_t idx) const; + const value &get(const std::string &key) const; + value &get(const size_t idx); + value &get(const std::string &key); + + bool contains(const size_t idx) const; + bool contains(const std::string &key) const; + std::string to_str() const; + template void serialize(Iter os, bool prettify = false) const; + std::string serialize(bool prettify = false) const; + +private: + template value(const T *); // intentionally defined to block implicit conversion of pointer to bool + template static void _indent(Iter os, int indent); + template void _serialize(Iter os, int indent) const; + std::string _serialize(int indent) const; + void clear(); +}; + +typedef value::array array; +typedef value::object object; + +inline value::value() : type_(null_type), u_() { +} - inline value::value(int type, bool) : type_(type), u_() { - switch (type) { -#define INIT(p, v) case p##type: u_.p = v; break - INIT(boolean_, false); - INIT(number_, 0.0); +inline value::value(int type, bool) : type_(type), u_() { + switch (type) { +#define INIT(p, v) \ + case p##type: \ + u_.p = v; \ + break + INIT(boolean_, false); + INIT(number_, 0.0); #ifdef PICOJSON_USE_INT64 - INIT(int64_, 0); + INIT(int64_, 0); #endif - INIT(string_, new std::string()); - INIT(array_, new array()); - INIT(object_, new object()); + INIT(string_, new std::string()); + INIT(array_, new array()); + INIT(object_, new object()); #undef INIT - default: break; - } + default: + break; } +} - inline value::value(bool b) : type_(boolean_type), u_() { - u_.boolean_ = b; - } +inline value::value(bool b) : type_(boolean_type), u_() { + u_.boolean_ = b; +} #ifdef PICOJSON_USE_INT64 - inline value::value(int64_t i) : type_(int64_type), u_() { - u_.int64_ = i; - } +inline value::value(int64_t i) : type_(int64_type), u_() { + u_.int64_ = i; +} #endif - inline value::value(double n) : type_(number_type), u_() { - if ( +inline value::value(double n) : type_(number_type), u_() { + if ( #ifdef _MSC_VER - ! _finite(n) -#elif __cplusplus>=201103L || !(defined(isnan) && defined(isinf)) - std::isnan(n) || std::isinf(n) + !_finite(n) +#elif __cplusplus >= 201103L || !(defined(isnan) && defined(isinf)) + std::isnan(n) || std::isinf(n) #else - isnan(n) || isinf(n) + isnan(n) || isinf(n) #endif - ) { - throw std::overflow_error(""); - } - u_.number_ = n; + ) { + throw std::overflow_error(""); } + u_.number_ = n; +} - inline value::value(const std::string& s) : type_(string_type), u_() { - u_.string_ = new std::string(s); - } +inline value::value(const std::string &s) : type_(string_type), u_() { + u_.string_ = new std::string(s); +} - inline value::value(const array& a) : type_(array_type), u_() { - u_.array_ = new array(a); - } +inline value::value(const array &a) : type_(array_type), u_() { + u_.array_ = new array(a); +} - inline value::value(const object& o) : type_(object_type), u_() { - u_.object_ = new object(o); - } +inline value::value(const object &o) : type_(object_type), u_() { + u_.object_ = new object(o); +} #if PICOJSON_USE_RVALUE_REFERENCE - inline value::value(std::string&& s) : type_(string_type), u_() { - u_.string_ = new std::string(std::move(s)); - } +inline value::value(std::string &&s) : type_(string_type), u_() { + u_.string_ = new std::string(std::move(s)); +} - inline value::value(array&& a) : type_(array_type), u_() { - u_.array_ = new array(std::move(a)); - } +inline value::value(array &&a) : type_(array_type), u_() { + u_.array_ = new array(std::move(a)); +} - inline value::value(object&& o) : type_(object_type), u_() { - u_.object_ = new object(std::move(o)); - } +inline value::value(object &&o) : type_(object_type), u_() { + u_.object_ = new object(std::move(o)); +} #endif - inline value::value(const char* s) : type_(string_type), u_() { - u_.string_ = new std::string(s); - } +inline value::value(const char *s) : type_(string_type), u_() { + u_.string_ = new std::string(s); +} - inline value::value(const char* s, size_t len) : type_(string_type), u_() { - u_.string_ = new std::string(s, len); - } +inline value::value(const char *s, size_t len) : type_(string_type), u_() { + u_.string_ = new std::string(s, len); +} - inline void value::clear() { - switch (type_) { -#define DEINIT(p) case p##type: delete u_.p; break - DEINIT(string_); - DEINIT(array_); - DEINIT(object_); +inline void value::clear() { + switch (type_) { +#define DEINIT(p) \ + case p##type: \ + delete u_.p; \ + break + DEINIT(string_); + DEINIT(array_); + DEINIT(object_); #undef DEINIT - default: break; - } + default: + break; } +} - inline value::~value() { - clear(); - } +inline value::~value() { + clear(); +} - inline value::value(const value& x) : type_(x.type_), u_() { - switch (type_) { -#define INIT(p, v) case p##type: u_.p = v; break - INIT(string_, new std::string(*x.u_.string_)); - INIT(array_, new array(*x.u_.array_)); - INIT(object_, new object(*x.u_.object_)); +inline value::value(const value &x) : type_(x.type_), u_() { + switch (type_) { +#define INIT(p, v) \ + case p##type: \ + u_.p = v; \ + break + INIT(string_, new std::string(*x.u_.string_)); + INIT(array_, new array(*x.u_.array_)); + INIT(object_, new object(*x.u_.object_)); #undef INIT - default: - u_ = x.u_; - break; - } + default: + u_ = x.u_; + break; } +} - inline value& value::operator=(const value& x) { - if (this != &x) { - value t(x); - swap(t); - } - return *this; +inline value &value::operator=(const value &x) { + if (this != &x) { + value t(x); + swap(t); } + return *this; +} #if PICOJSON_USE_RVALUE_REFERENCE - inline value::value(value&& x)throw() : type_(null_type), u_() { - swap(x); - } - inline value& value::operator=(value&& x)throw() { - swap(x); - return *this; - } +inline value::value(value &&x) throw() : type_(null_type), u_() { + swap(x); +} +inline value &value::operator=(value &&x) throw() { + swap(x); + return *this; +} #endif - inline void value::swap(value& x)throw() { - std::swap(type_, x.type_); - std::swap(u_, x.u_); - } +inline void value::swap(value &x) throw() { + std::swap(type_, x.type_); + std::swap(u_, x.u_); +} -#define IS(ctype, jtype) \ - template <> inline bool value::is() const { \ - return type_ == jtype##_type; \ +#define IS(ctype, jtype) \ + template <> inline bool value::is() const { \ + return type_ == jtype##_type; \ } - IS(null, null) - IS(bool, boolean) +IS(null, null) +IS(bool, boolean) #ifdef PICOJSON_USE_INT64 - IS(int64_t, int64) +IS(int64_t, int64) #endif - IS(std::string, string) - IS(array, array) - IS(object, object) +IS(std::string, string) +IS(array, array) +IS(object, object) #undef IS - template <> inline bool value::is() const { - return type_ == number_type +template <> inline bool value::is() const { + return type_ == number_type #ifdef PICOJSON_USE_INT64 - || type_ == int64_type + || type_ == int64_type #endif ; - } +} -#define GET(ctype, var) \ - template <> inline const ctype& value::get() const { \ - PICOJSON_ASSERT("type mismatch! call is() before get()" \ - && is()); \ - return var; \ - } \ - template <> inline ctype& value::get() { \ - PICOJSON_ASSERT("type mismatch! call is() before get()" \ - && is()); \ - return var; \ - } - GET(bool, u_.boolean_) - GET(std::string, *u_.string_) - GET(array, *u_.array_) - GET(object, *u_.object_) +#define GET(ctype, var) \ + template <> inline const ctype &value::get() const { \ + PICOJSON_ASSERT("type mismatch! call is() before get()" && is()); \ + return var; \ + } \ + template <> inline ctype &value::get() { \ + PICOJSON_ASSERT("type mismatch! call is() before get()" && is()); \ + return var; \ + } +GET(bool, u_.boolean_) +GET(std::string, *u_.string_) +GET(array, *u_.array_) +GET(object, *u_.object_) #ifdef PICOJSON_USE_INT64 - GET(double, (type_ == int64_type && (const_cast(this)->type_ = number_type, const_cast(this)->u_.number_ = u_.int64_), u_.number_)) - GET(int64_t, u_.int64_) +GET(double, + (type_ == int64_type && (const_cast(this)->type_ = number_type, const_cast(this)->u_.number_ = u_.int64_), + u_.number_)) +GET(int64_t, u_.int64_) #else - GET(double, u_.number_) +GET(double, u_.number_) #endif #undef GET -#define SET(ctype, jtype, setter) \ - template <> inline void value::set(const ctype &_val) { \ - clear(); \ - type_ = jtype##_type; \ - setter \ - } - SET(bool, boolean, u_.boolean_ = _val;) - SET(std::string, string, u_.string_ = new std::string(_val);) - SET(array, array, u_.array_ = new array(_val);) - SET(object, object, u_.object_ = new object(_val);) - SET(double, number, u_.number_ = _val;) +#define SET(ctype, jtype, setter) \ + template <> inline void value::set(const ctype &_val) { \ + clear(); \ + type_ = jtype##_type; \ + setter \ + } +SET(bool, boolean, u_.boolean_ = _val;) +SET(std::string, string, u_.string_ = new std::string(_val);) +SET(array, array, u_.array_ = new array(_val);) +SET(object, object, u_.object_ = new object(_val);) +SET(double, number, u_.number_ = _val;) #ifdef PICOJSON_USE_INT64 - SET(int64_t, int64, u_.int64_ = _val;) +SET(int64_t, int64, u_.int64_ = _val;) #endif #undef SET #if PICOJSON_USE_RVALUE_REFERENCE -#define MOVESET(ctype, jtype, setter) \ - template <> inline void value::set(ctype &&_val) { \ - clear(); \ - type_ = jtype##_type; \ - setter \ - } - MOVESET(std::string, string, u_.string_ = new std::string(std::move(_val));) - MOVESET(array, array, u_.array_ = new array(std::move(_val));) - MOVESET(object, object, u_.object_ = new object(std::move(_val));) +#define MOVESET(ctype, jtype, setter) \ + template <> inline void value::set(ctype && _val) { \ + clear(); \ + type_ = jtype##_type; \ + setter \ + } +MOVESET(std::string, string, u_.string_ = new std::string(std::move(_val));) +MOVESET(array, array, u_.array_ = new array(std::move(_val));) +MOVESET(object, object, u_.object_ = new object(std::move(_val));) #undef MOVESET #endif - inline bool value::evaluate_as_boolean() const { - switch (type_) { - case null_type: - return false; - case boolean_type: - return u_.boolean_; - case number_type: - return u_.number_ != 0; +inline bool value::evaluate_as_boolean() const { + switch (type_) { + case null_type: + return false; + case boolean_type: + return u_.boolean_; + case number_type: + return u_.number_ != 0; #ifdef PICOJSON_USE_INT64 - case int64_type: - return u_.int64_ != 0; + case int64_type: + return u_.int64_ != 0; #endif - case string_type: - return ! u_.string_->empty(); - default: - return true; - } + case string_type: + return !u_.string_->empty(); + default: + return true; } +} - inline const value& value::get(const size_t idx) const { - static value s_null; - PICOJSON_ASSERT(is()); - return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null; - } +inline const value &value::get(const size_t idx) const { + static value s_null; + PICOJSON_ASSERT(is()); + return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null; +} - inline value& value::get(const size_t idx) { - static value s_null; - PICOJSON_ASSERT(is()); - return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null; - } +inline value &value::get(const size_t idx) { + static value s_null; + PICOJSON_ASSERT(is()); + return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null; +} - inline const value& value::get(const std::string& key) const { - static value s_null; - PICOJSON_ASSERT(is()); - object::const_iterator i = u_.object_->find(key); - return i != u_.object_->end() ? i->second : s_null; - } +inline const value &value::get(const std::string &key) const { + static value s_null; + PICOJSON_ASSERT(is()); + object::const_iterator i = u_.object_->find(key); + return i != u_.object_->end() ? i->second : s_null; +} - inline value& value::get(const std::string& key) { - static value s_null; - PICOJSON_ASSERT(is()); - object::iterator i = u_.object_->find(key); - return i != u_.object_->end() ? i->second : s_null; - } +inline value &value::get(const std::string &key) { + static value s_null; + PICOJSON_ASSERT(is()); + object::iterator i = u_.object_->find(key); + return i != u_.object_->end() ? i->second : s_null; +} - inline bool value::contains(const size_t idx) const { - PICOJSON_ASSERT(is()); - return idx < u_.array_->size(); - } +inline bool value::contains(const size_t idx) const { + PICOJSON_ASSERT(is()); + return idx < u_.array_->size(); +} - inline bool value::contains(const std::string& key) const { - PICOJSON_ASSERT(is()); - object::const_iterator i = u_.object_->find(key); - return i != u_.object_->end(); - } +inline bool value::contains(const std::string &key) const { + PICOJSON_ASSERT(is()); + object::const_iterator i = u_.object_->find(key); + return i != u_.object_->end(); +} - inline std::string value::to_str() const { - switch (type_) { - case null_type: return "null"; - case boolean_type: return u_.boolean_ ? "true" : "false"; +inline std::string value::to_str() const { + switch (type_) { + case null_type: + return "null"; + case boolean_type: + return u_.boolean_ ? "true" : "false"; #ifdef PICOJSON_USE_INT64 - case int64_type: { - char buf[sizeof("-9223372036854775808")]; - SNPRINTF(buf, sizeof(buf), "%" PRId64, u_.int64_); - return buf; - } + case int64_type: { + char buf[sizeof("-9223372036854775808")]; + SNPRINTF(buf, sizeof(buf), "%" PRId64, u_.int64_); + return buf; + } #endif - case number_type: { - char buf[256]; - double tmp; - SNPRINTF(buf, sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ? "%.f" : "%.17g", u_.number_); + case number_type: { + char buf[256]; + double tmp; + SNPRINTF(buf, sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ? "%.f" : "%.17g", u_.number_); #if PICOJSON_USE_LOCALE - char *decimal_point = localeconv()->decimal_point; - if (strcmp(decimal_point, ".") != 0) { - size_t decimal_point_len = strlen(decimal_point); - for (char *p = buf; *p != '\0'; ++p) { - if (strncmp(p, decimal_point, decimal_point_len) == 0) { - return std::string(buf, p) + "." + (p + decimal_point_len); - } + char *decimal_point = localeconv()->decimal_point; + if (strcmp(decimal_point, ".") != 0) { + size_t decimal_point_len = strlen(decimal_point); + for (char *p = buf; *p != '\0'; ++p) { + if (strncmp(p, decimal_point, decimal_point_len) == 0) { + return std::string(buf, p) + "." + (p + decimal_point_len); } } -#endif - return buf; } - case string_type: return *u_.string_; - case array_type: return "array"; - case object_type: return "object"; - default: PICOJSON_ASSERT(0); +#endif + return buf; + } + case string_type: + return *u_.string_; + case array_type: + return "array"; + case object_type: + return "object"; + default: + PICOJSON_ASSERT(0); #ifdef _MSC_VER - __assume(0); + __assume(0); #endif - } - return std::string(); - } - - template void copy(const std::string& s, Iter oi) { - std::copy(s.begin(), s.end(), oi); - } - - template - struct serialize_str_char { - Iter oi; - void operator()(char c) { - switch (c) { -#define MAP(val, sym) case val: copy(sym, oi); break - MAP('"', "\\\""); - MAP('\\', "\\\\"); - MAP('/', "\\/"); - MAP('\b', "\\b"); - MAP('\f', "\\f"); - MAP('\n', "\\n"); - MAP('\r', "\\r"); - MAP('\t', "\\t"); + } + return std::string(); +} + +template void copy(const std::string &s, Iter oi) { + std::copy(s.begin(), s.end(), oi); +} + +template struct serialize_str_char { + Iter oi; + void operator()(char c) { + switch (c) { +#define MAP(val, sym) \ + case val: \ + copy(sym, oi); \ + break + MAP('"', "\\\""); + MAP('\\', "\\\\"); + MAP('/', "\\/"); + MAP('\b', "\\b"); + MAP('\f', "\\f"); + MAP('\n', "\\n"); + MAP('\r', "\\r"); + MAP('\t', "\\t"); #undef MAP - default: - if (static_cast(c) < 0x20 || c == 0x7f) { - char buf[7]; - SNPRINTF(buf, sizeof(buf), "\\u%04x", c & 0xff); - copy(buf, buf + 6, oi); - } else { - *oi++ = c; - } - break; + default: + if (static_cast(c) < 0x20 || c == 0x7f) { + char buf[7]; + SNPRINTF(buf, sizeof(buf), "\\u%04x", c & 0xff); + copy(buf, buf + 6, oi); + } else { + *oi++ = c; } + break; } - }; - - template void serialize_str(const std::string& s, Iter oi) { - *oi++ = '"'; - serialize_str_char process_char = { oi }; - std::for_each(s.begin(), s.end(), process_char); - *oi++ = '"'; } +}; - template void value::serialize(Iter oi, bool prettify) const { - return _serialize(oi, prettify ? 0 : -1); - } +template void serialize_str(const std::string &s, Iter oi) { + *oi++ = '"'; + serialize_str_char process_char = {oi}; + std::for_each(s.begin(), s.end(), process_char); + *oi++ = '"'; +} - inline std::string value::serialize(bool prettify) const { - return _serialize(prettify ? 0 : -1); - } +template void value::serialize(Iter oi, bool prettify) const { + return _serialize(oi, prettify ? 0 : -1); +} - template void value::_indent(Iter oi, int indent) { - *oi++ = '\n'; - for (int i = 0; i < indent * INDENT_WIDTH; ++i) { - *oi++ = ' '; - } +inline std::string value::serialize(bool prettify) const { + return _serialize(prettify ? 0 : -1); +} + +template void value::_indent(Iter oi, int indent) { + *oi++ = '\n'; + for (int i = 0; i < indent * INDENT_WIDTH; ++i) { + *oi++ = ' '; } +} - template void value::_serialize(Iter oi, int indent) const { - switch (type_) { - case string_type: - serialize_str(*u_.string_, oi); - break; - case array_type: { - *oi++ = '['; - if (indent != -1) { - ++indent; - } - for (array::const_iterator i = u_.array_->begin(); - i != u_.array_->end(); - ++i) { - if (i != u_.array_->begin()) { - *oi++ = ','; - } - if (indent != -1) { - _indent(oi, indent); - } - i->_serialize(oi, indent); +template void value::_serialize(Iter oi, int indent) const { + switch (type_) { + case string_type: + serialize_str(*u_.string_, oi); + break; + case array_type: { + *oi++ = '['; + if (indent != -1) { + ++indent; + } + for (array::const_iterator i = u_.array_->begin(); i != u_.array_->end(); ++i) { + if (i != u_.array_->begin()) { + *oi++ = ','; } if (indent != -1) { - --indent; - if (! u_.array_->empty()) { - _indent(oi, indent); - } + _indent(oi, indent); } - *oi++ = ']'; - break; + i->_serialize(oi, indent); } - case object_type: { - *oi++ = '{'; - if (indent != -1) { - ++indent; + if (indent != -1) { + --indent; + if (!u_.array_->empty()) { + _indent(oi, indent); } - for (object::const_iterator i = u_.object_->begin(); - i != u_.object_->end(); - ++i) { - if (i != u_.object_->begin()) { - *oi++ = ','; - } - if (indent != -1) { - _indent(oi, indent); - } - serialize_str(i->first, oi); - *oi++ = ':'; - if (indent != -1) { - *oi++ = ' '; - } - i->second._serialize(oi, indent); + } + *oi++ = ']'; + break; + } + case object_type: { + *oi++ = '{'; + if (indent != -1) { + ++indent; + } + for (object::const_iterator i = u_.object_->begin(); i != u_.object_->end(); ++i) { + if (i != u_.object_->begin()) { + *oi++ = ','; } if (indent != -1) { - --indent; - if (! u_.object_->empty()) { - _indent(oi, indent); - } + _indent(oi, indent); } - *oi++ = '}'; - break; - } - default: - copy(to_str(), oi); - break; + serialize_str(i->first, oi); + *oi++ = ':'; + if (indent != -1) { + *oi++ = ' '; + } + i->second._serialize(oi, indent); } - if (indent == 0) { - *oi++ = '\n'; + if (indent != -1) { + --indent; + if (!u_.object_->empty()) { + _indent(oi, indent); + } } + *oi++ = '}'; + break; } - - inline std::string value::_serialize(int indent) const { - std::string s; - _serialize(std::back_inserter(s), indent); - return s; + default: + copy(to_str(), oi); + break; + } + if (indent == 0) { + *oi++ = '\n'; } +} - template class input { - protected: - Iter cur_, end_; - bool consumed_; - int line_; - public: - input(const Iter& first, const Iter& last) : cur_(first), end_(last), consumed_(false), line_(1) {} - int getc() { - if (consumed_) { - if (*cur_ == '\n') { - ++line_; - } - ++cur_; - } - if (cur_ == end_) { - consumed_ = false; - return -1; +inline std::string value::_serialize(int indent) const { + std::string s; + _serialize(std::back_inserter(s), indent); + return s; +} + +template class input { +protected: + Iter cur_, end_; + bool consumed_; + int line_; + +public: + input(const Iter &first, const Iter &last) : cur_(first), end_(last), consumed_(false), line_(1) { + } + int getc() { + if (consumed_) { + if (*cur_ == '\n') { + ++line_; } - consumed_ = true; - return *cur_ & 0xff; + ++cur_; } - void ungetc() { + if (cur_ == end_) { consumed_ = false; + return -1; } - Iter cur() const { - if (consumed_) { - input *self = const_cast*>(this); - self->consumed_ = false; - ++self->cur_; - } - return cur_; + consumed_ = true; + return *cur_ & 0xff; + } + void ungetc() { + consumed_ = false; + } + Iter cur() const { + if (consumed_) { + input *self = const_cast *>(this); + self->consumed_ = false; + ++self->cur_; } - int line() const { return line_; } - void skip_ws() { - while (1) { - int ch = getc(); - if (! (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) { - ungetc(); - break; - } + return cur_; + } + int line() const { + return line_; + } + void skip_ws() { + while (1) { + int ch = getc(); + if (!(ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) { + ungetc(); + break; } } - bool expect(const int expected) { - skip_ws(); - if (getc() != expected) { - ungetc(); - return false; - } - return true; + } + bool expect(const int expected) { + skip_ws(); + if (getc() != expected) { + ungetc(); + return false; } - bool match(const std::string& pattern) { - for (std::string::const_iterator pi(pattern.begin()); - pi != pattern.end(); - ++pi) { - if (getc() != *pi) { - ungetc(); - return false; - } + return true; + } + bool match(const std::string &pattern) { + for (std::string::const_iterator pi(pattern.begin()); pi != pattern.end(); ++pi) { + if (getc() != *pi) { + ungetc(); + return false; } - return true; } - }; + return true; + } +}; - template inline int _parse_quadhex(input &in) { - int uni_ch = 0, hex; - for (int i = 0; i < 4; i++) { - if ((hex = in.getc()) == -1) { - return -1; - } - if ('0' <= hex && hex <= '9') { - hex -= '0'; - } else if ('A' <= hex && hex <= 'F') { - hex -= 'A' - 0xa; - } else if ('a' <= hex && hex <= 'f') { - hex -= 'a' - 0xa; - } else { - in.ungetc(); - return -1; - } - uni_ch = uni_ch * 16 + hex; +template inline int _parse_quadhex(input &in) { + int uni_ch = 0, hex; + for (int i = 0; i < 4; i++) { + if ((hex = in.getc()) == -1) { + return -1; } - return uni_ch; + if ('0' <= hex && hex <= '9') { + hex -= '0'; + } else if ('A' <= hex && hex <= 'F') { + hex -= 'A' - 0xa; + } else if ('a' <= hex && hex <= 'f') { + hex -= 'a' - 0xa; + } else { + in.ungetc(); + return -1; + } + uni_ch = uni_ch * 16 + hex; } + return uni_ch; +} - template inline bool _parse_codepoint(String& out, input& in) { - int uni_ch; - if ((uni_ch = _parse_quadhex(in)) == -1) { +template inline bool _parse_codepoint(String &out, input &in) { + int uni_ch; + if ((uni_ch = _parse_quadhex(in)) == -1) { + return false; + } + if (0xd800 <= uni_ch && uni_ch <= 0xdfff) { + if (0xdc00 <= uni_ch) { + // a second 16-bit of a surrogate pair appeared return false; } - if (0xd800 <= uni_ch && uni_ch <= 0xdfff) { - if (0xdc00 <= uni_ch) { - // a second 16-bit of a surrogate pair appeared - return false; - } - // first 16-bit of surrogate pair, get the next one - if (in.getc() != '\\' || in.getc() != 'u') { - in.ungetc(); - return false; - } - int second = _parse_quadhex(in); - if (! (0xdc00 <= second && second <= 0xdfff)) { - return false; - } - uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff); - uni_ch += 0x10000; + // first 16-bit of surrogate pair, get the next one + if (in.getc() != '\\' || in.getc() != 'u') { + in.ungetc(); + return false; + } + int second = _parse_quadhex(in); + if (!(0xdc00 <= second && second <= 0xdfff)) { + return false; } - if (uni_ch < 0x80) { - out.push_back( static_cast(uni_ch) ); + uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff); + uni_ch += 0x10000; + } + if (uni_ch < 0x80) { + out.push_back(static_cast(uni_ch)); + } else { + if (uni_ch < 0x800) { + out.push_back(static_cast(0xc0 | (uni_ch >> 6))); } else { - if (uni_ch < 0x800) { - out.push_back( static_cast(0xc0 | (uni_ch >> 6)) ); + if (uni_ch < 0x10000) { + out.push_back(static_cast(0xe0 | (uni_ch >> 12))); } else { - if (uni_ch < 0x10000) { - out.push_back( static_cast(0xe0 | (uni_ch >> 12)) ); - } else { - out.push_back( static_cast(0xf0 | (uni_ch >> 18)) ); - out.push_back( static_cast(0x80 | ((uni_ch >> 12) & 0x3f)) ); - } - out.push_back( static_cast(0x80 | ((uni_ch >> 6) & 0x3f)) ); + out.push_back(static_cast(0xf0 | (uni_ch >> 18))); + out.push_back(static_cast(0x80 | ((uni_ch >> 12) & 0x3f))); } - out.push_back( static_cast(0x80 | (uni_ch & 0x3f)) ); + out.push_back(static_cast(0x80 | ((uni_ch >> 6) & 0x3f))); } - return true; + out.push_back(static_cast(0x80 | (uni_ch & 0x3f))); } + return true; +} - template inline bool _parse_string(String& out, input& in) { - while (1) { - int ch = in.getc(); - if (ch < ' ') { - in.ungetc(); - return false; - } else if (ch == '"') { - return true; - } else if (ch == '\\') { - if ((ch = in.getc()) == -1) { - return false; - } - switch (ch) { -#define MAP(sym, val) case sym: out.push_back(val); break - MAP('"', '\"'); - MAP('\\', '\\'); - MAP('/', '/'); - MAP('b', '\b'); - MAP('f', '\f'); - MAP('n', '\n'); - MAP('r', '\r'); - MAP('t', '\t'); +template inline bool _parse_string(String &out, input &in) { + while (1) { + int ch = in.getc(); + if (ch < ' ') { + in.ungetc(); + return false; + } else if (ch == '"') { + return true; + } else if (ch == '\\') { + if ((ch = in.getc()) == -1) { + return false; + } + switch (ch) { +#define MAP(sym, val) \ + case sym: \ + out.push_back(val); \ + break + MAP('"', '\"'); + MAP('\\', '\\'); + MAP('/', '/'); + MAP('b', '\b'); + MAP('f', '\f'); + MAP('n', '\n'); + MAP('r', '\r'); + MAP('t', '\t'); #undef MAP - case 'u': - if (! _parse_codepoint(out, in)) { - return false; - } - break; - default: - return false; - } - } else { - out.push_back( static_cast(ch) ); + case 'u': + if (!_parse_codepoint(out, in)) { + return false; + } + break; + default: + return false; } + } else { + out.push_back(static_cast(ch)); } - return false; } + return false; +} - template inline bool _parse_array(Context& ctx, input& in) { - if (! ctx.parse_array_start()) { +template inline bool _parse_array(Context &ctx, input &in) { + if (!ctx.parse_array_start()) { + return false; + } + size_t idx = 0; + if (in.expect(']')) { + return ctx.parse_array_stop(idx); + } + do { + if (!ctx.parse_array_item(in, idx)) { return false; } - size_t idx = 0; - if (in.expect(']')) { - return ctx.parse_array_stop(idx); - } - do { - if (! ctx.parse_array_item(in, idx)) { - return false; - } - idx++; - } while (in.expect(',')); - return in.expect(']') && ctx.parse_array_stop(idx); - } + idx++; + } while (in.expect(',')); + return in.expect(']') && ctx.parse_array_stop(idx); +} - template inline bool _parse_object(Context& ctx, input& in) { - if (! ctx.parse_object_start()) { +template inline bool _parse_object(Context &ctx, input &in) { + if (!ctx.parse_object_start()) { + return false; + } + if (in.expect('}')) { + return true; + } + do { + std::string key; + if (!in.expect('"') || !_parse_string(key, in) || !in.expect(':')) { return false; } - if (in.expect('}')) { - return true; + if (!ctx.parse_object_item(in, key)) { + return false; } - do { - std::string key; - if (! in.expect('"') - || ! _parse_string(key, in) - || ! in.expect(':')) { - return false; - } - if (! ctx.parse_object_item(in, key)) { - return false; - } - } while (in.expect(',')); - return in.expect('}'); - } + } while (in.expect(',')); + return in.expect('}'); +} - template inline std::string _parse_number(input& in) { - std::string num_str; - while (1) { - int ch = in.getc(); - if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' - || ch == 'e' || ch == 'E') { - num_str.push_back( static_cast(ch) ); - } else if (ch == '.') { +template inline std::string _parse_number(input &in) { + std::string num_str; + while (1) { + int ch = in.getc(); + if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' || ch == 'e' || ch == 'E') { + num_str.push_back(static_cast(ch)); + } else if (ch == '.') { #if PICOJSON_USE_LOCALE - num_str += localeconv()->decimal_point; + num_str += localeconv()->decimal_point; #else - num_str.push_back('.'); + num_str.push_back('.'); #endif - } else { - in.ungetc(); - break; - } + } else { + in.ungetc(); + break; } - return num_str; } + return num_str; +} - template inline bool _parse(Context& ctx, input& in) { - in.skip_ws(); - int ch = in.getc(); - switch (ch) { -#define IS(ch, text, op) case ch: \ - if (in.match(text) && op) { \ - return true; \ - } else { \ - return false; \ - } - IS('n', "ull", ctx.set_null()); - IS('f', "alse", ctx.set_bool(false)); - IS('t', "rue", ctx.set_bool(true)); +template inline bool _parse(Context &ctx, input &in) { + in.skip_ws(); + int ch = in.getc(); + switch (ch) { +#define IS(ch, text, op) \ + case ch: \ + if (in.match(text) && op) { \ + return true; \ + } else { \ + return false; \ + } + IS('n', "ull", ctx.set_null()); + IS('f', "alse", ctx.set_bool(false)); + IS('t', "rue", ctx.set_bool(true)); #undef IS - case '"': - return ctx.parse_string(in); - case '[': - return _parse_array(ctx, in); - case '{': - return _parse_object(ctx, in); - default: - if (('0' <= ch && ch <= '9') || ch == '-') { - double f; - char *endp; - in.ungetc(); - std::string num_str( _parse_number(in) ); - if (num_str.empty()) { - return false; - } + case '"': + return ctx.parse_string(in); + case '[': + return _parse_array(ctx, in); + case '{': + return _parse_object(ctx, in); + default: + if (('0' <= ch && ch <= '9') || ch == '-') { + double f; + char *endp; + in.ungetc(); + std::string num_str(_parse_number(in)); + if (num_str.empty()) { + return false; + } #ifdef PICOJSON_USE_INT64 - { - errno = 0; - intmax_t ival = strtoimax(num_str.c_str(), &endp, 10); - if (errno == 0 - && std::numeric_limits::min() <= ival - && ival <= std::numeric_limits::max() - && endp == num_str.c_str() + num_str.size()) { - ctx.set_int64(ival); - return true; - } - } -#endif - f = strtod(num_str.c_str(), &endp); - if (endp == num_str.c_str() + num_str.size()) { - ctx.set_number(f); + { + errno = 0; + intmax_t ival = strtoimax(num_str.c_str(), &endp, 10); + if (errno == 0 && std::numeric_limits::min() <= ival && ival <= std::numeric_limits::max() && + endp == num_str.c_str() + num_str.size()) { + ctx.set_int64(ival); return true; } - return false; } - break; +#endif + f = strtod(num_str.c_str(), &endp); + if (endp == num_str.c_str() + num_str.size()) { + ctx.set_number(f); + return true; + } + return false; } - in.ungetc(); - return false; + break; } + in.ungetc(); + return false; +} - class deny_parse_context { - public: - bool set_null() { return false; } - bool set_bool(bool) { return false; } +class deny_parse_context { +public: + bool set_null() { + return false; + } + bool set_bool(bool) { + return false; + } #ifdef PICOJSON_USE_INT64 - bool set_int64(int64_t) { return false; } + bool set_int64(int64_t) { + return false; + } #endif - bool set_number(double) { return false; } - template bool parse_string(input&) { return false; } - bool parse_array_start() { return false; } - template bool parse_array_item(input&, size_t) { - return false; - } - bool parse_array_stop(size_t) { return false; } - bool parse_object_start() { return false; } - template bool parse_object_item(input&, const std::string&) { - return false; - } - }; + bool set_number(double) { + return false; + } + template bool parse_string(input &) { + return false; + } + bool parse_array_start() { + return false; + } + template bool parse_array_item(input &, size_t) { + return false; + } + bool parse_array_stop(size_t) { + return false; + } + bool parse_object_start() { + return false; + } + template bool parse_object_item(input &, const std::string &) { + return false; + } +}; - class default_parse_context { - protected: - value* out_; - public: - default_parse_context(value* out) : out_(out) {} - bool set_null() { - *out_ = value(); - return true; - } - bool set_bool(bool b) { - *out_ = value(b); - return true; - } +class default_parse_context { +protected: + value *out_; + +public: + default_parse_context(value *out) : out_(out) { + } + bool set_null() { + *out_ = value(); + return true; + } + bool set_bool(bool b) { + *out_ = value(b); + return true; + } #ifdef PICOJSON_USE_INT64 - bool set_int64(int64_t i) { - *out_ = value(i); - return true; - } + bool set_int64(int64_t i) { + *out_ = value(i); + return true; + } #endif - bool set_number(double f) { - *out_ = value(f); - return true; - } - template bool parse_string(input& in) { - *out_ = value(string_type, false); - return _parse_string(out_->get(), in); - } - bool parse_array_start() { - *out_ = value(array_type, false); - return true; - } - template bool parse_array_item(input& in, size_t) { - array& a = out_->get(); - a.push_back(value()); - default_parse_context ctx(&a.back()); - return _parse(ctx, in); - } - bool parse_array_stop(size_t) { return true; } - bool parse_object_start() { - *out_ = value(object_type, false); - return true; - } - template bool parse_object_item(input& in, const std::string& key) { - object& o = out_->get(); - default_parse_context ctx(&o[key]); - return _parse(ctx, in); + bool set_number(double f) { + *out_ = value(f); + return true; + } + template bool parse_string(input &in) { + *out_ = value(string_type, false); + return _parse_string(out_->get(), in); + } + bool parse_array_start() { + *out_ = value(array_type, false); + return true; + } + template bool parse_array_item(input &in, size_t) { + array &a = out_->get(); + a.push_back(value()); + default_parse_context ctx(&a.back()); + return _parse(ctx, in); + } + bool parse_array_stop(size_t) { + return true; + } + bool parse_object_start() { + *out_ = value(object_type, false); + return true; + } + template bool parse_object_item(input &in, const std::string &key) { + object &o = out_->get(); + default_parse_context ctx(&o[key]); + return _parse(ctx, in); + } + +private: + default_parse_context(const default_parse_context &); + default_parse_context &operator=(const default_parse_context &); +}; + +class null_parse_context { +public: + struct dummy_str { + void push_back(int) { } - private: - default_parse_context(const default_parse_context&); - default_parse_context& operator=(const default_parse_context&); }; - class null_parse_context { - public: - struct dummy_str { - void push_back(int) {} - }; - public: - null_parse_context() {} - bool set_null() { return true; } - bool set_bool(bool) { return true; } +public: + null_parse_context() { + } + bool set_null() { + return true; + } + bool set_bool(bool) { + return true; + } #ifdef PICOJSON_USE_INT64 - bool set_int64(int64_t) { return true; } + bool set_int64(int64_t) { + return true; + } #endif - bool set_number(double) { return true; } - template bool parse_string(input& in) { - dummy_str s; - return _parse_string(s, in); - } - bool parse_array_start() { return true; } - template bool parse_array_item(input& in, size_t) { - return _parse(*this, in); - } - bool parse_array_stop(size_t) { return true; } - bool parse_object_start() { return true; } - template bool parse_object_item(input& in, const std::string&) { - return _parse(*this, in); - } - private: - null_parse_context(const null_parse_context&); - null_parse_context& operator=(const null_parse_context&); - }; + bool set_number(double) { + return true; + } + template bool parse_string(input &in) { + dummy_str s; + return _parse_string(s, in); + } + bool parse_array_start() { + return true; + } + template bool parse_array_item(input &in, size_t) { + return _parse(*this, in); + } + bool parse_array_stop(size_t) { + return true; + } + bool parse_object_start() { + return true; + } + template bool parse_object_item(input &in, const std::string &) { + return _parse(*this, in); + } - // obsolete, use the version below - template inline std::string parse(value& out, Iter& pos, const Iter& last) { - std::string err; - pos = parse(out, pos, last, &err); - return err; - } - - template inline Iter _parse(Context& ctx, const Iter& first, const Iter& last, std::string* err) { - input in(first, last); - if (! _parse(ctx, in) && err != NULL) { - char buf[64]; - SNPRINTF(buf, sizeof(buf), "syntax error at line %d near: ", in.line()); - *err = buf; - while (1) { - int ch = in.getc(); - if (ch == -1 || ch == '\n') { - break; - } else if (ch >= ' ') { - err->push_back( static_cast(ch) ); - } +private: + null_parse_context(const null_parse_context &); + null_parse_context &operator=(const null_parse_context &); +}; + +// obsolete, use the version below +template inline std::string parse(value &out, Iter &pos, const Iter &last) { + std::string err; + pos = parse(out, pos, last, &err); + return err; +} + +template inline Iter _parse(Context &ctx, const Iter &first, const Iter &last, std::string *err) { + input in(first, last); + if (!_parse(ctx, in) && err != NULL) { + char buf[64]; + SNPRINTF(buf, sizeof(buf), "syntax error at line %d near: ", in.line()); + *err = buf; + while (1) { + int ch = in.getc(); + if (ch == -1 || ch == '\n') { + break; + } else if (ch >= ' ') { + err->push_back(static_cast(ch)); } } - return in.cur(); } + return in.cur(); +} - template inline Iter parse(value& out, const Iter& first, const Iter& last, std::string* err) { - default_parse_context ctx(&out); - return _parse(ctx, first, last, err); - } +template inline Iter parse(value &out, const Iter &first, const Iter &last, std::string *err) { + default_parse_context ctx(&out); + return _parse(ctx, first, last, err); +} - inline std::string parse(value& out, const std::string& s) { - std::string err; - parse(out, s.begin(), s.end(), &err); - return err; - } +inline std::string parse(value &out, const std::string &s) { + std::string err; + parse(out, s.begin(), s.end(), &err); + return err; +} - inline std::string parse(value& out, std::istream& is) { - std::string err; - parse(out, std::istreambuf_iterator(is.rdbuf()), - std::istreambuf_iterator(), &err); - return err; - } +inline std::string parse(value &out, std::istream &is) { + std::string err; + parse(out, std::istreambuf_iterator(is.rdbuf()), std::istreambuf_iterator(), &err); + return err; +} - template struct last_error_t { - static std::string s; - }; - template std::string last_error_t::s; +template struct last_error_t { static std::string s; }; +template std::string last_error_t::s; - inline void set_last_error(const std::string& s) { - last_error_t::s = s; - } +inline void set_last_error(const std::string &s) { + last_error_t::s = s; +} - inline const std::string& get_last_error() { - return last_error_t::s; - } +inline const std::string &get_last_error() { + return last_error_t::s; +} - inline bool operator==(const value& x, const value& y) { - if (x.is()) - return y.is(); -#define PICOJSON_CMP(type) \ - if (x.is()) \ - return y.is() && x.get() == y.get() - PICOJSON_CMP(bool); - PICOJSON_CMP(double); - PICOJSON_CMP(std::string); - PICOJSON_CMP(array); - PICOJSON_CMP(object); +inline bool operator==(const value &x, const value &y) { + if (x.is()) + return y.is(); +#define PICOJSON_CMP(type) \ + if (x.is()) \ + return y.is() && x.get() == y.get() + PICOJSON_CMP(bool); + PICOJSON_CMP(double); + PICOJSON_CMP(std::string); + PICOJSON_CMP(array); + PICOJSON_CMP(object); #undef PICOJSON_CMP - PICOJSON_ASSERT(0); + PICOJSON_ASSERT(0); #ifdef _MSC_VER - __assume(0); + __assume(0); #endif - return false; - } + return false; +} - inline bool operator!=(const value& x, const value& y) { - return ! (x == y); - } +inline bool operator!=(const value &x, const value &y) { + return !(x == y); +} } #if !PICOJSON_USE_RVALUE_REFERENCE namespace std { - template<> inline void swap(picojson::value& x, picojson::value& y) - { - x.swap(y); - } +template <> inline void swap(picojson::value &x, picojson::value &y) { + x.swap(y); +} } #endif -inline std::istream& operator>>(std::istream& is, picojson::value& x) -{ +inline std::istream &operator>>(std::istream &is, picojson::value &x) { picojson::set_last_error(std::string()); - const std::string err( picojson::parse(x, is) ); - if (! err.empty()) { + const std::string err(picojson::parse(x, is)); + if (!err.empty()) { picojson::set_last_error(err); is.setstate(std::ios::failbit); } return is; } -inline std::ostream& operator<<(std::ostream& os, const picojson::value& x) -{ +inline std::ostream &operator<<(std::ostream &os, const picojson::value &x) { x.serialize(std::ostream_iterator(os)); return os; } #ifdef _MSC_VER - #pragma warning(pop) +#pragma warning(pop) #endif #endif