Commit e7bcedb4 authored by miloyip's avatar miloyip

Simplify code

parent 1135ef66
...@@ -224,40 +224,27 @@ public: ...@@ -224,40 +224,27 @@ public:
ValueType& GetWithDefault(ValueType& root, const ValueType& defaultValue, typename ValueType::AllocatorType& allocator) const { ValueType& GetWithDefault(ValueType& root, const ValueType& defaultValue, typename ValueType::AllocatorType& allocator) const {
bool alreadyExist; bool alreadyExist;
Value& v = Create(root, allocator, &alreadyExist); Value& v = Create(root, allocator, &alreadyExist);
if (!alreadyExist) { return alreadyExist ? v : v.CopyFrom(defaultValue, allocator);
Value clone(defaultValue, allocator);
v = clone;
}
return v;
} }
ValueType& GetWithDefault(ValueType& root, const Ch* defaultValue, typename ValueType::AllocatorType& allocator) const { ValueType& GetWithDefault(ValueType& root, const Ch* defaultValue, typename ValueType::AllocatorType& allocator) const {
bool alreadyExist; bool alreadyExist;
Value& v = Create(root, allocator, &alreadyExist); Value& v = Create(root, allocator, &alreadyExist);
if (!alreadyExist) { return alreadyExist ? v : v.SetString(defaultValue, allocator);
Value clone(defaultValue, allocator); // This has overhead, so do it inside if.
v = clone;
}
return v;
} }
#if RAPIDJSON_HAS_STDSTRING #if RAPIDJSON_HAS_STDSTRING
ValueType& GetWithDefault(ValueType& root, const std::basic_string<Ch>& defaultValue, typename ValueType::AllocatorType& allocator) const { ValueType& GetWithDefault(ValueType& root, const std::basic_string<Ch>& defaultValue, typename ValueType::AllocatorType& allocator) const {
bool alreadyExist; bool alreadyExist;
Value& v = Create(root, allocator, &alreadyExist); Value& v = Create(root, allocator, &alreadyExist);
if (!alreadyExist) { return alreadyExist ? v : v.SetString(defaultValue, allocator);
Value clone(defaultValue, allocator); // This has overhead, so do it inside if.
v = clone;
}
return v;
} }
#endif #endif
template <typename T> template <typename T>
RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (ValueType&)) RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (ValueType&))
GetWithDefault(ValueType& root, T defaultValue, typename ValueType::AllocatorType& allocator) const { GetWithDefault(ValueType& root, T defaultValue, typename ValueType::AllocatorType& allocator) const {
ValueType v(defaultValue); return GetWithDefault(root, ValueType(defaultValue).Move(), allocator);
return GetWithDefault(root, v, allocator);
} }
template <typename stackAllocator> template <typename stackAllocator>
...@@ -294,22 +281,19 @@ public: ...@@ -294,22 +281,19 @@ public:
} }
ValueType& Set(ValueType& root, const Ch* value, typename ValueType::AllocatorType& allocator) const { ValueType& Set(ValueType& root, const Ch* value, typename ValueType::AllocatorType& allocator) const {
ValueType v(value, allocator); return Create(root, allocator) = ValueType(value, allocator).Move();
return Create(root, allocator) = v;
} }
#if RAPIDJSON_HAS_STDSTRING #if RAPIDJSON_HAS_STDSTRING
ValueType& Set(ValueType& root, const std::basic_string<Ch>& value, typename ValueType::AllocatorType& allocator) const { ValueType& Set(ValueType& root, const std::basic_string<Ch>& value, typename ValueType::AllocatorType& allocator) const {
ValueType v(value, allocator); return Create(root, allocator) = ValueType(value, allocator).Move();
return Create(root, allocator) = v;
} }
#endif #endif
template <typename T> template <typename T>
RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (ValueType&)) RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (ValueType&))
Set(ValueType& root, T value, typename ValueType::AllocatorType& allocator) const { Set(ValueType& root, T value, typename ValueType::AllocatorType& allocator) const {
ValueType v(value); return Create(root, allocator) = ValueType(value).Move();
return Create(root, allocator) = v;
} }
template <typename stackAllocator> template <typename stackAllocator>
...@@ -363,20 +347,19 @@ private: ...@@ -363,20 +347,19 @@ private:
\note Source cannot be JSON String Representation of JSON Pointer, e.g. In "/\u0000", \u0000 will not be unescaped. \note Source cannot be JSON String Representation of JSON Pointer, e.g. In "/\u0000", \u0000 will not be unescaped.
*/ */
void Parse(const Ch* source, size_t length) { void Parse(const Ch* source, size_t length) {
RAPIDJSON_ASSERT(source != NULL);
RAPIDJSON_ASSERT(nameBuffer_ == 0);
RAPIDJSON_ASSERT(tokens_ == 0);
// Create own allocator if user did not supply. // Create own allocator if user did not supply.
if (!allocator_) if (!allocator_)
ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
// Create a buffer as same size of source // Create a buffer as same size of source
RAPIDJSON_ASSERT(nameBuffer_ == 0);
nameBuffer_ = (Ch*)allocator_->Malloc(length * sizeof(Ch)); nameBuffer_ = (Ch*)allocator_->Malloc(length * sizeof(Ch));
RAPIDJSON_ASSERT(tokens_ == 0);
tokens_ = (Token*)allocator_->Malloc(length * sizeof(Token)); // Maximum possible tokens in the source tokens_ = (Token*)allocator_->Malloc(length * sizeof(Token)); // Maximum possible tokens in the source
tokenCount_ = 0; tokenCount_ = 0;
Ch* name = nameBuffer_; Ch* name = nameBuffer_;
size_t i = 0; size_t i = 0;
// Detect if it is a URI fragment // Detect if it is a URI fragment
...@@ -401,7 +384,6 @@ private: ...@@ -401,7 +384,6 @@ private:
while (i < length && source[i] != '/') { while (i < length && source[i] != '/') {
Ch c = source[i]; Ch c = source[i];
if (uriFragment) { if (uriFragment) {
// Decoding percent-encoding for URI fragment // Decoding percent-encoding for URI fragment
if (c == '%') { if (c == '%') {
......
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