Commit 4a71dc6d authored by Kosta's avatar Kosta

reunified `ParseKey`, `ParseString`, and `ParseStringOrKey` again

parent 25f4892a
...@@ -473,7 +473,7 @@ private: ...@@ -473,7 +473,7 @@ private:
if (is.Peek() != '"') if (is.Peek() != '"')
RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell());
ParseKey<parseFlags>(is, handler); ParseString<parseFlags>(is, handler, true);
RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
SkipWhitespace(is); SkipWhitespace(is);
...@@ -578,7 +578,7 @@ private: ...@@ -578,7 +578,7 @@ private:
RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell() - 1); RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell() - 1);
} }
// Helper function to parse four hexidecimal digits in \uXXXX in ParseStringOrKey(). // Helper function to parse four hexidecimal digits in \uXXXX in ParseString().
template<typename InputStream> template<typename InputStream>
unsigned ParseHex4(InputStream& is) { unsigned ParseHex4(InputStream& is) {
unsigned codepoint = 0; unsigned codepoint = 0;
...@@ -618,47 +618,33 @@ private: ...@@ -618,47 +618,33 @@ private:
}; };
// Parse string and generate String event. Different code paths for kParseInsituFlag. // Parse string and generate String event. Different code paths for kParseInsituFlag.
template<unsigned parseFlags, bool isKey, typename InputStream, typename Handler> template<unsigned parseFlags, typename InputStream, typename Handler>
void ParseStringOrKey(InputStream& is, Handler& handler) { void ParseString(InputStream& is, Handler& handler, bool isKey = false) {
internal::StreamLocalCopy<InputStream> copy(is); internal::StreamLocalCopy<InputStream> copy(is);
InputStream& s(copy.s); InputStream& s(copy.s);
const typename TargetEncoding::Ch* str = NULL;
SizeType len = 0;
if (parseFlags & kParseInsituFlag) { if (parseFlags & kParseInsituFlag) {
typename InputStream::Ch *head = s.PutBegin(); typename InputStream::Ch *head = s.PutBegin();
ParseStringToStream<parseFlags, SourceEncoding, SourceEncoding>(s, s); ParseStringToStream<parseFlags, SourceEncoding, SourceEncoding>(s, s);
RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
size_t length = s.PutEnd(head) - 1; size_t length = s.PutEnd(head) - 1;
RAPIDJSON_ASSERT(length <= 0xFFFFFFFF); RAPIDJSON_ASSERT(length <= 0xFFFFFFFF);
if (isKey) { str = (const typename TargetEncoding::Ch*)head;
if (!handler.Key((typename TargetEncoding::Ch*)head, SizeType(length), false)) len = SizeType(length);
RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell());
} else {
if (!handler.String((typename TargetEncoding::Ch*)head, SizeType(length), false))
RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell());
}
} }
else { else {
StackStream stackStream(stack_); StackStream stackStream(stack_);
ParseStringToStream<parseFlags, SourceEncoding, TargetEncoding>(s, stackStream); ParseStringToStream<parseFlags, SourceEncoding, TargetEncoding>(s, stackStream);
RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
if (isKey) { str = stack_.template Pop<typename TargetEncoding::Ch>(stackStream.length_);
if (!handler.Key(stack_.template Pop<typename TargetEncoding::Ch>(stackStream.length_), stackStream.length_ - 1, true)) len = stackStream.length_ - 1;
RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell());
} else {
if (!handler.String(stack_.template Pop<typename TargetEncoding::Ch>(stackStream.length_), stackStream.length_ - 1, true))
RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell());
}
} }
}
template<unsigned parseFlags, typename InputStream, typename Handler> if(!(isKey ? handler.Key(str, len, false) : handler.String(str, len, false)))
void ParseKey(InputStream& is, Handler& handler) { RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell());
return ParseStringOrKey<parseFlags, true>(is, handler);
}
template<unsigned parseFlags, typename InputStream, typename Handler>
void ParseString(InputStream& is, Handler& handler) {
return ParseStringOrKey<parseFlags, false>(is, handler);
} }
// Parse string to an output is // Parse string to an output is
...@@ -1216,7 +1202,7 @@ private: ...@@ -1216,7 +1202,7 @@ private:
} }
case IterativeParsingMemberKeyState: case IterativeParsingMemberKeyState:
ParseKey<parseFlags>(is, handler); ParseString<parseFlags>(is, handler, true);
if (HasParseError()) if (HasParseError())
return IterativeParsingErrorState; return IterativeParsingErrorState;
else else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment