Merge pull request #5 from natural-law/update-rapidjson
Update rapid json to version 1.1.0
This commit is contained in:
commit
4189719df7
|
|
@ -179,9 +179,10 @@ public:
|
||||||
|
|
||||||
size = RAPIDJSON_ALIGN(size);
|
size = RAPIDJSON_ALIGN(size);
|
||||||
if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
|
if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
|
||||||
AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
|
if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
void *buffer = reinterpret_cast<char *>(chunkHead_ + 1) + chunkHead_->size;
|
void *buffer = reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size;
|
||||||
chunkHead_->size += size;
|
chunkHead_->size += size;
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
@ -194,14 +195,16 @@ public:
|
||||||
if (newSize == 0)
|
if (newSize == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
originalSize = RAPIDJSON_ALIGN(originalSize);
|
||||||
|
newSize = RAPIDJSON_ALIGN(newSize);
|
||||||
|
|
||||||
// Do not shrink if new size is smaller than original
|
// Do not shrink if new size is smaller than original
|
||||||
if (originalSize >= newSize)
|
if (originalSize >= newSize)
|
||||||
return originalPtr;
|
return originalPtr;
|
||||||
|
|
||||||
// Simply expand it if it is the last allocation and there is sufficient space
|
// Simply expand it if it is the last allocation and there is sufficient space
|
||||||
if (originalPtr == (char *)(chunkHead_ + 1) + chunkHead_->size - originalSize) {
|
if (originalPtr == reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) {
|
||||||
size_t increment = static_cast<size_t>(newSize - originalSize);
|
size_t increment = static_cast<size_t>(newSize - originalSize);
|
||||||
increment = RAPIDJSON_ALIGN(increment);
|
|
||||||
if (chunkHead_->size + increment <= chunkHead_->capacity) {
|
if (chunkHead_->size + increment <= chunkHead_->capacity) {
|
||||||
chunkHead_->size += increment;
|
chunkHead_->size += increment;
|
||||||
return originalPtr;
|
return originalPtr;
|
||||||
|
|
@ -209,12 +212,14 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Realloc process: allocate and copy memory, do not free original buffer.
|
// Realloc process: allocate and copy memory, do not free original buffer.
|
||||||
void* newBuffer = Malloc(newSize);
|
if (void* newBuffer = Malloc(newSize)) {
|
||||||
RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly.
|
|
||||||
if (originalSize)
|
if (originalSize)
|
||||||
std::memcpy(newBuffer, originalPtr, originalSize);
|
std::memcpy(newBuffer, originalPtr, originalSize);
|
||||||
return newBuffer;
|
return newBuffer;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
//! Frees a memory block (concept Allocator)
|
//! Frees a memory block (concept Allocator)
|
||||||
static void Free(void *ptr) { (void)ptr; } // Do nothing
|
static void Free(void *ptr) { (void)ptr; } // Do nothing
|
||||||
|
|
@ -227,15 +232,20 @@ private:
|
||||||
|
|
||||||
//! Creates a new chunk.
|
//! Creates a new chunk.
|
||||||
/*! \param capacity Capacity of the chunk in bytes.
|
/*! \param capacity Capacity of the chunk in bytes.
|
||||||
|
\return true if success.
|
||||||
*/
|
*/
|
||||||
void AddChunk(size_t capacity) {
|
bool AddChunk(size_t capacity) {
|
||||||
if (!baseAllocator_)
|
if (!baseAllocator_)
|
||||||
ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator());
|
ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator());
|
||||||
ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(sizeof(ChunkHeader) + capacity));
|
if (ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) {
|
||||||
chunk->capacity = capacity;
|
chunk->capacity = capacity;
|
||||||
chunk->size = 0;
|
chunk->size = 0;
|
||||||
chunk->next = chunkHead_;
|
chunk->next = chunkHead_;
|
||||||
chunkHead_ = chunk;
|
chunkHead_ = chunk;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity.
|
static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity.
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -15,13 +15,19 @@
|
||||||
#ifndef RAPIDJSON_ENCODEDSTREAM_H_
|
#ifndef RAPIDJSON_ENCODEDSTREAM_H_
|
||||||
#define RAPIDJSON_ENCODEDSTREAM_H_
|
#define RAPIDJSON_ENCODEDSTREAM_H_
|
||||||
|
|
||||||
#include "rapidjson.h"
|
#include "stream.h"
|
||||||
|
#include "memorystream.h"
|
||||||
|
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
RAPIDJSON_DIAG_PUSH
|
RAPIDJSON_DIAG_PUSH
|
||||||
RAPIDJSON_DIAG_OFF(effc++)
|
RAPIDJSON_DIAG_OFF(effc++)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(padded)
|
||||||
|
#endif
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
//! Input byte stream wrapper with a statically bound encoding.
|
//! Input byte stream wrapper with a statically bound encoding.
|
||||||
|
|
@ -57,10 +63,38 @@ private:
|
||||||
Ch current_;
|
Ch current_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//! Specialized for UTF8 MemoryStream.
|
||||||
|
template <>
|
||||||
|
class EncodedInputStream<UTF8<>, MemoryStream> {
|
||||||
|
public:
|
||||||
|
typedef UTF8<>::Ch Ch;
|
||||||
|
|
||||||
|
EncodedInputStream(MemoryStream& is) : is_(is) {
|
||||||
|
if (static_cast<unsigned char>(is_.Peek()) == 0xEFu) is_.Take();
|
||||||
|
if (static_cast<unsigned char>(is_.Peek()) == 0xBBu) is_.Take();
|
||||||
|
if (static_cast<unsigned char>(is_.Peek()) == 0xBFu) is_.Take();
|
||||||
|
}
|
||||||
|
Ch Peek() const { return is_.Peek(); }
|
||||||
|
Ch Take() { return is_.Take(); }
|
||||||
|
size_t Tell() const { return is_.Tell(); }
|
||||||
|
|
||||||
|
// Not implemented
|
||||||
|
void Put(Ch) {}
|
||||||
|
void Flush() {}
|
||||||
|
Ch* PutBegin() { return 0; }
|
||||||
|
size_t PutEnd(Ch*) { return 0; }
|
||||||
|
|
||||||
|
MemoryStream& is_;
|
||||||
|
|
||||||
|
private:
|
||||||
|
EncodedInputStream(const EncodedInputStream&);
|
||||||
|
EncodedInputStream& operator=(const EncodedInputStream&);
|
||||||
|
};
|
||||||
|
|
||||||
//! Output byte stream wrapper with statically bound encoding.
|
//! Output byte stream wrapper with statically bound encoding.
|
||||||
/*!
|
/*!
|
||||||
\tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE.
|
\tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE.
|
||||||
\tparam InputByteStream Type of input byte stream. For example, FileWriteStream.
|
\tparam OutputByteStream Type of input byte stream. For example, FileWriteStream.
|
||||||
*/
|
*/
|
||||||
template <typename Encoding, typename OutputByteStream>
|
template <typename Encoding, typename OutputByteStream>
|
||||||
class EncodedOutputStream {
|
class EncodedOutputStream {
|
||||||
|
|
@ -77,8 +111,8 @@ public:
|
||||||
void Flush() { os_.Flush(); }
|
void Flush() { os_.Flush(); }
|
||||||
|
|
||||||
// Not implemented
|
// Not implemented
|
||||||
Ch Peek() const { RAPIDJSON_ASSERT(false); }
|
Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;}
|
||||||
Ch Take() { RAPIDJSON_ASSERT(false); }
|
Ch Take() { RAPIDJSON_ASSERT(false); return 0;}
|
||||||
size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
|
size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
|
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
|
|
@ -142,11 +176,11 @@ private:
|
||||||
// FF FE UTF-16LE
|
// FF FE UTF-16LE
|
||||||
// EF BB BF UTF-8
|
// EF BB BF UTF-8
|
||||||
|
|
||||||
const unsigned char* c = (const unsigned char *)is_->Peek4();
|
const unsigned char* c = reinterpret_cast<const unsigned char *>(is_->Peek4());
|
||||||
if (!c)
|
if (!c)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
unsigned bom = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24);
|
unsigned bom = static_cast<unsigned>(c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24));
|
||||||
hasBOM_ = false;
|
hasBOM_ = false;
|
||||||
if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
|
if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
|
||||||
else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
|
else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
|
||||||
|
|
@ -193,7 +227,7 @@ private:
|
||||||
//! Output stream wrapper with dynamically bound encoding and automatic encoding detection.
|
//! Output stream wrapper with dynamically bound encoding and automatic encoding detection.
|
||||||
/*!
|
/*!
|
||||||
\tparam CharType Type of character for writing.
|
\tparam CharType Type of character for writing.
|
||||||
\tparam InputByteStream type of output byte stream to be wrapped.
|
\tparam OutputByteStream type of output byte stream to be wrapped.
|
||||||
*/
|
*/
|
||||||
template <typename CharType, typename OutputByteStream>
|
template <typename CharType, typename OutputByteStream>
|
||||||
class AutoUTFOutputStream {
|
class AutoUTFOutputStream {
|
||||||
|
|
@ -227,8 +261,8 @@ public:
|
||||||
void Flush() { os_->Flush(); }
|
void Flush() { os_->Flush(); }
|
||||||
|
|
||||||
// Not implemented
|
// Not implemented
|
||||||
Ch Peek() const { RAPIDJSON_ASSERT(false); }
|
Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;}
|
||||||
Ch Take() { RAPIDJSON_ASSERT(false); }
|
Ch Take() { RAPIDJSON_ASSERT(false); return 0;}
|
||||||
size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
|
size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
|
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
|
|
@ -254,6 +288,10 @@ private:
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_END
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
RAPIDJSON_DIAG_POP
|
RAPIDJSON_DIAG_POP
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -120,19 +120,45 @@ struct UTF8 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename OutputStream>
|
||||||
|
static void EncodeUnsafe(OutputStream& os, unsigned codepoint) {
|
||||||
|
if (codepoint <= 0x7F)
|
||||||
|
PutUnsafe(os, static_cast<Ch>(codepoint & 0xFF));
|
||||||
|
else if (codepoint <= 0x7FF) {
|
||||||
|
PutUnsafe(os, static_cast<Ch>(0xC0 | ((codepoint >> 6) & 0xFF)));
|
||||||
|
PutUnsafe(os, static_cast<Ch>(0x80 | ((codepoint & 0x3F))));
|
||||||
|
}
|
||||||
|
else if (codepoint <= 0xFFFF) {
|
||||||
|
PutUnsafe(os, static_cast<Ch>(0xE0 | ((codepoint >> 12) & 0xFF)));
|
||||||
|
PutUnsafe(os, static_cast<Ch>(0x80 | ((codepoint >> 6) & 0x3F)));
|
||||||
|
PutUnsafe(os, static_cast<Ch>(0x80 | (codepoint & 0x3F)));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF);
|
||||||
|
PutUnsafe(os, static_cast<Ch>(0xF0 | ((codepoint >> 18) & 0xFF)));
|
||||||
|
PutUnsafe(os, static_cast<Ch>(0x80 | ((codepoint >> 12) & 0x3F)));
|
||||||
|
PutUnsafe(os, static_cast<Ch>(0x80 | ((codepoint >> 6) & 0x3F)));
|
||||||
|
PutUnsafe(os, static_cast<Ch>(0x80 | (codepoint & 0x3F)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <typename InputStream>
|
template <typename InputStream>
|
||||||
static bool Decode(InputStream& is, unsigned* codepoint) {
|
static bool Decode(InputStream& is, unsigned* codepoint) {
|
||||||
#define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | ((unsigned char)c & 0x3Fu)
|
#define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | (static_cast<unsigned char>(c) & 0x3Fu)
|
||||||
#define TRANS(mask) result &= ((GetRange((unsigned char)c) & mask) != 0)
|
#define TRANS(mask) result &= ((GetRange(static_cast<unsigned char>(c)) & mask) != 0)
|
||||||
#define TAIL() COPY(); TRANS(0x70)
|
#define TAIL() COPY(); TRANS(0x70)
|
||||||
Ch c = is.Take();
|
typename InputStream::Ch c = is.Take();
|
||||||
if (!(c & 0x80)) {
|
if (!(c & 0x80)) {
|
||||||
*codepoint = (unsigned char)c;
|
*codepoint = static_cast<unsigned char>(c);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char type = GetRange((unsigned char)c);
|
unsigned char type = GetRange(static_cast<unsigned char>(c));
|
||||||
*codepoint = (0xFF >> type) & (unsigned char)c;
|
if (type >= 32) {
|
||||||
|
*codepoint = 0;
|
||||||
|
} else {
|
||||||
|
*codepoint = (0xFF >> type) & static_cast<unsigned char>(c);
|
||||||
|
}
|
||||||
bool result = true;
|
bool result = true;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 2: TAIL(); return result;
|
case 2: TAIL(); return result;
|
||||||
|
|
@ -152,7 +178,7 @@ struct UTF8 {
|
||||||
template <typename InputStream, typename OutputStream>
|
template <typename InputStream, typename OutputStream>
|
||||||
static bool Validate(InputStream& is, OutputStream& os) {
|
static bool Validate(InputStream& is, OutputStream& os) {
|
||||||
#define COPY() os.Put(c = is.Take())
|
#define COPY() os.Put(c = is.Take())
|
||||||
#define TRANS(mask) result &= ((GetRange((unsigned char)c) & mask) != 0)
|
#define TRANS(mask) result &= ((GetRange(static_cast<unsigned char>(c)) & mask) != 0)
|
||||||
#define TAIL() COPY(); TRANS(0x70)
|
#define TAIL() COPY(); TRANS(0x70)
|
||||||
Ch c;
|
Ch c;
|
||||||
COPY();
|
COPY();
|
||||||
|
|
@ -160,7 +186,7 @@ struct UTF8 {
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
bool result = true;
|
bool result = true;
|
||||||
switch (GetRange((unsigned char)c)) {
|
switch (GetRange(static_cast<unsigned char>(c))) {
|
||||||
case 2: TAIL(); return result;
|
case 2: TAIL(); return result;
|
||||||
case 3: TAIL(); TAIL(); return result;
|
case 3: TAIL(); TAIL(); return result;
|
||||||
case 4: COPY(); TRANS(0x50); TAIL(); return result;
|
case 4: COPY(); TRANS(0x50); TAIL(); return result;
|
||||||
|
|
@ -196,12 +222,12 @@ struct UTF8 {
|
||||||
template <typename InputByteStream>
|
template <typename InputByteStream>
|
||||||
static CharType TakeBOM(InputByteStream& is) {
|
static CharType TakeBOM(InputByteStream& is) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
||||||
Ch c = Take(is);
|
typename InputByteStream::Ch c = Take(is);
|
||||||
if ((unsigned char)c != 0xEFu) return c;
|
if (static_cast<unsigned char>(c) != 0xEFu) return c;
|
||||||
c = is.Take();
|
c = is.Take();
|
||||||
if ((unsigned char)c != 0xBBu) return c;
|
if (static_cast<unsigned char>(c) != 0xBBu) return c;
|
||||||
c = is.Take();
|
c = is.Take();
|
||||||
if ((unsigned char)c != 0xBFu) return c;
|
if (static_cast<unsigned char>(c) != 0xBFu) return c;
|
||||||
c = is.Take();
|
c = is.Take();
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
@ -209,13 +235,15 @@ struct UTF8 {
|
||||||
template <typename InputByteStream>
|
template <typename InputByteStream>
|
||||||
static Ch Take(InputByteStream& is) {
|
static Ch Take(InputByteStream& is) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
||||||
return is.Take();
|
return static_cast<Ch>(is.Take());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputByteStream>
|
template <typename OutputByteStream>
|
||||||
static void PutBOM(OutputByteStream& os) {
|
static void PutBOM(OutputByteStream& os) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
||||||
os.Put(0xEFu); os.Put(0xBBu); os.Put(0xBFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>(0xEFu));
|
||||||
|
os.Put(static_cast<typename OutputByteStream::Ch>(0xBBu));
|
||||||
|
os.Put(static_cast<typename OutputByteStream::Ch>(0xBFu));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputByteStream>
|
template <typename OutputByteStream>
|
||||||
|
|
@ -259,18 +287,34 @@ struct UTF16 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename OutputStream>
|
||||||
|
static void EncodeUnsafe(OutputStream& os, unsigned codepoint) {
|
||||||
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2);
|
||||||
|
if (codepoint <= 0xFFFF) {
|
||||||
|
RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair
|
||||||
|
PutUnsafe(os, static_cast<typename OutputStream::Ch>(codepoint));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF);
|
||||||
|
unsigned v = codepoint - 0x10000;
|
||||||
|
PutUnsafe(os, static_cast<typename OutputStream::Ch>((v >> 10) | 0xD800));
|
||||||
|
PutUnsafe(os, (v & 0x3FF) | 0xDC00);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <typename InputStream>
|
template <typename InputStream>
|
||||||
static bool Decode(InputStream& is, unsigned* codepoint) {
|
static bool Decode(InputStream& is, unsigned* codepoint) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2);
|
||||||
Ch c = is.Take();
|
typename InputStream::Ch c = is.Take();
|
||||||
if (c < 0xD800 || c > 0xDFFF) {
|
if (c < 0xD800 || c > 0xDFFF) {
|
||||||
*codepoint = c;
|
*codepoint = static_cast<unsigned>(c);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if (c <= 0xDBFF) {
|
else if (c <= 0xDBFF) {
|
||||||
*codepoint = (c & 0x3FF) << 10;
|
*codepoint = (static_cast<unsigned>(c) & 0x3FF) << 10;
|
||||||
c = is.Take();
|
c = is.Take();
|
||||||
*codepoint |= (c & 0x3FF);
|
*codepoint |= (static_cast<unsigned>(c) & 0x3FF);
|
||||||
*codepoint += 0x10000;
|
*codepoint += 0x10000;
|
||||||
return c >= 0xDC00 && c <= 0xDFFF;
|
return c >= 0xDC00 && c <= 0xDFFF;
|
||||||
}
|
}
|
||||||
|
|
@ -281,8 +325,8 @@ struct UTF16 {
|
||||||
static bool Validate(InputStream& is, OutputStream& os) {
|
static bool Validate(InputStream& is, OutputStream& os) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2);
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2);
|
||||||
Ch c;
|
typename InputStream::Ch c;
|
||||||
os.Put(c = is.Take());
|
os.Put(static_cast<typename OutputStream::Ch>(c = is.Take()));
|
||||||
if (c < 0xD800 || c > 0xDFFF)
|
if (c < 0xD800 || c > 0xDFFF)
|
||||||
return true;
|
return true;
|
||||||
else if (c <= 0xDBFF) {
|
else if (c <= 0xDBFF) {
|
||||||
|
|
@ -300,28 +344,29 @@ struct UTF16LE : UTF16<CharType> {
|
||||||
static CharType TakeBOM(InputByteStream& is) {
|
static CharType TakeBOM(InputByteStream& is) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
||||||
CharType c = Take(is);
|
CharType c = Take(is);
|
||||||
return (unsigned short)c == 0xFEFFu ? Take(is) : c;
|
return static_cast<uint16_t>(c) == 0xFEFFu ? Take(is) : c;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename InputByteStream>
|
template <typename InputByteStream>
|
||||||
static CharType Take(InputByteStream& is) {
|
static CharType Take(InputByteStream& is) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
||||||
CharType c = (unsigned char)is.Take();
|
unsigned c = static_cast<uint8_t>(is.Take());
|
||||||
c |= (unsigned char)is.Take() << 8;
|
c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 8;
|
||||||
return c;
|
return static_cast<CharType>(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputByteStream>
|
template <typename OutputByteStream>
|
||||||
static void PutBOM(OutputByteStream& os) {
|
static void PutBOM(OutputByteStream& os) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
||||||
os.Put(0xFFu); os.Put(0xFEu);
|
os.Put(static_cast<typename OutputByteStream::Ch>(0xFFu));
|
||||||
|
os.Put(static_cast<typename OutputByteStream::Ch>(0xFEu));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputByteStream>
|
template <typename OutputByteStream>
|
||||||
static void Put(OutputByteStream& os, CharType c) {
|
static void Put(OutputByteStream& os, CharType c) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
||||||
os.Put(c & 0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>(static_cast<unsigned>(c) & 0xFFu));
|
||||||
os.Put((c >> 8) & 0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>((static_cast<unsigned>(c) >> 8) & 0xFFu));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -332,28 +377,29 @@ struct UTF16BE : UTF16<CharType> {
|
||||||
static CharType TakeBOM(InputByteStream& is) {
|
static CharType TakeBOM(InputByteStream& is) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
||||||
CharType c = Take(is);
|
CharType c = Take(is);
|
||||||
return (unsigned short)c == 0xFEFFu ? Take(is) : c;
|
return static_cast<uint16_t>(c) == 0xFEFFu ? Take(is) : c;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename InputByteStream>
|
template <typename InputByteStream>
|
||||||
static CharType Take(InputByteStream& is) {
|
static CharType Take(InputByteStream& is) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
||||||
CharType c = (unsigned char)is.Take() << 8;
|
unsigned c = static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 8;
|
||||||
c |= (unsigned char)is.Take();
|
c |= static_cast<uint8_t>(is.Take());
|
||||||
return c;
|
return static_cast<CharType>(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputByteStream>
|
template <typename OutputByteStream>
|
||||||
static void PutBOM(OutputByteStream& os) {
|
static void PutBOM(OutputByteStream& os) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
||||||
os.Put(0xFEu); os.Put(0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>(0xFEu));
|
||||||
|
os.Put(static_cast<typename OutputByteStream::Ch>(0xFFu));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputByteStream>
|
template <typename OutputByteStream>
|
||||||
static void Put(OutputByteStream& os, CharType c) {
|
static void Put(OutputByteStream& os, CharType c) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
||||||
os.Put((c >> 8) & 0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>((static_cast<unsigned>(c) >> 8) & 0xFFu));
|
||||||
os.Put(c & 0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>(static_cast<unsigned>(c) & 0xFFu));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -382,6 +428,13 @@ struct UTF32 {
|
||||||
os.Put(codepoint);
|
os.Put(codepoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename OutputStream>
|
||||||
|
static void EncodeUnsafe(OutputStream& os, unsigned codepoint) {
|
||||||
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4);
|
||||||
|
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF);
|
||||||
|
PutUnsafe(os, codepoint);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename InputStream>
|
template <typename InputStream>
|
||||||
static bool Decode(InputStream& is, unsigned* codepoint) {
|
static bool Decode(InputStream& is, unsigned* codepoint) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4);
|
||||||
|
|
@ -406,32 +459,35 @@ struct UTF32LE : UTF32<CharType> {
|
||||||
static CharType TakeBOM(InputByteStream& is) {
|
static CharType TakeBOM(InputByteStream& is) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
||||||
CharType c = Take(is);
|
CharType c = Take(is);
|
||||||
return (unsigned)c == 0x0000FEFFu ? Take(is) : c;
|
return static_cast<uint32_t>(c) == 0x0000FEFFu ? Take(is) : c;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename InputByteStream>
|
template <typename InputByteStream>
|
||||||
static CharType Take(InputByteStream& is) {
|
static CharType Take(InputByteStream& is) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
||||||
CharType c = (unsigned char)is.Take();
|
unsigned c = static_cast<uint8_t>(is.Take());
|
||||||
c |= (unsigned char)is.Take() << 8;
|
c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 8;
|
||||||
c |= (unsigned char)is.Take() << 16;
|
c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 16;
|
||||||
c |= (unsigned char)is.Take() << 24;
|
c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 24;
|
||||||
return c;
|
return static_cast<CharType>(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputByteStream>
|
template <typename OutputByteStream>
|
||||||
static void PutBOM(OutputByteStream& os) {
|
static void PutBOM(OutputByteStream& os) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
||||||
os.Put(0xFFu); os.Put(0xFEu); os.Put(0x00u); os.Put(0x00u);
|
os.Put(static_cast<typename OutputByteStream::Ch>(0xFFu));
|
||||||
|
os.Put(static_cast<typename OutputByteStream::Ch>(0xFEu));
|
||||||
|
os.Put(static_cast<typename OutputByteStream::Ch>(0x00u));
|
||||||
|
os.Put(static_cast<typename OutputByteStream::Ch>(0x00u));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputByteStream>
|
template <typename OutputByteStream>
|
||||||
static void Put(OutputByteStream& os, CharType c) {
|
static void Put(OutputByteStream& os, CharType c) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
||||||
os.Put(c & 0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>(c & 0xFFu));
|
||||||
os.Put((c >> 8) & 0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>((c >> 8) & 0xFFu));
|
||||||
os.Put((c >> 16) & 0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>((c >> 16) & 0xFFu));
|
||||||
os.Put((c >> 24) & 0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>((c >> 24) & 0xFFu));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -442,32 +498,35 @@ struct UTF32BE : UTF32<CharType> {
|
||||||
static CharType TakeBOM(InputByteStream& is) {
|
static CharType TakeBOM(InputByteStream& is) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
||||||
CharType c = Take(is);
|
CharType c = Take(is);
|
||||||
return (unsigned)c == 0x0000FEFFu ? Take(is) : c;
|
return static_cast<uint32_t>(c) == 0x0000FEFFu ? Take(is) : c;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename InputByteStream>
|
template <typename InputByteStream>
|
||||||
static CharType Take(InputByteStream& is) {
|
static CharType Take(InputByteStream& is) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
||||||
CharType c = (unsigned char)is.Take() << 24;
|
unsigned c = static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 24;
|
||||||
c |= (unsigned char)is.Take() << 16;
|
c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 16;
|
||||||
c |= (unsigned char)is.Take() << 8;
|
c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 8;
|
||||||
c |= (unsigned char)is.Take();
|
c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take()));
|
||||||
return c;
|
return static_cast<CharType>(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputByteStream>
|
template <typename OutputByteStream>
|
||||||
static void PutBOM(OutputByteStream& os) {
|
static void PutBOM(OutputByteStream& os) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
||||||
os.Put(0x00u); os.Put(0x00u); os.Put(0xFEu); os.Put(0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>(0x00u));
|
||||||
|
os.Put(static_cast<typename OutputByteStream::Ch>(0x00u));
|
||||||
|
os.Put(static_cast<typename OutputByteStream::Ch>(0xFEu));
|
||||||
|
os.Put(static_cast<typename OutputByteStream::Ch>(0xFFu));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputByteStream>
|
template <typename OutputByteStream>
|
||||||
static void Put(OutputByteStream& os, CharType c) {
|
static void Put(OutputByteStream& os, CharType c) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
|
||||||
os.Put((c >> 24) & 0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>((c >> 24) & 0xFFu));
|
||||||
os.Put((c >> 16) & 0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>((c >> 16) & 0xFFu));
|
||||||
os.Put((c >> 8) & 0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>((c >> 8) & 0xFFu));
|
||||||
os.Put(c & 0xFFu);
|
os.Put(static_cast<typename OutputByteStream::Ch>(c & 0xFFu));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -491,31 +550,37 @@ struct ASCII {
|
||||||
os.Put(static_cast<Ch>(codepoint & 0xFF));
|
os.Put(static_cast<Ch>(codepoint & 0xFF));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename OutputStream>
|
||||||
|
static void EncodeUnsafe(OutputStream& os, unsigned codepoint) {
|
||||||
|
RAPIDJSON_ASSERT(codepoint <= 0x7F);
|
||||||
|
PutUnsafe(os, static_cast<Ch>(codepoint & 0xFF));
|
||||||
|
}
|
||||||
|
|
||||||
template <typename InputStream>
|
template <typename InputStream>
|
||||||
static bool Decode(InputStream& is, unsigned* codepoint) {
|
static bool Decode(InputStream& is, unsigned* codepoint) {
|
||||||
unsigned char c = static_cast<unsigned char>(is.Take());
|
uint8_t c = static_cast<uint8_t>(is.Take());
|
||||||
*codepoint = c;
|
*codepoint = c;
|
||||||
return c <= 0X7F;
|
return c <= 0X7F;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename InputStream, typename OutputStream>
|
template <typename InputStream, typename OutputStream>
|
||||||
static bool Validate(InputStream& is, OutputStream& os) {
|
static bool Validate(InputStream& is, OutputStream& os) {
|
||||||
unsigned char c = is.Take();
|
uint8_t c = static_cast<uint8_t>(is.Take());
|
||||||
os.Put(c);
|
os.Put(static_cast<typename OutputStream::Ch>(c));
|
||||||
return c <= 0x7F;
|
return c <= 0x7F;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename InputByteStream>
|
template <typename InputByteStream>
|
||||||
static CharType TakeBOM(InputByteStream& is) {
|
static CharType TakeBOM(InputByteStream& is) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
||||||
Ch c = Take(is);
|
uint8_t c = static_cast<uint8_t>(Take(is));
|
||||||
return c;
|
return static_cast<Ch>(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename InputByteStream>
|
template <typename InputByteStream>
|
||||||
static Ch Take(InputByteStream& is) {
|
static Ch Take(InputByteStream& is) {
|
||||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
|
||||||
return is.Take();
|
return static_cast<Ch>(is.Take());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputByteStream>
|
template <typename OutputByteStream>
|
||||||
|
|
@ -561,6 +626,13 @@ struct AutoUTF {
|
||||||
(*f[os.GetType()])(os, codepoint);
|
(*f[os.GetType()])(os, codepoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename OutputStream>
|
||||||
|
RAPIDJSON_FORCEINLINE static void EncodeUnsafe(OutputStream& os, unsigned codepoint) {
|
||||||
|
typedef void (*EncodeFunc)(OutputStream&, unsigned);
|
||||||
|
static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(EncodeUnsafe) };
|
||||||
|
(*f[os.GetType()])(os, codepoint);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename InputStream>
|
template <typename InputStream>
|
||||||
RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) {
|
RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) {
|
||||||
typedef bool (*DecodeFunc)(InputStream&, unsigned*);
|
typedef bool (*DecodeFunc)(InputStream&, unsigned*);
|
||||||
|
|
@ -594,6 +666,15 @@ struct Transcoder {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename InputStream, typename OutputStream>
|
||||||
|
RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) {
|
||||||
|
unsigned codepoint;
|
||||||
|
if (!SourceEncoding::Decode(is, &codepoint))
|
||||||
|
return false;
|
||||||
|
TargetEncoding::EncodeUnsafe(os, codepoint);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
//! Validate one Unicode codepoint from an encoded stream.
|
//! Validate one Unicode codepoint from an encoded stream.
|
||||||
template<typename InputStream, typename OutputStream>
|
template<typename InputStream, typename OutputStream>
|
||||||
RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) {
|
RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) {
|
||||||
|
|
@ -601,6 +682,10 @@ struct Transcoder {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Forward declaration.
|
||||||
|
template<typename Stream>
|
||||||
|
inline void PutUnsafe(Stream& stream, typename Stream::Ch c);
|
||||||
|
|
||||||
//! Specialization of Transcoder with same source and target encoding.
|
//! Specialization of Transcoder with same source and target encoding.
|
||||||
template<typename Encoding>
|
template<typename Encoding>
|
||||||
struct Transcoder<Encoding, Encoding> {
|
struct Transcoder<Encoding, Encoding> {
|
||||||
|
|
@ -610,6 +695,12 @@ struct Transcoder<Encoding, Encoding> {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename InputStream, typename OutputStream>
|
||||||
|
RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) {
|
||||||
|
PutUnsafe(os, is.Take()); // Just copy one code unit. This semantic is different from primary template class.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename InputStream, typename OutputStream>
|
template<typename InputStream, typename OutputStream>
|
||||||
RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) {
|
RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) {
|
||||||
return Encoding::Validate(is, os); // source/target encoding are the same
|
return Encoding::Validate(is, os); // source/target encoding are the same
|
||||||
|
|
@ -618,7 +709,7 @@ struct Transcoder<Encoding, Encoding> {
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_END
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
#if defined(__GNUC__) || defined(_MSV_VER)
|
#if defined(__GNUC__) || defined(_MSC_VER)
|
||||||
RAPIDJSON_DIAG_POP
|
RAPIDJSON_DIAG_POP
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,17 @@
|
||||||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
// specific language governing permissions and limitations under the License.
|
// specific language governing permissions and limitations under the License.
|
||||||
|
|
||||||
#ifndef RAPIDJSON_ERROR_EN_H__
|
#ifndef RAPIDJSON_ERROR_EN_H_
|
||||||
#define RAPIDJSON_ERROR_EN_H__
|
#define RAPIDJSON_ERROR_EN_H_
|
||||||
|
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(switch-enum)
|
||||||
|
RAPIDJSON_DIAG_OFF(covered-switch-default)
|
||||||
|
#endif
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
//! Maps error code of parsing into error message.
|
//! Maps error code of parsing into error message.
|
||||||
|
|
@ -32,7 +38,7 @@ inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErro
|
||||||
case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error.");
|
case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error.");
|
||||||
|
|
||||||
case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty.");
|
case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty.");
|
||||||
case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not follow by other values.");
|
case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not be followed by other values.");
|
||||||
|
|
||||||
case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value.");
|
case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value.");
|
||||||
|
|
||||||
|
|
@ -55,11 +61,14 @@ inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErro
|
||||||
case kParseErrorTermination: return RAPIDJSON_ERROR_STRING("Terminate parsing due to Handler error.");
|
case kParseErrorTermination: return RAPIDJSON_ERROR_STRING("Terminate parsing due to Handler error.");
|
||||||
case kParseErrorUnspecificSyntaxError: return RAPIDJSON_ERROR_STRING("Unspecific syntax error.");
|
case kParseErrorUnspecificSyntaxError: return RAPIDJSON_ERROR_STRING("Unspecific syntax error.");
|
||||||
|
|
||||||
default:
|
default: return RAPIDJSON_ERROR_STRING("Unknown error.");
|
||||||
return RAPIDJSON_ERROR_STRING("Unknown error.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_END
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
#endif // RAPIDJSON_ERROR_EN_H__
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // RAPIDJSON_ERROR_EN_H_
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,16 @@
|
||||||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
// specific language governing permissions and limitations under the License.
|
// specific language governing permissions and limitations under the License.
|
||||||
|
|
||||||
#ifndef RAPIDJSON_ERROR_ERROR_H__
|
#ifndef RAPIDJSON_ERROR_ERROR_H_
|
||||||
#define RAPIDJSON_ERROR_ERROR_H__
|
#define RAPIDJSON_ERROR_ERROR_H_
|
||||||
|
|
||||||
#include "../rapidjson.h"
|
#include "../rapidjson.h"
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(padded)
|
||||||
|
#endif
|
||||||
|
|
||||||
/*! \file error.h */
|
/*! \file error.h */
|
||||||
|
|
||||||
/*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */
|
/*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */
|
||||||
|
|
@ -99,7 +104,7 @@ enum ParseErrorCode {
|
||||||
\see GenericReader::Parse, GenericDocument::Parse
|
\see GenericReader::Parse, GenericDocument::Parse
|
||||||
*/
|
*/
|
||||||
struct ParseResult {
|
struct ParseResult {
|
||||||
|
public:
|
||||||
//! Default constructor, no error.
|
//! Default constructor, no error.
|
||||||
ParseResult() : code_(kParseErrorNone), offset_(0) {}
|
ParseResult() : code_(kParseErrorNone), offset_(0) {}
|
||||||
//! Constructor to set an error.
|
//! Constructor to set an error.
|
||||||
|
|
@ -143,4 +148,8 @@ typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_END
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
#endif // RAPIDJSON_ERROR_ERROR_H__
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // RAPIDJSON_ERROR_ERROR_H_
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,16 @@
|
||||||
#ifndef RAPIDJSON_FILEREADSTREAM_H_
|
#ifndef RAPIDJSON_FILEREADSTREAM_H_
|
||||||
#define RAPIDJSON_FILEREADSTREAM_H_
|
#define RAPIDJSON_FILEREADSTREAM_H_
|
||||||
|
|
||||||
#include "rapidjson.h"
|
#include "stream.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(padded)
|
||||||
|
RAPIDJSON_DIAG_OFF(unreachable-code)
|
||||||
|
RAPIDJSON_DIAG_OFF(missing-noreturn)
|
||||||
|
#endif
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
//! File byte stream for input using fread().
|
//! File byte stream for input using fread().
|
||||||
|
|
@ -85,4 +92,8 @@ private:
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_END
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // RAPIDJSON_FILESTREAM_H_
|
#endif // RAPIDJSON_FILESTREAM_H_
|
||||||
|
|
|
||||||
|
|
@ -1,73 +0,0 @@
|
||||||
// Copyright (C) 2011 Milo Yip
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// copies of the Software, and to permit persons to whom the Software is
|
|
||||||
// furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included in
|
|
||||||
// all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
// THE SOFTWARE.
|
|
||||||
|
|
||||||
#ifndef RAPIDJSON_FILESTREAM_H_
|
|
||||||
#define RAPIDJSON_FILESTREAM_H_
|
|
||||||
|
|
||||||
#include "rapidjson.h"
|
|
||||||
#include <cstdio>
|
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
|
||||||
|
|
||||||
//! (Deprecated) Wrapper of C file stream for input or output.
|
|
||||||
/*!
|
|
||||||
This simple wrapper does not check the validity of the stream.
|
|
||||||
\note implements Stream concept
|
|
||||||
\note deprecated: This was only for basic testing in version 0.1, it is found that the performance is very low by using fgetc(). Use FileReadStream instead.
|
|
||||||
*/
|
|
||||||
class FileStream {
|
|
||||||
public:
|
|
||||||
typedef char Ch; //!< Character type. Only support char.
|
|
||||||
|
|
||||||
FileStream(std::FILE* fp) : fp_(fp), current_('\0'), count_(0) { Read(); }
|
|
||||||
char Peek() const { return current_; }
|
|
||||||
char Take() { char c = current_; Read(); return c; }
|
|
||||||
size_t Tell() const { return count_; }
|
|
||||||
void Put(char c) { fputc(c, fp_); }
|
|
||||||
void Flush() { fflush(fp_); }
|
|
||||||
|
|
||||||
// Not implemented
|
|
||||||
char* PutBegin() { return 0; }
|
|
||||||
size_t PutEnd(char*) { return 0; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
// Prohibit copy constructor & assignment operator.
|
|
||||||
FileStream(const FileStream&);
|
|
||||||
FileStream& operator=(const FileStream&);
|
|
||||||
|
|
||||||
void Read() {
|
|
||||||
RAPIDJSON_ASSERT(fp_ != 0);
|
|
||||||
int c = fgetc(fp_);
|
|
||||||
if (c != EOF) {
|
|
||||||
current_ = (char)c;
|
|
||||||
count_++;
|
|
||||||
}
|
|
||||||
else if (current_ != '\0')
|
|
||||||
current_ = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
std::FILE* fp_;
|
|
||||||
char current_;
|
|
||||||
size_t count_;
|
|
||||||
};
|
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_END
|
|
||||||
|
|
||||||
#endif // RAPIDJSON_FILESTREAM_H_
|
|
||||||
|
|
@ -15,9 +15,14 @@
|
||||||
#ifndef RAPIDJSON_FILEWRITESTREAM_H_
|
#ifndef RAPIDJSON_FILEWRITESTREAM_H_
|
||||||
#define RAPIDJSON_FILEWRITESTREAM_H_
|
#define RAPIDJSON_FILEWRITESTREAM_H_
|
||||||
|
|
||||||
#include "rapidjson.h"
|
#include "stream.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(unreachable-code)
|
||||||
|
#endif
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
//! Wrapper of C file stream for input using fread().
|
//! Wrapper of C file stream for input using fread().
|
||||||
|
|
@ -57,7 +62,11 @@ public:
|
||||||
|
|
||||||
void Flush() {
|
void Flush() {
|
||||||
if (current_ != buffer_) {
|
if (current_ != buffer_) {
|
||||||
fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
|
size_t result = fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
|
||||||
|
if (result < static_cast<size_t>(current_ - buffer_)) {
|
||||||
|
// failure deliberately ignored at this time
|
||||||
|
// added to avoid warn_unused_result build errors
|
||||||
|
}
|
||||||
current_ = buffer_;
|
current_ = buffer_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -88,4 +97,8 @@ inline void PutN(FileWriteStream& stream, char c, size_t n) {
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_END
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // RAPIDJSON_FILESTREAM_H_
|
#endif // RAPIDJSON_FILESTREAM_H_
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,151 @@
|
||||||
|
// 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_FWD_H_
|
||||||
|
#define RAPIDJSON_FWD_H_
|
||||||
|
|
||||||
|
#include "rapidjson.h"
|
||||||
|
|
||||||
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
// encodings.h
|
||||||
|
|
||||||
|
template<typename CharType> struct UTF8;
|
||||||
|
template<typename CharType> struct UTF16;
|
||||||
|
template<typename CharType> struct UTF16BE;
|
||||||
|
template<typename CharType> struct UTF16LE;
|
||||||
|
template<typename CharType> struct UTF32;
|
||||||
|
template<typename CharType> struct UTF32BE;
|
||||||
|
template<typename CharType> struct UTF32LE;
|
||||||
|
template<typename CharType> struct ASCII;
|
||||||
|
template<typename CharType> struct AutoUTF;
|
||||||
|
|
||||||
|
template<typename SourceEncoding, typename TargetEncoding>
|
||||||
|
struct Transcoder;
|
||||||
|
|
||||||
|
// allocators.h
|
||||||
|
|
||||||
|
class CrtAllocator;
|
||||||
|
|
||||||
|
template <typename BaseAllocator>
|
||||||
|
class MemoryPoolAllocator;
|
||||||
|
|
||||||
|
// stream.h
|
||||||
|
|
||||||
|
template <typename Encoding>
|
||||||
|
struct GenericStringStream;
|
||||||
|
|
||||||
|
typedef GenericStringStream<UTF8<char> > StringStream;
|
||||||
|
|
||||||
|
template <typename Encoding>
|
||||||
|
struct GenericInsituStringStream;
|
||||||
|
|
||||||
|
typedef GenericInsituStringStream<UTF8<char> > InsituStringStream;
|
||||||
|
|
||||||
|
// stringbuffer.h
|
||||||
|
|
||||||
|
template <typename Encoding, typename Allocator>
|
||||||
|
class GenericStringBuffer;
|
||||||
|
|
||||||
|
typedef GenericStringBuffer<UTF8<char>, CrtAllocator> StringBuffer;
|
||||||
|
|
||||||
|
// filereadstream.h
|
||||||
|
|
||||||
|
class FileReadStream;
|
||||||
|
|
||||||
|
// filewritestream.h
|
||||||
|
|
||||||
|
class FileWriteStream;
|
||||||
|
|
||||||
|
// memorybuffer.h
|
||||||
|
|
||||||
|
template <typename Allocator>
|
||||||
|
struct GenericMemoryBuffer;
|
||||||
|
|
||||||
|
typedef GenericMemoryBuffer<CrtAllocator> MemoryBuffer;
|
||||||
|
|
||||||
|
// memorystream.h
|
||||||
|
|
||||||
|
struct MemoryStream;
|
||||||
|
|
||||||
|
// reader.h
|
||||||
|
|
||||||
|
template<typename Encoding, typename Derived>
|
||||||
|
struct BaseReaderHandler;
|
||||||
|
|
||||||
|
template <typename SourceEncoding, typename TargetEncoding, typename StackAllocator>
|
||||||
|
class GenericReader;
|
||||||
|
|
||||||
|
typedef GenericReader<UTF8<char>, UTF8<char>, CrtAllocator> Reader;
|
||||||
|
|
||||||
|
// writer.h
|
||||||
|
|
||||||
|
template<typename OutputStream, typename SourceEncoding, typename TargetEncoding, typename StackAllocator, unsigned writeFlags>
|
||||||
|
class Writer;
|
||||||
|
|
||||||
|
// prettywriter.h
|
||||||
|
|
||||||
|
template<typename OutputStream, typename SourceEncoding, typename TargetEncoding, typename StackAllocator, unsigned writeFlags>
|
||||||
|
class PrettyWriter;
|
||||||
|
|
||||||
|
// document.h
|
||||||
|
|
||||||
|
template <typename Encoding, typename Allocator>
|
||||||
|
struct GenericMember;
|
||||||
|
|
||||||
|
template <bool Const, typename Encoding, typename Allocator>
|
||||||
|
class GenericMemberIterator;
|
||||||
|
|
||||||
|
template<typename CharType>
|
||||||
|
struct GenericStringRef;
|
||||||
|
|
||||||
|
template <typename Encoding, typename Allocator>
|
||||||
|
class GenericValue;
|
||||||
|
|
||||||
|
typedef GenericValue<UTF8<char>, MemoryPoolAllocator<CrtAllocator> > Value;
|
||||||
|
|
||||||
|
template <typename Encoding, typename Allocator, typename StackAllocator>
|
||||||
|
class GenericDocument;
|
||||||
|
|
||||||
|
typedef GenericDocument<UTF8<char>, MemoryPoolAllocator<CrtAllocator>, CrtAllocator> Document;
|
||||||
|
|
||||||
|
// pointer.h
|
||||||
|
|
||||||
|
template <typename ValueType, typename Allocator>
|
||||||
|
class GenericPointer;
|
||||||
|
|
||||||
|
typedef GenericPointer<Value, CrtAllocator> Pointer;
|
||||||
|
|
||||||
|
// schema.h
|
||||||
|
|
||||||
|
template <typename SchemaDocumentType>
|
||||||
|
class IGenericRemoteSchemaDocumentProvider;
|
||||||
|
|
||||||
|
template <typename ValueT, typename Allocator>
|
||||||
|
class GenericSchemaDocument;
|
||||||
|
|
||||||
|
typedef GenericSchemaDocument<Value, CrtAllocator> SchemaDocument;
|
||||||
|
typedef IGenericRemoteSchemaDocumentProvider<SchemaDocument> IRemoteSchemaDocumentProvider;
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename SchemaDocumentType,
|
||||||
|
typename OutputHandler,
|
||||||
|
typename StateAllocator>
|
||||||
|
class GenericSchemaValidator;
|
||||||
|
|
||||||
|
typedef GenericSchemaValidator<SchemaDocument, BaseReaderHandler<UTF8<char>, void>, CrtAllocator> SchemaValidator;
|
||||||
|
|
||||||
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
#endif // RAPIDJSON_RAPIDJSONFWD_H_
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#if defined(_MSC_VER) && defined(_M_AMD64)
|
#if defined(_MSC_VER) && defined(_M_AMD64)
|
||||||
#include <intrin.h> // for _umul128
|
#include <intrin.h> // for _umul128
|
||||||
|
#pragma intrinsic(_umul128)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
@ -51,6 +52,15 @@ public:
|
||||||
AppendDecimal64(decimals + i, decimals + i + length);
|
AppendDecimal64(decimals + i, decimals + i + length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BigInteger& operator=(const BigInteger &rhs)
|
||||||
|
{
|
||||||
|
if (this != &rhs) {
|
||||||
|
count_ = rhs.count_;
|
||||||
|
std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
BigInteger& operator=(uint64_t u) {
|
BigInteger& operator=(uint64_t u) {
|
||||||
digits_[0] = u;
|
digits_[0] = u;
|
||||||
count_ = 1;
|
count_ = 1;
|
||||||
|
|
@ -230,7 +240,7 @@ private:
|
||||||
uint64_t r = 0;
|
uint64_t r = 0;
|
||||||
for (const char* p = begin; p != end; ++p) {
|
for (const char* p = begin; p != end; ++p) {
|
||||||
RAPIDJSON_ASSERT(*p >= '0' && *p <= '9');
|
RAPIDJSON_ASSERT(*p >= '0' && *p <= '9');
|
||||||
r = r * 10 + (*p - '0');
|
r = r * 10u + static_cast<unsigned>(*p - '0');
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@
|
||||||
#if defined(_MSC_VER) && defined(_M_AMD64)
|
#if defined(_MSC_VER) && defined(_M_AMD64)
|
||||||
#include <intrin.h>
|
#include <intrin.h>
|
||||||
#pragma intrinsic(_BitScanReverse64)
|
#pragma intrinsic(_BitScanReverse64)
|
||||||
|
#pragma intrinsic(_umul128)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
@ -34,8 +35,13 @@ RAPIDJSON_DIAG_PUSH
|
||||||
RAPIDJSON_DIAG_OFF(effc++)
|
RAPIDJSON_DIAG_OFF(effc++)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(padded)
|
||||||
|
#endif
|
||||||
|
|
||||||
struct DiyFp {
|
struct DiyFp {
|
||||||
DiyFp() {}
|
DiyFp() : f(), e() {}
|
||||||
|
|
||||||
DiyFp(uint64_t fp, int exp) : f(fp), e(exp) {}
|
DiyFp(uint64_t fp, int exp) : f(fp), e(exp) {}
|
||||||
|
|
||||||
|
|
@ -232,8 +238,8 @@ inline DiyFp GetCachedPower(int e, int* K) {
|
||||||
}
|
}
|
||||||
|
|
||||||
inline DiyFp GetCachedPower10(int exp, int *outExp) {
|
inline DiyFp GetCachedPower10(int exp, int *outExp) {
|
||||||
unsigned index = (exp + 348) / 8;
|
unsigned index = (static_cast<unsigned>(exp) + 348u) / 8u;
|
||||||
*outExp = -348 + index * 8;
|
*outExp = -348 + static_cast<int>(index) * 8;
|
||||||
return GetCachedPowerByIndex(index);
|
return GetCachedPowerByIndex(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -241,6 +247,11 @@ inline DiyFp GetCachedPower10(int exp, int *outExp) {
|
||||||
RAPIDJSON_DIAG_POP
|
RAPIDJSON_DIAG_POP
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
RAPIDJSON_DIAG_OFF(padded)
|
||||||
|
#endif
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
RAPIDJSON_NAMESPACE_END
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ namespace internal {
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
RAPIDJSON_DIAG_PUSH
|
RAPIDJSON_DIAG_PUSH
|
||||||
RAPIDJSON_DIAG_OFF(effc++)
|
RAPIDJSON_DIAG_OFF(effc++)
|
||||||
|
RAPIDJSON_DIAG_OFF(array-bounds) // some gcc versions generate wrong warnings https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
|
inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) {
|
||||||
|
|
@ -62,7 +63,7 @@ inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buff
|
||||||
const DiyFp wp_w = Mp - W;
|
const DiyFp wp_w = Mp - W;
|
||||||
uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
|
uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
|
||||||
uint64_t p2 = Mp.f & (one.f - 1);
|
uint64_t p2 = Mp.f & (one.f - 1);
|
||||||
int kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
|
unsigned kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
|
||||||
*len = 0;
|
*len = 0;
|
||||||
|
|
||||||
while (kappa > 0) {
|
while (kappa > 0) {
|
||||||
|
|
@ -101,7 +102,8 @@ inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buff
|
||||||
kappa--;
|
kappa--;
|
||||||
if (p2 < delta) {
|
if (p2 < delta) {
|
||||||
*K += kappa;
|
*K += kappa;
|
||||||
GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * kPow10[-kappa]);
|
int index = -static_cast<int>(kappa);
|
||||||
|
GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[-static_cast<int>(kappa)] : 0));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -145,10 +147,10 @@ inline char* WriteExponent(int K, char* buffer) {
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline char* Prettify(char* buffer, int length, int k) {
|
inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) {
|
||||||
const int kk = length + k; // 10^(kk-1) <= v < 10^kk
|
const int kk = length + k; // 10^(kk-1) <= v < 10^kk
|
||||||
|
|
||||||
if (length <= kk && kk <= 21) {
|
if (0 <= k && kk <= 21) {
|
||||||
// 1234e7 -> 12340000000
|
// 1234e7 -> 12340000000
|
||||||
for (int i = length; i < kk; i++)
|
for (int i = length; i < kk; i++)
|
||||||
buffer[i] = '0';
|
buffer[i] = '0';
|
||||||
|
|
@ -158,20 +160,45 @@ inline char* Prettify(char* buffer, int length, int k) {
|
||||||
}
|
}
|
||||||
else if (0 < kk && kk <= 21) {
|
else if (0 < kk && kk <= 21) {
|
||||||
// 1234e-2 -> 12.34
|
// 1234e-2 -> 12.34
|
||||||
std::memmove(&buffer[kk + 1], &buffer[kk], length - kk);
|
std::memmove(&buffer[kk + 1], &buffer[kk], static_cast<size_t>(length - kk));
|
||||||
buffer[kk] = '.';
|
buffer[kk] = '.';
|
||||||
|
if (0 > k + maxDecimalPlaces) {
|
||||||
|
// When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1
|
||||||
|
// Remove extra trailing zeros (at least one) after truncation.
|
||||||
|
for (int i = kk + maxDecimalPlaces; i > kk + 1; i--)
|
||||||
|
if (buffer[i] != '0')
|
||||||
|
return &buffer[i + 1];
|
||||||
|
return &buffer[kk + 2]; // Reserve one zero
|
||||||
|
}
|
||||||
|
else
|
||||||
return &buffer[length + 1];
|
return &buffer[length + 1];
|
||||||
}
|
}
|
||||||
else if (-6 < kk && kk <= 0) {
|
else if (-6 < kk && kk <= 0) {
|
||||||
// 1234e-6 -> 0.001234
|
// 1234e-6 -> 0.001234
|
||||||
const int offset = 2 - kk;
|
const int offset = 2 - kk;
|
||||||
std::memmove(&buffer[offset], &buffer[0], length);
|
std::memmove(&buffer[offset], &buffer[0], static_cast<size_t>(length));
|
||||||
buffer[0] = '0';
|
buffer[0] = '0';
|
||||||
buffer[1] = '.';
|
buffer[1] = '.';
|
||||||
for (int i = 2; i < offset; i++)
|
for (int i = 2; i < offset; i++)
|
||||||
buffer[i] = '0';
|
buffer[i] = '0';
|
||||||
|
if (length - kk > maxDecimalPlaces) {
|
||||||
|
// When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1
|
||||||
|
// Remove extra trailing zeros (at least one) after truncation.
|
||||||
|
for (int i = maxDecimalPlaces + 1; i > 2; i--)
|
||||||
|
if (buffer[i] != '0')
|
||||||
|
return &buffer[i + 1];
|
||||||
|
return &buffer[3]; // Reserve one zero
|
||||||
|
}
|
||||||
|
else
|
||||||
return &buffer[length + offset];
|
return &buffer[length + offset];
|
||||||
}
|
}
|
||||||
|
else if (kk < -maxDecimalPlaces) {
|
||||||
|
// Truncate to zero
|
||||||
|
buffer[0] = '0';
|
||||||
|
buffer[1] = '.';
|
||||||
|
buffer[2] = '0';
|
||||||
|
return &buffer[3];
|
||||||
|
}
|
||||||
else if (length == 1) {
|
else if (length == 1) {
|
||||||
// 1e30
|
// 1e30
|
||||||
buffer[1] = 'e';
|
buffer[1] = 'e';
|
||||||
|
|
@ -179,14 +206,15 @@ inline char* Prettify(char* buffer, int length, int k) {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// 1234e30 -> 1.234e33
|
// 1234e30 -> 1.234e33
|
||||||
std::memmove(&buffer[2], &buffer[1], length - 1);
|
std::memmove(&buffer[2], &buffer[1], static_cast<size_t>(length - 1));
|
||||||
buffer[1] = '.';
|
buffer[1] = '.';
|
||||||
buffer[length + 1] = 'e';
|
buffer[length + 1] = 'e';
|
||||||
return WriteExponent(kk - 1, &buffer[0 + length + 2]);
|
return WriteExponent(kk - 1, &buffer[0 + length + 2]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline char* dtoa(double value, char* buffer) {
|
inline char* dtoa(double value, char* buffer, int maxDecimalPlaces = 324) {
|
||||||
|
RAPIDJSON_ASSERT(maxDecimalPlaces >= 1);
|
||||||
Double d(value);
|
Double d(value);
|
||||||
if (d.IsZero()) {
|
if (d.IsZero()) {
|
||||||
if (d.Sign())
|
if (d.Sign())
|
||||||
|
|
@ -203,7 +231,7 @@ inline char* dtoa(double value, char* buffer) {
|
||||||
}
|
}
|
||||||
int length, K;
|
int length, K;
|
||||||
Grisu2(value, buffer, &length, &K);
|
Grisu2(value, buffer, &length, &K);
|
||||||
return Prettify(buffer, length, K);
|
return Prettify(buffer, length, K, maxDecimalPlaces);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ public:
|
||||||
|
|
||||||
bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; }
|
bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; }
|
||||||
bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; }
|
bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; }
|
||||||
|
bool IsNanOrInf() const { return (u_ & kExponentMask) == kExponentMask; }
|
||||||
bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; }
|
bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; }
|
||||||
bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; }
|
bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; }
|
||||||
|
|
||||||
|
|
@ -53,7 +54,7 @@ public:
|
||||||
else if (order <= -1074)
|
else if (order <= -1074)
|
||||||
return 0;
|
return 0;
|
||||||
else
|
else
|
||||||
return order + 1074;
|
return static_cast<unsigned>(order) + 1074;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,701 @@
|
||||||
|
// 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_INTERNAL_REGEX_H_
|
||||||
|
#define RAPIDJSON_INTERNAL_REGEX_H_
|
||||||
|
|
||||||
|
#include "../allocators.h"
|
||||||
|
#include "../stream.h"
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(padded)
|
||||||
|
RAPIDJSON_DIAG_OFF(switch-enum)
|
||||||
|
RAPIDJSON_DIAG_OFF(implicit-fallthrough)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __GNUC__
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(effc++)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef RAPIDJSON_REGEX_VERBOSE
|
||||||
|
#define RAPIDJSON_REGEX_VERBOSE 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// GenericRegex
|
||||||
|
|
||||||
|
static const SizeType kRegexInvalidState = ~SizeType(0); //!< Represents an invalid index in GenericRegex::State::out, out1
|
||||||
|
static const SizeType kRegexInvalidRange = ~SizeType(0);
|
||||||
|
|
||||||
|
//! Regular expression engine with subset of ECMAscript grammar.
|
||||||
|
/*!
|
||||||
|
Supported regular expression syntax:
|
||||||
|
- \c ab Concatenation
|
||||||
|
- \c a|b Alternation
|
||||||
|
- \c a? Zero or one
|
||||||
|
- \c a* Zero or more
|
||||||
|
- \c a+ One or more
|
||||||
|
- \c a{3} Exactly 3 times
|
||||||
|
- \c a{3,} At least 3 times
|
||||||
|
- \c a{3,5} 3 to 5 times
|
||||||
|
- \c (ab) Grouping
|
||||||
|
- \c ^a At the beginning
|
||||||
|
- \c a$ At the end
|
||||||
|
- \c . Any character
|
||||||
|
- \c [abc] Character classes
|
||||||
|
- \c [a-c] Character class range
|
||||||
|
- \c [a-z0-9_] Character class combination
|
||||||
|
- \c [^abc] Negated character classes
|
||||||
|
- \c [^a-c] Negated character class range
|
||||||
|
- \c [\b] Backspace (U+0008)
|
||||||
|
- \c \\| \\\\ ... Escape characters
|
||||||
|
- \c \\f Form feed (U+000C)
|
||||||
|
- \c \\n Line feed (U+000A)
|
||||||
|
- \c \\r Carriage return (U+000D)
|
||||||
|
- \c \\t Tab (U+0009)
|
||||||
|
- \c \\v Vertical tab (U+000B)
|
||||||
|
|
||||||
|
\note This is a Thompson NFA engine, implemented with reference to
|
||||||
|
Cox, Russ. "Regular Expression Matching Can Be Simple And Fast (but is slow in Java, Perl, PHP, Python, Ruby,...).",
|
||||||
|
https://swtch.com/~rsc/regexp/regexp1.html
|
||||||
|
*/
|
||||||
|
template <typename Encoding, typename Allocator = CrtAllocator>
|
||||||
|
class GenericRegex {
|
||||||
|
public:
|
||||||
|
typedef typename Encoding::Ch Ch;
|
||||||
|
|
||||||
|
GenericRegex(const Ch* source, Allocator* allocator = 0) :
|
||||||
|
states_(allocator, 256), ranges_(allocator, 256), root_(kRegexInvalidState), stateCount_(), rangeCount_(),
|
||||||
|
stateSet_(), state0_(allocator, 0), state1_(allocator, 0), anchorBegin_(), anchorEnd_()
|
||||||
|
{
|
||||||
|
GenericStringStream<Encoding> ss(source);
|
||||||
|
DecodedStream<GenericStringStream<Encoding> > ds(ss);
|
||||||
|
Parse(ds);
|
||||||
|
}
|
||||||
|
|
||||||
|
~GenericRegex() {
|
||||||
|
Allocator::Free(stateSet_);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsValid() const {
|
||||||
|
return root_ != kRegexInvalidState;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename InputStream>
|
||||||
|
bool Match(InputStream& is) const {
|
||||||
|
return SearchWithAnchoring(is, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Match(const Ch* s) const {
|
||||||
|
GenericStringStream<Encoding> is(s);
|
||||||
|
return Match(is);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename InputStream>
|
||||||
|
bool Search(InputStream& is) const {
|
||||||
|
return SearchWithAnchoring(is, anchorBegin_, anchorEnd_);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Search(const Ch* s) const {
|
||||||
|
GenericStringStream<Encoding> is(s);
|
||||||
|
return Search(is);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
enum Operator {
|
||||||
|
kZeroOrOne,
|
||||||
|
kZeroOrMore,
|
||||||
|
kOneOrMore,
|
||||||
|
kConcatenation,
|
||||||
|
kAlternation,
|
||||||
|
kLeftParenthesis
|
||||||
|
};
|
||||||
|
|
||||||
|
static const unsigned kAnyCharacterClass = 0xFFFFFFFF; //!< For '.'
|
||||||
|
static const unsigned kRangeCharacterClass = 0xFFFFFFFE;
|
||||||
|
static const unsigned kRangeNegationFlag = 0x80000000;
|
||||||
|
|
||||||
|
struct Range {
|
||||||
|
unsigned start; //
|
||||||
|
unsigned end;
|
||||||
|
SizeType next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct State {
|
||||||
|
SizeType out; //!< Equals to kInvalid for matching state
|
||||||
|
SizeType out1; //!< Equals to non-kInvalid for split
|
||||||
|
SizeType rangeStart;
|
||||||
|
unsigned codepoint;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Frag {
|
||||||
|
Frag(SizeType s, SizeType o, SizeType m) : start(s), out(o), minIndex(m) {}
|
||||||
|
SizeType start;
|
||||||
|
SizeType out; //!< link-list of all output states
|
||||||
|
SizeType minIndex;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename SourceStream>
|
||||||
|
class DecodedStream {
|
||||||
|
public:
|
||||||
|
DecodedStream(SourceStream& ss) : ss_(ss), codepoint_() { Decode(); }
|
||||||
|
unsigned Peek() { return codepoint_; }
|
||||||
|
unsigned Take() {
|
||||||
|
unsigned c = codepoint_;
|
||||||
|
if (c) // No further decoding when '\0'
|
||||||
|
Decode();
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void Decode() {
|
||||||
|
if (!Encoding::Decode(ss_, &codepoint_))
|
||||||
|
codepoint_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SourceStream& ss_;
|
||||||
|
unsigned codepoint_;
|
||||||
|
};
|
||||||
|
|
||||||
|
State& GetState(SizeType index) {
|
||||||
|
RAPIDJSON_ASSERT(index < stateCount_);
|
||||||
|
return states_.template Bottom<State>()[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
const State& GetState(SizeType index) const {
|
||||||
|
RAPIDJSON_ASSERT(index < stateCount_);
|
||||||
|
return states_.template Bottom<State>()[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
Range& GetRange(SizeType index) {
|
||||||
|
RAPIDJSON_ASSERT(index < rangeCount_);
|
||||||
|
return ranges_.template Bottom<Range>()[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
const Range& GetRange(SizeType index) const {
|
||||||
|
RAPIDJSON_ASSERT(index < rangeCount_);
|
||||||
|
return ranges_.template Bottom<Range>()[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename InputStream>
|
||||||
|
void Parse(DecodedStream<InputStream>& ds) {
|
||||||
|
Allocator allocator;
|
||||||
|
Stack<Allocator> operandStack(&allocator, 256); // Frag
|
||||||
|
Stack<Allocator> operatorStack(&allocator, 256); // Operator
|
||||||
|
Stack<Allocator> atomCountStack(&allocator, 256); // unsigned (Atom per parenthesis)
|
||||||
|
|
||||||
|
*atomCountStack.template Push<unsigned>() = 0;
|
||||||
|
|
||||||
|
unsigned codepoint;
|
||||||
|
while (ds.Peek() != 0) {
|
||||||
|
switch (codepoint = ds.Take()) {
|
||||||
|
case '^':
|
||||||
|
anchorBegin_ = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '$':
|
||||||
|
anchorEnd_ = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '|':
|
||||||
|
while (!operatorStack.Empty() && *operatorStack.template Top<Operator>() < kAlternation)
|
||||||
|
if (!Eval(operandStack, *operatorStack.template Pop<Operator>(1)))
|
||||||
|
return;
|
||||||
|
*operatorStack.template Push<Operator>() = kAlternation;
|
||||||
|
*atomCountStack.template Top<unsigned>() = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '(':
|
||||||
|
*operatorStack.template Push<Operator>() = kLeftParenthesis;
|
||||||
|
*atomCountStack.template Push<unsigned>() = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ')':
|
||||||
|
while (!operatorStack.Empty() && *operatorStack.template Top<Operator>() != kLeftParenthesis)
|
||||||
|
if (!Eval(operandStack, *operatorStack.template Pop<Operator>(1)))
|
||||||
|
return;
|
||||||
|
if (operatorStack.Empty())
|
||||||
|
return;
|
||||||
|
operatorStack.template Pop<Operator>(1);
|
||||||
|
atomCountStack.template Pop<unsigned>(1);
|
||||||
|
ImplicitConcatenation(atomCountStack, operatorStack);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '?':
|
||||||
|
if (!Eval(operandStack, kZeroOrOne))
|
||||||
|
return;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '*':
|
||||||
|
if (!Eval(operandStack, kZeroOrMore))
|
||||||
|
return;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '+':
|
||||||
|
if (!Eval(operandStack, kOneOrMore))
|
||||||
|
return;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '{':
|
||||||
|
{
|
||||||
|
unsigned n, m;
|
||||||
|
if (!ParseUnsigned(ds, &n))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (ds.Peek() == ',') {
|
||||||
|
ds.Take();
|
||||||
|
if (ds.Peek() == '}')
|
||||||
|
m = kInfinityQuantifier;
|
||||||
|
else if (!ParseUnsigned(ds, &m) || m < n)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m = n;
|
||||||
|
|
||||||
|
if (!EvalQuantifier(operandStack, n, m) || ds.Peek() != '}')
|
||||||
|
return;
|
||||||
|
ds.Take();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '.':
|
||||||
|
PushOperand(operandStack, kAnyCharacterClass);
|
||||||
|
ImplicitConcatenation(atomCountStack, operatorStack);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '[':
|
||||||
|
{
|
||||||
|
SizeType range;
|
||||||
|
if (!ParseRange(ds, &range))
|
||||||
|
return;
|
||||||
|
SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, kRangeCharacterClass);
|
||||||
|
GetState(s).rangeStart = range;
|
||||||
|
*operandStack.template Push<Frag>() = Frag(s, s, s);
|
||||||
|
}
|
||||||
|
ImplicitConcatenation(atomCountStack, operatorStack);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '\\': // Escape character
|
||||||
|
if (!CharacterEscape(ds, &codepoint))
|
||||||
|
return; // Unsupported escape character
|
||||||
|
// fall through to default
|
||||||
|
|
||||||
|
default: // Pattern character
|
||||||
|
PushOperand(operandStack, codepoint);
|
||||||
|
ImplicitConcatenation(atomCountStack, operatorStack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!operatorStack.Empty())
|
||||||
|
if (!Eval(operandStack, *operatorStack.template Pop<Operator>(1)))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Link the operand to matching state.
|
||||||
|
if (operandStack.GetSize() == sizeof(Frag)) {
|
||||||
|
Frag* e = operandStack.template Pop<Frag>(1);
|
||||||
|
Patch(e->out, NewState(kRegexInvalidState, kRegexInvalidState, 0));
|
||||||
|
root_ = e->start;
|
||||||
|
|
||||||
|
#if RAPIDJSON_REGEX_VERBOSE
|
||||||
|
printf("root: %d\n", root_);
|
||||||
|
for (SizeType i = 0; i < stateCount_ ; i++) {
|
||||||
|
State& s = GetState(i);
|
||||||
|
printf("[%2d] out: %2d out1: %2d c: '%c'\n", i, s.out, s.out1, (char)s.codepoint);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preallocate buffer for SearchWithAnchoring()
|
||||||
|
RAPIDJSON_ASSERT(stateSet_ == 0);
|
||||||
|
if (stateCount_ > 0) {
|
||||||
|
stateSet_ = static_cast<unsigned*>(states_.GetAllocator().Malloc(GetStateSetSize()));
|
||||||
|
state0_.template Reserve<SizeType>(stateCount_);
|
||||||
|
state1_.template Reserve<SizeType>(stateCount_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SizeType NewState(SizeType out, SizeType out1, unsigned codepoint) {
|
||||||
|
State* s = states_.template Push<State>();
|
||||||
|
s->out = out;
|
||||||
|
s->out1 = out1;
|
||||||
|
s->codepoint = codepoint;
|
||||||
|
s->rangeStart = kRegexInvalidRange;
|
||||||
|
return stateCount_++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PushOperand(Stack<Allocator>& operandStack, unsigned codepoint) {
|
||||||
|
SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, codepoint);
|
||||||
|
*operandStack.template Push<Frag>() = Frag(s, s, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImplicitConcatenation(Stack<Allocator>& atomCountStack, Stack<Allocator>& operatorStack) {
|
||||||
|
if (*atomCountStack.template Top<unsigned>())
|
||||||
|
*operatorStack.template Push<Operator>() = kConcatenation;
|
||||||
|
(*atomCountStack.template Top<unsigned>())++;
|
||||||
|
}
|
||||||
|
|
||||||
|
SizeType Append(SizeType l1, SizeType l2) {
|
||||||
|
SizeType old = l1;
|
||||||
|
while (GetState(l1).out != kRegexInvalidState)
|
||||||
|
l1 = GetState(l1).out;
|
||||||
|
GetState(l1).out = l2;
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Patch(SizeType l, SizeType s) {
|
||||||
|
for (SizeType next; l != kRegexInvalidState; l = next) {
|
||||||
|
next = GetState(l).out;
|
||||||
|
GetState(l).out = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Eval(Stack<Allocator>& operandStack, Operator op) {
|
||||||
|
switch (op) {
|
||||||
|
case kConcatenation:
|
||||||
|
RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag) * 2);
|
||||||
|
{
|
||||||
|
Frag e2 = *operandStack.template Pop<Frag>(1);
|
||||||
|
Frag e1 = *operandStack.template Pop<Frag>(1);
|
||||||
|
Patch(e1.out, e2.start);
|
||||||
|
*operandStack.template Push<Frag>() = Frag(e1.start, e2.out, Min(e1.minIndex, e2.minIndex));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case kAlternation:
|
||||||
|
if (operandStack.GetSize() >= sizeof(Frag) * 2) {
|
||||||
|
Frag e2 = *operandStack.template Pop<Frag>(1);
|
||||||
|
Frag e1 = *operandStack.template Pop<Frag>(1);
|
||||||
|
SizeType s = NewState(e1.start, e2.start, 0);
|
||||||
|
*operandStack.template Push<Frag>() = Frag(s, Append(e1.out, e2.out), Min(e1.minIndex, e2.minIndex));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case kZeroOrOne:
|
||||||
|
if (operandStack.GetSize() >= sizeof(Frag)) {
|
||||||
|
Frag e = *operandStack.template Pop<Frag>(1);
|
||||||
|
SizeType s = NewState(kRegexInvalidState, e.start, 0);
|
||||||
|
*operandStack.template Push<Frag>() = Frag(s, Append(e.out, s), e.minIndex);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case kZeroOrMore:
|
||||||
|
if (operandStack.GetSize() >= sizeof(Frag)) {
|
||||||
|
Frag e = *operandStack.template Pop<Frag>(1);
|
||||||
|
SizeType s = NewState(kRegexInvalidState, e.start, 0);
|
||||||
|
Patch(e.out, s);
|
||||||
|
*operandStack.template Push<Frag>() = Frag(s, s, e.minIndex);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
|
default:
|
||||||
|
RAPIDJSON_ASSERT(op == kOneOrMore);
|
||||||
|
if (operandStack.GetSize() >= sizeof(Frag)) {
|
||||||
|
Frag e = *operandStack.template Pop<Frag>(1);
|
||||||
|
SizeType s = NewState(kRegexInvalidState, e.start, 0);
|
||||||
|
Patch(e.out, s);
|
||||||
|
*operandStack.template Push<Frag>() = Frag(e.start, s, e.minIndex);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool EvalQuantifier(Stack<Allocator>& operandStack, unsigned n, unsigned m) {
|
||||||
|
RAPIDJSON_ASSERT(n <= m);
|
||||||
|
RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag));
|
||||||
|
|
||||||
|
if (n == 0) {
|
||||||
|
if (m == 0) // a{0} not support
|
||||||
|
return false;
|
||||||
|
else if (m == kInfinityQuantifier)
|
||||||
|
Eval(operandStack, kZeroOrMore); // a{0,} -> a*
|
||||||
|
else {
|
||||||
|
Eval(operandStack, kZeroOrOne); // a{0,5} -> a?
|
||||||
|
for (unsigned i = 0; i < m - 1; i++)
|
||||||
|
CloneTopOperand(operandStack); // a{0,5} -> a? a? a? a? a?
|
||||||
|
for (unsigned i = 0; i < m - 1; i++)
|
||||||
|
Eval(operandStack, kConcatenation); // a{0,5} -> a?a?a?a?a?
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < n - 1; i++) // a{3} -> a a a
|
||||||
|
CloneTopOperand(operandStack);
|
||||||
|
|
||||||
|
if (m == kInfinityQuantifier)
|
||||||
|
Eval(operandStack, kOneOrMore); // a{3,} -> a a a+
|
||||||
|
else if (m > n) {
|
||||||
|
CloneTopOperand(operandStack); // a{3,5} -> a a a a
|
||||||
|
Eval(operandStack, kZeroOrOne); // a{3,5} -> a a a a?
|
||||||
|
for (unsigned i = n; i < m - 1; i++)
|
||||||
|
CloneTopOperand(operandStack); // a{3,5} -> a a a a? a?
|
||||||
|
for (unsigned i = n; i < m; i++)
|
||||||
|
Eval(operandStack, kConcatenation); // a{3,5} -> a a aa?a?
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < n - 1; i++)
|
||||||
|
Eval(operandStack, kConcatenation); // a{3} -> aaa, a{3,} -> aaa+, a{3.5} -> aaaa?a?
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static SizeType Min(SizeType a, SizeType b) { return a < b ? a : b; }
|
||||||
|
|
||||||
|
void CloneTopOperand(Stack<Allocator>& operandStack) {
|
||||||
|
const Frag src = *operandStack.template Top<Frag>(); // Copy constructor to prevent invalidation
|
||||||
|
SizeType count = stateCount_ - src.minIndex; // Assumes top operand contains states in [src->minIndex, stateCount_)
|
||||||
|
State* s = states_.template Push<State>(count);
|
||||||
|
memcpy(s, &GetState(src.minIndex), count * sizeof(State));
|
||||||
|
for (SizeType j = 0; j < count; j++) {
|
||||||
|
if (s[j].out != kRegexInvalidState)
|
||||||
|
s[j].out += count;
|
||||||
|
if (s[j].out1 != kRegexInvalidState)
|
||||||
|
s[j].out1 += count;
|
||||||
|
}
|
||||||
|
*operandStack.template Push<Frag>() = Frag(src.start + count, src.out + count, src.minIndex + count);
|
||||||
|
stateCount_ += count;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename InputStream>
|
||||||
|
bool ParseUnsigned(DecodedStream<InputStream>& ds, unsigned* u) {
|
||||||
|
unsigned r = 0;
|
||||||
|
if (ds.Peek() < '0' || ds.Peek() > '9')
|
||||||
|
return false;
|
||||||
|
while (ds.Peek() >= '0' && ds.Peek() <= '9') {
|
||||||
|
if (r >= 429496729 && ds.Peek() > '5') // 2^32 - 1 = 4294967295
|
||||||
|
return false; // overflow
|
||||||
|
r = r * 10 + (ds.Take() - '0');
|
||||||
|
}
|
||||||
|
*u = r;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename InputStream>
|
||||||
|
bool ParseRange(DecodedStream<InputStream>& ds, SizeType* range) {
|
||||||
|
bool isBegin = true;
|
||||||
|
bool negate = false;
|
||||||
|
int step = 0;
|
||||||
|
SizeType start = kRegexInvalidRange;
|
||||||
|
SizeType current = kRegexInvalidRange;
|
||||||
|
unsigned codepoint;
|
||||||
|
while ((codepoint = ds.Take()) != 0) {
|
||||||
|
if (isBegin) {
|
||||||
|
isBegin = false;
|
||||||
|
if (codepoint == '^') {
|
||||||
|
negate = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (codepoint) {
|
||||||
|
case ']':
|
||||||
|
if (start == kRegexInvalidRange)
|
||||||
|
return false; // Error: nothing inside []
|
||||||
|
if (step == 2) { // Add trailing '-'
|
||||||
|
SizeType r = NewRange('-');
|
||||||
|
RAPIDJSON_ASSERT(current != kRegexInvalidRange);
|
||||||
|
GetRange(current).next = r;
|
||||||
|
}
|
||||||
|
if (negate)
|
||||||
|
GetRange(start).start |= kRangeNegationFlag;
|
||||||
|
*range = start;
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case '\\':
|
||||||
|
if (ds.Peek() == 'b') {
|
||||||
|
ds.Take();
|
||||||
|
codepoint = 0x0008; // Escape backspace character
|
||||||
|
}
|
||||||
|
else if (!CharacterEscape(ds, &codepoint))
|
||||||
|
return false;
|
||||||
|
// fall through to default
|
||||||
|
|
||||||
|
default:
|
||||||
|
switch (step) {
|
||||||
|
case 1:
|
||||||
|
if (codepoint == '-') {
|
||||||
|
step++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// fall through to step 0 for other characters
|
||||||
|
|
||||||
|
case 0:
|
||||||
|
{
|
||||||
|
SizeType r = NewRange(codepoint);
|
||||||
|
if (current != kRegexInvalidRange)
|
||||||
|
GetRange(current).next = r;
|
||||||
|
if (start == kRegexInvalidRange)
|
||||||
|
start = r;
|
||||||
|
current = r;
|
||||||
|
}
|
||||||
|
step = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
RAPIDJSON_ASSERT(step == 2);
|
||||||
|
GetRange(current).end = codepoint;
|
||||||
|
step = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
SizeType NewRange(unsigned codepoint) {
|
||||||
|
Range* r = ranges_.template Push<Range>();
|
||||||
|
r->start = r->end = codepoint;
|
||||||
|
r->next = kRegexInvalidRange;
|
||||||
|
return rangeCount_++;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename InputStream>
|
||||||
|
bool CharacterEscape(DecodedStream<InputStream>& ds, unsigned* escapedCodepoint) {
|
||||||
|
unsigned codepoint;
|
||||||
|
switch (codepoint = ds.Take()) {
|
||||||
|
case '^':
|
||||||
|
case '$':
|
||||||
|
case '|':
|
||||||
|
case '(':
|
||||||
|
case ')':
|
||||||
|
case '?':
|
||||||
|
case '*':
|
||||||
|
case '+':
|
||||||
|
case '.':
|
||||||
|
case '[':
|
||||||
|
case ']':
|
||||||
|
case '{':
|
||||||
|
case '}':
|
||||||
|
case '\\':
|
||||||
|
*escapedCodepoint = codepoint; return true;
|
||||||
|
case 'f': *escapedCodepoint = 0x000C; return true;
|
||||||
|
case 'n': *escapedCodepoint = 0x000A; return true;
|
||||||
|
case 'r': *escapedCodepoint = 0x000D; return true;
|
||||||
|
case 't': *escapedCodepoint = 0x0009; return true;
|
||||||
|
case 'v': *escapedCodepoint = 0x000B; return true;
|
||||||
|
default:
|
||||||
|
return false; // Unsupported escape character
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename InputStream>
|
||||||
|
bool SearchWithAnchoring(InputStream& is, bool anchorBegin, bool anchorEnd) const {
|
||||||
|
RAPIDJSON_ASSERT(IsValid());
|
||||||
|
DecodedStream<InputStream> ds(is);
|
||||||
|
|
||||||
|
state0_.Clear();
|
||||||
|
Stack<Allocator> *current = &state0_, *next = &state1_;
|
||||||
|
const size_t stateSetSize = GetStateSetSize();
|
||||||
|
std::memset(stateSet_, 0, stateSetSize);
|
||||||
|
|
||||||
|
bool matched = AddState(*current, root_);
|
||||||
|
unsigned codepoint;
|
||||||
|
while (!current->Empty() && (codepoint = ds.Take()) != 0) {
|
||||||
|
std::memset(stateSet_, 0, stateSetSize);
|
||||||
|
next->Clear();
|
||||||
|
matched = false;
|
||||||
|
for (const SizeType* s = current->template Bottom<SizeType>(); s != current->template End<SizeType>(); ++s) {
|
||||||
|
const State& sr = GetState(*s);
|
||||||
|
if (sr.codepoint == codepoint ||
|
||||||
|
sr.codepoint == kAnyCharacterClass ||
|
||||||
|
(sr.codepoint == kRangeCharacterClass && MatchRange(sr.rangeStart, codepoint)))
|
||||||
|
{
|
||||||
|
matched = AddState(*next, sr.out) || matched;
|
||||||
|
if (!anchorEnd && matched)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!anchorBegin)
|
||||||
|
AddState(*next, root_);
|
||||||
|
}
|
||||||
|
internal::Swap(current, next);
|
||||||
|
}
|
||||||
|
|
||||||
|
return matched;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t GetStateSetSize() const {
|
||||||
|
return (stateCount_ + 31) / 32 * 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return whether the added states is a match state
|
||||||
|
bool AddState(Stack<Allocator>& l, SizeType index) const {
|
||||||
|
RAPIDJSON_ASSERT(index != kRegexInvalidState);
|
||||||
|
|
||||||
|
const State& s = GetState(index);
|
||||||
|
if (s.out1 != kRegexInvalidState) { // Split
|
||||||
|
bool matched = AddState(l, s.out);
|
||||||
|
return AddState(l, s.out1) || matched;
|
||||||
|
}
|
||||||
|
else if (!(stateSet_[index >> 5] & (1 << (index & 31)))) {
|
||||||
|
stateSet_[index >> 5] |= (1 << (index & 31));
|
||||||
|
*l.template PushUnsafe<SizeType>() = index;
|
||||||
|
}
|
||||||
|
return s.out == kRegexInvalidState; // by using PushUnsafe() above, we can ensure s is not validated due to reallocation.
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MatchRange(SizeType rangeIndex, unsigned codepoint) const {
|
||||||
|
bool yes = (GetRange(rangeIndex).start & kRangeNegationFlag) == 0;
|
||||||
|
while (rangeIndex != kRegexInvalidRange) {
|
||||||
|
const Range& r = GetRange(rangeIndex);
|
||||||
|
if (codepoint >= (r.start & ~kRangeNegationFlag) && codepoint <= r.end)
|
||||||
|
return yes;
|
||||||
|
rangeIndex = r.next;
|
||||||
|
}
|
||||||
|
return !yes;
|
||||||
|
}
|
||||||
|
|
||||||
|
Stack<Allocator> states_;
|
||||||
|
Stack<Allocator> ranges_;
|
||||||
|
SizeType root_;
|
||||||
|
SizeType stateCount_;
|
||||||
|
SizeType rangeCount_;
|
||||||
|
|
||||||
|
static const unsigned kInfinityQuantifier = ~0u;
|
||||||
|
|
||||||
|
// For SearchWithAnchoring()
|
||||||
|
uint32_t* stateSet_; // allocated by states_.GetAllocator()
|
||||||
|
mutable Stack<Allocator> state0_;
|
||||||
|
mutable Stack<Allocator> state1_;
|
||||||
|
bool anchorBegin_;
|
||||||
|
bool anchorEnd_;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef GenericRegex<UTF8<> > Regex;
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // RAPIDJSON_INTERNAL_REGEX_H_
|
||||||
|
|
@ -15,7 +15,13 @@
|
||||||
#ifndef RAPIDJSON_INTERNAL_STACK_H_
|
#ifndef RAPIDJSON_INTERNAL_STACK_H_
|
||||||
#define RAPIDJSON_INTERNAL_STACK_H_
|
#define RAPIDJSON_INTERNAL_STACK_H_
|
||||||
|
|
||||||
#include "../rapidjson.h"
|
#include "../allocators.h"
|
||||||
|
#include "swap.h"
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(c++98-compat)
|
||||||
|
#endif
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
@ -32,7 +38,6 @@ public:
|
||||||
// Optimization note: Do not allocate memory for stack_ in constructor.
|
// Optimization note: Do not allocate memory for stack_ in constructor.
|
||||||
// Do it lazily when first Push() -> Expand() -> Resize().
|
// Do it lazily when first Push() -> Expand() -> Resize().
|
||||||
Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
|
Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
|
||||||
RAPIDJSON_ASSERT(stackCapacity > 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
|
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
|
||||||
|
|
@ -81,6 +86,15 @@ public:
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT {
|
||||||
|
internal::Swap(allocator_, rhs.allocator_);
|
||||||
|
internal::Swap(ownAllocator_, rhs.ownAllocator_);
|
||||||
|
internal::Swap(stack_, rhs.stack_);
|
||||||
|
internal::Swap(stackTop_, rhs.stackTop_);
|
||||||
|
internal::Swap(stackEnd_, rhs.stackEnd_);
|
||||||
|
internal::Swap(initialCapacity_, rhs.initialCapacity_);
|
||||||
|
}
|
||||||
|
|
||||||
void Clear() { stackTop_ = stack_; }
|
void Clear() { stackTop_ = stack_; }
|
||||||
|
|
||||||
void ShrinkToFit() {
|
void ShrinkToFit() {
|
||||||
|
|
@ -98,11 +112,21 @@ public:
|
||||||
// Optimization note: try to minimize the size of this function for force inline.
|
// Optimization note: try to minimize the size of this function for force inline.
|
||||||
// Expansion is run very infrequently, so it is moved to another (probably non-inline) function.
|
// Expansion is run very infrequently, so it is moved to another (probably non-inline) function.
|
||||||
template<typename T>
|
template<typename T>
|
||||||
RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) {
|
RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) {
|
||||||
// Expand the stack if needed
|
// Expand the stack if needed
|
||||||
if (stackTop_ + sizeof(T) * count >= stackEnd_)
|
if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count > stackEnd_))
|
||||||
Expand<T>(count);
|
Expand<T>(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) {
|
||||||
|
Reserve<T>(count);
|
||||||
|
return PushUnsafe<T>(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) {
|
||||||
|
RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_);
|
||||||
T* ret = reinterpret_cast<T*>(stackTop_);
|
T* ret = reinterpret_cast<T*>(stackTop_);
|
||||||
stackTop_ += sizeof(T) * count;
|
stackTop_ += sizeof(T) * count;
|
||||||
return ret;
|
return ret;
|
||||||
|
|
@ -122,9 +146,32 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T* Bottom() { return (T*)stack_; }
|
const T* Top() const {
|
||||||
|
RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
|
||||||
|
return reinterpret_cast<T*>(stackTop_ - sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T* End() { return reinterpret_cast<T*>(stackTop_); }
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
const T* End() const { return reinterpret_cast<T*>(stackTop_); }
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T* Bottom() { return reinterpret_cast<T*>(stack_); }
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
const T* Bottom() const { return reinterpret_cast<T*>(stack_); }
|
||||||
|
|
||||||
|
bool HasAllocator() const {
|
||||||
|
return allocator_ != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Allocator& GetAllocator() {
|
||||||
|
RAPIDJSON_ASSERT(allocator_);
|
||||||
|
return *allocator_;
|
||||||
|
}
|
||||||
|
|
||||||
Allocator& GetAllocator() { return *allocator_; }
|
|
||||||
bool Empty() const { return stackTop_ == stack_; }
|
bool Empty() const { return stackTop_ == stack_; }
|
||||||
size_t GetSize() const { return static_cast<size_t>(stackTop_ - stack_); }
|
size_t GetSize() const { return static_cast<size_t>(stackTop_ - stack_); }
|
||||||
size_t GetCapacity() const { return static_cast<size_t>(stackEnd_ - stack_); }
|
size_t GetCapacity() const { return static_cast<size_t>(stackEnd_ - stack_); }
|
||||||
|
|
@ -151,7 +198,7 @@ private:
|
||||||
|
|
||||||
void Resize(size_t newCapacity) {
|
void Resize(size_t newCapacity) {
|
||||||
const size_t size = GetSize(); // Backup the current size
|
const size_t size = GetSize(); // Backup the current size
|
||||||
stack_ = (char*)allocator_->Realloc(stack_, GetCapacity(), newCapacity);
|
stack_ = static_cast<char*>(allocator_->Realloc(stack_, GetCapacity(), newCapacity));
|
||||||
stackTop_ = stack_ + size;
|
stackTop_ = stack_ + size;
|
||||||
stackEnd_ = stack_ + newCapacity;
|
stackEnd_ = stack_ + newCapacity;
|
||||||
}
|
}
|
||||||
|
|
@ -176,4 +223,8 @@ private:
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
RAPIDJSON_NAMESPACE_END
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // RAPIDJSON_STACK_H_
|
#endif // RAPIDJSON_STACK_H_
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
#ifndef RAPIDJSON_INTERNAL_STRFUNC_H_
|
#ifndef RAPIDJSON_INTERNAL_STRFUNC_H_
|
||||||
#define RAPIDJSON_INTERNAL_STRFUNC_H_
|
#define RAPIDJSON_INTERNAL_STRFUNC_H_
|
||||||
|
|
||||||
#include "../rapidjson.h"
|
#include "../stream.h"
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
@ -33,6 +33,22 @@ inline SizeType StrLen(const Ch* s) {
|
||||||
return SizeType(p - s);
|
return SizeType(p - s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Returns number of code points in a encoded string.
|
||||||
|
template<typename Encoding>
|
||||||
|
bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
|
||||||
|
GenericStringStream<Encoding> is(s);
|
||||||
|
const typename Encoding::Ch* end = s + length;
|
||||||
|
SizeType count = 0;
|
||||||
|
while (is.src_ < end) {
|
||||||
|
unsigned codepoint;
|
||||||
|
if (!Encoding::Decode(is, &codepoint))
|
||||||
|
return false;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
*outCount = count;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
RAPIDJSON_NAMESPACE_END
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,6 @@
|
||||||
#ifndef RAPIDJSON_STRTOD_
|
#ifndef RAPIDJSON_STRTOD_
|
||||||
#define RAPIDJSON_STRTOD_
|
#define RAPIDJSON_STRTOD_
|
||||||
|
|
||||||
#include "../rapidjson.h"
|
|
||||||
#include "ieee754.h"
|
#include "ieee754.h"
|
||||||
#include "biginteger.h"
|
#include "biginteger.h"
|
||||||
#include "diyfp.h"
|
#include "diyfp.h"
|
||||||
|
|
@ -95,13 +94,13 @@ inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) {
|
||||||
hS_Exp2 -= common_Exp2;
|
hS_Exp2 -= common_Exp2;
|
||||||
|
|
||||||
BigInteger dS = d;
|
BigInteger dS = d;
|
||||||
dS.MultiplyPow5(dS_Exp5) <<= dS_Exp2;
|
dS.MultiplyPow5(static_cast<unsigned>(dS_Exp5)) <<= static_cast<unsigned>(dS_Exp2);
|
||||||
|
|
||||||
BigInteger bS(bInt);
|
BigInteger bS(bInt);
|
||||||
bS.MultiplyPow5(bS_Exp5) <<= bS_Exp2;
|
bS.MultiplyPow5(static_cast<unsigned>(bS_Exp5)) <<= static_cast<unsigned>(bS_Exp2);
|
||||||
|
|
||||||
BigInteger hS(1);
|
BigInteger hS(1);
|
||||||
hS.MultiplyPow5(hS_Exp5) <<= hS_Exp2;
|
hS.MultiplyPow5(static_cast<unsigned>(hS_Exp5)) <<= static_cast<unsigned>(hS_Exp2);
|
||||||
|
|
||||||
BigInteger delta(0);
|
BigInteger delta(0);
|
||||||
dS.Difference(bS, &delta);
|
dS.Difference(bS, &delta);
|
||||||
|
|
@ -134,7 +133,7 @@ inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit
|
||||||
if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) ||
|
if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) ||
|
||||||
(significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > '5'))
|
(significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > '5'))
|
||||||
break;
|
break;
|
||||||
significand = significand * 10 + (decimals[i] - '0');
|
significand = significand * 10u + static_cast<unsigned>(decimals[i] - '0');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < length && decimals[i] >= '5') // Rounding
|
if (i < length && decimals[i] >= '5') // Rounding
|
||||||
|
|
@ -143,13 +142,13 @@ inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit
|
||||||
size_t remaining = length - i;
|
size_t remaining = length - i;
|
||||||
const unsigned kUlpShift = 3;
|
const unsigned kUlpShift = 3;
|
||||||
const unsigned kUlp = 1 << kUlpShift;
|
const unsigned kUlp = 1 << kUlpShift;
|
||||||
int error = (remaining == 0) ? 0 : kUlp / 2;
|
int64_t error = (remaining == 0) ? 0 : kUlp / 2;
|
||||||
|
|
||||||
DiyFp v(significand, 0);
|
DiyFp v(significand, 0);
|
||||||
v = v.Normalize();
|
v = v.Normalize();
|
||||||
error <<= -v.e;
|
error <<= -v.e;
|
||||||
|
|
||||||
const int dExp = (int)decimalPosition - (int)i + exp;
|
const int dExp = static_cast<int>(decimalPosition) - static_cast<int>(i) + exp;
|
||||||
|
|
||||||
int actualExp;
|
int actualExp;
|
||||||
DiyFp cachedPower = GetCachedPower10(dExp, &actualExp);
|
DiyFp cachedPower = GetCachedPower10(dExp, &actualExp);
|
||||||
|
|
@ -166,7 +165,7 @@ inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit
|
||||||
int adjustment = dExp - actualExp - 1;
|
int adjustment = dExp - actualExp - 1;
|
||||||
RAPIDJSON_ASSERT(adjustment >= 0 && adjustment < 7);
|
RAPIDJSON_ASSERT(adjustment >= 0 && adjustment < 7);
|
||||||
v = v * kPow10[adjustment];
|
v = v * kPow10[adjustment];
|
||||||
if (length + adjustment > 19) // has more digits than decimal digits in 64-bit
|
if (length + static_cast<unsigned>(adjustment)> 19u) // has more digits than decimal digits in 64-bit
|
||||||
error += kUlp / 2;
|
error += kUlp / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -184,14 +183,14 @@ inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit
|
||||||
unsigned scaleExp = (precisionSize + kUlpShift) - 63;
|
unsigned scaleExp = (precisionSize + kUlpShift) - 63;
|
||||||
v.f >>= scaleExp;
|
v.f >>= scaleExp;
|
||||||
v.e += scaleExp;
|
v.e += scaleExp;
|
||||||
error = (error >> scaleExp) + 1 + kUlp;
|
error = (error >> scaleExp) + 1 + static_cast<int>(kUlp);
|
||||||
precisionSize -= scaleExp;
|
precisionSize -= scaleExp;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiyFp rounded(v.f >> precisionSize, v.e + precisionSize);
|
DiyFp rounded(v.f >> precisionSize, v.e + static_cast<int>(precisionSize));
|
||||||
const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
|
const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
|
||||||
const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
|
const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
|
||||||
if (precisionBits >= halfWay + error) {
|
if (precisionBits >= halfWay + static_cast<unsigned>(error)) {
|
||||||
rounded.f++;
|
rounded.f++;
|
||||||
if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340)
|
if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340)
|
||||||
rounded.f >>= 1;
|
rounded.f >>= 1;
|
||||||
|
|
@ -201,12 +200,12 @@ inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit
|
||||||
|
|
||||||
*result = rounded.ToDouble();
|
*result = rounded.ToDouble();
|
||||||
|
|
||||||
return halfWay - error >= precisionBits || precisionBits >= halfWay + error;
|
return halfWay - static_cast<unsigned>(error) >= precisionBits || precisionBits >= halfWay + static_cast<unsigned>(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline double StrtodBigInteger(double approx, const char* decimals, size_t length, size_t decimalPosition, int exp) {
|
inline double StrtodBigInteger(double approx, const char* decimals, size_t length, size_t decimalPosition, int exp) {
|
||||||
const BigInteger dInt(decimals, length);
|
const BigInteger dInt(decimals, length);
|
||||||
const int dExp = (int)decimalPosition - (int)length + exp;
|
const int dExp = static_cast<int>(decimalPosition) - static_cast<int>(length) + exp;
|
||||||
Double a(approx);
|
Double a(approx);
|
||||||
int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp);
|
int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp);
|
||||||
if (cmp < 0)
|
if (cmp < 0)
|
||||||
|
|
@ -246,10 +245,10 @@ inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t
|
||||||
|
|
||||||
// Trim right-most digits
|
// Trim right-most digits
|
||||||
const int kMaxDecimalDigit = 780;
|
const int kMaxDecimalDigit = 780;
|
||||||
if ((int)length > kMaxDecimalDigit) {
|
if (static_cast<int>(length) > kMaxDecimalDigit) {
|
||||||
int delta = (int(length) - kMaxDecimalDigit);
|
int delta = (static_cast<int>(length) - kMaxDecimalDigit);
|
||||||
exp += delta;
|
exp += delta;
|
||||||
decimalPosition -= delta;
|
decimalPosition -= static_cast<unsigned>(delta);
|
||||||
length = kMaxDecimalDigit;
|
length = kMaxDecimalDigit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
// 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_INTERNAL_SWAP_H_
|
||||||
|
#define RAPIDJSON_INTERNAL_SWAP_H_
|
||||||
|
|
||||||
|
#include "../rapidjson.h"
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(c++98-compat)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
//! Custom swap() to avoid dependency on C++ <algorithm> header
|
||||||
|
/*! \tparam T Type of the arguments to swap, should be instantiated with primitive C++ types only.
|
||||||
|
\note This has the same semantics as std::swap().
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
inline void Swap(T& a, T& b) RAPIDJSON_NOEXCEPT {
|
||||||
|
T tmp = a;
|
||||||
|
a = b;
|
||||||
|
b = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // RAPIDJSON_INTERNAL_SWAP_H_
|
||||||
|
|
@ -0,0 +1,115 @@
|
||||||
|
// 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_ISTREAMWRAPPER_H_
|
||||||
|
#define RAPIDJSON_ISTREAMWRAPPER_H_
|
||||||
|
|
||||||
|
#include "stream.h"
|
||||||
|
#include <iosfwd>
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(padded)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized
|
||||||
|
#endif
|
||||||
|
|
||||||
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
//! Wrapper of \c std::basic_istream into RapidJSON's Stream concept.
|
||||||
|
/*!
|
||||||
|
The classes can be wrapped including but not limited to:
|
||||||
|
|
||||||
|
- \c std::istringstream
|
||||||
|
- \c std::stringstream
|
||||||
|
- \c std::wistringstream
|
||||||
|
- \c std::wstringstream
|
||||||
|
- \c std::ifstream
|
||||||
|
- \c std::fstream
|
||||||
|
- \c std::wifstream
|
||||||
|
- \c std::wfstream
|
||||||
|
|
||||||
|
\tparam StreamType Class derived from \c std::basic_istream.
|
||||||
|
*/
|
||||||
|
|
||||||
|
template <typename StreamType>
|
||||||
|
class BasicIStreamWrapper {
|
||||||
|
public:
|
||||||
|
typedef typename StreamType::char_type Ch;
|
||||||
|
BasicIStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {}
|
||||||
|
|
||||||
|
Ch Peek() const {
|
||||||
|
typename StreamType::int_type c = stream_.peek();
|
||||||
|
return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast<Ch>(c) : '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
Ch Take() {
|
||||||
|
typename StreamType::int_type c = stream_.get();
|
||||||
|
if (RAPIDJSON_LIKELY(c != StreamType::traits_type::eof())) {
|
||||||
|
count_++;
|
||||||
|
return static_cast<Ch>(c);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
// tellg() may return -1 when failed. So we count by ourself.
|
||||||
|
size_t Tell() const { return count_; }
|
||||||
|
|
||||||
|
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
|
void Put(Ch) { RAPIDJSON_ASSERT(false); }
|
||||||
|
void Flush() { RAPIDJSON_ASSERT(false); }
|
||||||
|
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
|
|
||||||
|
// For encoding detection only.
|
||||||
|
const Ch* Peek4() const {
|
||||||
|
RAPIDJSON_ASSERT(sizeof(Ch) == 1); // Only usable for byte stream.
|
||||||
|
int i;
|
||||||
|
bool hasError = false;
|
||||||
|
for (i = 0; i < 4; ++i) {
|
||||||
|
typename StreamType::int_type c = stream_.get();
|
||||||
|
if (c == StreamType::traits_type::eof()) {
|
||||||
|
hasError = true;
|
||||||
|
stream_.clear();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
peekBuffer_[i] = static_cast<Ch>(c);
|
||||||
|
}
|
||||||
|
for (--i; i >= 0; --i)
|
||||||
|
stream_.putback(peekBuffer_[i]);
|
||||||
|
return !hasError ? peekBuffer_ : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
BasicIStreamWrapper(const BasicIStreamWrapper&);
|
||||||
|
BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
|
||||||
|
|
||||||
|
StreamType& stream_;
|
||||||
|
size_t count_; //!< Number of characters read. Note:
|
||||||
|
mutable Ch peekBuffer_[4];
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef BasicIStreamWrapper<std::istream> IStreamWrapper;
|
||||||
|
typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper;
|
||||||
|
|
||||||
|
#if defined(__clang__) || defined(_MSC_VER)
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
#endif // RAPIDJSON_ISTREAMWRAPPER_H_
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
#ifndef RAPIDJSON_MEMORYBUFFER_H_
|
#ifndef RAPIDJSON_MEMORYBUFFER_H_
|
||||||
#define RAPIDJSON_MEMORYBUFFER_H_
|
#define RAPIDJSON_MEMORYBUFFER_H_
|
||||||
|
|
||||||
#include "rapidjson.h"
|
#include "stream.h"
|
||||||
#include "internal/stack.h"
|
#include "internal/stack.h"
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,13 @@
|
||||||
#ifndef RAPIDJSON_MEMORYSTREAM_H_
|
#ifndef RAPIDJSON_MEMORYSTREAM_H_
|
||||||
#define RAPIDJSON_MEMORYSTREAM_H_
|
#define RAPIDJSON_MEMORYSTREAM_H_
|
||||||
|
|
||||||
#include "rapidjson.h"
|
#include "stream.h"
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(unreachable-code)
|
||||||
|
RAPIDJSON_DIAG_OFF(missing-noreturn)
|
||||||
|
#endif
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
|
@ -36,8 +42,8 @@ struct MemoryStream {
|
||||||
|
|
||||||
MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {}
|
MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {}
|
||||||
|
|
||||||
Ch Peek() const { return (src_ == end_) ? '\0' : *src_; }
|
Ch Peek() const { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_; }
|
||||||
Ch Take() { return (src_ == end_) ? '\0' : *src_++; }
|
Ch Take() { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_++; }
|
||||||
size_t Tell() const { return static_cast<size_t>(src_ - begin_); }
|
size_t Tell() const { return static_cast<size_t>(src_ - begin_); }
|
||||||
|
|
||||||
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
|
|
@ -58,4 +64,8 @@ struct MemoryStream {
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_END
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // RAPIDJSON_MEMORYBUFFER_H_
|
#endif // RAPIDJSON_MEMORYBUFFER_H_
|
||||||
|
|
|
||||||
|
|
@ -89,14 +89,14 @@
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
// For Visual Studio 6 in C++ mode and for many Visual Studio versions when
|
// For Visual Studio 6 in C++ mode and for many Visual Studio versions when
|
||||||
// compiling for ARM we should wrap <wchar.h> include with 'extern "C++" {}'
|
// compiling for ARM we have to wrap <wchar.h> include with 'extern "C++" {}'
|
||||||
// or compiler give many errors like this:
|
// or compiler would give many errors like this:
|
||||||
// error C2733: second C linkage of overloaded function 'wmemchr' not allowed
|
// error C2733: second C linkage of overloaded function 'wmemchr' not allowed
|
||||||
#ifdef __cplusplus
|
#if defined(__cplusplus) && !defined(_M_ARM)
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
# include <wchar.h>
|
# include <wchar.h>
|
||||||
#ifdef __cplusplus
|
#if defined(__cplusplus) && !defined(_M_ARM)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,81 @@
|
||||||
|
// 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_OSTREAMWRAPPER_H_
|
||||||
|
#define RAPIDJSON_OSTREAMWRAPPER_H_
|
||||||
|
|
||||||
|
#include "stream.h"
|
||||||
|
#include <iosfwd>
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(padded)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
//! Wrapper of \c std::basic_ostream into RapidJSON's Stream concept.
|
||||||
|
/*!
|
||||||
|
The classes can be wrapped including but not limited to:
|
||||||
|
|
||||||
|
- \c std::ostringstream
|
||||||
|
- \c std::stringstream
|
||||||
|
- \c std::wpstringstream
|
||||||
|
- \c std::wstringstream
|
||||||
|
- \c std::ifstream
|
||||||
|
- \c std::fstream
|
||||||
|
- \c std::wofstream
|
||||||
|
- \c std::wfstream
|
||||||
|
|
||||||
|
\tparam StreamType Class derived from \c std::basic_ostream.
|
||||||
|
*/
|
||||||
|
|
||||||
|
template <typename StreamType>
|
||||||
|
class BasicOStreamWrapper {
|
||||||
|
public:
|
||||||
|
typedef typename StreamType::char_type Ch;
|
||||||
|
BasicOStreamWrapper(StreamType& stream) : stream_(stream) {}
|
||||||
|
|
||||||
|
void Put(Ch c) {
|
||||||
|
stream_.put(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Flush() {
|
||||||
|
stream_.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not implemented
|
||||||
|
char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
|
char Take() { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
|
size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
|
char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
|
size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
BasicOStreamWrapper(const BasicOStreamWrapper&);
|
||||||
|
BasicOStreamWrapper& operator=(const BasicOStreamWrapper&);
|
||||||
|
|
||||||
|
StreamType& stream_;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef BasicOStreamWrapper<std::ostream> OStreamWrapper;
|
||||||
|
typedef BasicOStreamWrapper<std::wostream> WOStreamWrapper;
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
#endif // RAPIDJSON_OSTREAMWRAPPER_H_
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -24,6 +24,14 @@ RAPIDJSON_DIAG_OFF(effc++)
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
//! Combination of PrettyWriter format flags.
|
||||||
|
/*! \see PrettyWriter::SetFormatOptions
|
||||||
|
*/
|
||||||
|
enum PrettyFormatOptions {
|
||||||
|
kFormatDefault = 0, //!< Default pretty formatting.
|
||||||
|
kFormatSingleLineArray = 1 //!< Format arrays on a single line.
|
||||||
|
};
|
||||||
|
|
||||||
//! Writer with indentation and spacing.
|
//! Writer with indentation and spacing.
|
||||||
/*!
|
/*!
|
||||||
\tparam OutputStream Type of ouptut os.
|
\tparam OutputStream Type of ouptut os.
|
||||||
|
|
@ -31,8 +39,8 @@ RAPIDJSON_NAMESPACE_BEGIN
|
||||||
\tparam TargetEncoding Encoding of output stream.
|
\tparam TargetEncoding Encoding of output stream.
|
||||||
\tparam StackAllocator Type of allocator for allocating memory of stack.
|
\tparam StackAllocator Type of allocator for allocating memory of stack.
|
||||||
*/
|
*/
|
||||||
template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
|
template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
|
||||||
class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator> {
|
class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags> {
|
||||||
public:
|
public:
|
||||||
typedef Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator> Base;
|
typedef Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator> Base;
|
||||||
typedef typename Base::Ch Ch;
|
typedef typename Base::Ch Ch;
|
||||||
|
|
@ -42,8 +50,12 @@ public:
|
||||||
\param allocator User supplied allocator. If it is null, it will create a private one.
|
\param allocator User supplied allocator. If it is null, it will create a private one.
|
||||||
\param levelDepth Initial capacity of stack.
|
\param levelDepth Initial capacity of stack.
|
||||||
*/
|
*/
|
||||||
PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
|
explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
|
||||||
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
|
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {}
|
||||||
|
|
||||||
|
|
||||||
|
explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
|
||||||
|
Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
|
||||||
|
|
||||||
//! Set custom indentation.
|
//! Set custom indentation.
|
||||||
/*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r').
|
/*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r').
|
||||||
|
|
@ -57,6 +69,14 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Set pretty writer formatting options.
|
||||||
|
/*! \param options Formatting options.
|
||||||
|
*/
|
||||||
|
PrettyWriter& SetFormatOptions(PrettyFormatOptions options) {
|
||||||
|
formatOptions_ = options;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/*! @name Implementation of Handler
|
/*! @name Implementation of Handler
|
||||||
\see Handler
|
\see Handler
|
||||||
*/
|
*/
|
||||||
|
|
@ -70,6 +90,12 @@ public:
|
||||||
bool Uint64(uint64_t u64) { PrettyPrefix(kNumberType); return Base::WriteUint64(u64); }
|
bool Uint64(uint64_t u64) { PrettyPrefix(kNumberType); return Base::WriteUint64(u64); }
|
||||||
bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); }
|
bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); }
|
||||||
|
|
||||||
|
bool RawNumber(const Ch* str, SizeType length, bool copy = false) {
|
||||||
|
(void)copy;
|
||||||
|
PrettyPrefix(kNumberType);
|
||||||
|
return Base::WriteString(str, length);
|
||||||
|
}
|
||||||
|
|
||||||
bool String(const Ch* str, SizeType length, bool copy = false) {
|
bool String(const Ch* str, SizeType length, bool copy = false) {
|
||||||
(void)copy;
|
(void)copy;
|
||||||
PrettyPrefix(kStringType);
|
PrettyPrefix(kStringType);
|
||||||
|
|
@ -90,6 +116,12 @@ public:
|
||||||
|
|
||||||
bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }
|
bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }
|
||||||
|
|
||||||
|
#if RAPIDJSON_HAS_STDSTRING
|
||||||
|
bool Key(const std::basic_string<Ch>& str) {
|
||||||
|
return Key(str.data(), SizeType(str.size()));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
bool EndObject(SizeType memberCount = 0) {
|
bool EndObject(SizeType memberCount = 0) {
|
||||||
(void)memberCount;
|
(void)memberCount;
|
||||||
RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
|
RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
|
||||||
|
|
@ -120,7 +152,7 @@ public:
|
||||||
RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
|
RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
|
||||||
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
|
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
|
||||||
|
|
||||||
if (!empty) {
|
if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
|
||||||
Base::os_->Put('\n');
|
Base::os_->Put('\n');
|
||||||
WriteIndent();
|
WriteIndent();
|
||||||
}
|
}
|
||||||
|
|
@ -142,6 +174,18 @@ public:
|
||||||
bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
|
bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
|
||||||
|
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
|
//! Write a raw JSON value.
|
||||||
|
/*!
|
||||||
|
For user to write a stringified JSON as a value.
|
||||||
|
|
||||||
|
\param json A well-formed JSON value. It should not contain null character within [0, length - 1] range.
|
||||||
|
\param length Length of the json.
|
||||||
|
\param type Type of the root of json.
|
||||||
|
\note When using PrettyWriter::RawValue(), the result json may not be indented correctly.
|
||||||
|
*/
|
||||||
|
bool RawValue(const Ch* json, size_t length, Type type) { PrettyPrefix(type); return Base::WriteRawValue(json, length); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void PrettyPrefix(Type type) {
|
void PrettyPrefix(Type type) {
|
||||||
(void)type;
|
(void)type;
|
||||||
|
|
@ -151,12 +195,15 @@ protected:
|
||||||
if (level->inArray) {
|
if (level->inArray) {
|
||||||
if (level->valueCount > 0) {
|
if (level->valueCount > 0) {
|
||||||
Base::os_->Put(','); // add comma if it is not the first element in array
|
Base::os_->Put(','); // add comma if it is not the first element in array
|
||||||
Base::os_->Put('\n');
|
if (formatOptions_ & kFormatSingleLineArray)
|
||||||
|
Base::os_->Put(' ');
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
if (!(formatOptions_ & kFormatSingleLineArray)) {
|
||||||
Base::os_->Put('\n');
|
Base::os_->Put('\n');
|
||||||
WriteIndent();
|
WriteIndent();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else { // in object
|
else { // in object
|
||||||
if (level->valueCount > 0) {
|
if (level->valueCount > 0) {
|
||||||
if (level->valueCount % 2 == 0) {
|
if (level->valueCount % 2 == 0) {
|
||||||
|
|
@ -186,11 +233,12 @@ protected:
|
||||||
|
|
||||||
void WriteIndent() {
|
void WriteIndent() {
|
||||||
size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
|
size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
|
||||||
PutN(*Base::os_, indentChar_, count);
|
PutN(*Base::os_, static_cast<typename TargetEncoding::Ch>(indentChar_), count);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ch indentChar_;
|
Ch indentChar_;
|
||||||
unsigned indentCharCount_;
|
unsigned indentCharCount_;
|
||||||
|
PrettyFormatOptions formatOptions_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Prohibit copy constructor & assignment operator.
|
// Prohibit copy constructor & assignment operator.
|
||||||
|
|
|
||||||
|
|
@ -68,8 +68,8 @@
|
||||||
\brief Version of RapidJSON in "<major>.<minor>.<patch>" string format.
|
\brief Version of RapidJSON in "<major>.<minor>.<patch>" string format.
|
||||||
*/
|
*/
|
||||||
#define RAPIDJSON_MAJOR_VERSION 1
|
#define RAPIDJSON_MAJOR_VERSION 1
|
||||||
#define RAPIDJSON_MINOR_VERSION 0
|
#define RAPIDJSON_MINOR_VERSION 1
|
||||||
#define RAPIDJSON_PATCH_VERSION 2
|
#define RAPIDJSON_PATCH_VERSION 0
|
||||||
#define RAPIDJSON_VERSION_STRING \
|
#define RAPIDJSON_VERSION_STRING \
|
||||||
RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION)
|
RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION)
|
||||||
|
|
||||||
|
|
@ -119,6 +119,31 @@
|
||||||
#define RAPIDJSON_NAMESPACE_END }
|
#define RAPIDJSON_NAMESPACE_END }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// RAPIDJSON_HAS_STDSTRING
|
||||||
|
|
||||||
|
#ifndef RAPIDJSON_HAS_STDSTRING
|
||||||
|
#ifdef RAPIDJSON_DOXYGEN_RUNNING
|
||||||
|
#define RAPIDJSON_HAS_STDSTRING 1 // force generation of documentation
|
||||||
|
#else
|
||||||
|
#define RAPIDJSON_HAS_STDSTRING 0 // no std::string support by default
|
||||||
|
#endif
|
||||||
|
/*! \def RAPIDJSON_HAS_STDSTRING
|
||||||
|
\ingroup RAPIDJSON_CONFIG
|
||||||
|
\brief Enable RapidJSON support for \c std::string
|
||||||
|
|
||||||
|
By defining this preprocessor symbol to \c 1, several convenience functions for using
|
||||||
|
\ref rapidjson::GenericValue with \c std::string are enabled, especially
|
||||||
|
for construction and comparison.
|
||||||
|
|
||||||
|
\hideinitializer
|
||||||
|
*/
|
||||||
|
#endif // !defined(RAPIDJSON_HAS_STDSTRING)
|
||||||
|
|
||||||
|
#if RAPIDJSON_HAS_STDSTRING
|
||||||
|
#include <string>
|
||||||
|
#endif // RAPIDJSON_HAS_STDSTRING
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// RAPIDJSON_NO_INT64DEFINE
|
// RAPIDJSON_NO_INT64DEFINE
|
||||||
|
|
||||||
|
|
@ -134,7 +159,7 @@
|
||||||
*/
|
*/
|
||||||
#ifndef RAPIDJSON_NO_INT64DEFINE
|
#ifndef RAPIDJSON_NO_INT64DEFINE
|
||||||
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
|
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
|
||||||
#ifdef _MSC_VER
|
#if defined(_MSC_VER) && (_MSC_VER < 1800) // Visual Studio 2013
|
||||||
#include "msinttypes/stdint.h"
|
#include "msinttypes/stdint.h"
|
||||||
#include "msinttypes/inttypes.h"
|
#include "msinttypes/inttypes.h"
|
||||||
#else
|
#else
|
||||||
|
|
@ -153,9 +178,9 @@
|
||||||
|
|
||||||
#ifndef RAPIDJSON_FORCEINLINE
|
#ifndef RAPIDJSON_FORCEINLINE
|
||||||
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
|
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
|
||||||
#if defined(_MSC_VER) && !defined(NDEBUG)
|
#if defined(_MSC_VER) && defined(NDEBUG)
|
||||||
#define RAPIDJSON_FORCEINLINE __forceinline
|
#define RAPIDJSON_FORCEINLINE __forceinline
|
||||||
#elif defined(__GNUC__) && __GNUC__ >= 4 && !defined(NDEBUG)
|
#elif defined(__GNUC__) && __GNUC__ >= 4 && defined(NDEBUG)
|
||||||
#define RAPIDJSON_FORCEINLINE __attribute__((always_inline))
|
#define RAPIDJSON_FORCEINLINE __attribute__((always_inline))
|
||||||
#else
|
#else
|
||||||
#define RAPIDJSON_FORCEINLINE
|
#define RAPIDJSON_FORCEINLINE
|
||||||
|
|
@ -209,7 +234,9 @@
|
||||||
// Detect with architecture macros
|
// Detect with architecture macros
|
||||||
# elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
|
# elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
|
||||||
# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
|
# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
|
||||||
# elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__bfin__) || defined(_M_ARM)
|
# elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__bfin__)
|
||||||
|
# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
|
||||||
|
# elif defined(_MSC_VER) && defined(_M_ARM)
|
||||||
# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
|
# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
|
||||||
# elif defined(RAPIDJSON_DOXYGEN_RUNNING)
|
# elif defined(RAPIDJSON_DOXYGEN_RUNNING)
|
||||||
# define RAPIDJSON_ENDIAN
|
# define RAPIDJSON_ENDIAN
|
||||||
|
|
@ -223,7 +250,7 @@
|
||||||
|
|
||||||
//! Whether using 64-bit architecture
|
//! Whether using 64-bit architecture
|
||||||
#ifndef RAPIDJSON_64BIT
|
#ifndef RAPIDJSON_64BIT
|
||||||
#if defined(__LP64__) || defined(_WIN64)
|
#if defined(__LP64__) || (defined(__x86_64__) && defined(__ILP32__)) || defined(_WIN64) || defined(__EMSCRIPTEN__)
|
||||||
#define RAPIDJSON_64BIT 1
|
#define RAPIDJSON_64BIT 1
|
||||||
#else
|
#else
|
||||||
#define RAPIDJSON_64BIT 0
|
#define RAPIDJSON_64BIT 0
|
||||||
|
|
@ -238,13 +265,14 @@
|
||||||
\param x pointer to align
|
\param x pointer to align
|
||||||
|
|
||||||
Some machines require strict data alignment. Currently the default uses 4 bytes
|
Some machines require strict data alignment. Currently the default uses 4 bytes
|
||||||
alignment. User can customize by defining the RAPIDJSON_ALIGN function macro.,
|
alignment on 32-bit platforms and 8 bytes alignment for 64-bit platforms.
|
||||||
|
User can customize by defining the RAPIDJSON_ALIGN function macro.
|
||||||
*/
|
*/
|
||||||
#ifndef RAPIDJSON_ALIGN
|
#ifndef RAPIDJSON_ALIGN
|
||||||
#if RAPIDJSON_64BIT == 1
|
#if RAPIDJSON_64BIT == 1
|
||||||
#define RAPIDJSON_ALIGN(x) ((x + 7u) & ~7u)
|
#define RAPIDJSON_ALIGN(x) (((x) + static_cast<uint64_t>(7u)) & ~static_cast<uint64_t>(7u))
|
||||||
#else
|
#else
|
||||||
#define RAPIDJSON_ALIGN(x) ((x + 3u) & ~3u)
|
#define RAPIDJSON_ALIGN(x) (((x) + 3u) & ~3u)
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -261,6 +289,36 @@
|
||||||
#define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast<uint64_t>(high32) << 32) | static_cast<uint64_t>(low32))
|
#define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast<uint64_t>(high32) << 32) | static_cast<uint64_t>(low32))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// RAPIDJSON_48BITPOINTER_OPTIMIZATION
|
||||||
|
|
||||||
|
//! Use only lower 48-bit address for some pointers.
|
||||||
|
/*!
|
||||||
|
\ingroup RAPIDJSON_CONFIG
|
||||||
|
|
||||||
|
This optimization uses the fact that current X86-64 architecture only implement lower 48-bit virtual address.
|
||||||
|
The higher 16-bit can be used for storing other data.
|
||||||
|
\c GenericValue uses this optimization to reduce its size form 24 bytes to 16 bytes in 64-bit architecture.
|
||||||
|
*/
|
||||||
|
#ifndef RAPIDJSON_48BITPOINTER_OPTIMIZATION
|
||||||
|
#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
|
||||||
|
#define RAPIDJSON_48BITPOINTER_OPTIMIZATION 1
|
||||||
|
#else
|
||||||
|
#define RAPIDJSON_48BITPOINTER_OPTIMIZATION 0
|
||||||
|
#endif
|
||||||
|
#endif // RAPIDJSON_48BITPOINTER_OPTIMIZATION
|
||||||
|
|
||||||
|
#if RAPIDJSON_48BITPOINTER_OPTIMIZATION == 1
|
||||||
|
#if RAPIDJSON_64BIT != 1
|
||||||
|
#error RAPIDJSON_48BITPOINTER_OPTIMIZATION can only be set to 1 when RAPIDJSON_64BIT=1
|
||||||
|
#endif
|
||||||
|
#define RAPIDJSON_SETPOINTER(type, p, x) (p = reinterpret_cast<type *>((reinterpret_cast<uintptr_t>(p) & static_cast<uintptr_t>(RAPIDJSON_UINT64_C2(0xFFFF0000, 0x00000000))) | reinterpret_cast<uintptr_t>(reinterpret_cast<const void*>(x))))
|
||||||
|
#define RAPIDJSON_GETPOINTER(type, p) (reinterpret_cast<type *>(reinterpret_cast<uintptr_t>(p) & static_cast<uintptr_t>(RAPIDJSON_UINT64_C2(0x0000FFFF, 0xFFFFFFFF))))
|
||||||
|
#else
|
||||||
|
#define RAPIDJSON_SETPOINTER(type, p, x) (p = (x))
|
||||||
|
#define RAPIDJSON_GETPOINTER(type, p) (p)
|
||||||
|
#endif
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD
|
// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD
|
||||||
|
|
||||||
|
|
@ -349,7 +407,9 @@ RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
// Adopt from boost
|
// Adopt from boost
|
||||||
#ifndef RAPIDJSON_STATIC_ASSERT
|
#ifndef RAPIDJSON_STATIC_ASSERT
|
||||||
|
#ifndef __clang__
|
||||||
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
|
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
|
||||||
|
#endif
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
template <bool x> struct STATIC_ASSERTION_FAILURE;
|
template <bool x> struct STATIC_ASSERTION_FAILURE;
|
||||||
template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
|
template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
|
||||||
|
|
@ -365,7 +425,9 @@ RAPIDJSON_NAMESPACE_END
|
||||||
#else
|
#else
|
||||||
#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
|
#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef __clang__
|
||||||
//!@endcond
|
//!@endcond
|
||||||
|
#endif
|
||||||
|
|
||||||
/*! \def RAPIDJSON_STATIC_ASSERT
|
/*! \def RAPIDJSON_STATIC_ASSERT
|
||||||
\brief (Internal) macro to check for conditions at compile-time
|
\brief (Internal) macro to check for conditions at compile-time
|
||||||
|
|
@ -378,6 +440,35 @@ RAPIDJSON_NAMESPACE_END
|
||||||
RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
|
RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// RAPIDJSON_LIKELY, RAPIDJSON_UNLIKELY
|
||||||
|
|
||||||
|
//! Compiler branching hint for expression with high probability to be true.
|
||||||
|
/*!
|
||||||
|
\ingroup RAPIDJSON_CONFIG
|
||||||
|
\param x Boolean expression likely to be true.
|
||||||
|
*/
|
||||||
|
#ifndef RAPIDJSON_LIKELY
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
#define RAPIDJSON_LIKELY(x) __builtin_expect(!!(x), 1)
|
||||||
|
#else
|
||||||
|
#define RAPIDJSON_LIKELY(x) (x)
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//! Compiler branching hint for expression with low probability to be true.
|
||||||
|
/*!
|
||||||
|
\ingroup RAPIDJSON_CONFIG
|
||||||
|
\param x Boolean expression unlikely to be true.
|
||||||
|
*/
|
||||||
|
#ifndef RAPIDJSON_UNLIKELY
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
#define RAPIDJSON_UNLIKELY(x) __builtin_expect(!!(x), 0)
|
||||||
|
#else
|
||||||
|
#define RAPIDJSON_UNLIKELY(x) (x)
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// Helpers
|
// Helpers
|
||||||
|
|
||||||
|
|
@ -438,8 +529,12 @@ RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
#ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS
|
#ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS
|
||||||
#if defined(__clang__)
|
#if defined(__clang__)
|
||||||
#define RAPIDJSON_HAS_CXX11_RVALUE_REFS __has_feature(cxx_rvalue_references) && \
|
#if __has_feature(cxx_rvalue_references) && \
|
||||||
(defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306)
|
(defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306)
|
||||||
|
#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1
|
||||||
|
#else
|
||||||
|
#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0
|
||||||
|
#endif
|
||||||
#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
|
#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
|
||||||
(defined(_MSC_VER) && _MSC_VER >= 1600)
|
(defined(_MSC_VER) && _MSC_VER >= 1600)
|
||||||
|
|
||||||
|
|
@ -470,6 +565,17 @@ RAPIDJSON_NAMESPACE_END
|
||||||
#define RAPIDJSON_HAS_CXX11_TYPETRAITS 0
|
#define RAPIDJSON_HAS_CXX11_TYPETRAITS 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef RAPIDJSON_HAS_CXX11_RANGE_FOR
|
||||||
|
#if defined(__clang__)
|
||||||
|
#define RAPIDJSON_HAS_CXX11_RANGE_FOR __has_feature(cxx_range_for)
|
||||||
|
#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
|
||||||
|
(defined(_MSC_VER) && _MSC_VER >= 1700)
|
||||||
|
#define RAPIDJSON_HAS_CXX11_RANGE_FOR 1
|
||||||
|
#else
|
||||||
|
#define RAPIDJSON_HAS_CXX11_RANGE_FOR 0
|
||||||
|
#endif
|
||||||
|
#endif // RAPIDJSON_HAS_CXX11_RANGE_FOR
|
||||||
|
|
||||||
//!@endcond
|
//!@endcond
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
@ -485,10 +591,7 @@ RAPIDJSON_NAMESPACE_END
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// Allocators and Encodings
|
// Type
|
||||||
|
|
||||||
#include "allocators.h"
|
|
||||||
#include "encodings.h"
|
|
||||||
|
|
||||||
/*! \namespace rapidjson
|
/*! \namespace rapidjson
|
||||||
\brief main RapidJSON namespace
|
\brief main RapidJSON namespace
|
||||||
|
|
@ -496,148 +599,6 @@ RAPIDJSON_NAMESPACE_END
|
||||||
*/
|
*/
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Stream
|
|
||||||
|
|
||||||
/*! \class rapidjson::Stream
|
|
||||||
\brief Concept for reading and writing characters.
|
|
||||||
|
|
||||||
For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd().
|
|
||||||
|
|
||||||
For write-only stream, only need to implement Put() and Flush().
|
|
||||||
|
|
||||||
\code
|
|
||||||
concept Stream {
|
|
||||||
typename Ch; //!< Character type of the stream.
|
|
||||||
|
|
||||||
//! Read the current character from stream without moving the read cursor.
|
|
||||||
Ch Peek() const;
|
|
||||||
|
|
||||||
//! Read the current character from stream and moving the read cursor to next character.
|
|
||||||
Ch Take();
|
|
||||||
|
|
||||||
//! Get the current read cursor.
|
|
||||||
//! \return Number of characters read from start.
|
|
||||||
size_t Tell();
|
|
||||||
|
|
||||||
//! Begin writing operation at the current read pointer.
|
|
||||||
//! \return The begin writer pointer.
|
|
||||||
Ch* PutBegin();
|
|
||||||
|
|
||||||
//! Write a character.
|
|
||||||
void Put(Ch c);
|
|
||||||
|
|
||||||
//! Flush the buffer.
|
|
||||||
void Flush();
|
|
||||||
|
|
||||||
//! End the writing operation.
|
|
||||||
//! \param begin The begin write pointer returned by PutBegin().
|
|
||||||
//! \return Number of characters written.
|
|
||||||
size_t PutEnd(Ch* begin);
|
|
||||||
}
|
|
||||||
\endcode
|
|
||||||
*/
|
|
||||||
|
|
||||||
//! Provides additional information for stream.
|
|
||||||
/*!
|
|
||||||
By using traits pattern, this type provides a default configuration for stream.
|
|
||||||
For custom stream, this type can be specialized for other configuration.
|
|
||||||
See TEST(Reader, CustomStringStream) in readertest.cpp for example.
|
|
||||||
*/
|
|
||||||
template<typename Stream>
|
|
||||||
struct StreamTraits {
|
|
||||||
//! Whether to make local copy of stream for optimization during parsing.
|
|
||||||
/*!
|
|
||||||
By default, for safety, streams do not use local copy optimization.
|
|
||||||
Stream that can be copied fast should specialize this, like StreamTraits<StringStream>.
|
|
||||||
*/
|
|
||||||
enum { copyOptimization = 0 };
|
|
||||||
};
|
|
||||||
|
|
||||||
//! Put N copies of a character to a stream.
|
|
||||||
template<typename Stream, typename Ch>
|
|
||||||
inline void PutN(Stream& stream, Ch c, size_t n) {
|
|
||||||
for (size_t i = 0; i < n; i++)
|
|
||||||
stream.Put(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
// StringStream
|
|
||||||
|
|
||||||
//! Read-only string stream.
|
|
||||||
/*! \note implements Stream concept
|
|
||||||
*/
|
|
||||||
template <typename Encoding>
|
|
||||||
struct GenericStringStream {
|
|
||||||
typedef typename Encoding::Ch Ch;
|
|
||||||
|
|
||||||
GenericStringStream(const Ch *src) : src_(src), head_(src) {}
|
|
||||||
|
|
||||||
Ch Peek() const { return *src_; }
|
|
||||||
Ch Take() { return *src_++; }
|
|
||||||
size_t Tell() const { return static_cast<size_t>(src_ - head_); }
|
|
||||||
|
|
||||||
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
|
||||||
void Put(Ch) { RAPIDJSON_ASSERT(false); }
|
|
||||||
void Flush() { RAPIDJSON_ASSERT(false); }
|
|
||||||
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
|
|
||||||
|
|
||||||
const Ch* src_; //!< Current read position.
|
|
||||||
const Ch* head_; //!< Original head of the string.
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename Encoding>
|
|
||||||
struct StreamTraits<GenericStringStream<Encoding> > {
|
|
||||||
enum { copyOptimization = 1 };
|
|
||||||
};
|
|
||||||
|
|
||||||
//! String stream with UTF8 encoding.
|
|
||||||
typedef GenericStringStream<UTF8<> > StringStream;
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
// InsituStringStream
|
|
||||||
|
|
||||||
//! A read-write string stream.
|
|
||||||
/*! This string stream is particularly designed for in-situ parsing.
|
|
||||||
\note implements Stream concept
|
|
||||||
*/
|
|
||||||
template <typename Encoding>
|
|
||||||
struct GenericInsituStringStream {
|
|
||||||
typedef typename Encoding::Ch Ch;
|
|
||||||
|
|
||||||
GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {}
|
|
||||||
|
|
||||||
// Read
|
|
||||||
Ch Peek() { return *src_; }
|
|
||||||
Ch Take() { return *src_++; }
|
|
||||||
size_t Tell() { return static_cast<size_t>(src_ - head_); }
|
|
||||||
|
|
||||||
// Write
|
|
||||||
void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; }
|
|
||||||
|
|
||||||
Ch* PutBegin() { return dst_ = src_; }
|
|
||||||
size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); }
|
|
||||||
void Flush() {}
|
|
||||||
|
|
||||||
Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; }
|
|
||||||
void Pop(size_t count) { dst_ -= count; }
|
|
||||||
|
|
||||||
Ch* src_;
|
|
||||||
Ch* dst_;
|
|
||||||
Ch* head_;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename Encoding>
|
|
||||||
struct StreamTraits<GenericInsituStringStream<Encoding> > {
|
|
||||||
enum { copyOptimization = 1 };
|
|
||||||
};
|
|
||||||
|
|
||||||
//! Insitu string stream with UTF8 encoding.
|
|
||||||
typedef GenericInsituStringStream<UTF8<> > InsituStringStream;
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Type
|
|
||||||
|
|
||||||
//! Type of JSON value
|
//! Type of JSON value
|
||||||
enum Type {
|
enum Type {
|
||||||
kNullType = 0, //!< null
|
kNullType = 0, //!< null
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,179 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
#include "rapidjson.h"
|
||||||
|
|
||||||
|
#ifndef RAPIDJSON_STREAM_H_
|
||||||
|
#define RAPIDJSON_STREAM_H_
|
||||||
|
|
||||||
|
#include "encodings.h"
|
||||||
|
|
||||||
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Stream
|
||||||
|
|
||||||
|
/*! \class rapidjson::Stream
|
||||||
|
\brief Concept for reading and writing characters.
|
||||||
|
|
||||||
|
For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd().
|
||||||
|
|
||||||
|
For write-only stream, only need to implement Put() and Flush().
|
||||||
|
|
||||||
|
\code
|
||||||
|
concept Stream {
|
||||||
|
typename Ch; //!< Character type of the stream.
|
||||||
|
|
||||||
|
//! Read the current character from stream without moving the read cursor.
|
||||||
|
Ch Peek() const;
|
||||||
|
|
||||||
|
//! Read the current character from stream and moving the read cursor to next character.
|
||||||
|
Ch Take();
|
||||||
|
|
||||||
|
//! Get the current read cursor.
|
||||||
|
//! \return Number of characters read from start.
|
||||||
|
size_t Tell();
|
||||||
|
|
||||||
|
//! Begin writing operation at the current read pointer.
|
||||||
|
//! \return The begin writer pointer.
|
||||||
|
Ch* PutBegin();
|
||||||
|
|
||||||
|
//! Write a character.
|
||||||
|
void Put(Ch c);
|
||||||
|
|
||||||
|
//! Flush the buffer.
|
||||||
|
void Flush();
|
||||||
|
|
||||||
|
//! End the writing operation.
|
||||||
|
//! \param begin The begin write pointer returned by PutBegin().
|
||||||
|
//! \return Number of characters written.
|
||||||
|
size_t PutEnd(Ch* begin);
|
||||||
|
}
|
||||||
|
\endcode
|
||||||
|
*/
|
||||||
|
|
||||||
|
//! Provides additional information for stream.
|
||||||
|
/*!
|
||||||
|
By using traits pattern, this type provides a default configuration for stream.
|
||||||
|
For custom stream, this type can be specialized for other configuration.
|
||||||
|
See TEST(Reader, CustomStringStream) in readertest.cpp for example.
|
||||||
|
*/
|
||||||
|
template<typename Stream>
|
||||||
|
struct StreamTraits {
|
||||||
|
//! Whether to make local copy of stream for optimization during parsing.
|
||||||
|
/*!
|
||||||
|
By default, for safety, streams do not use local copy optimization.
|
||||||
|
Stream that can be copied fast should specialize this, like StreamTraits<StringStream>.
|
||||||
|
*/
|
||||||
|
enum { copyOptimization = 0 };
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Reserve n characters for writing to a stream.
|
||||||
|
template<typename Stream>
|
||||||
|
inline void PutReserve(Stream& stream, size_t count) {
|
||||||
|
(void)stream;
|
||||||
|
(void)count;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Write character to a stream, presuming buffer is reserved.
|
||||||
|
template<typename Stream>
|
||||||
|
inline void PutUnsafe(Stream& stream, typename Stream::Ch c) {
|
||||||
|
stream.Put(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Put N copies of a character to a stream.
|
||||||
|
template<typename Stream, typename Ch>
|
||||||
|
inline void PutN(Stream& stream, Ch c, size_t n) {
|
||||||
|
PutReserve(stream, n);
|
||||||
|
for (size_t i = 0; i < n; i++)
|
||||||
|
PutUnsafe(stream, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// StringStream
|
||||||
|
|
||||||
|
//! Read-only string stream.
|
||||||
|
/*! \note implements Stream concept
|
||||||
|
*/
|
||||||
|
template <typename Encoding>
|
||||||
|
struct GenericStringStream {
|
||||||
|
typedef typename Encoding::Ch Ch;
|
||||||
|
|
||||||
|
GenericStringStream(const Ch *src) : src_(src), head_(src) {}
|
||||||
|
|
||||||
|
Ch Peek() const { return *src_; }
|
||||||
|
Ch Take() { return *src_++; }
|
||||||
|
size_t Tell() const { return static_cast<size_t>(src_ - head_); }
|
||||||
|
|
||||||
|
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
|
void Put(Ch) { RAPIDJSON_ASSERT(false); }
|
||||||
|
void Flush() { RAPIDJSON_ASSERT(false); }
|
||||||
|
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
|
||||||
|
|
||||||
|
const Ch* src_; //!< Current read position.
|
||||||
|
const Ch* head_; //!< Original head of the string.
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Encoding>
|
||||||
|
struct StreamTraits<GenericStringStream<Encoding> > {
|
||||||
|
enum { copyOptimization = 1 };
|
||||||
|
};
|
||||||
|
|
||||||
|
//! String stream with UTF8 encoding.
|
||||||
|
typedef GenericStringStream<UTF8<> > StringStream;
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// InsituStringStream
|
||||||
|
|
||||||
|
//! A read-write string stream.
|
||||||
|
/*! This string stream is particularly designed for in-situ parsing.
|
||||||
|
\note implements Stream concept
|
||||||
|
*/
|
||||||
|
template <typename Encoding>
|
||||||
|
struct GenericInsituStringStream {
|
||||||
|
typedef typename Encoding::Ch Ch;
|
||||||
|
|
||||||
|
GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {}
|
||||||
|
|
||||||
|
// Read
|
||||||
|
Ch Peek() { return *src_; }
|
||||||
|
Ch Take() { return *src_++; }
|
||||||
|
size_t Tell() { return static_cast<size_t>(src_ - head_); }
|
||||||
|
|
||||||
|
// Write
|
||||||
|
void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; }
|
||||||
|
|
||||||
|
Ch* PutBegin() { return dst_ = src_; }
|
||||||
|
size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); }
|
||||||
|
void Flush() {}
|
||||||
|
|
||||||
|
Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; }
|
||||||
|
void Pop(size_t count) { dst_ -= count; }
|
||||||
|
|
||||||
|
Ch* src_;
|
||||||
|
Ch* dst_;
|
||||||
|
Ch* head_;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Encoding>
|
||||||
|
struct StreamTraits<GenericInsituStringStream<Encoding> > {
|
||||||
|
enum { copyOptimization = 1 };
|
||||||
|
};
|
||||||
|
|
||||||
|
//! Insitu string stream with UTF8 encoding.
|
||||||
|
typedef GenericInsituStringStream<UTF8<> > InsituStringStream;
|
||||||
|
|
||||||
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
#endif // RAPIDJSON_STREAM_H_
|
||||||
|
|
@ -15,7 +15,8 @@
|
||||||
#ifndef RAPIDJSON_STRINGBUFFER_H_
|
#ifndef RAPIDJSON_STRINGBUFFER_H_
|
||||||
#define RAPIDJSON_STRINGBUFFER_H_
|
#define RAPIDJSON_STRINGBUFFER_H_
|
||||||
|
|
||||||
#include "rapidjson.h"
|
#include "stream.h"
|
||||||
|
#include "internal/stack.h"
|
||||||
|
|
||||||
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
|
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
|
||||||
#include <utility> // std::move
|
#include <utility> // std::move
|
||||||
|
|
@ -23,6 +24,11 @@
|
||||||
|
|
||||||
#include "internal/stack.h"
|
#include "internal/stack.h"
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(c++98-compat)
|
||||||
|
#endif
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
//! Represents an in-memory output stream.
|
//! Represents an in-memory output stream.
|
||||||
|
|
@ -48,6 +54,7 @@ public:
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void Put(Ch c) { *stack_.template Push<Ch>() = c; }
|
void Put(Ch c) { *stack_.template Push<Ch>() = c; }
|
||||||
|
void PutUnsafe(Ch c) { *stack_.template PushUnsafe<Ch>() = c; }
|
||||||
void Flush() {}
|
void Flush() {}
|
||||||
|
|
||||||
void Clear() { stack_.Clear(); }
|
void Clear() { stack_.Clear(); }
|
||||||
|
|
@ -57,7 +64,10 @@ public:
|
||||||
stack_.ShrinkToFit();
|
stack_.ShrinkToFit();
|
||||||
stack_.template Pop<Ch>(1);
|
stack_.template Pop<Ch>(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Reserve(size_t count) { stack_.template Reserve<Ch>(count); }
|
||||||
Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
|
Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
|
||||||
|
Ch* PushUnsafe(size_t count) { return stack_.template PushUnsafe<Ch>(count); }
|
||||||
void Pop(size_t count) { stack_.template Pop<Ch>(count); }
|
void Pop(size_t count) { stack_.template Pop<Ch>(count); }
|
||||||
|
|
||||||
const Ch* GetString() const {
|
const Ch* GetString() const {
|
||||||
|
|
@ -82,6 +92,16 @@ private:
|
||||||
//! String buffer with UTF8 encoding
|
//! String buffer with UTF8 encoding
|
||||||
typedef GenericStringBuffer<UTF8<> > StringBuffer;
|
typedef GenericStringBuffer<UTF8<> > StringBuffer;
|
||||||
|
|
||||||
|
template<typename Encoding, typename Allocator>
|
||||||
|
inline void PutReserve(GenericStringBuffer<Encoding, Allocator>& stream, size_t count) {
|
||||||
|
stream.Reserve(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Encoding, typename Allocator>
|
||||||
|
inline void PutUnsafe(GenericStringBuffer<Encoding, Allocator>& stream, typename Encoding::Ch c) {
|
||||||
|
stream.PutUnsafe(c);
|
||||||
|
}
|
||||||
|
|
||||||
//! Implement specialized version of PutN() with memset() for better performance.
|
//! Implement specialized version of PutN() with memset() for better performance.
|
||||||
template<>
|
template<>
|
||||||
inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {
|
inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {
|
||||||
|
|
@ -90,4 +110,8 @@ inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_END
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
|
#if defined(__clang__)
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // RAPIDJSON_STRINGBUFFER_H_
|
#endif // RAPIDJSON_STRINGBUFFER_H_
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
#ifndef RAPIDJSON_WRITER_H_
|
#ifndef RAPIDJSON_WRITER_H_
|
||||||
#define RAPIDJSON_WRITER_H_
|
#define RAPIDJSON_WRITER_H_
|
||||||
|
|
||||||
#include "rapidjson.h"
|
#include "stream.h"
|
||||||
#include "internal/stack.h"
|
#include "internal/stack.h"
|
||||||
#include "internal/strfunc.h"
|
#include "internal/strfunc.h"
|
||||||
#include "internal/dtoa.h"
|
#include "internal/dtoa.h"
|
||||||
|
|
@ -23,8 +23,14 @@
|
||||||
#include "stringbuffer.h"
|
#include "stringbuffer.h"
|
||||||
#include <new> // placement new
|
#include <new> // placement new
|
||||||
|
|
||||||
#if RAPIDJSON_HAS_STDSTRING
|
#if defined(RAPIDJSON_SIMD) && defined(_MSC_VER)
|
||||||
#include <string>
|
#include <intrin.h>
|
||||||
|
#pragma intrinsic(_BitScanForward)
|
||||||
|
#endif
|
||||||
|
#ifdef RAPIDJSON_SSE42
|
||||||
|
#include <nmmintrin.h>
|
||||||
|
#elif defined(RAPIDJSON_SSE2)
|
||||||
|
#include <emmintrin.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
|
|
@ -32,8 +38,35 @@ RAPIDJSON_DIAG_PUSH
|
||||||
RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
|
RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_PUSH
|
||||||
|
RAPIDJSON_DIAG_OFF(padded)
|
||||||
|
RAPIDJSON_DIAG_OFF(unreachable-code)
|
||||||
|
#endif
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// WriteFlag
|
||||||
|
|
||||||
|
/*! \def RAPIDJSON_WRITE_DEFAULT_FLAGS
|
||||||
|
\ingroup RAPIDJSON_CONFIG
|
||||||
|
\brief User-defined kWriteDefaultFlags definition.
|
||||||
|
|
||||||
|
User can define this as any \c WriteFlag combinations.
|
||||||
|
*/
|
||||||
|
#ifndef RAPIDJSON_WRITE_DEFAULT_FLAGS
|
||||||
|
#define RAPIDJSON_WRITE_DEFAULT_FLAGS kWriteNoFlags
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//! Combination of writeFlags
|
||||||
|
enum WriteFlag {
|
||||||
|
kWriteNoFlags = 0, //!< No flags are set.
|
||||||
|
kWriteValidateEncodingFlag = 1, //!< Validate encoding of JSON strings.
|
||||||
|
kWriteNanAndInfFlag = 2, //!< Allow writing of Infinity, -Infinity and NaN.
|
||||||
|
kWriteDefaultFlags = RAPIDJSON_WRITE_DEFAULT_FLAGS //!< Default write flags. Can be customized by defining RAPIDJSON_WRITE_DEFAULT_FLAGS
|
||||||
|
};
|
||||||
|
|
||||||
//! JSON writer
|
//! JSON writer
|
||||||
/*! Writer implements the concept Handler.
|
/*! Writer implements the concept Handler.
|
||||||
It generates JSON text by events to an output os.
|
It generates JSON text by events to an output os.
|
||||||
|
|
@ -50,11 +83,13 @@ RAPIDJSON_NAMESPACE_BEGIN
|
||||||
\tparam StackAllocator Type of allocator for allocating memory of stack.
|
\tparam StackAllocator Type of allocator for allocating memory of stack.
|
||||||
\note implements Handler concept
|
\note implements Handler concept
|
||||||
*/
|
*/
|
||||||
template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
|
template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
|
||||||
class Writer {
|
class Writer {
|
||||||
public:
|
public:
|
||||||
typedef typename SourceEncoding::Ch Ch;
|
typedef typename SourceEncoding::Ch Ch;
|
||||||
|
|
||||||
|
static const int kDefaultMaxDecimalPlaces = 324;
|
||||||
|
|
||||||
//! Constructor
|
//! Constructor
|
||||||
/*! \param os Output stream.
|
/*! \param os Output stream.
|
||||||
\param stackAllocator User supplied allocator. If it is null, it will create a private one.
|
\param stackAllocator User supplied allocator. If it is null, it will create a private one.
|
||||||
|
|
@ -62,11 +97,11 @@ public:
|
||||||
*/
|
*/
|
||||||
explicit
|
explicit
|
||||||
Writer(OutputStream& os, StackAllocator* stackAllocator = 0, size_t levelDepth = kDefaultLevelDepth) :
|
Writer(OutputStream& os, StackAllocator* stackAllocator = 0, size_t levelDepth = kDefaultLevelDepth) :
|
||||||
os_(&os), level_stack_(stackAllocator, levelDepth * sizeof(Level)), hasRoot_(false) {}
|
os_(&os), level_stack_(stackAllocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {}
|
||||||
|
|
||||||
explicit
|
explicit
|
||||||
Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) :
|
Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) :
|
||||||
os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), hasRoot_(false) {}
|
os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {}
|
||||||
|
|
||||||
//! Reset the writer with a new stream.
|
//! Reset the writer with a new stream.
|
||||||
/*!
|
/*!
|
||||||
|
|
@ -100,29 +135,64 @@ public:
|
||||||
return hasRoot_ && level_stack_.Empty();
|
return hasRoot_ && level_stack_.Empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int GetMaxDecimalPlaces() const {
|
||||||
|
return maxDecimalPlaces_;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Sets the maximum number of decimal places for double output.
|
||||||
|
/*!
|
||||||
|
This setting truncates the output with specified number of decimal places.
|
||||||
|
|
||||||
|
For example,
|
||||||
|
|
||||||
|
\code
|
||||||
|
writer.SetMaxDecimalPlaces(3);
|
||||||
|
writer.StartArray();
|
||||||
|
writer.Double(0.12345); // "0.123"
|
||||||
|
writer.Double(0.0001); // "0.0"
|
||||||
|
writer.Double(1.234567890123456e30); // "1.234567890123456e30" (do not truncate significand for positive exponent)
|
||||||
|
writer.Double(1.23e-4); // "0.0" (do truncate significand for negative exponent)
|
||||||
|
writer.EndArray();
|
||||||
|
\endcode
|
||||||
|
|
||||||
|
The default setting does not truncate any decimal places. You can restore to this setting by calling
|
||||||
|
\code
|
||||||
|
writer.SetMaxDecimalPlaces(Writer::kDefaultMaxDecimalPlaces);
|
||||||
|
\endcode
|
||||||
|
*/
|
||||||
|
void SetMaxDecimalPlaces(int maxDecimalPlaces) {
|
||||||
|
maxDecimalPlaces_ = maxDecimalPlaces;
|
||||||
|
}
|
||||||
|
|
||||||
/*!@name Implementation of Handler
|
/*!@name Implementation of Handler
|
||||||
\see Handler
|
\see Handler
|
||||||
*/
|
*/
|
||||||
//@{
|
//@{
|
||||||
|
|
||||||
bool Null() { Prefix(kNullType); return WriteNull(); }
|
bool Null() { Prefix(kNullType); return EndValue(WriteNull()); }
|
||||||
bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return WriteBool(b); }
|
bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return EndValue(WriteBool(b)); }
|
||||||
bool Int(int i) { Prefix(kNumberType); return WriteInt(i); }
|
bool Int(int i) { Prefix(kNumberType); return EndValue(WriteInt(i)); }
|
||||||
bool Uint(unsigned u) { Prefix(kNumberType); return WriteUint(u); }
|
bool Uint(unsigned u) { Prefix(kNumberType); return EndValue(WriteUint(u)); }
|
||||||
bool Int64(int64_t i64) { Prefix(kNumberType); return WriteInt64(i64); }
|
bool Int64(int64_t i64) { Prefix(kNumberType); return EndValue(WriteInt64(i64)); }
|
||||||
bool Uint64(uint64_t u64) { Prefix(kNumberType); return WriteUint64(u64); }
|
bool Uint64(uint64_t u64) { Prefix(kNumberType); return EndValue(WriteUint64(u64)); }
|
||||||
|
|
||||||
//! Writes the given \c double value to the stream
|
//! Writes the given \c double value to the stream
|
||||||
/*!
|
/*!
|
||||||
\param d The value to be written.
|
\param d The value to be written.
|
||||||
\return Whether it is succeed.
|
\return Whether it is succeed.
|
||||||
*/
|
*/
|
||||||
bool Double(double d) { Prefix(kNumberType); return WriteDouble(d); }
|
bool Double(double d) { Prefix(kNumberType); return EndValue(WriteDouble(d)); }
|
||||||
|
|
||||||
|
bool RawNumber(const Ch* str, SizeType length, bool copy = false) {
|
||||||
|
(void)copy;
|
||||||
|
Prefix(kNumberType);
|
||||||
|
return EndValue(WriteString(str, length));
|
||||||
|
}
|
||||||
|
|
||||||
bool String(const Ch* str, SizeType length, bool copy = false) {
|
bool String(const Ch* str, SizeType length, bool copy = false) {
|
||||||
(void)copy;
|
(void)copy;
|
||||||
Prefix(kStringType);
|
Prefix(kStringType);
|
||||||
return WriteString(str, length);
|
return EndValue(WriteString(str, length));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if RAPIDJSON_HAS_STDSTRING
|
#if RAPIDJSON_HAS_STDSTRING
|
||||||
|
|
@ -144,10 +214,7 @@ public:
|
||||||
RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level));
|
RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level));
|
||||||
RAPIDJSON_ASSERT(!level_stack_.template Top<Level>()->inArray);
|
RAPIDJSON_ASSERT(!level_stack_.template Top<Level>()->inArray);
|
||||||
level_stack_.template Pop<Level>(1);
|
level_stack_.template Pop<Level>(1);
|
||||||
bool ret = WriteEndObject();
|
return EndValue(WriteEndObject());
|
||||||
if (level_stack_.Empty()) // end of json text
|
|
||||||
os_->Flush();
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StartArray() {
|
bool StartArray() {
|
||||||
|
|
@ -161,10 +228,7 @@ public:
|
||||||
RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level));
|
RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level));
|
||||||
RAPIDJSON_ASSERT(level_stack_.template Top<Level>()->inArray);
|
RAPIDJSON_ASSERT(level_stack_.template Top<Level>()->inArray);
|
||||||
level_stack_.template Pop<Level>(1);
|
level_stack_.template Pop<Level>(1);
|
||||||
bool ret = WriteEndArray();
|
return EndValue(WriteEndArray());
|
||||||
if (level_stack_.Empty()) // end of json text
|
|
||||||
os_->Flush();
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
|
|
@ -177,6 +241,16 @@ public:
|
||||||
|
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
|
//! Write a raw JSON value.
|
||||||
|
/*!
|
||||||
|
For user to write a stringified JSON as a value.
|
||||||
|
|
||||||
|
\param json A well-formed JSON value. It should not contain null character within [0, length - 1] range.
|
||||||
|
\param length Length of the json.
|
||||||
|
\param type Type of the root of json.
|
||||||
|
*/
|
||||||
|
bool RawValue(const Ch* json, size_t length, Type type) { Prefix(type); return EndValue(WriteRawValue(json, length)); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//! Information for each nested level
|
//! Information for each nested level
|
||||||
struct Level {
|
struct Level {
|
||||||
|
|
@ -188,15 +262,18 @@ protected:
|
||||||
static const size_t kDefaultLevelDepth = 32;
|
static const size_t kDefaultLevelDepth = 32;
|
||||||
|
|
||||||
bool WriteNull() {
|
bool WriteNull() {
|
||||||
os_->Put('n'); os_->Put('u'); os_->Put('l'); os_->Put('l'); return true;
|
PutReserve(*os_, 4);
|
||||||
|
PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'u'); PutUnsafe(*os_, 'l'); PutUnsafe(*os_, 'l'); return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WriteBool(bool b) {
|
bool WriteBool(bool b) {
|
||||||
if (b) {
|
if (b) {
|
||||||
os_->Put('t'); os_->Put('r'); os_->Put('u'); os_->Put('e');
|
PutReserve(*os_, 4);
|
||||||
|
PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'r'); PutUnsafe(*os_, 'u'); PutUnsafe(*os_, 'e');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
os_->Put('f'); os_->Put('a'); os_->Put('l'); os_->Put('s'); os_->Put('e');
|
PutReserve(*os_, 5);
|
||||||
|
PutUnsafe(*os_, 'f'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'l'); PutUnsafe(*os_, 's'); PutUnsafe(*os_, 'e');
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -204,45 +281,69 @@ protected:
|
||||||
bool WriteInt(int i) {
|
bool WriteInt(int i) {
|
||||||
char buffer[11];
|
char buffer[11];
|
||||||
const char* end = internal::i32toa(i, buffer);
|
const char* end = internal::i32toa(i, buffer);
|
||||||
|
PutReserve(*os_, static_cast<size_t>(end - buffer));
|
||||||
for (const char* p = buffer; p != end; ++p)
|
for (const char* p = buffer; p != end; ++p)
|
||||||
os_->Put(*p);
|
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WriteUint(unsigned u) {
|
bool WriteUint(unsigned u) {
|
||||||
char buffer[10];
|
char buffer[10];
|
||||||
const char* end = internal::u32toa(u, buffer);
|
const char* end = internal::u32toa(u, buffer);
|
||||||
|
PutReserve(*os_, static_cast<size_t>(end - buffer));
|
||||||
for (const char* p = buffer; p != end; ++p)
|
for (const char* p = buffer; p != end; ++p)
|
||||||
os_->Put(*p);
|
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WriteInt64(int64_t i64) {
|
bool WriteInt64(int64_t i64) {
|
||||||
char buffer[21];
|
char buffer[21];
|
||||||
const char* end = internal::i64toa(i64, buffer);
|
const char* end = internal::i64toa(i64, buffer);
|
||||||
|
PutReserve(*os_, static_cast<size_t>(end - buffer));
|
||||||
for (const char* p = buffer; p != end; ++p)
|
for (const char* p = buffer; p != end; ++p)
|
||||||
os_->Put(*p);
|
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WriteUint64(uint64_t u64) {
|
bool WriteUint64(uint64_t u64) {
|
||||||
char buffer[20];
|
char buffer[20];
|
||||||
char* end = internal::u64toa(u64, buffer);
|
char* end = internal::u64toa(u64, buffer);
|
||||||
|
PutReserve(*os_, static_cast<size_t>(end - buffer));
|
||||||
for (char* p = buffer; p != end; ++p)
|
for (char* p = buffer; p != end; ++p)
|
||||||
os_->Put(*p);
|
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WriteDouble(double d) {
|
bool WriteDouble(double d) {
|
||||||
|
if (internal::Double(d).IsNanOrInf()) {
|
||||||
|
if (!(writeFlags & kWriteNanAndInfFlag))
|
||||||
|
return false;
|
||||||
|
if (internal::Double(d).IsNan()) {
|
||||||
|
PutReserve(*os_, 3);
|
||||||
|
PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (internal::Double(d).Sign()) {
|
||||||
|
PutReserve(*os_, 9);
|
||||||
|
PutUnsafe(*os_, '-');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
PutReserve(*os_, 8);
|
||||||
|
PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f');
|
||||||
|
PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
char buffer[25];
|
char buffer[25];
|
||||||
char* end = internal::dtoa(d, buffer);
|
char* end = internal::dtoa(d, buffer, maxDecimalPlaces_);
|
||||||
|
PutReserve(*os_, static_cast<size_t>(end - buffer));
|
||||||
for (char* p = buffer; p != end; ++p)
|
for (char* p = buffer; p != end; ++p)
|
||||||
os_->Put(*p);
|
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(*p));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WriteString(const Ch* str, SizeType length) {
|
bool WriteString(const Ch* str, SizeType length) {
|
||||||
static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
static const typename TargetEncoding::Ch hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
||||||
static const char escape[256] = {
|
static const char escape[256] = {
|
||||||
#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
||||||
//0 1 2 3 4 5 6 7 8 9 A B C D E F
|
//0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||||
|
|
@ -255,22 +356,27 @@ protected:
|
||||||
#undef Z16
|
#undef Z16
|
||||||
};
|
};
|
||||||
|
|
||||||
os_->Put('\"');
|
if (TargetEncoding::supportUnicode)
|
||||||
|
PutReserve(*os_, 2 + length * 6); // "\uxxxx..."
|
||||||
|
else
|
||||||
|
PutReserve(*os_, 2 + length * 12); // "\uxxxx\uyyyy..."
|
||||||
|
|
||||||
|
PutUnsafe(*os_, '\"');
|
||||||
GenericStringStream<SourceEncoding> is(str);
|
GenericStringStream<SourceEncoding> is(str);
|
||||||
while (is.Tell() < length) {
|
while (ScanWriteUnescapedString(is, length)) {
|
||||||
const Ch c = is.Peek();
|
const Ch c = is.Peek();
|
||||||
if (!TargetEncoding::supportUnicode && (unsigned)c >= 0x80) {
|
if (!TargetEncoding::supportUnicode && static_cast<unsigned>(c) >= 0x80) {
|
||||||
// Unicode escaping
|
// Unicode escaping
|
||||||
unsigned codepoint;
|
unsigned codepoint;
|
||||||
if (!SourceEncoding::Decode(is, &codepoint))
|
if (RAPIDJSON_UNLIKELY(!SourceEncoding::Decode(is, &codepoint)))
|
||||||
return false;
|
return false;
|
||||||
os_->Put('\\');
|
PutUnsafe(*os_, '\\');
|
||||||
os_->Put('u');
|
PutUnsafe(*os_, 'u');
|
||||||
if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) {
|
if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) {
|
||||||
os_->Put(hexDigits[(codepoint >> 12) & 15]);
|
PutUnsafe(*os_, hexDigits[(codepoint >> 12) & 15]);
|
||||||
os_->Put(hexDigits[(codepoint >> 8) & 15]);
|
PutUnsafe(*os_, hexDigits[(codepoint >> 8) & 15]);
|
||||||
os_->Put(hexDigits[(codepoint >> 4) & 15]);
|
PutUnsafe(*os_, hexDigits[(codepoint >> 4) & 15]);
|
||||||
os_->Put(hexDigits[(codepoint ) & 15]);
|
PutUnsafe(*os_, hexDigits[(codepoint ) & 15]);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
RAPIDJSON_ASSERT(codepoint >= 0x010000 && codepoint <= 0x10FFFF);
|
RAPIDJSON_ASSERT(codepoint >= 0x010000 && codepoint <= 0x10FFFF);
|
||||||
|
|
@ -278,45 +384,59 @@ protected:
|
||||||
unsigned s = codepoint - 0x010000;
|
unsigned s = codepoint - 0x010000;
|
||||||
unsigned lead = (s >> 10) + 0xD800;
|
unsigned lead = (s >> 10) + 0xD800;
|
||||||
unsigned trail = (s & 0x3FF) + 0xDC00;
|
unsigned trail = (s & 0x3FF) + 0xDC00;
|
||||||
os_->Put(hexDigits[(lead >> 12) & 15]);
|
PutUnsafe(*os_, hexDigits[(lead >> 12) & 15]);
|
||||||
os_->Put(hexDigits[(lead >> 8) & 15]);
|
PutUnsafe(*os_, hexDigits[(lead >> 8) & 15]);
|
||||||
os_->Put(hexDigits[(lead >> 4) & 15]);
|
PutUnsafe(*os_, hexDigits[(lead >> 4) & 15]);
|
||||||
os_->Put(hexDigits[(lead ) & 15]);
|
PutUnsafe(*os_, hexDigits[(lead ) & 15]);
|
||||||
os_->Put('\\');
|
PutUnsafe(*os_, '\\');
|
||||||
os_->Put('u');
|
PutUnsafe(*os_, 'u');
|
||||||
os_->Put(hexDigits[(trail >> 12) & 15]);
|
PutUnsafe(*os_, hexDigits[(trail >> 12) & 15]);
|
||||||
os_->Put(hexDigits[(trail >> 8) & 15]);
|
PutUnsafe(*os_, hexDigits[(trail >> 8) & 15]);
|
||||||
os_->Put(hexDigits[(trail >> 4) & 15]);
|
PutUnsafe(*os_, hexDigits[(trail >> 4) & 15]);
|
||||||
os_->Put(hexDigits[(trail ) & 15]);
|
PutUnsafe(*os_, hexDigits[(trail ) & 15]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ((sizeof(Ch) == 1 || (unsigned)c < 256) && escape[(unsigned char)c]) {
|
else if ((sizeof(Ch) == 1 || static_cast<unsigned>(c) < 256) && RAPIDJSON_UNLIKELY(escape[static_cast<unsigned char>(c)])) {
|
||||||
is.Take();
|
is.Take();
|
||||||
os_->Put('\\');
|
PutUnsafe(*os_, '\\');
|
||||||
os_->Put(escape[(unsigned char)c]);
|
PutUnsafe(*os_, static_cast<typename TargetEncoding::Ch>(escape[static_cast<unsigned char>(c)]));
|
||||||
if (escape[(unsigned char)c] == 'u') {
|
if (escape[static_cast<unsigned char>(c)] == 'u') {
|
||||||
os_->Put('0');
|
PutUnsafe(*os_, '0');
|
||||||
os_->Put('0');
|
PutUnsafe(*os_, '0');
|
||||||
os_->Put(hexDigits[(unsigned char)c >> 4]);
|
PutUnsafe(*os_, hexDigits[static_cast<unsigned char>(c) >> 4]);
|
||||||
os_->Put(hexDigits[(unsigned char)c & 0xF]);
|
PutUnsafe(*os_, hexDigits[static_cast<unsigned char>(c) & 0xF]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else if (RAPIDJSON_UNLIKELY(!(writeFlags & kWriteValidateEncodingFlag ?
|
||||||
if (!Transcoder<SourceEncoding, TargetEncoding>::Transcode(is, *os_))
|
Transcoder<SourceEncoding, TargetEncoding>::Validate(is, *os_) :
|
||||||
|
Transcoder<SourceEncoding, TargetEncoding>::TranscodeUnsafe(is, *os_))))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
os_->Put('\"');
|
PutUnsafe(*os_, '\"');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ScanWriteUnescapedString(GenericStringStream<SourceEncoding>& is, size_t length) {
|
||||||
|
return RAPIDJSON_LIKELY(is.Tell() < length);
|
||||||
|
}
|
||||||
|
|
||||||
bool WriteStartObject() { os_->Put('{'); return true; }
|
bool WriteStartObject() { os_->Put('{'); return true; }
|
||||||
bool WriteEndObject() { os_->Put('}'); return true; }
|
bool WriteEndObject() { os_->Put('}'); return true; }
|
||||||
bool WriteStartArray() { os_->Put('['); return true; }
|
bool WriteStartArray() { os_->Put('['); return true; }
|
||||||
bool WriteEndArray() { os_->Put(']'); return true; }
|
bool WriteEndArray() { os_->Put(']'); return true; }
|
||||||
|
|
||||||
|
bool WriteRawValue(const Ch* json, size_t length) {
|
||||||
|
PutReserve(*os_, length);
|
||||||
|
for (size_t i = 0; i < length; i++) {
|
||||||
|
RAPIDJSON_ASSERT(json[i] != '\0');
|
||||||
|
PutUnsafe(*os_, json[i]);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void Prefix(Type type) {
|
void Prefix(Type type) {
|
||||||
(void)type;
|
(void)type;
|
||||||
if (level_stack_.GetSize() != 0) { // this value is not at root
|
if (RAPIDJSON_LIKELY(level_stack_.GetSize() != 0)) { // this value is not at root
|
||||||
Level* level = level_stack_.template Top<Level>();
|
Level* level = level_stack_.template Top<Level>();
|
||||||
if (level->valueCount > 0) {
|
if (level->valueCount > 0) {
|
||||||
if (level->inArray)
|
if (level->inArray)
|
||||||
|
|
@ -334,8 +454,16 @@ protected:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flush the value if it is the top level one.
|
||||||
|
bool EndValue(bool ret) {
|
||||||
|
if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text
|
||||||
|
os_->Flush();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
OutputStream* os_;
|
OutputStream* os_;
|
||||||
internal::Stack<StackAllocator> level_stack_;
|
internal::Stack<StackAllocator> level_stack_;
|
||||||
|
int maxDecimalPlaces_;
|
||||||
bool hasRoot_;
|
bool hasRoot_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -350,7 +478,7 @@ template<>
|
||||||
inline bool Writer<StringBuffer>::WriteInt(int i) {
|
inline bool Writer<StringBuffer>::WriteInt(int i) {
|
||||||
char *buffer = os_->Push(11);
|
char *buffer = os_->Push(11);
|
||||||
const char* end = internal::i32toa(i, buffer);
|
const char* end = internal::i32toa(i, buffer);
|
||||||
os_->Pop(11 - (end - buffer));
|
os_->Pop(static_cast<size_t>(11 - (end - buffer)));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -358,7 +486,7 @@ template<>
|
||||||
inline bool Writer<StringBuffer>::WriteUint(unsigned u) {
|
inline bool Writer<StringBuffer>::WriteUint(unsigned u) {
|
||||||
char *buffer = os_->Push(10);
|
char *buffer = os_->Push(10);
|
||||||
const char* end = internal::u32toa(u, buffer);
|
const char* end = internal::u32toa(u, buffer);
|
||||||
os_->Pop(10 - (end - buffer));
|
os_->Pop(static_cast<size_t>(10 - (end - buffer)));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -366,7 +494,7 @@ template<>
|
||||||
inline bool Writer<StringBuffer>::WriteInt64(int64_t i64) {
|
inline bool Writer<StringBuffer>::WriteInt64(int64_t i64) {
|
||||||
char *buffer = os_->Push(21);
|
char *buffer = os_->Push(21);
|
||||||
const char* end = internal::i64toa(i64, buffer);
|
const char* end = internal::i64toa(i64, buffer);
|
||||||
os_->Pop(21 - (end - buffer));
|
os_->Pop(static_cast<size_t>(21 - (end - buffer)));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -374,22 +502,109 @@ template<>
|
||||||
inline bool Writer<StringBuffer>::WriteUint64(uint64_t u) {
|
inline bool Writer<StringBuffer>::WriteUint64(uint64_t u) {
|
||||||
char *buffer = os_->Push(20);
|
char *buffer = os_->Push(20);
|
||||||
const char* end = internal::u64toa(u, buffer);
|
const char* end = internal::u64toa(u, buffer);
|
||||||
os_->Pop(20 - (end - buffer));
|
os_->Pop(static_cast<size_t>(20 - (end - buffer)));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool Writer<StringBuffer>::WriteDouble(double d) {
|
inline bool Writer<StringBuffer>::WriteDouble(double d) {
|
||||||
|
if (internal::Double(d).IsNanOrInf()) {
|
||||||
|
// Note: This code path can only be reached if (RAPIDJSON_WRITE_DEFAULT_FLAGS & kWriteNanAndInfFlag).
|
||||||
|
if (!(kWriteDefaultFlags & kWriteNanAndInfFlag))
|
||||||
|
return false;
|
||||||
|
if (internal::Double(d).IsNan()) {
|
||||||
|
PutReserve(*os_, 3);
|
||||||
|
PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (internal::Double(d).Sign()) {
|
||||||
|
PutReserve(*os_, 9);
|
||||||
|
PutUnsafe(*os_, '-');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
PutReserve(*os_, 8);
|
||||||
|
PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f');
|
||||||
|
PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
char *buffer = os_->Push(25);
|
char *buffer = os_->Push(25);
|
||||||
char* end = internal::dtoa(d, buffer);
|
char* end = internal::dtoa(d, buffer, maxDecimalPlaces_);
|
||||||
os_->Pop(25 - (end - buffer));
|
os_->Pop(static_cast<size_t>(25 - (end - buffer)));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42)
|
||||||
|
template<>
|
||||||
|
inline bool Writer<StringBuffer>::ScanWriteUnescapedString(StringStream& is, size_t length) {
|
||||||
|
if (length < 16)
|
||||||
|
return RAPIDJSON_LIKELY(is.Tell() < length);
|
||||||
|
|
||||||
|
if (!RAPIDJSON_LIKELY(is.Tell() < length))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const char* p = is.src_;
|
||||||
|
const char* end = is.head_ + length;
|
||||||
|
const char* nextAligned = reinterpret_cast<const char*>((reinterpret_cast<size_t>(p) + 15) & static_cast<size_t>(~15));
|
||||||
|
const char* endAligned = reinterpret_cast<const char*>(reinterpret_cast<size_t>(end) & static_cast<size_t>(~15));
|
||||||
|
if (nextAligned > end)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
while (p != nextAligned)
|
||||||
|
if (*p < 0x20 || *p == '\"' || *p == '\\') {
|
||||||
|
is.src_ = p;
|
||||||
|
return RAPIDJSON_LIKELY(is.Tell() < length);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
os_->PutUnsafe(*p++);
|
||||||
|
|
||||||
|
// The rest of string using SIMD
|
||||||
|
static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' };
|
||||||
|
static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' };
|
||||||
|
static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 };
|
||||||
|
const __m128i dq = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&dquote[0]));
|
||||||
|
const __m128i bs = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&bslash[0]));
|
||||||
|
const __m128i sp = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&space[0]));
|
||||||
|
|
||||||
|
for (; p != endAligned; p += 16) {
|
||||||
|
const __m128i s = _mm_load_si128(reinterpret_cast<const __m128i *>(p));
|
||||||
|
const __m128i t1 = _mm_cmpeq_epi8(s, dq);
|
||||||
|
const __m128i t2 = _mm_cmpeq_epi8(s, bs);
|
||||||
|
const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19
|
||||||
|
const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3);
|
||||||
|
unsigned short r = static_cast<unsigned short>(_mm_movemask_epi8(x));
|
||||||
|
if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped
|
||||||
|
SizeType len;
|
||||||
|
#ifdef _MSC_VER // Find the index of first escaped
|
||||||
|
unsigned long offset;
|
||||||
|
_BitScanForward(&offset, r);
|
||||||
|
len = offset;
|
||||||
|
#else
|
||||||
|
len = static_cast<SizeType>(__builtin_ffs(r) - 1);
|
||||||
|
#endif
|
||||||
|
char* q = reinterpret_cast<char*>(os_->PushUnsafe(len));
|
||||||
|
for (size_t i = 0; i < len; i++)
|
||||||
|
q[i] = p[i];
|
||||||
|
|
||||||
|
p += len;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(os_->PushUnsafe(16)), s);
|
||||||
|
}
|
||||||
|
|
||||||
|
is.src_ = p;
|
||||||
|
return RAPIDJSON_LIKELY(is.Tell() < length);
|
||||||
|
}
|
||||||
|
#endif // defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42)
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_END
|
RAPIDJSON_NAMESPACE_END
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
RAPIDJSON_DIAG_POP
|
RAPIDJSON_DIAG_POP
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
RAPIDJSON_DIAG_POP
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // RAPIDJSON_RAPIDJSON_H_
|
#endif // RAPIDJSON_RAPIDJSON_H_
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue