15#ifndef RAPIDJSON_POINTER_H_
16#define RAPIDJSON_POINTER_H_
23RAPIDJSON_DIAG_OFF(
switch-
enum)
24#elif defined(_MSC_VER)
26RAPIDJSON_DIAG_OFF(4512)
31static const SizeType kPointerInvalidIndex = ~SizeType(0);
78template <
typename ValueType,
typename Allocator = CrtAllocator>
82 typedef typename ValueType::Ch
Ch;
118#if RAPIDJSON_HAS_STDSTRING
247 Ch *
p =
r.CopyFromRaw(*
this, 1, token.
length + 1);
263 Token token = {
name, length, kPointerInvalidIndex };
273 template <
typename T>
279#if RAPIDJSON_HAS_STDSTRING
301 buffer[length] =
'\0';
303 if (
sizeof(
Ch) == 1) {
304 Token token = {
reinterpret_cast<Ch*
>(buffer), length, index };
309 for (
size_t i = 0; i <= length; i++)
310 name[i] =
static_cast<Ch>(buffer[i]);
311 Token token = {
name, length, index };
323 if (token.IsString())
353 const Token* GetTokens()
const {
return tokens_; }
356 size_t GetTokenCount()
const {
return tokenCount_; }
426 template<
typename OutputStream>
427 bool Stringify(OutputStream&
os)
const {
428 return Stringify<false, OutputStream>(
os);
436 template<
typename OutputStream>
437 bool StringifyUriFragment(OutputStream&
os)
const {
438 return Stringify<true, OutputStream>(
os);
461 ValueType& Create(ValueType& root,
typename ValueType::AllocatorType&
allocator,
bool* alreadyExist = 0)
const {
466 if (v->IsArray() && t->name[0] ==
'-' && t->length == 1) {
468 v = &((*v)[v->Size() - 1]);
472 if (t->index == kPointerInvalidIndex) {
477 if (!v->IsArray() && !v->IsObject())
482 if (t->index >= v->Size()) {
484 while (t->index >= v->Size())
488 v = &((*v)[t->index]);
492 if (m == v->MemberEnd()) {
494 v = &(--v->MemberEnd())->value;
504 *alreadyExist = exist;
515 template <
typename stackAllocator>
517 return Create(document, document.
GetAllocator(), alreadyExist);
539 ValueType* Get(ValueType& root,
size_t* unresolvedTokenIndex = 0)
const {
543 switch (v->GetType()) {
547 if (m == v->MemberEnd())
553 if (t->index == kPointerInvalidIndex || t->index >= v->Size())
555 v = &((*v)[t->index]);
562 if (unresolvedTokenIndex)
563 *unresolvedTokenIndex =
static_cast<size_t>(t -
tokens_);
574 const ValueType* Get(
const ValueType& root,
size_t* unresolvedTokenIndex = 0)
const {
575 return Get(
const_cast<ValueType&
>(root), unresolvedTokenIndex);
606#if RAPIDJSON_HAS_STDSTRING
619 template <
typename T>
626 template <
typename stackAllocator>
632 template <
typename stackAllocator>
637#if RAPIDJSON_HAS_STDSTRING
639 template <
typename stackAllocator>
649 template <
typename T,
typename stackAllocator>
684#if RAPIDJSON_HAS_STDSTRING
686 ValueType& Set(ValueType& root,
const std::basic_string<Ch>&
value,
typename ValueType::AllocatorType&
allocator)
const {
695 template <
typename T>
697 Set(ValueType& root,
T value,
typename ValueType::AllocatorType&
allocator)
const {
702 template <
typename stackAllocator>
704 return Create(document) =
value;
708 template <
typename stackAllocator>
714 template <
typename stackAllocator>
719#if RAPIDJSON_HAS_STDSTRING
721 template <
typename stackAllocator>
731 template <
typename T,
typename stackAllocator>
734 return Create(document) =
value;
757 template <
typename stackAllocator>
771 bool Erase(ValueType& root)
const {
778 for (
const Token *t =
tokens_; t != last; ++t) {
779 switch (v->GetType()) {
783 if (m == v->MemberEnd())
789 if (t->index == kPointerInvalidIndex || t->index >= v->Size())
791 v = &((*v)[t->index]);
798 switch (v->GetType()) {
802 if (last->index == kPointerInvalidIndex || last->index >= v->Size())
804 v->Erase(v->Begin() + last->index);
819 Ch* CopyFromRaw(
const GenericPointer& rhs,
size_t extraToken = 0,
size_t extraNameBufferSize = 0) {
825 nameBufferSize += t->length;
833 if (nameBufferSize > 0) {
850 bool NeedPercentEncode(
Ch c)
const {
851 return !((c >=
'0' && c <=
'9') || (c >=
'A' && c <=
'Z') || (c >=
'a' && c <=
'z') || c ==
'-' || c ==
'.' || c ==
'_' || c ==
'~');
862 void Parse(
const Ch*
source,
size_t length) {
882 bool uriFragment =
false;
888 if (i != length &&
source[i] !=
'/') {
898 bool isNumber =
true;
900 while (i < length &&
source[i] !=
'/') {
907 Ch* begin =
os.PutBegin();
912 size_t len =
os.PutEnd(begin);
923 else if (NeedPercentEncode(c)) {
935 if (c ==
'0') c =
'~';
936 else if (c ==
'1') c =
'/';
950 if (c <
'0' || c >
'9')
955 token->length =
static_cast<SizeType>(
name - token->name);
956 if (token->length == 0)
961 if (isNumber && token->length > 1 && token->name[0] ==
'0')
967 for (
size_t j = 0;
j < token->length;
j++) {
977 token->index = isNumber ? n : kPointerInvalidIndex;
1000 template<
bool uriFragment,
typename OutputStream>
1001 bool Stringify(OutputStream&
os)
const {
1009 for (
size_t j = 0;
j < t->length;
j++) {
1015 else if (c ==
'/') {
1019 else if (uriFragment && NeedPercentEncode(c)) {
1022 PercentEncodeStream<OutputStream> target(
os);
1040 class PercentDecodeStream {
1042 typedef typename ValueType::Ch
Ch;
1049 PercentDecodeStream(
const Ch*
source,
const Ch* end) : src_(
source), head_(
source), end_(end), valid_(true) {}
1052 if (*src_ !=
'%' || src_ + 3 > end_) {
1058 for (
int j = 0;
j < 2;
j++) {
1059 c =
static_cast<Ch>(c << 4);
1061 if (h >=
'0' && h <=
'9') c =
static_cast<Ch>(c + h -
'0');
1062 else if (h >=
'A' && h <=
'F') c =
static_cast<Ch>(c + h -
'A' + 10);
1063 else if (h >=
'a' && h <=
'f') c =
static_cast<Ch>(c + h -
'a' + 10);
1073 size_t Tell()
const {
return static_cast<size_t>(src_ - head_); }
1074 bool IsValid()
const {
return valid_; }
1084 template <
typename OutputStream>
1089 unsigned char u =
static_cast<unsigned char>(c);
1090 static const char hexDigits[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F' };
1092 os_.Put(
static_cast<typename OutputStream::Ch
>(hexDigits[u >> 4]));
1093 os_.Put(
static_cast<typename OutputStream::Ch
>(hexDigits[u & 15]));
1116template <
typename T>
1121template <
typename T,
typename CharType,
size_t N>
1128template <
typename DocumentType>
1130 return pointer.Create(document);
1133template <
typename DocumentType,
typename CharType,
size_t N>
1140template <
typename T>
1142 return pointer.Get(root, unresolvedTokenIndex);
1145template <
typename T>
1147 return pointer.Get(root, unresolvedTokenIndex);
1150template <
typename T,
typename CharType,
size_t N>
1155template <
typename T,
typename CharType,
size_t N>
1162template <
typename T>
1167template <
typename T>
1172#if RAPIDJSON_HAS_STDSTRING
1173template <
typename T>
1179template <
typename T,
typename T2>
1185template <
typename T,
typename CharType,
size_t N>
1190template <
typename T,
typename CharType,
size_t N>
1195#if RAPIDJSON_HAS_STDSTRING
1196template <
typename T,
typename CharType,
size_t N>
1202template <
typename T,
typename CharType,
size_t N,
typename T2>
1210template <
typename DocumentType>
1215template <
typename DocumentType>
1220#if RAPIDJSON_HAS_STDSTRING
1221template <
typename DocumentType>
1227template <
typename DocumentType,
typename T2>
1233template <
typename DocumentType,
typename CharType,
size_t N>
1238template <
typename DocumentType,
typename CharType,
size_t N>
1243#if RAPIDJSON_HAS_STDSTRING
1244template <
typename DocumentType,
typename CharType,
size_t N>
1250template <
typename DocumentType,
typename CharType,
size_t N,
typename T2>
1258template <
typename T>
1263template <
typename T>
1268template <
typename T>
1273#if RAPIDJSON_HAS_STDSTRING
1274template <
typename T>
1280template <
typename T,
typename T2>
1286template <
typename T,
typename CharType,
size_t N>
1291template <
typename T,
typename CharType,
size_t N>
1296template <
typename T,
typename CharType,
size_t N>
1301#if RAPIDJSON_HAS_STDSTRING
1302template <
typename T,
typename CharType,
size_t N>
1303typename T::ValueType&
SetValueByPointer(
T& root,
const CharType(&
source)[
N],
const std::basic_string<typename T::Ch>&
value,
typename T::AllocatorType&
a) {
1308template <
typename T,
typename CharType,
size_t N,
typename T2>
1316template <
typename DocumentType>
1321template <
typename DocumentType>
1326template <
typename DocumentType>
1331#if RAPIDJSON_HAS_STDSTRING
1332template <
typename DocumentType>
1338template <
typename DocumentType,
typename T2>
1344template <
typename DocumentType,
typename CharType,
size_t N>
1349template <
typename DocumentType,
typename CharType,
size_t N>
1354template <
typename DocumentType,
typename CharType,
size_t N>
1359#if RAPIDJSON_HAS_STDSTRING
1360template <
typename DocumentType,
typename CharType,
size_t N>
1361typename DocumentType::ValueType&
SetValueByPointer(DocumentType& document,
const CharType(&
source)[
N],
const std::basic_string<typename DocumentType::Ch>&
value) {
1366template <
typename DocumentType,
typename CharType,
size_t N,
typename T2>
1374template <
typename T>
1379template <
typename T,
typename CharType,
size_t N>
1384template <
typename DocumentType>
1389template <
typename DocumentType,
typename CharType,
size_t N>
1396template <
typename T>
1401template <
typename T,
typename CharType,
size_t N>
1410#if defined(__clang__) || defined(_MSC_VER)
bool operator<(const CBigNum &a, const CBigNum &b)
A document for parsing JSON text as DOM.
Allocator & GetAllocator()
Get the allocator of this document.
A helper stream to encode character (UTF-8 code unit) into percent-encoded sequence.
PercentEncodeStream(OutputStream &os)
Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator.
ValueType::EncodingType EncodingType
Encoding type from Value.
friend void swap(GenericPointer &a, GenericPointer &b) RAPIDJSON_NOEXCEPT
free-standing swap function helper
Ch * nameBuffer_
A buffer containing all names in tokens.
Allocator * allocator_
The current allocator. It is either user-supplied or equal to ownAllocator_.
GenericPointer(const GenericPointer &rhs, Allocator *allocator)
Copy constructor.
GenericPointer(const Ch *source, Allocator *allocator=0)
Constructor that parses a string or URI fragment representation.
GenericPointer(const Token *tokens, size_t tokenCount)
Constructor with user-supplied tokens.
GenericPointer(Allocator *allocator=0)
Default constructor.
size_t tokenCount_
Number of tokens in tokens_.
ValueType::Ch Ch
Character type from Value.
GenericPointer(const GenericPointer &rhs)
Copy constructor.
PointerParseErrorCode parseErrorCode_
Parsing error code.
Token * tokens_
A list of tokens.
Allocator * ownAllocator_
Allocator owned by this Pointer.
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.
GenericPointer Append(const Ch *name, SizeType length, Allocator *allocator=0) const
Append a name token with length, and return a new Pointer.
GenericPointer Append(const Token &token, Allocator *allocator=0) const
Append a token and return a new Pointer.
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.
~GenericPointer()
Destructor.
size_t parseErrorOffset_
Offset in code unit when parsing fail.
GenericPointer & operator=(const GenericPointer &rhs)
Assignment operator.
GenericPointer & Swap(GenericPointer &other) RAPIDJSON_NOEXCEPT
Swap the content of this pointer with an other.
Concept for allocating, resizing and freeing memory block.
bool operator!=(const environment &other)
bool operator==(const environment &other)
#define RAPIDJSON_ASSERT(x)
Assertion.
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
PointerParseErrorCode
Error code of parsing.
@ kPointerParseErrorInvalidEscape
Invalid escape.
@ kPointerParseErrorTokenMustBeginWithSolidus
A token must begin with a '/'.
@ kPointerParseErrorNone
The parse is successful.
@ kPointerParseErrorCharacterMustPercentEncode
A character must percent encoded in URI fragment.
@ kPointerParseErrorInvalidPercentEncoding
Invalid percent encoding in URI fragment.
void diff(const std::string &a, const std::string &b)
char * u64toa(uint64_t value, char *buffer)
void Swap(T &a, T &b) RAPIDJSON_NOEXCEPT
Custom swap() to avoid dependency on C++ <algorithm> header.
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
char * u32toa(uint32_t value, char *buffer)
const GenericPointer< typename T::ValueType > T2 value
const GenericPointer< typename T::ValueType > T2 defaultValue
T::ValueType & SwapValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer, typename T::ValueType &value, typename T::AllocatorType &a)
T::ValueType & SetValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer, typename T::ValueType &value, typename T::AllocatorType &a)
T::ValueType * GetValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer, size_t *unresolvedTokenIndex=0)
bool EraseValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer)
T::ValueType & CreateValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer, typename T::AllocatorType &a)
RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr< internal::IsPointer< T2 >, internal::IsGenericValue< T2 > >),(typename T::ValueType &)) GetValueByPointerWithDefault(T &root
GenericPointer< Value > Pointer
GenericPointer for Value (UTF-8, default allocator).
const GenericPointer< typename T::ValueType > & pointer
T::ValueType & GetValueByPointerWithDefault(T &root, const GenericPointer< typename T::ValueType > &pointer, const typename T::ValueType &defaultValue, typename T::AllocatorType &a)
const CharType(& source)[N]
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
#define T(meth, val, expected)
#define RAPIDJSON_DELETE(x)
! customization point for global delete
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
A read-write string stream.
A token is the basic units of internal representation.
SizeType index
A valid array index, if it is not equal to kPointerInvalidIndex.
SizeType length
Length of the name.
const Ch * name
Name of the token. It has null character at the end but it can contain null character.
Reference to a constant string (not taking a copy)
void cmp(const Operand &op, uint32 imm)