209 lines
6.4 KiB
C++
209 lines
6.4 KiB
C++
// Tencent is pleased to support the open source community by making RapidJSON available.
|
|
//
|
|
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
|
|
//
|
|
// Licensed under the MIT License (the "License"); you may not use this file except
|
|
// in compliance with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://opensource.org/licenses/MIT
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software distributed
|
|
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
// specific language governing permissions and limitations under the License.
|
|
|
|
#ifndef RAPIDJSON_POINTER_H_
|
|
#define RAPIDJSON_POINTER_H_
|
|
|
|
#include "document.h"
|
|
|
|
RAPIDJSON_NAMESPACE_BEGIN
|
|
|
|
template <typename ValueType, typename Allocator = CrtAllocator>
|
|
class GenericPointer {
|
|
public:
|
|
typedef typename ValueType::EncodingType EncodingType;
|
|
typedef typename EncodingType::Ch Ch;
|
|
|
|
struct Token {
|
|
const typename Ch* name;
|
|
SizeType length;
|
|
SizeType index; //!< A valid index if not equal to kInvalidIndex.
|
|
};
|
|
|
|
GenericPointer(const Ch* source, Allocator* allocator = 0)
|
|
: allocator_(allocator),
|
|
ownAllocator_(),
|
|
nameBuffer_(),
|
|
tokens_(),
|
|
tokenCount_(),
|
|
valid_(true)
|
|
{
|
|
Parse(source, internal::StrLen(source));
|
|
}
|
|
|
|
GenericPointer(const Ch* source, size_t length, Allocator* allocator = 0)
|
|
: allocator_(allocator),
|
|
ownAllocator_(),
|
|
nameBuffer_(),
|
|
tokens_(),
|
|
tokenCount_(),
|
|
valid_(true)
|
|
{
|
|
Parse(source, length);
|
|
}
|
|
|
|
GenericPointer(const Token* tokens, size_t tokenCount) :
|
|
: allocator_(),
|
|
ownAllocator_(),
|
|
nameBuffer_(),
|
|
tokens_(tokens),
|
|
tokenCount_(tokenCount),
|
|
valid_(true)
|
|
{
|
|
}
|
|
|
|
~GenericPointer() {
|
|
if (nameBuffer_) {
|
|
Allocator::Free(nameBuffer_);
|
|
Allocator::Free(tokens_);
|
|
}
|
|
RAPIDJSON_DELETE(ownAllocator_);
|
|
}
|
|
|
|
bool IsValid() const { return valid_; }
|
|
|
|
const Token* GetTokens() const { return tokens_; }
|
|
|
|
size_t GetTokenCount() const { return tokenCount_; }
|
|
|
|
template<typename OutputStream>
|
|
void Stringify(OutputStream& os) const {
|
|
RAPIDJSON_ASSERT(IsValid());
|
|
for (Token *t = tokens_; t != tokens_ + tokenCount_; ++t) {
|
|
os.Put('/');
|
|
for (size_t j = 0; j < t->length; j++) {
|
|
Ch c = t->name[j];
|
|
if (c == '~') { os.Put('~'); os.Put('0'); }
|
|
else if (c == '/') { os.Put('~'); os.Put('1'); }
|
|
else os.Put(c);
|
|
}
|
|
}
|
|
}
|
|
|
|
ValueType* Get(ValueType& root) const;
|
|
const ValueType* Get(const ValueType& root) const {
|
|
return Get(const_cast<ValueType&>(root));
|
|
}
|
|
|
|
ValueType* Get(ValueType& root, const ValueType& defaultValue) const;
|
|
const ValueType* Get(const ValueType& root, const ValueType& defaultValue) const;
|
|
|
|
// Move semantics, create parents if non-exist
|
|
void Set(ValueType& root, ValueType& value) const;
|
|
|
|
// Create parents if non-exist
|
|
void Swap(ValueType& root, ValueType& value) const;
|
|
|
|
static const size_t kDefaultTokenCapacity = 4;
|
|
static const SizeType kInvalidIndex = ~SizeType(0);
|
|
|
|
private:
|
|
void Parse(const Ch* source, size_t length) {
|
|
// Create own allocator if user did not supply.
|
|
if (!allocator_)
|
|
ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
|
|
|
|
// Create a buffer as same size of source
|
|
RAPIDJSON_ASSERT(nameBuffer_ == 0);
|
|
nameBuffer_ = (Ch*)allocator_->Malloc(length);
|
|
|
|
RAPIDJSON_ASSERT(tokens_ == 0);
|
|
tokens_ = (Token*)allocator_->Malloc(length * sizeof(Token)); // Maximum possible tokens in the source
|
|
|
|
tokenCount_ = 0;
|
|
Ch* name = nameBuffer_;
|
|
|
|
for (size_t i = 0; i < length;) {
|
|
if (source[i++] != '/') // Consumes '/'
|
|
goto error;
|
|
|
|
Token& token = tokens_[tokenCount_++];
|
|
token.name = name;
|
|
bool isNumber = true;
|
|
|
|
while (i < length && source[i] != '/') {
|
|
Ch c = source[i++];
|
|
|
|
// Escaping "~0" -> '~', "~1" -> '/'
|
|
if (c == '~') {
|
|
if (i < length) {
|
|
c = source[i++];
|
|
if (c == '0') c = '~';
|
|
else if (c == '1') c = '/';
|
|
else goto error;
|
|
}
|
|
else
|
|
goto error;
|
|
}
|
|
|
|
// First check for index: all of characters are digit
|
|
if (c < '0' || c > '9')
|
|
isNumber = false;
|
|
|
|
*name++ = c;
|
|
}
|
|
token.length = name - token.name;
|
|
*name++ = '\0'; // Null terminator
|
|
|
|
// Second check for index: more than one digit cannot have leading zero
|
|
if (isNumber && token.length > 1 && token.name[0] == '0')
|
|
isNumber = false;
|
|
|
|
// String to SizeType conversion
|
|
SizeType n = 0;
|
|
if (isNumber) {
|
|
for (size_t j = 0; j < token.length; j++) {
|
|
SizeType m = n * 10 + static_cast<SizeType>(token.name[j] - '0');
|
|
if (m < n) { // overflow detection
|
|
isNumber = false;
|
|
break;
|
|
}
|
|
n = m;
|
|
}
|
|
}
|
|
|
|
token.index = isNumber ? n : kInvalidIndex;
|
|
}
|
|
|
|
RAPIDJSON_ASSERT(name <= nameBuffer_ + length); // Should not overflow buffer
|
|
tokens_ = (Token*)allocator_->Realloc(tokens_, length * sizeof(Token), tokenCount_ * sizeof(Token)); // Shrink tokens_
|
|
return;
|
|
|
|
error:
|
|
Allocator::Free(nameBuffer_);
|
|
Allocator::Free(tokens_);
|
|
nameBuffer_ = 0;
|
|
tokens_ = 0;
|
|
tokenCount_ = 0;
|
|
valid_ = false;
|
|
return;
|
|
}
|
|
|
|
GenericPointer(const GenericPointer& rhs);
|
|
GenericPointer& operator=(const GenericPointer& rhs);
|
|
|
|
Allocator* allocator_;
|
|
Allocator* ownAllocator_;
|
|
Ch* nameBuffer_;
|
|
Token* tokens_;
|
|
size_t tokenCount_;
|
|
bool valid_;
|
|
};
|
|
|
|
typedef GenericPointer<Value> Pointer;
|
|
|
|
RAPIDJSON_NAMESPACE_END
|
|
|
|
#endif // RAPIDJSON_POINTER_H_
|