15 #ifndef RAPIDJSON_POINTER_H_
16 #define RAPIDJSON_POINTER_H_
23 RAPIDJSON_DIAG_OFF(
switch-
enum)
28 RAPIDJSON_DIAG_OFF(4512)
80 template <
typename ValueType,
typename Allocator = CrtAllocator>
84 typedef typename ValueType::Ch
Ch;
120 #if RAPIDJSON_HAS_STDSTRING
175 Allocator::Free(tokens_);
184 Allocator::Free(tokens_);
186 tokenCount_ = rhs.tokenCount_;
187 parseErrorOffset_ = rhs.parseErrorOffset_;
188 parseErrorCode_ = rhs.parseErrorCode_;
193 tokens_ = rhs.tokens_;
214 Ch *p = r.CopyFromRaw(*
this, 1, token.
length + 1);
215 std::memcpy(p, token.
name, (token.
length + 1) *
sizeof(
Ch));
216 r.tokens_[tokenCount_].
name = p;
240 template <
typename T>
246 #if RAPIDJSON_HAS_STDSTRING
268 buffer[length] =
'\0';
270 if (
sizeof(
Ch) == 1) {
271 Token token = {
reinterpret_cast<Ch*
>(buffer), length, index };
276 for (
size_t i = 0; i <= length; i++)
278 Token token = { name, length, index };
290 if (token.IsString())
306 size_t GetParseErrorOffset()
const {
return parseErrorOffset_; }
314 Allocator& GetAllocator() {
return *allocator_; }
320 const Token* GetTokens()
const {
return tokens_; }
323 size_t GetTokenCount()
const {
return tokenCount_; }
335 if (!IsValid() || !rhs.IsValid() || tokenCount_ != rhs.tokenCount_)
338 for (
size_t i = 0; i < tokenCount_; i++) {
339 if (tokens_[i].index != rhs.tokens_[i].
index ||
340 tokens_[i].length != rhs.tokens_[i].
length ||
341 (tokens_[i].length != 0 && std::memcmp(tokens_[i].name, rhs.tokens_[i].
name,
sizeof(
Ch)* tokens_[i].length) != 0))
366 template<
typename OutputStream>
367 bool Stringify(OutputStream& os)
const {
368 return Stringify<false, OutputStream>(os);
376 template<
typename OutputStream>
377 bool StringifyUriFragment(OutputStream& os)
const {
378 return Stringify<true, OutputStream>(os);
401 ValueType& Create(ValueType& root,
typename ValueType::AllocatorType&
allocator,
bool* alreadyExist = 0)
const {
403 ValueType* v = &root;
405 for (
const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) {
406 if (v->IsArray() && t->name[0] ==
'-' && t->length == 1) {
407 v->PushBack(ValueType().Move(),
allocator);
408 v = &((*v)[v->Size() - 1]);
417 if (!v->IsArray() && !v->IsObject())
422 if (t->index >= v->Size()) {
424 while (t->index >= v->Size())
425 v->PushBack(ValueType().Move(),
allocator);
428 v = &((*v)[t->index]);
432 if (m == v->MemberEnd()) {
433 v->AddMember(ValueType(t->name, t->length,
allocator).Move(), ValueType().Move(),
allocator);
434 v = &(--v->MemberEnd())->
value;
444 *alreadyExist = exist;
455 template <
typename stackAllocator>
479 ValueType* Get(ValueType& root,
size_t* unresolvedTokenIndex = 0)
const {
481 ValueType* v = &root;
482 for (
const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) {
483 switch (v->GetType()) {
487 if (m == v->MemberEnd())
495 v = &((*v)[t->index]);
502 if (unresolvedTokenIndex)
503 *unresolvedTokenIndex =
static_cast<size_t>(t - tokens_);
514 const ValueType* Get(
const ValueType& root,
size_t* unresolvedTokenIndex = 0)
const {
515 return Get(
const_cast<ValueType&
>(root), unresolvedTokenIndex);
533 ValueType& GetWithDefault(ValueType& root,
const ValueType&
defaultValue,
typename ValueType::AllocatorType&
allocator)
const {
540 ValueType& GetWithDefault(ValueType& root,
const Ch*
defaultValue,
typename ValueType::AllocatorType&
allocator)
const {
546 #if RAPIDJSON_HAS_STDSTRING
548 ValueType& GetWithDefault(ValueType& root,
const std::basic_string<Ch>&
defaultValue,
typename ValueType::AllocatorType&
allocator)
const {
559 template <
typename T>
561 GetWithDefault(ValueType& root, T
defaultValue,
typename ValueType::AllocatorType&
allocator)
const {
566 template <
typename stackAllocator>
572 template <
typename stackAllocator>
577 #if RAPIDJSON_HAS_STDSTRING
579 template <
typename stackAllocator>
589 template <
typename T,
typename stackAllocator>
610 ValueType&
Set(ValueType& root, ValueType&
value,
typename ValueType::AllocatorType&
allocator)
const {
615 ValueType&
Set(ValueType& root,
const ValueType&
value,
typename ValueType::AllocatorType&
allocator)
const {
624 #if RAPIDJSON_HAS_STDSTRING
626 ValueType&
Set(ValueType& root,
const std::basic_string<Ch>&
value,
typename ValueType::AllocatorType&
allocator)
const {
635 template <
typename T>
642 template <
typename stackAllocator>
648 template <
typename stackAllocator>
654 template <
typename stackAllocator>
659 #if RAPIDJSON_HAS_STDSTRING
661 template <
typename stackAllocator>
671 template <
typename T,
typename stackAllocator>
692 ValueType&
Swap(ValueType& root, ValueType&
value,
typename ValueType::AllocatorType&
allocator)
const {
697 template <
typename stackAllocator>
713 if (tokenCount_ == 0)
716 ValueType* v = &root;
717 const Token* last = tokens_ + (tokenCount_ - 1);
718 for (
const Token *t = tokens_; t != last; ++t) {
719 switch (v->GetType()) {
723 if (m == v->MemberEnd())
731 v = &((*v)[t->index]);
738 switch (v->GetType()) {
744 v->Erase(v->Begin() + last->
index);
759 Ch* CopyFromRaw(
const GenericPointer& rhs,
size_t extraToken = 0,
size_t extraNameBufferSize = 0) {
763 size_t nameBufferSize = rhs.tokenCount_;
764 for (Token *t = rhs.tokens_; t != rhs.tokens_ + rhs.tokenCount_; ++t)
765 nameBufferSize += t->length;
767 tokenCount_ = rhs.tokenCount_ + extraToken;
768 tokens_ =
static_cast<Token *
>(allocator_->Malloc(tokenCount_ *
sizeof(Token) + (nameBufferSize + extraNameBufferSize) *
sizeof(
Ch)));
769 nameBuffer_ =
reinterpret_cast<Ch *
>(tokens_ + tokenCount_);
770 if (rhs.tokenCount_ > 0) {
771 std::memcpy(tokens_, rhs.tokens_, rhs.tokenCount_ *
sizeof(Token));
773 if (nameBufferSize > 0) {
774 std::memcpy(nameBuffer_, rhs.nameBuffer_, nameBufferSize *
sizeof(
Ch));
778 std::ptrdiff_t diff = nameBuffer_ - rhs.nameBuffer_;
779 for (Token *t = tokens_; t != tokens_ + rhs.tokenCount_; ++t)
782 return nameBuffer_ + nameBufferSize;
790 bool NeedPercentEncode(
Ch c)
const {
791 return !((
c >=
'0' &&
c <=
'9') || (
c >=
'A' &&
c <=
'Z') || (
c >=
'a' &&
c <=
'z') ||
c ==
'-' ||
c ==
'.' ||
c ==
'_' ||
c ==
'~');
795 #ifndef __clang__ // -Wdocumentation
802 void Parse(
const Ch*
source,
size_t length) {
817 Token* token = tokens_ =
static_cast<Token *
>(allocator_->Malloc(tokenCount_ *
sizeof(Token) + length *
sizeof(
Ch)));
818 Ch* name = nameBuffer_ =
reinterpret_cast<Ch *
>(tokens_ + tokenCount_);
822 bool uriFragment =
false;
828 if (i != length &&
source[i] !=
'/') {
838 bool isNumber =
true;
840 while (i < length &&
source[i] !=
'/') {
847 Ch* begin = os.PutBegin();
852 size_t len = os.PutEnd(begin);
863 else if (NeedPercentEncode(
c)) {
875 if (
c ==
'0')
c =
'~';
876 else if (
c ==
'1')
c =
'/';
890 if (c < '0' || c >
'9')
895 token->length =
static_cast<SizeType>(name - token->name);
896 if (token->length == 0)
901 if (isNumber && token->length > 1 && token->name[0] ==
'0')
907 for (
size_t j = 0; j < token->length; j++) {
926 Allocator::Free(tokens_);
930 parseErrorOffset_ = i;
940 template<
bool uriFragment,
typename OutputStream>
941 bool Stringify(OutputStream& os)
const {
947 for (Token *t = tokens_; t != tokens_ + tokenCount_; ++t) {
949 for (
size_t j = 0; j < t->length; j++) {
959 else if (uriFragment && NeedPercentEncode(
c)) {
962 PercentEncodeStream<OutputStream> target(os);
980 class PercentDecodeStream {
982 typedef typename ValueType::Ch
Ch;
989 PercentDecodeStream(
const Ch*
source,
const Ch* end) : src_(
source), head_(
source), end_(end), valid_(true) {}
992 if (*src_ !=
'%' || src_ + 3 > end_) {
998 for (
int j = 0; j < 2; j++) {
999 c =
static_cast<Ch>(
c << 4);
1001 if (h >=
'0' && h <=
'9')
c =
static_cast<Ch>(
c + h -
'0');
1002 else if (h >=
'A' && h <=
'F')
c =
static_cast<Ch>(
c + h -
'A' + 10);
1003 else if (h >=
'a' && h <=
'f')
c =
static_cast<Ch>(
c + h -
'a' + 10);
1013 size_t Tell()
const {
return static_cast<size_t>(src_ - head_); }
1014 bool IsValid()
const {
return valid_; }
1024 template <
typename OutputStream>
1025 class PercentEncodeStream {
1027 PercentEncodeStream(OutputStream& os) : os_(os) {}
1029 unsigned char u =
static_cast<unsigned char>(
c);
1030 static const char hexDigits[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F' };
1032 os_.Put(hexDigits[u >> 4]);
1033 os_.Put(hexDigits[u & 15]);
1044 size_t parseErrorOffset_;
1056 template <
typename T>
1061 template <
typename T,
typename CharType,
size_t N>
1068 template <
typename DocumentType>
1070 return pointer.Create(document);
1073 template <
typename DocumentType,
typename CharType,
size_t N>
1080 template <
typename T>
1082 return pointer.Get(root, unresolvedTokenIndex);
1085 template <
typename T>
1087 return pointer.Get(root, unresolvedTokenIndex);
1090 template <
typename T,
typename CharType,
size_t N>
1095 template <
typename T,
typename CharType,
size_t N>
1102 template <
typename T>
1107 template <
typename T>
1112 #if RAPIDJSON_HAS_STDSTRING
1113 template <
typename T>
1119 template <
typename T,
typename T2>
1125 template <
typename T,
typename CharType,
size_t N>
1130 template <
typename T,
typename CharType,
size_t N>
1135 #if RAPIDJSON_HAS_STDSTRING
1136 template <
typename T,
typename CharType,
size_t N>
1142 template <
typename T,
typename CharType,
size_t N,
typename T2>
1150 template <
typename DocumentType>
1155 template <
typename DocumentType>
1160 #if RAPIDJSON_HAS_STDSTRING
1161 template <
typename DocumentType>
1167 template <
typename DocumentType,
typename T2>
1173 template <
typename DocumentType,
typename CharType,
size_t N>
1178 template <
typename DocumentType,
typename CharType,
size_t N>
1183 #if RAPIDJSON_HAS_STDSTRING
1184 template <
typename DocumentType,
typename CharType,
size_t N>
1190 template <
typename DocumentType,
typename CharType,
size_t N,
typename T2>
1198 template <
typename T>
1203 template <
typename T>
1208 template <
typename T>
1213 #if RAPIDJSON_HAS_STDSTRING
1214 template <
typename T>
1220 template <
typename T,
typename T2>
1226 template <
typename T,
typename CharType,
size_t N>
1231 template <
typename T,
typename CharType,
size_t N>
1236 template <
typename T,
typename CharType,
size_t N>
1241 #if RAPIDJSON_HAS_STDSTRING
1242 template <
typename T,
typename CharType,
size_t N>
1243 typename T::ValueType&
SetValueByPointer(T& root,
const CharType(&
source)[N],
const std::basic_string<typename T::Ch>&
value,
typename T::AllocatorType&
a) {
1248 template <
typename T,
typename CharType,
size_t N,
typename T2>
1256 template <
typename DocumentType>
1261 template <
typename DocumentType>
1266 template <
typename DocumentType>
1271 #if RAPIDJSON_HAS_STDSTRING
1272 template <
typename DocumentType>
1278 template <
typename DocumentType,
typename T2>
1284 template <
typename DocumentType,
typename CharType,
size_t N>
1289 template <
typename DocumentType,
typename CharType,
size_t N>
1294 template <
typename DocumentType,
typename CharType,
size_t N>
1299 #if RAPIDJSON_HAS_STDSTRING
1300 template <
typename DocumentType,
typename CharType,
size_t N>
1301 typename DocumentType::ValueType&
SetValueByPointer(DocumentType& document,
const CharType(&
source)[N],
const std::basic_string<typename DocumentType::Ch>&
value) {
1306 template <
typename DocumentType,
typename CharType,
size_t N,
typename T2>
1314 template <
typename T>
1319 template <
typename T,
typename CharType,
size_t N>
1324 template <
typename DocumentType>
1329 template <
typename DocumentType,
typename CharType,
size_t N>
1336 template <
typename T>
1341 template <
typename T,
typename CharType,
size_t N>
1358 #endif // RAPIDJSON_POINTER_H_