├── .gitignore ├── CMakeLists.txt ├── README.md ├── include └── rapidjson │ ├── allocators.h │ ├── document.h │ ├── encodedstream.h │ ├── encodings.h │ ├── error │ ├── en.h │ └── error.h │ ├── filereadstream.h │ ├── filewritestream.h │ ├── internal │ ├── biginteger.h │ ├── diyfp.h │ ├── dtoa.h │ ├── ieee754.h │ ├── itoa.h │ ├── meta.h │ ├── pow10.h │ ├── stack.h │ ├── strfunc.h │ └── strtod.h │ ├── memorybuffer.h │ ├── memorystream.h │ ├── msinttypes │ ├── inttypes.h │ └── stdint.h │ ├── pointer.h │ ├── prettywriter.h │ ├── rapidjson.h │ ├── reader.h │ ├── stringbuffer.h │ └── writer.h ├── jsonUdf-test.cc ├── jsonUdf.cc └── jsonUdf.h /.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | CMakeFiles/* 3 | CMakeCache.txt 4 | Makefile 5 | cmake_install.cmake 6 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2012 Cloudera Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | cmake_minimum_required(VERSION 2.6) 16 | 17 | # where to put generated libraries 18 | set(LIBRARY_OUTPUT_PATH "build") 19 | # where to put generated binaries 20 | set(EXECUTABLE_OUTPUT_PATH "build") 21 | 22 | find_program(CLANG_EXECUTABLE clang++) 23 | 24 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -ggdb") 25 | 26 | include_directories("include") 27 | 28 | # Function to generate rule to cross compile a source file to an IR module. 29 | # This should be called with the .cc src file and it will generate a 30 | # src-file-ir target that can be built. 31 | # e.g. COMPILE_TO_IR(test.cc) generates the "test-ir" make target. 32 | set(IR_COMPILE_FLAGS "-emit-llvm" "-O3" "-c" "-Iinclude") 33 | function(COMPILE_TO_IR SRC_FILE) 34 | get_filename_component(BASE_NAME ${SRC_FILE} NAME_WE) 35 | set(OUTPUT_FILE "build/${BASE_NAME}.ll") 36 | add_custom_command( 37 | OUTPUT ${OUTPUT_FILE} 38 | COMMAND ${CLANG_EXECUTABLE} ${IR_COMPILE_FLAGS} ${SRC_FILE} -o ${OUTPUT_FILE} 39 | DEPENDS ${SRC_FILE}) 40 | add_custom_target(${BASE_NAME}-ir ALL DEPENDS ${OUTPUT_FILE}) 41 | endfunction(COMPILE_TO_IR) 42 | 43 | # Build the UDA/UDFs into a shared library. 44 | add_library(jsonUdf SHARED jsonUdf.cc) 45 | 46 | # Custom targest to cross compile UDA/UDF to ir 47 | if (CLANG_EXECUTABLE) 48 | COMPILE_TO_IR(jsonUdf.cc ) 49 | endif(CLANG_EXECUTABLE) 50 | 51 | # This is an example of how to use the test harness to help develop UDF and UDAs. 52 | target_link_libraries(jsonUdf ImpalaUdf) 53 | add_executable(jsonUdf-test jsonUdf-test.cc) 54 | target_link_libraries(jsonUdf-test jsonUdf) 55 | 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # impala-get-json-object-udf 2 | A UDF for Cloudera Impala ( hive get_json_object equivalent ) 3 | 4 | ## build 5 | - cmake . 6 | - make 7 | 8 | ## install 9 | - upload build/jsonUdf.ll to [hdfs path]/jsonUdf.ll 10 | - impala-shell> create function [database.]json_get_object (string, string) returns string location '[hdfs path]/jsonUdf.ll' symbol='JsonGetObject'; 11 | 12 | ## usage 13 | - impala-shell> select json_get_object('{"name":"steven"}', '$.name'); 14 | - --> returns a string steven 15 | -------------------------------------------------------------------------------- /include/rapidjson/allocators.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_ALLOCATORS_H_ 16 | #define RAPIDJSON_ALLOCATORS_H_ 17 | 18 | #include "rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | 22 | /////////////////////////////////////////////////////////////////////////////// 23 | // Allocator 24 | 25 | /*! \class rapidjson::Allocator 26 | \brief Concept for allocating, resizing and freeing memory block. 27 | 28 | Note that Malloc() and Realloc() are non-static but Free() is static. 29 | 30 | So if an allocator need to support Free(), it needs to put its pointer in 31 | the header of memory block. 32 | 33 | \code 34 | concept Allocator { 35 | static const bool kNeedFree; //!< Whether this allocator needs to call Free(). 36 | 37 | // Allocate a memory block. 38 | // \param size of the memory block in bytes. 39 | // \returns pointer to the memory block. 40 | void* Malloc(size_t size); 41 | 42 | // Resize a memory block. 43 | // \param originalPtr The pointer to current memory block. Null pointer is permitted. 44 | // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.) 45 | // \param newSize the new size in bytes. 46 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize); 47 | 48 | // Free a memory block. 49 | // \param pointer to the memory block. Null pointer is permitted. 50 | static void Free(void *ptr); 51 | }; 52 | \endcode 53 | */ 54 | 55 | /////////////////////////////////////////////////////////////////////////////// 56 | // CrtAllocator 57 | 58 | //! C-runtime library allocator. 59 | /*! This class is just wrapper for standard C library memory routines. 60 | \note implements Allocator concept 61 | */ 62 | class CrtAllocator { 63 | public: 64 | static const bool kNeedFree = true; 65 | void* Malloc(size_t size) { 66 | if (size) // behavior of malloc(0) is implementation defined. 67 | return std::malloc(size); 68 | else 69 | return NULL; // standardize to returning NULL. 70 | } 71 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { 72 | (void)originalSize; 73 | if (newSize == 0) { 74 | std::free(originalPtr); 75 | return NULL; 76 | } 77 | return std::realloc(originalPtr, newSize); 78 | } 79 | static void Free(void *ptr) { std::free(ptr); } 80 | }; 81 | 82 | /////////////////////////////////////////////////////////////////////////////// 83 | // MemoryPoolAllocator 84 | 85 | //! Default memory allocator used by the parser and DOM. 86 | /*! This allocator allocate memory blocks from pre-allocated memory chunks. 87 | 88 | It does not free memory blocks. And Realloc() only allocate new memory. 89 | 90 | The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default. 91 | 92 | User may also supply a buffer as the first chunk. 93 | 94 | If the user-buffer is full then additional chunks are allocated by BaseAllocator. 95 | 96 | The user-buffer is not deallocated by this allocator. 97 | 98 | \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator. 99 | \note implements Allocator concept 100 | */ 101 | template 102 | class MemoryPoolAllocator { 103 | public: 104 | static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator) 105 | 106 | //! Constructor with chunkSize. 107 | /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. 108 | \param baseAllocator The allocator for allocating memory chunks. 109 | */ 110 | MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : 111 | chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0) 112 | { 113 | } 114 | 115 | //! Constructor with user-supplied buffer. 116 | /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size. 117 | 118 | The user buffer will not be deallocated when this allocator is destructed. 119 | 120 | \param buffer User supplied buffer. 121 | \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader). 122 | \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. 123 | \param baseAllocator The allocator for allocating memory chunks. 124 | */ 125 | MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : 126 | chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0) 127 | { 128 | RAPIDJSON_ASSERT(buffer != 0); 129 | RAPIDJSON_ASSERT(size > sizeof(ChunkHeader)); 130 | chunkHead_ = reinterpret_cast(buffer); 131 | chunkHead_->capacity = size - sizeof(ChunkHeader); 132 | chunkHead_->size = 0; 133 | chunkHead_->next = 0; 134 | } 135 | 136 | //! Destructor. 137 | /*! This deallocates all memory chunks, excluding the user-supplied buffer. 138 | */ 139 | ~MemoryPoolAllocator() { 140 | Clear(); 141 | RAPIDJSON_DELETE(ownBaseAllocator_); 142 | } 143 | 144 | //! Deallocates all memory chunks, excluding the user-supplied buffer. 145 | void Clear() { 146 | while (chunkHead_ && chunkHead_ != userBuffer_) { 147 | ChunkHeader* next = chunkHead_->next; 148 | baseAllocator_->Free(chunkHead_); 149 | chunkHead_ = next; 150 | } 151 | if (chunkHead_ && chunkHead_ == userBuffer_) 152 | chunkHead_->size = 0; // Clear user buffer 153 | } 154 | 155 | //! Computes the total capacity of allocated memory chunks. 156 | /*! \return total capacity in bytes. 157 | */ 158 | size_t Capacity() const { 159 | size_t capacity = 0; 160 | for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) 161 | capacity += c->capacity; 162 | return capacity; 163 | } 164 | 165 | //! Computes the memory blocks allocated. 166 | /*! \return total used bytes. 167 | */ 168 | size_t Size() const { 169 | size_t size = 0; 170 | for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) 171 | size += c->size; 172 | return size; 173 | } 174 | 175 | //! Allocates a memory block. (concept Allocator) 176 | void* Malloc(size_t size) { 177 | if (!size) 178 | return NULL; 179 | 180 | size = RAPIDJSON_ALIGN(size); 181 | if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity) 182 | AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size); 183 | 184 | void *buffer = reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size; 185 | chunkHead_->size += size; 186 | return buffer; 187 | } 188 | 189 | //! Resizes a memory block (concept Allocator) 190 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { 191 | if (originalPtr == 0) 192 | return Malloc(newSize); 193 | 194 | if (newSize == 0) 195 | return NULL; 196 | 197 | // Do not shrink if new size is smaller than original 198 | if (originalSize >= newSize) 199 | return originalPtr; 200 | 201 | // Simply expand it if it is the last allocation and there is sufficient space 202 | if (originalPtr == (char *)(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) { 203 | size_t increment = static_cast(newSize - originalSize); 204 | increment = RAPIDJSON_ALIGN(increment); 205 | if (chunkHead_->size + increment <= chunkHead_->capacity) { 206 | chunkHead_->size += increment; 207 | return originalPtr; 208 | } 209 | } 210 | 211 | // Realloc process: allocate and copy memory, do not free original buffer. 212 | void* newBuffer = Malloc(newSize); 213 | RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly. 214 | if (originalSize) 215 | std::memcpy(newBuffer, originalPtr, originalSize); 216 | return newBuffer; 217 | } 218 | 219 | //! Frees a memory block (concept Allocator) 220 | static void Free(void *ptr) { (void)ptr; } // Do nothing 221 | 222 | private: 223 | //! Copy constructor is not permitted. 224 | MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */; 225 | //! Copy assignment operator is not permitted. 226 | MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */; 227 | 228 | //! Creates a new chunk. 229 | /*! \param capacity Capacity of the chunk in bytes. 230 | */ 231 | void AddChunk(size_t capacity) { 232 | if (!baseAllocator_) 233 | ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator()); 234 | ChunkHeader* chunk = reinterpret_cast(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity)); 235 | chunk->capacity = capacity; 236 | chunk->size = 0; 237 | chunk->next = chunkHead_; 238 | chunkHead_ = chunk; 239 | } 240 | 241 | static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity. 242 | 243 | //! Chunk header for perpending to each chunk. 244 | /*! Chunks are stored as a singly linked list. 245 | */ 246 | struct ChunkHeader { 247 | size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself). 248 | size_t size; //!< Current size of allocated memory in bytes. 249 | ChunkHeader *next; //!< Next chunk in the linked list. 250 | }; 251 | 252 | ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation. 253 | size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated. 254 | void *userBuffer_; //!< User supplied buffer. 255 | BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks. 256 | BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object. 257 | }; 258 | 259 | RAPIDJSON_NAMESPACE_END 260 | 261 | #endif // RAPIDJSON_ENCODINGS_H_ 262 | -------------------------------------------------------------------------------- /include/rapidjson/encodedstream.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_ENCODEDSTREAM_H_ 16 | #define RAPIDJSON_ENCODEDSTREAM_H_ 17 | 18 | #include "rapidjson.h" 19 | 20 | #ifdef __GNUC__ 21 | RAPIDJSON_DIAG_PUSH 22 | RAPIDJSON_DIAG_OFF(effc++) 23 | #endif 24 | 25 | RAPIDJSON_NAMESPACE_BEGIN 26 | 27 | //! Input byte stream wrapper with a statically bound encoding. 28 | /*! 29 | \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. 30 | \tparam InputByteStream Type of input byte stream. For example, FileReadStream. 31 | */ 32 | template 33 | class EncodedInputStream { 34 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 35 | public: 36 | typedef typename Encoding::Ch Ch; 37 | 38 | EncodedInputStream(InputByteStream& is) : is_(is) { 39 | current_ = Encoding::TakeBOM(is_); 40 | } 41 | 42 | Ch Peek() const { return current_; } 43 | Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; } 44 | size_t Tell() const { return is_.Tell(); } 45 | 46 | // Not implemented 47 | void Put(Ch) { RAPIDJSON_ASSERT(false); } 48 | void Flush() { RAPIDJSON_ASSERT(false); } 49 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 50 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 51 | 52 | private: 53 | EncodedInputStream(const EncodedInputStream&); 54 | EncodedInputStream& operator=(const EncodedInputStream&); 55 | 56 | InputByteStream& is_; 57 | Ch current_; 58 | }; 59 | 60 | //! Output byte stream wrapper with statically bound encoding. 61 | /*! 62 | \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. 63 | \tparam InputByteStream Type of input byte stream. For example, FileWriteStream. 64 | */ 65 | template 66 | class EncodedOutputStream { 67 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 68 | public: 69 | typedef typename Encoding::Ch Ch; 70 | 71 | EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) { 72 | if (putBOM) 73 | Encoding::PutBOM(os_); 74 | } 75 | 76 | void Put(Ch c) { Encoding::Put(os_, c); } 77 | void Flush() { os_.Flush(); } 78 | 79 | // Not implemented 80 | Ch Peek() const { RAPIDJSON_ASSERT(false); } 81 | Ch Take() { RAPIDJSON_ASSERT(false); } 82 | size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } 83 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 84 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 85 | 86 | private: 87 | EncodedOutputStream(const EncodedOutputStream&); 88 | EncodedOutputStream& operator=(const EncodedOutputStream&); 89 | 90 | OutputByteStream& os_; 91 | }; 92 | 93 | #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x 94 | 95 | //! Input stream wrapper with dynamically bound encoding and automatic encoding detection. 96 | /*! 97 | \tparam CharType Type of character for reading. 98 | \tparam InputByteStream type of input byte stream to be wrapped. 99 | */ 100 | template 101 | class AutoUTFInputStream { 102 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 103 | public: 104 | typedef CharType Ch; 105 | 106 | //! Constructor. 107 | /*! 108 | \param is input stream to be wrapped. 109 | \param type UTF encoding type if it is not detected from the stream. 110 | */ 111 | AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) { 112 | RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); 113 | DetectType(); 114 | static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) }; 115 | takeFunc_ = f[type_]; 116 | current_ = takeFunc_(*is_); 117 | } 118 | 119 | UTFType GetType() const { return type_; } 120 | bool HasBOM() const { return hasBOM_; } 121 | 122 | Ch Peek() const { return current_; } 123 | Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; } 124 | size_t Tell() const { return is_->Tell(); } 125 | 126 | // Not implemented 127 | void Put(Ch) { RAPIDJSON_ASSERT(false); } 128 | void Flush() { RAPIDJSON_ASSERT(false); } 129 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 130 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 131 | 132 | private: 133 | AutoUTFInputStream(const AutoUTFInputStream&); 134 | AutoUTFInputStream& operator=(const AutoUTFInputStream&); 135 | 136 | // Detect encoding type with BOM or RFC 4627 137 | void DetectType() { 138 | // BOM (Byte Order Mark): 139 | // 00 00 FE FF UTF-32BE 140 | // FF FE 00 00 UTF-32LE 141 | // FE FF UTF-16BE 142 | // FF FE UTF-16LE 143 | // EF BB BF UTF-8 144 | 145 | const unsigned char* c = (const unsigned char *)is_->Peek4(); 146 | if (!c) 147 | return; 148 | 149 | unsigned bom = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24); 150 | hasBOM_ = false; 151 | if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } 152 | else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } 153 | else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take(); } 154 | else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take(); } 155 | else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); } 156 | 157 | // RFC 4627: Section 3 158 | // "Since the first two characters of a JSON text will always be ASCII 159 | // characters [RFC0020], it is possible to determine whether an octet 160 | // stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking 161 | // at the pattern of nulls in the first four octets." 162 | // 00 00 00 xx UTF-32BE 163 | // 00 xx 00 xx UTF-16BE 164 | // xx 00 00 00 UTF-32LE 165 | // xx 00 xx 00 UTF-16LE 166 | // xx xx xx xx UTF-8 167 | 168 | if (!hasBOM_) { 169 | unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0); 170 | switch (pattern) { 171 | case 0x08: type_ = kUTF32BE; break; 172 | case 0x0A: type_ = kUTF16BE; break; 173 | case 0x01: type_ = kUTF32LE; break; 174 | case 0x05: type_ = kUTF16LE; break; 175 | case 0x0F: type_ = kUTF8; break; 176 | default: break; // Use type defined by user. 177 | } 178 | } 179 | 180 | // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. 181 | if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); 182 | if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); 183 | } 184 | 185 | typedef Ch (*TakeFunc)(InputByteStream& is); 186 | InputByteStream* is_; 187 | UTFType type_; 188 | Ch current_; 189 | TakeFunc takeFunc_; 190 | bool hasBOM_; 191 | }; 192 | 193 | //! Output stream wrapper with dynamically bound encoding and automatic encoding detection. 194 | /*! 195 | \tparam CharType Type of character for writing. 196 | \tparam InputByteStream type of output byte stream to be wrapped. 197 | */ 198 | template 199 | class AutoUTFOutputStream { 200 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 201 | public: 202 | typedef CharType Ch; 203 | 204 | //! Constructor. 205 | /*! 206 | \param os output stream to be wrapped. 207 | \param type UTF encoding type. 208 | \param putBOM Whether to write BOM at the beginning of the stream. 209 | */ 210 | AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) { 211 | RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); 212 | 213 | // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. 214 | if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); 215 | if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); 216 | 217 | static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) }; 218 | putFunc_ = f[type_]; 219 | 220 | if (putBOM) 221 | PutBOM(); 222 | } 223 | 224 | UTFType GetType() const { return type_; } 225 | 226 | void Put(Ch c) { putFunc_(*os_, c); } 227 | void Flush() { os_->Flush(); } 228 | 229 | // Not implemented 230 | Ch Peek() const { RAPIDJSON_ASSERT(false); } 231 | Ch Take() { RAPIDJSON_ASSERT(false); } 232 | size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } 233 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 234 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 235 | 236 | private: 237 | AutoUTFOutputStream(const AutoUTFOutputStream&); 238 | AutoUTFOutputStream& operator=(const AutoUTFOutputStream&); 239 | 240 | void PutBOM() { 241 | typedef void (*PutBOMFunc)(OutputByteStream&); 242 | static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) }; 243 | f[type_](*os_); 244 | } 245 | 246 | typedef void (*PutFunc)(OutputByteStream&, Ch); 247 | 248 | OutputByteStream* os_; 249 | UTFType type_; 250 | PutFunc putFunc_; 251 | }; 252 | 253 | #undef RAPIDJSON_ENCODINGS_FUNC 254 | 255 | RAPIDJSON_NAMESPACE_END 256 | 257 | #ifdef __GNUC__ 258 | RAPIDJSON_DIAG_POP 259 | #endif 260 | 261 | #endif // RAPIDJSON_FILESTREAM_H_ 262 | -------------------------------------------------------------------------------- /include/rapidjson/encodings.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_ENCODINGS_H_ 16 | #define RAPIDJSON_ENCODINGS_H_ 17 | 18 | #include "rapidjson.h" 19 | 20 | #ifdef _MSC_VER 21 | RAPIDJSON_DIAG_PUSH 22 | RAPIDJSON_DIAG_OFF(4244) // conversion from 'type1' to 'type2', possible loss of data 23 | RAPIDJSON_DIAG_OFF(4702) // unreachable code 24 | #elif defined(__GNUC__) 25 | RAPIDJSON_DIAG_PUSH 26 | RAPIDJSON_DIAG_OFF(effc++) 27 | RAPIDJSON_DIAG_OFF(overflow) 28 | #endif 29 | 30 | RAPIDJSON_NAMESPACE_BEGIN 31 | 32 | /////////////////////////////////////////////////////////////////////////////// 33 | // Encoding 34 | 35 | /*! \class rapidjson::Encoding 36 | \brief Concept for encoding of Unicode characters. 37 | 38 | \code 39 | concept Encoding { 40 | typename Ch; //! Type of character. A "character" is actually a code unit in unicode's definition. 41 | 42 | enum { supportUnicode = 1 }; // or 0 if not supporting unicode 43 | 44 | //! \brief Encode a Unicode codepoint to an output stream. 45 | //! \param os Output stream. 46 | //! \param codepoint An unicode codepoint, ranging from 0x0 to 0x10FFFF inclusively. 47 | template 48 | static void Encode(OutputStream& os, unsigned codepoint); 49 | 50 | //! \brief Decode a Unicode codepoint from an input stream. 51 | //! \param is Input stream. 52 | //! \param codepoint Output of the unicode codepoint. 53 | //! \return true if a valid codepoint can be decoded from the stream. 54 | template 55 | static bool Decode(InputStream& is, unsigned* codepoint); 56 | 57 | //! \brief Validate one Unicode codepoint from an encoded stream. 58 | //! \param is Input stream to obtain codepoint. 59 | //! \param os Output for copying one codepoint. 60 | //! \return true if it is valid. 61 | //! \note This function just validating and copying the codepoint without actually decode it. 62 | template 63 | static bool Validate(InputStream& is, OutputStream& os); 64 | 65 | // The following functions are deal with byte streams. 66 | 67 | //! Take a character from input byte stream, skip BOM if exist. 68 | template 69 | static CharType TakeBOM(InputByteStream& is); 70 | 71 | //! Take a character from input byte stream. 72 | template 73 | static Ch Take(InputByteStream& is); 74 | 75 | //! Put BOM to output byte stream. 76 | template 77 | static void PutBOM(OutputByteStream& os); 78 | 79 | //! Put a character to output byte stream. 80 | template 81 | static void Put(OutputByteStream& os, Ch c); 82 | }; 83 | \endcode 84 | */ 85 | 86 | /////////////////////////////////////////////////////////////////////////////// 87 | // UTF8 88 | 89 | //! UTF-8 encoding. 90 | /*! http://en.wikipedia.org/wiki/UTF-8 91 | http://tools.ietf.org/html/rfc3629 92 | \tparam CharType Code unit for storing 8-bit UTF-8 data. Default is char. 93 | \note implements Encoding concept 94 | */ 95 | template 96 | struct UTF8 { 97 | typedef CharType Ch; 98 | 99 | enum { supportUnicode = 1 }; 100 | 101 | template 102 | static void Encode(OutputStream& os, unsigned codepoint) { 103 | if (codepoint <= 0x7F) 104 | os.Put(static_cast(codepoint & 0xFF)); 105 | else if (codepoint <= 0x7FF) { 106 | os.Put(static_cast(0xC0 | ((codepoint >> 6) & 0xFF))); 107 | os.Put(static_cast(0x80 | ((codepoint & 0x3F)))); 108 | } 109 | else if (codepoint <= 0xFFFF) { 110 | os.Put(static_cast(0xE0 | ((codepoint >> 12) & 0xFF))); 111 | os.Put(static_cast(0x80 | ((codepoint >> 6) & 0x3F))); 112 | os.Put(static_cast(0x80 | (codepoint & 0x3F))); 113 | } 114 | else { 115 | RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); 116 | os.Put(static_cast(0xF0 | ((codepoint >> 18) & 0xFF))); 117 | os.Put(static_cast(0x80 | ((codepoint >> 12) & 0x3F))); 118 | os.Put(static_cast(0x80 | ((codepoint >> 6) & 0x3F))); 119 | os.Put(static_cast(0x80 | (codepoint & 0x3F))); 120 | } 121 | } 122 | 123 | template 124 | static bool Decode(InputStream& is, unsigned* codepoint) { 125 | #define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | ((unsigned char)c & 0x3Fu) 126 | #define TRANS(mask) result &= ((GetRange((unsigned char)c) & mask) != 0) 127 | #define TAIL() COPY(); TRANS(0x70) 128 | Ch c = is.Take(); 129 | if (!(c & 0x80)) { 130 | *codepoint = (unsigned char)c; 131 | return true; 132 | } 133 | 134 | unsigned char type = GetRange((unsigned char)c); 135 | *codepoint = (0xFF >> type) & (unsigned char)c; 136 | bool result = true; 137 | switch (type) { 138 | case 2: TAIL(); return result; 139 | case 3: TAIL(); TAIL(); return result; 140 | case 4: COPY(); TRANS(0x50); TAIL(); return result; 141 | case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; 142 | case 6: TAIL(); TAIL(); TAIL(); return result; 143 | case 10: COPY(); TRANS(0x20); TAIL(); return result; 144 | case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; 145 | default: return false; 146 | } 147 | #undef COPY 148 | #undef TRANS 149 | #undef TAIL 150 | } 151 | 152 | template 153 | static bool Validate(InputStream& is, OutputStream& os) { 154 | #define COPY() os.Put(c = is.Take()) 155 | #define TRANS(mask) result &= ((GetRange((unsigned char)c) & mask) != 0) 156 | #define TAIL() COPY(); TRANS(0x70) 157 | Ch c; 158 | COPY(); 159 | if (!(c & 0x80)) 160 | return true; 161 | 162 | bool result = true; 163 | switch (GetRange((unsigned char)c)) { 164 | case 2: TAIL(); return result; 165 | case 3: TAIL(); TAIL(); return result; 166 | case 4: COPY(); TRANS(0x50); TAIL(); return result; 167 | case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; 168 | case 6: TAIL(); TAIL(); TAIL(); return result; 169 | case 10: COPY(); TRANS(0x20); TAIL(); return result; 170 | case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; 171 | default: return false; 172 | } 173 | #undef COPY 174 | #undef TRANS 175 | #undef TAIL 176 | } 177 | 178 | static unsigned char GetRange(unsigned char c) { 179 | // Referring to DFA of http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ 180 | // With new mapping 1 -> 0x10, 7 -> 0x20, 9 -> 0x40, such that AND operation can test multiple types. 181 | static const unsigned char type[] = { 182 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 183 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 184 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 185 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 186 | 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10, 187 | 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40, 188 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 189 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, 190 | 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 191 | 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, 192 | }; 193 | return type[c]; 194 | } 195 | 196 | template 197 | static CharType TakeBOM(InputByteStream& is) { 198 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 199 | Ch c = Take(is); 200 | if ((unsigned char)c != 0xEFu) return c; 201 | c = is.Take(); 202 | if ((unsigned char)c != 0xBBu) return c; 203 | c = is.Take(); 204 | if ((unsigned char)c != 0xBFu) return c; 205 | c = is.Take(); 206 | return c; 207 | } 208 | 209 | template 210 | static Ch Take(InputByteStream& is) { 211 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 212 | return is.Take(); 213 | } 214 | 215 | template 216 | static void PutBOM(OutputByteStream& os) { 217 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 218 | os.Put(0xEFu); os.Put(0xBBu); os.Put(0xBFu); 219 | } 220 | 221 | template 222 | static void Put(OutputByteStream& os, Ch c) { 223 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 224 | os.Put(static_cast(c)); 225 | } 226 | }; 227 | 228 | /////////////////////////////////////////////////////////////////////////////// 229 | // UTF16 230 | 231 | //! UTF-16 encoding. 232 | /*! http://en.wikipedia.org/wiki/UTF-16 233 | http://tools.ietf.org/html/rfc2781 234 | \tparam CharType Type for storing 16-bit UTF-16 data. Default is wchar_t. C++11 may use char16_t instead. 235 | \note implements Encoding concept 236 | 237 | \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. 238 | For streaming, use UTF16LE and UTF16BE, which handle endianness. 239 | */ 240 | template 241 | struct UTF16 { 242 | typedef CharType Ch; 243 | RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 2); 244 | 245 | enum { supportUnicode = 1 }; 246 | 247 | template 248 | static void Encode(OutputStream& os, unsigned codepoint) { 249 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); 250 | if (codepoint <= 0xFFFF) { 251 | RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair 252 | os.Put(static_cast(codepoint)); 253 | } 254 | else { 255 | RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); 256 | unsigned v = codepoint - 0x10000; 257 | os.Put(static_cast((v >> 10) | 0xD800)); 258 | os.Put((v & 0x3FF) | 0xDC00); 259 | } 260 | } 261 | 262 | template 263 | static bool Decode(InputStream& is, unsigned* codepoint) { 264 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); 265 | Ch c = is.Take(); 266 | if (c < 0xD800 || c > 0xDFFF) { 267 | *codepoint = c; 268 | return true; 269 | } 270 | else if (c <= 0xDBFF) { 271 | *codepoint = (c & 0x3FF) << 10; 272 | c = is.Take(); 273 | *codepoint |= (c & 0x3FF); 274 | *codepoint += 0x10000; 275 | return c >= 0xDC00 && c <= 0xDFFF; 276 | } 277 | return false; 278 | } 279 | 280 | template 281 | static bool Validate(InputStream& is, OutputStream& os) { 282 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); 283 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); 284 | Ch c; 285 | os.Put(c = is.Take()); 286 | if (c < 0xD800 || c > 0xDFFF) 287 | return true; 288 | else if (c <= 0xDBFF) { 289 | os.Put(c = is.Take()); 290 | return c >= 0xDC00 && c <= 0xDFFF; 291 | } 292 | return false; 293 | } 294 | }; 295 | 296 | //! UTF-16 little endian encoding. 297 | template 298 | struct UTF16LE : UTF16 { 299 | template 300 | static CharType TakeBOM(InputByteStream& is) { 301 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 302 | CharType c = Take(is); 303 | return (unsigned short)c == 0xFEFFu ? Take(is) : c; 304 | } 305 | 306 | template 307 | static CharType Take(InputByteStream& is) { 308 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 309 | CharType c = (unsigned char)is.Take(); 310 | c |= (unsigned char)is.Take() << 8; 311 | return c; 312 | } 313 | 314 | template 315 | static void PutBOM(OutputByteStream& os) { 316 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 317 | os.Put(0xFFu); os.Put(0xFEu); 318 | } 319 | 320 | template 321 | static void Put(OutputByteStream& os, CharType c) { 322 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 323 | os.Put(c & 0xFFu); 324 | os.Put((c >> 8) & 0xFFu); 325 | } 326 | }; 327 | 328 | //! UTF-16 big endian encoding. 329 | template 330 | struct UTF16BE : UTF16 { 331 | template 332 | static CharType TakeBOM(InputByteStream& is) { 333 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 334 | CharType c = Take(is); 335 | return (unsigned short)c == 0xFEFFu ? Take(is) : c; 336 | } 337 | 338 | template 339 | static CharType Take(InputByteStream& is) { 340 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 341 | CharType c = (unsigned char)is.Take() << 8; 342 | c |= (unsigned char)is.Take(); 343 | return c; 344 | } 345 | 346 | template 347 | static void PutBOM(OutputByteStream& os) { 348 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 349 | os.Put(0xFEu); os.Put(0xFFu); 350 | } 351 | 352 | template 353 | static void Put(OutputByteStream& os, CharType c) { 354 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 355 | os.Put((c >> 8) & 0xFFu); 356 | os.Put(c & 0xFFu); 357 | } 358 | }; 359 | 360 | /////////////////////////////////////////////////////////////////////////////// 361 | // UTF32 362 | 363 | //! UTF-32 encoding. 364 | /*! http://en.wikipedia.org/wiki/UTF-32 365 | \tparam CharType Type for storing 32-bit UTF-32 data. Default is unsigned. C++11 may use char32_t instead. 366 | \note implements Encoding concept 367 | 368 | \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. 369 | For streaming, use UTF32LE and UTF32BE, which handle endianness. 370 | */ 371 | template 372 | struct UTF32 { 373 | typedef CharType Ch; 374 | RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 4); 375 | 376 | enum { supportUnicode = 1 }; 377 | 378 | template 379 | static void Encode(OutputStream& os, unsigned codepoint) { 380 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); 381 | RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); 382 | os.Put(codepoint); 383 | } 384 | 385 | template 386 | static bool Decode(InputStream& is, unsigned* codepoint) { 387 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); 388 | Ch c = is.Take(); 389 | *codepoint = c; 390 | return c <= 0x10FFFF; 391 | } 392 | 393 | template 394 | static bool Validate(InputStream& is, OutputStream& os) { 395 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); 396 | Ch c; 397 | os.Put(c = is.Take()); 398 | return c <= 0x10FFFF; 399 | } 400 | }; 401 | 402 | //! UTF-32 little endian enocoding. 403 | template 404 | struct UTF32LE : UTF32 { 405 | template 406 | static CharType TakeBOM(InputByteStream& is) { 407 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 408 | CharType c = Take(is); 409 | return (unsigned)c == 0x0000FEFFu ? Take(is) : c; 410 | } 411 | 412 | template 413 | static CharType Take(InputByteStream& is) { 414 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 415 | CharType c = (unsigned char)is.Take(); 416 | c |= (unsigned char)is.Take() << 8; 417 | c |= (unsigned char)is.Take() << 16; 418 | c |= (unsigned char)is.Take() << 24; 419 | return c; 420 | } 421 | 422 | template 423 | static void PutBOM(OutputByteStream& os) { 424 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 425 | os.Put(0xFFu); os.Put(0xFEu); os.Put(0x00u); os.Put(0x00u); 426 | } 427 | 428 | template 429 | static void Put(OutputByteStream& os, CharType c) { 430 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 431 | os.Put(c & 0xFFu); 432 | os.Put((c >> 8) & 0xFFu); 433 | os.Put((c >> 16) & 0xFFu); 434 | os.Put((c >> 24) & 0xFFu); 435 | } 436 | }; 437 | 438 | //! UTF-32 big endian encoding. 439 | template 440 | struct UTF32BE : UTF32 { 441 | template 442 | static CharType TakeBOM(InputByteStream& is) { 443 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 444 | CharType c = Take(is); 445 | return (unsigned)c == 0x0000FEFFu ? Take(is) : c; 446 | } 447 | 448 | template 449 | static CharType Take(InputByteStream& is) { 450 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 451 | CharType c = (unsigned char)is.Take() << 24; 452 | c |= (unsigned char)is.Take() << 16; 453 | c |= (unsigned char)is.Take() << 8; 454 | c |= (unsigned char)is.Take(); 455 | return c; 456 | } 457 | 458 | template 459 | static void PutBOM(OutputByteStream& os) { 460 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 461 | os.Put(0x00u); os.Put(0x00u); os.Put(0xFEu); os.Put(0xFFu); 462 | } 463 | 464 | template 465 | static void Put(OutputByteStream& os, CharType c) { 466 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 467 | os.Put((c >> 24) & 0xFFu); 468 | os.Put((c >> 16) & 0xFFu); 469 | os.Put((c >> 8) & 0xFFu); 470 | os.Put(c & 0xFFu); 471 | } 472 | }; 473 | 474 | /////////////////////////////////////////////////////////////////////////////// 475 | // ASCII 476 | 477 | //! ASCII encoding. 478 | /*! http://en.wikipedia.org/wiki/ASCII 479 | \tparam CharType Code unit for storing 7-bit ASCII data. Default is char. 480 | \note implements Encoding concept 481 | */ 482 | template 483 | struct ASCII { 484 | typedef CharType Ch; 485 | 486 | enum { supportUnicode = 0 }; 487 | 488 | template 489 | static void Encode(OutputStream& os, unsigned codepoint) { 490 | RAPIDJSON_ASSERT(codepoint <= 0x7F); 491 | os.Put(static_cast(codepoint & 0xFF)); 492 | } 493 | 494 | template 495 | static bool Decode(InputStream& is, unsigned* codepoint) { 496 | unsigned char c = static_cast(is.Take()); 497 | *codepoint = c; 498 | return c <= 0X7F; 499 | } 500 | 501 | template 502 | static bool Validate(InputStream& is, OutputStream& os) { 503 | unsigned char c = is.Take(); 504 | os.Put(c); 505 | return c <= 0x7F; 506 | } 507 | 508 | template 509 | static CharType TakeBOM(InputByteStream& is) { 510 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 511 | Ch c = Take(is); 512 | return c; 513 | } 514 | 515 | template 516 | static Ch Take(InputByteStream& is) { 517 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); 518 | return is.Take(); 519 | } 520 | 521 | template 522 | static void PutBOM(OutputByteStream& os) { 523 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 524 | (void)os; 525 | } 526 | 527 | template 528 | static void Put(OutputByteStream& os, Ch c) { 529 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); 530 | os.Put(static_cast(c)); 531 | } 532 | }; 533 | 534 | /////////////////////////////////////////////////////////////////////////////// 535 | // AutoUTF 536 | 537 | //! Runtime-specified UTF encoding type of a stream. 538 | enum UTFType { 539 | kUTF8 = 0, //!< UTF-8. 540 | kUTF16LE = 1, //!< UTF-16 little endian. 541 | kUTF16BE = 2, //!< UTF-16 big endian. 542 | kUTF32LE = 3, //!< UTF-32 little endian. 543 | kUTF32BE = 4 //!< UTF-32 big endian. 544 | }; 545 | 546 | //! Dynamically select encoding according to stream's runtime-specified UTF encoding type. 547 | /*! \note This class can be used with AutoUTFInputtStream and AutoUTFOutputStream, which provides GetType(). 548 | */ 549 | template 550 | struct AutoUTF { 551 | typedef CharType Ch; 552 | 553 | enum { supportUnicode = 1 }; 554 | 555 | #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x 556 | 557 | template 558 | RAPIDJSON_FORCEINLINE static void Encode(OutputStream& os, unsigned codepoint) { 559 | typedef void (*EncodeFunc)(OutputStream&, unsigned); 560 | static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Encode) }; 561 | (*f[os.GetType()])(os, codepoint); 562 | } 563 | 564 | template 565 | RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) { 566 | typedef bool (*DecodeFunc)(InputStream&, unsigned*); 567 | static const DecodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Decode) }; 568 | return (*f[is.GetType()])(is, codepoint); 569 | } 570 | 571 | template 572 | RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { 573 | typedef bool (*ValidateFunc)(InputStream&, OutputStream&); 574 | static const ValidateFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Validate) }; 575 | return (*f[is.GetType()])(is, os); 576 | } 577 | 578 | #undef RAPIDJSON_ENCODINGS_FUNC 579 | }; 580 | 581 | /////////////////////////////////////////////////////////////////////////////// 582 | // Transcoder 583 | 584 | //! Encoding conversion. 585 | template 586 | struct Transcoder { 587 | //! Take one Unicode codepoint from source encoding, convert it to target encoding and put it to the output stream. 588 | template 589 | RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { 590 | unsigned codepoint; 591 | if (!SourceEncoding::Decode(is, &codepoint)) 592 | return false; 593 | TargetEncoding::Encode(os, codepoint); 594 | return true; 595 | } 596 | 597 | //! Validate one Unicode codepoint from an encoded stream. 598 | template 599 | RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { 600 | return Transcode(is, os); // Since source/target encoding is different, must transcode. 601 | } 602 | }; 603 | 604 | //! Specialization of Transcoder with same source and target encoding. 605 | template 606 | struct Transcoder { 607 | template 608 | RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { 609 | os.Put(is.Take()); // Just copy one code unit. This semantic is different from primary template class. 610 | return true; 611 | } 612 | 613 | template 614 | RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { 615 | return Encoding::Validate(is, os); // source/target encoding are the same 616 | } 617 | }; 618 | 619 | RAPIDJSON_NAMESPACE_END 620 | 621 | #if defined(__GNUC__) || defined(_MSV_VER) 622 | RAPIDJSON_DIAG_POP 623 | #endif 624 | 625 | #endif // RAPIDJSON_ENCODINGS_H_ 626 | -------------------------------------------------------------------------------- /include/rapidjson/error/en.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_ERROR_EN_H__ 16 | #define RAPIDJSON_ERROR_EN_H__ 17 | 18 | #include "error.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | 22 | //! Maps error code of parsing into error message. 23 | /*! 24 | \ingroup RAPIDJSON_ERRORS 25 | \param parseErrorCode Error code obtained in parsing. 26 | \return the error message. 27 | \note User can make a copy of this function for localization. 28 | Using switch-case is safer for future modification of error codes. 29 | */ 30 | inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErrorCode) { 31 | switch (parseErrorCode) { 32 | case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error."); 33 | 34 | case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty."); 35 | case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not follow by other values."); 36 | 37 | case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value."); 38 | 39 | case kParseErrorObjectMissName: return RAPIDJSON_ERROR_STRING("Missing a name for object member."); 40 | case kParseErrorObjectMissColon: return RAPIDJSON_ERROR_STRING("Missing a colon after a name of object member."); 41 | case kParseErrorObjectMissCommaOrCurlyBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or '}' after an object member."); 42 | 43 | case kParseErrorArrayMissCommaOrSquareBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or ']' after an array element."); 44 | 45 | case kParseErrorStringUnicodeEscapeInvalidHex: return RAPIDJSON_ERROR_STRING("Incorrect hex digit after \\u escape in string."); 46 | case kParseErrorStringUnicodeSurrogateInvalid: return RAPIDJSON_ERROR_STRING("The surrogate pair in string is invalid."); 47 | case kParseErrorStringEscapeInvalid: return RAPIDJSON_ERROR_STRING("Invalid escape character in string."); 48 | case kParseErrorStringMissQuotationMark: return RAPIDJSON_ERROR_STRING("Missing a closing quotation mark in string."); 49 | case kParseErrorStringInvalidEncoding: return RAPIDJSON_ERROR_STRING("Invalid encoding in string."); 50 | 51 | case kParseErrorNumberTooBig: return RAPIDJSON_ERROR_STRING("Number too big to be stored in double."); 52 | case kParseErrorNumberMissFraction: return RAPIDJSON_ERROR_STRING("Miss fraction part in number."); 53 | case kParseErrorNumberMissExponent: return RAPIDJSON_ERROR_STRING("Miss exponent in number."); 54 | 55 | case kParseErrorTermination: return RAPIDJSON_ERROR_STRING("Terminate parsing due to Handler error."); 56 | case kParseErrorUnspecificSyntaxError: return RAPIDJSON_ERROR_STRING("Unspecific syntax error."); 57 | 58 | default: 59 | return RAPIDJSON_ERROR_STRING("Unknown error."); 60 | } 61 | } 62 | 63 | RAPIDJSON_NAMESPACE_END 64 | 65 | #endif // RAPIDJSON_ERROR_EN_H__ 66 | -------------------------------------------------------------------------------- /include/rapidjson/error/error.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_ERROR_ERROR_H__ 16 | #define RAPIDJSON_ERROR_ERROR_H__ 17 | 18 | #include "../rapidjson.h" 19 | 20 | /*! \file error.h */ 21 | 22 | /*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */ 23 | 24 | /////////////////////////////////////////////////////////////////////////////// 25 | // RAPIDJSON_ERROR_CHARTYPE 26 | 27 | //! Character type of error messages. 28 | /*! \ingroup RAPIDJSON_ERRORS 29 | The default character type is \c char. 30 | On Windows, user can define this macro as \c TCHAR for supporting both 31 | unicode/non-unicode settings. 32 | */ 33 | #ifndef RAPIDJSON_ERROR_CHARTYPE 34 | #define RAPIDJSON_ERROR_CHARTYPE char 35 | #endif 36 | 37 | /////////////////////////////////////////////////////////////////////////////// 38 | // RAPIDJSON_ERROR_STRING 39 | 40 | //! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[]. 41 | /*! \ingroup RAPIDJSON_ERRORS 42 | By default this conversion macro does nothing. 43 | On Windows, user can define this macro as \c _T(x) for supporting both 44 | unicode/non-unicode settings. 45 | */ 46 | #ifndef RAPIDJSON_ERROR_STRING 47 | #define RAPIDJSON_ERROR_STRING(x) x 48 | #endif 49 | 50 | RAPIDJSON_NAMESPACE_BEGIN 51 | 52 | /////////////////////////////////////////////////////////////////////////////// 53 | // ParseErrorCode 54 | 55 | //! Error code of parsing. 56 | /*! \ingroup RAPIDJSON_ERRORS 57 | \see GenericReader::Parse, GenericReader::GetParseErrorCode 58 | */ 59 | enum ParseErrorCode { 60 | kParseErrorNone = 0, //!< No error. 61 | 62 | kParseErrorDocumentEmpty, //!< The document is empty. 63 | kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values. 64 | 65 | kParseErrorValueInvalid, //!< Invalid value. 66 | 67 | kParseErrorObjectMissName, //!< Missing a name for object member. 68 | kParseErrorObjectMissColon, //!< Missing a colon after a name of object member. 69 | kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member. 70 | 71 | kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element. 72 | 73 | kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string. 74 | kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid. 75 | kParseErrorStringEscapeInvalid, //!< Invalid escape character in string. 76 | kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string. 77 | kParseErrorStringInvalidEncoding, //!< Invalid encoding in string. 78 | 79 | kParseErrorNumberTooBig, //!< Number too big to be stored in double. 80 | kParseErrorNumberMissFraction, //!< Miss fraction part in number. 81 | kParseErrorNumberMissExponent, //!< Miss exponent in number. 82 | 83 | kParseErrorTermination, //!< Parsing was terminated. 84 | kParseErrorUnspecificSyntaxError //!< Unspecific syntax error. 85 | }; 86 | 87 | //! Result of parsing (wraps ParseErrorCode) 88 | /*! 89 | \ingroup RAPIDJSON_ERRORS 90 | \code 91 | Document doc; 92 | ParseResult ok = doc.Parse("[42]"); 93 | if (!ok) { 94 | fprintf(stderr, "JSON parse error: %s (%u)", 95 | GetParseError_En(ok.Code()), ok.Offset()); 96 | exit(EXIT_FAILURE); 97 | } 98 | \endcode 99 | \see GenericReader::Parse, GenericDocument::Parse 100 | */ 101 | struct ParseResult { 102 | 103 | //! Default constructor, no error. 104 | ParseResult() : code_(kParseErrorNone), offset_(0) {} 105 | //! Constructor to set an error. 106 | ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {} 107 | 108 | //! Get the error code. 109 | ParseErrorCode Code() const { return code_; } 110 | //! Get the error offset, if \ref IsError(), 0 otherwise. 111 | size_t Offset() const { return offset_; } 112 | 113 | //! Conversion to \c bool, returns \c true, iff !\ref IsError(). 114 | operator bool() const { return !IsError(); } 115 | //! Whether the result is an error. 116 | bool IsError() const { return code_ != kParseErrorNone; } 117 | 118 | bool operator==(const ParseResult& that) const { return code_ == that.code_; } 119 | bool operator==(ParseErrorCode code) const { return code_ == code; } 120 | friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; } 121 | 122 | //! Reset error code. 123 | void Clear() { Set(kParseErrorNone); } 124 | //! Update error code and offset. 125 | void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; } 126 | 127 | private: 128 | ParseErrorCode code_; 129 | size_t offset_; 130 | }; 131 | 132 | //! Function pointer type of GetParseError(). 133 | /*! \ingroup RAPIDJSON_ERRORS 134 | 135 | This is the prototype for \c GetParseError_X(), where \c X is a locale. 136 | User can dynamically change locale in runtime, e.g.: 137 | \code 138 | GetParseErrorFunc GetParseError = GetParseError_En; // or whatever 139 | const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode()); 140 | \endcode 141 | */ 142 | typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode); 143 | 144 | RAPIDJSON_NAMESPACE_END 145 | 146 | #endif // RAPIDJSON_ERROR_ERROR_H__ 147 | -------------------------------------------------------------------------------- /include/rapidjson/filereadstream.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_FILEREADSTREAM_H_ 16 | #define RAPIDJSON_FILEREADSTREAM_H_ 17 | 18 | #include "rapidjson.h" 19 | #include 20 | 21 | RAPIDJSON_NAMESPACE_BEGIN 22 | 23 | //! File byte stream for input using fread(). 24 | /*! 25 | \note implements Stream concept 26 | */ 27 | class FileReadStream { 28 | public: 29 | typedef char Ch; //!< Character type (byte). 30 | 31 | //! Constructor. 32 | /*! 33 | \param fp File pointer opened for read. 34 | \param buffer user-supplied buffer. 35 | \param bufferSize size of buffer in bytes. Must >=4 bytes. 36 | */ 37 | FileReadStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { 38 | RAPIDJSON_ASSERT(fp_ != 0); 39 | RAPIDJSON_ASSERT(bufferSize >= 4); 40 | Read(); 41 | } 42 | 43 | Ch Peek() const { return *current_; } 44 | Ch Take() { Ch c = *current_; Read(); return c; } 45 | size_t Tell() const { return count_ + static_cast(current_ - buffer_); } 46 | 47 | // Not implemented 48 | void Put(Ch) { RAPIDJSON_ASSERT(false); } 49 | void Flush() { RAPIDJSON_ASSERT(false); } 50 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 51 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 52 | 53 | // For encoding detection only. 54 | const Ch* Peek4() const { 55 | return (current_ + 4 <= bufferLast_) ? current_ : 0; 56 | } 57 | 58 | private: 59 | void Read() { 60 | if (current_ < bufferLast_) 61 | ++current_; 62 | else if (!eof_) { 63 | count_ += readCount_; 64 | readCount_ = fread(buffer_, 1, bufferSize_, fp_); 65 | bufferLast_ = buffer_ + readCount_ - 1; 66 | current_ = buffer_; 67 | 68 | if (readCount_ < bufferSize_) { 69 | buffer_[readCount_] = '\0'; 70 | ++bufferLast_; 71 | eof_ = true; 72 | } 73 | } 74 | } 75 | 76 | std::FILE* fp_; 77 | Ch *buffer_; 78 | size_t bufferSize_; 79 | Ch *bufferLast_; 80 | Ch *current_; 81 | size_t readCount_; 82 | size_t count_; //!< Number of characters read 83 | bool eof_; 84 | }; 85 | 86 | RAPIDJSON_NAMESPACE_END 87 | 88 | #endif // RAPIDJSON_FILESTREAM_H_ 89 | -------------------------------------------------------------------------------- /include/rapidjson/filewritestream.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_FILEWRITESTREAM_H_ 16 | #define RAPIDJSON_FILEWRITESTREAM_H_ 17 | 18 | #include "rapidjson.h" 19 | #include 20 | 21 | RAPIDJSON_NAMESPACE_BEGIN 22 | 23 | //! Wrapper of C file stream for input using fread(). 24 | /*! 25 | \note implements Stream concept 26 | */ 27 | class FileWriteStream { 28 | public: 29 | typedef char Ch; //!< Character type. Only support char. 30 | 31 | FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { 32 | RAPIDJSON_ASSERT(fp_ != 0); 33 | } 34 | 35 | void Put(char c) { 36 | if (current_ >= bufferEnd_) 37 | Flush(); 38 | 39 | *current_++ = c; 40 | } 41 | 42 | void PutN(char c, size_t n) { 43 | size_t avail = static_cast(bufferEnd_ - current_); 44 | while (n > avail) { 45 | std::memset(current_, c, avail); 46 | current_ += avail; 47 | Flush(); 48 | n -= avail; 49 | avail = static_cast(bufferEnd_ - current_); 50 | } 51 | 52 | if (n > 0) { 53 | std::memset(current_, c, n); 54 | current_ += n; 55 | } 56 | } 57 | 58 | void Flush() { 59 | if (current_ != buffer_) { 60 | fwrite(buffer_, 1, static_cast(current_ - buffer_), fp_); 61 | current_ = buffer_; 62 | } 63 | } 64 | 65 | // Not implemented 66 | char Peek() const { RAPIDJSON_ASSERT(false); return 0; } 67 | char Take() { RAPIDJSON_ASSERT(false); return 0; } 68 | size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } 69 | char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 70 | size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } 71 | 72 | private: 73 | // Prohibit copy constructor & assignment operator. 74 | FileWriteStream(const FileWriteStream&); 75 | FileWriteStream& operator=(const FileWriteStream&); 76 | 77 | std::FILE* fp_; 78 | char *buffer_; 79 | char *bufferEnd_; 80 | char *current_; 81 | }; 82 | 83 | //! Implement specialized version of PutN() with memset() for better performance. 84 | template<> 85 | inline void PutN(FileWriteStream& stream, char c, size_t n) { 86 | stream.PutN(c, n); 87 | } 88 | 89 | RAPIDJSON_NAMESPACE_END 90 | 91 | #endif // RAPIDJSON_FILESTREAM_H_ 92 | -------------------------------------------------------------------------------- /include/rapidjson/internal/biginteger.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_BIGINTEGER_H_ 16 | #define RAPIDJSON_BIGINTEGER_H_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | #if defined(_MSC_VER) && defined(_M_AMD64) 21 | #include // for _umul128 22 | #endif 23 | 24 | RAPIDJSON_NAMESPACE_BEGIN 25 | namespace internal { 26 | 27 | class BigInteger { 28 | public: 29 | typedef uint64_t Type; 30 | 31 | BigInteger(const BigInteger& rhs) : count_(rhs.count_) { 32 | std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type)); 33 | } 34 | 35 | explicit BigInteger(uint64_t u) : count_(1) { 36 | digits_[0] = u; 37 | } 38 | 39 | BigInteger(const char* decimals, size_t length) : count_(1) { 40 | RAPIDJSON_ASSERT(length > 0); 41 | digits_[0] = 0; 42 | size_t i = 0; 43 | const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19 44 | while (length >= kMaxDigitPerIteration) { 45 | AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration); 46 | length -= kMaxDigitPerIteration; 47 | i += kMaxDigitPerIteration; 48 | } 49 | 50 | if (length > 0) 51 | AppendDecimal64(decimals + i, decimals + i + length); 52 | } 53 | 54 | BigInteger& operator=(uint64_t u) { 55 | digits_[0] = u; 56 | count_ = 1; 57 | return *this; 58 | } 59 | 60 | BigInteger& operator+=(uint64_t u) { 61 | Type backup = digits_[0]; 62 | digits_[0] += u; 63 | for (size_t i = 0; i < count_ - 1; i++) { 64 | if (digits_[i] >= backup) 65 | return *this; // no carry 66 | backup = digits_[i + 1]; 67 | digits_[i + 1] += 1; 68 | } 69 | 70 | // Last carry 71 | if (digits_[count_ - 1] < backup) 72 | PushBack(1); 73 | 74 | return *this; 75 | } 76 | 77 | BigInteger& operator*=(uint64_t u) { 78 | if (u == 0) return *this = 0; 79 | if (u == 1) return *this; 80 | if (*this == 1) return *this = u; 81 | 82 | uint64_t k = 0; 83 | for (size_t i = 0; i < count_; i++) { 84 | uint64_t hi; 85 | digits_[i] = MulAdd64(digits_[i], u, k, &hi); 86 | k = hi; 87 | } 88 | 89 | if (k > 0) 90 | PushBack(k); 91 | 92 | return *this; 93 | } 94 | 95 | BigInteger& operator*=(uint32_t u) { 96 | if (u == 0) return *this = 0; 97 | if (u == 1) return *this; 98 | if (*this == 1) return *this = u; 99 | 100 | uint64_t k = 0; 101 | for (size_t i = 0; i < count_; i++) { 102 | const uint64_t c = digits_[i] >> 32; 103 | const uint64_t d = digits_[i] & 0xFFFFFFFF; 104 | const uint64_t uc = u * c; 105 | const uint64_t ud = u * d; 106 | const uint64_t p0 = ud + k; 107 | const uint64_t p1 = uc + (p0 >> 32); 108 | digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32); 109 | k = p1 >> 32; 110 | } 111 | 112 | if (k > 0) 113 | PushBack(k); 114 | 115 | return *this; 116 | } 117 | 118 | BigInteger& operator<<=(size_t shift) { 119 | if (IsZero() || shift == 0) return *this; 120 | 121 | size_t offset = shift / kTypeBit; 122 | size_t interShift = shift % kTypeBit; 123 | RAPIDJSON_ASSERT(count_ + offset <= kCapacity); 124 | 125 | if (interShift == 0) { 126 | std::memmove(&digits_[count_ - 1 + offset], &digits_[count_ - 1], count_ * sizeof(Type)); 127 | count_ += offset; 128 | } 129 | else { 130 | digits_[count_] = 0; 131 | for (size_t i = count_; i > 0; i--) 132 | digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift)); 133 | digits_[offset] = digits_[0] << interShift; 134 | count_ += offset; 135 | if (digits_[count_]) 136 | count_++; 137 | } 138 | 139 | std::memset(digits_, 0, offset * sizeof(Type)); 140 | 141 | return *this; 142 | } 143 | 144 | bool operator==(const BigInteger& rhs) const { 145 | return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0; 146 | } 147 | 148 | bool operator==(const Type rhs) const { 149 | return count_ == 1 && digits_[0] == rhs; 150 | } 151 | 152 | BigInteger& MultiplyPow5(unsigned exp) { 153 | static const uint32_t kPow5[12] = { 154 | 5, 155 | 5 * 5, 156 | 5 * 5 * 5, 157 | 5 * 5 * 5 * 5, 158 | 5 * 5 * 5 * 5 * 5, 159 | 5 * 5 * 5 * 5 * 5 * 5, 160 | 5 * 5 * 5 * 5 * 5 * 5 * 5, 161 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, 162 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, 163 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, 164 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, 165 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 166 | }; 167 | if (exp == 0) return *this; 168 | for (; exp >= 27; exp -= 27) *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27 169 | for (; exp >= 13; exp -= 13) *this *= static_cast(1220703125u); // 5^13 170 | if (exp > 0) *this *= kPow5[exp - 1]; 171 | return *this; 172 | } 173 | 174 | // Compute absolute difference of this and rhs. 175 | // Assume this != rhs 176 | bool Difference(const BigInteger& rhs, BigInteger* out) const { 177 | int cmp = Compare(rhs); 178 | RAPIDJSON_ASSERT(cmp != 0); 179 | const BigInteger *a, *b; // Makes a > b 180 | bool ret; 181 | if (cmp < 0) { a = &rhs; b = this; ret = true; } 182 | else { a = this; b = &rhs; ret = false; } 183 | 184 | Type borrow = 0; 185 | for (size_t i = 0; i < a->count_; i++) { 186 | Type d = a->digits_[i] - borrow; 187 | if (i < b->count_) 188 | d -= b->digits_[i]; 189 | borrow = (d > a->digits_[i]) ? 1 : 0; 190 | out->digits_[i] = d; 191 | if (d != 0) 192 | out->count_ = i + 1; 193 | } 194 | 195 | return ret; 196 | } 197 | 198 | int Compare(const BigInteger& rhs) const { 199 | if (count_ != rhs.count_) 200 | return count_ < rhs.count_ ? -1 : 1; 201 | 202 | for (size_t i = count_; i-- > 0;) 203 | if (digits_[i] != rhs.digits_[i]) 204 | return digits_[i] < rhs.digits_[i] ? -1 : 1; 205 | 206 | return 0; 207 | } 208 | 209 | size_t GetCount() const { return count_; } 210 | Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; } 211 | bool IsZero() const { return count_ == 1 && digits_[0] == 0; } 212 | 213 | private: 214 | void AppendDecimal64(const char* begin, const char* end) { 215 | uint64_t u = ParseUint64(begin, end); 216 | if (IsZero()) 217 | *this = u; 218 | else { 219 | unsigned exp = static_cast(end - begin); 220 | (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u 221 | } 222 | } 223 | 224 | void PushBack(Type digit) { 225 | RAPIDJSON_ASSERT(count_ < kCapacity); 226 | digits_[count_++] = digit; 227 | } 228 | 229 | static uint64_t ParseUint64(const char* begin, const char* end) { 230 | uint64_t r = 0; 231 | for (const char* p = begin; p != end; ++p) { 232 | RAPIDJSON_ASSERT(*p >= '0' && *p <= '9'); 233 | r = r * 10 + (*p - '0'); 234 | } 235 | return r; 236 | } 237 | 238 | // Assume a * b + k < 2^128 239 | static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) { 240 | #if defined(_MSC_VER) && defined(_M_AMD64) 241 | uint64_t low = _umul128(a, b, outHigh) + k; 242 | if (low < k) 243 | (*outHigh)++; 244 | return low; 245 | #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) 246 | __extension__ typedef unsigned __int128 uint128; 247 | uint128 p = static_cast(a) * static_cast(b); 248 | p += k; 249 | *outHigh = static_cast(p >> 64); 250 | return static_cast(p); 251 | #else 252 | const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32; 253 | uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1; 254 | x1 += (x0 >> 32); // can't give carry 255 | x1 += x2; 256 | if (x1 < x2) 257 | x3 += (static_cast(1) << 32); 258 | uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF); 259 | uint64_t hi = x3 + (x1 >> 32); 260 | 261 | lo += k; 262 | if (lo < k) 263 | hi++; 264 | *outHigh = hi; 265 | return lo; 266 | #endif 267 | } 268 | 269 | static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000 270 | static const size_t kCapacity = kBitCount / sizeof(Type); 271 | static const size_t kTypeBit = sizeof(Type) * 8; 272 | 273 | Type digits_[kCapacity]; 274 | size_t count_; 275 | }; 276 | 277 | } // namespace internal 278 | RAPIDJSON_NAMESPACE_END 279 | 280 | #endif // RAPIDJSON_BIGINTEGER_H_ 281 | -------------------------------------------------------------------------------- /include/rapidjson/internal/diyfp.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | // This is a C++ header-only implementation of Grisu2 algorithm from the publication: 16 | // Loitsch, Florian. "Printing floating-point numbers quickly and accurately with 17 | // integers." ACM Sigplan Notices 45.6 (2010): 233-243. 18 | 19 | #ifndef RAPIDJSON_DIYFP_H_ 20 | #define RAPIDJSON_DIYFP_H_ 21 | 22 | #include "../rapidjson.h" 23 | 24 | #if defined(_MSC_VER) && defined(_M_AMD64) 25 | #include 26 | #pragma intrinsic(_BitScanReverse64) 27 | #endif 28 | 29 | RAPIDJSON_NAMESPACE_BEGIN 30 | namespace internal { 31 | 32 | #ifdef __GNUC__ 33 | RAPIDJSON_DIAG_PUSH 34 | RAPIDJSON_DIAG_OFF(effc++) 35 | #endif 36 | 37 | struct DiyFp { 38 | DiyFp() {} 39 | 40 | DiyFp(uint64_t fp, int exp) : f(fp), e(exp) {} 41 | 42 | explicit DiyFp(double d) { 43 | union { 44 | double d; 45 | uint64_t u64; 46 | } u = { d }; 47 | 48 | int biased_e = static_cast((u.u64 & kDpExponentMask) >> kDpSignificandSize); 49 | uint64_t significand = (u.u64 & kDpSignificandMask); 50 | if (biased_e != 0) { 51 | f = significand + kDpHiddenBit; 52 | e = biased_e - kDpExponentBias; 53 | } 54 | else { 55 | f = significand; 56 | e = kDpMinExponent + 1; 57 | } 58 | } 59 | 60 | DiyFp operator-(const DiyFp& rhs) const { 61 | return DiyFp(f - rhs.f, e); 62 | } 63 | 64 | DiyFp operator*(const DiyFp& rhs) const { 65 | #if defined(_MSC_VER) && defined(_M_AMD64) 66 | uint64_t h; 67 | uint64_t l = _umul128(f, rhs.f, &h); 68 | if (l & (uint64_t(1) << 63)) // rounding 69 | h++; 70 | return DiyFp(h, e + rhs.e + 64); 71 | #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) 72 | __extension__ typedef unsigned __int128 uint128; 73 | uint128 p = static_cast(f) * static_cast(rhs.f); 74 | uint64_t h = static_cast(p >> 64); 75 | uint64_t l = static_cast(p); 76 | if (l & (uint64_t(1) << 63)) // rounding 77 | h++; 78 | return DiyFp(h, e + rhs.e + 64); 79 | #else 80 | const uint64_t M32 = 0xFFFFFFFF; 81 | const uint64_t a = f >> 32; 82 | const uint64_t b = f & M32; 83 | const uint64_t c = rhs.f >> 32; 84 | const uint64_t d = rhs.f & M32; 85 | const uint64_t ac = a * c; 86 | const uint64_t bc = b * c; 87 | const uint64_t ad = a * d; 88 | const uint64_t bd = b * d; 89 | uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32); 90 | tmp += 1U << 31; /// mult_round 91 | return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), e + rhs.e + 64); 92 | #endif 93 | } 94 | 95 | DiyFp Normalize() const { 96 | #if defined(_MSC_VER) && defined(_M_AMD64) 97 | unsigned long index; 98 | _BitScanReverse64(&index, f); 99 | return DiyFp(f << (63 - index), e - (63 - index)); 100 | #elif defined(__GNUC__) && __GNUC__ >= 4 101 | int s = __builtin_clzll(f); 102 | return DiyFp(f << s, e - s); 103 | #else 104 | DiyFp res = *this; 105 | while (!(res.f & (static_cast(1) << 63))) { 106 | res.f <<= 1; 107 | res.e--; 108 | } 109 | return res; 110 | #endif 111 | } 112 | 113 | DiyFp NormalizeBoundary() const { 114 | DiyFp res = *this; 115 | while (!(res.f & (kDpHiddenBit << 1))) { 116 | res.f <<= 1; 117 | res.e--; 118 | } 119 | res.f <<= (kDiySignificandSize - kDpSignificandSize - 2); 120 | res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 2); 121 | return res; 122 | } 123 | 124 | void NormalizedBoundaries(DiyFp* minus, DiyFp* plus) const { 125 | DiyFp pl = DiyFp((f << 1) + 1, e - 1).NormalizeBoundary(); 126 | DiyFp mi = (f == kDpHiddenBit) ? DiyFp((f << 2) - 1, e - 2) : DiyFp((f << 1) - 1, e - 1); 127 | mi.f <<= mi.e - pl.e; 128 | mi.e = pl.e; 129 | *plus = pl; 130 | *minus = mi; 131 | } 132 | 133 | double ToDouble() const { 134 | union { 135 | double d; 136 | uint64_t u64; 137 | }u; 138 | const uint64_t be = (e == kDpDenormalExponent && (f & kDpHiddenBit) == 0) ? 0 : 139 | static_cast(e + kDpExponentBias); 140 | u.u64 = (f & kDpSignificandMask) | (be << kDpSignificandSize); 141 | return u.d; 142 | } 143 | 144 | static const int kDiySignificandSize = 64; 145 | static const int kDpSignificandSize = 52; 146 | static const int kDpExponentBias = 0x3FF + kDpSignificandSize; 147 | static const int kDpMaxExponent = 0x7FF - kDpExponentBias; 148 | static const int kDpMinExponent = -kDpExponentBias; 149 | static const int kDpDenormalExponent = -kDpExponentBias + 1; 150 | static const uint64_t kDpExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); 151 | static const uint64_t kDpSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); 152 | static const uint64_t kDpHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); 153 | 154 | uint64_t f; 155 | int e; 156 | }; 157 | 158 | inline DiyFp GetCachedPowerByIndex(size_t index) { 159 | // 10^-348, 10^-340, ..., 10^340 160 | static const uint64_t kCachedPowers_F[] = { 161 | RAPIDJSON_UINT64_C2(0xfa8fd5a0, 0x081c0288), RAPIDJSON_UINT64_C2(0xbaaee17f, 0xa23ebf76), 162 | RAPIDJSON_UINT64_C2(0x8b16fb20, 0x3055ac76), RAPIDJSON_UINT64_C2(0xcf42894a, 0x5dce35ea), 163 | RAPIDJSON_UINT64_C2(0x9a6bb0aa, 0x55653b2d), RAPIDJSON_UINT64_C2(0xe61acf03, 0x3d1a45df), 164 | RAPIDJSON_UINT64_C2(0xab70fe17, 0xc79ac6ca), RAPIDJSON_UINT64_C2(0xff77b1fc, 0xbebcdc4f), 165 | RAPIDJSON_UINT64_C2(0xbe5691ef, 0x416bd60c), RAPIDJSON_UINT64_C2(0x8dd01fad, 0x907ffc3c), 166 | RAPIDJSON_UINT64_C2(0xd3515c28, 0x31559a83), RAPIDJSON_UINT64_C2(0x9d71ac8f, 0xada6c9b5), 167 | RAPIDJSON_UINT64_C2(0xea9c2277, 0x23ee8bcb), RAPIDJSON_UINT64_C2(0xaecc4991, 0x4078536d), 168 | RAPIDJSON_UINT64_C2(0x823c1279, 0x5db6ce57), RAPIDJSON_UINT64_C2(0xc2109436, 0x4dfb5637), 169 | RAPIDJSON_UINT64_C2(0x9096ea6f, 0x3848984f), RAPIDJSON_UINT64_C2(0xd77485cb, 0x25823ac7), 170 | RAPIDJSON_UINT64_C2(0xa086cfcd, 0x97bf97f4), RAPIDJSON_UINT64_C2(0xef340a98, 0x172aace5), 171 | RAPIDJSON_UINT64_C2(0xb23867fb, 0x2a35b28e), RAPIDJSON_UINT64_C2(0x84c8d4df, 0xd2c63f3b), 172 | RAPIDJSON_UINT64_C2(0xc5dd4427, 0x1ad3cdba), RAPIDJSON_UINT64_C2(0x936b9fce, 0xbb25c996), 173 | RAPIDJSON_UINT64_C2(0xdbac6c24, 0x7d62a584), RAPIDJSON_UINT64_C2(0xa3ab6658, 0x0d5fdaf6), 174 | RAPIDJSON_UINT64_C2(0xf3e2f893, 0xdec3f126), RAPIDJSON_UINT64_C2(0xb5b5ada8, 0xaaff80b8), 175 | RAPIDJSON_UINT64_C2(0x87625f05, 0x6c7c4a8b), RAPIDJSON_UINT64_C2(0xc9bcff60, 0x34c13053), 176 | RAPIDJSON_UINT64_C2(0x964e858c, 0x91ba2655), RAPIDJSON_UINT64_C2(0xdff97724, 0x70297ebd), 177 | RAPIDJSON_UINT64_C2(0xa6dfbd9f, 0xb8e5b88f), RAPIDJSON_UINT64_C2(0xf8a95fcf, 0x88747d94), 178 | RAPIDJSON_UINT64_C2(0xb9447093, 0x8fa89bcf), RAPIDJSON_UINT64_C2(0x8a08f0f8, 0xbf0f156b), 179 | RAPIDJSON_UINT64_C2(0xcdb02555, 0x653131b6), RAPIDJSON_UINT64_C2(0x993fe2c6, 0xd07b7fac), 180 | RAPIDJSON_UINT64_C2(0xe45c10c4, 0x2a2b3b06), RAPIDJSON_UINT64_C2(0xaa242499, 0x697392d3), 181 | RAPIDJSON_UINT64_C2(0xfd87b5f2, 0x8300ca0e), RAPIDJSON_UINT64_C2(0xbce50864, 0x92111aeb), 182 | RAPIDJSON_UINT64_C2(0x8cbccc09, 0x6f5088cc), RAPIDJSON_UINT64_C2(0xd1b71758, 0xe219652c), 183 | RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), RAPIDJSON_UINT64_C2(0xe8d4a510, 0x00000000), 184 | RAPIDJSON_UINT64_C2(0xad78ebc5, 0xac620000), RAPIDJSON_UINT64_C2(0x813f3978, 0xf8940984), 185 | RAPIDJSON_UINT64_C2(0xc097ce7b, 0xc90715b3), RAPIDJSON_UINT64_C2(0x8f7e32ce, 0x7bea5c70), 186 | RAPIDJSON_UINT64_C2(0xd5d238a4, 0xabe98068), RAPIDJSON_UINT64_C2(0x9f4f2726, 0x179a2245), 187 | RAPIDJSON_UINT64_C2(0xed63a231, 0xd4c4fb27), RAPIDJSON_UINT64_C2(0xb0de6538, 0x8cc8ada8), 188 | RAPIDJSON_UINT64_C2(0x83c7088e, 0x1aab65db), RAPIDJSON_UINT64_C2(0xc45d1df9, 0x42711d9a), 189 | RAPIDJSON_UINT64_C2(0x924d692c, 0xa61be758), RAPIDJSON_UINT64_C2(0xda01ee64, 0x1a708dea), 190 | RAPIDJSON_UINT64_C2(0xa26da399, 0x9aef774a), RAPIDJSON_UINT64_C2(0xf209787b, 0xb47d6b85), 191 | RAPIDJSON_UINT64_C2(0xb454e4a1, 0x79dd1877), RAPIDJSON_UINT64_C2(0x865b8692, 0x5b9bc5c2), 192 | RAPIDJSON_UINT64_C2(0xc83553c5, 0xc8965d3d), RAPIDJSON_UINT64_C2(0x952ab45c, 0xfa97a0b3), 193 | RAPIDJSON_UINT64_C2(0xde469fbd, 0x99a05fe3), RAPIDJSON_UINT64_C2(0xa59bc234, 0xdb398c25), 194 | RAPIDJSON_UINT64_C2(0xf6c69a72, 0xa3989f5c), RAPIDJSON_UINT64_C2(0xb7dcbf53, 0x54e9bece), 195 | RAPIDJSON_UINT64_C2(0x88fcf317, 0xf22241e2), RAPIDJSON_UINT64_C2(0xcc20ce9b, 0xd35c78a5), 196 | RAPIDJSON_UINT64_C2(0x98165af3, 0x7b2153df), RAPIDJSON_UINT64_C2(0xe2a0b5dc, 0x971f303a), 197 | RAPIDJSON_UINT64_C2(0xa8d9d153, 0x5ce3b396), RAPIDJSON_UINT64_C2(0xfb9b7cd9, 0xa4a7443c), 198 | RAPIDJSON_UINT64_C2(0xbb764c4c, 0xa7a44410), RAPIDJSON_UINT64_C2(0x8bab8eef, 0xb6409c1a), 199 | RAPIDJSON_UINT64_C2(0xd01fef10, 0xa657842c), RAPIDJSON_UINT64_C2(0x9b10a4e5, 0xe9913129), 200 | RAPIDJSON_UINT64_C2(0xe7109bfb, 0xa19c0c9d), RAPIDJSON_UINT64_C2(0xac2820d9, 0x623bf429), 201 | RAPIDJSON_UINT64_C2(0x80444b5e, 0x7aa7cf85), RAPIDJSON_UINT64_C2(0xbf21e440, 0x03acdd2d), 202 | RAPIDJSON_UINT64_C2(0x8e679c2f, 0x5e44ff8f), RAPIDJSON_UINT64_C2(0xd433179d, 0x9c8cb841), 203 | RAPIDJSON_UINT64_C2(0x9e19db92, 0xb4e31ba9), RAPIDJSON_UINT64_C2(0xeb96bf6e, 0xbadf77d9), 204 | RAPIDJSON_UINT64_C2(0xaf87023b, 0x9bf0ee6b) 205 | }; 206 | static const int16_t kCachedPowers_E[] = { 207 | -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, 208 | -954, -927, -901, -874, -847, -821, -794, -768, -741, -715, 209 | -688, -661, -635, -608, -582, -555, -529, -502, -475, -449, 210 | -422, -396, -369, -343, -316, -289, -263, -236, -210, -183, 211 | -157, -130, -103, -77, -50, -24, 3, 30, 56, 83, 212 | 109, 136, 162, 189, 216, 242, 269, 295, 322, 348, 213 | 375, 402, 428, 455, 481, 508, 534, 561, 588, 614, 214 | 641, 667, 694, 720, 747, 774, 800, 827, 853, 880, 215 | 907, 933, 960, 986, 1013, 1039, 1066 216 | }; 217 | return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]); 218 | } 219 | 220 | inline DiyFp GetCachedPower(int e, int* K) { 221 | 222 | //int k = static_cast(ceil((-61 - e) * 0.30102999566398114)) + 374; 223 | double dk = (-61 - e) * 0.30102999566398114 + 347; // dk must be positive, so can do ceiling in positive 224 | int k = static_cast(dk); 225 | if (dk - k > 0.0) 226 | k++; 227 | 228 | unsigned index = static_cast((k >> 3) + 1); 229 | *K = -(-348 + static_cast(index << 3)); // decimal exponent no need lookup table 230 | 231 | return GetCachedPowerByIndex(index); 232 | } 233 | 234 | inline DiyFp GetCachedPower10(int exp, int *outExp) { 235 | unsigned index = (exp + 348) / 8; 236 | *outExp = -348 + index * 8; 237 | return GetCachedPowerByIndex(index); 238 | } 239 | 240 | #ifdef __GNUC__ 241 | RAPIDJSON_DIAG_POP 242 | #endif 243 | 244 | } // namespace internal 245 | RAPIDJSON_NAMESPACE_END 246 | 247 | #endif // RAPIDJSON_DIYFP_H_ 248 | -------------------------------------------------------------------------------- /include/rapidjson/internal/dtoa.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | // This is a C++ header-only implementation of Grisu2 algorithm from the publication: 16 | // Loitsch, Florian. "Printing floating-point numbers quickly and accurately with 17 | // integers." ACM Sigplan Notices 45.6 (2010): 233-243. 18 | 19 | #ifndef RAPIDJSON_DTOA_ 20 | #define RAPIDJSON_DTOA_ 21 | 22 | #include "itoa.h" // GetDigitsLut() 23 | #include "diyfp.h" 24 | #include "ieee754.h" 25 | 26 | RAPIDJSON_NAMESPACE_BEGIN 27 | namespace internal { 28 | 29 | #ifdef __GNUC__ 30 | RAPIDJSON_DIAG_PUSH 31 | RAPIDJSON_DIAG_OFF(effc++) 32 | #endif 33 | 34 | inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) { 35 | while (rest < wp_w && delta - rest >= ten_kappa && 36 | (rest + ten_kappa < wp_w || /// closer 37 | wp_w - rest > rest + ten_kappa - wp_w)) { 38 | buffer[len - 1]--; 39 | rest += ten_kappa; 40 | } 41 | } 42 | 43 | inline unsigned CountDecimalDigit32(uint32_t n) { 44 | // Simple pure C++ implementation was faster than __builtin_clz version in this situation. 45 | if (n < 10) return 1; 46 | if (n < 100) return 2; 47 | if (n < 1000) return 3; 48 | if (n < 10000) return 4; 49 | if (n < 100000) return 5; 50 | if (n < 1000000) return 6; 51 | if (n < 10000000) return 7; 52 | if (n < 100000000) return 8; 53 | // Will not reach 10 digits in DigitGen() 54 | //if (n < 1000000000) return 9; 55 | //return 10; 56 | return 9; 57 | } 58 | 59 | inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) { 60 | static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; 61 | const DiyFp one(uint64_t(1) << -Mp.e, Mp.e); 62 | const DiyFp wp_w = Mp - W; 63 | uint32_t p1 = static_cast(Mp.f >> -one.e); 64 | uint64_t p2 = Mp.f & (one.f - 1); 65 | int kappa = CountDecimalDigit32(p1); // kappa in [0, 9] 66 | *len = 0; 67 | 68 | while (kappa > 0) { 69 | uint32_t d = 0; 70 | switch (kappa) { 71 | case 9: d = p1 / 100000000; p1 %= 100000000; break; 72 | case 8: d = p1 / 10000000; p1 %= 10000000; break; 73 | case 7: d = p1 / 1000000; p1 %= 1000000; break; 74 | case 6: d = p1 / 100000; p1 %= 100000; break; 75 | case 5: d = p1 / 10000; p1 %= 10000; break; 76 | case 4: d = p1 / 1000; p1 %= 1000; break; 77 | case 3: d = p1 / 100; p1 %= 100; break; 78 | case 2: d = p1 / 10; p1 %= 10; break; 79 | case 1: d = p1; p1 = 0; break; 80 | default:; 81 | } 82 | if (d || *len) 83 | buffer[(*len)++] = static_cast('0' + static_cast(d)); 84 | kappa--; 85 | uint64_t tmp = (static_cast(p1) << -one.e) + p2; 86 | if (tmp <= delta) { 87 | *K += kappa; 88 | GrisuRound(buffer, *len, delta, tmp, static_cast(kPow10[kappa]) << -one.e, wp_w.f); 89 | return; 90 | } 91 | } 92 | 93 | // kappa = 0 94 | for (;;) { 95 | p2 *= 10; 96 | delta *= 10; 97 | char d = static_cast(p2 >> -one.e); 98 | if (d || *len) 99 | buffer[(*len)++] = static_cast('0' + d); 100 | p2 &= one.f - 1; 101 | kappa--; 102 | if (p2 < delta) { 103 | *K += kappa; 104 | GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * kPow10[-kappa]); 105 | return; 106 | } 107 | } 108 | } 109 | 110 | inline void Grisu2(double value, char* buffer, int* length, int* K) { 111 | const DiyFp v(value); 112 | DiyFp w_m, w_p; 113 | v.NormalizedBoundaries(&w_m, &w_p); 114 | 115 | const DiyFp c_mk = GetCachedPower(w_p.e, K); 116 | const DiyFp W = v.Normalize() * c_mk; 117 | DiyFp Wp = w_p * c_mk; 118 | DiyFp Wm = w_m * c_mk; 119 | Wm.f++; 120 | Wp.f--; 121 | DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K); 122 | } 123 | 124 | inline char* WriteExponent(int K, char* buffer) { 125 | if (K < 0) { 126 | *buffer++ = '-'; 127 | K = -K; 128 | } 129 | 130 | if (K >= 100) { 131 | *buffer++ = static_cast('0' + static_cast(K / 100)); 132 | K %= 100; 133 | const char* d = GetDigitsLut() + K * 2; 134 | *buffer++ = d[0]; 135 | *buffer++ = d[1]; 136 | } 137 | else if (K >= 10) { 138 | const char* d = GetDigitsLut() + K * 2; 139 | *buffer++ = d[0]; 140 | *buffer++ = d[1]; 141 | } 142 | else 143 | *buffer++ = static_cast('0' + static_cast(K)); 144 | 145 | return buffer; 146 | } 147 | 148 | inline char* Prettify(char* buffer, int length, int k) { 149 | const int kk = length + k; // 10^(kk-1) <= v < 10^kk 150 | 151 | if (length <= kk && kk <= 21) { 152 | // 1234e7 -> 12340000000 153 | for (int i = length; i < kk; i++) 154 | buffer[i] = '0'; 155 | buffer[kk] = '.'; 156 | buffer[kk + 1] = '0'; 157 | return &buffer[kk + 2]; 158 | } 159 | else if (0 < kk && kk <= 21) { 160 | // 1234e-2 -> 12.34 161 | std::memmove(&buffer[kk + 1], &buffer[kk], length - kk); 162 | buffer[kk] = '.'; 163 | return &buffer[length + 1]; 164 | } 165 | else if (-6 < kk && kk <= 0) { 166 | // 1234e-6 -> 0.001234 167 | const int offset = 2 - kk; 168 | std::memmove(&buffer[offset], &buffer[0], length); 169 | buffer[0] = '0'; 170 | buffer[1] = '.'; 171 | for (int i = 2; i < offset; i++) 172 | buffer[i] = '0'; 173 | return &buffer[length + offset]; 174 | } 175 | else if (length == 1) { 176 | // 1e30 177 | buffer[1] = 'e'; 178 | return WriteExponent(kk - 1, &buffer[2]); 179 | } 180 | else { 181 | // 1234e30 -> 1.234e33 182 | std::memmove(&buffer[2], &buffer[1], length - 1); 183 | buffer[1] = '.'; 184 | buffer[length + 1] = 'e'; 185 | return WriteExponent(kk - 1, &buffer[0 + length + 2]); 186 | } 187 | } 188 | 189 | inline char* dtoa(double value, char* buffer) { 190 | Double d(value); 191 | if (d.IsZero()) { 192 | if (d.Sign()) 193 | *buffer++ = '-'; // -0.0, Issue #289 194 | buffer[0] = '0'; 195 | buffer[1] = '.'; 196 | buffer[2] = '0'; 197 | return &buffer[3]; 198 | } 199 | else { 200 | if (value < 0) { 201 | *buffer++ = '-'; 202 | value = -value; 203 | } 204 | int length, K; 205 | Grisu2(value, buffer, &length, &K); 206 | return Prettify(buffer, length, K); 207 | } 208 | } 209 | 210 | #ifdef __GNUC__ 211 | RAPIDJSON_DIAG_POP 212 | #endif 213 | 214 | } // namespace internal 215 | RAPIDJSON_NAMESPACE_END 216 | 217 | #endif // RAPIDJSON_DTOA_ 218 | -------------------------------------------------------------------------------- /include/rapidjson/internal/ieee754.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_IEEE754_ 16 | #define RAPIDJSON_IEEE754_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | namespace internal { 22 | 23 | class Double { 24 | public: 25 | Double() {} 26 | Double(double d) : d_(d) {} 27 | Double(uint64_t u) : u_(u) {} 28 | 29 | double Value() const { return d_; } 30 | uint64_t Uint64Value() const { return u_; } 31 | 32 | double NextPositiveDouble() const { 33 | RAPIDJSON_ASSERT(!Sign()); 34 | return Double(u_ + 1).Value(); 35 | } 36 | 37 | bool Sign() const { return (u_ & kSignMask) != 0; } 38 | uint64_t Significand() const { return u_ & kSignificandMask; } 39 | int Exponent() const { return static_cast(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias); } 40 | 41 | bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; } 42 | bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; } 43 | bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; } 44 | bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; } 45 | 46 | uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); } 47 | int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; } 48 | uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; } 49 | 50 | static unsigned EffectiveSignificandSize(int order) { 51 | if (order >= -1021) 52 | return 53; 53 | else if (order <= -1074) 54 | return 0; 55 | else 56 | return order + 1074; 57 | } 58 | 59 | private: 60 | static const int kSignificandSize = 52; 61 | static const int kExponentBias = 0x3FF; 62 | static const int kDenormalExponent = 1 - kExponentBias; 63 | static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000); 64 | static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); 65 | static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); 66 | static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); 67 | 68 | union { 69 | double d_; 70 | uint64_t u_; 71 | }; 72 | }; 73 | 74 | } // namespace internal 75 | RAPIDJSON_NAMESPACE_END 76 | 77 | #endif // RAPIDJSON_IEEE754_ 78 | -------------------------------------------------------------------------------- /include/rapidjson/internal/itoa.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_ITOA_ 16 | #define RAPIDJSON_ITOA_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | namespace internal { 22 | 23 | inline const char* GetDigitsLut() { 24 | static const char cDigitsLut[200] = { 25 | '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9', 26 | '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9', 27 | '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9', 28 | '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9', 29 | '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9', 30 | '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9', 31 | '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9', 32 | '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9', 33 | '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9', 34 | '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9' 35 | }; 36 | return cDigitsLut; 37 | } 38 | 39 | inline char* u32toa(uint32_t value, char* buffer) { 40 | const char* cDigitsLut = GetDigitsLut(); 41 | 42 | if (value < 10000) { 43 | const uint32_t d1 = (value / 100) << 1; 44 | const uint32_t d2 = (value % 100) << 1; 45 | 46 | if (value >= 1000) 47 | *buffer++ = cDigitsLut[d1]; 48 | if (value >= 100) 49 | *buffer++ = cDigitsLut[d1 + 1]; 50 | if (value >= 10) 51 | *buffer++ = cDigitsLut[d2]; 52 | *buffer++ = cDigitsLut[d2 + 1]; 53 | } 54 | else if (value < 100000000) { 55 | // value = bbbbcccc 56 | const uint32_t b = value / 10000; 57 | const uint32_t c = value % 10000; 58 | 59 | const uint32_t d1 = (b / 100) << 1; 60 | const uint32_t d2 = (b % 100) << 1; 61 | 62 | const uint32_t d3 = (c / 100) << 1; 63 | const uint32_t d4 = (c % 100) << 1; 64 | 65 | if (value >= 10000000) 66 | *buffer++ = cDigitsLut[d1]; 67 | if (value >= 1000000) 68 | *buffer++ = cDigitsLut[d1 + 1]; 69 | if (value >= 100000) 70 | *buffer++ = cDigitsLut[d2]; 71 | *buffer++ = cDigitsLut[d2 + 1]; 72 | 73 | *buffer++ = cDigitsLut[d3]; 74 | *buffer++ = cDigitsLut[d3 + 1]; 75 | *buffer++ = cDigitsLut[d4]; 76 | *buffer++ = cDigitsLut[d4 + 1]; 77 | } 78 | else { 79 | // value = aabbbbcccc in decimal 80 | 81 | const uint32_t a = value / 100000000; // 1 to 42 82 | value %= 100000000; 83 | 84 | if (a >= 10) { 85 | const unsigned i = a << 1; 86 | *buffer++ = cDigitsLut[i]; 87 | *buffer++ = cDigitsLut[i + 1]; 88 | } 89 | else 90 | *buffer++ = static_cast('0' + static_cast(a)); 91 | 92 | const uint32_t b = value / 10000; // 0 to 9999 93 | const uint32_t c = value % 10000; // 0 to 9999 94 | 95 | const uint32_t d1 = (b / 100) << 1; 96 | const uint32_t d2 = (b % 100) << 1; 97 | 98 | const uint32_t d3 = (c / 100) << 1; 99 | const uint32_t d4 = (c % 100) << 1; 100 | 101 | *buffer++ = cDigitsLut[d1]; 102 | *buffer++ = cDigitsLut[d1 + 1]; 103 | *buffer++ = cDigitsLut[d2]; 104 | *buffer++ = cDigitsLut[d2 + 1]; 105 | *buffer++ = cDigitsLut[d3]; 106 | *buffer++ = cDigitsLut[d3 + 1]; 107 | *buffer++ = cDigitsLut[d4]; 108 | *buffer++ = cDigitsLut[d4 + 1]; 109 | } 110 | return buffer; 111 | } 112 | 113 | inline char* i32toa(int32_t value, char* buffer) { 114 | uint32_t u = static_cast(value); 115 | if (value < 0) { 116 | *buffer++ = '-'; 117 | u = ~u + 1; 118 | } 119 | 120 | return u32toa(u, buffer); 121 | } 122 | 123 | inline char* u64toa(uint64_t value, char* buffer) { 124 | const char* cDigitsLut = GetDigitsLut(); 125 | const uint64_t kTen8 = 100000000; 126 | const uint64_t kTen9 = kTen8 * 10; 127 | const uint64_t kTen10 = kTen8 * 100; 128 | const uint64_t kTen11 = kTen8 * 1000; 129 | const uint64_t kTen12 = kTen8 * 10000; 130 | const uint64_t kTen13 = kTen8 * 100000; 131 | const uint64_t kTen14 = kTen8 * 1000000; 132 | const uint64_t kTen15 = kTen8 * 10000000; 133 | const uint64_t kTen16 = kTen8 * kTen8; 134 | 135 | if (value < kTen8) { 136 | uint32_t v = static_cast(value); 137 | if (v < 10000) { 138 | const uint32_t d1 = (v / 100) << 1; 139 | const uint32_t d2 = (v % 100) << 1; 140 | 141 | if (v >= 1000) 142 | *buffer++ = cDigitsLut[d1]; 143 | if (v >= 100) 144 | *buffer++ = cDigitsLut[d1 + 1]; 145 | if (v >= 10) 146 | *buffer++ = cDigitsLut[d2]; 147 | *buffer++ = cDigitsLut[d2 + 1]; 148 | } 149 | else { 150 | // value = bbbbcccc 151 | const uint32_t b = v / 10000; 152 | const uint32_t c = v % 10000; 153 | 154 | const uint32_t d1 = (b / 100) << 1; 155 | const uint32_t d2 = (b % 100) << 1; 156 | 157 | const uint32_t d3 = (c / 100) << 1; 158 | const uint32_t d4 = (c % 100) << 1; 159 | 160 | if (value >= 10000000) 161 | *buffer++ = cDigitsLut[d1]; 162 | if (value >= 1000000) 163 | *buffer++ = cDigitsLut[d1 + 1]; 164 | if (value >= 100000) 165 | *buffer++ = cDigitsLut[d2]; 166 | *buffer++ = cDigitsLut[d2 + 1]; 167 | 168 | *buffer++ = cDigitsLut[d3]; 169 | *buffer++ = cDigitsLut[d3 + 1]; 170 | *buffer++ = cDigitsLut[d4]; 171 | *buffer++ = cDigitsLut[d4 + 1]; 172 | } 173 | } 174 | else if (value < kTen16) { 175 | const uint32_t v0 = static_cast(value / kTen8); 176 | const uint32_t v1 = static_cast(value % kTen8); 177 | 178 | const uint32_t b0 = v0 / 10000; 179 | const uint32_t c0 = v0 % 10000; 180 | 181 | const uint32_t d1 = (b0 / 100) << 1; 182 | const uint32_t d2 = (b0 % 100) << 1; 183 | 184 | const uint32_t d3 = (c0 / 100) << 1; 185 | const uint32_t d4 = (c0 % 100) << 1; 186 | 187 | const uint32_t b1 = v1 / 10000; 188 | const uint32_t c1 = v1 % 10000; 189 | 190 | const uint32_t d5 = (b1 / 100) << 1; 191 | const uint32_t d6 = (b1 % 100) << 1; 192 | 193 | const uint32_t d7 = (c1 / 100) << 1; 194 | const uint32_t d8 = (c1 % 100) << 1; 195 | 196 | if (value >= kTen15) 197 | *buffer++ = cDigitsLut[d1]; 198 | if (value >= kTen14) 199 | *buffer++ = cDigitsLut[d1 + 1]; 200 | if (value >= kTen13) 201 | *buffer++ = cDigitsLut[d2]; 202 | if (value >= kTen12) 203 | *buffer++ = cDigitsLut[d2 + 1]; 204 | if (value >= kTen11) 205 | *buffer++ = cDigitsLut[d3]; 206 | if (value >= kTen10) 207 | *buffer++ = cDigitsLut[d3 + 1]; 208 | if (value >= kTen9) 209 | *buffer++ = cDigitsLut[d4]; 210 | if (value >= kTen8) 211 | *buffer++ = cDigitsLut[d4 + 1]; 212 | 213 | *buffer++ = cDigitsLut[d5]; 214 | *buffer++ = cDigitsLut[d5 + 1]; 215 | *buffer++ = cDigitsLut[d6]; 216 | *buffer++ = cDigitsLut[d6 + 1]; 217 | *buffer++ = cDigitsLut[d7]; 218 | *buffer++ = cDigitsLut[d7 + 1]; 219 | *buffer++ = cDigitsLut[d8]; 220 | *buffer++ = cDigitsLut[d8 + 1]; 221 | } 222 | else { 223 | const uint32_t a = static_cast(value / kTen16); // 1 to 1844 224 | value %= kTen16; 225 | 226 | if (a < 10) 227 | *buffer++ = static_cast('0' + static_cast(a)); 228 | else if (a < 100) { 229 | const uint32_t i = a << 1; 230 | *buffer++ = cDigitsLut[i]; 231 | *buffer++ = cDigitsLut[i + 1]; 232 | } 233 | else if (a < 1000) { 234 | *buffer++ = static_cast('0' + static_cast(a / 100)); 235 | 236 | const uint32_t i = (a % 100) << 1; 237 | *buffer++ = cDigitsLut[i]; 238 | *buffer++ = cDigitsLut[i + 1]; 239 | } 240 | else { 241 | const uint32_t i = (a / 100) << 1; 242 | const uint32_t j = (a % 100) << 1; 243 | *buffer++ = cDigitsLut[i]; 244 | *buffer++ = cDigitsLut[i + 1]; 245 | *buffer++ = cDigitsLut[j]; 246 | *buffer++ = cDigitsLut[j + 1]; 247 | } 248 | 249 | const uint32_t v0 = static_cast(value / kTen8); 250 | const uint32_t v1 = static_cast(value % kTen8); 251 | 252 | const uint32_t b0 = v0 / 10000; 253 | const uint32_t c0 = v0 % 10000; 254 | 255 | const uint32_t d1 = (b0 / 100) << 1; 256 | const uint32_t d2 = (b0 % 100) << 1; 257 | 258 | const uint32_t d3 = (c0 / 100) << 1; 259 | const uint32_t d4 = (c0 % 100) << 1; 260 | 261 | const uint32_t b1 = v1 / 10000; 262 | const uint32_t c1 = v1 % 10000; 263 | 264 | const uint32_t d5 = (b1 / 100) << 1; 265 | const uint32_t d6 = (b1 % 100) << 1; 266 | 267 | const uint32_t d7 = (c1 / 100) << 1; 268 | const uint32_t d8 = (c1 % 100) << 1; 269 | 270 | *buffer++ = cDigitsLut[d1]; 271 | *buffer++ = cDigitsLut[d1 + 1]; 272 | *buffer++ = cDigitsLut[d2]; 273 | *buffer++ = cDigitsLut[d2 + 1]; 274 | *buffer++ = cDigitsLut[d3]; 275 | *buffer++ = cDigitsLut[d3 + 1]; 276 | *buffer++ = cDigitsLut[d4]; 277 | *buffer++ = cDigitsLut[d4 + 1]; 278 | *buffer++ = cDigitsLut[d5]; 279 | *buffer++ = cDigitsLut[d5 + 1]; 280 | *buffer++ = cDigitsLut[d6]; 281 | *buffer++ = cDigitsLut[d6 + 1]; 282 | *buffer++ = cDigitsLut[d7]; 283 | *buffer++ = cDigitsLut[d7 + 1]; 284 | *buffer++ = cDigitsLut[d8]; 285 | *buffer++ = cDigitsLut[d8 + 1]; 286 | } 287 | 288 | return buffer; 289 | } 290 | 291 | inline char* i64toa(int64_t value, char* buffer) { 292 | uint64_t u = static_cast(value); 293 | if (value < 0) { 294 | *buffer++ = '-'; 295 | u = ~u + 1; 296 | } 297 | 298 | return u64toa(u, buffer); 299 | } 300 | 301 | } // namespace internal 302 | RAPIDJSON_NAMESPACE_END 303 | 304 | #endif // RAPIDJSON_ITOA_ 305 | -------------------------------------------------------------------------------- /include/rapidjson/internal/meta.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_INTERNAL_META_H_ 16 | #define RAPIDJSON_INTERNAL_META_H_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | #ifdef __GNUC__ 21 | RAPIDJSON_DIAG_PUSH 22 | RAPIDJSON_DIAG_OFF(effc++) 23 | #endif 24 | #if defined(_MSC_VER) 25 | RAPIDJSON_DIAG_PUSH 26 | RAPIDJSON_DIAG_OFF(6334) 27 | #endif 28 | 29 | #if RAPIDJSON_HAS_CXX11_TYPETRAITS 30 | #include 31 | #endif 32 | 33 | //@cond RAPIDJSON_INTERNAL 34 | RAPIDJSON_NAMESPACE_BEGIN 35 | namespace internal { 36 | 37 | // Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching 38 | template struct Void { typedef void Type; }; 39 | 40 | /////////////////////////////////////////////////////////////////////////////// 41 | // BoolType, TrueType, FalseType 42 | // 43 | template struct BoolType { 44 | static const bool Value = Cond; 45 | typedef BoolType Type; 46 | }; 47 | typedef BoolType TrueType; 48 | typedef BoolType FalseType; 49 | 50 | 51 | /////////////////////////////////////////////////////////////////////////////// 52 | // SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr 53 | // 54 | 55 | template struct SelectIfImpl { template struct Apply { typedef T1 Type; }; }; 56 | template <> struct SelectIfImpl { template struct Apply { typedef T2 Type; }; }; 57 | template struct SelectIfCond : SelectIfImpl::template Apply {}; 58 | template struct SelectIf : SelectIfCond {}; 59 | 60 | template struct AndExprCond : FalseType {}; 61 | template <> struct AndExprCond : TrueType {}; 62 | template struct OrExprCond : TrueType {}; 63 | template <> struct OrExprCond : FalseType {}; 64 | 65 | template struct BoolExpr : SelectIf::Type {}; 66 | template struct NotExpr : SelectIf::Type {}; 67 | template struct AndExpr : AndExprCond::Type {}; 68 | template struct OrExpr : OrExprCond::Type {}; 69 | 70 | 71 | /////////////////////////////////////////////////////////////////////////////// 72 | // AddConst, MaybeAddConst, RemoveConst 73 | template struct AddConst { typedef const T Type; }; 74 | template struct MaybeAddConst : SelectIfCond {}; 75 | template struct RemoveConst { typedef T Type; }; 76 | template struct RemoveConst { typedef T Type; }; 77 | 78 | 79 | /////////////////////////////////////////////////////////////////////////////// 80 | // IsSame, IsConst, IsMoreConst, IsPointer 81 | // 82 | template struct IsSame : FalseType {}; 83 | template struct IsSame : TrueType {}; 84 | 85 | template struct IsConst : FalseType {}; 86 | template struct IsConst : TrueType {}; 87 | 88 | template 89 | struct IsMoreConst 90 | : AndExpr::Type, typename RemoveConst::Type>, 91 | BoolType::Value >= IsConst::Value> >::Type {}; 92 | 93 | template struct IsPointer : FalseType {}; 94 | template struct IsPointer : TrueType {}; 95 | 96 | /////////////////////////////////////////////////////////////////////////////// 97 | // IsBaseOf 98 | // 99 | #if RAPIDJSON_HAS_CXX11_TYPETRAITS 100 | 101 | template struct IsBaseOf 102 | : BoolType< ::std::is_base_of::value> {}; 103 | 104 | #else // simplified version adopted from Boost 105 | 106 | template struct IsBaseOfImpl { 107 | RAPIDJSON_STATIC_ASSERT(sizeof(B) != 0); 108 | RAPIDJSON_STATIC_ASSERT(sizeof(D) != 0); 109 | 110 | typedef char (&Yes)[1]; 111 | typedef char (&No) [2]; 112 | 113 | template 114 | static Yes Check(const D*, T); 115 | static No Check(const B*, int); 116 | 117 | struct Host { 118 | operator const B*() const; 119 | operator const D*(); 120 | }; 121 | 122 | enum { Value = (sizeof(Check(Host(), 0)) == sizeof(Yes)) }; 123 | }; 124 | 125 | template struct IsBaseOf 126 | : OrExpr, BoolExpr > >::Type {}; 127 | 128 | #endif // RAPIDJSON_HAS_CXX11_TYPETRAITS 129 | 130 | 131 | ////////////////////////////////////////////////////////////////////////// 132 | // EnableIf / DisableIf 133 | // 134 | template struct EnableIfCond { typedef T Type; }; 135 | template struct EnableIfCond { /* empty */ }; 136 | 137 | template struct DisableIfCond { typedef T Type; }; 138 | template struct DisableIfCond { /* empty */ }; 139 | 140 | template 141 | struct EnableIf : EnableIfCond {}; 142 | 143 | template 144 | struct DisableIf : DisableIfCond {}; 145 | 146 | // SFINAE helpers 147 | struct SfinaeTag {}; 148 | template struct RemoveSfinaeTag; 149 | template struct RemoveSfinaeTag { typedef T Type; }; 150 | 151 | #define RAPIDJSON_REMOVEFPTR_(type) \ 152 | typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag \ 153 | < ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*) type>::Type 154 | 155 | #define RAPIDJSON_ENABLEIF(cond) \ 156 | typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ 157 | ::Type * = NULL 158 | 159 | #define RAPIDJSON_DISABLEIF(cond) \ 160 | typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ 161 | ::Type * = NULL 162 | 163 | #define RAPIDJSON_ENABLEIF_RETURN(cond,returntype) \ 164 | typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ 165 | ::Type 167 | 168 | #define RAPIDJSON_DISABLEIF_RETURN(cond,returntype) \ 169 | typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ 170 | ::Type 172 | 173 | } // namespace internal 174 | RAPIDJSON_NAMESPACE_END 175 | //@endcond 176 | 177 | #if defined(__GNUC__) || defined(_MSC_VER) 178 | RAPIDJSON_DIAG_POP 179 | #endif 180 | 181 | #endif // RAPIDJSON_INTERNAL_META_H_ 182 | -------------------------------------------------------------------------------- /include/rapidjson/internal/pow10.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_POW10_ 16 | #define RAPIDJSON_POW10_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | namespace internal { 22 | 23 | //! Computes integer powers of 10 in double (10.0^n). 24 | /*! This function uses lookup table for fast and accurate results. 25 | \param n non-negative exponent. Must <= 308. 26 | \return 10.0^n 27 | */ 28 | inline double Pow10(int n) { 29 | static const double e[] = { // 1e-0...1e308: 309 * 8 bytes = 2472 bytes 30 | 1e+0, 31 | 1e+1, 1e+2, 1e+3, 1e+4, 1e+5, 1e+6, 1e+7, 1e+8, 1e+9, 1e+10, 1e+11, 1e+12, 1e+13, 1e+14, 1e+15, 1e+16, 1e+17, 1e+18, 1e+19, 1e+20, 32 | 1e+21, 1e+22, 1e+23, 1e+24, 1e+25, 1e+26, 1e+27, 1e+28, 1e+29, 1e+30, 1e+31, 1e+32, 1e+33, 1e+34, 1e+35, 1e+36, 1e+37, 1e+38, 1e+39, 1e+40, 33 | 1e+41, 1e+42, 1e+43, 1e+44, 1e+45, 1e+46, 1e+47, 1e+48, 1e+49, 1e+50, 1e+51, 1e+52, 1e+53, 1e+54, 1e+55, 1e+56, 1e+57, 1e+58, 1e+59, 1e+60, 34 | 1e+61, 1e+62, 1e+63, 1e+64, 1e+65, 1e+66, 1e+67, 1e+68, 1e+69, 1e+70, 1e+71, 1e+72, 1e+73, 1e+74, 1e+75, 1e+76, 1e+77, 1e+78, 1e+79, 1e+80, 35 | 1e+81, 1e+82, 1e+83, 1e+84, 1e+85, 1e+86, 1e+87, 1e+88, 1e+89, 1e+90, 1e+91, 1e+92, 1e+93, 1e+94, 1e+95, 1e+96, 1e+97, 1e+98, 1e+99, 1e+100, 36 | 1e+101,1e+102,1e+103,1e+104,1e+105,1e+106,1e+107,1e+108,1e+109,1e+110,1e+111,1e+112,1e+113,1e+114,1e+115,1e+116,1e+117,1e+118,1e+119,1e+120, 37 | 1e+121,1e+122,1e+123,1e+124,1e+125,1e+126,1e+127,1e+128,1e+129,1e+130,1e+131,1e+132,1e+133,1e+134,1e+135,1e+136,1e+137,1e+138,1e+139,1e+140, 38 | 1e+141,1e+142,1e+143,1e+144,1e+145,1e+146,1e+147,1e+148,1e+149,1e+150,1e+151,1e+152,1e+153,1e+154,1e+155,1e+156,1e+157,1e+158,1e+159,1e+160, 39 | 1e+161,1e+162,1e+163,1e+164,1e+165,1e+166,1e+167,1e+168,1e+169,1e+170,1e+171,1e+172,1e+173,1e+174,1e+175,1e+176,1e+177,1e+178,1e+179,1e+180, 40 | 1e+181,1e+182,1e+183,1e+184,1e+185,1e+186,1e+187,1e+188,1e+189,1e+190,1e+191,1e+192,1e+193,1e+194,1e+195,1e+196,1e+197,1e+198,1e+199,1e+200, 41 | 1e+201,1e+202,1e+203,1e+204,1e+205,1e+206,1e+207,1e+208,1e+209,1e+210,1e+211,1e+212,1e+213,1e+214,1e+215,1e+216,1e+217,1e+218,1e+219,1e+220, 42 | 1e+221,1e+222,1e+223,1e+224,1e+225,1e+226,1e+227,1e+228,1e+229,1e+230,1e+231,1e+232,1e+233,1e+234,1e+235,1e+236,1e+237,1e+238,1e+239,1e+240, 43 | 1e+241,1e+242,1e+243,1e+244,1e+245,1e+246,1e+247,1e+248,1e+249,1e+250,1e+251,1e+252,1e+253,1e+254,1e+255,1e+256,1e+257,1e+258,1e+259,1e+260, 44 | 1e+261,1e+262,1e+263,1e+264,1e+265,1e+266,1e+267,1e+268,1e+269,1e+270,1e+271,1e+272,1e+273,1e+274,1e+275,1e+276,1e+277,1e+278,1e+279,1e+280, 45 | 1e+281,1e+282,1e+283,1e+284,1e+285,1e+286,1e+287,1e+288,1e+289,1e+290,1e+291,1e+292,1e+293,1e+294,1e+295,1e+296,1e+297,1e+298,1e+299,1e+300, 46 | 1e+301,1e+302,1e+303,1e+304,1e+305,1e+306,1e+307,1e+308 47 | }; 48 | RAPIDJSON_ASSERT(n >= 0 && n <= 308); 49 | return e[n]; 50 | } 51 | 52 | } // namespace internal 53 | RAPIDJSON_NAMESPACE_END 54 | 55 | #endif // RAPIDJSON_POW10_ 56 | -------------------------------------------------------------------------------- /include/rapidjson/internal/stack.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_INTERNAL_STACK_H_ 16 | #define RAPIDJSON_INTERNAL_STACK_H_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | namespace internal { 22 | 23 | /////////////////////////////////////////////////////////////////////////////// 24 | // Stack 25 | 26 | //! A type-unsafe stack for storing different types of data. 27 | /*! \tparam Allocator Allocator for allocating stack memory. 28 | */ 29 | template 30 | class Stack { 31 | public: 32 | // Optimization note: Do not allocate memory for stack_ in constructor. 33 | // Do it lazily when first Push() -> Expand() -> Resize(). 34 | Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) { 35 | RAPIDJSON_ASSERT(stackCapacity > 0); 36 | } 37 | 38 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 39 | Stack(Stack&& rhs) 40 | : allocator_(rhs.allocator_), 41 | ownAllocator_(rhs.ownAllocator_), 42 | stack_(rhs.stack_), 43 | stackTop_(rhs.stackTop_), 44 | stackEnd_(rhs.stackEnd_), 45 | initialCapacity_(rhs.initialCapacity_) 46 | { 47 | rhs.allocator_ = 0; 48 | rhs.ownAllocator_ = 0; 49 | rhs.stack_ = 0; 50 | rhs.stackTop_ = 0; 51 | rhs.stackEnd_ = 0; 52 | rhs.initialCapacity_ = 0; 53 | } 54 | #endif 55 | 56 | ~Stack() { 57 | Destroy(); 58 | } 59 | 60 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 61 | Stack& operator=(Stack&& rhs) { 62 | if (&rhs != this) 63 | { 64 | Destroy(); 65 | 66 | allocator_ = rhs.allocator_; 67 | ownAllocator_ = rhs.ownAllocator_; 68 | stack_ = rhs.stack_; 69 | stackTop_ = rhs.stackTop_; 70 | stackEnd_ = rhs.stackEnd_; 71 | initialCapacity_ = rhs.initialCapacity_; 72 | 73 | rhs.allocator_ = 0; 74 | rhs.ownAllocator_ = 0; 75 | rhs.stack_ = 0; 76 | rhs.stackTop_ = 0; 77 | rhs.stackEnd_ = 0; 78 | rhs.initialCapacity_ = 0; 79 | } 80 | return *this; 81 | } 82 | #endif 83 | 84 | void Clear() { stackTop_ = stack_; } 85 | 86 | void ShrinkToFit() { 87 | if (Empty()) { 88 | // If the stack is empty, completely deallocate the memory. 89 | Allocator::Free(stack_); 90 | stack_ = 0; 91 | stackTop_ = 0; 92 | stackEnd_ = 0; 93 | } 94 | else 95 | Resize(GetSize()); 96 | } 97 | 98 | // Optimization note: try to minimize the size of this function for force inline. 99 | // Expansion is run very infrequently, so it is moved to another (probably non-inline) function. 100 | template 101 | RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) { 102 | // Expand the stack if needed 103 | if (stackTop_ + sizeof(T) * count >= stackEnd_) 104 | Expand(count); 105 | 106 | T* ret = reinterpret_cast(stackTop_); 107 | stackTop_ += sizeof(T) * count; 108 | return ret; 109 | } 110 | 111 | template 112 | T* Pop(size_t count) { 113 | RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T)); 114 | stackTop_ -= count * sizeof(T); 115 | return reinterpret_cast(stackTop_); 116 | } 117 | 118 | template 119 | T* Top() { 120 | RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); 121 | return reinterpret_cast(stackTop_ - sizeof(T)); 122 | } 123 | 124 | template 125 | T* Bottom() { return (T*)stack_; } 126 | 127 | Allocator& GetAllocator() { return *allocator_; } 128 | bool Empty() const { return stackTop_ == stack_; } 129 | size_t GetSize() const { return static_cast(stackTop_ - stack_); } 130 | size_t GetCapacity() const { return static_cast(stackEnd_ - stack_); } 131 | 132 | private: 133 | template 134 | void Expand(size_t count) { 135 | // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity. 136 | size_t newCapacity; 137 | if (stack_ == 0) { 138 | if (!allocator_) 139 | ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); 140 | newCapacity = initialCapacity_; 141 | } else { 142 | newCapacity = GetCapacity(); 143 | newCapacity += (newCapacity + 1) / 2; 144 | } 145 | size_t newSize = GetSize() + sizeof(T) * count; 146 | if (newCapacity < newSize) 147 | newCapacity = newSize; 148 | 149 | Resize(newCapacity); 150 | } 151 | 152 | void Resize(size_t newCapacity) { 153 | const size_t size = GetSize(); // Backup the current size 154 | stack_ = (char*)allocator_->Realloc(stack_, GetCapacity(), newCapacity); 155 | stackTop_ = stack_ + size; 156 | stackEnd_ = stack_ + newCapacity; 157 | } 158 | 159 | void Destroy() { 160 | Allocator::Free(stack_); 161 | RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack 162 | } 163 | 164 | // Prohibit copy constructor & assignment operator. 165 | Stack(const Stack&); 166 | Stack& operator=(const Stack&); 167 | 168 | Allocator* allocator_; 169 | Allocator* ownAllocator_; 170 | char *stack_; 171 | char *stackTop_; 172 | char *stackEnd_; 173 | size_t initialCapacity_; 174 | }; 175 | 176 | } // namespace internal 177 | RAPIDJSON_NAMESPACE_END 178 | 179 | #endif // RAPIDJSON_STACK_H_ 180 | -------------------------------------------------------------------------------- /include/rapidjson/internal/strfunc.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_INTERNAL_STRFUNC_H_ 16 | #define RAPIDJSON_INTERNAL_STRFUNC_H_ 17 | 18 | #include "../rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | namespace internal { 22 | 23 | //! Custom strlen() which works on different character types. 24 | /*! \tparam Ch Character type (e.g. char, wchar_t, short) 25 | \param s Null-terminated input string. 26 | \return Number of characters in the string. 27 | \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints. 28 | */ 29 | template 30 | inline SizeType StrLen(const Ch* s) { 31 | const Ch* p = s; 32 | while (*p) ++p; 33 | return SizeType(p - s); 34 | } 35 | 36 | } // namespace internal 37 | RAPIDJSON_NAMESPACE_END 38 | 39 | #endif // RAPIDJSON_INTERNAL_STRFUNC_H_ 40 | -------------------------------------------------------------------------------- /include/rapidjson/internal/strtod.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_STRTOD_ 16 | #define RAPIDJSON_STRTOD_ 17 | 18 | #include "../rapidjson.h" 19 | #include "ieee754.h" 20 | #include "biginteger.h" 21 | #include "diyfp.h" 22 | #include "pow10.h" 23 | 24 | RAPIDJSON_NAMESPACE_BEGIN 25 | namespace internal { 26 | 27 | inline double FastPath(double significand, int exp) { 28 | if (exp < -308) 29 | return 0.0; 30 | else if (exp >= 0) 31 | return significand * internal::Pow10(exp); 32 | else 33 | return significand / internal::Pow10(-exp); 34 | } 35 | 36 | inline double StrtodNormalPrecision(double d, int p) { 37 | if (p < -308) { 38 | // Prevent expSum < -308, making Pow10(p) = 0 39 | d = FastPath(d, -308); 40 | d = FastPath(d, p + 308); 41 | } 42 | else 43 | d = FastPath(d, p); 44 | return d; 45 | } 46 | 47 | template 48 | inline T Min3(T a, T b, T c) { 49 | T m = a; 50 | if (m > b) m = b; 51 | if (m > c) m = c; 52 | return m; 53 | } 54 | 55 | inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) { 56 | const Double db(b); 57 | const uint64_t bInt = db.IntegerSignificand(); 58 | const int bExp = db.IntegerExponent(); 59 | const int hExp = bExp - 1; 60 | 61 | int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0; 62 | 63 | // Adjust for decimal exponent 64 | if (dExp >= 0) { 65 | dS_Exp2 += dExp; 66 | dS_Exp5 += dExp; 67 | } 68 | else { 69 | bS_Exp2 -= dExp; 70 | bS_Exp5 -= dExp; 71 | hS_Exp2 -= dExp; 72 | hS_Exp5 -= dExp; 73 | } 74 | 75 | // Adjust for binary exponent 76 | if (bExp >= 0) 77 | bS_Exp2 += bExp; 78 | else { 79 | dS_Exp2 -= bExp; 80 | hS_Exp2 -= bExp; 81 | } 82 | 83 | // Adjust for half ulp exponent 84 | if (hExp >= 0) 85 | hS_Exp2 += hExp; 86 | else { 87 | dS_Exp2 -= hExp; 88 | bS_Exp2 -= hExp; 89 | } 90 | 91 | // Remove common power of two factor from all three scaled values 92 | int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2); 93 | dS_Exp2 -= common_Exp2; 94 | bS_Exp2 -= common_Exp2; 95 | hS_Exp2 -= common_Exp2; 96 | 97 | BigInteger dS = d; 98 | dS.MultiplyPow5(dS_Exp5) <<= dS_Exp2; 99 | 100 | BigInteger bS(bInt); 101 | bS.MultiplyPow5(bS_Exp5) <<= bS_Exp2; 102 | 103 | BigInteger hS(1); 104 | hS.MultiplyPow5(hS_Exp5) <<= hS_Exp2; 105 | 106 | BigInteger delta(0); 107 | dS.Difference(bS, &delta); 108 | 109 | return delta.Compare(hS); 110 | } 111 | 112 | inline bool StrtodFast(double d, int p, double* result) { 113 | // Use fast path for string-to-double conversion if possible 114 | // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/ 115 | if (p > 22 && p < 22 + 16) { 116 | // Fast Path Cases In Disguise 117 | d *= internal::Pow10(p - 22); 118 | p = 22; 119 | } 120 | 121 | if (p >= -22 && p <= 22 && d <= 9007199254740991.0) { // 2^53 - 1 122 | *result = FastPath(d, p); 123 | return true; 124 | } 125 | else 126 | return false; 127 | } 128 | 129 | // Compute an approximation and see if it is within 1/2 ULP 130 | inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosition, int exp, double* result) { 131 | uint64_t significand = 0; 132 | size_t i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999 133 | for (; i < length; i++) { 134 | if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || 135 | (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > '5')) 136 | break; 137 | significand = significand * 10 + (decimals[i] - '0'); 138 | } 139 | 140 | if (i < length && decimals[i] >= '5') // Rounding 141 | significand++; 142 | 143 | size_t remaining = length - i; 144 | const unsigned kUlpShift = 3; 145 | const unsigned kUlp = 1 << kUlpShift; 146 | int error = (remaining == 0) ? 0 : kUlp / 2; 147 | 148 | DiyFp v(significand, 0); 149 | v = v.Normalize(); 150 | error <<= -v.e; 151 | 152 | const int dExp = (int)decimalPosition - (int)i + exp; 153 | 154 | int actualExp; 155 | DiyFp cachedPower = GetCachedPower10(dExp, &actualExp); 156 | if (actualExp != dExp) { 157 | static const DiyFp kPow10[] = { 158 | DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 00000000), -60), // 10^1 159 | DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 00000000), -57), // 10^2 160 | DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 00000000), -54), // 10^3 161 | DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 00000000), -50), // 10^4 162 | DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 00000000), -47), // 10^5 163 | DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 00000000), -44), // 10^6 164 | DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 00000000), -40) // 10^7 165 | }; 166 | int adjustment = dExp - actualExp - 1; 167 | RAPIDJSON_ASSERT(adjustment >= 0 && adjustment < 7); 168 | v = v * kPow10[adjustment]; 169 | if (length + adjustment > 19) // has more digits than decimal digits in 64-bit 170 | error += kUlp / 2; 171 | } 172 | 173 | v = v * cachedPower; 174 | 175 | error += kUlp + (error == 0 ? 0 : 1); 176 | 177 | const int oldExp = v.e; 178 | v = v.Normalize(); 179 | error <<= oldExp - v.e; 180 | 181 | const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e); 182 | unsigned precisionSize = 64 - effectiveSignificandSize; 183 | if (precisionSize + kUlpShift >= 64) { 184 | unsigned scaleExp = (precisionSize + kUlpShift) - 63; 185 | v.f >>= scaleExp; 186 | v.e += scaleExp; 187 | error = (error >> scaleExp) + 1 + kUlp; 188 | precisionSize -= scaleExp; 189 | } 190 | 191 | DiyFp rounded(v.f >> precisionSize, v.e + precisionSize); 192 | const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp; 193 | const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp; 194 | if (precisionBits >= halfWay + error) { 195 | rounded.f++; 196 | if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340) 197 | rounded.f >>= 1; 198 | rounded.e++; 199 | } 200 | } 201 | 202 | *result = rounded.ToDouble(); 203 | 204 | return halfWay - error >= precisionBits || precisionBits >= halfWay + error; 205 | } 206 | 207 | inline double StrtodBigInteger(double approx, const char* decimals, size_t length, size_t decimalPosition, int exp) { 208 | const BigInteger dInt(decimals, length); 209 | const int dExp = (int)decimalPosition - (int)length + exp; 210 | Double a(approx); 211 | int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp); 212 | if (cmp < 0) 213 | return a.Value(); // within half ULP 214 | else if (cmp == 0) { 215 | // Round towards even 216 | if (a.Significand() & 1) 217 | return a.NextPositiveDouble(); 218 | else 219 | return a.Value(); 220 | } 221 | else // adjustment 222 | return a.NextPositiveDouble(); 223 | } 224 | 225 | inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t length, size_t decimalPosition, int exp) { 226 | RAPIDJSON_ASSERT(d >= 0.0); 227 | RAPIDJSON_ASSERT(length >= 1); 228 | 229 | double result; 230 | if (StrtodFast(d, p, &result)) 231 | return result; 232 | 233 | // Trim leading zeros 234 | while (*decimals == '0' && length > 1) { 235 | length--; 236 | decimals++; 237 | decimalPosition--; 238 | } 239 | 240 | // Trim trailing zeros 241 | while (decimals[length - 1] == '0' && length > 1) { 242 | length--; 243 | decimalPosition--; 244 | exp++; 245 | } 246 | 247 | // Trim right-most digits 248 | const int kMaxDecimalDigit = 780; 249 | if ((int)length > kMaxDecimalDigit) { 250 | int delta = (int(length) - kMaxDecimalDigit); 251 | exp += delta; 252 | decimalPosition -= delta; 253 | length = kMaxDecimalDigit; 254 | } 255 | 256 | // If too small, underflow to zero 257 | if (int(length) + exp < -324) 258 | return 0.0; 259 | 260 | if (StrtodDiyFp(decimals, length, decimalPosition, exp, &result)) 261 | return result; 262 | 263 | // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison 264 | return StrtodBigInteger(result, decimals, length, decimalPosition, exp); 265 | } 266 | 267 | } // namespace internal 268 | RAPIDJSON_NAMESPACE_END 269 | 270 | #endif // RAPIDJSON_STRTOD_ 271 | -------------------------------------------------------------------------------- /include/rapidjson/memorybuffer.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_MEMORYBUFFER_H_ 16 | #define RAPIDJSON_MEMORYBUFFER_H_ 17 | 18 | #include "rapidjson.h" 19 | #include "internal/stack.h" 20 | 21 | RAPIDJSON_NAMESPACE_BEGIN 22 | 23 | //! Represents an in-memory output byte stream. 24 | /*! 25 | This class is mainly for being wrapped by EncodedOutputStream or AutoUTFOutputStream. 26 | 27 | It is similar to FileWriteBuffer but the destination is an in-memory buffer instead of a file. 28 | 29 | Differences between MemoryBuffer and StringBuffer: 30 | 1. StringBuffer has Encoding but MemoryBuffer is only a byte buffer. 31 | 2. StringBuffer::GetString() returns a null-terminated string. MemoryBuffer::GetBuffer() returns a buffer without terminator. 32 | 33 | \tparam Allocator type for allocating memory buffer. 34 | \note implements Stream concept 35 | */ 36 | template 37 | struct GenericMemoryBuffer { 38 | typedef char Ch; // byte 39 | 40 | GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} 41 | 42 | void Put(Ch c) { *stack_.template Push() = c; } 43 | void Flush() {} 44 | 45 | void Clear() { stack_.Clear(); } 46 | void ShrinkToFit() { stack_.ShrinkToFit(); } 47 | Ch* Push(size_t count) { return stack_.template Push(count); } 48 | void Pop(size_t count) { stack_.template Pop(count); } 49 | 50 | const Ch* GetBuffer() const { 51 | return stack_.template Bottom(); 52 | } 53 | 54 | size_t GetSize() const { return stack_.GetSize(); } 55 | 56 | static const size_t kDefaultCapacity = 256; 57 | mutable internal::Stack stack_; 58 | }; 59 | 60 | typedef GenericMemoryBuffer<> MemoryBuffer; 61 | 62 | //! Implement specialized version of PutN() with memset() for better performance. 63 | template<> 64 | inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) { 65 | std::memset(memoryBuffer.stack_.Push(n), c, n * sizeof(c)); 66 | } 67 | 68 | RAPIDJSON_NAMESPACE_END 69 | 70 | #endif // RAPIDJSON_MEMORYBUFFER_H_ 71 | -------------------------------------------------------------------------------- /include/rapidjson/memorystream.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_MEMORYSTREAM_H_ 16 | #define RAPIDJSON_MEMORYSTREAM_H_ 17 | 18 | #include "rapidjson.h" 19 | 20 | RAPIDJSON_NAMESPACE_BEGIN 21 | 22 | //! Represents an in-memory input byte stream. 23 | /*! 24 | This class is mainly for being wrapped by EncodedInputStream or AutoUTFInputStream. 25 | 26 | It is similar to FileReadBuffer but the source is an in-memory buffer instead of a file. 27 | 28 | Differences between MemoryStream and StringStream: 29 | 1. StringStream has encoding but MemoryStream is a byte stream. 30 | 2. MemoryStream needs size of the source buffer and the buffer don't need to be null terminated. StringStream assume null-terminated string as source. 31 | 3. MemoryStream supports Peek4() for encoding detection. StringStream is specified with an encoding so it should not have Peek4(). 32 | \note implements Stream concept 33 | */ 34 | struct MemoryStream { 35 | typedef char Ch; // byte 36 | 37 | MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {} 38 | 39 | Ch Peek() const { return (src_ == end_) ? '\0' : *src_; } 40 | Ch Take() { return (src_ == end_) ? '\0' : *src_++; } 41 | size_t Tell() const { return static_cast(src_ - begin_); } 42 | 43 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 44 | void Put(Ch) { RAPIDJSON_ASSERT(false); } 45 | void Flush() { RAPIDJSON_ASSERT(false); } 46 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 47 | 48 | // For encoding detection only. 49 | const Ch* Peek4() const { 50 | return Tell() + 4 <= size_ ? src_ : 0; 51 | } 52 | 53 | const Ch* src_; //!< Current read position. 54 | const Ch* begin_; //!< Original head of the string. 55 | const Ch* end_; //!< End of stream. 56 | size_t size_; //!< Size of the stream. 57 | }; 58 | 59 | RAPIDJSON_NAMESPACE_END 60 | 61 | #endif // RAPIDJSON_MEMORYBUFFER_H_ 62 | -------------------------------------------------------------------------------- /include/rapidjson/msinttypes/inttypes.h: -------------------------------------------------------------------------------- 1 | // ISO C9x compliant inttypes.h for Microsoft Visual Studio 2 | // Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 3 | // 4 | // Copyright (c) 2006-2013 Alexander Chemeris 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are met: 8 | // 9 | // 1. Redistributions of source code must retain the above copyright notice, 10 | // this list of conditions and the following disclaimer. 11 | // 12 | // 2. Redistributions in binary form must reproduce the above copyright 13 | // notice, this list of conditions and the following disclaimer in the 14 | // documentation and/or other materials provided with the distribution. 15 | // 16 | // 3. Neither the name of the product nor the names of its contributors may 17 | // be used to endorse or promote products derived from this software 18 | // without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 21 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 23 | // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 | // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 | // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 | // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | // 31 | /////////////////////////////////////////////////////////////////////////////// 32 | 33 | // The above software in this distribution may have been modified by 34 | // THL A29 Limited ("Tencent Modifications"). 35 | // All Tencent Modifications are Copyright (C) 2015 THL A29 Limited. 36 | 37 | #ifndef _MSC_VER // [ 38 | #error "Use this header only with Microsoft Visual C++ compilers!" 39 | #endif // _MSC_VER ] 40 | 41 | #ifndef _MSC_INTTYPES_H_ // [ 42 | #define _MSC_INTTYPES_H_ 43 | 44 | #if _MSC_VER > 1000 45 | #pragma once 46 | #endif 47 | 48 | #include "stdint.h" 49 | 50 | // miloyip: VC supports inttypes.h since VC2013 51 | #if _MSC_VER >= 1800 52 | #include 53 | #else 54 | 55 | // 7.8 Format conversion of integer types 56 | 57 | typedef struct { 58 | intmax_t quot; 59 | intmax_t rem; 60 | } imaxdiv_t; 61 | 62 | // 7.8.1 Macros for format specifiers 63 | 64 | #if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS) // [ See footnote 185 at page 198 65 | 66 | // The fprintf macros for signed integers are: 67 | #define PRId8 "d" 68 | #define PRIi8 "i" 69 | #define PRIdLEAST8 "d" 70 | #define PRIiLEAST8 "i" 71 | #define PRIdFAST8 "d" 72 | #define PRIiFAST8 "i" 73 | 74 | #define PRId16 "hd" 75 | #define PRIi16 "hi" 76 | #define PRIdLEAST16 "hd" 77 | #define PRIiLEAST16 "hi" 78 | #define PRIdFAST16 "hd" 79 | #define PRIiFAST16 "hi" 80 | 81 | #define PRId32 "I32d" 82 | #define PRIi32 "I32i" 83 | #define PRIdLEAST32 "I32d" 84 | #define PRIiLEAST32 "I32i" 85 | #define PRIdFAST32 "I32d" 86 | #define PRIiFAST32 "I32i" 87 | 88 | #define PRId64 "I64d" 89 | #define PRIi64 "I64i" 90 | #define PRIdLEAST64 "I64d" 91 | #define PRIiLEAST64 "I64i" 92 | #define PRIdFAST64 "I64d" 93 | #define PRIiFAST64 "I64i" 94 | 95 | #define PRIdMAX "I64d" 96 | #define PRIiMAX "I64i" 97 | 98 | #define PRIdPTR "Id" 99 | #define PRIiPTR "Ii" 100 | 101 | // The fprintf macros for unsigned integers are: 102 | #define PRIo8 "o" 103 | #define PRIu8 "u" 104 | #define PRIx8 "x" 105 | #define PRIX8 "X" 106 | #define PRIoLEAST8 "o" 107 | #define PRIuLEAST8 "u" 108 | #define PRIxLEAST8 "x" 109 | #define PRIXLEAST8 "X" 110 | #define PRIoFAST8 "o" 111 | #define PRIuFAST8 "u" 112 | #define PRIxFAST8 "x" 113 | #define PRIXFAST8 "X" 114 | 115 | #define PRIo16 "ho" 116 | #define PRIu16 "hu" 117 | #define PRIx16 "hx" 118 | #define PRIX16 "hX" 119 | #define PRIoLEAST16 "ho" 120 | #define PRIuLEAST16 "hu" 121 | #define PRIxLEAST16 "hx" 122 | #define PRIXLEAST16 "hX" 123 | #define PRIoFAST16 "ho" 124 | #define PRIuFAST16 "hu" 125 | #define PRIxFAST16 "hx" 126 | #define PRIXFAST16 "hX" 127 | 128 | #define PRIo32 "I32o" 129 | #define PRIu32 "I32u" 130 | #define PRIx32 "I32x" 131 | #define PRIX32 "I32X" 132 | #define PRIoLEAST32 "I32o" 133 | #define PRIuLEAST32 "I32u" 134 | #define PRIxLEAST32 "I32x" 135 | #define PRIXLEAST32 "I32X" 136 | #define PRIoFAST32 "I32o" 137 | #define PRIuFAST32 "I32u" 138 | #define PRIxFAST32 "I32x" 139 | #define PRIXFAST32 "I32X" 140 | 141 | #define PRIo64 "I64o" 142 | #define PRIu64 "I64u" 143 | #define PRIx64 "I64x" 144 | #define PRIX64 "I64X" 145 | #define PRIoLEAST64 "I64o" 146 | #define PRIuLEAST64 "I64u" 147 | #define PRIxLEAST64 "I64x" 148 | #define PRIXLEAST64 "I64X" 149 | #define PRIoFAST64 "I64o" 150 | #define PRIuFAST64 "I64u" 151 | #define PRIxFAST64 "I64x" 152 | #define PRIXFAST64 "I64X" 153 | 154 | #define PRIoMAX "I64o" 155 | #define PRIuMAX "I64u" 156 | #define PRIxMAX "I64x" 157 | #define PRIXMAX "I64X" 158 | 159 | #define PRIoPTR "Io" 160 | #define PRIuPTR "Iu" 161 | #define PRIxPTR "Ix" 162 | #define PRIXPTR "IX" 163 | 164 | // The fscanf macros for signed integers are: 165 | #define SCNd8 "d" 166 | #define SCNi8 "i" 167 | #define SCNdLEAST8 "d" 168 | #define SCNiLEAST8 "i" 169 | #define SCNdFAST8 "d" 170 | #define SCNiFAST8 "i" 171 | 172 | #define SCNd16 "hd" 173 | #define SCNi16 "hi" 174 | #define SCNdLEAST16 "hd" 175 | #define SCNiLEAST16 "hi" 176 | #define SCNdFAST16 "hd" 177 | #define SCNiFAST16 "hi" 178 | 179 | #define SCNd32 "ld" 180 | #define SCNi32 "li" 181 | #define SCNdLEAST32 "ld" 182 | #define SCNiLEAST32 "li" 183 | #define SCNdFAST32 "ld" 184 | #define SCNiFAST32 "li" 185 | 186 | #define SCNd64 "I64d" 187 | #define SCNi64 "I64i" 188 | #define SCNdLEAST64 "I64d" 189 | #define SCNiLEAST64 "I64i" 190 | #define SCNdFAST64 "I64d" 191 | #define SCNiFAST64 "I64i" 192 | 193 | #define SCNdMAX "I64d" 194 | #define SCNiMAX "I64i" 195 | 196 | #ifdef _WIN64 // [ 197 | # define SCNdPTR "I64d" 198 | # define SCNiPTR "I64i" 199 | #else // _WIN64 ][ 200 | # define SCNdPTR "ld" 201 | # define SCNiPTR "li" 202 | #endif // _WIN64 ] 203 | 204 | // The fscanf macros for unsigned integers are: 205 | #define SCNo8 "o" 206 | #define SCNu8 "u" 207 | #define SCNx8 "x" 208 | #define SCNX8 "X" 209 | #define SCNoLEAST8 "o" 210 | #define SCNuLEAST8 "u" 211 | #define SCNxLEAST8 "x" 212 | #define SCNXLEAST8 "X" 213 | #define SCNoFAST8 "o" 214 | #define SCNuFAST8 "u" 215 | #define SCNxFAST8 "x" 216 | #define SCNXFAST8 "X" 217 | 218 | #define SCNo16 "ho" 219 | #define SCNu16 "hu" 220 | #define SCNx16 "hx" 221 | #define SCNX16 "hX" 222 | #define SCNoLEAST16 "ho" 223 | #define SCNuLEAST16 "hu" 224 | #define SCNxLEAST16 "hx" 225 | #define SCNXLEAST16 "hX" 226 | #define SCNoFAST16 "ho" 227 | #define SCNuFAST16 "hu" 228 | #define SCNxFAST16 "hx" 229 | #define SCNXFAST16 "hX" 230 | 231 | #define SCNo32 "lo" 232 | #define SCNu32 "lu" 233 | #define SCNx32 "lx" 234 | #define SCNX32 "lX" 235 | #define SCNoLEAST32 "lo" 236 | #define SCNuLEAST32 "lu" 237 | #define SCNxLEAST32 "lx" 238 | #define SCNXLEAST32 "lX" 239 | #define SCNoFAST32 "lo" 240 | #define SCNuFAST32 "lu" 241 | #define SCNxFAST32 "lx" 242 | #define SCNXFAST32 "lX" 243 | 244 | #define SCNo64 "I64o" 245 | #define SCNu64 "I64u" 246 | #define SCNx64 "I64x" 247 | #define SCNX64 "I64X" 248 | #define SCNoLEAST64 "I64o" 249 | #define SCNuLEAST64 "I64u" 250 | #define SCNxLEAST64 "I64x" 251 | #define SCNXLEAST64 "I64X" 252 | #define SCNoFAST64 "I64o" 253 | #define SCNuFAST64 "I64u" 254 | #define SCNxFAST64 "I64x" 255 | #define SCNXFAST64 "I64X" 256 | 257 | #define SCNoMAX "I64o" 258 | #define SCNuMAX "I64u" 259 | #define SCNxMAX "I64x" 260 | #define SCNXMAX "I64X" 261 | 262 | #ifdef _WIN64 // [ 263 | # define SCNoPTR "I64o" 264 | # define SCNuPTR "I64u" 265 | # define SCNxPTR "I64x" 266 | # define SCNXPTR "I64X" 267 | #else // _WIN64 ][ 268 | # define SCNoPTR "lo" 269 | # define SCNuPTR "lu" 270 | # define SCNxPTR "lx" 271 | # define SCNXPTR "lX" 272 | #endif // _WIN64 ] 273 | 274 | #endif // __STDC_FORMAT_MACROS ] 275 | 276 | // 7.8.2 Functions for greatest-width integer types 277 | 278 | // 7.8.2.1 The imaxabs function 279 | #define imaxabs _abs64 280 | 281 | // 7.8.2.2 The imaxdiv function 282 | 283 | // This is modified version of div() function from Microsoft's div.c found 284 | // in %MSVC.NET%\crt\src\div.c 285 | #ifdef STATIC_IMAXDIV // [ 286 | static 287 | #else // STATIC_IMAXDIV ][ 288 | _inline 289 | #endif // STATIC_IMAXDIV ] 290 | imaxdiv_t __cdecl imaxdiv(intmax_t numer, intmax_t denom) 291 | { 292 | imaxdiv_t result; 293 | 294 | result.quot = numer / denom; 295 | result.rem = numer % denom; 296 | 297 | if (numer < 0 && result.rem > 0) { 298 | // did division wrong; must fix up 299 | ++result.quot; 300 | result.rem -= denom; 301 | } 302 | 303 | return result; 304 | } 305 | 306 | // 7.8.2.3 The strtoimax and strtoumax functions 307 | #define strtoimax _strtoi64 308 | #define strtoumax _strtoui64 309 | 310 | // 7.8.2.4 The wcstoimax and wcstoumax functions 311 | #define wcstoimax _wcstoi64 312 | #define wcstoumax _wcstoui64 313 | 314 | #endif // _MSC_VER >= 1800 315 | 316 | #endif // _MSC_INTTYPES_H_ ] 317 | -------------------------------------------------------------------------------- /include/rapidjson/msinttypes/stdint.h: -------------------------------------------------------------------------------- 1 | // ISO C9x compliant stdint.h for Microsoft Visual Studio 2 | // Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 3 | // 4 | // Copyright (c) 2006-2013 Alexander Chemeris 5 | // 6 | // Redistribution and use in source and binary forms, with or without 7 | // modification, are permitted provided that the following conditions are met: 8 | // 9 | // 1. Redistributions of source code must retain the above copyright notice, 10 | // this list of conditions and the following disclaimer. 11 | // 12 | // 2. Redistributions in binary form must reproduce the above copyright 13 | // notice, this list of conditions and the following disclaimer in the 14 | // documentation and/or other materials provided with the distribution. 15 | // 16 | // 3. Neither the name of the product nor the names of its contributors may 17 | // be used to endorse or promote products derived from this software 18 | // without specific prior written permission. 19 | // 20 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 21 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 23 | // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 | // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 | // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 | // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 | // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | // 31 | /////////////////////////////////////////////////////////////////////////////// 32 | 33 | // The above software in this distribution may have been modified by 34 | // THL A29 Limited ("Tencent Modifications"). 35 | // All Tencent Modifications are Copyright (C) 2015 THL A29 Limited. 36 | 37 | #ifndef _MSC_VER // [ 38 | #error "Use this header only with Microsoft Visual C++ compilers!" 39 | #endif // _MSC_VER ] 40 | 41 | #ifndef _MSC_STDINT_H_ // [ 42 | #define _MSC_STDINT_H_ 43 | 44 | #if _MSC_VER > 1000 45 | #pragma once 46 | #endif 47 | 48 | // miloyip: Originally Visual Studio 2010 uses its own stdint.h. However it generates warning with INT64_C(), so change to use this file for vs2010. 49 | #if _MSC_VER >= 1600 // [ 50 | #include 51 | 52 | #if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 53 | 54 | #undef INT8_C 55 | #undef INT16_C 56 | #undef INT32_C 57 | #undef INT64_C 58 | #undef UINT8_C 59 | #undef UINT16_C 60 | #undef UINT32_C 61 | #undef UINT64_C 62 | 63 | // 7.18.4.1 Macros for minimum-width integer constants 64 | 65 | #define INT8_C(val) val##i8 66 | #define INT16_C(val) val##i16 67 | #define INT32_C(val) val##i32 68 | #define INT64_C(val) val##i64 69 | 70 | #define UINT8_C(val) val##ui8 71 | #define UINT16_C(val) val##ui16 72 | #define UINT32_C(val) val##ui32 73 | #define UINT64_C(val) val##ui64 74 | 75 | // 7.18.4.2 Macros for greatest-width integer constants 76 | // These #ifndef's are needed to prevent collisions with . 77 | // Check out Issue 9 for the details. 78 | #ifndef INTMAX_C // [ 79 | # define INTMAX_C INT64_C 80 | #endif // INTMAX_C ] 81 | #ifndef UINTMAX_C // [ 82 | # define UINTMAX_C UINT64_C 83 | #endif // UINTMAX_C ] 84 | 85 | #endif // __STDC_CONSTANT_MACROS ] 86 | 87 | #else // ] _MSC_VER >= 1700 [ 88 | 89 | #include 90 | 91 | // For Visual Studio 6 in C++ mode and for many Visual Studio versions when 92 | // compiling for ARM we should wrap include with 'extern "C++" {}' 93 | // or compiler give many errors like this: 94 | // error C2733: second C linkage of overloaded function 'wmemchr' not allowed 95 | #ifdef __cplusplus 96 | extern "C" { 97 | #endif 98 | # include 99 | #ifdef __cplusplus 100 | } 101 | #endif 102 | 103 | // Define _W64 macros to mark types changing their size, like intptr_t. 104 | #ifndef _W64 105 | # if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300 106 | # define _W64 __w64 107 | # else 108 | # define _W64 109 | # endif 110 | #endif 111 | 112 | 113 | // 7.18.1 Integer types 114 | 115 | // 7.18.1.1 Exact-width integer types 116 | 117 | // Visual Studio 6 and Embedded Visual C++ 4 doesn't 118 | // realize that, e.g. char has the same size as __int8 119 | // so we give up on __intX for them. 120 | #if (_MSC_VER < 1300) 121 | typedef signed char int8_t; 122 | typedef signed short int16_t; 123 | typedef signed int int32_t; 124 | typedef unsigned char uint8_t; 125 | typedef unsigned short uint16_t; 126 | typedef unsigned int uint32_t; 127 | #else 128 | typedef signed __int8 int8_t; 129 | typedef signed __int16 int16_t; 130 | typedef signed __int32 int32_t; 131 | typedef unsigned __int8 uint8_t; 132 | typedef unsigned __int16 uint16_t; 133 | typedef unsigned __int32 uint32_t; 134 | #endif 135 | typedef signed __int64 int64_t; 136 | typedef unsigned __int64 uint64_t; 137 | 138 | 139 | // 7.18.1.2 Minimum-width integer types 140 | typedef int8_t int_least8_t; 141 | typedef int16_t int_least16_t; 142 | typedef int32_t int_least32_t; 143 | typedef int64_t int_least64_t; 144 | typedef uint8_t uint_least8_t; 145 | typedef uint16_t uint_least16_t; 146 | typedef uint32_t uint_least32_t; 147 | typedef uint64_t uint_least64_t; 148 | 149 | // 7.18.1.3 Fastest minimum-width integer types 150 | typedef int8_t int_fast8_t; 151 | typedef int16_t int_fast16_t; 152 | typedef int32_t int_fast32_t; 153 | typedef int64_t int_fast64_t; 154 | typedef uint8_t uint_fast8_t; 155 | typedef uint16_t uint_fast16_t; 156 | typedef uint32_t uint_fast32_t; 157 | typedef uint64_t uint_fast64_t; 158 | 159 | // 7.18.1.4 Integer types capable of holding object pointers 160 | #ifdef _WIN64 // [ 161 | typedef signed __int64 intptr_t; 162 | typedef unsigned __int64 uintptr_t; 163 | #else // _WIN64 ][ 164 | typedef _W64 signed int intptr_t; 165 | typedef _W64 unsigned int uintptr_t; 166 | #endif // _WIN64 ] 167 | 168 | // 7.18.1.5 Greatest-width integer types 169 | typedef int64_t intmax_t; 170 | typedef uint64_t uintmax_t; 171 | 172 | 173 | // 7.18.2 Limits of specified-width integer types 174 | 175 | #if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259 176 | 177 | // 7.18.2.1 Limits of exact-width integer types 178 | #define INT8_MIN ((int8_t)_I8_MIN) 179 | #define INT8_MAX _I8_MAX 180 | #define INT16_MIN ((int16_t)_I16_MIN) 181 | #define INT16_MAX _I16_MAX 182 | #define INT32_MIN ((int32_t)_I32_MIN) 183 | #define INT32_MAX _I32_MAX 184 | #define INT64_MIN ((int64_t)_I64_MIN) 185 | #define INT64_MAX _I64_MAX 186 | #define UINT8_MAX _UI8_MAX 187 | #define UINT16_MAX _UI16_MAX 188 | #define UINT32_MAX _UI32_MAX 189 | #define UINT64_MAX _UI64_MAX 190 | 191 | // 7.18.2.2 Limits of minimum-width integer types 192 | #define INT_LEAST8_MIN INT8_MIN 193 | #define INT_LEAST8_MAX INT8_MAX 194 | #define INT_LEAST16_MIN INT16_MIN 195 | #define INT_LEAST16_MAX INT16_MAX 196 | #define INT_LEAST32_MIN INT32_MIN 197 | #define INT_LEAST32_MAX INT32_MAX 198 | #define INT_LEAST64_MIN INT64_MIN 199 | #define INT_LEAST64_MAX INT64_MAX 200 | #define UINT_LEAST8_MAX UINT8_MAX 201 | #define UINT_LEAST16_MAX UINT16_MAX 202 | #define UINT_LEAST32_MAX UINT32_MAX 203 | #define UINT_LEAST64_MAX UINT64_MAX 204 | 205 | // 7.18.2.3 Limits of fastest minimum-width integer types 206 | #define INT_FAST8_MIN INT8_MIN 207 | #define INT_FAST8_MAX INT8_MAX 208 | #define INT_FAST16_MIN INT16_MIN 209 | #define INT_FAST16_MAX INT16_MAX 210 | #define INT_FAST32_MIN INT32_MIN 211 | #define INT_FAST32_MAX INT32_MAX 212 | #define INT_FAST64_MIN INT64_MIN 213 | #define INT_FAST64_MAX INT64_MAX 214 | #define UINT_FAST8_MAX UINT8_MAX 215 | #define UINT_FAST16_MAX UINT16_MAX 216 | #define UINT_FAST32_MAX UINT32_MAX 217 | #define UINT_FAST64_MAX UINT64_MAX 218 | 219 | // 7.18.2.4 Limits of integer types capable of holding object pointers 220 | #ifdef _WIN64 // [ 221 | # define INTPTR_MIN INT64_MIN 222 | # define INTPTR_MAX INT64_MAX 223 | # define UINTPTR_MAX UINT64_MAX 224 | #else // _WIN64 ][ 225 | # define INTPTR_MIN INT32_MIN 226 | # define INTPTR_MAX INT32_MAX 227 | # define UINTPTR_MAX UINT32_MAX 228 | #endif // _WIN64 ] 229 | 230 | // 7.18.2.5 Limits of greatest-width integer types 231 | #define INTMAX_MIN INT64_MIN 232 | #define INTMAX_MAX INT64_MAX 233 | #define UINTMAX_MAX UINT64_MAX 234 | 235 | // 7.18.3 Limits of other integer types 236 | 237 | #ifdef _WIN64 // [ 238 | # define PTRDIFF_MIN _I64_MIN 239 | # define PTRDIFF_MAX _I64_MAX 240 | #else // _WIN64 ][ 241 | # define PTRDIFF_MIN _I32_MIN 242 | # define PTRDIFF_MAX _I32_MAX 243 | #endif // _WIN64 ] 244 | 245 | #define SIG_ATOMIC_MIN INT_MIN 246 | #define SIG_ATOMIC_MAX INT_MAX 247 | 248 | #ifndef SIZE_MAX // [ 249 | # ifdef _WIN64 // [ 250 | # define SIZE_MAX _UI64_MAX 251 | # else // _WIN64 ][ 252 | # define SIZE_MAX _UI32_MAX 253 | # endif // _WIN64 ] 254 | #endif // SIZE_MAX ] 255 | 256 | // WCHAR_MIN and WCHAR_MAX are also defined in 257 | #ifndef WCHAR_MIN // [ 258 | # define WCHAR_MIN 0 259 | #endif // WCHAR_MIN ] 260 | #ifndef WCHAR_MAX // [ 261 | # define WCHAR_MAX _UI16_MAX 262 | #endif // WCHAR_MAX ] 263 | 264 | #define WINT_MIN 0 265 | #define WINT_MAX _UI16_MAX 266 | 267 | #endif // __STDC_LIMIT_MACROS ] 268 | 269 | 270 | // 7.18.4 Limits of other integer types 271 | 272 | #if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260 273 | 274 | // 7.18.4.1 Macros for minimum-width integer constants 275 | 276 | #define INT8_C(val) val##i8 277 | #define INT16_C(val) val##i16 278 | #define INT32_C(val) val##i32 279 | #define INT64_C(val) val##i64 280 | 281 | #define UINT8_C(val) val##ui8 282 | #define UINT16_C(val) val##ui16 283 | #define UINT32_C(val) val##ui32 284 | #define UINT64_C(val) val##ui64 285 | 286 | // 7.18.4.2 Macros for greatest-width integer constants 287 | // These #ifndef's are needed to prevent collisions with . 288 | // Check out Issue 9 for the details. 289 | #ifndef INTMAX_C // [ 290 | # define INTMAX_C INT64_C 291 | #endif // INTMAX_C ] 292 | #ifndef UINTMAX_C // [ 293 | # define UINTMAX_C UINT64_C 294 | #endif // UINTMAX_C ] 295 | 296 | #endif // __STDC_CONSTANT_MACROS ] 297 | 298 | #endif // _MSC_VER >= 1600 ] 299 | 300 | #endif // _MSC_STDINT_H_ ] 301 | -------------------------------------------------------------------------------- /include/rapidjson/prettywriter.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_PRETTYWRITER_H_ 16 | #define RAPIDJSON_PRETTYWRITER_H_ 17 | 18 | #include "writer.h" 19 | 20 | #ifdef __GNUC__ 21 | RAPIDJSON_DIAG_PUSH 22 | RAPIDJSON_DIAG_OFF(effc++) 23 | #endif 24 | 25 | RAPIDJSON_NAMESPACE_BEGIN 26 | 27 | //! Writer with indentation and spacing. 28 | /*! 29 | \tparam OutputStream Type of ouptut os. 30 | \tparam SourceEncoding Encoding of source string. 31 | \tparam TargetEncoding Encoding of output stream. 32 | \tparam StackAllocator Type of allocator for allocating memory of stack. 33 | */ 34 | template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator> 35 | class PrettyWriter : public Writer { 36 | public: 37 | typedef Writer Base; 38 | typedef typename Base::Ch Ch; 39 | 40 | //! Constructor 41 | /*! \param os Output stream. 42 | \param allocator User supplied allocator. If it is null, it will create a private one. 43 | \param levelDepth Initial capacity of stack. 44 | */ 45 | PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : 46 | Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {} 47 | 48 | //! Set custom indentation. 49 | /*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r'). 50 | \param indentCharCount Number of indent characters for each indentation level. 51 | \note The default indentation is 4 spaces. 52 | */ 53 | PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount) { 54 | RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r'); 55 | indentChar_ = indentChar; 56 | indentCharCount_ = indentCharCount; 57 | return *this; 58 | } 59 | 60 | /*! @name Implementation of Handler 61 | \see Handler 62 | */ 63 | //@{ 64 | 65 | bool Null() { PrettyPrefix(kNullType); return Base::WriteNull(); } 66 | bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::WriteBool(b); } 67 | bool Int(int i) { PrettyPrefix(kNumberType); return Base::WriteInt(i); } 68 | bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::WriteUint(u); } 69 | bool Int64(int64_t i64) { PrettyPrefix(kNumberType); return Base::WriteInt64(i64); } 70 | bool Uint64(uint64_t u64) { PrettyPrefix(kNumberType); return Base::WriteUint64(u64); } 71 | bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); } 72 | 73 | bool String(const Ch* str, SizeType length, bool copy = false) { 74 | (void)copy; 75 | PrettyPrefix(kStringType); 76 | return Base::WriteString(str, length); 77 | } 78 | 79 | #if RAPIDJSON_HAS_STDSTRING 80 | bool String(const std::basic_string& str) { 81 | return String(str.data(), SizeType(str.size())); 82 | } 83 | #endif 84 | 85 | bool StartObject() { 86 | PrettyPrefix(kObjectType); 87 | new (Base::level_stack_.template Push()) typename Base::Level(false); 88 | return Base::WriteStartObject(); 89 | } 90 | 91 | bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } 92 | 93 | bool EndObject(SizeType memberCount = 0) { 94 | (void)memberCount; 95 | RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); 96 | RAPIDJSON_ASSERT(!Base::level_stack_.template Top()->inArray); 97 | bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; 98 | 99 | if (!empty) { 100 | Base::os_->Put('\n'); 101 | WriteIndent(); 102 | } 103 | bool ret = Base::WriteEndObject(); 104 | (void)ret; 105 | RAPIDJSON_ASSERT(ret == true); 106 | if (Base::level_stack_.Empty()) // end of json text 107 | Base::os_->Flush(); 108 | return true; 109 | } 110 | 111 | bool StartArray() { 112 | PrettyPrefix(kArrayType); 113 | new (Base::level_stack_.template Push()) typename Base::Level(true); 114 | return Base::WriteStartArray(); 115 | } 116 | 117 | bool EndArray(SizeType memberCount = 0) { 118 | (void)memberCount; 119 | RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); 120 | RAPIDJSON_ASSERT(Base::level_stack_.template Top()->inArray); 121 | bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; 122 | 123 | if (!empty) { 124 | Base::os_->Put('\n'); 125 | WriteIndent(); 126 | } 127 | bool ret = Base::WriteEndArray(); 128 | (void)ret; 129 | RAPIDJSON_ASSERT(ret == true); 130 | if (Base::level_stack_.Empty()) // end of json text 131 | Base::os_->Flush(); 132 | return true; 133 | } 134 | 135 | //@} 136 | 137 | /*! @name Convenience extensions */ 138 | //@{ 139 | 140 | //! Simpler but slower overload. 141 | bool String(const Ch* str) { return String(str, internal::StrLen(str)); } 142 | bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } 143 | 144 | //@} 145 | protected: 146 | void PrettyPrefix(Type type) { 147 | (void)type; 148 | if (Base::level_stack_.GetSize() != 0) { // this value is not at root 149 | typename Base::Level* level = Base::level_stack_.template Top(); 150 | 151 | if (level->inArray) { 152 | if (level->valueCount > 0) { 153 | Base::os_->Put(','); // add comma if it is not the first element in array 154 | Base::os_->Put('\n'); 155 | } 156 | else 157 | Base::os_->Put('\n'); 158 | WriteIndent(); 159 | } 160 | else { // in object 161 | if (level->valueCount > 0) { 162 | if (level->valueCount % 2 == 0) { 163 | Base::os_->Put(','); 164 | Base::os_->Put('\n'); 165 | } 166 | else { 167 | Base::os_->Put(':'); 168 | Base::os_->Put(' '); 169 | } 170 | } 171 | else 172 | Base::os_->Put('\n'); 173 | 174 | if (level->valueCount % 2 == 0) 175 | WriteIndent(); 176 | } 177 | if (!level->inArray && level->valueCount % 2 == 0) 178 | RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name 179 | level->valueCount++; 180 | } 181 | else { 182 | RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root. 183 | Base::hasRoot_ = true; 184 | } 185 | } 186 | 187 | void WriteIndent() { 188 | size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_; 189 | PutN(*Base::os_, indentChar_, count); 190 | } 191 | 192 | Ch indentChar_; 193 | unsigned indentCharCount_; 194 | 195 | private: 196 | // Prohibit copy constructor & assignment operator. 197 | PrettyWriter(const PrettyWriter&); 198 | PrettyWriter& operator=(const PrettyWriter&); 199 | }; 200 | 201 | RAPIDJSON_NAMESPACE_END 202 | 203 | #ifdef __GNUC__ 204 | RAPIDJSON_DIAG_POP 205 | #endif 206 | 207 | #endif // RAPIDJSON_RAPIDJSON_H_ 208 | -------------------------------------------------------------------------------- /include/rapidjson/rapidjson.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_RAPIDJSON_H_ 16 | #define RAPIDJSON_RAPIDJSON_H_ 17 | 18 | /*!\file rapidjson.h 19 | \brief common definitions and configuration 20 | 21 | \see RAPIDJSON_CONFIG 22 | */ 23 | 24 | /*! \defgroup RAPIDJSON_CONFIG RapidJSON configuration 25 | \brief Configuration macros for library features 26 | 27 | Some RapidJSON features are configurable to adapt the library to a wide 28 | variety of platforms, environments and usage scenarios. Most of the 29 | features can be configured in terms of overriden or predefined 30 | preprocessor macros at compile-time. 31 | 32 | Some additional customization is available in the \ref RAPIDJSON_ERRORS APIs. 33 | 34 | \note These macros should be given on the compiler command-line 35 | (where applicable) to avoid inconsistent values when compiling 36 | different translation units of a single application. 37 | */ 38 | 39 | #include // malloc(), realloc(), free(), size_t 40 | #include // memset(), memcpy(), memmove(), memcmp() 41 | 42 | /////////////////////////////////////////////////////////////////////////////// 43 | // RAPIDJSON_VERSION_STRING 44 | // 45 | // ALWAYS synchronize the following 3 macros with corresponding variables in /CMakeLists.txt. 46 | // 47 | 48 | //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN 49 | // token stringification 50 | #define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x) 51 | #define RAPIDJSON_DO_STRINGIFY(x) #x 52 | //!@endcond 53 | 54 | /*! \def RAPIDJSON_MAJOR_VERSION 55 | \ingroup RAPIDJSON_CONFIG 56 | \brief Major version of RapidJSON in integer. 57 | */ 58 | /*! \def RAPIDJSON_MINOR_VERSION 59 | \ingroup RAPIDJSON_CONFIG 60 | \brief Minor version of RapidJSON in integer. 61 | */ 62 | /*! \def RAPIDJSON_PATCH_VERSION 63 | \ingroup RAPIDJSON_CONFIG 64 | \brief Patch version of RapidJSON in integer. 65 | */ 66 | /*! \def RAPIDJSON_VERSION_STRING 67 | \ingroup RAPIDJSON_CONFIG 68 | \brief Version of RapidJSON in ".." string format. 69 | */ 70 | #define RAPIDJSON_MAJOR_VERSION 1 71 | #define RAPIDJSON_MINOR_VERSION 0 72 | #define RAPIDJSON_PATCH_VERSION 2 73 | #define RAPIDJSON_VERSION_STRING \ 74 | RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION) 75 | 76 | /////////////////////////////////////////////////////////////////////////////// 77 | // RAPIDJSON_NAMESPACE_(BEGIN|END) 78 | /*! \def RAPIDJSON_NAMESPACE 79 | \ingroup RAPIDJSON_CONFIG 80 | \brief provide custom rapidjson namespace 81 | 82 | In order to avoid symbol clashes and/or "One Definition Rule" errors 83 | between multiple inclusions of (different versions of) RapidJSON in 84 | a single binary, users can customize the name of the main RapidJSON 85 | namespace. 86 | 87 | In case of a single nesting level, defining \c RAPIDJSON_NAMESPACE 88 | to a custom name (e.g. \c MyRapidJSON) is sufficient. If multiple 89 | levels are needed, both \ref RAPIDJSON_NAMESPACE_BEGIN and \ref 90 | RAPIDJSON_NAMESPACE_END need to be defined as well: 91 | 92 | \code 93 | // in some .cpp file 94 | #define RAPIDJSON_NAMESPACE my::rapidjson 95 | #define RAPIDJSON_NAMESPACE_BEGIN namespace my { namespace rapidjson { 96 | #define RAPIDJSON_NAMESPACE_END } } 97 | #include "rapidjson/..." 98 | \endcode 99 | 100 | \see rapidjson 101 | */ 102 | /*! \def RAPIDJSON_NAMESPACE_BEGIN 103 | \ingroup RAPIDJSON_CONFIG 104 | \brief provide custom rapidjson namespace (opening expression) 105 | \see RAPIDJSON_NAMESPACE 106 | */ 107 | /*! \def RAPIDJSON_NAMESPACE_END 108 | \ingroup RAPIDJSON_CONFIG 109 | \brief provide custom rapidjson namespace (closing expression) 110 | \see RAPIDJSON_NAMESPACE 111 | */ 112 | #ifndef RAPIDJSON_NAMESPACE 113 | #define RAPIDJSON_NAMESPACE rapidjson 114 | #endif 115 | #ifndef RAPIDJSON_NAMESPACE_BEGIN 116 | #define RAPIDJSON_NAMESPACE_BEGIN namespace RAPIDJSON_NAMESPACE { 117 | #endif 118 | #ifndef RAPIDJSON_NAMESPACE_END 119 | #define RAPIDJSON_NAMESPACE_END } 120 | #endif 121 | 122 | /////////////////////////////////////////////////////////////////////////////// 123 | // RAPIDJSON_NO_INT64DEFINE 124 | 125 | /*! \def RAPIDJSON_NO_INT64DEFINE 126 | \ingroup RAPIDJSON_CONFIG 127 | \brief Use external 64-bit integer types. 128 | 129 | RapidJSON requires the 64-bit integer types \c int64_t and \c uint64_t types 130 | to be available at global scope. 131 | 132 | If users have their own definition, define RAPIDJSON_NO_INT64DEFINE to 133 | prevent RapidJSON from defining its own types. 134 | */ 135 | #ifndef RAPIDJSON_NO_INT64DEFINE 136 | //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN 137 | #ifdef _MSC_VER 138 | #include "msinttypes/stdint.h" 139 | #include "msinttypes/inttypes.h" 140 | #else 141 | // Other compilers should have this. 142 | #include 143 | #include 144 | #endif 145 | //!@endcond 146 | #ifdef RAPIDJSON_DOXYGEN_RUNNING 147 | #define RAPIDJSON_NO_INT64DEFINE 148 | #endif 149 | #endif // RAPIDJSON_NO_INT64TYPEDEF 150 | 151 | /////////////////////////////////////////////////////////////////////////////// 152 | // RAPIDJSON_FORCEINLINE 153 | 154 | #ifndef RAPIDJSON_FORCEINLINE 155 | //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN 156 | #if defined(_MSC_VER) && !defined(NDEBUG) 157 | #define RAPIDJSON_FORCEINLINE __forceinline 158 | #elif defined(__GNUC__) && __GNUC__ >= 4 && !defined(NDEBUG) 159 | #define RAPIDJSON_FORCEINLINE __attribute__((always_inline)) 160 | #else 161 | #define RAPIDJSON_FORCEINLINE 162 | #endif 163 | //!@endcond 164 | #endif // RAPIDJSON_FORCEINLINE 165 | 166 | /////////////////////////////////////////////////////////////////////////////// 167 | // RAPIDJSON_ENDIAN 168 | #define RAPIDJSON_LITTLEENDIAN 0 //!< Little endian machine 169 | #define RAPIDJSON_BIGENDIAN 1 //!< Big endian machine 170 | 171 | //! Endianness of the machine. 172 | /*! 173 | \def RAPIDJSON_ENDIAN 174 | \ingroup RAPIDJSON_CONFIG 175 | 176 | GCC 4.6 provided macro for detecting endianness of the target machine. But other 177 | compilers may not have this. User can define RAPIDJSON_ENDIAN to either 178 | \ref RAPIDJSON_LITTLEENDIAN or \ref RAPIDJSON_BIGENDIAN. 179 | 180 | Default detection implemented with reference to 181 | \li https://gcc.gnu.org/onlinedocs/gcc-4.6.0/cpp/Common-Predefined-Macros.html 182 | \li http://www.boost.org/doc/libs/1_42_0/boost/detail/endian.hpp 183 | */ 184 | #ifndef RAPIDJSON_ENDIAN 185 | // Detect with GCC 4.6's macro 186 | # ifdef __BYTE_ORDER__ 187 | # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 188 | # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN 189 | # elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 190 | # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN 191 | # else 192 | # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. 193 | # endif // __BYTE_ORDER__ 194 | // Detect with GLIBC's endian.h 195 | # elif defined(__GLIBC__) 196 | # include 197 | # if (__BYTE_ORDER == __LITTLE_ENDIAN) 198 | # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN 199 | # elif (__BYTE_ORDER == __BIG_ENDIAN) 200 | # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN 201 | # else 202 | # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. 203 | # endif // __GLIBC__ 204 | // Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro 205 | # elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) 206 | # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN 207 | # elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) 208 | # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN 209 | // Detect with architecture macros 210 | # elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__) 211 | # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN 212 | # 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__) 213 | # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN 214 | # elif defined(RAPIDJSON_DOXYGEN_RUNNING) 215 | # define RAPIDJSON_ENDIAN 216 | # else 217 | # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. 218 | # endif 219 | #endif // RAPIDJSON_ENDIAN 220 | 221 | /////////////////////////////////////////////////////////////////////////////// 222 | // RAPIDJSON_64BIT 223 | 224 | //! Whether using 64-bit architecture 225 | #ifndef RAPIDJSON_64BIT 226 | #if defined(__LP64__) || defined(_WIN64) || defined(__EMSCRIPTEN__) 227 | #define RAPIDJSON_64BIT 1 228 | #else 229 | #define RAPIDJSON_64BIT 0 230 | #endif 231 | #endif // RAPIDJSON_64BIT 232 | 233 | /////////////////////////////////////////////////////////////////////////////// 234 | // RAPIDJSON_ALIGN 235 | 236 | //! Data alignment of the machine. 237 | /*! \ingroup RAPIDJSON_CONFIG 238 | \param x pointer to align 239 | 240 | Some machines require strict data alignment. Currently the default uses 4 bytes 241 | alignment. User can customize by defining the RAPIDJSON_ALIGN function macro., 242 | */ 243 | #ifndef RAPIDJSON_ALIGN 244 | #if RAPIDJSON_64BIT == 1 245 | #define RAPIDJSON_ALIGN(x) ((x + 7u) & ~7u) 246 | #else 247 | #define RAPIDJSON_ALIGN(x) ((x + 3u) & ~3u) 248 | #endif 249 | #endif 250 | 251 | /////////////////////////////////////////////////////////////////////////////// 252 | // RAPIDJSON_UINT64_C2 253 | 254 | //! Construct a 64-bit literal by a pair of 32-bit integer. 255 | /*! 256 | 64-bit literal with or without ULL suffix is prone to compiler warnings. 257 | UINT64_C() is C macro which cause compilation problems. 258 | Use this macro to define 64-bit constants by a pair of 32-bit integer. 259 | */ 260 | #ifndef RAPIDJSON_UINT64_C2 261 | #define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast(high32) << 32) | static_cast(low32)) 262 | #endif 263 | 264 | /////////////////////////////////////////////////////////////////////////////// 265 | // RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD 266 | 267 | /*! \def RAPIDJSON_SIMD 268 | \ingroup RAPIDJSON_CONFIG 269 | \brief Enable SSE2/SSE4.2 optimization. 270 | 271 | RapidJSON supports optimized implementations for some parsing operations 272 | based on the SSE2 or SSE4.2 SIMD extensions on modern Intel-compatible 273 | processors. 274 | 275 | To enable these optimizations, two different symbols can be defined; 276 | \code 277 | // Enable SSE2 optimization. 278 | #define RAPIDJSON_SSE2 279 | 280 | // Enable SSE4.2 optimization. 281 | #define RAPIDJSON_SSE42 282 | \endcode 283 | 284 | \c RAPIDJSON_SSE42 takes precedence, if both are defined. 285 | 286 | If any of these symbols is defined, RapidJSON defines the macro 287 | \c RAPIDJSON_SIMD to indicate the availability of the optimized code. 288 | */ 289 | #if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) \ 290 | || defined(RAPIDJSON_DOXYGEN_RUNNING) 291 | #define RAPIDJSON_SIMD 292 | #endif 293 | 294 | /////////////////////////////////////////////////////////////////////////////// 295 | // RAPIDJSON_NO_SIZETYPEDEFINE 296 | 297 | #ifndef RAPIDJSON_NO_SIZETYPEDEFINE 298 | /*! \def RAPIDJSON_NO_SIZETYPEDEFINE 299 | \ingroup RAPIDJSON_CONFIG 300 | \brief User-provided \c SizeType definition. 301 | 302 | In order to avoid using 32-bit size types for indexing strings and arrays, 303 | define this preprocessor symbol and provide the type rapidjson::SizeType 304 | before including RapidJSON: 305 | \code 306 | #define RAPIDJSON_NO_SIZETYPEDEFINE 307 | namespace rapidjson { typedef ::std::size_t SizeType; } 308 | #include "rapidjson/..." 309 | \endcode 310 | 311 | \see rapidjson::SizeType 312 | */ 313 | #ifdef RAPIDJSON_DOXYGEN_RUNNING 314 | #define RAPIDJSON_NO_SIZETYPEDEFINE 315 | #endif 316 | RAPIDJSON_NAMESPACE_BEGIN 317 | //! Size type (for string lengths, array sizes, etc.) 318 | /*! RapidJSON uses 32-bit array/string indices even on 64-bit platforms, 319 | instead of using \c size_t. Users may override the SizeType by defining 320 | \ref RAPIDJSON_NO_SIZETYPEDEFINE. 321 | */ 322 | typedef unsigned SizeType; 323 | RAPIDJSON_NAMESPACE_END 324 | #endif 325 | 326 | // always import std::size_t to rapidjson namespace 327 | RAPIDJSON_NAMESPACE_BEGIN 328 | using std::size_t; 329 | RAPIDJSON_NAMESPACE_END 330 | 331 | /////////////////////////////////////////////////////////////////////////////// 332 | // RAPIDJSON_ASSERT 333 | 334 | //! Assertion. 335 | /*! \ingroup RAPIDJSON_CONFIG 336 | By default, rapidjson uses C \c assert() for internal assertions. 337 | User can override it by defining RAPIDJSON_ASSERT(x) macro. 338 | 339 | \note Parsing errors are handled and can be customized by the 340 | \ref RAPIDJSON_ERRORS APIs. 341 | */ 342 | #ifndef RAPIDJSON_ASSERT 343 | #include 344 | #define RAPIDJSON_ASSERT(x) assert(x) 345 | #endif // RAPIDJSON_ASSERT 346 | 347 | /////////////////////////////////////////////////////////////////////////////// 348 | // RAPIDJSON_STATIC_ASSERT 349 | 350 | // Adopt from boost 351 | #ifndef RAPIDJSON_STATIC_ASSERT 352 | //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN 353 | RAPIDJSON_NAMESPACE_BEGIN 354 | template struct STATIC_ASSERTION_FAILURE; 355 | template <> struct STATIC_ASSERTION_FAILURE { enum { value = 1 }; }; 356 | template struct StaticAssertTest {}; 357 | RAPIDJSON_NAMESPACE_END 358 | 359 | #define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y) 360 | #define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y) 361 | #define RAPIDJSON_DO_JOIN2(X, Y) X##Y 362 | 363 | #if defined(__GNUC__) 364 | #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused)) 365 | #else 366 | #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE 367 | #endif 368 | //!@endcond 369 | 370 | /*! \def RAPIDJSON_STATIC_ASSERT 371 | \brief (Internal) macro to check for conditions at compile-time 372 | \param x compile-time condition 373 | \hideinitializer 374 | */ 375 | #define RAPIDJSON_STATIC_ASSERT(x) \ 376 | typedef ::RAPIDJSON_NAMESPACE::StaticAssertTest< \ 377 | sizeof(::RAPIDJSON_NAMESPACE::STATIC_ASSERTION_FAILURE)> \ 378 | RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE 379 | #endif 380 | 381 | /////////////////////////////////////////////////////////////////////////////// 382 | // Helpers 383 | 384 | //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN 385 | 386 | #define RAPIDJSON_MULTILINEMACRO_BEGIN do { 387 | #define RAPIDJSON_MULTILINEMACRO_END \ 388 | } while((void)0, 0) 389 | 390 | // adopted from Boost 391 | #define RAPIDJSON_VERSION_CODE(x,y,z) \ 392 | (((x)*100000) + ((y)*100) + (z)) 393 | 394 | /////////////////////////////////////////////////////////////////////////////// 395 | // RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF 396 | 397 | #if defined(__GNUC__) 398 | #define RAPIDJSON_GNUC \ 399 | RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__) 400 | #endif 401 | 402 | #if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,2,0)) 403 | 404 | #define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x)) 405 | #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x) 406 | #define RAPIDJSON_DIAG_OFF(x) \ 407 | RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x))) 408 | 409 | // push/pop support in Clang and GCC>=4.6 410 | #if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) 411 | #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push) 412 | #define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop) 413 | #else // GCC >= 4.2, < 4.6 414 | #define RAPIDJSON_DIAG_PUSH /* ignored */ 415 | #define RAPIDJSON_DIAG_POP /* ignored */ 416 | #endif 417 | 418 | #elif defined(_MSC_VER) 419 | 420 | // pragma (MSVC specific) 421 | #define RAPIDJSON_PRAGMA(x) __pragma(x) 422 | #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x)) 423 | 424 | #define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable: x) 425 | #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push) 426 | #define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop) 427 | 428 | #else 429 | 430 | #define RAPIDJSON_DIAG_OFF(x) /* ignored */ 431 | #define RAPIDJSON_DIAG_PUSH /* ignored */ 432 | #define RAPIDJSON_DIAG_POP /* ignored */ 433 | 434 | #endif // RAPIDJSON_DIAG_* 435 | 436 | /////////////////////////////////////////////////////////////////////////////// 437 | // C++11 features 438 | 439 | #ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS 440 | #if defined(__clang__) 441 | #define RAPIDJSON_HAS_CXX11_RVALUE_REFS __has_feature(cxx_rvalue_references) && \ 442 | (defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306) 443 | #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ 444 | (defined(_MSC_VER) && _MSC_VER >= 1600) 445 | 446 | #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1 447 | #else 448 | #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0 449 | #endif 450 | #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS 451 | 452 | #ifndef RAPIDJSON_HAS_CXX11_NOEXCEPT 453 | #if defined(__clang__) 454 | #define RAPIDJSON_HAS_CXX11_NOEXCEPT __has_feature(cxx_noexcept) 455 | #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) 456 | // (defined(_MSC_VER) && _MSC_VER >= ????) // not yet supported 457 | #define RAPIDJSON_HAS_CXX11_NOEXCEPT 1 458 | #else 459 | #define RAPIDJSON_HAS_CXX11_NOEXCEPT 0 460 | #endif 461 | #endif 462 | #if RAPIDJSON_HAS_CXX11_NOEXCEPT 463 | #define RAPIDJSON_NOEXCEPT noexcept 464 | #else 465 | #define RAPIDJSON_NOEXCEPT /* noexcept */ 466 | #endif // RAPIDJSON_HAS_CXX11_NOEXCEPT 467 | 468 | // no automatic detection, yet 469 | #ifndef RAPIDJSON_HAS_CXX11_TYPETRAITS 470 | #define RAPIDJSON_HAS_CXX11_TYPETRAITS 0 471 | #endif 472 | 473 | //!@endcond 474 | 475 | /////////////////////////////////////////////////////////////////////////////// 476 | // new/delete 477 | 478 | #ifndef RAPIDJSON_NEW 479 | ///! customization point for global \c new 480 | #define RAPIDJSON_NEW(x) new x 481 | #endif 482 | #ifndef RAPIDJSON_DELETE 483 | ///! customization point for global \c delete 484 | #define RAPIDJSON_DELETE(x) delete x 485 | #endif 486 | 487 | /////////////////////////////////////////////////////////////////////////////// 488 | // Allocators and Encodings 489 | 490 | #include "allocators.h" 491 | #include "encodings.h" 492 | 493 | /*! \namespace rapidjson 494 | \brief main RapidJSON namespace 495 | \see RAPIDJSON_NAMESPACE 496 | */ 497 | RAPIDJSON_NAMESPACE_BEGIN 498 | 499 | /////////////////////////////////////////////////////////////////////////////// 500 | // Stream 501 | 502 | /*! \class rapidjson::Stream 503 | \brief Concept for reading and writing characters. 504 | 505 | For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd(). 506 | 507 | For write-only stream, only need to implement Put() and Flush(). 508 | 509 | \code 510 | concept Stream { 511 | typename Ch; //!< Character type of the stream. 512 | 513 | //! Read the current character from stream without moving the read cursor. 514 | Ch Peek() const; 515 | 516 | //! Read the current character from stream and moving the read cursor to next character. 517 | Ch Take(); 518 | 519 | //! Get the current read cursor. 520 | //! \return Number of characters read from start. 521 | size_t Tell(); 522 | 523 | //! Begin writing operation at the current read pointer. 524 | //! \return The begin writer pointer. 525 | Ch* PutBegin(); 526 | 527 | //! Write a character. 528 | void Put(Ch c); 529 | 530 | //! Flush the buffer. 531 | void Flush(); 532 | 533 | //! End the writing operation. 534 | //! \param begin The begin write pointer returned by PutBegin(). 535 | //! \return Number of characters written. 536 | size_t PutEnd(Ch* begin); 537 | } 538 | \endcode 539 | */ 540 | 541 | //! Provides additional information for stream. 542 | /*! 543 | By using traits pattern, this type provides a default configuration for stream. 544 | For custom stream, this type can be specialized for other configuration. 545 | See TEST(Reader, CustomStringStream) in readertest.cpp for example. 546 | */ 547 | template 548 | struct StreamTraits { 549 | //! Whether to make local copy of stream for optimization during parsing. 550 | /*! 551 | By default, for safety, streams do not use local copy optimization. 552 | Stream that can be copied fast should specialize this, like StreamTraits. 553 | */ 554 | enum { copyOptimization = 0 }; 555 | }; 556 | 557 | //! Put N copies of a character to a stream. 558 | template 559 | inline void PutN(Stream& stream, Ch c, size_t n) { 560 | for (size_t i = 0; i < n; i++) 561 | stream.Put(c); 562 | } 563 | 564 | /////////////////////////////////////////////////////////////////////////////// 565 | // StringStream 566 | 567 | //! Read-only string stream. 568 | /*! \note implements Stream concept 569 | */ 570 | template 571 | struct GenericStringStream { 572 | typedef typename Encoding::Ch Ch; 573 | 574 | GenericStringStream(const Ch *src) : src_(src), head_(src) {} 575 | 576 | Ch Peek() const { return *src_; } 577 | Ch Take() { return *src_++; } 578 | size_t Tell() const { return static_cast(src_ - head_); } 579 | 580 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 581 | void Put(Ch) { RAPIDJSON_ASSERT(false); } 582 | void Flush() { RAPIDJSON_ASSERT(false); } 583 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 584 | 585 | const Ch* src_; //!< Current read position. 586 | const Ch* head_; //!< Original head of the string. 587 | }; 588 | 589 | template 590 | struct StreamTraits > { 591 | enum { copyOptimization = 1 }; 592 | }; 593 | 594 | //! String stream with UTF8 encoding. 595 | typedef GenericStringStream > StringStream; 596 | 597 | /////////////////////////////////////////////////////////////////////////////// 598 | // InsituStringStream 599 | 600 | //! A read-write string stream. 601 | /*! This string stream is particularly designed for in-situ parsing. 602 | \note implements Stream concept 603 | */ 604 | template 605 | struct GenericInsituStringStream { 606 | typedef typename Encoding::Ch Ch; 607 | 608 | GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {} 609 | 610 | // Read 611 | Ch Peek() { return *src_; } 612 | Ch Take() { return *src_++; } 613 | size_t Tell() { return static_cast(src_ - head_); } 614 | 615 | // Write 616 | void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; } 617 | 618 | Ch* PutBegin() { return dst_ = src_; } 619 | size_t PutEnd(Ch* begin) { return static_cast(dst_ - begin); } 620 | void Flush() {} 621 | 622 | Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; } 623 | void Pop(size_t count) { dst_ -= count; } 624 | 625 | Ch* src_; 626 | Ch* dst_; 627 | Ch* head_; 628 | }; 629 | 630 | template 631 | struct StreamTraits > { 632 | enum { copyOptimization = 1 }; 633 | }; 634 | 635 | //! Insitu string stream with UTF8 encoding. 636 | typedef GenericInsituStringStream > InsituStringStream; 637 | 638 | /////////////////////////////////////////////////////////////////////////////// 639 | // Type 640 | 641 | //! Type of JSON value 642 | enum Type { 643 | kNullType = 0, //!< null 644 | kFalseType = 1, //!< false 645 | kTrueType = 2, //!< true 646 | kObjectType = 3, //!< object 647 | kArrayType = 4, //!< array 648 | kStringType = 5, //!< string 649 | kNumberType = 6 //!< number 650 | }; 651 | 652 | RAPIDJSON_NAMESPACE_END 653 | 654 | #endif // RAPIDJSON_RAPIDJSON_H_ 655 | -------------------------------------------------------------------------------- /include/rapidjson/stringbuffer.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_STRINGBUFFER_H_ 16 | #define RAPIDJSON_STRINGBUFFER_H_ 17 | 18 | #include "rapidjson.h" 19 | 20 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 21 | #include // std::move 22 | #endif 23 | 24 | #include "internal/stack.h" 25 | 26 | RAPIDJSON_NAMESPACE_BEGIN 27 | 28 | //! Represents an in-memory output stream. 29 | /*! 30 | \tparam Encoding Encoding of the stream. 31 | \tparam Allocator type for allocating memory buffer. 32 | \note implements Stream concept 33 | */ 34 | template 35 | class GenericStringBuffer { 36 | public: 37 | typedef typename Encoding::Ch Ch; 38 | 39 | GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} 40 | 41 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 42 | GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {} 43 | GenericStringBuffer& operator=(GenericStringBuffer&& rhs) { 44 | if (&rhs != this) 45 | stack_ = std::move(rhs.stack_); 46 | return *this; 47 | } 48 | #endif 49 | 50 | void Put(Ch c) { *stack_.template Push() = c; } 51 | void Flush() {} 52 | 53 | void Clear() { stack_.Clear(); } 54 | void ShrinkToFit() { 55 | // Push and pop a null terminator. This is safe. 56 | *stack_.template Push() = '\0'; 57 | stack_.ShrinkToFit(); 58 | stack_.template Pop(1); 59 | } 60 | Ch* Push(size_t count) { return stack_.template Push(count); } 61 | void Pop(size_t count) { stack_.template Pop(count); } 62 | 63 | const Ch* GetString() const { 64 | // Push and pop a null terminator. This is safe. 65 | *stack_.template Push() = '\0'; 66 | stack_.template Pop(1); 67 | 68 | return stack_.template Bottom(); 69 | } 70 | 71 | size_t GetSize() const { return stack_.GetSize(); } 72 | 73 | static const size_t kDefaultCapacity = 256; 74 | mutable internal::Stack stack_; 75 | 76 | private: 77 | // Prohibit copy constructor & assignment operator. 78 | GenericStringBuffer(const GenericStringBuffer&); 79 | GenericStringBuffer& operator=(const GenericStringBuffer&); 80 | }; 81 | 82 | //! String buffer with UTF8 encoding 83 | typedef GenericStringBuffer > StringBuffer; 84 | 85 | //! Implement specialized version of PutN() with memset() for better performance. 86 | template<> 87 | inline void PutN(GenericStringBuffer >& stream, char c, size_t n) { 88 | std::memset(stream.stack_.Push(n), c, n * sizeof(c)); 89 | } 90 | 91 | RAPIDJSON_NAMESPACE_END 92 | 93 | #endif // RAPIDJSON_STRINGBUFFER_H_ 94 | -------------------------------------------------------------------------------- /include/rapidjson/writer.h: -------------------------------------------------------------------------------- 1 | // Tencent is pleased to support the open source community by making RapidJSON available. 2 | // 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. 4 | // 5 | // Licensed under the MIT License (the "License"); you may not use this file except 6 | // in compliance with the License. You may obtain a copy of the License at 7 | // 8 | // http://opensource.org/licenses/MIT 9 | // 10 | // Unless required by applicable law or agreed to in writing, software distributed 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | // specific language governing permissions and limitations under the License. 14 | 15 | #ifndef RAPIDJSON_WRITER_H_ 16 | #define RAPIDJSON_WRITER_H_ 17 | 18 | #include "rapidjson.h" 19 | #include "internal/stack.h" 20 | #include "internal/strfunc.h" 21 | #include "internal/dtoa.h" 22 | #include "internal/itoa.h" 23 | #include "stringbuffer.h" 24 | #include // placement new 25 | 26 | #if RAPIDJSON_HAS_STDSTRING 27 | #include 28 | #endif 29 | 30 | #ifdef _MSC_VER 31 | RAPIDJSON_DIAG_PUSH 32 | RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant 33 | #endif 34 | 35 | RAPIDJSON_NAMESPACE_BEGIN 36 | 37 | //! JSON writer 38 | /*! Writer implements the concept Handler. 39 | It generates JSON text by events to an output os. 40 | 41 | User may programmatically calls the functions of a writer to generate JSON text. 42 | 43 | On the other side, a writer can also be passed to objects that generates events, 44 | 45 | for example Reader::Parse() and Document::Accept(). 46 | 47 | \tparam OutputStream Type of output stream. 48 | \tparam SourceEncoding Encoding of source string. 49 | \tparam TargetEncoding Encoding of output stream. 50 | \tparam StackAllocator Type of allocator for allocating memory of stack. 51 | \note implements Handler concept 52 | */ 53 | template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator> 54 | class Writer { 55 | public: 56 | typedef typename SourceEncoding::Ch Ch; 57 | 58 | //! Constructor 59 | /*! \param os Output stream. 60 | \param stackAllocator User supplied allocator. If it is null, it will create a private one. 61 | \param levelDepth Initial capacity of stack. 62 | */ 63 | explicit 64 | Writer(OutputStream& os, StackAllocator* stackAllocator = 0, size_t levelDepth = kDefaultLevelDepth) : 65 | os_(&os), level_stack_(stackAllocator, levelDepth * sizeof(Level)), hasRoot_(false) {} 66 | 67 | explicit 68 | Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : 69 | os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), hasRoot_(false) {} 70 | 71 | //! Reset the writer with a new stream. 72 | /*! 73 | This function reset the writer with a new stream and default settings, 74 | in order to make a Writer object reusable for output multiple JSONs. 75 | 76 | \param os New output stream. 77 | \code 78 | Writer writer(os1); 79 | writer.StartObject(); 80 | // ... 81 | writer.EndObject(); 82 | 83 | writer.Reset(os2); 84 | writer.StartObject(); 85 | // ... 86 | writer.EndObject(); 87 | \endcode 88 | */ 89 | void Reset(OutputStream& os) { 90 | os_ = &os; 91 | hasRoot_ = false; 92 | level_stack_.Clear(); 93 | } 94 | 95 | //! Checks whether the output is a complete JSON. 96 | /*! 97 | A complete JSON has a complete root object or array. 98 | */ 99 | bool IsComplete() const { 100 | return hasRoot_ && level_stack_.Empty(); 101 | } 102 | 103 | /*!@name Implementation of Handler 104 | \see Handler 105 | */ 106 | //@{ 107 | 108 | bool Null() { Prefix(kNullType); return WriteNull(); } 109 | bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return WriteBool(b); } 110 | bool Int(int i) { Prefix(kNumberType); return WriteInt(i); } 111 | bool Uint(unsigned u) { Prefix(kNumberType); return WriteUint(u); } 112 | bool Int64(int64_t i64) { Prefix(kNumberType); return WriteInt64(i64); } 113 | bool Uint64(uint64_t u64) { Prefix(kNumberType); return WriteUint64(u64); } 114 | 115 | //! Writes the given \c double value to the stream 116 | /*! 117 | \param d The value to be written. 118 | \return Whether it is succeed. 119 | */ 120 | bool Double(double d) { Prefix(kNumberType); return WriteDouble(d); } 121 | 122 | bool String(const Ch* str, SizeType length, bool copy = false) { 123 | (void)copy; 124 | Prefix(kStringType); 125 | return WriteString(str, length); 126 | } 127 | 128 | #if RAPIDJSON_HAS_STDSTRING 129 | bool String(const std::basic_string& str) { 130 | return String(str.data(), SizeType(str.size())); 131 | } 132 | #endif 133 | 134 | bool StartObject() { 135 | Prefix(kObjectType); 136 | new (level_stack_.template Push()) Level(false); 137 | return WriteStartObject(); 138 | } 139 | 140 | bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } 141 | 142 | bool EndObject(SizeType memberCount = 0) { 143 | (void)memberCount; 144 | RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); 145 | RAPIDJSON_ASSERT(!level_stack_.template Top()->inArray); 146 | level_stack_.template Pop(1); 147 | bool ret = WriteEndObject(); 148 | if (level_stack_.Empty()) // end of json text 149 | os_->Flush(); 150 | return ret; 151 | } 152 | 153 | bool StartArray() { 154 | Prefix(kArrayType); 155 | new (level_stack_.template Push()) Level(true); 156 | return WriteStartArray(); 157 | } 158 | 159 | bool EndArray(SizeType elementCount = 0) { 160 | (void)elementCount; 161 | RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); 162 | RAPIDJSON_ASSERT(level_stack_.template Top()->inArray); 163 | level_stack_.template Pop(1); 164 | bool ret = WriteEndArray(); 165 | if (level_stack_.Empty()) // end of json text 166 | os_->Flush(); 167 | return ret; 168 | } 169 | //@} 170 | 171 | /*! @name Convenience extensions */ 172 | //@{ 173 | 174 | //! Simpler but slower overload. 175 | bool String(const Ch* str) { return String(str, internal::StrLen(str)); } 176 | bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } 177 | 178 | //@} 179 | 180 | protected: 181 | //! Information for each nested level 182 | struct Level { 183 | Level(bool inArray_) : valueCount(0), inArray(inArray_) {} 184 | size_t valueCount; //!< number of values in this level 185 | bool inArray; //!< true if in array, otherwise in object 186 | }; 187 | 188 | static const size_t kDefaultLevelDepth = 32; 189 | 190 | bool WriteNull() { 191 | os_->Put('n'); os_->Put('u'); os_->Put('l'); os_->Put('l'); return true; 192 | } 193 | 194 | bool WriteBool(bool b) { 195 | if (b) { 196 | os_->Put('t'); os_->Put('r'); os_->Put('u'); os_->Put('e'); 197 | } 198 | else { 199 | os_->Put('f'); os_->Put('a'); os_->Put('l'); os_->Put('s'); os_->Put('e'); 200 | } 201 | return true; 202 | } 203 | 204 | bool WriteInt(int i) { 205 | char buffer[11]; 206 | const char* end = internal::i32toa(i, buffer); 207 | for (const char* p = buffer; p != end; ++p) 208 | os_->Put(*p); 209 | return true; 210 | } 211 | 212 | bool WriteUint(unsigned u) { 213 | char buffer[10]; 214 | const char* end = internal::u32toa(u, buffer); 215 | for (const char* p = buffer; p != end; ++p) 216 | os_->Put(*p); 217 | return true; 218 | } 219 | 220 | bool WriteInt64(int64_t i64) { 221 | char buffer[21]; 222 | const char* end = internal::i64toa(i64, buffer); 223 | for (const char* p = buffer; p != end; ++p) 224 | os_->Put(*p); 225 | return true; 226 | } 227 | 228 | bool WriteUint64(uint64_t u64) { 229 | char buffer[20]; 230 | char* end = internal::u64toa(u64, buffer); 231 | for (char* p = buffer; p != end; ++p) 232 | os_->Put(*p); 233 | return true; 234 | } 235 | 236 | bool WriteDouble(double d) { 237 | char buffer[25]; 238 | char* end = internal::dtoa(d, buffer); 239 | for (char* p = buffer; p != end; ++p) 240 | os_->Put(*p); 241 | return true; 242 | } 243 | 244 | bool WriteString(const Ch* str, SizeType length) { 245 | static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 246 | static const char escape[256] = { 247 | #define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 248 | //0 1 2 3 4 5 6 7 8 9 A B C D E F 249 | 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'b', 't', 'n', 'u', 'f', 'r', 'u', 'u', // 00 250 | 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 10 251 | 0, 0, '"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 252 | Z16, Z16, // 30~4F 253 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, // 50 254 | Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 // 60~FF 255 | #undef Z16 256 | }; 257 | 258 | os_->Put('\"'); 259 | GenericStringStream is(str); 260 | while (is.Tell() < length) { 261 | const Ch c = is.Peek(); 262 | if (!TargetEncoding::supportUnicode && (unsigned)c >= 0x80) { 263 | // Unicode escaping 264 | unsigned codepoint; 265 | if (!SourceEncoding::Decode(is, &codepoint)) 266 | return false; 267 | os_->Put('\\'); 268 | os_->Put('u'); 269 | if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) { 270 | os_->Put(hexDigits[(codepoint >> 12) & 15]); 271 | os_->Put(hexDigits[(codepoint >> 8) & 15]); 272 | os_->Put(hexDigits[(codepoint >> 4) & 15]); 273 | os_->Put(hexDigits[(codepoint ) & 15]); 274 | } 275 | else { 276 | RAPIDJSON_ASSERT(codepoint >= 0x010000 && codepoint <= 0x10FFFF); 277 | // Surrogate pair 278 | unsigned s = codepoint - 0x010000; 279 | unsigned lead = (s >> 10) + 0xD800; 280 | unsigned trail = (s & 0x3FF) + 0xDC00; 281 | os_->Put(hexDigits[(lead >> 12) & 15]); 282 | os_->Put(hexDigits[(lead >> 8) & 15]); 283 | os_->Put(hexDigits[(lead >> 4) & 15]); 284 | os_->Put(hexDigits[(lead ) & 15]); 285 | os_->Put('\\'); 286 | os_->Put('u'); 287 | os_->Put(hexDigits[(trail >> 12) & 15]); 288 | os_->Put(hexDigits[(trail >> 8) & 15]); 289 | os_->Put(hexDigits[(trail >> 4) & 15]); 290 | os_->Put(hexDigits[(trail ) & 15]); 291 | } 292 | } 293 | else if ((sizeof(Ch) == 1 || (unsigned)c < 256) && escape[(unsigned char)c]) { 294 | is.Take(); 295 | os_->Put('\\'); 296 | os_->Put(escape[(unsigned char)c]); 297 | if (escape[(unsigned char)c] == 'u') { 298 | os_->Put('0'); 299 | os_->Put('0'); 300 | os_->Put(hexDigits[(unsigned char)c >> 4]); 301 | os_->Put(hexDigits[(unsigned char)c & 0xF]); 302 | } 303 | } 304 | else 305 | if (!Transcoder::Transcode(is, *os_)) 306 | return false; 307 | } 308 | os_->Put('\"'); 309 | return true; 310 | } 311 | 312 | bool WriteStartObject() { os_->Put('{'); return true; } 313 | bool WriteEndObject() { os_->Put('}'); return true; } 314 | bool WriteStartArray() { os_->Put('['); return true; } 315 | bool WriteEndArray() { os_->Put(']'); return true; } 316 | 317 | void Prefix(Type type) { 318 | (void)type; 319 | if (level_stack_.GetSize() != 0) { // this value is not at root 320 | Level* level = level_stack_.template Top(); 321 | if (level->valueCount > 0) { 322 | if (level->inArray) 323 | os_->Put(','); // add comma if it is not the first element in array 324 | else // in object 325 | os_->Put((level->valueCount % 2 == 0) ? ',' : ':'); 326 | } 327 | if (!level->inArray && level->valueCount % 2 == 0) 328 | RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name 329 | level->valueCount++; 330 | } 331 | else { 332 | RAPIDJSON_ASSERT(!hasRoot_); // Should only has one and only one root. 333 | hasRoot_ = true; 334 | } 335 | } 336 | 337 | OutputStream* os_; 338 | internal::Stack level_stack_; 339 | bool hasRoot_; 340 | 341 | private: 342 | // Prohibit copy constructor & assignment operator. 343 | Writer(const Writer&); 344 | Writer& operator=(const Writer&); 345 | }; 346 | 347 | // Full specialization for StringStream to prevent memory copying 348 | 349 | template<> 350 | inline bool Writer::WriteInt(int i) { 351 | char *buffer = os_->Push(11); 352 | const char* end = internal::i32toa(i, buffer); 353 | os_->Pop(11 - (end - buffer)); 354 | return true; 355 | } 356 | 357 | template<> 358 | inline bool Writer::WriteUint(unsigned u) { 359 | char *buffer = os_->Push(10); 360 | const char* end = internal::u32toa(u, buffer); 361 | os_->Pop(10 - (end - buffer)); 362 | return true; 363 | } 364 | 365 | template<> 366 | inline bool Writer::WriteInt64(int64_t i64) { 367 | char *buffer = os_->Push(21); 368 | const char* end = internal::i64toa(i64, buffer); 369 | os_->Pop(21 - (end - buffer)); 370 | return true; 371 | } 372 | 373 | template<> 374 | inline bool Writer::WriteUint64(uint64_t u) { 375 | char *buffer = os_->Push(20); 376 | const char* end = internal::u64toa(u, buffer); 377 | os_->Pop(20 - (end - buffer)); 378 | return true; 379 | } 380 | 381 | template<> 382 | inline bool Writer::WriteDouble(double d) { 383 | char *buffer = os_->Push(25); 384 | char* end = internal::dtoa(d, buffer); 385 | os_->Pop(25 - (end - buffer)); 386 | return true; 387 | } 388 | 389 | RAPIDJSON_NAMESPACE_END 390 | 391 | #ifdef _MSC_VER 392 | RAPIDJSON_DIAG_POP 393 | #endif 394 | 395 | #endif // RAPIDJSON_RAPIDJSON_H_ 396 | -------------------------------------------------------------------------------- /jsonUdf-test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Cloudera Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include 18 | #include "jsonUdf.h" 19 | 20 | using namespace impala; 21 | using namespace impala_udf; 22 | using namespace std; 23 | 24 | int main(int argc, char** argv) { 25 | bool passed = true; 26 | // Using the test harness helpers, validate the UDF returns correct results. 27 | // These tests validate: 28 | // AddUdf(1, 2) == 3 29 | // AddUdf(null, 2) == null 30 | { 31 | const char * json = "{\"project\":\"rapidjson\",\"stars\":10}"; 32 | passed &= UdfTestHarness::ValidateUdf( 33 | JsonGetObject, StringVal(json), StringVal("$.project"), StringVal("rapidjson")); 34 | } 35 | 36 | { 37 | const char * json = "{\"store\":{\"fruit\":[{\"weight\":8,\"type\":\"apple\"},{\"weight\":9,\"type\":\"pear\"}],\"bicycle\":{\"price\":19.95,\"color\":\"red\"}},\"email\":\"amy@only_for_json_udf_test.net\",\"owner\":\"amy\"}"; 38 | passed &= UdfTestHarness::ValidateUdf( 39 | JsonGetObject, StringVal(json), StringVal("$.owner"), StringVal("amy")); 40 | passed &= UdfTestHarness::ValidateUdf( 41 | JsonGetObject, StringVal(json), StringVal("$.store.fruit[0]"), StringVal("{\"weight\":8,\"type\":\"apple\"}")); 42 | passed &= UdfTestHarness::ValidateUdf( 43 | JsonGetObject, StringVal(json), StringVal("$.non_exist_key"), StringVal::null()); 44 | } 45 | 46 | cout << "Tests " << (passed ? "Passed." : "Failed.") << endl; 47 | return !passed; 48 | } 49 | -------------------------------------------------------------------------------- /jsonUdf.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2015 Steven Han 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | // Author : Steven Han 16 | // Rapid json is obtainable at 17 | // 18 | // current status: 19 | // - input should be a String 20 | // - output is always a String 21 | // - llvm udf works fine. .so udf wasn't tested 22 | // - selector is the same with hive get_json_object, except that * operator is not supported 23 | 24 | 25 | // we have to define this macro before is included 26 | // so compile on linux works 27 | #define __STDC_FORMAT_MACROS 28 | 29 | #include 30 | #include "jsonUdf.h" 31 | #include 32 | 33 | // customize rapidjson namespace name to avoid version clashes 34 | #define RAPIDJSON_NAMESPACE json_get_object::rapidjson 35 | #define RAPIDJSON_NAMESPACE_BEGIN namespace json_get_object { namespace rapidjson { 36 | #define RAPIDJSON_NAMESPACE_END } } 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | #include 43 | #include 44 | 45 | #include 46 | #include 47 | 48 | StringVal JsonGetObject(FunctionContext *context, const StringVal & jsonVal, const StringVal & selectorVal) { 49 | if (jsonVal.is_null) return StringVal::null(); 50 | if (selectorVal.is_null) return StringVal::null(); 51 | 52 | std::string json((const char*)jsonVal.ptr, jsonVal.len); 53 | std::string selector((const char*)selectorVal.ptr, selectorVal.len); 54 | 55 | RAPIDJSON_NAMESPACE::Document d; 56 | d.Parse(json.c_str()); 57 | if (d.HasParseError()) { 58 | // context->AddWarning("Failed to parse json string"); 59 | return StringVal::null(); 60 | } 61 | 62 | const char * pSel = selector.c_str(); 63 | RAPIDJSON_NAMESPACE::Value *currentVal = NULL; 64 | int i; 65 | bool inBracket = false; 66 | std::string token; 67 | 68 | #define selectValByToken(tok) { \ 69 | RAPIDJSON_NAMESPACE::Value& va = *currentVal; \ 70 | RAPIDJSON_NAMESPACE::Value key(RAPIDJSON_NAMESPACE::StringRef(tok.c_str())); \ 71 | if (va.HasMember(key)) { \ 72 | currentVal = &(va[key]); \ 73 | } else { \ 74 | /* context->AddWarning("no member"); */ \ 75 | /* context->AddWarning(tok.c_str()); */ \ 76 | return StringVal::null(); \ 77 | } \ 78 | } 79 | 80 | if (pSel[0] != '$') { 81 | context->SetError("seletor should start with $"); 82 | return StringVal::null(); 83 | } 84 | currentVal = &d; 85 | for (i=1; iSetError("$ can only be placed at start"); 89 | return StringVal::null(); 90 | case '.': 91 | if (token.size() != 0) 92 | selectValByToken(token); 93 | token.clear(); 94 | break; 95 | case '[': 96 | if (token.size() != 0) { 97 | selectValByToken(token); 98 | token.clear(); 99 | } 100 | if (inBracket) { 101 | context->SetError("[ in [] is not allowed"); 102 | return StringVal::null(); 103 | } 104 | if (!currentVal->IsArray()) { 105 | //context->SetWarning("bracket on a non-array object"); 106 | return StringVal::null(); 107 | } 108 | 109 | inBracket = true; 110 | break; 111 | case ']': 112 | if (!inBracket) { 113 | context->SetError("] symbol without ["); 114 | return StringVal::null(); 115 | } 116 | if (token.compare("*")==0) { 117 | // TODO: return all in current list 118 | context->SetError("* symbol is not supported, yet"); 119 | return StringVal::null(); 120 | } else { 121 | RAPIDJSON_NAMESPACE::Value& va = *currentVal; 122 | int idx = atoi(token.c_str()); 123 | if (idx>=va.Size()) { 124 | //context->AddWarning("array index is too big"); 125 | return StringVal::null(); 126 | } 127 | currentVal = &(va[idx]); 128 | token.clear(); 129 | } 130 | inBracket = false; 131 | break; 132 | default: 133 | token += pSel[i]; 134 | break; 135 | } 136 | } 137 | if (inBracket) { 138 | context->SetError("bracket is not closed"); 139 | return StringVal::null(); 140 | } 141 | if (token.size() != 0) { 142 | selectValByToken(token); 143 | token.clear(); 144 | } 145 | 146 | if (currentVal->IsNull()) { 147 | return StringVal::null(); 148 | } 149 | if (currentVal->IsBool()) { 150 | bool val = currentVal->GetBool(); 151 | if (val) { 152 | StringVal result((uint8_t*)"true", 4); 153 | return result; 154 | } else { 155 | StringVal result((uint8_t*)"false", 5); 156 | return result; 157 | } 158 | } 159 | if (currentVal->IsNumber()) { 160 | StringVal result(context, 32); 161 | int len; 162 | if (currentVal->IsInt64()) { 163 | // covers int, int64, uint 164 | len = snprintf((char*)result.ptr, 32, "%" PRId64, currentVal->GetInt64()); 165 | } else if (currentVal->IsUint64()) { 166 | // covers uint64 167 | len = snprintf((char*)result.ptr, 32, "%" PRIu64, currentVal->GetUint64()); 168 | } else { 169 | // covers double 170 | len = snprintf((char*)result.ptr, 32, "%f", currentVal->GetDouble()); 171 | } 172 | result.len = len; 173 | return result; 174 | } 175 | if (currentVal->IsString()) { 176 | StringVal result(context, currentVal->GetStringLength()); 177 | memcpy(result.ptr, currentVal->GetString(), currentVal->GetStringLength()); 178 | return result; 179 | } 180 | // other cases : object, array 181 | RAPIDJSON_NAMESPACE::StringBuffer buffer; 182 | RAPIDJSON_NAMESPACE::Writer writer(buffer); 183 | currentVal->Accept(writer); 184 | 185 | const char * written = buffer.GetString(); 186 | int writtenSize = strlen(written); 187 | 188 | StringVal result(context, writtenSize); 189 | memcpy(result.ptr, written, writtenSize); 190 | return result; 191 | } 192 | -------------------------------------------------------------------------------- /jsonUdf.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Cloudera Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | 16 | #ifndef SAMPLES_UDF_H 17 | #define SAMPLES_UDF_H 18 | 19 | #include 20 | 21 | using namespace impala_udf; 22 | 23 | StringVal JsonGetObject(FunctionContext* context, const StringVal& json, const StringVal& selector); 24 | 25 | #endif 26 | --------------------------------------------------------------------------------