15#ifndef RAPIDJSON_POINTER_H_
16#define RAPIDJSON_POINTER_H_
23RAPIDJSON_DIAG_OFF(
switch-
enum)
24#elif defined(_MSC_VER)
26RAPIDJSON_DIAG_OFF(4512)
78template <
typename ValueType,
typename Allocator = CrtAllocator>
82 typedef typename ValueType::Ch
Ch;
118#if RAPIDJSON_HAS_STDSTRING
217 Ch *p = r.CopyFromRaw(*
this, 1, token.length + 1);
218 std::memcpy(p, token.name, (token.length + 1) *
sizeof(
Ch));
243 template <
typename T>
249#if RAPIDJSON_HAS_STDSTRING
271 buffer[length] =
'\0';
273 if (
sizeof(
Ch) == 1) {
274 Token token = {
reinterpret_cast<Ch*
>(buffer), length, index };
279 for (
size_t i = 0;
i <= length;
i++)
280 name[i] =
static_cast<Ch>(buffer[i]);
293 if (token.IsString())
317 Allocator& GetAllocator() {
return *
allocator_; }
326 size_t GetTokenCount()
const {
return tokenCount_; }
369 template<
typename OutputStream>
379 template<
typename OutputStream>
380 bool StringifyUriFragment(OutputStream& os)
const {
404 ValueType& Create(ValueType& root,
typename ValueType::AllocatorType&
allocator,
bool* alreadyExist = 0)
const {
406 ValueType* v = &root;
409 if (v->IsArray() &&
t->name[0] ==
'-' &&
t->length == 1) {
410 v->PushBack(ValueType().Move(),
allocator);
411 v = &((*v)[v->Size() - 1]);
420 if (!v->IsArray() && !v->IsObject())
425 if (
t->index >= v->Size()) {
427 while (
t->index >= v->Size())
428 v->PushBack(ValueType().Move(),
allocator);
431 v = &((*v)[
t->index]);
434 typename ValueType::MemberIterator m = v->FindMember(GenericStringRef<Ch>(
t->name,
t->length));
435 if (m == v->MemberEnd()) {
437 v = &(--v->MemberEnd())->value;
447 *alreadyExist = exist;
458 template <
typename stackAllocator>
459 ValueType& Create(GenericDocument<EncodingType, typename ValueType::AllocatorType, stackAllocator>&
document,
bool* alreadyExist = 0)
const {
482 ValueType* Get(ValueType& root,
size_t* unresolvedTokenIndex = 0)
const {
484 ValueType* v = &root;
486 switch (v->GetType()) {
489 typename ValueType::MemberIterator m = v->FindMember(GenericStringRef<Ch>(
t->name,
t->length));
490 if (m == v->MemberEnd())
498 v = &((*v)[
t->index]);
505 if (unresolvedTokenIndex)
506 *unresolvedTokenIndex =
static_cast<size_t>(
t -
tokens_);
517 const ValueType* Get(
const ValueType& root,
size_t* unresolvedTokenIndex = 0)
const {
518 return Get(
const_cast<ValueType&
>(root), unresolvedTokenIndex);
536 ValueType& GetWithDefault(ValueType& root,
const ValueType&
defaultValue,
typename ValueType::AllocatorType&
allocator)
const {
538 ValueType& v = Create(root,
allocator, &alreadyExist);
543 ValueType& GetWithDefault(ValueType& root,
const Ch*
defaultValue,
typename ValueType::AllocatorType&
allocator)
const {
545 ValueType& v = Create(root,
allocator, &alreadyExist);
549#if RAPIDJSON_HAS_STDSTRING
551 ValueType& GetWithDefault(ValueType& root,
const std::basic_string<Ch>&
defaultValue,
typename ValueType::AllocatorType&
allocator)
const {
553 ValueType& v = Create(root,
allocator, &alreadyExist);
562 template <
typename T>
569 template <
typename stackAllocator>
570 ValueType& GetWithDefault(GenericDocument<EncodingType, typename ValueType::AllocatorType, stackAllocator>&
document,
const ValueType&
defaultValue)
const {
575 template <
typename stackAllocator>
576 ValueType& GetWithDefault(GenericDocument<EncodingType, typename ValueType::AllocatorType, stackAllocator>&
document,
const Ch*
defaultValue)
const {
580#if RAPIDJSON_HAS_STDSTRING
582 template <
typename stackAllocator>
583 ValueType& GetWithDefault(GenericDocument<EncodingType, typename ValueType::AllocatorType, stackAllocator>&
document,
const std::basic_string<Ch>&
defaultValue)
const {
592 template <
typename T,
typename stackAllocator>
613 ValueType&
Set(ValueType& root, ValueType&
value,
typename ValueType::AllocatorType&
allocator)
const {
618 ValueType&
Set(ValueType& root,
const ValueType&
value,
typename ValueType::AllocatorType&
allocator)
const {
627#if RAPIDJSON_HAS_STDSTRING
629 ValueType&
Set(ValueType& root,
const std::basic_string<Ch>&
value,
typename ValueType::AllocatorType&
allocator)
const {
638 template <
typename T>
645 template <
typename stackAllocator>
651 template <
typename stackAllocator>
657 template <
typename stackAllocator>
662#if RAPIDJSON_HAS_STDSTRING
664 template <
typename stackAllocator>
674 template <
typename T,
typename stackAllocator>
695 ValueType&
Swap(ValueType& root, ValueType&
value,
typename ValueType::AllocatorType&
allocator)
const {
700 template <
typename stackAllocator>
719 ValueType* v = &root;
721 for (
const Token *t =
tokens_; t != last; ++t) {
722 switch (v->GetType()) {
726 if (m == v->MemberEnd())
734 v = &((*v)[t->index]);
741 switch (v->GetType()) {
747 v->Erase(v->Begin() + last->index);
768 nameBufferSize += t->length;
776 if (nameBufferSize > 0) {
794 return !((c >=
'0' && c <=
'9') || (c >=
'A' && c <=
'Z') || (c >=
'a' && c <=
'z') || c ==
'-' || c ==
'.' || c ==
'_' || c ==
'~');
825 bool uriFragment =
false;
831 if (i != length &&
source[i] !=
'/') {
841 bool isNumber =
true;
843 while (i < length &&
source[i] !=
'/') {
855 size_t len = os.
PutEnd(begin);
878 if (c ==
'0') c =
'~';
879 else if (c ==
'1') c =
'/';
893 if (c <
'0' || c >
'9')
898 token->length =
static_cast<SizeType>(
name - token->name);
899 if (token->length == 0)
904 if (isNumber && token->length > 1 && token->name[0] ==
'0')
910 for (
size_t j = 0; j < token->length; j++) {
943 template<
bool uriFragment,
typename OutputStream>
952 for (
size_t j = 0; j < t->length; j++) {
965 PercentEncodeStream<OutputStream> target(os);
985 typedef typename ValueType::Ch
Ch;
1001 for (
int j = 0; j < 2; j++) {
1002 c =
static_cast<Ch>(c << 4);
1004 if (
h >=
'0' &&
h <=
'9') c =
static_cast<Ch>(c +
h -
'0');
1005 else if (
h >=
'A' &&
h <=
'F') c =
static_cast<Ch>(c +
h -
'A' + 10);
1006 else if (
h >=
'a' &&
h <=
'f') c =
static_cast<Ch>(c +
h -
'a' + 10);
1027 template <
typename OutputStream>
1032 unsigned char u =
static_cast<unsigned char>(c);
1033 static const char hexDigits[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F' };
1035 os_.Put(
static_cast<typename OutputStream::Ch
>(hexDigits[u >> 4]));
1036 os_.Put(
static_cast<typename OutputStream::Ch
>(hexDigits[u & 15]));
1059template <
typename T>
1064template <
typename T,
typename CharType,
size_t N>
1071template <
typename DocumentType>
1073 return pointer.Create(document);
1076template <
typename DocumentType,
typename CharType,
size_t N>
1083template <
typename T>
1085 return pointer.Get(root, unresolvedTokenIndex);
1088template <
typename T>
1090 return pointer.Get(root, unresolvedTokenIndex);
1093template <
typename T,
typename CharType,
size_t N>
1098template <
typename T,
typename CharType,
size_t N>
1105template <
typename T>
1110template <
typename T>
1115#if RAPIDJSON_HAS_STDSTRING
1116template <
typename T>
1122template <
typename T,
typename T2>
1128template <
typename T,
typename CharType,
size_t N>
1133template <
typename T,
typename CharType,
size_t N>
1138#if RAPIDJSON_HAS_STDSTRING
1139template <
typename T,
typename CharType,
size_t N>
1145template <
typename T,
typename CharType,
size_t N,
typename T2>
1153template <
typename DocumentType>
1158template <
typename DocumentType>
1163#if RAPIDJSON_HAS_STDSTRING
1164template <
typename DocumentType>
1170template <
typename DocumentType,
typename T2>
1176template <
typename DocumentType,
typename CharType,
size_t N>
1181template <
typename DocumentType,
typename CharType,
size_t N>
1186#if RAPIDJSON_HAS_STDSTRING
1187template <
typename DocumentType,
typename CharType,
size_t N>
1193template <
typename DocumentType,
typename CharType,
size_t N,
typename T2>
1201template <
typename T>
1206template <
typename T>
1211template <
typename T>
1216#if RAPIDJSON_HAS_STDSTRING
1217template <
typename T>
1223template <
typename T,
typename T2>
1229template <
typename T,
typename CharType,
size_t N>
1234template <
typename T,
typename CharType,
size_t N>
1239template <
typename T,
typename CharType,
size_t N>
1244#if RAPIDJSON_HAS_STDSTRING
1245template <
typename T,
typename CharType,
size_t N>
1246typename T::ValueType&
SetValueByPointer(
T& root,
const CharType(&
source)[N],
const std::basic_string<typename T::Ch>&
value,
typename T::AllocatorType&
a) {
1251template <
typename T,
typename CharType,
size_t N,
typename T2>
1259template <
typename DocumentType>
1264template <
typename DocumentType>
1269template <
typename DocumentType>
1274#if RAPIDJSON_HAS_STDSTRING
1275template <
typename DocumentType>
1281template <
typename DocumentType,
typename T2>
1287template <
typename DocumentType,
typename CharType,
size_t N>
1292template <
typename DocumentType,
typename CharType,
size_t N>
1297template <
typename DocumentType,
typename CharType,
size_t N>
1302#if RAPIDJSON_HAS_STDSTRING
1303template <
typename DocumentType,
typename CharType,
size_t N>
1304typename DocumentType::ValueType&
SetValueByPointer(DocumentType& document,
const CharType(&
source)[N],
const std::basic_string<typename DocumentType::Ch>&
value) {
1309template <
typename DocumentType,
typename CharType,
size_t N,
typename T2>
1317template <
typename T>
1322template <
typename T,
typename CharType,
size_t N>
1327template <
typename DocumentType>
1332template <
typename DocumentType,
typename CharType,
size_t N>
1339template <
typename T>
1344template <
typename T,
typename CharType,
size_t N>
1353#if defined(__clang__) || defined(_MSC_VER)
#define s(x, c)
Definition aesb.c:47
static uint64_t h
Definition blockchain_stats.cpp:55
A document for parsing JSON text as DOM.
Definition document.h:2130
Ch Take()
Definition pointer.h:994
size_t Tell() const
Definition pointer.h:1016
PercentDecodeStream(const Ch *source, const Ch *end)
Constructor.
Definition pointer.h:992
bool IsValid() const
Definition pointer.h:1017
const Ch * src_
Current read position.
Definition pointer.h:1020
ValueType::Ch Ch
Definition pointer.h:985
bool valid_
Whether the parsing is valid.
Definition pointer.h:1023
const Ch * end_
Past-the-end position.
Definition pointer.h:1022
const Ch * head_
Original head of the string.
Definition pointer.h:1021
PercentEncodeStream(OutputStream &os)
Definition pointer.h:1030
OutputStream & os_
Definition pointer.h:1039
void Put(char c)
Definition pointer.h:1031
Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator.
Definition pointer.h:79
ValueType & Swap(GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, ValueType &value) const
Swap a value with a value in a document.
Definition pointer.h:701
Ch * CopyFromRaw(const GenericPointer &rhs, size_t extraToken=0, size_t extraNameBufferSize=0)
Clone the content from rhs to this.
Definition pointer.h:762
T value
Definition pointer.h:640
bool Stringify(OutputStream &os) const
Stringify to string or URI fragment representation.
Definition pointer.h:944
ValueType & Set(GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, const Ch *value) const
Set a null-terminated string in a document.
Definition pointer.h:658
RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr< internal::IsPointer< T >, internal::IsGenericValue< T > >),(ValueType &)) Set(GenericDocument< EncodingType
Set a primitive value in a document.
ValueType & Set(GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, const ValueType &value) const
Set a value in a document, with copy semantics.
Definition pointer.h:652
Ch * nameBuffer_
Definition pointer.h:1044
CrtAllocator * allocator_
Definition pointer.h:1042
GenericPointer(const GenericPointer &rhs, Allocator *allocator)
Copy constructor.
Definition pointer.h:171
GenericPointer(const Ch *source, Allocator *allocator=0)
Constructor that parses a string or URI fragment representation.
Definition pointer.h:114
Value::EncodingType EncodingType
Definition pointer.h:81
GenericPointer(const Token *tokens, size_t tokenCount)
Constructor with user-supplied tokens.
Definition pointer.h:163
void Parse(const Ch *source, size_t length)
Parse a JSON String or its URI fragment representation into tokens.
Definition pointer.h:805
ValueType & Set(GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, ValueType &value) const
Set a value in a document, with move semantics.
Definition pointer.h:646
GenericPointer(Allocator *allocator=0)
Default constructor.
Definition pointer.h:107
Allocator stackAllocator stackAllocator & document
Definition pointer.h:594
ValueType & Swap(ValueType &root, ValueType &value, typename ValueType::AllocatorType &allocator) const
Swap a value with a value in a subtree.
Definition pointer.h:695
size_t tokenCount_
Definition pointer.h:1046
bool Erase(ValueType &root) const
Erase a value in a subtree.
Definition pointer.h:714
GenericPointer(const GenericPointer &rhs)
Copy constructor.
Definition pointer.h:166
bool NeedPercentEncode(Ch c) const
Check whether a character should be percent-encoded.
Definition pointer.h:793
ValueType & Set(ValueType &root, ValueType &value, typename ValueType::AllocatorType &allocator) const
Set a value in a subtree, with move semantics.
Definition pointer.h:613
PointerParseErrorCode parseErrorCode_
Definition pointer.h:1048
RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr< internal::IsPointer< T >, internal::IsGenericValue< T > >),(ValueType &)) Set(ValueType &root
Set a primitive value in a subtree.
ValueType & Set(ValueType &root, const ValueType &value, typename ValueType::AllocatorType &allocator) const
Set a value in a subtree, with copy semantics.
Definition pointer.h:618
Token * tokens_
Definition pointer.h:1045
CrtAllocator * ownAllocator_
Definition pointer.h:1043
GenericPointer(const Ch *source, size_t length, Allocator *allocator=0)
Constructor that parses a string or URI fragment representation, with length of the source string.
Definition pointer.h:137
GenericPointer Append(const Ch *name, SizeType length, Allocator *allocator=0) const
Append a name token with length, and return a new Pointer.
Definition pointer.h:232
GenericPointer Append(const Token &token, Allocator *allocator=0) const
Append a token and return a new Pointer.
Definition pointer.h:214
RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr< internal::IsSame< typename internal::RemoveConst< T >::Type, Ch > >),(GenericPointer)) Append(T *name
Append a name token without length, and return a new Pointer.
Value::Ch Ch
Definition pointer.h:82
~GenericPointer()
Destructor.
Definition pointer.h:176
size_t parseErrorOffset_
Definition pointer.h:1047
ValueType & Set(ValueType &root, const Ch *value, typename ValueType::AllocatorType &allocator) const
Set a null-terminated string in a subtree.
Definition pointer.h:623
GenericPointer & operator=(const GenericPointer &rhs)
Assignment operator.
Definition pointer.h:183
CrtAllocator * allocator
Definition pointer.h:245
Allocator stackAllocator RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr< internal::IsPointer< T >, internal::IsGenericValue< T > >),(ValueType &)) GetWithDefault(GenericDocument< EncodingType
Concept for allocating, resizing and freeing memory block.
bool operator!=(expect< T > const &lhs, expect< U > const &rhs) noexcept(noexcept(lhs.equal(rhs)))
Definition expect.h:423
bool operator==(expect< T > const &lhs, expect< U > const &rhs) noexcept(noexcept(lhs.equal(rhs)))
Definition expect.h:402
GenericPointer< Value, CrtAllocator > Pointer
Definition fwd.h:128
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition rapidjson.h:411
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition rapidjson.h:121
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition rapidjson.h:124
PointerParseErrorCode
Error code of parsing.
Definition pointer.h:37
@ kPointerParseErrorInvalidEscape
Invalid escape.
Definition pointer.h:41
@ kPointerParseErrorTokenMustBeginWithSolidus
A token must begin with a '/'.
Definition pointer.h:40
@ kPointerParseErrorNone
The parse is successful.
Definition pointer.h:38
@ kPointerParseErrorCharacterMustPercentEncode
A character must percent encoded in URI fragment.
Definition pointer.h:43
@ kPointerParseErrorInvalidPercentEncoding
Invalid percent encoding in URI fragment.
Definition pointer.h:42
#define const
Definition ipfrdr.c:80
t
Definition console.py:33
GetTokens(source)
Definition tokenize.py:119
char * u64toa(uint64_t value, char *buffer)
Definition itoa.h:126
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition strfunc.h:31
char * u32toa(uint32_t value, char *buffer)
Definition itoa.h:39
int i
Definition pymoduletest.py:23
const char * name
Definition options.c:30
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
const GenericPointer< typename T::ValueType > T2 defaultValue
Definition pointer.h:1124
T::ValueType & SwapValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer, typename T::ValueType &value, typename T::AllocatorType &a)
Definition pointer.h:1318
T::ValueType & SetValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer, typename T::ValueType &value, typename T::AllocatorType &a)
Definition pointer.h:1202
T::ValueType * GetValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer, size_t *unresolvedTokenIndex=0)
Definition pointer.h:1084
bool EraseValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer)
Definition pointer.h:1340
T::ValueType & CreateValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer, typename T::AllocatorType &a)
Definition pointer.h:1060
RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr< internal::IsPointer< T2 >, internal::IsGenericValue< T2 > >),(typename T::ValueType &)) GetValueByPointerWithDefault(T &root
const GenericPointer< typename T::ValueType > & pointer
Definition pointer.h:1124
T::ValueType & GetValueByPointerWithDefault(T &root, const GenericPointer< typename T::ValueType > &pointer, const typename T::ValueType &defaultValue, typename T::AllocatorType &a)
Definition pointer.h:1106
const CharType(& source)[N]
Definition pointer.h:1147
static RAPIDJSON_NAMESPACE_BEGIN const SizeType kPointerInvalidIndex
Represents an invalid index in GenericPointer::Token.
Definition pointer.h:31
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1124
@ kObjectType
object
Definition rapidjson.h:627
@ kArrayType
array
Definition rapidjson.h:628
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition rapidjson.h:610
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.).
Definition rapidjson.h:389
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
Definition rapidjson.h:606
A read-write string stream.
Definition stream.h:188
Ch * PutBegin()
Definition stream.h:201
size_t PutEnd(Ch *begin)
Definition stream.h:202
A token is the basic units of internal representation.
Definition pointer.h:97
SizeType index
A valid array index, if it is not equal to kPointerInvalidIndex.
Definition pointer.h:100
SizeType length
Length of the name.
Definition pointer.h:99
const Ch * name
Name of the token. It has null character at the end but it can contain null character.
Definition pointer.h:98
Reference to a constant string (not taking a copy).
Definition document.h:253
Read-only string stream.
Definition stream.h:154
Encoding conversion.
Definition encodings.h:658
UTF-8 encoding.
Definition encodings.h:96
Definition document.h:416