From e70494bc00a60e47be06cb625889b09fe63342ea Mon Sep 17 00:00:00 2001 From: Kosta Date: Thu, 4 Sep 2014 17:29:52 +0200 Subject: [PATCH] fix for `Reader::ParseString()` implementation plus some minor code cleanups and additions --- example/capitalize/capitalize.cpp | 3 +-- example/simplereader/simplereader.cpp | 7 +++++-- include/rapidjson/prettywriter.h | 1 + include/rapidjson/reader.h | 14 ++++++-------- include/rapidjson/writer.h | 1 + 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/example/capitalize/capitalize.cpp b/example/capitalize/capitalize.cpp index dd94d24..230bc28 100644 --- a/example/capitalize/capitalize.cpp +++ b/example/capitalize/capitalize.cpp @@ -14,7 +14,7 @@ using namespace rapidjson; template -struct CapitalizeFilter { +struct CapitalizeFilter : public BaseReaderHandler, OutputHandler> { CapitalizeFilter(OutputHandler& out) : out_(out), buffer_() {} bool Null() { return out_.Null(); } @@ -31,7 +31,6 @@ struct CapitalizeFilter { return out_.String(&buffer_.front(), length, true); // true = output handler need to copy the string } bool StartObject() { return out_.StartObject(); } - bool Key(const char* str, SizeType length, bool copy) { return String(str, length, copy); } bool EndObject(SizeType memberCount) { return out_.EndObject(memberCount); } bool StartArray() { return out_.StartArray(); } bool EndArray(SizeType elementCount) { return out_.EndArray(elementCount); } diff --git a/example/simplereader/simplereader.cpp b/example/simplereader/simplereader.cpp index 83b3f1d..b47d9b2 100644 --- a/example/simplereader/simplereader.cpp +++ b/example/simplereader/simplereader.cpp @@ -4,7 +4,7 @@ using namespace rapidjson; using namespace std; -struct MyHandler { +struct MyHandler : public BaseReaderHandler, MyHandler> { bool Null() { cout << "Null()" << endl; return true; } bool Bool(bool b) { cout << "Bool(" << boolalpha << b << ")" << endl; return true; } bool Int(int i) { cout << "Int(" << i << ")" << endl; return true; } @@ -17,7 +17,10 @@ struct MyHandler { return true; } bool StartObject() { cout << "StartObject()" << endl; return true; } - bool Key(const char* str, SizeType length, bool copy) { return String(str, length, copy); } + bool Key(const char* str, SizeType length, bool copy) { + cout << "Key(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl; + return true; + } bool EndObject(SizeType memberCount) { cout << "EndObject(" << memberCount << ")" << endl; return true; } bool StartArray() { cout << "StartArray()" << endl; return true; } bool EndArray(SizeType elementCount) { cout << "EndArray(" << elementCount << ")" << endl; return true; } diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index 2fc9b75..4eac8d7 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -137,6 +137,7 @@ public: //! Simpler but slower overload. bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } //@} protected: diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index d93c8c2..e853bea 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -623,27 +623,25 @@ private: internal::StreamLocalCopy copy(is); InputStream& s(copy.s); - const typename TargetEncoding::Ch* str = NULL; - SizeType len = 0; - + bool success = false; if (parseFlags & kParseInsituFlag) { typename InputStream::Ch *head = s.PutBegin(); ParseStringToStream(s, s); RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; size_t length = s.PutEnd(head) - 1; RAPIDJSON_ASSERT(length <= 0xFFFFFFFF); - str = (const typename TargetEncoding::Ch*)head; - len = SizeType(length); + const typename TargetEncoding::Ch* const str = (const typename TargetEncoding::Ch*)head; + success = (isKey ? handler.Key(str, SizeType(length), false) : handler.String(str, SizeType(length), false)); } else { StackStream stackStream(stack_); ParseStringToStream(s, stackStream); RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; - str = stack_.template Pop(stackStream.length_); - len = stackStream.length_ - 1; + const typename TargetEncoding::Ch* const str = stack_.template Pop(stackStream.length_); + success = (isKey ? handler.Key(str, stackStream.length_ - 1, false) : handler.String(str, stackStream.length_ - 1, false)); } - if(!(isKey ? handler.Key(str, len, false) : handler.String(str, len, false))) + if(!success) RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell()); } diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 2f46b66..fb6601e 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -167,6 +167,7 @@ public: //! Simpler but slower overload. bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } //@}