Revert "Normalize all the line endings"

This reverts commit 6047e3ce128954ec594e9a893ef2125c9f9b61c7.
This commit is contained in:
Milo Yip 2016-03-04 11:51:30 +08:00
parent 6047e3ce12
commit 7fb84d304b
41 changed files with 8275 additions and 8275 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1 +1 @@
sample.json is obtained from http://code.google.com/p/json-test-suite/downloads/detail?name=sample.zip sample.json is obtained from http://code.google.com/p/json-test-suite/downloads/detail?name=sample.zip

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,32 +1,32 @@
// JSON condenser example // JSON condenser example
// This example parses JSON text from stdin with validation, // This example parses JSON text from stdin with validation,
// and re-output the JSON content to stdout without whitespace. // and re-output the JSON content to stdout without whitespace.
#include "rapidjson/reader.h" #include "rapidjson/reader.h"
#include "rapidjson/writer.h" #include "rapidjson/writer.h"
#include "rapidjson/filereadstream.h" #include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h" #include "rapidjson/filewritestream.h"
#include "rapidjson/error/en.h" #include "rapidjson/error/en.h"
using namespace rapidjson; using namespace rapidjson;
int main(int, char*[]) { int main(int, char*[]) {
// Prepare JSON reader and input stream. // Prepare JSON reader and input stream.
Reader reader; Reader reader;
char readBuffer[65536]; char readBuffer[65536];
FileReadStream is(stdin, readBuffer, sizeof(readBuffer)); FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
// Prepare JSON writer and output stream. // Prepare JSON writer and output stream.
char writeBuffer[65536]; char writeBuffer[65536];
FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer)); FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
Writer<FileWriteStream> writer(os); Writer<FileWriteStream> writer(os);
// JSON reader parse from the input stream and let writer generate the output. // JSON reader parse from the input stream and let writer generate the output.
if (!reader.Parse(is, writer)) { if (!reader.Parse(is, writer)) {
fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode())); fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode()));
return 1; return 1;
} }
return 0; return 0;
} }

View File

@ -1,30 +1,30 @@
// JSON pretty formatting example // JSON pretty formatting example
// This example can only handle UTF-8. For handling other encodings, see prettyauto example. // This example can only handle UTF-8. For handling other encodings, see prettyauto example.
#include "rapidjson/reader.h" #include "rapidjson/reader.h"
#include "rapidjson/prettywriter.h" #include "rapidjson/prettywriter.h"
#include "rapidjson/filereadstream.h" #include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h" #include "rapidjson/filewritestream.h"
#include "rapidjson/error/en.h" #include "rapidjson/error/en.h"
using namespace rapidjson; using namespace rapidjson;
int main(int, char*[]) { int main(int, char*[]) {
// Prepare reader and input stream. // Prepare reader and input stream.
Reader reader; Reader reader;
char readBuffer[65536]; char readBuffer[65536];
FileReadStream is(stdin, readBuffer, sizeof(readBuffer)); FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
// Prepare writer and output stream. // Prepare writer and output stream.
char writeBuffer[65536]; char writeBuffer[65536];
FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer)); FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
PrettyWriter<FileWriteStream> writer(os); PrettyWriter<FileWriteStream> writer(os);
// JSON reader parse from the input stream and let writer generate the output. // JSON reader parse from the input stream and let writer generate the output.
if (!reader.Parse<kParseValidateEncodingFlag>(is, writer)) { if (!reader.Parse<kParseValidateEncodingFlag>(is, writer)) {
fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode())); fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode()));
return 1; return 1;
} }
return 0; return 0;
} }

View File

@ -1,56 +1,56 @@
// JSON pretty formatting example // JSON pretty formatting example
// This example can handle UTF-8/UTF-16LE/UTF-16BE/UTF-32LE/UTF-32BE. // This example can handle UTF-8/UTF-16LE/UTF-16BE/UTF-32LE/UTF-32BE.
// The input firstly convert to UTF8, and then write to the original encoding with pretty formatting. // The input firstly convert to UTF8, and then write to the original encoding with pretty formatting.
#include "rapidjson/reader.h" #include "rapidjson/reader.h"
#include "rapidjson/prettywriter.h" #include "rapidjson/prettywriter.h"
#include "rapidjson/filereadstream.h" #include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h" #include "rapidjson/filewritestream.h"
#include "rapidjson/encodedstream.h" // NEW #include "rapidjson/encodedstream.h" // NEW
#include "rapidjson/error/en.h" #include "rapidjson/error/en.h"
#ifdef _WIN32 #ifdef _WIN32
#include <fcntl.h> #include <fcntl.h>
#include <io.h> #include <io.h>
#endif #endif
using namespace rapidjson; using namespace rapidjson;
int main(int, char*[]) { int main(int, char*[]) {
#ifdef _WIN32 #ifdef _WIN32
// Prevent Windows converting between CR+LF and LF // Prevent Windows converting between CR+LF and LF
_setmode(_fileno(stdin), _O_BINARY); // NEW _setmode(_fileno(stdin), _O_BINARY); // NEW
_setmode(_fileno(stdout), _O_BINARY); // NEW _setmode(_fileno(stdout), _O_BINARY); // NEW
#endif #endif
// Prepare reader and input stream. // Prepare reader and input stream.
//Reader reader; //Reader reader;
GenericReader<AutoUTF<unsigned>, UTF8<> > reader; // CHANGED GenericReader<AutoUTF<unsigned>, UTF8<> > reader; // CHANGED
char readBuffer[65536]; char readBuffer[65536];
FileReadStream is(stdin, readBuffer, sizeof(readBuffer)); FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
AutoUTFInputStream<unsigned, FileReadStream> eis(is); // NEW AutoUTFInputStream<unsigned, FileReadStream> eis(is); // NEW
// Prepare writer and output stream. // Prepare writer and output stream.
char writeBuffer[65536]; char writeBuffer[65536];
FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer)); FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
#if 1 #if 1
// Use the same Encoding of the input. Also use BOM according to input. // Use the same Encoding of the input. Also use BOM according to input.
typedef AutoUTFOutputStream<unsigned, FileWriteStream> OutputStream; // NEW typedef AutoUTFOutputStream<unsigned, FileWriteStream> OutputStream; // NEW
OutputStream eos(os, eis.GetType(), eis.HasBOM()); // NEW OutputStream eos(os, eis.GetType(), eis.HasBOM()); // NEW
PrettyWriter<OutputStream, UTF8<>, AutoUTF<unsigned> > writer(eos); // CHANGED PrettyWriter<OutputStream, UTF8<>, AutoUTF<unsigned> > writer(eos); // CHANGED
#else #else
// You may also use static bound encoding type, such as output to UTF-16LE with BOM // You may also use static bound encoding type, such as output to UTF-16LE with BOM
typedef EncodedOutputStream<UTF16LE<>,FileWriteStream> OutputStream; // NEW typedef EncodedOutputStream<UTF16LE<>,FileWriteStream> OutputStream; // NEW
OutputStream eos(os, true); // NEW OutputStream eos(os, true); // NEW
PrettyWriter<OutputStream, UTF8<>, UTF16LE<> > writer(eos); // CHANGED PrettyWriter<OutputStream, UTF8<>, UTF16LE<> > writer(eos); // CHANGED
#endif #endif
// JSON reader parse from the input stream and let writer generate the output. // JSON reader parse from the input stream and let writer generate the output.
//if (!reader.Parse<kParseValidateEncodingFlag>(is, writer)) { //if (!reader.Parse<kParseValidateEncodingFlag>(is, writer)) {
if (!reader.Parse<kParseValidateEncodingFlag>(eis, writer)) { // CHANGED if (!reader.Parse<kParseValidateEncodingFlag>(eis, writer)) { // CHANGED
fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode())); fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode()));
return 1; return 1;
} }
return 0; return 0;
} }

View File

@ -1,173 +1,173 @@
// Serialize example // Serialize example
// This example shows writing JSON string with writer directly. // This example shows writing JSON string with writer directly.
#include "rapidjson/prettywriter.h" // for stringify JSON #include "rapidjson/prettywriter.h" // for stringify JSON
#include <cstdio> #include <cstdio>
#include <string> #include <string>
#include <vector> #include <vector>
using namespace rapidjson; using namespace rapidjson;
class Person { class Person {
public: public:
Person(const std::string& name, unsigned age) : name_(name), age_(age) {} Person(const std::string& name, unsigned age) : name_(name), age_(age) {}
Person(const Person& rhs) : name_(rhs.name_), age_(rhs.age_) {} Person(const Person& rhs) : name_(rhs.name_), age_(rhs.age_) {}
virtual ~Person(); virtual ~Person();
Person& operator=(const Person& rhs) { Person& operator=(const Person& rhs) {
name_ = rhs.name_; name_ = rhs.name_;
age_ = rhs.age_; age_ = rhs.age_;
return *this; return *this;
} }
protected: protected:
template <typename Writer> template <typename Writer>
void Serialize(Writer& writer) const { void Serialize(Writer& writer) const {
// This base class just write out name-value pairs, without wrapping within an object. // This base class just write out name-value pairs, without wrapping within an object.
writer.String("name"); writer.String("name");
#if RAPIDJSON_HAS_STDSTRING #if RAPIDJSON_HAS_STDSTRING
writer.String(name_); writer.String(name_);
#else #else
writer.String(name_.c_str(), static_cast<SizeType>(name_.length())); // Supplying length of string is faster. writer.String(name_.c_str(), static_cast<SizeType>(name_.length())); // Supplying length of string is faster.
#endif #endif
writer.String("age"); writer.String("age");
writer.Uint(age_); writer.Uint(age_);
} }
private: private:
std::string name_; std::string name_;
unsigned age_; unsigned age_;
}; };
Person::~Person() { Person::~Person() {
} }
class Education { class Education {
public: public:
Education(const std::string& school, double GPA) : school_(school), GPA_(GPA) {} Education(const std::string& school, double GPA) : school_(school), GPA_(GPA) {}
Education(const Education& rhs) : school_(rhs.school_), GPA_(rhs.GPA_) {} Education(const Education& rhs) : school_(rhs.school_), GPA_(rhs.GPA_) {}
template <typename Writer> template <typename Writer>
void Serialize(Writer& writer) const { void Serialize(Writer& writer) const {
writer.StartObject(); writer.StartObject();
writer.String("school"); writer.String("school");
#if RAPIDJSON_HAS_STDSTRING #if RAPIDJSON_HAS_STDSTRING
writer.String(school_); writer.String(school_);
#else #else
writer.String(school_.c_str(), static_cast<SizeType>(school_.length())); writer.String(school_.c_str(), static_cast<SizeType>(school_.length()));
#endif #endif
writer.String("GPA"); writer.String("GPA");
writer.Double(GPA_); writer.Double(GPA_);
writer.EndObject(); writer.EndObject();
} }
private: private:
std::string school_; std::string school_;
double GPA_; double GPA_;
}; };
class Dependent : public Person { class Dependent : public Person {
public: public:
Dependent(const std::string& name, unsigned age, Education* education = 0) : Person(name, age), education_(education) {} Dependent(const std::string& name, unsigned age, Education* education = 0) : Person(name, age), education_(education) {}
Dependent(const Dependent& rhs) : Person(rhs), education_(0) { education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); } Dependent(const Dependent& rhs) : Person(rhs), education_(0) { education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); }
virtual ~Dependent(); virtual ~Dependent();
Dependent& operator=(const Dependent& rhs) { Dependent& operator=(const Dependent& rhs) {
if (this == &rhs) if (this == &rhs)
return *this; return *this;
delete education_; delete education_;
education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_);
return *this; return *this;
} }
template <typename Writer> template <typename Writer>
void Serialize(Writer& writer) const { void Serialize(Writer& writer) const {
writer.StartObject(); writer.StartObject();
Person::Serialize(writer); Person::Serialize(writer);
writer.String("education"); writer.String("education");
if (education_) if (education_)
education_->Serialize(writer); education_->Serialize(writer);
else else
writer.Null(); writer.Null();
writer.EndObject(); writer.EndObject();
} }
private: private:
Education *education_; Education *education_;
}; };
Dependent::~Dependent() { Dependent::~Dependent() {
delete education_; delete education_;
} }
class Employee : public Person { class Employee : public Person {
public: public:
Employee(const std::string& name, unsigned age, bool married) : Person(name, age), dependents_(), married_(married) {} Employee(const std::string& name, unsigned age, bool married) : Person(name, age), dependents_(), married_(married) {}
Employee(const Employee& rhs) : Person(rhs), dependents_(rhs.dependents_), married_(rhs.married_) {} Employee(const Employee& rhs) : Person(rhs), dependents_(rhs.dependents_), married_(rhs.married_) {}
virtual ~Employee(); virtual ~Employee();
Employee& operator=(const Employee& rhs) { Employee& operator=(const Employee& rhs) {
static_cast<Person&>(*this) = rhs; static_cast<Person&>(*this) = rhs;
dependents_ = rhs.dependents_; dependents_ = rhs.dependents_;
married_ = rhs.married_; married_ = rhs.married_;
return *this; return *this;
} }
void AddDependent(const Dependent& dependent) { void AddDependent(const Dependent& dependent) {
dependents_.push_back(dependent); dependents_.push_back(dependent);
} }
template <typename Writer> template <typename Writer>
void Serialize(Writer& writer) const { void Serialize(Writer& writer) const {
writer.StartObject(); writer.StartObject();
Person::Serialize(writer); Person::Serialize(writer);
writer.String("married"); writer.String("married");
writer.Bool(married_); writer.Bool(married_);
writer.String(("dependents")); writer.String(("dependents"));
writer.StartArray(); writer.StartArray();
for (std::vector<Dependent>::const_iterator dependentItr = dependents_.begin(); dependentItr != dependents_.end(); ++dependentItr) for (std::vector<Dependent>::const_iterator dependentItr = dependents_.begin(); dependentItr != dependents_.end(); ++dependentItr)
dependentItr->Serialize(writer); dependentItr->Serialize(writer);
writer.EndArray(); writer.EndArray();
writer.EndObject(); writer.EndObject();
} }
private: private:
std::vector<Dependent> dependents_; std::vector<Dependent> dependents_;
bool married_; bool married_;
}; };
Employee::~Employee() { Employee::~Employee() {
} }
int main(int, char*[]) { int main(int, char*[]) {
std::vector<Employee> employees; std::vector<Employee> employees;
employees.push_back(Employee("Milo YIP", 34, true)); employees.push_back(Employee("Milo YIP", 34, true));
employees.back().AddDependent(Dependent("Lua YIP", 3, new Education("Happy Kindergarten", 3.5))); employees.back().AddDependent(Dependent("Lua YIP", 3, new Education("Happy Kindergarten", 3.5)));
employees.back().AddDependent(Dependent("Mio YIP", 1)); employees.back().AddDependent(Dependent("Mio YIP", 1));
employees.push_back(Employee("Percy TSE", 30, false)); employees.push_back(Employee("Percy TSE", 30, false));
StringBuffer sb; StringBuffer sb;
PrettyWriter<StringBuffer> writer(sb); PrettyWriter<StringBuffer> writer(sb);
writer.StartArray(); writer.StartArray();
for (std::vector<Employee>::const_iterator employeeItr = employees.begin(); employeeItr != employees.end(); ++employeeItr) for (std::vector<Employee>::const_iterator employeeItr = employees.begin(); employeeItr != employees.end(); ++employeeItr)
employeeItr->Serialize(writer); employeeItr->Serialize(writer);
writer.EndArray(); writer.EndArray();
puts(sb.GetString()); puts(sb.GetString());
return 0; return 0;
} }

View File

@ -1,151 +1,151 @@
// Hello World example // Hello World example
// This example shows basic usage of DOM-style API. // This example shows basic usage of DOM-style API.
#include "rapidjson/document.h" // rapidjson's DOM-style API #include "rapidjson/document.h" // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON #include "rapidjson/prettywriter.h" // for stringify JSON
#include <cstdio> #include <cstdio>
using namespace rapidjson; using namespace rapidjson;
using namespace std; using namespace std;
int main(int, char*[]) { int main(int, char*[]) {
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// 1. Parse a JSON text string to a document. // 1. Parse a JSON text string to a document.
const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "; const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
printf("Original JSON:\n %s\n", json); printf("Original JSON:\n %s\n", json);
Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator. Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.
#if 0 #if 0
// "normal" parsing, decode strings to new buffers. Can use other input stream via ParseStream(). // "normal" parsing, decode strings to new buffers. Can use other input stream via ParseStream().
if (document.Parse(json).HasParseError()) if (document.Parse(json).HasParseError())
return 1; return 1;
#else #else
// In-situ parsing, decode strings directly in the source string. Source must be string. // In-situ parsing, decode strings directly in the source string. Source must be string.
char buffer[sizeof(json)]; char buffer[sizeof(json)];
memcpy(buffer, json, sizeof(json)); memcpy(buffer, json, sizeof(json));
if (document.ParseInsitu(buffer).HasParseError()) if (document.ParseInsitu(buffer).HasParseError())
return 1; return 1;
#endif #endif
printf("\nParsing to document succeeded.\n"); printf("\nParsing to document succeeded.\n");
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// 2. Access values in document. // 2. Access values in document.
printf("\nAccess values in document:\n"); printf("\nAccess values in document:\n");
assert(document.IsObject()); // Document is a JSON value represents the root of DOM. Root can be either an object or array. assert(document.IsObject()); // Document is a JSON value represents the root of DOM. Root can be either an object or array.
assert(document.HasMember("hello")); assert(document.HasMember("hello"));
assert(document["hello"].IsString()); assert(document["hello"].IsString());
printf("hello = %s\n", document["hello"].GetString()); printf("hello = %s\n", document["hello"].GetString());
// Since version 0.2, you can use single lookup to check the existing of member and its value: // Since version 0.2, you can use single lookup to check the existing of member and its value:
Value::MemberIterator hello = document.FindMember("hello"); Value::MemberIterator hello = document.FindMember("hello");
assert(hello != document.MemberEnd()); assert(hello != document.MemberEnd());
assert(hello->value.IsString()); assert(hello->value.IsString());
assert(strcmp("world", hello->value.GetString()) == 0); assert(strcmp("world", hello->value.GetString()) == 0);
(void)hello; (void)hello;
assert(document["t"].IsBool()); // JSON true/false are bool. Can also uses more specific function IsTrue(). assert(document["t"].IsBool()); // JSON true/false are bool. Can also uses more specific function IsTrue().
printf("t = %s\n", document["t"].GetBool() ? "true" : "false"); printf("t = %s\n", document["t"].GetBool() ? "true" : "false");
assert(document["f"].IsBool()); assert(document["f"].IsBool());
printf("f = %s\n", document["f"].GetBool() ? "true" : "false"); printf("f = %s\n", document["f"].GetBool() ? "true" : "false");
printf("n = %s\n", document["n"].IsNull() ? "null" : "?"); printf("n = %s\n", document["n"].IsNull() ? "null" : "?");
assert(document["i"].IsNumber()); // Number is a JSON type, but C++ needs more specific type. assert(document["i"].IsNumber()); // Number is a JSON type, but C++ needs more specific type.
assert(document["i"].IsInt()); // In this case, IsUint()/IsInt64()/IsUInt64() also return true. assert(document["i"].IsInt()); // In this case, IsUint()/IsInt64()/IsUInt64() also return true.
printf("i = %d\n", document["i"].GetInt()); // Alternative (int)document["i"] printf("i = %d\n", document["i"].GetInt()); // Alternative (int)document["i"]
assert(document["pi"].IsNumber()); assert(document["pi"].IsNumber());
assert(document["pi"].IsDouble()); assert(document["pi"].IsDouble());
printf("pi = %g\n", document["pi"].GetDouble()); printf("pi = %g\n", document["pi"].GetDouble());
{ {
const Value& a = document["a"]; // Using a reference for consecutive access is handy and faster. const Value& a = document["a"]; // Using a reference for consecutive access is handy and faster.
assert(a.IsArray()); assert(a.IsArray());
for (SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size_t. for (SizeType i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size_t.
printf("a[%d] = %d\n", i, a[i].GetInt()); printf("a[%d] = %d\n", i, a[i].GetInt());
int y = a[0].GetInt(); int y = a[0].GetInt();
(void)y; (void)y;
// Iterating array with iterators // Iterating array with iterators
printf("a = "); printf("a = ");
for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr)
printf("%d ", itr->GetInt()); printf("%d ", itr->GetInt());
printf("\n"); printf("\n");
} }
// Iterating object members // Iterating object members
static const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" }; static const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" };
for (Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr) for (Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr)
printf("Type of member %s is %s\n", itr->name.GetString(), kTypeNames[itr->value.GetType()]); printf("Type of member %s is %s\n", itr->name.GetString(), kTypeNames[itr->value.GetType()]);
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// 3. Modify values in document. // 3. Modify values in document.
// Change i to a bigger number // Change i to a bigger number
{ {
uint64_t f20 = 1; // compute factorial of 20 uint64_t f20 = 1; // compute factorial of 20
for (uint64_t j = 1; j <= 20; j++) for (uint64_t j = 1; j <= 20; j++)
f20 *= j; f20 *= j;
document["i"] = f20; // Alternate form: document["i"].SetUint64(f20) document["i"] = f20; // Alternate form: document["i"].SetUint64(f20)
assert(!document["i"].IsInt()); // No longer can be cast as int or uint. assert(!document["i"].IsInt()); // No longer can be cast as int or uint.
} }
// Adding values to array. // Adding values to array.
{ {
Value& a = document["a"]; // This time we uses non-const reference. Value& a = document["a"]; // This time we uses non-const reference.
Document::AllocatorType& allocator = document.GetAllocator(); Document::AllocatorType& allocator = document.GetAllocator();
for (int i = 5; i <= 10; i++) for (int i = 5; i <= 10; i++)
a.PushBack(i, allocator); // May look a bit strange, allocator is needed for potentially realloc. We normally uses the document's. a.PushBack(i, allocator); // May look a bit strange, allocator is needed for potentially realloc. We normally uses the document's.
// Fluent API // Fluent API
a.PushBack("Lua", allocator).PushBack("Mio", allocator); a.PushBack("Lua", allocator).PushBack("Mio", allocator);
} }
// Making string values. // Making string values.
// This version of SetString() just store the pointer to the string. // This version of SetString() just store the pointer to the string.
// So it is for literal and string that exists within value's life-cycle. // So it is for literal and string that exists within value's life-cycle.
{ {
document["hello"] = "rapidjson"; // This will invoke strlen() document["hello"] = "rapidjson"; // This will invoke strlen()
// Faster version: // Faster version:
// document["hello"].SetString("rapidjson", 9); // document["hello"].SetString("rapidjson", 9);
} }
// This version of SetString() needs an allocator, which means it will allocate a new buffer and copy the the string into the buffer. // This version of SetString() needs an allocator, which means it will allocate a new buffer and copy the the string into the buffer.
Value author; Value author;
{ {
char buffer2[10]; char buffer2[10];
int len = sprintf(buffer2, "%s %s", "Milo", "Yip"); // synthetic example of dynamically created string. int len = sprintf(buffer2, "%s %s", "Milo", "Yip"); // synthetic example of dynamically created string.
author.SetString(buffer2, static_cast<SizeType>(len), document.GetAllocator()); author.SetString(buffer2, static_cast<SizeType>(len), document.GetAllocator());
// Shorter but slower version: // Shorter but slower version:
// document["hello"].SetString(buffer, document.GetAllocator()); // document["hello"].SetString(buffer, document.GetAllocator());
// Constructor version: // Constructor version:
// Value author(buffer, len, document.GetAllocator()); // Value author(buffer, len, document.GetAllocator());
// Value author(buffer, document.GetAllocator()); // Value author(buffer, document.GetAllocator());
memset(buffer2, 0, sizeof(buffer2)); // For demonstration purpose. memset(buffer2, 0, sizeof(buffer2)); // For demonstration purpose.
} }
// Variable 'buffer' is unusable now but 'author' has already made a copy. // Variable 'buffer' is unusable now but 'author' has already made a copy.
document.AddMember("author", author, document.GetAllocator()); document.AddMember("author", author, document.GetAllocator());
assert(author.IsNull()); // Move semantic for assignment. After this variable is assigned as a member, the variable becomes null. assert(author.IsNull()); // Move semantic for assignment. After this variable is assigned as a member, the variable becomes null.
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// 4. Stringify JSON // 4. Stringify JSON
printf("\nModified JSON with reformatting:\n"); printf("\nModified JSON with reformatting:\n");
StringBuffer sb; StringBuffer sb;
PrettyWriter<StringBuffer> writer(sb); PrettyWriter<StringBuffer> writer(sb);
document.Accept(writer); // Accept() traverses the DOM and generates Handler events. document.Accept(writer); // Accept() traverses the DOM and generates Handler events.
puts(sb.GetString()); puts(sb.GetString());
return 0; return 0;
} }

View File

@ -1,263 +1,263 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_ALLOCATORS_H_ #ifndef RAPIDJSON_ALLOCATORS_H_
#define RAPIDJSON_ALLOCATORS_H_ #define RAPIDJSON_ALLOCATORS_H_
#include "rapidjson.h" #include "rapidjson.h"
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Allocator // Allocator
/*! \class rapidjson::Allocator /*! \class rapidjson::Allocator
\brief Concept for allocating, resizing and freeing memory block. \brief Concept for allocating, resizing and freeing memory block.
Note that Malloc() and Realloc() are non-static but Free() is static. Note that Malloc() and Realloc() are non-static but Free() is static.
So if an allocator need to support Free(), it needs to put its pointer in So if an allocator need to support Free(), it needs to put its pointer in
the header of memory block. the header of memory block.
\code \code
concept Allocator { concept Allocator {
static const bool kNeedFree; //!< Whether this allocator needs to call Free(). static const bool kNeedFree; //!< Whether this allocator needs to call Free().
// Allocate a memory block. // Allocate a memory block.
// \param size of the memory block in bytes. // \param size of the memory block in bytes.
// \returns pointer to the memory block. // \returns pointer to the memory block.
void* Malloc(size_t size); void* Malloc(size_t size);
// Resize a memory block. // Resize a memory block.
// \param originalPtr The pointer to current memory block. Null pointer is permitted. // \param originalPtr The pointer to current memory block. Null pointer is permitted.
// \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.) // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.)
// \param newSize the new size in bytes. // \param newSize the new size in bytes.
void* Realloc(void* originalPtr, size_t originalSize, size_t newSize); void* Realloc(void* originalPtr, size_t originalSize, size_t newSize);
// Free a memory block. // Free a memory block.
// \param pointer to the memory block. Null pointer is permitted. // \param pointer to the memory block. Null pointer is permitted.
static void Free(void *ptr); static void Free(void *ptr);
}; };
\endcode \endcode
*/ */
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// CrtAllocator // CrtAllocator
//! C-runtime library allocator. //! C-runtime library allocator.
/*! This class is just wrapper for standard C library memory routines. /*! This class is just wrapper for standard C library memory routines.
\note implements Allocator concept \note implements Allocator concept
*/ */
class CrtAllocator { class CrtAllocator {
public: public:
static const bool kNeedFree = true; static const bool kNeedFree = true;
void* Malloc(size_t size) { void* Malloc(size_t size) {
if (size) // behavior of malloc(0) is implementation defined. if (size) // behavior of malloc(0) is implementation defined.
return std::malloc(size); return std::malloc(size);
else else
return NULL; // standardize to returning NULL. return NULL; // standardize to returning NULL.
} }
void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
(void)originalSize; (void)originalSize;
if (newSize == 0) { if (newSize == 0) {
std::free(originalPtr); std::free(originalPtr);
return NULL; return NULL;
} }
return std::realloc(originalPtr, newSize); return std::realloc(originalPtr, newSize);
} }
static void Free(void *ptr) { std::free(ptr); } static void Free(void *ptr) { std::free(ptr); }
}; };
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// MemoryPoolAllocator // MemoryPoolAllocator
//! Default memory allocator used by the parser and DOM. //! Default memory allocator used by the parser and DOM.
/*! This allocator allocate memory blocks from pre-allocated memory chunks. /*! This allocator allocate memory blocks from pre-allocated memory chunks.
It does not free memory blocks. And Realloc() only allocate new memory. It does not free memory blocks. And Realloc() only allocate new memory.
The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default. The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default.
User may also supply a buffer as the first chunk. User may also supply a buffer as the first chunk.
If the user-buffer is full then additional chunks are allocated by BaseAllocator. If the user-buffer is full then additional chunks are allocated by BaseAllocator.
The user-buffer is not deallocated by this allocator. The user-buffer is not deallocated by this allocator.
\tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator. \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator.
\note implements Allocator concept \note implements Allocator concept
*/ */
template <typename BaseAllocator = CrtAllocator> template <typename BaseAllocator = CrtAllocator>
class MemoryPoolAllocator { class MemoryPoolAllocator {
public: public:
static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator) static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator)
//! Constructor with chunkSize. //! Constructor with chunkSize.
/*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize.
\param baseAllocator The allocator for allocating memory chunks. \param baseAllocator The allocator for allocating memory chunks.
*/ */
MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0) chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
{ {
} }
//! Constructor with user-supplied buffer. //! Constructor with user-supplied buffer.
/*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size. /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size.
The user buffer will not be deallocated when this allocator is destructed. The user buffer will not be deallocated when this allocator is destructed.
\param buffer User supplied buffer. \param buffer User supplied buffer.
\param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader). \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader).
\param chunkSize The size of memory chunk. The default is kDefaultChunkSize. \param chunkSize The size of memory chunk. The default is kDefaultChunkSize.
\param baseAllocator The allocator for allocating memory chunks. \param baseAllocator The allocator for allocating memory chunks.
*/ */
MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0) chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
{ {
RAPIDJSON_ASSERT(buffer != 0); RAPIDJSON_ASSERT(buffer != 0);
RAPIDJSON_ASSERT(size > sizeof(ChunkHeader)); RAPIDJSON_ASSERT(size > sizeof(ChunkHeader));
chunkHead_ = reinterpret_cast<ChunkHeader*>(buffer); chunkHead_ = reinterpret_cast<ChunkHeader*>(buffer);
chunkHead_->capacity = size - sizeof(ChunkHeader); chunkHead_->capacity = size - sizeof(ChunkHeader);
chunkHead_->size = 0; chunkHead_->size = 0;
chunkHead_->next = 0; chunkHead_->next = 0;
} }
//! Destructor. //! Destructor.
/*! This deallocates all memory chunks, excluding the user-supplied buffer. /*! This deallocates all memory chunks, excluding the user-supplied buffer.
*/ */
~MemoryPoolAllocator() { ~MemoryPoolAllocator() {
Clear(); Clear();
RAPIDJSON_DELETE(ownBaseAllocator_); RAPIDJSON_DELETE(ownBaseAllocator_);
} }
//! Deallocates all memory chunks, excluding the user-supplied buffer. //! Deallocates all memory chunks, excluding the user-supplied buffer.
void Clear() { void Clear() {
while (chunkHead_ && chunkHead_ != userBuffer_) { while (chunkHead_ && chunkHead_ != userBuffer_) {
ChunkHeader* next = chunkHead_->next; ChunkHeader* next = chunkHead_->next;
baseAllocator_->Free(chunkHead_); baseAllocator_->Free(chunkHead_);
chunkHead_ = next; chunkHead_ = next;
} }
if (chunkHead_ && chunkHead_ == userBuffer_) if (chunkHead_ && chunkHead_ == userBuffer_)
chunkHead_->size = 0; // Clear user buffer chunkHead_->size = 0; // Clear user buffer
} }
//! Computes the total capacity of allocated memory chunks. //! Computes the total capacity of allocated memory chunks.
/*! \return total capacity in bytes. /*! \return total capacity in bytes.
*/ */
size_t Capacity() const { size_t Capacity() const {
size_t capacity = 0; size_t capacity = 0;
for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
capacity += c->capacity; capacity += c->capacity;
return capacity; return capacity;
} }
//! Computes the memory blocks allocated. //! Computes the memory blocks allocated.
/*! \return total used bytes. /*! \return total used bytes.
*/ */
size_t Size() const { size_t Size() const {
size_t size = 0; size_t size = 0;
for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
size += c->size; size += c->size;
return size; return size;
} }
//! Allocates a memory block. (concept Allocator) //! Allocates a memory block. (concept Allocator)
void* Malloc(size_t size) { void* Malloc(size_t size) {
if (!size) if (!size)
return NULL; return NULL;
size = RAPIDJSON_ALIGN(size); size = RAPIDJSON_ALIGN(size);
if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity) if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size); AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
void *buffer = reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size; void *buffer = reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size;
chunkHead_->size += size; chunkHead_->size += size;
return buffer; return buffer;
} }
//! Resizes a memory block (concept Allocator) //! Resizes a memory block (concept Allocator)
void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
if (originalPtr == 0) if (originalPtr == 0)
return Malloc(newSize); return Malloc(newSize);
if (newSize == 0) if (newSize == 0)
return NULL; return NULL;
originalSize = RAPIDJSON_ALIGN(originalSize); originalSize = RAPIDJSON_ALIGN(originalSize);
newSize = RAPIDJSON_ALIGN(newSize); newSize = RAPIDJSON_ALIGN(newSize);
// Do not shrink if new size is smaller than original // Do not shrink if new size is smaller than original
if (originalSize >= newSize) if (originalSize >= newSize)
return originalPtr; return originalPtr;
// Simply expand it if it is the last allocation and there is sufficient space // Simply expand it if it is the last allocation and there is sufficient space
if (originalPtr == reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) { if (originalPtr == reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) {
size_t increment = static_cast<size_t>(newSize - originalSize); size_t increment = static_cast<size_t>(newSize - originalSize);
if (chunkHead_->size + increment <= chunkHead_->capacity) { if (chunkHead_->size + increment <= chunkHead_->capacity) {
chunkHead_->size += increment; chunkHead_->size += increment;
return originalPtr; return originalPtr;
} }
} }
// Realloc process: allocate and copy memory, do not free original buffer. // Realloc process: allocate and copy memory, do not free original buffer.
void* newBuffer = Malloc(newSize); void* newBuffer = Malloc(newSize);
RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly. RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly.
if (originalSize) if (originalSize)
std::memcpy(newBuffer, originalPtr, originalSize); std::memcpy(newBuffer, originalPtr, originalSize);
return newBuffer; return newBuffer;
} }
//! Frees a memory block (concept Allocator) //! Frees a memory block (concept Allocator)
static void Free(void *ptr) { (void)ptr; } // Do nothing static void Free(void *ptr) { (void)ptr; } // Do nothing
private: private:
//! Copy constructor is not permitted. //! Copy constructor is not permitted.
MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */; MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */;
//! Copy assignment operator is not permitted. //! Copy assignment operator is not permitted.
MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */; MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */;
//! Creates a new chunk. //! Creates a new chunk.
/*! \param capacity Capacity of the chunk in bytes. /*! \param capacity Capacity of the chunk in bytes.
*/ */
void AddChunk(size_t capacity) { void AddChunk(size_t capacity) {
if (!baseAllocator_) if (!baseAllocator_)
ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator()); ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator());
ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity)); ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity));
chunk->capacity = capacity; chunk->capacity = capacity;
chunk->size = 0; chunk->size = 0;
chunk->next = chunkHead_; chunk->next = chunkHead_;
chunkHead_ = chunk; chunkHead_ = chunk;
} }
static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity. static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity.
//! Chunk header for perpending to each chunk. //! Chunk header for perpending to each chunk.
/*! Chunks are stored as a singly linked list. /*! Chunks are stored as a singly linked list.
*/ */
struct ChunkHeader { struct ChunkHeader {
size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself). size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself).
size_t size; //!< Current size of allocated memory in bytes. size_t size; //!< Current size of allocated memory in bytes.
ChunkHeader *next; //!< Next chunk in the linked list. ChunkHeader *next; //!< Next chunk in the linked list.
}; };
ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation. ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation.
size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated. size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated.
void *userBuffer_; //!< User supplied buffer. void *userBuffer_; //!< User supplied buffer.
BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks. BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks.
BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object. BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object.
}; };
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
#endif // RAPIDJSON_ENCODINGS_H_ #endif // RAPIDJSON_ENCODINGS_H_

View File

@ -1,295 +1,295 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_ENCODEDSTREAM_H_ #ifndef RAPIDJSON_ENCODEDSTREAM_H_
#define RAPIDJSON_ENCODEDSTREAM_H_ #define RAPIDJSON_ENCODEDSTREAM_H_
#include "stream.h" #include "stream.h"
#include "memorystream.h" #include "memorystream.h"
#ifdef __GNUC__ #ifdef __GNUC__
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++) RAPIDJSON_DIAG_OFF(effc++)
#endif #endif
#ifdef __clang__ #ifdef __clang__
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(padded) RAPIDJSON_DIAG_OFF(padded)
#endif #endif
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
//! Input byte stream wrapper with a statically bound encoding. //! Input byte stream wrapper with a statically bound encoding.
/*! /*!
\tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE.
\tparam InputByteStream Type of input byte stream. For example, FileReadStream. \tparam InputByteStream Type of input byte stream. For example, FileReadStream.
*/ */
template <typename Encoding, typename InputByteStream> template <typename Encoding, typename InputByteStream>
class EncodedInputStream { class EncodedInputStream {
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
public: public:
typedef typename Encoding::Ch Ch; typedef typename Encoding::Ch Ch;
EncodedInputStream(InputByteStream& is) : is_(is) { EncodedInputStream(InputByteStream& is) : is_(is) {
current_ = Encoding::TakeBOM(is_); current_ = Encoding::TakeBOM(is_);
} }
Ch Peek() const { return current_; } Ch Peek() const { return current_; }
Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; } Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; }
size_t Tell() const { return is_.Tell(); } size_t Tell() const { return is_.Tell(); }
// Not implemented // Not implemented
void Put(Ch) { RAPIDJSON_ASSERT(false); } void Put(Ch) { RAPIDJSON_ASSERT(false); }
void Flush() { RAPIDJSON_ASSERT(false); } void Flush() { RAPIDJSON_ASSERT(false); }
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
private: private:
EncodedInputStream(const EncodedInputStream&); EncodedInputStream(const EncodedInputStream&);
EncodedInputStream& operator=(const EncodedInputStream&); EncodedInputStream& operator=(const EncodedInputStream&);
InputByteStream& is_; InputByteStream& is_;
Ch current_; Ch current_;
}; };
//! Specialized for UTF8 MemoryStream. //! Specialized for UTF8 MemoryStream.
template <> template <>
class EncodedInputStream<UTF8<>, MemoryStream> { class EncodedInputStream<UTF8<>, MemoryStream> {
public: public:
typedef UTF8<>::Ch Ch; typedef UTF8<>::Ch Ch;
EncodedInputStream(MemoryStream& is) : is_(is) { EncodedInputStream(MemoryStream& is) : is_(is) {
if (static_cast<unsigned char>(is_.Peek()) == 0xEFu) is_.Take(); if (static_cast<unsigned char>(is_.Peek()) == 0xEFu) is_.Take();
if (static_cast<unsigned char>(is_.Peek()) == 0xBBu) is_.Take(); if (static_cast<unsigned char>(is_.Peek()) == 0xBBu) is_.Take();
if (static_cast<unsigned char>(is_.Peek()) == 0xBFu) is_.Take(); if (static_cast<unsigned char>(is_.Peek()) == 0xBFu) is_.Take();
} }
Ch Peek() const { return is_.Peek(); } Ch Peek() const { return is_.Peek(); }
Ch Take() { return is_.Take(); } Ch Take() { return is_.Take(); }
size_t Tell() const { return is_.Tell(); } size_t Tell() const { return is_.Tell(); }
// Not implemented // Not implemented
void Put(Ch) {} void Put(Ch) {}
void Flush() {} void Flush() {}
Ch* PutBegin() { return 0; } Ch* PutBegin() { return 0; }
size_t PutEnd(Ch*) { return 0; } size_t PutEnd(Ch*) { return 0; }
MemoryStream& is_; MemoryStream& is_;
}; };
//! Output byte stream wrapper with statically bound encoding. //! Output byte stream wrapper with statically bound encoding.
/*! /*!
\tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE.
\tparam OutputByteStream Type of input byte stream. For example, FileWriteStream. \tparam OutputByteStream Type of input byte stream. For example, FileWriteStream.
*/ */
template <typename Encoding, typename OutputByteStream> template <typename Encoding, typename OutputByteStream>
class EncodedOutputStream { class EncodedOutputStream {
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
public: public:
typedef typename Encoding::Ch Ch; typedef typename Encoding::Ch Ch;
EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) { EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) {
if (putBOM) if (putBOM)
Encoding::PutBOM(os_); Encoding::PutBOM(os_);
} }
void Put(Ch c) { Encoding::Put(os_, c); } void Put(Ch c) { Encoding::Put(os_, c); }
void Flush() { os_.Flush(); } void Flush() { os_.Flush(); }
// Not implemented // Not implemented
Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;} Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;}
Ch Take() { RAPIDJSON_ASSERT(false); return 0;} Ch Take() { RAPIDJSON_ASSERT(false); return 0;}
size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
private: private:
EncodedOutputStream(const EncodedOutputStream&); EncodedOutputStream(const EncodedOutputStream&);
EncodedOutputStream& operator=(const EncodedOutputStream&); EncodedOutputStream& operator=(const EncodedOutputStream&);
OutputByteStream& os_; OutputByteStream& os_;
}; };
#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x
//! Input stream wrapper with dynamically bound encoding and automatic encoding detection. //! Input stream wrapper with dynamically bound encoding and automatic encoding detection.
/*! /*!
\tparam CharType Type of character for reading. \tparam CharType Type of character for reading.
\tparam InputByteStream type of input byte stream to be wrapped. \tparam InputByteStream type of input byte stream to be wrapped.
*/ */
template <typename CharType, typename InputByteStream> template <typename CharType, typename InputByteStream>
class AutoUTFInputStream { class AutoUTFInputStream {
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
public: public:
typedef CharType Ch; typedef CharType Ch;
//! Constructor. //! Constructor.
/*! /*!
\param is input stream to be wrapped. \param is input stream to be wrapped.
\param type UTF encoding type if it is not detected from the stream. \param type UTF encoding type if it is not detected from the stream.
*/ */
AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) { AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) {
RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE);
DetectType(); DetectType();
static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) }; static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) };
takeFunc_ = f[type_]; takeFunc_ = f[type_];
current_ = takeFunc_(*is_); current_ = takeFunc_(*is_);
} }
UTFType GetType() const { return type_; } UTFType GetType() const { return type_; }
bool HasBOM() const { return hasBOM_; } bool HasBOM() const { return hasBOM_; }
Ch Peek() const { return current_; } Ch Peek() const { return current_; }
Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; } Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; }
size_t Tell() const { return is_->Tell(); } size_t Tell() const { return is_->Tell(); }
// Not implemented // Not implemented
void Put(Ch) { RAPIDJSON_ASSERT(false); } void Put(Ch) { RAPIDJSON_ASSERT(false); }
void Flush() { RAPIDJSON_ASSERT(false); } void Flush() { RAPIDJSON_ASSERT(false); }
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
private: private:
AutoUTFInputStream(const AutoUTFInputStream&); AutoUTFInputStream(const AutoUTFInputStream&);
AutoUTFInputStream& operator=(const AutoUTFInputStream&); AutoUTFInputStream& operator=(const AutoUTFInputStream&);
// Detect encoding type with BOM or RFC 4627 // Detect encoding type with BOM or RFC 4627
void DetectType() { void DetectType() {
// BOM (Byte Order Mark): // BOM (Byte Order Mark):
// 00 00 FE FF UTF-32BE // 00 00 FE FF UTF-32BE
// FF FE 00 00 UTF-32LE // FF FE 00 00 UTF-32LE
// FE FF UTF-16BE // FE FF UTF-16BE
// FF FE UTF-16LE // FF FE UTF-16LE
// EF BB BF UTF-8 // EF BB BF UTF-8
const unsigned char* c = reinterpret_cast<const unsigned char *>(is_->Peek4()); const unsigned char* c = reinterpret_cast<const unsigned char *>(is_->Peek4());
if (!c) if (!c)
return; return;
unsigned bom = static_cast<unsigned>(c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24)); unsigned bom = static_cast<unsigned>(c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24));
hasBOM_ = false; hasBOM_ = false;
if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take(); } else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take(); }
else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take(); } else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take(); }
else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); } else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); }
// RFC 4627: Section 3 // RFC 4627: Section 3
// "Since the first two characters of a JSON text will always be ASCII // "Since the first two characters of a JSON text will always be ASCII
// characters [RFC0020], it is possible to determine whether an octet // characters [RFC0020], it is possible to determine whether an octet
// stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking // stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking
// at the pattern of nulls in the first four octets." // at the pattern of nulls in the first four octets."
// 00 00 00 xx UTF-32BE // 00 00 00 xx UTF-32BE
// 00 xx 00 xx UTF-16BE // 00 xx 00 xx UTF-16BE
// xx 00 00 00 UTF-32LE // xx 00 00 00 UTF-32LE
// xx 00 xx 00 UTF-16LE // xx 00 xx 00 UTF-16LE
// xx xx xx xx UTF-8 // xx xx xx xx UTF-8
if (!hasBOM_) { if (!hasBOM_) {
unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0); unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
switch (pattern) { switch (pattern) {
case 0x08: type_ = kUTF32BE; break; case 0x08: type_ = kUTF32BE; break;
case 0x0A: type_ = kUTF16BE; break; case 0x0A: type_ = kUTF16BE; break;
case 0x01: type_ = kUTF32LE; break; case 0x01: type_ = kUTF32LE; break;
case 0x05: type_ = kUTF16LE; break; case 0x05: type_ = kUTF16LE; break;
case 0x0F: type_ = kUTF8; break; case 0x0F: type_ = kUTF8; break;
default: break; // Use type defined by user. default: break; // Use type defined by user.
} }
} }
// Runtime check whether the size of character type is sufficient. It only perform checks with assertion. // Runtime check whether the size of character type is sufficient. It only perform checks with assertion.
if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2);
if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4);
} }
typedef Ch (*TakeFunc)(InputByteStream& is); typedef Ch (*TakeFunc)(InputByteStream& is);
InputByteStream* is_; InputByteStream* is_;
UTFType type_; UTFType type_;
Ch current_; Ch current_;
TakeFunc takeFunc_; TakeFunc takeFunc_;
bool hasBOM_; bool hasBOM_;
}; };
//! Output stream wrapper with dynamically bound encoding and automatic encoding detection. //! Output stream wrapper with dynamically bound encoding and automatic encoding detection.
/*! /*!
\tparam CharType Type of character for writing. \tparam CharType Type of character for writing.
\tparam OutputByteStream type of output byte stream to be wrapped. \tparam OutputByteStream type of output byte stream to be wrapped.
*/ */
template <typename CharType, typename OutputByteStream> template <typename CharType, typename OutputByteStream>
class AutoUTFOutputStream { class AutoUTFOutputStream {
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
public: public:
typedef CharType Ch; typedef CharType Ch;
//! Constructor. //! Constructor.
/*! /*!
\param os output stream to be wrapped. \param os output stream to be wrapped.
\param type UTF encoding type. \param type UTF encoding type.
\param putBOM Whether to write BOM at the beginning of the stream. \param putBOM Whether to write BOM at the beginning of the stream.
*/ */
AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) { AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) {
RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE);
// Runtime check whether the size of character type is sufficient. It only perform checks with assertion. // Runtime check whether the size of character type is sufficient. It only perform checks with assertion.
if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2);
if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4);
static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) }; static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) };
putFunc_ = f[type_]; putFunc_ = f[type_];
if (putBOM) if (putBOM)
PutBOM(); PutBOM();
} }
UTFType GetType() const { return type_; } UTFType GetType() const { return type_; }
void Put(Ch c) { putFunc_(*os_, c); } void Put(Ch c) { putFunc_(*os_, c); }
void Flush() { os_->Flush(); } void Flush() { os_->Flush(); }
// Not implemented // Not implemented
Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;} Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;}
Ch Take() { RAPIDJSON_ASSERT(false); return 0;} Ch Take() { RAPIDJSON_ASSERT(false); return 0;}
size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
private: private:
AutoUTFOutputStream(const AutoUTFOutputStream&); AutoUTFOutputStream(const AutoUTFOutputStream&);
AutoUTFOutputStream& operator=(const AutoUTFOutputStream&); AutoUTFOutputStream& operator=(const AutoUTFOutputStream&);
void PutBOM() { void PutBOM() {
typedef void (*PutBOMFunc)(OutputByteStream&); typedef void (*PutBOMFunc)(OutputByteStream&);
static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) }; static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) };
f[type_](*os_); f[type_](*os_);
} }
typedef void (*PutFunc)(OutputByteStream&, Ch); typedef void (*PutFunc)(OutputByteStream&, Ch);
OutputByteStream* os_; OutputByteStream* os_;
UTFType type_; UTFType type_;
PutFunc putFunc_; PutFunc putFunc_;
}; };
#undef RAPIDJSON_ENCODINGS_FUNC #undef RAPIDJSON_ENCODINGS_FUNC
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
#ifdef __clang__ #ifdef __clang__
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
#ifdef __GNUC__ #ifdef __GNUC__
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
#endif // RAPIDJSON_FILESTREAM_H_ #endif // RAPIDJSON_FILESTREAM_H_

File diff suppressed because it is too large Load Diff

View File

@ -1,99 +1,99 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_FILEREADSTREAM_H_ #ifndef RAPIDJSON_FILEREADSTREAM_H_
#define RAPIDJSON_FILEREADSTREAM_H_ #define RAPIDJSON_FILEREADSTREAM_H_
#include "stream.h" #include "stream.h"
#include <cstdio> #include <cstdio>
#ifdef __clang__ #ifdef __clang__
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(padded) RAPIDJSON_DIAG_OFF(padded)
RAPIDJSON_DIAG_OFF(unreachable-code) RAPIDJSON_DIAG_OFF(unreachable-code)
RAPIDJSON_DIAG_OFF(missing-noreturn) RAPIDJSON_DIAG_OFF(missing-noreturn)
#endif #endif
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
//! File byte stream for input using fread(). //! File byte stream for input using fread().
/*! /*!
\note implements Stream concept \note implements Stream concept
*/ */
class FileReadStream { class FileReadStream {
public: public:
typedef char Ch; //!< Character type (byte). typedef char Ch; //!< Character type (byte).
//! Constructor. //! Constructor.
/*! /*!
\param fp File pointer opened for read. \param fp File pointer opened for read.
\param buffer user-supplied buffer. \param buffer user-supplied buffer.
\param bufferSize size of buffer in bytes. Must >=4 bytes. \param bufferSize size of buffer in bytes. Must >=4 bytes.
*/ */
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) { 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) {
RAPIDJSON_ASSERT(fp_ != 0); RAPIDJSON_ASSERT(fp_ != 0);
RAPIDJSON_ASSERT(bufferSize >= 4); RAPIDJSON_ASSERT(bufferSize >= 4);
Read(); Read();
} }
Ch Peek() const { return *current_; } Ch Peek() const { return *current_; }
Ch Take() { Ch c = *current_; Read(); return c; } Ch Take() { Ch c = *current_; Read(); return c; }
size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); } size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
// Not implemented // Not implemented
void Put(Ch) { RAPIDJSON_ASSERT(false); } void Put(Ch) { RAPIDJSON_ASSERT(false); }
void Flush() { RAPIDJSON_ASSERT(false); } void Flush() { RAPIDJSON_ASSERT(false); }
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
// For encoding detection only. // For encoding detection only.
const Ch* Peek4() const { const Ch* Peek4() const {
return (current_ + 4 <= bufferLast_) ? current_ : 0; return (current_ + 4 <= bufferLast_) ? current_ : 0;
} }
private: private:
void Read() { void Read() {
if (current_ < bufferLast_) if (current_ < bufferLast_)
++current_; ++current_;
else if (!eof_) { else if (!eof_) {
count_ += readCount_; count_ += readCount_;
readCount_ = fread(buffer_, 1, bufferSize_, fp_); readCount_ = fread(buffer_, 1, bufferSize_, fp_);
bufferLast_ = buffer_ + readCount_ - 1; bufferLast_ = buffer_ + readCount_ - 1;
current_ = buffer_; current_ = buffer_;
if (readCount_ < bufferSize_) { if (readCount_ < bufferSize_) {
buffer_[readCount_] = '\0'; buffer_[readCount_] = '\0';
++bufferLast_; ++bufferLast_;
eof_ = true; eof_ = true;
} }
} }
} }
std::FILE* fp_; std::FILE* fp_;
Ch *buffer_; Ch *buffer_;
size_t bufferSize_; size_t bufferSize_;
Ch *bufferLast_; Ch *bufferLast_;
Ch *current_; Ch *current_;
size_t readCount_; size_t readCount_;
size_t count_; //!< Number of characters read size_t count_; //!< Number of characters read
bool eof_; bool eof_;
}; };
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
#ifdef __clang__ #ifdef __clang__
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
#endif // RAPIDJSON_FILESTREAM_H_ #endif // RAPIDJSON_FILESTREAM_H_

View File

@ -1,104 +1,104 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_FILEWRITESTREAM_H_ #ifndef RAPIDJSON_FILEWRITESTREAM_H_
#define RAPIDJSON_FILEWRITESTREAM_H_ #define RAPIDJSON_FILEWRITESTREAM_H_
#include "stream.h" #include "stream.h"
#include <cstdio> #include <cstdio>
#ifdef __clang__ #ifdef __clang__
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(unreachable-code) RAPIDJSON_DIAG_OFF(unreachable-code)
#endif #endif
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
//! Wrapper of C file stream for input using fread(). //! Wrapper of C file stream for input using fread().
/*! /*!
\note implements Stream concept \note implements Stream concept
*/ */
class FileWriteStream { class FileWriteStream {
public: public:
typedef char Ch; //!< Character type. Only support char. typedef char Ch; //!< Character type. Only support char.
FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) {
RAPIDJSON_ASSERT(fp_ != 0); RAPIDJSON_ASSERT(fp_ != 0);
} }
void Put(char c) { void Put(char c) {
if (current_ >= bufferEnd_) if (current_ >= bufferEnd_)
Flush(); Flush();
*current_++ = c; *current_++ = c;
} }
void PutN(char c, size_t n) { void PutN(char c, size_t n) {
size_t avail = static_cast<size_t>(bufferEnd_ - current_); size_t avail = static_cast<size_t>(bufferEnd_ - current_);
while (n > avail) { while (n > avail) {
std::memset(current_, c, avail); std::memset(current_, c, avail);
current_ += avail; current_ += avail;
Flush(); Flush();
n -= avail; n -= avail;
avail = static_cast<size_t>(bufferEnd_ - current_); avail = static_cast<size_t>(bufferEnd_ - current_);
} }
if (n > 0) { if (n > 0) {
std::memset(current_, c, n); std::memset(current_, c, n);
current_ += n; current_ += n;
} }
} }
void Flush() { void Flush() {
if (current_ != buffer_) { if (current_ != buffer_) {
size_t result = fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_); size_t result = fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
if (result < static_cast<size_t>(current_ - buffer_)) { if (result < static_cast<size_t>(current_ - buffer_)) {
// failure deliberately ignored at this time // failure deliberately ignored at this time
// added to avoid warn_unused_result build errors // added to avoid warn_unused_result build errors
} }
current_ = buffer_; current_ = buffer_;
} }
} }
// Not implemented // Not implemented
char Peek() const { RAPIDJSON_ASSERT(false); return 0; } char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
char Take() { RAPIDJSON_ASSERT(false); return 0; } char Take() { RAPIDJSON_ASSERT(false); return 0; }
size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
private: private:
// Prohibit copy constructor & assignment operator. // Prohibit copy constructor & assignment operator.
FileWriteStream(const FileWriteStream&); FileWriteStream(const FileWriteStream&);
FileWriteStream& operator=(const FileWriteStream&); FileWriteStream& operator=(const FileWriteStream&);
std::FILE* fp_; std::FILE* fp_;
char *buffer_; char *buffer_;
char *bufferEnd_; char *bufferEnd_;
char *current_; char *current_;
}; };
//! Implement specialized version of PutN() with memset() for better performance. //! Implement specialized version of PutN() with memset() for better performance.
template<> template<>
inline void PutN(FileWriteStream& stream, char c, size_t n) { inline void PutN(FileWriteStream& stream, char c, size_t n) {
stream.PutN(c, n); stream.PutN(c, n);
} }
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
#ifdef __clang__ #ifdef __clang__
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
#endif // RAPIDJSON_FILESTREAM_H_ #endif // RAPIDJSON_FILESTREAM_H_

View File

@ -1,181 +1,181 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_INTERNAL_META_H_ #ifndef RAPIDJSON_INTERNAL_META_H_
#define RAPIDJSON_INTERNAL_META_H_ #define RAPIDJSON_INTERNAL_META_H_
#include "../rapidjson.h" #include "../rapidjson.h"
#ifdef __GNUC__ #ifdef __GNUC__
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++) RAPIDJSON_DIAG_OFF(effc++)
#endif #endif
#if defined(_MSC_VER) #if defined(_MSC_VER)
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(6334) RAPIDJSON_DIAG_OFF(6334)
#endif #endif
#if RAPIDJSON_HAS_CXX11_TYPETRAITS #if RAPIDJSON_HAS_CXX11_TYPETRAITS
#include <type_traits> #include <type_traits>
#endif #endif
//@cond RAPIDJSON_INTERNAL //@cond RAPIDJSON_INTERNAL
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
namespace internal { namespace internal {
// Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching // Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching
template <typename T> struct Void { typedef void Type; }; template <typename T> struct Void { typedef void Type; };
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// BoolType, TrueType, FalseType // BoolType, TrueType, FalseType
// //
template <bool Cond> struct BoolType { template <bool Cond> struct BoolType {
static const bool Value = Cond; static const bool Value = Cond;
typedef BoolType Type; typedef BoolType Type;
}; };
typedef BoolType<true> TrueType; typedef BoolType<true> TrueType;
typedef BoolType<false> FalseType; typedef BoolType<false> FalseType;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr // SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr
// //
template <bool C> struct SelectIfImpl { template <typename T1, typename T2> struct Apply { typedef T1 Type; }; }; template <bool C> struct SelectIfImpl { template <typename T1, typename T2> struct Apply { typedef T1 Type; }; };
template <> struct SelectIfImpl<false> { template <typename T1, typename T2> struct Apply { typedef T2 Type; }; }; template <> struct SelectIfImpl<false> { template <typename T1, typename T2> struct Apply { typedef T2 Type; }; };
template <bool C, typename T1, typename T2> struct SelectIfCond : SelectIfImpl<C>::template Apply<T1,T2> {}; template <bool C, typename T1, typename T2> struct SelectIfCond : SelectIfImpl<C>::template Apply<T1,T2> {};
template <typename C, typename T1, typename T2> struct SelectIf : SelectIfCond<C::Value, T1, T2> {}; template <typename C, typename T1, typename T2> struct SelectIf : SelectIfCond<C::Value, T1, T2> {};
template <bool Cond1, bool Cond2> struct AndExprCond : FalseType {}; template <bool Cond1, bool Cond2> struct AndExprCond : FalseType {};
template <> struct AndExprCond<true, true> : TrueType {}; template <> struct AndExprCond<true, true> : TrueType {};
template <bool Cond1, bool Cond2> struct OrExprCond : TrueType {}; template <bool Cond1, bool Cond2> struct OrExprCond : TrueType {};
template <> struct OrExprCond<false, false> : FalseType {}; template <> struct OrExprCond<false, false> : FalseType {};
template <typename C> struct BoolExpr : SelectIf<C,TrueType,FalseType>::Type {}; template <typename C> struct BoolExpr : SelectIf<C,TrueType,FalseType>::Type {};
template <typename C> struct NotExpr : SelectIf<C,FalseType,TrueType>::Type {}; template <typename C> struct NotExpr : SelectIf<C,FalseType,TrueType>::Type {};
template <typename C1, typename C2> struct AndExpr : AndExprCond<C1::Value, C2::Value>::Type {}; template <typename C1, typename C2> struct AndExpr : AndExprCond<C1::Value, C2::Value>::Type {};
template <typename C1, typename C2> struct OrExpr : OrExprCond<C1::Value, C2::Value>::Type {}; template <typename C1, typename C2> struct OrExpr : OrExprCond<C1::Value, C2::Value>::Type {};
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// AddConst, MaybeAddConst, RemoveConst // AddConst, MaybeAddConst, RemoveConst
template <typename T> struct AddConst { typedef const T Type; }; template <typename T> struct AddConst { typedef const T Type; };
template <bool Constify, typename T> struct MaybeAddConst : SelectIfCond<Constify, const T, T> {}; template <bool Constify, typename T> struct MaybeAddConst : SelectIfCond<Constify, const T, T> {};
template <typename T> struct RemoveConst { typedef T Type; }; template <typename T> struct RemoveConst { typedef T Type; };
template <typename T> struct RemoveConst<const T> { typedef T Type; }; template <typename T> struct RemoveConst<const T> { typedef T Type; };
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// IsSame, IsConst, IsMoreConst, IsPointer // IsSame, IsConst, IsMoreConst, IsPointer
// //
template <typename T, typename U> struct IsSame : FalseType {}; template <typename T, typename U> struct IsSame : FalseType {};
template <typename T> struct IsSame<T, T> : TrueType {}; template <typename T> struct IsSame<T, T> : TrueType {};
template <typename T> struct IsConst : FalseType {}; template <typename T> struct IsConst : FalseType {};
template <typename T> struct IsConst<const T> : TrueType {}; template <typename T> struct IsConst<const T> : TrueType {};
template <typename CT, typename T> template <typename CT, typename T>
struct IsMoreConst struct IsMoreConst
: AndExpr<IsSame<typename RemoveConst<CT>::Type, typename RemoveConst<T>::Type>, : AndExpr<IsSame<typename RemoveConst<CT>::Type, typename RemoveConst<T>::Type>,
BoolType<IsConst<CT>::Value >= IsConst<T>::Value> >::Type {}; BoolType<IsConst<CT>::Value >= IsConst<T>::Value> >::Type {};
template <typename T> struct IsPointer : FalseType {}; template <typename T> struct IsPointer : FalseType {};
template <typename T> struct IsPointer<T*> : TrueType {}; template <typename T> struct IsPointer<T*> : TrueType {};
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// IsBaseOf // IsBaseOf
// //
#if RAPIDJSON_HAS_CXX11_TYPETRAITS #if RAPIDJSON_HAS_CXX11_TYPETRAITS
template <typename B, typename D> struct IsBaseOf template <typename B, typename D> struct IsBaseOf
: BoolType< ::std::is_base_of<B,D>::value> {}; : BoolType< ::std::is_base_of<B,D>::value> {};
#else // simplified version adopted from Boost #else // simplified version adopted from Boost
template<typename B, typename D> struct IsBaseOfImpl { template<typename B, typename D> struct IsBaseOfImpl {
RAPIDJSON_STATIC_ASSERT(sizeof(B) != 0); RAPIDJSON_STATIC_ASSERT(sizeof(B) != 0);
RAPIDJSON_STATIC_ASSERT(sizeof(D) != 0); RAPIDJSON_STATIC_ASSERT(sizeof(D) != 0);
typedef char (&Yes)[1]; typedef char (&Yes)[1];
typedef char (&No) [2]; typedef char (&No) [2];
template <typename T> template <typename T>
static Yes Check(const D*, T); static Yes Check(const D*, T);
static No Check(const B*, int); static No Check(const B*, int);
struct Host { struct Host {
operator const B*() const; operator const B*() const;
operator const D*(); operator const D*();
}; };
enum { Value = (sizeof(Check(Host(), 0)) == sizeof(Yes)) }; enum { Value = (sizeof(Check(Host(), 0)) == sizeof(Yes)) };
}; };
template <typename B, typename D> struct IsBaseOf template <typename B, typename D> struct IsBaseOf
: OrExpr<IsSame<B, D>, BoolExpr<IsBaseOfImpl<B, D> > >::Type {}; : OrExpr<IsSame<B, D>, BoolExpr<IsBaseOfImpl<B, D> > >::Type {};
#endif // RAPIDJSON_HAS_CXX11_TYPETRAITS #endif // RAPIDJSON_HAS_CXX11_TYPETRAITS
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// EnableIf / DisableIf // EnableIf / DisableIf
// //
template <bool Condition, typename T = void> struct EnableIfCond { typedef T Type; }; template <bool Condition, typename T = void> struct EnableIfCond { typedef T Type; };
template <typename T> struct EnableIfCond<false, T> { /* empty */ }; template <typename T> struct EnableIfCond<false, T> { /* empty */ };
template <bool Condition, typename T = void> struct DisableIfCond { typedef T Type; }; template <bool Condition, typename T = void> struct DisableIfCond { typedef T Type; };
template <typename T> struct DisableIfCond<true, T> { /* empty */ }; template <typename T> struct DisableIfCond<true, T> { /* empty */ };
template <typename Condition, typename T = void> template <typename Condition, typename T = void>
struct EnableIf : EnableIfCond<Condition::Value, T> {}; struct EnableIf : EnableIfCond<Condition::Value, T> {};
template <typename Condition, typename T = void> template <typename Condition, typename T = void>
struct DisableIf : DisableIfCond<Condition::Value, T> {}; struct DisableIf : DisableIfCond<Condition::Value, T> {};
// SFINAE helpers // SFINAE helpers
struct SfinaeTag {}; struct SfinaeTag {};
template <typename T> struct RemoveSfinaeTag; template <typename T> struct RemoveSfinaeTag;
template <typename T> struct RemoveSfinaeTag<SfinaeTag&(*)(T)> { typedef T Type; }; template <typename T> struct RemoveSfinaeTag<SfinaeTag&(*)(T)> { typedef T Type; };
#define RAPIDJSON_REMOVEFPTR_(type) \ #define RAPIDJSON_REMOVEFPTR_(type) \
typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag \ typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag \
< ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*) type>::Type < ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*) type>::Type
#define RAPIDJSON_ENABLEIF(cond) \ #define RAPIDJSON_ENABLEIF(cond) \
typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \
<RAPIDJSON_REMOVEFPTR_(cond)>::Type * = NULL <RAPIDJSON_REMOVEFPTR_(cond)>::Type * = NULL
#define RAPIDJSON_DISABLEIF(cond) \ #define RAPIDJSON_DISABLEIF(cond) \
typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \
<RAPIDJSON_REMOVEFPTR_(cond)>::Type * = NULL <RAPIDJSON_REMOVEFPTR_(cond)>::Type * = NULL
#define RAPIDJSON_ENABLEIF_RETURN(cond,returntype) \ #define RAPIDJSON_ENABLEIF_RETURN(cond,returntype) \
typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \
<RAPIDJSON_REMOVEFPTR_(cond), \ <RAPIDJSON_REMOVEFPTR_(cond), \
RAPIDJSON_REMOVEFPTR_(returntype)>::Type RAPIDJSON_REMOVEFPTR_(returntype)>::Type
#define RAPIDJSON_DISABLEIF_RETURN(cond,returntype) \ #define RAPIDJSON_DISABLEIF_RETURN(cond,returntype) \
typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \
<RAPIDJSON_REMOVEFPTR_(cond), \ <RAPIDJSON_REMOVEFPTR_(cond), \
RAPIDJSON_REMOVEFPTR_(returntype)>::Type RAPIDJSON_REMOVEFPTR_(returntype)>::Type
} // namespace internal } // namespace internal
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
//@endcond //@endcond
#if defined(__GNUC__) || defined(_MSC_VER) #if defined(__GNUC__) || defined(_MSC_VER)
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
#endif // RAPIDJSON_INTERNAL_META_H_ #endif // RAPIDJSON_INTERNAL_META_H_

View File

@ -1,55 +1,55 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_POW10_ #ifndef RAPIDJSON_POW10_
#define RAPIDJSON_POW10_ #define RAPIDJSON_POW10_
#include "../rapidjson.h" #include "../rapidjson.h"
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
namespace internal { namespace internal {
//! Computes integer powers of 10 in double (10.0^n). //! Computes integer powers of 10 in double (10.0^n).
/*! This function uses lookup table for fast and accurate results. /*! This function uses lookup table for fast and accurate results.
\param n non-negative exponent. Must <= 308. \param n non-negative exponent. Must <= 308.
\return 10.0^n \return 10.0^n
*/ */
inline double Pow10(int n) { inline double Pow10(int n) {
static const double e[] = { // 1e-0...1e308: 309 * 8 bytes = 2472 bytes static const double e[] = { // 1e-0...1e308: 309 * 8 bytes = 2472 bytes
1e+0, 1e+0,
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, 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,
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, 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,
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, 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,
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, 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,
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, 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,
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, 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,
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, 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,
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, 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,
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, 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,
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, 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,
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, 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,
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, 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,
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, 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,
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, 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,
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, 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,
1e+301,1e+302,1e+303,1e+304,1e+305,1e+306,1e+307,1e+308 1e+301,1e+302,1e+303,1e+304,1e+305,1e+306,1e+307,1e+308
}; };
RAPIDJSON_ASSERT(n >= 0 && n <= 308); RAPIDJSON_ASSERT(n >= 0 && n <= 308);
return e[n]; return e[n];
} }
} // namespace internal } // namespace internal
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
#endif // RAPIDJSON_POW10_ #endif // RAPIDJSON_POW10_

File diff suppressed because it is too large Load Diff

View File

@ -1,230 +1,230 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_INTERNAL_STACK_H_ #ifndef RAPIDJSON_INTERNAL_STACK_H_
#define RAPIDJSON_INTERNAL_STACK_H_ #define RAPIDJSON_INTERNAL_STACK_H_
#include "../allocators.h" #include "../allocators.h"
#include "swap.h" #include "swap.h"
#if defined(__clang__) #if defined(__clang__)
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat) RAPIDJSON_DIAG_OFF(c++98-compat)
#endif #endif
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
namespace internal { namespace internal {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Stack // Stack
//! A type-unsafe stack for storing different types of data. //! A type-unsafe stack for storing different types of data.
/*! \tparam Allocator Allocator for allocating stack memory. /*! \tparam Allocator Allocator for allocating stack memory.
*/ */
template <typename Allocator> template <typename Allocator>
class Stack { class Stack {
public: public:
// Optimization note: Do not allocate memory for stack_ in constructor. // Optimization note: Do not allocate memory for stack_ in constructor.
// Do it lazily when first Push() -> Expand() -> Resize(). // Do it lazily when first Push() -> Expand() -> Resize().
Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) { Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
} }
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
Stack(Stack&& rhs) Stack(Stack&& rhs)
: allocator_(rhs.allocator_), : allocator_(rhs.allocator_),
ownAllocator_(rhs.ownAllocator_), ownAllocator_(rhs.ownAllocator_),
stack_(rhs.stack_), stack_(rhs.stack_),
stackTop_(rhs.stackTop_), stackTop_(rhs.stackTop_),
stackEnd_(rhs.stackEnd_), stackEnd_(rhs.stackEnd_),
initialCapacity_(rhs.initialCapacity_) initialCapacity_(rhs.initialCapacity_)
{ {
rhs.allocator_ = 0; rhs.allocator_ = 0;
rhs.ownAllocator_ = 0; rhs.ownAllocator_ = 0;
rhs.stack_ = 0; rhs.stack_ = 0;
rhs.stackTop_ = 0; rhs.stackTop_ = 0;
rhs.stackEnd_ = 0; rhs.stackEnd_ = 0;
rhs.initialCapacity_ = 0; rhs.initialCapacity_ = 0;
} }
#endif #endif
~Stack() { ~Stack() {
Destroy(); Destroy();
} }
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
Stack& operator=(Stack&& rhs) { Stack& operator=(Stack&& rhs) {
if (&rhs != this) if (&rhs != this)
{ {
Destroy(); Destroy();
allocator_ = rhs.allocator_; allocator_ = rhs.allocator_;
ownAllocator_ = rhs.ownAllocator_; ownAllocator_ = rhs.ownAllocator_;
stack_ = rhs.stack_; stack_ = rhs.stack_;
stackTop_ = rhs.stackTop_; stackTop_ = rhs.stackTop_;
stackEnd_ = rhs.stackEnd_; stackEnd_ = rhs.stackEnd_;
initialCapacity_ = rhs.initialCapacity_; initialCapacity_ = rhs.initialCapacity_;
rhs.allocator_ = 0; rhs.allocator_ = 0;
rhs.ownAllocator_ = 0; rhs.ownAllocator_ = 0;
rhs.stack_ = 0; rhs.stack_ = 0;
rhs.stackTop_ = 0; rhs.stackTop_ = 0;
rhs.stackEnd_ = 0; rhs.stackEnd_ = 0;
rhs.initialCapacity_ = 0; rhs.initialCapacity_ = 0;
} }
return *this; return *this;
} }
#endif #endif
void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT { void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT {
internal::Swap(allocator_, rhs.allocator_); internal::Swap(allocator_, rhs.allocator_);
internal::Swap(ownAllocator_, rhs.ownAllocator_); internal::Swap(ownAllocator_, rhs.ownAllocator_);
internal::Swap(stack_, rhs.stack_); internal::Swap(stack_, rhs.stack_);
internal::Swap(stackTop_, rhs.stackTop_); internal::Swap(stackTop_, rhs.stackTop_);
internal::Swap(stackEnd_, rhs.stackEnd_); internal::Swap(stackEnd_, rhs.stackEnd_);
internal::Swap(initialCapacity_, rhs.initialCapacity_); internal::Swap(initialCapacity_, rhs.initialCapacity_);
} }
void Clear() { stackTop_ = stack_; } void Clear() { stackTop_ = stack_; }
void ShrinkToFit() { void ShrinkToFit() {
if (Empty()) { if (Empty()) {
// If the stack is empty, completely deallocate the memory. // If the stack is empty, completely deallocate the memory.
Allocator::Free(stack_); Allocator::Free(stack_);
stack_ = 0; stack_ = 0;
stackTop_ = 0; stackTop_ = 0;
stackEnd_ = 0; stackEnd_ = 0;
} }
else else
Resize(GetSize()); Resize(GetSize());
} }
// Optimization note: try to minimize the size of this function for force inline. // Optimization note: try to minimize the size of this function for force inline.
// Expansion is run very infrequently, so it is moved to another (probably non-inline) function. // Expansion is run very infrequently, so it is moved to another (probably non-inline) function.
template<typename T> template<typename T>
RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) { RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) {
// Expand the stack if needed // Expand the stack if needed
if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count > stackEnd_)) if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count > stackEnd_))
Expand<T>(count); Expand<T>(count);
} }
template<typename T> template<typename T>
RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) { RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) {
Reserve<T>(count); Reserve<T>(count);
return PushUnsafe<T>(count); return PushUnsafe<T>(count);
} }
template<typename T> template<typename T>
RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) { RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) {
RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_); RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_);
T* ret = reinterpret_cast<T*>(stackTop_); T* ret = reinterpret_cast<T*>(stackTop_);
stackTop_ += sizeof(T) * count; stackTop_ += sizeof(T) * count;
return ret; return ret;
} }
template<typename T> template<typename T>
T* Pop(size_t count) { T* Pop(size_t count) {
RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T)); RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T));
stackTop_ -= count * sizeof(T); stackTop_ -= count * sizeof(T);
return reinterpret_cast<T*>(stackTop_); return reinterpret_cast<T*>(stackTop_);
} }
template<typename T> template<typename T>
T* Top() { T* Top() {
RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
return reinterpret_cast<T*>(stackTop_ - sizeof(T)); return reinterpret_cast<T*>(stackTop_ - sizeof(T));
} }
template<typename T> template<typename T>
const T* Top() const { const T* Top() const {
RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
return reinterpret_cast<T*>(stackTop_ - sizeof(T)); return reinterpret_cast<T*>(stackTop_ - sizeof(T));
} }
template<typename T> template<typename T>
T* End() { return reinterpret_cast<T*>(stackTop_); } T* End() { return reinterpret_cast<T*>(stackTop_); }
template<typename T> template<typename T>
const T* End() const { return reinterpret_cast<T*>(stackTop_); } const T* End() const { return reinterpret_cast<T*>(stackTop_); }
template<typename T> template<typename T>
T* Bottom() { return reinterpret_cast<T*>(stack_); } T* Bottom() { return reinterpret_cast<T*>(stack_); }
template<typename T> template<typename T>
const T* Bottom() const { return reinterpret_cast<T*>(stack_); } const T* Bottom() const { return reinterpret_cast<T*>(stack_); }
bool HasAllocator() const { bool HasAllocator() const {
return allocator_ != 0; return allocator_ != 0;
} }
Allocator& GetAllocator() { Allocator& GetAllocator() {
RAPIDJSON_ASSERT(allocator_); RAPIDJSON_ASSERT(allocator_);
return *allocator_; return *allocator_;
} }
bool Empty() const { return stackTop_ == stack_; } bool Empty() const { return stackTop_ == stack_; }
size_t GetSize() const { return static_cast<size_t>(stackTop_ - stack_); } size_t GetSize() const { return static_cast<size_t>(stackTop_ - stack_); }
size_t GetCapacity() const { return static_cast<size_t>(stackEnd_ - stack_); } size_t GetCapacity() const { return static_cast<size_t>(stackEnd_ - stack_); }
private: private:
template<typename T> template<typename T>
void Expand(size_t count) { void Expand(size_t count) {
// Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity. // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity.
size_t newCapacity; size_t newCapacity;
if (stack_ == 0) { if (stack_ == 0) {
if (!allocator_) if (!allocator_)
ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
newCapacity = initialCapacity_; newCapacity = initialCapacity_;
} else { } else {
newCapacity = GetCapacity(); newCapacity = GetCapacity();
newCapacity += (newCapacity + 1) / 2; newCapacity += (newCapacity + 1) / 2;
} }
size_t newSize = GetSize() + sizeof(T) * count; size_t newSize = GetSize() + sizeof(T) * count;
if (newCapacity < newSize) if (newCapacity < newSize)
newCapacity = newSize; newCapacity = newSize;
Resize(newCapacity); Resize(newCapacity);
} }
void Resize(size_t newCapacity) { void Resize(size_t newCapacity) {
const size_t size = GetSize(); // Backup the current size const size_t size = GetSize(); // Backup the current size
stack_ = static_cast<char*>(allocator_->Realloc(stack_, GetCapacity(), newCapacity)); stack_ = static_cast<char*>(allocator_->Realloc(stack_, GetCapacity(), newCapacity));
stackTop_ = stack_ + size; stackTop_ = stack_ + size;
stackEnd_ = stack_ + newCapacity; stackEnd_ = stack_ + newCapacity;
} }
void Destroy() { void Destroy() {
Allocator::Free(stack_); Allocator::Free(stack_);
RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack
} }
// Prohibit copy constructor & assignment operator. // Prohibit copy constructor & assignment operator.
Stack(const Stack&); Stack(const Stack&);
Stack& operator=(const Stack&); Stack& operator=(const Stack&);
Allocator* allocator_; Allocator* allocator_;
Allocator* ownAllocator_; Allocator* ownAllocator_;
char *stack_; char *stack_;
char *stackTop_; char *stackTop_;
char *stackEnd_; char *stackEnd_;
size_t initialCapacity_; size_t initialCapacity_;
}; };
} // namespace internal } // namespace internal
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
#if defined(__clang__) #if defined(__clang__)
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
#endif // RAPIDJSON_STACK_H_ #endif // RAPIDJSON_STACK_H_

View File

@ -1,55 +1,55 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_INTERNAL_STRFUNC_H_ #ifndef RAPIDJSON_INTERNAL_STRFUNC_H_
#define RAPIDJSON_INTERNAL_STRFUNC_H_ #define RAPIDJSON_INTERNAL_STRFUNC_H_
#include "../stream.h" #include "../stream.h"
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
namespace internal { namespace internal {
//! Custom strlen() which works on different character types. //! Custom strlen() which works on different character types.
/*! \tparam Ch Character type (e.g. char, wchar_t, short) /*! \tparam Ch Character type (e.g. char, wchar_t, short)
\param s Null-terminated input string. \param s Null-terminated input string.
\return Number of characters in the string. \return Number of characters in the string.
\note This has the same semantics as strlen(), the return value is not number of Unicode codepoints. \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints.
*/ */
template <typename Ch> template <typename Ch>
inline SizeType StrLen(const Ch* s) { inline SizeType StrLen(const Ch* s) {
const Ch* p = s; const Ch* p = s;
while (*p) ++p; while (*p) ++p;
return SizeType(p - s); return SizeType(p - s);
} }
//! Returns number of code points in a encoded string. //! Returns number of code points in a encoded string.
template<typename Encoding> template<typename Encoding>
bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) { bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
GenericStringStream<Encoding> is(s); GenericStringStream<Encoding> is(s);
const typename Encoding::Ch* end = s + length; const typename Encoding::Ch* end = s + length;
SizeType count = 0; SizeType count = 0;
while (is.src_ < end) { while (is.src_ < end) {
unsigned codepoint; unsigned codepoint;
if (!Encoding::Decode(is, &codepoint)) if (!Encoding::Decode(is, &codepoint))
return false; return false;
count++; count++;
} }
*outCount = count; *outCount = count;
return true; return true;
} }
} // namespace internal } // namespace internal
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
#endif // RAPIDJSON_INTERNAL_STRFUNC_H_ #endif // RAPIDJSON_INTERNAL_STRFUNC_H_

View File

@ -1,46 +1,46 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_INTERNAL_SWAP_H_ #ifndef RAPIDJSON_INTERNAL_SWAP_H_
#define RAPIDJSON_INTERNAL_SWAP_H_ #define RAPIDJSON_INTERNAL_SWAP_H_
#include "../rapidjson.h" #include "../rapidjson.h"
#if defined(__clang__) #if defined(__clang__)
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat) RAPIDJSON_DIAG_OFF(c++98-compat)
#endif #endif
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
namespace internal { namespace internal {
//! Custom swap() to avoid dependency on C++ <algorithm> header //! Custom swap() to avoid dependency on C++ <algorithm> header
/*! \tparam T Type of the arguments to swap, should be instantiated with primitive C++ types only. /*! \tparam T Type of the arguments to swap, should be instantiated with primitive C++ types only.
\note This has the same semantics as std::swap(). \note This has the same semantics as std::swap().
*/ */
template <typename T> template <typename T>
inline void Swap(T& a, T& b) RAPIDJSON_NOEXCEPT { inline void Swap(T& a, T& b) RAPIDJSON_NOEXCEPT {
T tmp = a; T tmp = a;
a = b; a = b;
b = tmp; b = tmp;
} }
} // namespace internal } // namespace internal
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
#if defined(__clang__) #if defined(__clang__)
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
#endif // RAPIDJSON_INTERNAL_SWAP_H_ #endif // RAPIDJSON_INTERNAL_SWAP_H_

File diff suppressed because it is too large Load Diff

View File

@ -1,117 +1,117 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef RAPIDJSON_STRINGBUFFER_H_ #ifndef RAPIDJSON_STRINGBUFFER_H_
#define RAPIDJSON_STRINGBUFFER_H_ #define RAPIDJSON_STRINGBUFFER_H_
#include "stream.h" #include "stream.h"
#include "internal/stack.h" #include "internal/stack.h"
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
#include <utility> // std::move #include <utility> // std::move
#endif #endif
#include "internal/stack.h" #include "internal/stack.h"
#if defined(__clang__) #if defined(__clang__)
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat) RAPIDJSON_DIAG_OFF(c++98-compat)
#endif #endif
RAPIDJSON_NAMESPACE_BEGIN RAPIDJSON_NAMESPACE_BEGIN
//! Represents an in-memory output stream. //! Represents an in-memory output stream.
/*! /*!
\tparam Encoding Encoding of the stream. \tparam Encoding Encoding of the stream.
\tparam Allocator type for allocating memory buffer. \tparam Allocator type for allocating memory buffer.
\note implements Stream concept \note implements Stream concept
*/ */
template <typename Encoding, typename Allocator = CrtAllocator> template <typename Encoding, typename Allocator = CrtAllocator>
class GenericStringBuffer { class GenericStringBuffer {
public: public:
typedef typename Encoding::Ch Ch; typedef typename Encoding::Ch Ch;
GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {} GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {}
GenericStringBuffer& operator=(GenericStringBuffer&& rhs) { GenericStringBuffer& operator=(GenericStringBuffer&& rhs) {
if (&rhs != this) if (&rhs != this)
stack_ = std::move(rhs.stack_); stack_ = std::move(rhs.stack_);
return *this; return *this;
} }
#endif #endif
void Put(Ch c) { *stack_.template Push<Ch>() = c; } void Put(Ch c) { *stack_.template Push<Ch>() = c; }
void PutUnsafe(Ch c) { *stack_.template PushUnsafe<Ch>() = c; } void PutUnsafe(Ch c) { *stack_.template PushUnsafe<Ch>() = c; }
void Flush() {} void Flush() {}
void Clear() { stack_.Clear(); } void Clear() { stack_.Clear(); }
void ShrinkToFit() { void ShrinkToFit() {
// Push and pop a null terminator. This is safe. // Push and pop a null terminator. This is safe.
*stack_.template Push<Ch>() = '\0'; *stack_.template Push<Ch>() = '\0';
stack_.ShrinkToFit(); stack_.ShrinkToFit();
stack_.template Pop<Ch>(1); stack_.template Pop<Ch>(1);
} }
void Reserve(size_t count) { stack_.template Reserve<Ch>(count); } void Reserve(size_t count) { stack_.template Reserve<Ch>(count); }
Ch* Push(size_t count) { return stack_.template Push<Ch>(count); } Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
Ch* PushUnsafe(size_t count) { return stack_.template PushUnsafe<Ch>(count); } Ch* PushUnsafe(size_t count) { return stack_.template PushUnsafe<Ch>(count); }
void Pop(size_t count) { stack_.template Pop<Ch>(count); } void Pop(size_t count) { stack_.template Pop<Ch>(count); }
const Ch* GetString() const { const Ch* GetString() const {
// Push and pop a null terminator. This is safe. // Push and pop a null terminator. This is safe.
*stack_.template Push<Ch>() = '\0'; *stack_.template Push<Ch>() = '\0';
stack_.template Pop<Ch>(1); stack_.template Pop<Ch>(1);
return stack_.template Bottom<Ch>(); return stack_.template Bottom<Ch>();
} }
size_t GetSize() const { return stack_.GetSize(); } size_t GetSize() const { return stack_.GetSize(); }
static const size_t kDefaultCapacity = 256; static const size_t kDefaultCapacity = 256;
mutable internal::Stack<Allocator> stack_; mutable internal::Stack<Allocator> stack_;
private: private:
// Prohibit copy constructor & assignment operator. // Prohibit copy constructor & assignment operator.
GenericStringBuffer(const GenericStringBuffer&); GenericStringBuffer(const GenericStringBuffer&);
GenericStringBuffer& operator=(const GenericStringBuffer&); GenericStringBuffer& operator=(const GenericStringBuffer&);
}; };
//! String buffer with UTF8 encoding //! String buffer with UTF8 encoding
typedef GenericStringBuffer<UTF8<> > StringBuffer; typedef GenericStringBuffer<UTF8<> > StringBuffer;
template<typename Encoding, typename Allocator> template<typename Encoding, typename Allocator>
inline void PutReserve(GenericStringBuffer<Encoding, Allocator>& stream, size_t count) { inline void PutReserve(GenericStringBuffer<Encoding, Allocator>& stream, size_t count) {
stream.Reserve(count); stream.Reserve(count);
} }
template<typename Encoding, typename Allocator> template<typename Encoding, typename Allocator>
inline void PutUnsafe(GenericStringBuffer<Encoding, Allocator>& stream, typename Encoding::Ch c) { inline void PutUnsafe(GenericStringBuffer<Encoding, Allocator>& stream, typename Encoding::Ch c) {
stream.PutUnsafe(c); stream.PutUnsafe(c);
} }
//! Implement specialized version of PutN() with memset() for better performance. //! Implement specialized version of PutN() with memset() for better performance.
template<> template<>
inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) { inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {
std::memset(stream.stack_.Push<char>(n), c, n * sizeof(c)); std::memset(stream.stack_.Push<char>(n), c, n * sizeof(c));
} }
RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_END
#if defined(__clang__) #if defined(__clang__)
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
#endif // RAPIDJSON_STRINGBUFFER_H_ #endif // RAPIDJSON_STRINGBUFFER_H_

View File

@ -1,57 +1,57 @@
Tencent is pleased to support the open source community by making RapidJSON available. Tencent is pleased to support the open source community by making RapidJSON available.
Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
If you have downloaded a copy of the RapidJSON binary from Tencent, please note that the RapidJSON binary is licensed under the MIT License. If you have downloaded a copy of the RapidJSON binary from Tencent, please note that the RapidJSON binary is licensed under the MIT License.
If you have downloaded a copy of the RapidJSON source code from Tencent, please note that RapidJSON source code is licensed under the MIT License, except for the third-party components listed below which are subject to different license terms. Your integration of RapidJSON into your own projects may require compliance with the MIT License, as well as the other licenses applicable to the third-party components included within RapidJSON. To avoid the problematic JSON license in your own projects, it's sufficient to exclude the bin/jsonchecker/ directory, as it's the only code under the JSON license. If you have downloaded a copy of the RapidJSON source code from Tencent, please note that RapidJSON source code is licensed under the MIT License, except for the third-party components listed below which are subject to different license terms. Your integration of RapidJSON into your own projects may require compliance with the MIT License, as well as the other licenses applicable to the third-party components included within RapidJSON. To avoid the problematic JSON license in your own projects, it's sufficient to exclude the bin/jsonchecker/ directory, as it's the only code under the JSON license.
A copy of the MIT License is included in this file. A copy of the MIT License is included in this file.
Other dependencies and licenses: Other dependencies and licenses:
Open Source Software Licensed Under the BSD License: Open Source Software Licensed Under the BSD License:
-------------------------------------------------------------------- --------------------------------------------------------------------
The msinttypes r29 The msinttypes r29
Copyright (c) 2006-2013 Alexander Chemeris Copyright (c) 2006-2013 Alexander Chemeris
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. * Neither the name of copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Open Source Software Licensed Under the JSON License: Open Source Software Licensed Under the JSON License:
-------------------------------------------------------------------- --------------------------------------------------------------------
json.org json.org
Copyright (c) 2002 JSON.org Copyright (c) 2002 JSON.org
All Rights Reserved. All Rights Reserved.
JSON_checker JSON_checker
Copyright (c) 2002 JSON.org Copyright (c) 2002 JSON.org
All Rights Reserved. All Rights Reserved.
Terms of the JSON License: Terms of the JSON License:
--------------------------------------------------- ---------------------------------------------------
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The Software shall be used for Good, not Evil. The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Terms of the MIT License: Terms of the MIT License:
-------------------------------------------------------------------- --------------------------------------------------------------------
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

File diff suppressed because it is too large Load Diff

View File

@ -1,24 +1,24 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "perftest.h" #include "perftest.h"
int main(int argc, char **argv) { int main(int argc, char **argv) {
#if _MSC_VER #if _MSC_VER
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
//void *testWhetherMemoryLeakDetectionWorks = malloc(1); //void *testWhetherMemoryLeakDetectionWorks = malloc(1);
#endif #endif
::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
} }

View File

@ -1,182 +1,182 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef PERFTEST_H_ #ifndef PERFTEST_H_
#define PERFTEST_H_ #define PERFTEST_H_
#define TEST_RAPIDJSON 1 #define TEST_RAPIDJSON 1
#define TEST_PLATFORM 0 #define TEST_PLATFORM 0
#define TEST_MISC 0 #define TEST_MISC 0
#define TEST_VERSION_CODE(x,y,z) \ #define TEST_VERSION_CODE(x,y,z) \
(((x)*100000) + ((y)*100) + (z)) (((x)*100000) + ((y)*100) + (z))
// __SSE2__ and __SSE4_2__ are recognized by gcc, clang, and the Intel compiler. // __SSE2__ and __SSE4_2__ are recognized by gcc, clang, and the Intel compiler.
// We use -march=native with gmake to enable -msse2 and -msse4.2, if supported. // We use -march=native with gmake to enable -msse2 and -msse4.2, if supported.
#if defined(__SSE4_2__) #if defined(__SSE4_2__)
# define RAPIDJSON_SSE42 # define RAPIDJSON_SSE42
#elif defined(__SSE2__) #elif defined(__SSE2__)
# define RAPIDJSON_SSE2 # define RAPIDJSON_SSE2
#endif #endif
#define RAPIDJSON_HAS_STDSTRING 1 #define RAPIDJSON_HAS_STDSTRING 1
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Google Test // Google Test
#ifdef __cplusplus #ifdef __cplusplus
// gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS. // gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
#ifndef __STDC_CONSTANT_MACROS #ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS 1 // required by C++ standard # define __STDC_CONSTANT_MACROS 1 // required by C++ standard
#endif #endif
#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
#if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) #if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#pragma GCC diagnostic push #pragma GCC diagnostic push
#endif #endif
#pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Weffc++"
#endif #endif
#include "gtest/gtest.h" #include "gtest/gtest.h"
#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
#define _CRTDBG_MAP_ALLOC #define _CRTDBG_MAP_ALLOC
#include <crtdbg.h> #include <crtdbg.h>
#pragma warning(disable : 4996) // 'function': was declared deprecated #pragma warning(disable : 4996) // 'function': was declared deprecated
#endif #endif
//! Base class for all performance tests //! Base class for all performance tests
class PerfTest : public ::testing::Test { class PerfTest : public ::testing::Test {
public: public:
PerfTest() : filename_(), json_(), length_(), whitespace_(), whitespace_length_() {} PerfTest() : filename_(), json_(), length_(), whitespace_(), whitespace_length_() {}
virtual void SetUp() { virtual void SetUp() {
{ {
const char *paths[] = { const char *paths[] = {
"data/sample.json", "data/sample.json",
"bin/data/sample.json", "bin/data/sample.json",
"../bin/data/sample.json", "../bin/data/sample.json",
"../../bin/data/sample.json", "../../bin/data/sample.json",
"../../../bin/data/sample.json" "../../../bin/data/sample.json"
}; };
FILE *fp = 0; FILE *fp = 0;
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
fp = fopen(filename_ = paths[i], "rb"); fp = fopen(filename_ = paths[i], "rb");
if (fp) if (fp)
break; break;
} }
ASSERT_TRUE(fp != 0); ASSERT_TRUE(fp != 0);
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
length_ = (size_t)ftell(fp); length_ = (size_t)ftell(fp);
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
json_ = (char*)malloc(length_ + 1); json_ = (char*)malloc(length_ + 1);
ASSERT_EQ(length_, fread(json_, 1, length_, fp)); ASSERT_EQ(length_, fread(json_, 1, length_, fp));
json_[length_] = '\0'; json_[length_] = '\0';
fclose(fp); fclose(fp);
} }
// whitespace test // whitespace test
{ {
whitespace_length_ = 1024 * 1024; whitespace_length_ = 1024 * 1024;
whitespace_ = (char *)malloc(whitespace_length_ + 4); whitespace_ = (char *)malloc(whitespace_length_ + 4);
char *p = whitespace_; char *p = whitespace_;
for (size_t i = 0; i < whitespace_length_; i += 4) { for (size_t i = 0; i < whitespace_length_; i += 4) {
*p++ = ' '; *p++ = ' ';
*p++ = '\n'; *p++ = '\n';
*p++ = '\r'; *p++ = '\r';
*p++ = '\t'; *p++ = '\t';
} }
*p++ = '['; *p++ = '[';
*p++ = '0'; *p++ = '0';
*p++ = ']'; *p++ = ']';
*p++ = '\0'; *p++ = '\0';
} }
// types test // types test
{ {
const char *typespaths[] = { const char *typespaths[] = {
"data/types", "data/types",
"bin/types", "bin/types",
"../bin/types", "../bin/types",
"../../bin/types/", "../../bin/types/",
"../../../bin/types" "../../../bin/types"
}; };
const char* typesfilenames[] = { const char* typesfilenames[] = {
"booleans.json", "booleans.json",
"floats.json", "floats.json",
"guids.json", "guids.json",
"integers.json", "integers.json",
"mixed.json", "mixed.json",
"nulls.json", "nulls.json",
"paragraphs.json" "paragraphs.json"
}; };
for (size_t j = 0; j < sizeof(typesfilenames) / sizeof(typesfilenames[0]); j++) { for (size_t j = 0; j < sizeof(typesfilenames) / sizeof(typesfilenames[0]); j++) {
types_[j] = 0; types_[j] = 0;
for (size_t i = 0; i < sizeof(typespaths) / sizeof(typespaths[0]); i++) { for (size_t i = 0; i < sizeof(typespaths) / sizeof(typespaths[0]); i++) {
char filename[256]; char filename[256];
sprintf(filename, "%s/%s", typespaths[i], typesfilenames[j]); sprintf(filename, "%s/%s", typespaths[i], typesfilenames[j]);
if (FILE* fp = fopen(filename, "rb")) { if (FILE* fp = fopen(filename, "rb")) {
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
typesLength_[j] = (size_t)ftell(fp); typesLength_[j] = (size_t)ftell(fp);
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
types_[j] = (char*)malloc(typesLength_[j] + 1); types_[j] = (char*)malloc(typesLength_[j] + 1);
ASSERT_EQ(typesLength_[j], fread(types_[j], 1, typesLength_[j], fp)); ASSERT_EQ(typesLength_[j], fread(types_[j], 1, typesLength_[j], fp));
types_[j][typesLength_[j]] = '\0'; types_[j][typesLength_[j]] = '\0';
fclose(fp); fclose(fp);
break; break;
} }
} }
} }
} }
} }
virtual void TearDown() { virtual void TearDown() {
free(json_); free(json_);
free(whitespace_); free(whitespace_);
json_ = 0; json_ = 0;
whitespace_ = 0; whitespace_ = 0;
for (size_t i = 0; i < 7; i++) { for (size_t i = 0; i < 7; i++) {
free(types_[i]); free(types_[i]);
types_[i] = 0; types_[i] = 0;
} }
} }
private: private:
PerfTest(const PerfTest&); PerfTest(const PerfTest&);
PerfTest& operator=(const PerfTest&); PerfTest& operator=(const PerfTest&);
protected: protected:
const char* filename_; const char* filename_;
char *json_; char *json_;
size_t length_; size_t length_;
char *whitespace_; char *whitespace_;
size_t whitespace_length_; size_t whitespace_length_;
char *types_[7]; char *types_[7];
size_t typesLength_[7]; size_t typesLength_[7];
static const size_t kTrialCount = 1000; static const size_t kTrialCount = 1000;
}; };
#endif // __cplusplus #endif // __cplusplus
#endif // PERFTEST_H_ #endif // PERFTEST_H_

View File

@ -1,166 +1,166 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "perftest.h" #include "perftest.h"
// This file is for giving the performance characteristics of the platform (compiler/OS/CPU). // This file is for giving the performance characteristics of the platform (compiler/OS/CPU).
#if TEST_PLATFORM #if TEST_PLATFORM
#include <cmath> #include <cmath>
#include <fcntl.h> #include <fcntl.h>
// Windows // Windows
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#endif #endif
// UNIX // UNIX
#if defined(unix) || defined(__unix__) || defined(__unix) #if defined(unix) || defined(__unix__) || defined(__unix)
#include <unistd.h> #include <unistd.h>
#ifdef _POSIX_MAPPED_FILES #ifdef _POSIX_MAPPED_FILES
#include <sys/mman.h> #include <sys/mman.h>
#endif #endif
#endif #endif
class Platform : public PerfTest { class Platform : public PerfTest {
public: public:
virtual void SetUp() { virtual void SetUp() {
PerfTest::SetUp(); PerfTest::SetUp();
// temp buffer for testing // temp buffer for testing
temp_ = (char *)malloc(length_ + 1); temp_ = (char *)malloc(length_ + 1);
memcpy(temp_, json_, length_); memcpy(temp_, json_, length_);
checkSum_ = CheckSum(); checkSum_ = CheckSum();
} }
char CheckSum() { char CheckSum() {
char c = 0; char c = 0;
for (size_t i = 0; i < length_; ++i) for (size_t i = 0; i < length_; ++i)
c += temp_[i]; c += temp_[i];
return c; return c;
} }
virtual void TearDown() { virtual void TearDown() {
PerfTest::TearDown(); PerfTest::TearDown();
free(temp_); free(temp_);
} }
protected: protected:
char *temp_; char *temp_;
char checkSum_; char checkSum_;
}; };
TEST_F(Platform, CheckSum) { TEST_F(Platform, CheckSum) {
for (int i = 0; i < kTrialCount; i++) for (int i = 0; i < kTrialCount; i++)
EXPECT_EQ(checkSum_, CheckSum()); EXPECT_EQ(checkSum_, CheckSum());
} }
TEST_F(Platform, strlen) { TEST_F(Platform, strlen) {
for (int i = 0; i < kTrialCount; i++) { for (int i = 0; i < kTrialCount; i++) {
size_t l = strlen(json_); size_t l = strlen(json_);
EXPECT_EQ(length_, l); EXPECT_EQ(length_, l);
} }
} }
TEST_F(Platform, memcmp) { TEST_F(Platform, memcmp) {
for (int i = 0; i < kTrialCount; i++) { for (int i = 0; i < kTrialCount; i++) {
EXPECT_EQ(0, memcmp(temp_, json_, length_)); EXPECT_EQ(0, memcmp(temp_, json_, length_));
} }
} }
TEST_F(Platform, pow) { TEST_F(Platform, pow) {
double sum = 0; double sum = 0;
for (int i = 0; i < kTrialCount * kTrialCount; i++) for (int i = 0; i < kTrialCount * kTrialCount; i++)
sum += pow(10.0, i & 255); sum += pow(10.0, i & 255);
EXPECT_GT(sum, 0.0); EXPECT_GT(sum, 0.0);
} }
TEST_F(Platform, Whitespace_strlen) { TEST_F(Platform, Whitespace_strlen) {
for (int i = 0; i < kTrialCount; i++) { for (int i = 0; i < kTrialCount; i++) {
size_t l = strlen(whitespace_); size_t l = strlen(whitespace_);
EXPECT_GT(l, whitespace_length_); EXPECT_GT(l, whitespace_length_);
} }
} }
TEST_F(Platform, Whitespace_strspn) { TEST_F(Platform, Whitespace_strspn) {
for (int i = 0; i < kTrialCount; i++) { for (int i = 0; i < kTrialCount; i++) {
size_t l = strspn(whitespace_, " \n\r\t"); size_t l = strspn(whitespace_, " \n\r\t");
EXPECT_EQ(whitespace_length_, l); EXPECT_EQ(whitespace_length_, l);
} }
} }
TEST_F(Platform, fread) { TEST_F(Platform, fread) {
for (int i = 0; i < kTrialCount; i++) { for (int i = 0; i < kTrialCount; i++) {
FILE *fp = fopen(filename_, "rb"); FILE *fp = fopen(filename_, "rb");
ASSERT_EQ(length_, fread(temp_, 1, length_, fp)); ASSERT_EQ(length_, fread(temp_, 1, length_, fp));
EXPECT_EQ(checkSum_, CheckSum()); EXPECT_EQ(checkSum_, CheckSum());
fclose(fp); fclose(fp);
} }
} }
#ifdef _MSC_VER #ifdef _MSC_VER
TEST_F(Platform, read) { TEST_F(Platform, read) {
for (int i = 0; i < kTrialCount; i++) { for (int i = 0; i < kTrialCount; i++) {
int fd = _open(filename_, _O_BINARY | _O_RDONLY); int fd = _open(filename_, _O_BINARY | _O_RDONLY);
ASSERT_NE(-1, fd); ASSERT_NE(-1, fd);
ASSERT_EQ(length_, _read(fd, temp_, length_)); ASSERT_EQ(length_, _read(fd, temp_, length_));
EXPECT_EQ(checkSum_, CheckSum()); EXPECT_EQ(checkSum_, CheckSum());
_close(fd); _close(fd);
} }
} }
#else #else
TEST_F(Platform, read) { TEST_F(Platform, read) {
for (int i = 0; i < kTrialCount; i++) { for (int i = 0; i < kTrialCount; i++) {
int fd = open(filename_, O_RDONLY); int fd = open(filename_, O_RDONLY);
ASSERT_NE(-1, fd); ASSERT_NE(-1, fd);
ASSERT_EQ(length_, read(fd, temp_, length_)); ASSERT_EQ(length_, read(fd, temp_, length_));
EXPECT_EQ(checkSum_, CheckSum()); EXPECT_EQ(checkSum_, CheckSum());
close(fd); close(fd);
} }
} }
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
TEST_F(Platform, MapViewOfFile) { TEST_F(Platform, MapViewOfFile) {
for (int i = 0; i < kTrialCount; i++) { for (int i = 0; i < kTrialCount; i++) {
HANDLE file = CreateFile(filename_, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); HANDLE file = CreateFile(filename_, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
ASSERT_NE(INVALID_HANDLE_VALUE, file); ASSERT_NE(INVALID_HANDLE_VALUE, file);
HANDLE mapObject = CreateFileMapping(file, NULL, PAGE_READONLY, 0, length_, NULL); HANDLE mapObject = CreateFileMapping(file, NULL, PAGE_READONLY, 0, length_, NULL);
ASSERT_NE(INVALID_HANDLE_VALUE, mapObject); ASSERT_NE(INVALID_HANDLE_VALUE, mapObject);
void *p = MapViewOfFile(mapObject, FILE_MAP_READ, 0, 0, length_); void *p = MapViewOfFile(mapObject, FILE_MAP_READ, 0, 0, length_);
ASSERT_TRUE(p != NULL); ASSERT_TRUE(p != NULL);
EXPECT_EQ(checkSum_, CheckSum()); EXPECT_EQ(checkSum_, CheckSum());
ASSERT_TRUE(UnmapViewOfFile(p) == TRUE); ASSERT_TRUE(UnmapViewOfFile(p) == TRUE);
ASSERT_TRUE(CloseHandle(mapObject) == TRUE); ASSERT_TRUE(CloseHandle(mapObject) == TRUE);
ASSERT_TRUE(CloseHandle(file) == TRUE); ASSERT_TRUE(CloseHandle(file) == TRUE);
} }
} }
#endif #endif
#ifdef _POSIX_MAPPED_FILES #ifdef _POSIX_MAPPED_FILES
TEST_F(Platform, mmap) { TEST_F(Platform, mmap) {
for (int i = 0; i < kTrialCount; i++) { for (int i = 0; i < kTrialCount; i++) {
int fd = open(filename_, O_RDONLY); int fd = open(filename_, O_RDONLY);
ASSERT_NE(-1, fd); ASSERT_NE(-1, fd);
void *p = mmap(NULL, length_, PROT_READ, MAP_PRIVATE, fd, 0); void *p = mmap(NULL, length_, PROT_READ, MAP_PRIVATE, fd, 0);
ASSERT_TRUE(p != NULL); ASSERT_TRUE(p != NULL);
EXPECT_EQ(checkSum_, CheckSum()); EXPECT_EQ(checkSum_, CheckSum());
munmap(p, length_); munmap(p, length_);
close(fd); close(fd);
} }
} }
#endif #endif
#endif // TEST_PLATFORM #endif // TEST_PLATFORM

View File

@ -1,441 +1,441 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "perftest.h" #include "perftest.h"
#if TEST_RAPIDJSON #if TEST_RAPIDJSON
#include "rapidjson/rapidjson.h" #include "rapidjson/rapidjson.h"
#include "rapidjson/document.h" #include "rapidjson/document.h"
#include "rapidjson/prettywriter.h" #include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
#include "rapidjson/filereadstream.h" #include "rapidjson/filereadstream.h"
#include "rapidjson/encodedstream.h" #include "rapidjson/encodedstream.h"
#include "rapidjson/memorystream.h" #include "rapidjson/memorystream.h"
#ifdef RAPIDJSON_SSE2 #ifdef RAPIDJSON_SSE2
#define SIMD_SUFFIX(name) name##_SSE2 #define SIMD_SUFFIX(name) name##_SSE2
#elif defined(RAPIDJSON_SSE42) #elif defined(RAPIDJSON_SSE42)
#define SIMD_SUFFIX(name) name##_SSE42 #define SIMD_SUFFIX(name) name##_SSE42
#else #else
#define SIMD_SUFFIX(name) name #define SIMD_SUFFIX(name) name
#endif #endif
using namespace rapidjson; using namespace rapidjson;
class RapidJson : public PerfTest { class RapidJson : public PerfTest {
public: public:
RapidJson() : temp_(), doc_() {} RapidJson() : temp_(), doc_() {}
virtual void SetUp() { virtual void SetUp() {
PerfTest::SetUp(); PerfTest::SetUp();
// temp buffer for insitu parsing. // temp buffer for insitu parsing.
temp_ = (char *)malloc(length_ + 1); temp_ = (char *)malloc(length_ + 1);
// Parse as a document // Parse as a document
EXPECT_FALSE(doc_.Parse(json_).HasParseError()); EXPECT_FALSE(doc_.Parse(json_).HasParseError());
for (size_t i = 0; i < 7; i++) for (size_t i = 0; i < 7; i++)
EXPECT_FALSE(typesDoc_[i].Parse(types_[i]).HasParseError()); EXPECT_FALSE(typesDoc_[i].Parse(types_[i]).HasParseError());
} }
virtual void TearDown() { virtual void TearDown() {
PerfTest::TearDown(); PerfTest::TearDown();
free(temp_); free(temp_);
} }
private: private:
RapidJson(const RapidJson&); RapidJson(const RapidJson&);
RapidJson& operator=(const RapidJson&); RapidJson& operator=(const RapidJson&);
protected: protected:
char *temp_; char *temp_;
Document doc_; Document doc_;
Document typesDoc_[7]; Document typesDoc_[7];
}; };
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseInsitu_DummyHandler)) { TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseInsitu_DummyHandler)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
memcpy(temp_, json_, length_ + 1); memcpy(temp_, json_, length_ + 1);
InsituStringStream s(temp_); InsituStringStream s(temp_);
BaseReaderHandler<> h; BaseReaderHandler<> h;
Reader reader; Reader reader;
EXPECT_TRUE(reader.Parse<kParseInsituFlag>(s, h)); EXPECT_TRUE(reader.Parse<kParseInsituFlag>(s, h));
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseInsitu_DummyHandler_ValidateEncoding)) { TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseInsitu_DummyHandler_ValidateEncoding)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
memcpy(temp_, json_, length_ + 1); memcpy(temp_, json_, length_ + 1);
InsituStringStream s(temp_); InsituStringStream s(temp_);
BaseReaderHandler<> h; BaseReaderHandler<> h;
Reader reader; Reader reader;
EXPECT_TRUE(reader.Parse<kParseInsituFlag | kParseValidateEncodingFlag>(s, h)); EXPECT_TRUE(reader.Parse<kParseInsituFlag | kParseValidateEncodingFlag>(s, h));
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler)) { TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
StringStream s(json_); StringStream s(json_);
BaseReaderHandler<> h; BaseReaderHandler<> h;
Reader reader; Reader reader;
EXPECT_TRUE(reader.Parse(s, h)); EXPECT_TRUE(reader.Parse(s, h));
} }
} }
#define TEST_TYPED(index, Name)\ #define TEST_TYPED(index, Name)\
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_##Name)) {\ TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_##Name)) {\
for (size_t i = 0; i < kTrialCount * 10; i++) {\ for (size_t i = 0; i < kTrialCount * 10; i++) {\
StringStream s(types_[index]);\ StringStream s(types_[index]);\
BaseReaderHandler<> h;\ BaseReaderHandler<> h;\
Reader reader;\ Reader reader;\
EXPECT_TRUE(reader.Parse(s, h));\ EXPECT_TRUE(reader.Parse(s, h));\
}\ }\
}\ }\
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseInsitu_DummyHandler_##Name)) {\ TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseInsitu_DummyHandler_##Name)) {\
for (size_t i = 0; i < kTrialCount * 10; i++) {\ for (size_t i = 0; i < kTrialCount * 10; i++) {\
memcpy(temp_, types_[index], typesLength_[index] + 1);\ memcpy(temp_, types_[index], typesLength_[index] + 1);\
InsituStringStream s(temp_);\ InsituStringStream s(temp_);\
BaseReaderHandler<> h;\ BaseReaderHandler<> h;\
Reader reader;\ Reader reader;\
EXPECT_TRUE(reader.Parse<kParseInsituFlag>(s, h));\ EXPECT_TRUE(reader.Parse<kParseInsituFlag>(s, h));\
}\ }\
} }
TEST_TYPED(0, Booleans) TEST_TYPED(0, Booleans)
TEST_TYPED(1, Floats) TEST_TYPED(1, Floats)
TEST_TYPED(2, Guids) TEST_TYPED(2, Guids)
TEST_TYPED(3, Integers) TEST_TYPED(3, Integers)
TEST_TYPED(4, Mixed) TEST_TYPED(4, Mixed)
TEST_TYPED(5, Nulls) TEST_TYPED(5, Nulls)
TEST_TYPED(6, Paragraphs) TEST_TYPED(6, Paragraphs)
#undef TEST_TYPED #undef TEST_TYPED
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_FullPrecision)) { TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_FullPrecision)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
StringStream s(json_); StringStream s(json_);
BaseReaderHandler<> h; BaseReaderHandler<> h;
Reader reader; Reader reader;
EXPECT_TRUE(reader.Parse<kParseFullPrecisionFlag>(s, h)); EXPECT_TRUE(reader.Parse<kParseFullPrecisionFlag>(s, h));
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterative_DummyHandler)) { TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterative_DummyHandler)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
StringStream s(json_); StringStream s(json_);
BaseReaderHandler<> h; BaseReaderHandler<> h;
Reader reader; Reader reader;
EXPECT_TRUE(reader.Parse<kParseIterativeFlag>(s, h)); EXPECT_TRUE(reader.Parse<kParseIterativeFlag>(s, h));
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativeInsitu_DummyHandler)) { TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativeInsitu_DummyHandler)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
memcpy(temp_, json_, length_ + 1); memcpy(temp_, json_, length_ + 1);
InsituStringStream s(temp_); InsituStringStream s(temp_);
BaseReaderHandler<> h; BaseReaderHandler<> h;
Reader reader; Reader reader;
EXPECT_TRUE(reader.Parse<kParseIterativeFlag|kParseInsituFlag>(s, h)); EXPECT_TRUE(reader.Parse<kParseIterativeFlag|kParseInsituFlag>(s, h));
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_ValidateEncoding)) { TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_ValidateEncoding)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
StringStream s(json_); StringStream s(json_);
BaseReaderHandler<> h; BaseReaderHandler<> h;
Reader reader; Reader reader;
EXPECT_TRUE(reader.Parse<kParseValidateEncodingFlag>(s, h)); EXPECT_TRUE(reader.Parse<kParseValidateEncodingFlag>(s, h));
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseInsitu_MemoryPoolAllocator)) { TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseInsitu_MemoryPoolAllocator)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
memcpy(temp_, json_, length_ + 1); memcpy(temp_, json_, length_ + 1);
Document doc; Document doc;
doc.ParseInsitu(temp_); doc.ParseInsitu(temp_);
ASSERT_TRUE(doc.IsObject()); ASSERT_TRUE(doc.IsObject());
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseIterativeInsitu_MemoryPoolAllocator)) { TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseIterativeInsitu_MemoryPoolAllocator)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
memcpy(temp_, json_, length_ + 1); memcpy(temp_, json_, length_ + 1);
Document doc; Document doc;
doc.ParseInsitu<kParseIterativeFlag>(temp_); doc.ParseInsitu<kParseIterativeFlag>(temp_);
ASSERT_TRUE(doc.IsObject()); ASSERT_TRUE(doc.IsObject());
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(DocumentParse_MemoryPoolAllocator)) { TEST_F(RapidJson, SIMD_SUFFIX(DocumentParse_MemoryPoolAllocator)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
Document doc; Document doc;
doc.Parse(json_); doc.Parse(json_);
ASSERT_TRUE(doc.IsObject()); ASSERT_TRUE(doc.IsObject());
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseLength_MemoryPoolAllocator)) { TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseLength_MemoryPoolAllocator)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
Document doc; Document doc;
doc.Parse(json_, length_); doc.Parse(json_, length_);
ASSERT_TRUE(doc.IsObject()); ASSERT_TRUE(doc.IsObject());
} }
} }
#if RAPIDJSON_HAS_STDSTRING #if RAPIDJSON_HAS_STDSTRING
TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseStdString_MemoryPoolAllocator)) { TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseStdString_MemoryPoolAllocator)) {
const std::string s(json_, length_); const std::string s(json_, length_);
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
Document doc; Document doc;
doc.Parse(s); doc.Parse(s);
ASSERT_TRUE(doc.IsObject()); ASSERT_TRUE(doc.IsObject());
} }
} }
#endif #endif
TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseIterative_MemoryPoolAllocator)) { TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseIterative_MemoryPoolAllocator)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
Document doc; Document doc;
doc.Parse<kParseIterativeFlag>(json_); doc.Parse<kParseIterativeFlag>(json_);
ASSERT_TRUE(doc.IsObject()); ASSERT_TRUE(doc.IsObject());
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(DocumentParse_CrtAllocator)) { TEST_F(RapidJson, SIMD_SUFFIX(DocumentParse_CrtAllocator)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
memcpy(temp_, json_, length_ + 1); memcpy(temp_, json_, length_ + 1);
GenericDocument<UTF8<>, CrtAllocator> doc; GenericDocument<UTF8<>, CrtAllocator> doc;
doc.Parse(temp_); doc.Parse(temp_);
ASSERT_TRUE(doc.IsObject()); ASSERT_TRUE(doc.IsObject());
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseEncodedInputStream_MemoryStream)) { TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseEncodedInputStream_MemoryStream)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
MemoryStream ms(json_, length_); MemoryStream ms(json_, length_);
EncodedInputStream<UTF8<>, MemoryStream> is(ms); EncodedInputStream<UTF8<>, MemoryStream> is(ms);
Document doc; Document doc;
doc.ParseStream<0, UTF8<> >(is); doc.ParseStream<0, UTF8<> >(is);
ASSERT_TRUE(doc.IsObject()); ASSERT_TRUE(doc.IsObject());
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseAutoUTFInputStream_MemoryStream)) { TEST_F(RapidJson, SIMD_SUFFIX(DocumentParseAutoUTFInputStream_MemoryStream)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
MemoryStream ms(json_, length_); MemoryStream ms(json_, length_);
AutoUTFInputStream<unsigned, MemoryStream> is(ms); AutoUTFInputStream<unsigned, MemoryStream> is(ms);
Document doc; Document doc;
doc.ParseStream<0, AutoUTF<unsigned> >(is); doc.ParseStream<0, AutoUTF<unsigned> >(is);
ASSERT_TRUE(doc.IsObject()); ASSERT_TRUE(doc.IsObject());
} }
} }
template<typename T> template<typename T>
size_t Traverse(const T& value) { size_t Traverse(const T& value) {
size_t count = 1; size_t count = 1;
switch(value.GetType()) { switch(value.GetType()) {
case kObjectType: case kObjectType:
for (typename T::ConstMemberIterator itr = value.MemberBegin(); itr != value.MemberEnd(); ++itr) { for (typename T::ConstMemberIterator itr = value.MemberBegin(); itr != value.MemberEnd(); ++itr) {
count++; // name count++; // name
count += Traverse(itr->value); count += Traverse(itr->value);
} }
break; break;
case kArrayType: case kArrayType:
for (typename T::ConstValueIterator itr = value.Begin(); itr != value.End(); ++itr) for (typename T::ConstValueIterator itr = value.Begin(); itr != value.End(); ++itr)
count += Traverse(*itr); count += Traverse(*itr);
break; break;
default: default:
// Do nothing. // Do nothing.
break; break;
} }
return count; return count;
} }
TEST_F(RapidJson, DocumentTraverse) { TEST_F(RapidJson, DocumentTraverse) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
size_t count = Traverse(doc_); size_t count = Traverse(doc_);
EXPECT_EQ(4339u, count); EXPECT_EQ(4339u, count);
//if (i == 0) //if (i == 0)
// std::cout << count << std::endl; // std::cout << count << std::endl;
} }
} }
#ifdef __GNUC__ #ifdef __GNUC__
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++) RAPIDJSON_DIAG_OFF(effc++)
#endif #endif
struct ValueCounter : public BaseReaderHandler<> { struct ValueCounter : public BaseReaderHandler<> {
ValueCounter() : count_(1) {} // root ValueCounter() : count_(1) {} // root
bool EndObject(SizeType memberCount) { count_ += memberCount * 2; return true; } bool EndObject(SizeType memberCount) { count_ += memberCount * 2; return true; }
bool EndArray(SizeType elementCount) { count_ += elementCount; return true; } bool EndArray(SizeType elementCount) { count_ += elementCount; return true; }
SizeType count_; SizeType count_;
}; };
#ifdef __GNUC__ #ifdef __GNUC__
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif
TEST_F(RapidJson, DocumentAccept) { TEST_F(RapidJson, DocumentAccept) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
ValueCounter counter; ValueCounter counter;
doc_.Accept(counter); doc_.Accept(counter);
EXPECT_EQ(4339u, counter.count_); EXPECT_EQ(4339u, counter.count_);
} }
} }
struct NullStream { struct NullStream {
typedef char Ch; typedef char Ch;
NullStream() /*: length_(0)*/ {} NullStream() /*: length_(0)*/ {}
void Put(Ch) { /*++length_;*/ } void Put(Ch) { /*++length_;*/ }
void Flush() {} void Flush() {}
//size_t length_; //size_t length_;
}; };
TEST_F(RapidJson, Writer_NullStream) { TEST_F(RapidJson, Writer_NullStream) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
NullStream s; NullStream s;
Writer<NullStream> writer(s); Writer<NullStream> writer(s);
doc_.Accept(writer); doc_.Accept(writer);
//if (i == 0) //if (i == 0)
// std::cout << s.length_ << std::endl; // std::cout << s.length_ << std::endl;
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(Writer_StringBuffer)) { TEST_F(RapidJson, SIMD_SUFFIX(Writer_StringBuffer)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
StringBuffer s(0, 1024 * 1024); StringBuffer s(0, 1024 * 1024);
Writer<StringBuffer> writer(s); Writer<StringBuffer> writer(s);
doc_.Accept(writer); doc_.Accept(writer);
const char* str = s.GetString(); const char* str = s.GetString();
(void)str; (void)str;
//if (i == 0) //if (i == 0)
// std::cout << strlen(str) << std::endl; // std::cout << strlen(str) << std::endl;
} }
} }
#define TEST_TYPED(index, Name)\ #define TEST_TYPED(index, Name)\
TEST_F(RapidJson, SIMD_SUFFIX(Writer_StringBuffer_##Name)) {\ TEST_F(RapidJson, SIMD_SUFFIX(Writer_StringBuffer_##Name)) {\
for (size_t i = 0; i < kTrialCount * 10; i++) {\ for (size_t i = 0; i < kTrialCount * 10; i++) {\
StringBuffer s(0, 1024 * 1024);\ StringBuffer s(0, 1024 * 1024);\
Writer<StringBuffer> writer(s);\ Writer<StringBuffer> writer(s);\
typesDoc_[index].Accept(writer);\ typesDoc_[index].Accept(writer);\
const char* str = s.GetString();\ const char* str = s.GetString();\
(void)str;\ (void)str;\
}\ }\
} }
TEST_TYPED(0, Booleans) TEST_TYPED(0, Booleans)
TEST_TYPED(1, Floats) TEST_TYPED(1, Floats)
TEST_TYPED(2, Guids) TEST_TYPED(2, Guids)
TEST_TYPED(3, Integers) TEST_TYPED(3, Integers)
TEST_TYPED(4, Mixed) TEST_TYPED(4, Mixed)
TEST_TYPED(5, Nulls) TEST_TYPED(5, Nulls)
TEST_TYPED(6, Paragraphs) TEST_TYPED(6, Paragraphs)
#undef TEST_TYPED #undef TEST_TYPED
TEST_F(RapidJson, SIMD_SUFFIX(PrettyWriter_StringBuffer)) { TEST_F(RapidJson, SIMD_SUFFIX(PrettyWriter_StringBuffer)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
StringBuffer s(0, 2048 * 1024); StringBuffer s(0, 2048 * 1024);
PrettyWriter<StringBuffer> writer(s); PrettyWriter<StringBuffer> writer(s);
writer.SetIndent(' ', 1); writer.SetIndent(' ', 1);
doc_.Accept(writer); doc_.Accept(writer);
const char* str = s.GetString(); const char* str = s.GetString();
(void)str; (void)str;
//if (i == 0) //if (i == 0)
// std::cout << strlen(str) << std::endl; // std::cout << strlen(str) << std::endl;
} }
} }
TEST_F(RapidJson, internal_Pow10) { TEST_F(RapidJson, internal_Pow10) {
double sum = 0; double sum = 0;
for (size_t i = 0; i < kTrialCount * kTrialCount; i++) for (size_t i = 0; i < kTrialCount * kTrialCount; i++)
sum += internal::Pow10(int(i & 255)); sum += internal::Pow10(int(i & 255));
EXPECT_GT(sum, 0.0); EXPECT_GT(sum, 0.0);
} }
TEST_F(RapidJson, SkipWhitespace_Basic) { TEST_F(RapidJson, SkipWhitespace_Basic) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
rapidjson::StringStream s(whitespace_); rapidjson::StringStream s(whitespace_);
while (s.Peek() == ' ' || s.Peek() == '\n' || s.Peek() == '\r' || s.Peek() == '\t') while (s.Peek() == ' ' || s.Peek() == '\n' || s.Peek() == '\r' || s.Peek() == '\t')
s.Take(); s.Take();
ASSERT_EQ('[', s.Peek()); ASSERT_EQ('[', s.Peek());
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(SkipWhitespace)) { TEST_F(RapidJson, SIMD_SUFFIX(SkipWhitespace)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
rapidjson::StringStream s(whitespace_); rapidjson::StringStream s(whitespace_);
rapidjson::SkipWhitespace(s); rapidjson::SkipWhitespace(s);
ASSERT_EQ('[', s.Peek()); ASSERT_EQ('[', s.Peek());
} }
} }
TEST_F(RapidJson, SkipWhitespace_strspn) { TEST_F(RapidJson, SkipWhitespace_strspn) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
const char* s = whitespace_ + std::strspn(whitespace_, " \t\r\n"); const char* s = whitespace_ + std::strspn(whitespace_, " \t\r\n");
ASSERT_EQ('[', *s); ASSERT_EQ('[', *s);
} }
} }
TEST_F(RapidJson, UTF8_Validate) { TEST_F(RapidJson, UTF8_Validate) {
NullStream os; NullStream os;
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
StringStream is(json_); StringStream is(json_);
bool result = true; bool result = true;
while (is.Peek() != '\0') while (is.Peek() != '\0')
result &= UTF8<>::Validate(is, os); result &= UTF8<>::Validate(is, os);
EXPECT_TRUE(result); EXPECT_TRUE(result);
} }
} }
TEST_F(RapidJson, FileReadStream) { TEST_F(RapidJson, FileReadStream) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
FILE *fp = fopen(filename_, "rb"); FILE *fp = fopen(filename_, "rb");
char buffer[65536]; char buffer[65536];
FileReadStream s(fp, buffer, sizeof(buffer)); FileReadStream s(fp, buffer, sizeof(buffer));
while (s.Take() != '\0') while (s.Take() != '\0')
; ;
fclose(fp); fclose(fp);
} }
} }
TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_FileReadStream)) { TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_FileReadStream)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
FILE *fp = fopen(filename_, "rb"); FILE *fp = fopen(filename_, "rb");
char buffer[65536]; char buffer[65536];
FileReadStream s(fp, buffer, sizeof(buffer)); FileReadStream s(fp, buffer, sizeof(buffer));
BaseReaderHandler<> h; BaseReaderHandler<> h;
Reader reader; Reader reader;
reader.Parse(s, h); reader.Parse(s, h);
fclose(fp); fclose(fp);
} }
} }
TEST_F(RapidJson, StringBuffer) { TEST_F(RapidJson, StringBuffer) {
StringBuffer sb; StringBuffer sb;
for (int i = 0; i < 32 * 1024 * 1024; i++) for (int i = 0; i < 32 * 1024 * 1024; i++)
sb.Put(i & 0x7f); sb.Put(i & 0x7f);
} }
#endif // TEST_RAPIDJSON #endif // TEST_RAPIDJSON

File diff suppressed because it is too large Load Diff

View File

@ -1,313 +1,313 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "unittest.h" #include "unittest.h"
#include "rapidjson/filereadstream.h" #include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h" #include "rapidjson/filewritestream.h"
#include "rapidjson/encodedstream.h" #include "rapidjson/encodedstream.h"
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
#include "rapidjson/memorystream.h" #include "rapidjson/memorystream.h"
#include "rapidjson/memorybuffer.h" #include "rapidjson/memorybuffer.h"
using namespace rapidjson; using namespace rapidjson;
class EncodedStreamTest : public ::testing::Test { class EncodedStreamTest : public ::testing::Test {
public: public:
EncodedStreamTest() : json_(), length_() {} EncodedStreamTest() : json_(), length_() {}
virtual ~EncodedStreamTest(); virtual ~EncodedStreamTest();
virtual void SetUp() { virtual void SetUp() {
json_ = ReadFile("utf8.json", true, &length_); json_ = ReadFile("utf8.json", true, &length_);
} }
virtual void TearDown() { virtual void TearDown() {
free(json_); free(json_);
json_ = 0; json_ = 0;
} }
private: private:
EncodedStreamTest(const EncodedStreamTest&); EncodedStreamTest(const EncodedStreamTest&);
EncodedStreamTest& operator=(const EncodedStreamTest&); EncodedStreamTest& operator=(const EncodedStreamTest&);
protected: protected:
static FILE* Open(const char* filename) { static FILE* Open(const char* filename) {
const char *paths[] = { const char *paths[] = {
"encodings", "encodings",
"bin/encodings", "bin/encodings",
"../bin/encodings", "../bin/encodings",
"../../bin/encodings", "../../bin/encodings",
"../../../bin/encodings" "../../../bin/encodings"
}; };
char buffer[1024]; char buffer[1024];
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
sprintf(buffer, "%s/%s", paths[i], filename); sprintf(buffer, "%s/%s", paths[i], filename);
FILE *fp = fopen(buffer, "rb"); FILE *fp = fopen(buffer, "rb");
if (fp) if (fp)
return fp; return fp;
} }
return 0; return 0;
} }
static char *ReadFile(const char* filename, bool appendPath, size_t* outLength) { static char *ReadFile(const char* filename, bool appendPath, size_t* outLength) {
FILE *fp = appendPath ? Open(filename) : fopen(filename, "rb"); FILE *fp = appendPath ? Open(filename) : fopen(filename, "rb");
if (!fp) { if (!fp) {
*outLength = 0; *outLength = 0;
return 0; return 0;
} }
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
*outLength = static_cast<size_t>(ftell(fp)); *outLength = static_cast<size_t>(ftell(fp));
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
char* buffer = static_cast<char*>(malloc(*outLength + 1)); char* buffer = static_cast<char*>(malloc(*outLength + 1));
size_t readLength = fread(buffer, 1, *outLength, fp); size_t readLength = fread(buffer, 1, *outLength, fp);
buffer[readLength] = '\0'; buffer[readLength] = '\0';
fclose(fp); fclose(fp);
return buffer; return buffer;
} }
template <typename FileEncoding, typename MemoryEncoding> template <typename FileEncoding, typename MemoryEncoding>
void TestEncodedInputStream(const char* filename) { void TestEncodedInputStream(const char* filename) {
// Test FileReadStream // Test FileReadStream
{ {
char buffer[16]; char buffer[16];
FILE *fp = Open(filename); FILE *fp = Open(filename);
ASSERT_TRUE(fp != 0); ASSERT_TRUE(fp != 0);
FileReadStream fs(fp, buffer, sizeof(buffer)); FileReadStream fs(fp, buffer, sizeof(buffer));
EncodedInputStream<FileEncoding, FileReadStream> eis(fs); EncodedInputStream<FileEncoding, FileReadStream> eis(fs);
StringStream s(json_); StringStream s(json_);
while (eis.Peek() != '\0') { while (eis.Peek() != '\0') {
unsigned expected, actual; unsigned expected, actual;
EXPECT_TRUE(UTF8<>::Decode(s, &expected)); EXPECT_TRUE(UTF8<>::Decode(s, &expected));
EXPECT_TRUE(MemoryEncoding::Decode(eis, &actual)); EXPECT_TRUE(MemoryEncoding::Decode(eis, &actual));
EXPECT_EQ(expected, actual); EXPECT_EQ(expected, actual);
} }
EXPECT_EQ('\0', s.Peek()); EXPECT_EQ('\0', s.Peek());
fclose(fp); fclose(fp);
} }
// Test MemoryStream // Test MemoryStream
{ {
size_t size; size_t size;
char* data = ReadFile(filename, true, &size); char* data = ReadFile(filename, true, &size);
MemoryStream ms(data, size); MemoryStream ms(data, size);
EncodedInputStream<FileEncoding, MemoryStream> eis(ms); EncodedInputStream<FileEncoding, MemoryStream> eis(ms);
StringStream s(json_); StringStream s(json_);
while (eis.Peek() != '\0') { while (eis.Peek() != '\0') {
unsigned expected, actual; unsigned expected, actual;
EXPECT_TRUE(UTF8<>::Decode(s, &expected)); EXPECT_TRUE(UTF8<>::Decode(s, &expected));
EXPECT_TRUE(MemoryEncoding::Decode(eis, &actual)); EXPECT_TRUE(MemoryEncoding::Decode(eis, &actual));
EXPECT_EQ(expected, actual); EXPECT_EQ(expected, actual);
} }
EXPECT_EQ('\0', s.Peek()); EXPECT_EQ('\0', s.Peek());
free(data); free(data);
EXPECT_EQ(size, eis.Tell()); EXPECT_EQ(size, eis.Tell());
} }
} }
void TestAutoUTFInputStream(const char *filename, bool expectHasBOM) { void TestAutoUTFInputStream(const char *filename, bool expectHasBOM) {
// Test FileReadStream // Test FileReadStream
{ {
char buffer[16]; char buffer[16];
FILE *fp = Open(filename); FILE *fp = Open(filename);
ASSERT_TRUE(fp != 0); ASSERT_TRUE(fp != 0);
FileReadStream fs(fp, buffer, sizeof(buffer)); FileReadStream fs(fp, buffer, sizeof(buffer));
AutoUTFInputStream<unsigned, FileReadStream> eis(fs); AutoUTFInputStream<unsigned, FileReadStream> eis(fs);
EXPECT_EQ(expectHasBOM, eis.HasBOM()); EXPECT_EQ(expectHasBOM, eis.HasBOM());
StringStream s(json_); StringStream s(json_);
while (eis.Peek() != '\0') { while (eis.Peek() != '\0') {
unsigned expected, actual; unsigned expected, actual;
EXPECT_TRUE(UTF8<>::Decode(s, &expected)); EXPECT_TRUE(UTF8<>::Decode(s, &expected));
EXPECT_TRUE(AutoUTF<unsigned>::Decode(eis, &actual)); EXPECT_TRUE(AutoUTF<unsigned>::Decode(eis, &actual));
EXPECT_EQ(expected, actual); EXPECT_EQ(expected, actual);
} }
EXPECT_EQ('\0', s.Peek()); EXPECT_EQ('\0', s.Peek());
fclose(fp); fclose(fp);
} }
// Test MemoryStream // Test MemoryStream
{ {
size_t size; size_t size;
char* data = ReadFile(filename, true, &size); char* data = ReadFile(filename, true, &size);
MemoryStream ms(data, size); MemoryStream ms(data, size);
AutoUTFInputStream<unsigned, MemoryStream> eis(ms); AutoUTFInputStream<unsigned, MemoryStream> eis(ms);
EXPECT_EQ(expectHasBOM, eis.HasBOM()); EXPECT_EQ(expectHasBOM, eis.HasBOM());
StringStream s(json_); StringStream s(json_);
while (eis.Peek() != '\0') { while (eis.Peek() != '\0') {
unsigned expected, actual; unsigned expected, actual;
EXPECT_TRUE(UTF8<>::Decode(s, &expected)); EXPECT_TRUE(UTF8<>::Decode(s, &expected));
EXPECT_TRUE(AutoUTF<unsigned>::Decode(eis, &actual)); EXPECT_TRUE(AutoUTF<unsigned>::Decode(eis, &actual));
EXPECT_EQ(expected, actual); EXPECT_EQ(expected, actual);
} }
EXPECT_EQ('\0', s.Peek()); EXPECT_EQ('\0', s.Peek());
free(data); free(data);
EXPECT_EQ(size, eis.Tell()); EXPECT_EQ(size, eis.Tell());
} }
} }
template <typename FileEncoding, typename MemoryEncoding> template <typename FileEncoding, typename MemoryEncoding>
void TestEncodedOutputStream(const char* expectedFilename, bool putBOM) { void TestEncodedOutputStream(const char* expectedFilename, bool putBOM) {
// Test FileWriteStream // Test FileWriteStream
{ {
char filename[L_tmpnam]; char filename[L_tmpnam];
FILE* fp = TempFile(filename); FILE* fp = TempFile(filename);
char buffer[16]; char buffer[16];
FileWriteStream os(fp, buffer, sizeof(buffer)); FileWriteStream os(fp, buffer, sizeof(buffer));
EncodedOutputStream<FileEncoding, FileWriteStream> eos(os, putBOM); EncodedOutputStream<FileEncoding, FileWriteStream> eos(os, putBOM);
StringStream s(json_); StringStream s(json_);
while (s.Peek() != '\0') { while (s.Peek() != '\0') {
bool success = Transcoder<UTF8<>, MemoryEncoding>::Transcode(s, eos); bool success = Transcoder<UTF8<>, MemoryEncoding>::Transcode(s, eos);
EXPECT_TRUE(success); EXPECT_TRUE(success);
} }
eos.Flush(); eos.Flush();
fclose(fp); fclose(fp);
EXPECT_TRUE(CompareFile(filename, expectedFilename)); EXPECT_TRUE(CompareFile(filename, expectedFilename));
remove(filename); remove(filename);
} }
// Test MemoryBuffer // Test MemoryBuffer
{ {
MemoryBuffer mb; MemoryBuffer mb;
EncodedOutputStream<FileEncoding, MemoryBuffer> eos(mb, putBOM); EncodedOutputStream<FileEncoding, MemoryBuffer> eos(mb, putBOM);
StringStream s(json_); StringStream s(json_);
while (s.Peek() != '\0') { while (s.Peek() != '\0') {
bool success = Transcoder<UTF8<>, MemoryEncoding>::Transcode(s, eos); bool success = Transcoder<UTF8<>, MemoryEncoding>::Transcode(s, eos);
EXPECT_TRUE(success); EXPECT_TRUE(success);
} }
eos.Flush(); eos.Flush();
EXPECT_TRUE(CompareBufferFile(mb.GetBuffer(), mb.GetSize(), expectedFilename)); EXPECT_TRUE(CompareBufferFile(mb.GetBuffer(), mb.GetSize(), expectedFilename));
} }
} }
void TestAutoUTFOutputStream(UTFType type, bool putBOM, const char *expectedFilename) { void TestAutoUTFOutputStream(UTFType type, bool putBOM, const char *expectedFilename) {
// Test FileWriteStream // Test FileWriteStream
{ {
char filename[L_tmpnam]; char filename[L_tmpnam];
FILE* fp = TempFile(filename); FILE* fp = TempFile(filename);
char buffer[16]; char buffer[16];
FileWriteStream os(fp, buffer, sizeof(buffer)); FileWriteStream os(fp, buffer, sizeof(buffer));
AutoUTFOutputStream<unsigned, FileWriteStream> eos(os, type, putBOM); AutoUTFOutputStream<unsigned, FileWriteStream> eos(os, type, putBOM);
StringStream s(json_); StringStream s(json_);
while (s.Peek() != '\0') { while (s.Peek() != '\0') {
bool success = Transcoder<UTF8<>, AutoUTF<unsigned> >::Transcode(s, eos); bool success = Transcoder<UTF8<>, AutoUTF<unsigned> >::Transcode(s, eos);
EXPECT_TRUE(success); EXPECT_TRUE(success);
} }
eos.Flush(); eos.Flush();
fclose(fp); fclose(fp);
EXPECT_TRUE(CompareFile(filename, expectedFilename)); EXPECT_TRUE(CompareFile(filename, expectedFilename));
remove(filename); remove(filename);
} }
// Test MemoryBuffer // Test MemoryBuffer
{ {
MemoryBuffer mb; MemoryBuffer mb;
AutoUTFOutputStream<unsigned, MemoryBuffer> eos(mb, type, putBOM); AutoUTFOutputStream<unsigned, MemoryBuffer> eos(mb, type, putBOM);
StringStream s(json_); StringStream s(json_);
while (s.Peek() != '\0') { while (s.Peek() != '\0') {
bool success = Transcoder<UTF8<>, AutoUTF<unsigned> >::Transcode(s, eos); bool success = Transcoder<UTF8<>, AutoUTF<unsigned> >::Transcode(s, eos);
EXPECT_TRUE(success); EXPECT_TRUE(success);
} }
eos.Flush(); eos.Flush();
EXPECT_TRUE(CompareBufferFile(mb.GetBuffer(), mb.GetSize(), expectedFilename)); EXPECT_TRUE(CompareBufferFile(mb.GetBuffer(), mb.GetSize(), expectedFilename));
} }
} }
bool CompareFile(const char* filename, const char* expectedFilename) { bool CompareFile(const char* filename, const char* expectedFilename) {
size_t actualLength, expectedLength; size_t actualLength, expectedLength;
char* actualBuffer = ReadFile(filename, false, &actualLength); char* actualBuffer = ReadFile(filename, false, &actualLength);
char* expectedBuffer = ReadFile(expectedFilename, true, &expectedLength); char* expectedBuffer = ReadFile(expectedFilename, true, &expectedLength);
bool ret = (expectedLength == actualLength) && memcmp(expectedBuffer, actualBuffer, actualLength) == 0; bool ret = (expectedLength == actualLength) && memcmp(expectedBuffer, actualBuffer, actualLength) == 0;
free(actualBuffer); free(actualBuffer);
free(expectedBuffer); free(expectedBuffer);
return ret; return ret;
} }
bool CompareBufferFile(const char* actualBuffer, size_t actualLength, const char* expectedFilename) { bool CompareBufferFile(const char* actualBuffer, size_t actualLength, const char* expectedFilename) {
size_t expectedLength; size_t expectedLength;
char* expectedBuffer = ReadFile(expectedFilename, true, &expectedLength); char* expectedBuffer = ReadFile(expectedFilename, true, &expectedLength);
bool ret = (expectedLength == actualLength) && memcmp(expectedBuffer, actualBuffer, actualLength) == 0; bool ret = (expectedLength == actualLength) && memcmp(expectedBuffer, actualBuffer, actualLength) == 0;
free(expectedBuffer); free(expectedBuffer);
return ret; return ret;
} }
char *json_; char *json_;
size_t length_; size_t length_;
}; };
EncodedStreamTest::~EncodedStreamTest() {} EncodedStreamTest::~EncodedStreamTest() {}
TEST_F(EncodedStreamTest, EncodedInputStream) { TEST_F(EncodedStreamTest, EncodedInputStream) {
TestEncodedInputStream<UTF8<>, UTF8<> >("utf8.json"); TestEncodedInputStream<UTF8<>, UTF8<> >("utf8.json");
TestEncodedInputStream<UTF8<>, UTF8<> >("utf8bom.json"); TestEncodedInputStream<UTF8<>, UTF8<> >("utf8bom.json");
TestEncodedInputStream<UTF16LE<>, UTF16<> >("utf16le.json"); TestEncodedInputStream<UTF16LE<>, UTF16<> >("utf16le.json");
TestEncodedInputStream<UTF16LE<>, UTF16<> >("utf16lebom.json"); TestEncodedInputStream<UTF16LE<>, UTF16<> >("utf16lebom.json");
TestEncodedInputStream<UTF16BE<>, UTF16<> >("utf16be.json"); TestEncodedInputStream<UTF16BE<>, UTF16<> >("utf16be.json");
TestEncodedInputStream<UTF16BE<>, UTF16<> >("utf16bebom.json"); TestEncodedInputStream<UTF16BE<>, UTF16<> >("utf16bebom.json");
TestEncodedInputStream<UTF32LE<>, UTF32<> >("utf32le.json"); TestEncodedInputStream<UTF32LE<>, UTF32<> >("utf32le.json");
TestEncodedInputStream<UTF32LE<>, UTF32<> >("utf32lebom.json"); TestEncodedInputStream<UTF32LE<>, UTF32<> >("utf32lebom.json");
TestEncodedInputStream<UTF32BE<>, UTF32<> >("utf32be.json"); TestEncodedInputStream<UTF32BE<>, UTF32<> >("utf32be.json");
TestEncodedInputStream<UTF32BE<>, UTF32<> >("utf32bebom.json"); TestEncodedInputStream<UTF32BE<>, UTF32<> >("utf32bebom.json");
} }
TEST_F(EncodedStreamTest, AutoUTFInputStream) { TEST_F(EncodedStreamTest, AutoUTFInputStream) {
TestAutoUTFInputStream("utf8.json", false); TestAutoUTFInputStream("utf8.json", false);
TestAutoUTFInputStream("utf8bom.json", true); TestAutoUTFInputStream("utf8bom.json", true);
TestAutoUTFInputStream("utf16le.json", false); TestAutoUTFInputStream("utf16le.json", false);
TestAutoUTFInputStream("utf16lebom.json",true); TestAutoUTFInputStream("utf16lebom.json",true);
TestAutoUTFInputStream("utf16be.json", false); TestAutoUTFInputStream("utf16be.json", false);
TestAutoUTFInputStream("utf16bebom.json",true); TestAutoUTFInputStream("utf16bebom.json",true);
TestAutoUTFInputStream("utf32le.json", false); TestAutoUTFInputStream("utf32le.json", false);
TestAutoUTFInputStream("utf32lebom.json",true); TestAutoUTFInputStream("utf32lebom.json",true);
TestAutoUTFInputStream("utf32be.json", false); TestAutoUTFInputStream("utf32be.json", false);
TestAutoUTFInputStream("utf32bebom.json", true); TestAutoUTFInputStream("utf32bebom.json", true);
{ {
// Auto detection fail, use user defined UTF type // Auto detection fail, use user defined UTF type
const char json[] = "{ }"; const char json[] = "{ }";
MemoryStream ms(json, sizeof(json)); MemoryStream ms(json, sizeof(json));
AutoUTFInputStream<unsigned, MemoryStream> eis(ms, kUTF8); AutoUTFInputStream<unsigned, MemoryStream> eis(ms, kUTF8);
EXPECT_FALSE(eis.HasBOM()); EXPECT_FALSE(eis.HasBOM());
EXPECT_EQ(kUTF8, eis.GetType()); EXPECT_EQ(kUTF8, eis.GetType());
} }
} }
TEST_F(EncodedStreamTest, EncodedOutputStream) { TEST_F(EncodedStreamTest, EncodedOutputStream) {
TestEncodedOutputStream<UTF8<>, UTF8<> >("utf8.json", false); TestEncodedOutputStream<UTF8<>, UTF8<> >("utf8.json", false);
TestEncodedOutputStream<UTF8<>, UTF8<> >("utf8bom.json", true); TestEncodedOutputStream<UTF8<>, UTF8<> >("utf8bom.json", true);
TestEncodedOutputStream<UTF16LE<>, UTF16<> >("utf16le.json", false); TestEncodedOutputStream<UTF16LE<>, UTF16<> >("utf16le.json", false);
TestEncodedOutputStream<UTF16LE<>, UTF16<> >("utf16lebom.json",true); TestEncodedOutputStream<UTF16LE<>, UTF16<> >("utf16lebom.json",true);
TestEncodedOutputStream<UTF16BE<>, UTF16<> >("utf16be.json", false); TestEncodedOutputStream<UTF16BE<>, UTF16<> >("utf16be.json", false);
TestEncodedOutputStream<UTF16BE<>, UTF16<> >("utf16bebom.json",true); TestEncodedOutputStream<UTF16BE<>, UTF16<> >("utf16bebom.json",true);
TestEncodedOutputStream<UTF32LE<>, UTF32<> >("utf32le.json", false); TestEncodedOutputStream<UTF32LE<>, UTF32<> >("utf32le.json", false);
TestEncodedOutputStream<UTF32LE<>, UTF32<> >("utf32lebom.json",true); TestEncodedOutputStream<UTF32LE<>, UTF32<> >("utf32lebom.json",true);
TestEncodedOutputStream<UTF32BE<>, UTF32<> >("utf32be.json", false); TestEncodedOutputStream<UTF32BE<>, UTF32<> >("utf32be.json", false);
TestEncodedOutputStream<UTF32BE<>, UTF32<> >("utf32bebom.json",true); TestEncodedOutputStream<UTF32BE<>, UTF32<> >("utf32bebom.json",true);
} }
TEST_F(EncodedStreamTest, AutoUTFOutputStream) { TEST_F(EncodedStreamTest, AutoUTFOutputStream) {
TestAutoUTFOutputStream(kUTF8, false, "utf8.json"); TestAutoUTFOutputStream(kUTF8, false, "utf8.json");
TestAutoUTFOutputStream(kUTF8, true, "utf8bom.json"); TestAutoUTFOutputStream(kUTF8, true, "utf8bom.json");
TestAutoUTFOutputStream(kUTF16LE, false, "utf16le.json"); TestAutoUTFOutputStream(kUTF16LE, false, "utf16le.json");
TestAutoUTFOutputStream(kUTF16LE, true, "utf16lebom.json"); TestAutoUTFOutputStream(kUTF16LE, true, "utf16lebom.json");
TestAutoUTFOutputStream(kUTF16BE, false, "utf16be.json"); TestAutoUTFOutputStream(kUTF16BE, false, "utf16be.json");
TestAutoUTFOutputStream(kUTF16BE, true, "utf16bebom.json"); TestAutoUTFOutputStream(kUTF16BE, true, "utf16bebom.json");
TestAutoUTFOutputStream(kUTF32LE, false, "utf32le.json"); TestAutoUTFOutputStream(kUTF32LE, false, "utf32le.json");
TestAutoUTFOutputStream(kUTF32LE, true, "utf32lebom.json"); TestAutoUTFOutputStream(kUTF32LE, true, "utf32lebom.json");
TestAutoUTFOutputStream(kUTF32BE, false, "utf32be.json"); TestAutoUTFOutputStream(kUTF32BE, false, "utf32be.json");
TestAutoUTFOutputStream(kUTF32BE, true, "utf32bebom.json"); TestAutoUTFOutputStream(kUTF32BE, true, "utf32bebom.json");
} }

View File

@ -1,425 +1,425 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "unittest.h" #include "unittest.h"
#include "rapidjson/filereadstream.h" #include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h" #include "rapidjson/filewritestream.h"
#include "rapidjson/encodedstream.h" #include "rapidjson/encodedstream.h"
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
using namespace rapidjson; using namespace rapidjson;
// Verification of encoders/decoders with Hoehrmann's UTF8 decoder // Verification of encoders/decoders with Hoehrmann's UTF8 decoder
// http://www.unicode.org/Public/UNIDATA/Blocks.txt // http://www.unicode.org/Public/UNIDATA/Blocks.txt
static const unsigned kCodepointRanges[] = { static const unsigned kCodepointRanges[] = {
0x0000, 0x007F, // Basic Latin 0x0000, 0x007F, // Basic Latin
0x0080, 0x00FF, // Latin-1 Supplement 0x0080, 0x00FF, // Latin-1 Supplement
0x0100, 0x017F, // Latin Extended-A 0x0100, 0x017F, // Latin Extended-A
0x0180, 0x024F, // Latin Extended-B 0x0180, 0x024F, // Latin Extended-B
0x0250, 0x02AF, // IPA Extensions 0x0250, 0x02AF, // IPA Extensions
0x02B0, 0x02FF, // Spacing Modifier Letters 0x02B0, 0x02FF, // Spacing Modifier Letters
0x0300, 0x036F, // Combining Diacritical Marks 0x0300, 0x036F, // Combining Diacritical Marks
0x0370, 0x03FF, // Greek and Coptic 0x0370, 0x03FF, // Greek and Coptic
0x0400, 0x04FF, // Cyrillic 0x0400, 0x04FF, // Cyrillic
0x0500, 0x052F, // Cyrillic Supplement 0x0500, 0x052F, // Cyrillic Supplement
0x0530, 0x058F, // Armenian 0x0530, 0x058F, // Armenian
0x0590, 0x05FF, // Hebrew 0x0590, 0x05FF, // Hebrew
0x0600, 0x06FF, // Arabic 0x0600, 0x06FF, // Arabic
0x0700, 0x074F, // Syriac 0x0700, 0x074F, // Syriac
0x0750, 0x077F, // Arabic Supplement 0x0750, 0x077F, // Arabic Supplement
0x0780, 0x07BF, // Thaana 0x0780, 0x07BF, // Thaana
0x07C0, 0x07FF, // NKo 0x07C0, 0x07FF, // NKo
0x0800, 0x083F, // Samaritan 0x0800, 0x083F, // Samaritan
0x0840, 0x085F, // Mandaic 0x0840, 0x085F, // Mandaic
0x0900, 0x097F, // Devanagari 0x0900, 0x097F, // Devanagari
0x0980, 0x09FF, // Bengali 0x0980, 0x09FF, // Bengali
0x0A00, 0x0A7F, // Gurmukhi 0x0A00, 0x0A7F, // Gurmukhi
0x0A80, 0x0AFF, // Gujarati 0x0A80, 0x0AFF, // Gujarati
0x0B00, 0x0B7F, // Oriya 0x0B00, 0x0B7F, // Oriya
0x0B80, 0x0BFF, // Tamil 0x0B80, 0x0BFF, // Tamil
0x0C00, 0x0C7F, // Telugu 0x0C00, 0x0C7F, // Telugu
0x0C80, 0x0CFF, // Kannada 0x0C80, 0x0CFF, // Kannada
0x0D00, 0x0D7F, // Malayalam 0x0D00, 0x0D7F, // Malayalam
0x0D80, 0x0DFF, // Sinhala 0x0D80, 0x0DFF, // Sinhala
0x0E00, 0x0E7F, // Thai 0x0E00, 0x0E7F, // Thai
0x0E80, 0x0EFF, // Lao 0x0E80, 0x0EFF, // Lao
0x0F00, 0x0FFF, // Tibetan 0x0F00, 0x0FFF, // Tibetan
0x1000, 0x109F, // Myanmar 0x1000, 0x109F, // Myanmar
0x10A0, 0x10FF, // Georgian 0x10A0, 0x10FF, // Georgian
0x1100, 0x11FF, // Hangul Jamo 0x1100, 0x11FF, // Hangul Jamo
0x1200, 0x137F, // Ethiopic 0x1200, 0x137F, // Ethiopic
0x1380, 0x139F, // Ethiopic Supplement 0x1380, 0x139F, // Ethiopic Supplement
0x13A0, 0x13FF, // Cherokee 0x13A0, 0x13FF, // Cherokee
0x1400, 0x167F, // Unified Canadian Aboriginal Syllabics 0x1400, 0x167F, // Unified Canadian Aboriginal Syllabics
0x1680, 0x169F, // Ogham 0x1680, 0x169F, // Ogham
0x16A0, 0x16FF, // Runic 0x16A0, 0x16FF, // Runic
0x1700, 0x171F, // Tagalog 0x1700, 0x171F, // Tagalog
0x1720, 0x173F, // Hanunoo 0x1720, 0x173F, // Hanunoo
0x1740, 0x175F, // Buhid 0x1740, 0x175F, // Buhid
0x1760, 0x177F, // Tagbanwa 0x1760, 0x177F, // Tagbanwa
0x1780, 0x17FF, // Khmer 0x1780, 0x17FF, // Khmer
0x1800, 0x18AF, // Mongolian 0x1800, 0x18AF, // Mongolian
0x18B0, 0x18FF, // Unified Canadian Aboriginal Syllabics Extended 0x18B0, 0x18FF, // Unified Canadian Aboriginal Syllabics Extended
0x1900, 0x194F, // Limbu 0x1900, 0x194F, // Limbu
0x1950, 0x197F, // Tai Le 0x1950, 0x197F, // Tai Le
0x1980, 0x19DF, // New Tai Lue 0x1980, 0x19DF, // New Tai Lue
0x19E0, 0x19FF, // Khmer Symbols 0x19E0, 0x19FF, // Khmer Symbols
0x1A00, 0x1A1F, // Buginese 0x1A00, 0x1A1F, // Buginese
0x1A20, 0x1AAF, // Tai Tham 0x1A20, 0x1AAF, // Tai Tham
0x1B00, 0x1B7F, // Balinese 0x1B00, 0x1B7F, // Balinese
0x1B80, 0x1BBF, // Sundanese 0x1B80, 0x1BBF, // Sundanese
0x1BC0, 0x1BFF, // Batak 0x1BC0, 0x1BFF, // Batak
0x1C00, 0x1C4F, // Lepcha 0x1C00, 0x1C4F, // Lepcha
0x1C50, 0x1C7F, // Ol Chiki 0x1C50, 0x1C7F, // Ol Chiki
0x1CD0, 0x1CFF, // Vedic Extensions 0x1CD0, 0x1CFF, // Vedic Extensions
0x1D00, 0x1D7F, // Phonetic Extensions 0x1D00, 0x1D7F, // Phonetic Extensions
0x1D80, 0x1DBF, // Phonetic Extensions Supplement 0x1D80, 0x1DBF, // Phonetic Extensions Supplement
0x1DC0, 0x1DFF, // Combining Diacritical Marks Supplement 0x1DC0, 0x1DFF, // Combining Diacritical Marks Supplement
0x1E00, 0x1EFF, // Latin Extended Additional 0x1E00, 0x1EFF, // Latin Extended Additional
0x1F00, 0x1FFF, // Greek Extended 0x1F00, 0x1FFF, // Greek Extended
0x2000, 0x206F, // General Punctuation 0x2000, 0x206F, // General Punctuation
0x2070, 0x209F, // Superscripts and Subscripts 0x2070, 0x209F, // Superscripts and Subscripts
0x20A0, 0x20CF, // Currency Symbols 0x20A0, 0x20CF, // Currency Symbols
0x20D0, 0x20FF, // Combining Diacritical Marks for Symbols 0x20D0, 0x20FF, // Combining Diacritical Marks for Symbols
0x2100, 0x214F, // Letterlike Symbols 0x2100, 0x214F, // Letterlike Symbols
0x2150, 0x218F, // Number Forms 0x2150, 0x218F, // Number Forms
0x2190, 0x21FF, // Arrows 0x2190, 0x21FF, // Arrows
0x2200, 0x22FF, // Mathematical Operators 0x2200, 0x22FF, // Mathematical Operators
0x2300, 0x23FF, // Miscellaneous Technical 0x2300, 0x23FF, // Miscellaneous Technical
0x2400, 0x243F, // Control Pictures 0x2400, 0x243F, // Control Pictures
0x2440, 0x245F, // Optical Character Recognition 0x2440, 0x245F, // Optical Character Recognition
0x2460, 0x24FF, // Enclosed Alphanumerics 0x2460, 0x24FF, // Enclosed Alphanumerics
0x2500, 0x257F, // Box Drawing 0x2500, 0x257F, // Box Drawing
0x2580, 0x259F, // Block Elements 0x2580, 0x259F, // Block Elements
0x25A0, 0x25FF, // Geometric Shapes 0x25A0, 0x25FF, // Geometric Shapes
0x2600, 0x26FF, // Miscellaneous Symbols 0x2600, 0x26FF, // Miscellaneous Symbols
0x2700, 0x27BF, // Dingbats 0x2700, 0x27BF, // Dingbats
0x27C0, 0x27EF, // Miscellaneous Mathematical Symbols-A 0x27C0, 0x27EF, // Miscellaneous Mathematical Symbols-A
0x27F0, 0x27FF, // Supplemental Arrows-A 0x27F0, 0x27FF, // Supplemental Arrows-A
0x2800, 0x28FF, // Braille Patterns 0x2800, 0x28FF, // Braille Patterns
0x2900, 0x297F, // Supplemental Arrows-B 0x2900, 0x297F, // Supplemental Arrows-B
0x2980, 0x29FF, // Miscellaneous Mathematical Symbols-B 0x2980, 0x29FF, // Miscellaneous Mathematical Symbols-B
0x2A00, 0x2AFF, // Supplemental Mathematical Operators 0x2A00, 0x2AFF, // Supplemental Mathematical Operators
0x2B00, 0x2BFF, // Miscellaneous Symbols and Arrows 0x2B00, 0x2BFF, // Miscellaneous Symbols and Arrows
0x2C00, 0x2C5F, // Glagolitic 0x2C00, 0x2C5F, // Glagolitic
0x2C60, 0x2C7F, // Latin Extended-C 0x2C60, 0x2C7F, // Latin Extended-C
0x2C80, 0x2CFF, // Coptic 0x2C80, 0x2CFF, // Coptic
0x2D00, 0x2D2F, // Georgian Supplement 0x2D00, 0x2D2F, // Georgian Supplement
0x2D30, 0x2D7F, // Tifinagh 0x2D30, 0x2D7F, // Tifinagh
0x2D80, 0x2DDF, // Ethiopic Extended 0x2D80, 0x2DDF, // Ethiopic Extended
0x2DE0, 0x2DFF, // Cyrillic Extended-A 0x2DE0, 0x2DFF, // Cyrillic Extended-A
0x2E00, 0x2E7F, // Supplemental Punctuation 0x2E00, 0x2E7F, // Supplemental Punctuation
0x2E80, 0x2EFF, // CJK Radicals Supplement 0x2E80, 0x2EFF, // CJK Radicals Supplement
0x2F00, 0x2FDF, // Kangxi Radicals 0x2F00, 0x2FDF, // Kangxi Radicals
0x2FF0, 0x2FFF, // Ideographic Description Characters 0x2FF0, 0x2FFF, // Ideographic Description Characters
0x3000, 0x303F, // CJK Symbols and Punctuation 0x3000, 0x303F, // CJK Symbols and Punctuation
0x3040, 0x309F, // Hiragana 0x3040, 0x309F, // Hiragana
0x30A0, 0x30FF, // Katakana 0x30A0, 0x30FF, // Katakana
0x3100, 0x312F, // Bopomofo 0x3100, 0x312F, // Bopomofo
0x3130, 0x318F, // Hangul Compatibility Jamo 0x3130, 0x318F, // Hangul Compatibility Jamo
0x3190, 0x319F, // Kanbun 0x3190, 0x319F, // Kanbun
0x31A0, 0x31BF, // Bopomofo Extended 0x31A0, 0x31BF, // Bopomofo Extended
0x31C0, 0x31EF, // CJK Strokes 0x31C0, 0x31EF, // CJK Strokes
0x31F0, 0x31FF, // Katakana Phonetic Extensions 0x31F0, 0x31FF, // Katakana Phonetic Extensions
0x3200, 0x32FF, // Enclosed CJK Letters and Months 0x3200, 0x32FF, // Enclosed CJK Letters and Months
0x3300, 0x33FF, // CJK Compatibility 0x3300, 0x33FF, // CJK Compatibility
0x3400, 0x4DBF, // CJK Unified Ideographs Extension A 0x3400, 0x4DBF, // CJK Unified Ideographs Extension A
0x4DC0, 0x4DFF, // Yijing Hexagram Symbols 0x4DC0, 0x4DFF, // Yijing Hexagram Symbols
0x4E00, 0x9FFF, // CJK Unified Ideographs 0x4E00, 0x9FFF, // CJK Unified Ideographs
0xA000, 0xA48F, // Yi Syllables 0xA000, 0xA48F, // Yi Syllables
0xA490, 0xA4CF, // Yi Radicals 0xA490, 0xA4CF, // Yi Radicals
0xA4D0, 0xA4FF, // Lisu 0xA4D0, 0xA4FF, // Lisu
0xA500, 0xA63F, // Vai 0xA500, 0xA63F, // Vai
0xA640, 0xA69F, // Cyrillic Extended-B 0xA640, 0xA69F, // Cyrillic Extended-B
0xA6A0, 0xA6FF, // Bamum 0xA6A0, 0xA6FF, // Bamum
0xA700, 0xA71F, // Modifier Tone Letters 0xA700, 0xA71F, // Modifier Tone Letters
0xA720, 0xA7FF, // Latin Extended-D 0xA720, 0xA7FF, // Latin Extended-D
0xA800, 0xA82F, // Syloti Nagri 0xA800, 0xA82F, // Syloti Nagri
0xA830, 0xA83F, // Common Indic Number Forms 0xA830, 0xA83F, // Common Indic Number Forms
0xA840, 0xA87F, // Phags-pa 0xA840, 0xA87F, // Phags-pa
0xA880, 0xA8DF, // Saurashtra 0xA880, 0xA8DF, // Saurashtra
0xA8E0, 0xA8FF, // Devanagari Extended 0xA8E0, 0xA8FF, // Devanagari Extended
0xA900, 0xA92F, // Kayah Li 0xA900, 0xA92F, // Kayah Li
0xA930, 0xA95F, // Rejang 0xA930, 0xA95F, // Rejang
0xA960, 0xA97F, // Hangul Jamo Extended-A 0xA960, 0xA97F, // Hangul Jamo Extended-A
0xA980, 0xA9DF, // Javanese 0xA980, 0xA9DF, // Javanese
0xAA00, 0xAA5F, // Cham 0xAA00, 0xAA5F, // Cham
0xAA60, 0xAA7F, // Myanmar Extended-A 0xAA60, 0xAA7F, // Myanmar Extended-A
0xAA80, 0xAADF, // Tai Viet 0xAA80, 0xAADF, // Tai Viet
0xAB00, 0xAB2F, // Ethiopic Extended-A 0xAB00, 0xAB2F, // Ethiopic Extended-A
0xABC0, 0xABFF, // Meetei Mayek 0xABC0, 0xABFF, // Meetei Mayek
0xAC00, 0xD7AF, // Hangul Syllables 0xAC00, 0xD7AF, // Hangul Syllables
0xD7B0, 0xD7FF, // Hangul Jamo Extended-B 0xD7B0, 0xD7FF, // Hangul Jamo Extended-B
//0xD800, 0xDB7F, // High Surrogates //0xD800, 0xDB7F, // High Surrogates
//0xDB80, 0xDBFF, // High Private Use Surrogates //0xDB80, 0xDBFF, // High Private Use Surrogates
//0xDC00, 0xDFFF, // Low Surrogates //0xDC00, 0xDFFF, // Low Surrogates
0xE000, 0xF8FF, // Private Use Area 0xE000, 0xF8FF, // Private Use Area
0xF900, 0xFAFF, // CJK Compatibility Ideographs 0xF900, 0xFAFF, // CJK Compatibility Ideographs
0xFB00, 0xFB4F, // Alphabetic Presentation Forms 0xFB00, 0xFB4F, // Alphabetic Presentation Forms
0xFB50, 0xFDFF, // Arabic Presentation Forms-A 0xFB50, 0xFDFF, // Arabic Presentation Forms-A
0xFE00, 0xFE0F, // Variation Selectors 0xFE00, 0xFE0F, // Variation Selectors
0xFE10, 0xFE1F, // Vertical Forms 0xFE10, 0xFE1F, // Vertical Forms
0xFE20, 0xFE2F, // Combining Half Marks 0xFE20, 0xFE2F, // Combining Half Marks
0xFE30, 0xFE4F, // CJK Compatibility Forms 0xFE30, 0xFE4F, // CJK Compatibility Forms
0xFE50, 0xFE6F, // Small Form Variants 0xFE50, 0xFE6F, // Small Form Variants
0xFE70, 0xFEFF, // Arabic Presentation Forms-B 0xFE70, 0xFEFF, // Arabic Presentation Forms-B
0xFF00, 0xFFEF, // Halfwidth and Fullwidth Forms 0xFF00, 0xFFEF, // Halfwidth and Fullwidth Forms
0xFFF0, 0xFFFF, // Specials 0xFFF0, 0xFFFF, // Specials
0x10000, 0x1007F, // Linear B Syllabary 0x10000, 0x1007F, // Linear B Syllabary
0x10080, 0x100FF, // Linear B Ideograms 0x10080, 0x100FF, // Linear B Ideograms
0x10100, 0x1013F, // Aegean Numbers 0x10100, 0x1013F, // Aegean Numbers
0x10140, 0x1018F, // Ancient Greek Numbers 0x10140, 0x1018F, // Ancient Greek Numbers
0x10190, 0x101CF, // Ancient Symbols 0x10190, 0x101CF, // Ancient Symbols
0x101D0, 0x101FF, // Phaistos Disc 0x101D0, 0x101FF, // Phaistos Disc
0x10280, 0x1029F, // Lycian 0x10280, 0x1029F, // Lycian
0x102A0, 0x102DF, // Carian 0x102A0, 0x102DF, // Carian
0x10300, 0x1032F, // Old Italic 0x10300, 0x1032F, // Old Italic
0x10330, 0x1034F, // Gothic 0x10330, 0x1034F, // Gothic
0x10380, 0x1039F, // Ugaritic 0x10380, 0x1039F, // Ugaritic
0x103A0, 0x103DF, // Old Persian 0x103A0, 0x103DF, // Old Persian
0x10400, 0x1044F, // Deseret 0x10400, 0x1044F, // Deseret
0x10450, 0x1047F, // Shavian 0x10450, 0x1047F, // Shavian
0x10480, 0x104AF, // Osmanya 0x10480, 0x104AF, // Osmanya
0x10800, 0x1083F, // Cypriot Syllabary 0x10800, 0x1083F, // Cypriot Syllabary
0x10840, 0x1085F, // Imperial Aramaic 0x10840, 0x1085F, // Imperial Aramaic
0x10900, 0x1091F, // Phoenician 0x10900, 0x1091F, // Phoenician
0x10920, 0x1093F, // Lydian 0x10920, 0x1093F, // Lydian
0x10A00, 0x10A5F, // Kharoshthi 0x10A00, 0x10A5F, // Kharoshthi
0x10A60, 0x10A7F, // Old South Arabian 0x10A60, 0x10A7F, // Old South Arabian
0x10B00, 0x10B3F, // Avestan 0x10B00, 0x10B3F, // Avestan
0x10B40, 0x10B5F, // Inscriptional Parthian 0x10B40, 0x10B5F, // Inscriptional Parthian
0x10B60, 0x10B7F, // Inscriptional Pahlavi 0x10B60, 0x10B7F, // Inscriptional Pahlavi
0x10C00, 0x10C4F, // Old Turkic 0x10C00, 0x10C4F, // Old Turkic
0x10E60, 0x10E7F, // Rumi Numeral Symbols 0x10E60, 0x10E7F, // Rumi Numeral Symbols
0x11000, 0x1107F, // Brahmi 0x11000, 0x1107F, // Brahmi
0x11080, 0x110CF, // Kaithi 0x11080, 0x110CF, // Kaithi
0x12000, 0x123FF, // Cuneiform 0x12000, 0x123FF, // Cuneiform
0x12400, 0x1247F, // Cuneiform Numbers and Punctuation 0x12400, 0x1247F, // Cuneiform Numbers and Punctuation
0x13000, 0x1342F, // Egyptian Hieroglyphs 0x13000, 0x1342F, // Egyptian Hieroglyphs
0x16800, 0x16A3F, // Bamum Supplement 0x16800, 0x16A3F, // Bamum Supplement
0x1B000, 0x1B0FF, // Kana Supplement 0x1B000, 0x1B0FF, // Kana Supplement
0x1D000, 0x1D0FF, // Byzantine Musical Symbols 0x1D000, 0x1D0FF, // Byzantine Musical Symbols
0x1D100, 0x1D1FF, // Musical Symbols 0x1D100, 0x1D1FF, // Musical Symbols
0x1D200, 0x1D24F, // Ancient Greek Musical Notation 0x1D200, 0x1D24F, // Ancient Greek Musical Notation
0x1D300, 0x1D35F, // Tai Xuan Jing Symbols 0x1D300, 0x1D35F, // Tai Xuan Jing Symbols
0x1D360, 0x1D37F, // Counting Rod Numerals 0x1D360, 0x1D37F, // Counting Rod Numerals
0x1D400, 0x1D7FF, // Mathematical Alphanumeric Symbols 0x1D400, 0x1D7FF, // Mathematical Alphanumeric Symbols
0x1F000, 0x1F02F, // Mahjong Tiles 0x1F000, 0x1F02F, // Mahjong Tiles
0x1F030, 0x1F09F, // Domino Tiles 0x1F030, 0x1F09F, // Domino Tiles
0x1F0A0, 0x1F0FF, // Playing Cards 0x1F0A0, 0x1F0FF, // Playing Cards
0x1F100, 0x1F1FF, // Enclosed Alphanumeric Supplement 0x1F100, 0x1F1FF, // Enclosed Alphanumeric Supplement
0x1F200, 0x1F2FF, // Enclosed Ideographic Supplement 0x1F200, 0x1F2FF, // Enclosed Ideographic Supplement
0x1F300, 0x1F5FF, // Miscellaneous Symbols And Pictographs 0x1F300, 0x1F5FF, // Miscellaneous Symbols And Pictographs
0x1F600, 0x1F64F, // Emoticons 0x1F600, 0x1F64F, // Emoticons
0x1F680, 0x1F6FF, // Transport And Map Symbols 0x1F680, 0x1F6FF, // Transport And Map Symbols
0x1F700, 0x1F77F, // Alchemical Symbols 0x1F700, 0x1F77F, // Alchemical Symbols
0x20000, 0x2A6DF, // CJK Unified Ideographs Extension B 0x20000, 0x2A6DF, // CJK Unified Ideographs Extension B
0x2A700, 0x2B73F, // CJK Unified Ideographs Extension C 0x2A700, 0x2B73F, // CJK Unified Ideographs Extension C
0x2B740, 0x2B81F, // CJK Unified Ideographs Extension D 0x2B740, 0x2B81F, // CJK Unified Ideographs Extension D
0x2F800, 0x2FA1F, // CJK Compatibility Ideographs Supplement 0x2F800, 0x2FA1F, // CJK Compatibility Ideographs Supplement
0xE0000, 0xE007F, // Tags 0xE0000, 0xE007F, // Tags
0xE0100, 0xE01EF, // Variation Selectors Supplement 0xE0100, 0xE01EF, // Variation Selectors Supplement
0xF0000, 0xFFFFF, // Supplementary Private Use Area-A 0xF0000, 0xFFFFF, // Supplementary Private Use Area-A
0x100000, 0x10FFFF, // Supplementary Private Use Area-B 0x100000, 0x10FFFF, // Supplementary Private Use Area-B
0xFFFFFFFF 0xFFFFFFFF
}; };
// Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de> // Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. // See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
#define UTF8_ACCEPT 0u #define UTF8_ACCEPT 0u
static const unsigned char utf8d[] = { static const unsigned char utf8d[] = {
// The first part of the table maps bytes to character classes that // The first part of the table maps bytes to character classes that
// to reduce the size of the transition table and create bitmasks. // to reduce the size of the transition table and create bitmasks.
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, 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,
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, 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,
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, 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,
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, 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,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
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, 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,
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, 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,
// The second part is a transition table that maps a combination // The second part is a transition table that maps a combination
// of a state of the automaton and a character class to a state. // of a state of the automaton and a character class to a state.
0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12, 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12, 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12, 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12, 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
12,36,12,12,12,12,12,12,12,12,12,12, 12,36,12,12,12,12,12,12,12,12,12,12,
}; };
static unsigned inline decode(unsigned* state, unsigned* codep, unsigned byte) { static unsigned inline decode(unsigned* state, unsigned* codep, unsigned byte) {
unsigned type = utf8d[byte]; unsigned type = utf8d[byte];
*codep = (*state != UTF8_ACCEPT) ? *codep = (*state != UTF8_ACCEPT) ?
(byte & 0x3fu) | (*codep << 6) : (byte & 0x3fu) | (*codep << 6) :
(0xff >> type) & (byte); (0xff >> type) & (byte);
*state = utf8d[256 + *state + type]; *state = utf8d[256 + *state + type];
return *state; return *state;
} }
//static bool IsUTF8(unsigned char* s) { //static bool IsUTF8(unsigned char* s) {
// unsigned codepoint, state = 0; // unsigned codepoint, state = 0;
// //
// while (*s) // while (*s)
// decode(&state, &codepoint, *s++); // decode(&state, &codepoint, *s++);
// //
// return state == UTF8_ACCEPT; // return state == UTF8_ACCEPT;
//} //}
TEST(EncodingsTest, UTF8) { TEST(EncodingsTest, UTF8) {
StringBuffer os, os2; StringBuffer os, os2;
for (const unsigned* range = kCodepointRanges; *range != 0xFFFFFFFF; range += 2) { for (const unsigned* range = kCodepointRanges; *range != 0xFFFFFFFF; range += 2) {
for (unsigned codepoint = range[0]; codepoint <= range[1]; ++codepoint) { for (unsigned codepoint = range[0]; codepoint <= range[1]; ++codepoint) {
os.Clear(); os.Clear();
UTF8<>::Encode(os, codepoint); UTF8<>::Encode(os, codepoint);
const char* encodedStr = os.GetString(); const char* encodedStr = os.GetString();
// Decode with Hoehrmann // Decode with Hoehrmann
{ {
unsigned decodedCodepoint = 0; unsigned decodedCodepoint = 0;
unsigned state = 0; unsigned state = 0;
unsigned decodedCount = 0; unsigned decodedCount = 0;
for (const char* s = encodedStr; *s; ++s) for (const char* s = encodedStr; *s; ++s)
if (!decode(&state, &decodedCodepoint, static_cast<unsigned char>(*s))) { if (!decode(&state, &decodedCodepoint, static_cast<unsigned char>(*s))) {
EXPECT_EQ(codepoint, decodedCodepoint); EXPECT_EQ(codepoint, decodedCodepoint);
decodedCount++; decodedCount++;
} }
if (*encodedStr) // This decoder cannot handle U+0000 if (*encodedStr) // This decoder cannot handle U+0000
EXPECT_EQ(1u, decodedCount); // Should only contain one code point EXPECT_EQ(1u, decodedCount); // Should only contain one code point
EXPECT_EQ(UTF8_ACCEPT, state); EXPECT_EQ(UTF8_ACCEPT, state);
if (UTF8_ACCEPT != state) if (UTF8_ACCEPT != state)
std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl; std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl;
} }
// Decode // Decode
{ {
StringStream is(encodedStr); StringStream is(encodedStr);
unsigned decodedCodepoint; unsigned decodedCodepoint;
bool result = UTF8<>::Decode(is, &decodedCodepoint); bool result = UTF8<>::Decode(is, &decodedCodepoint);
EXPECT_TRUE(result); EXPECT_TRUE(result);
EXPECT_EQ(codepoint, decodedCodepoint); EXPECT_EQ(codepoint, decodedCodepoint);
if (!result || codepoint != decodedCodepoint) if (!result || codepoint != decodedCodepoint)
std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl; std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl;
} }
// Validate // Validate
{ {
StringStream is(encodedStr); StringStream is(encodedStr);
os2.Clear(); os2.Clear();
bool result = UTF8<>::Validate(is, os2); bool result = UTF8<>::Validate(is, os2);
EXPECT_TRUE(result); EXPECT_TRUE(result);
EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString())); EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString()));
} }
} }
} }
} }
TEST(EncodingsTest, UTF16) { TEST(EncodingsTest, UTF16) {
GenericStringBuffer<UTF16<> > os, os2; GenericStringBuffer<UTF16<> > os, os2;
GenericStringBuffer<UTF8<> > utf8os; GenericStringBuffer<UTF8<> > utf8os;
for (const unsigned* range = kCodepointRanges; *range != 0xFFFFFFFF; range += 2) { for (const unsigned* range = kCodepointRanges; *range != 0xFFFFFFFF; range += 2) {
for (unsigned codepoint = range[0]; codepoint <= range[1]; ++codepoint) { for (unsigned codepoint = range[0]; codepoint <= range[1]; ++codepoint) {
os.Clear(); os.Clear();
UTF16<>::Encode(os, codepoint); UTF16<>::Encode(os, codepoint);
const UTF16<>::Ch* encodedStr = os.GetString(); const UTF16<>::Ch* encodedStr = os.GetString();
// Encode with Hoehrmann's code // Encode with Hoehrmann's code
if (codepoint != 0) // cannot handle U+0000 if (codepoint != 0) // cannot handle U+0000
{ {
// encode with UTF8<> first // encode with UTF8<> first
utf8os.Clear(); utf8os.Clear();
UTF8<>::Encode(utf8os, codepoint); UTF8<>::Encode(utf8os, codepoint);
// transcode from UTF8 to UTF16 with Hoehrmann's code // transcode from UTF8 to UTF16 with Hoehrmann's code
unsigned decodedCodepoint = 0; unsigned decodedCodepoint = 0;
unsigned state = 0; unsigned state = 0;
UTF16<>::Ch buffer[3], *p = &buffer[0]; UTF16<>::Ch buffer[3], *p = &buffer[0];
for (const char* s = utf8os.GetString(); *s; ++s) { for (const char* s = utf8os.GetString(); *s; ++s) {
if (!decode(&state, &decodedCodepoint, static_cast<unsigned char>(*s))) if (!decode(&state, &decodedCodepoint, static_cast<unsigned char>(*s)))
break; break;
} }
if (codepoint <= 0xFFFF) if (codepoint <= 0xFFFF)
*p++ = static_cast<UTF16<>::Ch>(decodedCodepoint); *p++ = static_cast<UTF16<>::Ch>(decodedCodepoint);
else { else {
// Encode code points above U+FFFF as surrogate pair. // Encode code points above U+FFFF as surrogate pair.
*p++ = static_cast<UTF16<>::Ch>(0xD7C0 + (decodedCodepoint >> 10)); *p++ = static_cast<UTF16<>::Ch>(0xD7C0 + (decodedCodepoint >> 10));
*p++ = static_cast<UTF16<>::Ch>(0xDC00 + (decodedCodepoint & 0x3FF)); *p++ = static_cast<UTF16<>::Ch>(0xDC00 + (decodedCodepoint & 0x3FF));
} }
*p++ = '\0'; *p++ = '\0';
EXPECT_EQ(0, StrCmp(buffer, encodedStr)); EXPECT_EQ(0, StrCmp(buffer, encodedStr));
} }
// Decode // Decode
{ {
GenericStringStream<UTF16<> > is(encodedStr); GenericStringStream<UTF16<> > is(encodedStr);
unsigned decodedCodepoint; unsigned decodedCodepoint;
bool result = UTF16<>::Decode(is, &decodedCodepoint); bool result = UTF16<>::Decode(is, &decodedCodepoint);
EXPECT_TRUE(result); EXPECT_TRUE(result);
EXPECT_EQ(codepoint, decodedCodepoint); EXPECT_EQ(codepoint, decodedCodepoint);
if (!result || codepoint != decodedCodepoint) if (!result || codepoint != decodedCodepoint)
std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl; std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl;
} }
// Validate // Validate
{ {
GenericStringStream<UTF16<> > is(encodedStr); GenericStringStream<UTF16<> > is(encodedStr);
os2.Clear(); os2.Clear();
bool result = UTF16<>::Validate(is, os2); bool result = UTF16<>::Validate(is, os2);
EXPECT_TRUE(result); EXPECT_TRUE(result);
EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString())); EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString()));
} }
} }
} }
} }
TEST(EncodingsTest, UTF32) { TEST(EncodingsTest, UTF32) {
GenericStringBuffer<UTF32<> > os, os2; GenericStringBuffer<UTF32<> > os, os2;
for (const unsigned* range = kCodepointRanges; *range != 0xFFFFFFFF; range += 2) { for (const unsigned* range = kCodepointRanges; *range != 0xFFFFFFFF; range += 2) {
for (unsigned codepoint = range[0]; codepoint <= range[1]; ++codepoint) { for (unsigned codepoint = range[0]; codepoint <= range[1]; ++codepoint) {
os.Clear(); os.Clear();
UTF32<>::Encode(os, codepoint); UTF32<>::Encode(os, codepoint);
const UTF32<>::Ch* encodedStr = os.GetString(); const UTF32<>::Ch* encodedStr = os.GetString();
// Decode // Decode
{ {
GenericStringStream<UTF32<> > is(encodedStr); GenericStringStream<UTF32<> > is(encodedStr);
unsigned decodedCodepoint; unsigned decodedCodepoint;
bool result = UTF32<>::Decode(is, &decodedCodepoint); bool result = UTF32<>::Decode(is, &decodedCodepoint);
EXPECT_TRUE(result); EXPECT_TRUE(result);
EXPECT_EQ(codepoint, decodedCodepoint); EXPECT_EQ(codepoint, decodedCodepoint);
if (!result || codepoint != decodedCodepoint) if (!result || codepoint != decodedCodepoint)
std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl; std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl;
} }
// Validate // Validate
{ {
GenericStringStream<UTF32<> > is(encodedStr); GenericStringStream<UTF32<> > is(encodedStr);
os2.Clear(); os2.Clear();
bool result = UTF32<>::Validate(is, os2); bool result = UTF32<>::Validate(is, os2);
EXPECT_TRUE(result); EXPECT_TRUE(result);
EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString())); EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString()));
} }
} }
} }
} }

View File

@ -1,112 +1,112 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "unittest.h" #include "unittest.h"
#include "rapidjson/filereadstream.h" #include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h" #include "rapidjson/filewritestream.h"
#include "rapidjson/encodedstream.h" #include "rapidjson/encodedstream.h"
using namespace rapidjson; using namespace rapidjson;
class FileStreamTest : public ::testing::Test { class FileStreamTest : public ::testing::Test {
public: public:
FileStreamTest() : filename_(), json_(), length_() {} FileStreamTest() : filename_(), json_(), length_() {}
virtual ~FileStreamTest(); virtual ~FileStreamTest();
virtual void SetUp() { virtual void SetUp() {
const char *paths[] = { const char *paths[] = {
"data/sample.json", "data/sample.json",
"bin/data/sample.json", "bin/data/sample.json",
"../bin/data/sample.json", "../bin/data/sample.json",
"../../bin/data/sample.json", "../../bin/data/sample.json",
"../../../bin/data/sample.json" "../../../bin/data/sample.json"
}; };
FILE* fp = 0; FILE* fp = 0;
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
fp = fopen(paths[i], "rb"); fp = fopen(paths[i], "rb");
if (fp) { if (fp) {
filename_ = paths[i]; filename_ = paths[i];
break; break;
} }
} }
ASSERT_TRUE(fp != 0); ASSERT_TRUE(fp != 0);
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
length_ = static_cast<size_t>(ftell(fp)); length_ = static_cast<size_t>(ftell(fp));
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
json_ = static_cast<char*>(malloc(length_ + 1)); json_ = static_cast<char*>(malloc(length_ + 1));
size_t readLength = fread(json_, 1, length_, fp); size_t readLength = fread(json_, 1, length_, fp);
json_[readLength] = '\0'; json_[readLength] = '\0';
fclose(fp); fclose(fp);
} }
virtual void TearDown() { virtual void TearDown() {
free(json_); free(json_);
json_ = 0; json_ = 0;
} }
private: private:
FileStreamTest(const FileStreamTest&); FileStreamTest(const FileStreamTest&);
FileStreamTest& operator=(const FileStreamTest&); FileStreamTest& operator=(const FileStreamTest&);
protected: protected:
const char* filename_; const char* filename_;
char *json_; char *json_;
size_t length_; size_t length_;
}; };
FileStreamTest::~FileStreamTest() {} FileStreamTest::~FileStreamTest() {}
TEST_F(FileStreamTest, FileReadStream) { TEST_F(FileStreamTest, FileReadStream) {
FILE *fp = fopen(filename_, "rb"); FILE *fp = fopen(filename_, "rb");
ASSERT_TRUE(fp != 0); ASSERT_TRUE(fp != 0);
char buffer[65536]; char buffer[65536];
FileReadStream s(fp, buffer, sizeof(buffer)); FileReadStream s(fp, buffer, sizeof(buffer));
for (size_t i = 0; i < length_; i++) { for (size_t i = 0; i < length_; i++) {
EXPECT_EQ(json_[i], s.Peek()); EXPECT_EQ(json_[i], s.Peek());
EXPECT_EQ(json_[i], s.Peek()); // 2nd time should be the same EXPECT_EQ(json_[i], s.Peek()); // 2nd time should be the same
EXPECT_EQ(json_[i], s.Take()); EXPECT_EQ(json_[i], s.Take());
} }
EXPECT_EQ(length_, s.Tell()); EXPECT_EQ(length_, s.Tell());
EXPECT_EQ('\0', s.Peek()); EXPECT_EQ('\0', s.Peek());
fclose(fp); fclose(fp);
} }
TEST_F(FileStreamTest, FileWriteStream) { TEST_F(FileStreamTest, FileWriteStream) {
char filename[L_tmpnam]; char filename[L_tmpnam];
FILE* fp = TempFile(filename); FILE* fp = TempFile(filename);
char buffer[65536]; char buffer[65536];
FileWriteStream os(fp, buffer, sizeof(buffer)); FileWriteStream os(fp, buffer, sizeof(buffer));
for (size_t i = 0; i < length_; i++) for (size_t i = 0; i < length_; i++)
os.Put(json_[i]); os.Put(json_[i]);
os.Flush(); os.Flush();
fclose(fp); fclose(fp);
// Read it back to verify // Read it back to verify
fp = fopen(filename, "rb"); fp = fopen(filename, "rb");
FileReadStream is(fp, buffer, sizeof(buffer)); FileReadStream is(fp, buffer, sizeof(buffer));
for (size_t i = 0; i < length_; i++) for (size_t i = 0; i < length_; i++)
EXPECT_EQ(json_[i], is.Take()); EXPECT_EQ(json_[i], is.Take());
EXPECT_EQ(length_, is.Tell()); EXPECT_EQ(length_, is.Tell());
fclose(fp); fclose(fp);
//std::cout << filename << std::endl; //std::cout << filename << std::endl;
remove(filename); remove(filename);
} }

View File

@ -1,227 +1,227 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "unittest.h" #include "unittest.h"
// Using forward declared types here. // Using forward declared types here.
#include "rapidjson/fwd.h" #include "rapidjson/fwd.h"
#ifdef __GNUC__ #ifdef __GNUC__
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++) RAPIDJSON_DIAG_OFF(effc++)
#endif #endif
using namespace rapidjson; using namespace rapidjson;
struct Foo { struct Foo {
Foo(); Foo();
~Foo(); ~Foo();
// encodings.h // encodings.h
UTF8<char>* utf8; UTF8<char>* utf8;
UTF16<wchar_t>* utf16; UTF16<wchar_t>* utf16;
UTF16BE<wchar_t>* utf16be; UTF16BE<wchar_t>* utf16be;
UTF16LE<wchar_t>* utf16le; UTF16LE<wchar_t>* utf16le;
UTF32<unsigned>* utf32; UTF32<unsigned>* utf32;
UTF32BE<unsigned>* utf32be; UTF32BE<unsigned>* utf32be;
UTF32LE<unsigned>* utf32le; UTF32LE<unsigned>* utf32le;
ASCII<char>* ascii; ASCII<char>* ascii;
AutoUTF<unsigned>* autoutf; AutoUTF<unsigned>* autoutf;
Transcoder<UTF8<char>, UTF8<char> >* transcoder; Transcoder<UTF8<char>, UTF8<char> >* transcoder;
// allocators.h // allocators.h
CrtAllocator* crtallocator; CrtAllocator* crtallocator;
MemoryPoolAllocator<CrtAllocator>* memorypoolallocator; MemoryPoolAllocator<CrtAllocator>* memorypoolallocator;
// stream.h // stream.h
StringStream* stringstream; StringStream* stringstream;
InsituStringStream* insitustringstream; InsituStringStream* insitustringstream;
// stringbuffer.h // stringbuffer.h
StringBuffer* stringbuffer; StringBuffer* stringbuffer;
// // filereadstream.h // // filereadstream.h
// FileReadStream* filereadstream; // FileReadStream* filereadstream;
// // filewritestream.h // // filewritestream.h
// FileWriteStream* filewritestream; // FileWriteStream* filewritestream;
// memorybuffer.h // memorybuffer.h
MemoryBuffer* memorybuffer; MemoryBuffer* memorybuffer;
// memorystream.h // memorystream.h
MemoryStream* memorystream; MemoryStream* memorystream;
// reader.h // reader.h
BaseReaderHandler<UTF8<char>, void>* basereaderhandler; BaseReaderHandler<UTF8<char>, void>* basereaderhandler;
Reader* reader; Reader* reader;
// writer.h // writer.h
Writer<StringBuffer, UTF8<char>, UTF8<char>, CrtAllocator, 0>* writer; Writer<StringBuffer, UTF8<char>, UTF8<char>, CrtAllocator, 0>* writer;
// prettywriter.h // prettywriter.h
PrettyWriter<StringBuffer, UTF8<char>, UTF8<char>, CrtAllocator, 0>* prettywriter; PrettyWriter<StringBuffer, UTF8<char>, UTF8<char>, CrtAllocator, 0>* prettywriter;
// document.h // document.h
Value* value; Value* value;
Document* document; Document* document;
// pointer.h // pointer.h
Pointer* pointer; Pointer* pointer;
// schema.h // schema.h
SchemaDocument* schemadocument; SchemaDocument* schemadocument;
SchemaValidator* schemavalidator; SchemaValidator* schemavalidator;
// char buffer[16]; // char buffer[16];
}; };
// Using type definitions here. // Using type definitions here.
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
#include "rapidjson/filereadstream.h" #include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h" #include "rapidjson/filewritestream.h"
#include "rapidjson/memorybuffer.h" #include "rapidjson/memorybuffer.h"
#include "rapidjson/memorystream.h" #include "rapidjson/memorystream.h"
#include "rapidjson/document.h" // -> reader.h #include "rapidjson/document.h" // -> reader.h
#include "rapidjson/writer.h" #include "rapidjson/writer.h"
#include "rapidjson/prettywriter.h" #include "rapidjson/prettywriter.h"
#include "rapidjson/schema.h" // -> pointer.h #include "rapidjson/schema.h" // -> pointer.h
Foo::Foo() : Foo::Foo() :
// encodings.h // encodings.h
utf8(RAPIDJSON_NEW(UTF8<>)), utf8(RAPIDJSON_NEW(UTF8<>)),
utf16(RAPIDJSON_NEW(UTF16<>)), utf16(RAPIDJSON_NEW(UTF16<>)),
utf16be(RAPIDJSON_NEW(UTF16BE<>)), utf16be(RAPIDJSON_NEW(UTF16BE<>)),
utf16le(RAPIDJSON_NEW(UTF16LE<>)), utf16le(RAPIDJSON_NEW(UTF16LE<>)),
utf32(RAPIDJSON_NEW(UTF32<>)), utf32(RAPIDJSON_NEW(UTF32<>)),
utf32be(RAPIDJSON_NEW(UTF32BE<>)), utf32be(RAPIDJSON_NEW(UTF32BE<>)),
utf32le(RAPIDJSON_NEW(UTF32LE<>)), utf32le(RAPIDJSON_NEW(UTF32LE<>)),
ascii(RAPIDJSON_NEW(ASCII<>)), ascii(RAPIDJSON_NEW(ASCII<>)),
autoutf(RAPIDJSON_NEW(AutoUTF<unsigned>)), autoutf(RAPIDJSON_NEW(AutoUTF<unsigned>)),
transcoder(RAPIDJSON_NEW((Transcoder<UTF8<>, UTF8<> >))), transcoder(RAPIDJSON_NEW((Transcoder<UTF8<>, UTF8<> >))),
// allocators.h // allocators.h
crtallocator(RAPIDJSON_NEW(CrtAllocator)), crtallocator(RAPIDJSON_NEW(CrtAllocator)),
memorypoolallocator(RAPIDJSON_NEW(MemoryPoolAllocator<>)), memorypoolallocator(RAPIDJSON_NEW(MemoryPoolAllocator<>)),
// stream.h // stream.h
stringstream(RAPIDJSON_NEW(StringStream(0))), stringstream(RAPIDJSON_NEW(StringStream(0))),
insitustringstream(RAPIDJSON_NEW(InsituStringStream(0))), insitustringstream(RAPIDJSON_NEW(InsituStringStream(0))),
// stringbuffer.h // stringbuffer.h
stringbuffer(RAPIDJSON_NEW(StringBuffer)), stringbuffer(RAPIDJSON_NEW(StringBuffer)),
// // filereadstream.h // // filereadstream.h
// filereadstream(RAPIDJSON_NEW(FileReadStream(stdout, buffer, sizeof(buffer)))), // filereadstream(RAPIDJSON_NEW(FileReadStream(stdout, buffer, sizeof(buffer)))),
// // filewritestream.h // // filewritestream.h
// filewritestream(RAPIDJSON_NEW(FileWriteStream(stdout, buffer, sizeof(buffer)))), // filewritestream(RAPIDJSON_NEW(FileWriteStream(stdout, buffer, sizeof(buffer)))),
// memorybuffer.h // memorybuffer.h
memorybuffer(RAPIDJSON_NEW(MemoryBuffer)), memorybuffer(RAPIDJSON_NEW(MemoryBuffer)),
// memorystream.h // memorystream.h
memorystream(RAPIDJSON_NEW(MemoryStream(0, 0))), memorystream(RAPIDJSON_NEW(MemoryStream(0, 0))),
// reader.h // reader.h
basereaderhandler(RAPIDJSON_NEW((BaseReaderHandler<UTF8<>, void>))), basereaderhandler(RAPIDJSON_NEW((BaseReaderHandler<UTF8<>, void>))),
reader(RAPIDJSON_NEW(Reader)), reader(RAPIDJSON_NEW(Reader)),
// writer.h // writer.h
writer(RAPIDJSON_NEW((Writer<StringBuffer>))), writer(RAPIDJSON_NEW((Writer<StringBuffer>))),
// prettywriter.h // prettywriter.h
prettywriter(RAPIDJSON_NEW((PrettyWriter<StringBuffer>))), prettywriter(RAPIDJSON_NEW((PrettyWriter<StringBuffer>))),
// document.h // document.h
value(RAPIDJSON_NEW(Value)), value(RAPIDJSON_NEW(Value)),
document(RAPIDJSON_NEW(Document)), document(RAPIDJSON_NEW(Document)),
// pointer.h // pointer.h
pointer(RAPIDJSON_NEW(Pointer)), pointer(RAPIDJSON_NEW(Pointer)),
// schema.h // schema.h
schemadocument(RAPIDJSON_NEW(SchemaDocument(*document))), schemadocument(RAPIDJSON_NEW(SchemaDocument(*document))),
schemavalidator(RAPIDJSON_NEW(SchemaValidator(*schemadocument))) schemavalidator(RAPIDJSON_NEW(SchemaValidator(*schemadocument)))
{ {
} }
Foo::~Foo() { Foo::~Foo() {
// encodings.h // encodings.h
RAPIDJSON_DELETE(utf8); RAPIDJSON_DELETE(utf8);
RAPIDJSON_DELETE(utf16); RAPIDJSON_DELETE(utf16);
RAPIDJSON_DELETE(utf16be); RAPIDJSON_DELETE(utf16be);
RAPIDJSON_DELETE(utf16le); RAPIDJSON_DELETE(utf16le);
RAPIDJSON_DELETE(utf32); RAPIDJSON_DELETE(utf32);
RAPIDJSON_DELETE(utf32be); RAPIDJSON_DELETE(utf32be);
RAPIDJSON_DELETE(utf32le); RAPIDJSON_DELETE(utf32le);
RAPIDJSON_DELETE(ascii); RAPIDJSON_DELETE(ascii);
RAPIDJSON_DELETE(autoutf); RAPIDJSON_DELETE(autoutf);
RAPIDJSON_DELETE(transcoder); RAPIDJSON_DELETE(transcoder);
// allocators.h // allocators.h
RAPIDJSON_DELETE(crtallocator); RAPIDJSON_DELETE(crtallocator);
RAPIDJSON_DELETE(memorypoolallocator); RAPIDJSON_DELETE(memorypoolallocator);
// stream.h // stream.h
RAPIDJSON_DELETE(stringstream); RAPIDJSON_DELETE(stringstream);
RAPIDJSON_DELETE(insitustringstream); RAPIDJSON_DELETE(insitustringstream);
// stringbuffer.h // stringbuffer.h
RAPIDJSON_DELETE(stringbuffer); RAPIDJSON_DELETE(stringbuffer);
// // filereadstream.h // // filereadstream.h
// RAPIDJSON_DELETE(filereadstream); // RAPIDJSON_DELETE(filereadstream);
// // filewritestream.h // // filewritestream.h
// RAPIDJSON_DELETE(filewritestream); // RAPIDJSON_DELETE(filewritestream);
// memorybuffer.h // memorybuffer.h
RAPIDJSON_DELETE(memorybuffer); RAPIDJSON_DELETE(memorybuffer);
// memorystream.h // memorystream.h
RAPIDJSON_DELETE(memorystream); RAPIDJSON_DELETE(memorystream);
// reader.h // reader.h
RAPIDJSON_DELETE(basereaderhandler); RAPIDJSON_DELETE(basereaderhandler);
RAPIDJSON_DELETE(reader); RAPIDJSON_DELETE(reader);
// writer.h // writer.h
RAPIDJSON_DELETE(writer); RAPIDJSON_DELETE(writer);
// prettywriter.h // prettywriter.h
RAPIDJSON_DELETE(prettywriter); RAPIDJSON_DELETE(prettywriter);
// document.h // document.h
RAPIDJSON_DELETE(value); RAPIDJSON_DELETE(value);
RAPIDJSON_DELETE(document); RAPIDJSON_DELETE(document);
// pointer.h // pointer.h
RAPIDJSON_DELETE(pointer); RAPIDJSON_DELETE(pointer);
// schema.h // schema.h
RAPIDJSON_DELETE(schemadocument); RAPIDJSON_DELETE(schemadocument);
RAPIDJSON_DELETE(schemavalidator); RAPIDJSON_DELETE(schemavalidator);
} }
TEST(Fwd, Fwd) { TEST(Fwd, Fwd) {
Foo f; Foo f;
} }
#ifdef __GNUC__ #ifdef __GNUC__
RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP
#endif #endif

View File

@ -1,99 +1,99 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "unittest.h" #include "unittest.h"
#include "rapidjson/document.h" #include "rapidjson/document.h"
using namespace rapidjson; using namespace rapidjson;
static char* ReadFile(const char* filename, size_t& length) { static char* ReadFile(const char* filename, size_t& length) {
const char *paths[] = { const char *paths[] = {
"jsonchecker", "jsonchecker",
"bin/jsonchecker", "bin/jsonchecker",
"../bin/jsonchecker", "../bin/jsonchecker",
"../../bin/jsonchecker", "../../bin/jsonchecker",
"../../../bin/jsonchecker" "../../../bin/jsonchecker"
}; };
char buffer[1024]; char buffer[1024];
FILE *fp = 0; FILE *fp = 0;
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
sprintf(buffer, "%s/%s", paths[i], filename); sprintf(buffer, "%s/%s", paths[i], filename);
fp = fopen(buffer, "rb"); fp = fopen(buffer, "rb");
if (fp) if (fp)
break; break;
} }
if (!fp) if (!fp)
return 0; return 0;
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
length = static_cast<size_t>(ftell(fp)); length = static_cast<size_t>(ftell(fp));
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
char* json = static_cast<char*>(malloc(length + 1)); char* json = static_cast<char*>(malloc(length + 1));
size_t readLength = fread(json, 1, length, fp); size_t readLength = fread(json, 1, length, fp);
json[readLength] = '\0'; json[readLength] = '\0';
fclose(fp); fclose(fp);
return json; return json;
} }
TEST(JsonChecker, Reader) { TEST(JsonChecker, Reader) {
char filename[256]; char filename[256];
// jsonchecker/failXX.json // jsonchecker/failXX.json
for (int i = 1; i <= 33; i++) { for (int i = 1; i <= 33; i++) {
if (i == 1) // fail1.json is valid in rapidjson, which has no limitation on type of root element (RFC 7159). if (i == 1) // fail1.json is valid in rapidjson, which has no limitation on type of root element (RFC 7159).
continue; continue;
if (i == 18) // fail18.json is valid in rapidjson, which has no limitation on depth of nesting. if (i == 18) // fail18.json is valid in rapidjson, which has no limitation on depth of nesting.
continue; continue;
sprintf(filename, "fail%d.json", i); sprintf(filename, "fail%d.json", i);
size_t length; size_t length;
char* json = ReadFile(filename, length); char* json = ReadFile(filename, length);
if (!json) { if (!json) {
printf("jsonchecker file %s not found", filename); printf("jsonchecker file %s not found", filename);
ADD_FAILURE(); ADD_FAILURE();
continue; continue;
} }
GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak) GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
document.Parse(json); document.Parse(json);
EXPECT_TRUE(document.HasParseError()); EXPECT_TRUE(document.HasParseError());
document.Parse<kParseIterativeFlag>(json); document.Parse<kParseIterativeFlag>(json);
EXPECT_TRUE(document.HasParseError()); EXPECT_TRUE(document.HasParseError());
free(json); free(json);
} }
// passX.json // passX.json
for (int i = 1; i <= 3; i++) { for (int i = 1; i <= 3; i++) {
sprintf(filename, "pass%d.json", i); sprintf(filename, "pass%d.json", i);
size_t length; size_t length;
char* json = ReadFile(filename, length); char* json = ReadFile(filename, length);
if (!json) { if (!json) {
printf("jsonchecker file %s not found", filename); printf("jsonchecker file %s not found", filename);
continue; continue;
} }
GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak) GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
document.Parse(json); document.Parse(json);
EXPECT_FALSE(document.HasParseError()); EXPECT_FALSE(document.HasParseError());
document.Parse<kParseIterativeFlag>(json); document.Parse<kParseIterativeFlag>(json);
EXPECT_FALSE(document.HasParseError()); EXPECT_FALSE(document.HasParseError());
free(json); free(json);
} }
} }

View File

@ -1,70 +1,70 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "unittest.h" #include "unittest.h"
// test another instantiation of RapidJSON in a different namespace // test another instantiation of RapidJSON in a different namespace
#define RAPIDJSON_NAMESPACE my::rapid::json #define RAPIDJSON_NAMESPACE my::rapid::json
#define RAPIDJSON_NAMESPACE_BEGIN namespace my { namespace rapid { namespace json { #define RAPIDJSON_NAMESPACE_BEGIN namespace my { namespace rapid { namespace json {
#define RAPIDJSON_NAMESPACE_END } } } #define RAPIDJSON_NAMESPACE_END } } }
// include lots of RapidJSON files // include lots of RapidJSON files
#include "rapidjson/document.h" #include "rapidjson/document.h"
#include "rapidjson/writer.h" #include "rapidjson/writer.h"
#include "rapidjson/filereadstream.h" #include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h" #include "rapidjson/filewritestream.h"
#include "rapidjson/encodedstream.h" #include "rapidjson/encodedstream.h"
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
static const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,4]}"; static const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,4]}";
TEST(NamespaceTest,Using) { TEST(NamespaceTest,Using) {
using namespace RAPIDJSON_NAMESPACE; using namespace RAPIDJSON_NAMESPACE;
typedef GenericDocument<UTF8<>, CrtAllocator> DocumentType; typedef GenericDocument<UTF8<>, CrtAllocator> DocumentType;
DocumentType doc; DocumentType doc;
doc.Parse(json); doc.Parse(json);
EXPECT_TRUE(!doc.HasParseError()); EXPECT_TRUE(!doc.HasParseError());
} }
TEST(NamespaceTest,Direct) { TEST(NamespaceTest,Direct) {
typedef RAPIDJSON_NAMESPACE::Document Document; typedef RAPIDJSON_NAMESPACE::Document Document;
typedef RAPIDJSON_NAMESPACE::Reader Reader; typedef RAPIDJSON_NAMESPACE::Reader Reader;
typedef RAPIDJSON_NAMESPACE::StringStream StringStream; typedef RAPIDJSON_NAMESPACE::StringStream StringStream;
typedef RAPIDJSON_NAMESPACE::StringBuffer StringBuffer; typedef RAPIDJSON_NAMESPACE::StringBuffer StringBuffer;
typedef RAPIDJSON_NAMESPACE::Writer<StringBuffer> WriterType; typedef RAPIDJSON_NAMESPACE::Writer<StringBuffer> WriterType;
StringStream s(json); StringStream s(json);
StringBuffer buffer; StringBuffer buffer;
WriterType writer(buffer); WriterType writer(buffer);
buffer.ShrinkToFit(); buffer.ShrinkToFit();
Reader reader; Reader reader;
reader.Parse(s, writer); reader.Parse(s, writer);
EXPECT_STREQ(json, buffer.GetString()); EXPECT_STREQ(json, buffer.GetString());
EXPECT_EQ(sizeof(json)-1, buffer.GetSize()); EXPECT_EQ(sizeof(json)-1, buffer.GetSize());
EXPECT_TRUE(writer.IsComplete()); EXPECT_TRUE(writer.IsComplete());
Document doc; Document doc;
doc.Parse(buffer.GetString()); doc.Parse(buffer.GetString());
EXPECT_TRUE(!doc.HasParseError()); EXPECT_TRUE(!doc.HasParseError());
buffer.Clear(); buffer.Clear();
writer.Reset(buffer); writer.Reset(buffer);
doc.Accept(writer); doc.Accept(writer);
EXPECT_STREQ(json, buffer.GetString()); EXPECT_STREQ(json, buffer.GetString());
EXPECT_TRUE(writer.IsComplete()); EXPECT_TRUE(writer.IsComplete());
} }

View File

@ -1,50 +1,50 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "unittest.h" #include "unittest.h"
#include "rapidjson/rapidjson.h" #include "rapidjson/rapidjson.h"
#ifdef __clang__ #ifdef __clang__
#pragma GCC diagnostic push #pragma GCC diagnostic push
#if __has_warning("-Wdeprecated") #if __has_warning("-Wdeprecated")
#pragma GCC diagnostic ignored "-Wdeprecated" #pragma GCC diagnostic ignored "-Wdeprecated"
#endif #endif
#endif #endif
AssertException::~AssertException() throw() {} AssertException::~AssertException() throw() {}
#ifdef __clang__ #ifdef __clang__
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
int main(int argc, char **argv) { int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleTest(&argc, argv);
std::cout << "RapidJSON v" << RAPIDJSON_VERSION_STRING << std::endl; std::cout << "RapidJSON v" << RAPIDJSON_VERSION_STRING << std::endl;
#ifdef _MSC_VER #ifdef _MSC_VER
_CrtMemState memoryState = { 0 }; _CrtMemState memoryState = { 0 };
_CrtMemCheckpoint(&memoryState); _CrtMemCheckpoint(&memoryState);
//_CrtSetBreakAlloc(X); //_CrtSetBreakAlloc(X);
//void *testWhetherMemoryLeakDetectionWorks = malloc(1); //void *testWhetherMemoryLeakDetectionWorks = malloc(1);
#endif #endif
int ret = RUN_ALL_TESTS(); int ret = RUN_ALL_TESTS();
#ifdef _MSC_VER #ifdef _MSC_VER
// Current gtest constantly leak 2 blocks at exit // Current gtest constantly leak 2 blocks at exit
_CrtMemDumpAllObjectsSince(&memoryState); _CrtMemDumpAllObjectsSince(&memoryState);
#endif #endif
return ret; return ret;
} }

View File

@ -1,135 +1,135 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#ifndef UNITTEST_H_ #ifndef UNITTEST_H_
#define UNITTEST_H_ #define UNITTEST_H_
// gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS. // gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS.
#ifndef __STDC_CONSTANT_MACROS #ifndef __STDC_CONSTANT_MACROS
#ifdef __clang__ #ifdef __clang__
#pragma GCC diagnostic push #pragma GCC diagnostic push
#if __has_warning("-Wreserved-id-macro") #if __has_warning("-Wreserved-id-macro")
#pragma GCC diagnostic ignored "-Wreserved-id-macro" #pragma GCC diagnostic ignored "-Wreserved-id-macro"
#endif #endif
#endif #endif
# define __STDC_CONSTANT_MACROS 1 // required by C++ standard # define __STDC_CONSTANT_MACROS 1 // required by C++ standard
#ifdef __clang__ #ifdef __clang__
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
#define _CRTDBG_MAP_ALLOC #define _CRTDBG_MAP_ALLOC
#include <crtdbg.h> #include <crtdbg.h>
#pragma warning(disable : 4996) // 'function': was declared deprecated #pragma warning(disable : 4996) // 'function': was declared deprecated
#endif #endif
#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
#if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) #if defined(__clang__) || (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#pragma GCC diagnostic push #pragma GCC diagnostic push
#endif #endif
#pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Weffc++"
#endif #endif
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <stdexcept> #include <stdexcept>
#if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) #if defined(__clang__) || defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
#ifdef __clang__ #ifdef __clang__
// All TEST() macro generated this warning, disable globally // All TEST() macro generated this warning, disable globally
#pragma GCC diagnostic ignored "-Wglobal-constructors" #pragma GCC diagnostic ignored "-Wglobal-constructors"
#endif #endif
template <typename Ch> template <typename Ch>
inline unsigned StrLen(const Ch* s) { inline unsigned StrLen(const Ch* s) {
const Ch* p = s; const Ch* p = s;
while (*p) p++; while (*p) p++;
return unsigned(p - s); return unsigned(p - s);
} }
template<typename Ch> template<typename Ch>
inline int StrCmp(const Ch* s1, const Ch* s2) { inline int StrCmp(const Ch* s1, const Ch* s2) {
while(*s1 && (*s1 == *s2)) { s1++; s2++; } while(*s1 && (*s1 == *s2)) { s1++; s2++; }
return static_cast<unsigned>(*s1) < static_cast<unsigned>(*s2) ? -1 : static_cast<unsigned>(*s1) > static_cast<unsigned>(*s2); return static_cast<unsigned>(*s1) < static_cast<unsigned>(*s2) ? -1 : static_cast<unsigned>(*s1) > static_cast<unsigned>(*s2);
} }
template <typename Ch> template <typename Ch>
inline Ch* StrDup(const Ch* str) { inline Ch* StrDup(const Ch* str) {
size_t bufferSize = sizeof(Ch) * (StrLen(str) + 1); size_t bufferSize = sizeof(Ch) * (StrLen(str) + 1);
Ch* buffer = static_cast<Ch*>(malloc(bufferSize)); Ch* buffer = static_cast<Ch*>(malloc(bufferSize));
memcpy(buffer, str, bufferSize); memcpy(buffer, str, bufferSize);
return buffer; return buffer;
} }
inline FILE* TempFile(char *filename) { inline FILE* TempFile(char *filename) {
#ifdef _MSC_VER #ifdef _MSC_VER
filename = tmpnam(filename); filename = tmpnam(filename);
// For Visual Studio, tmpnam() adds a backslash in front. Remove it. // For Visual Studio, tmpnam() adds a backslash in front. Remove it.
if (filename[0] == '\\') if (filename[0] == '\\')
for (int i = 0; filename[i] != '\0'; i++) for (int i = 0; filename[i] != '\0'; i++)
filename[i] = filename[i + 1]; filename[i] = filename[i + 1];
return fopen(filename, "wb"); return fopen(filename, "wb");
#else #else
strcpy(filename, "/tmp/fileXXXXXX"); strcpy(filename, "/tmp/fileXXXXXX");
int fd = mkstemp(filename); int fd = mkstemp(filename);
return fdopen(fd, "w"); return fdopen(fd, "w");
#endif #endif
} }
// Use exception for catching assert // Use exception for catching assert
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(disable : 4127) #pragma warning(disable : 4127)
#endif #endif
#ifdef __clang__ #ifdef __clang__
#pragma GCC diagnostic push #pragma GCC diagnostic push
#if __has_warning("-Wdeprecated") #if __has_warning("-Wdeprecated")
#pragma GCC diagnostic ignored "-Wdeprecated" #pragma GCC diagnostic ignored "-Wdeprecated"
#endif #endif
#endif #endif
class AssertException : public std::logic_error { class AssertException : public std::logic_error {
public: public:
AssertException(const char* w) : std::logic_error(w) {} AssertException(const char* w) : std::logic_error(w) {}
AssertException(const AssertException& rhs) : std::logic_error(rhs) {} AssertException(const AssertException& rhs) : std::logic_error(rhs) {}
virtual ~AssertException() throw(); virtual ~AssertException() throw();
}; };
#ifdef __clang__ #ifdef __clang__
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
#define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x)) #define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x))
class Random { class Random {
public: public:
Random(unsigned seed = 0) : mSeed(seed) {} Random(unsigned seed = 0) : mSeed(seed) {}
unsigned operator()() { unsigned operator()() {
mSeed = 214013 * mSeed + 2531011; mSeed = 214013 * mSeed + 2531011;
return mSeed; return mSeed;
} }
private: private:
unsigned mSeed; unsigned mSeed;
}; };
#endif // UNITTEST_H_ #endif // UNITTEST_H_

View File

@ -1,441 +1,441 @@
// Tencent is pleased to support the open source community by making RapidJSON available. // Tencent is pleased to support the open source community by making RapidJSON available.
// //
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
// //
// Licensed under the MIT License (the "License"); you may not use this file except // Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at // in compliance with the License. You may obtain a copy of the License at
// //
// http://opensource.org/licenses/MIT // http://opensource.org/licenses/MIT
// //
// Unless required by applicable law or agreed to in writing, software distributed // Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
#include "unittest.h" #include "unittest.h"
#include "rapidjson/document.h" #include "rapidjson/document.h"
#include "rapidjson/reader.h" #include "rapidjson/reader.h"
#include "rapidjson/writer.h" #include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
using namespace rapidjson; using namespace rapidjson;
TEST(Writer, Compact) { TEST(Writer, Compact) {
StringStream s("{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } "); StringStream s("{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } ");
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
buffer.ShrinkToFit(); buffer.ShrinkToFit();
Reader reader; Reader reader;
reader.Parse<0>(s, writer); reader.Parse<0>(s, writer);
EXPECT_STREQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3]}", buffer.GetString()); EXPECT_STREQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3]}", buffer.GetString());
EXPECT_EQ(77u, buffer.GetSize()); EXPECT_EQ(77u, buffer.GetSize());
EXPECT_TRUE(writer.IsComplete()); EXPECT_TRUE(writer.IsComplete());
} }
// json -> parse -> writer -> json // json -> parse -> writer -> json
#define TEST_ROUNDTRIP(json) \ #define TEST_ROUNDTRIP(json) \
{ \ { \
StringStream s(json); \ StringStream s(json); \
StringBuffer buffer; \ StringBuffer buffer; \
Writer<StringBuffer> writer(buffer); \ Writer<StringBuffer> writer(buffer); \
Reader reader; \ Reader reader; \
reader.Parse<kParseFullPrecisionFlag>(s, writer); \ reader.Parse<kParseFullPrecisionFlag>(s, writer); \
EXPECT_STREQ(json, buffer.GetString()); \ EXPECT_STREQ(json, buffer.GetString()); \
EXPECT_TRUE(writer.IsComplete()); \ EXPECT_TRUE(writer.IsComplete()); \
} }
TEST(Writer, Root) { TEST(Writer, Root) {
TEST_ROUNDTRIP("null"); TEST_ROUNDTRIP("null");
TEST_ROUNDTRIP("true"); TEST_ROUNDTRIP("true");
TEST_ROUNDTRIP("false"); TEST_ROUNDTRIP("false");
TEST_ROUNDTRIP("0"); TEST_ROUNDTRIP("0");
TEST_ROUNDTRIP("\"foo\""); TEST_ROUNDTRIP("\"foo\"");
TEST_ROUNDTRIP("[]"); TEST_ROUNDTRIP("[]");
TEST_ROUNDTRIP("{}"); TEST_ROUNDTRIP("{}");
} }
TEST(Writer, Int) { TEST(Writer, Int) {
TEST_ROUNDTRIP("[-1]"); TEST_ROUNDTRIP("[-1]");
TEST_ROUNDTRIP("[-123]"); TEST_ROUNDTRIP("[-123]");
TEST_ROUNDTRIP("[-2147483648]"); TEST_ROUNDTRIP("[-2147483648]");
} }
TEST(Writer, UInt) { TEST(Writer, UInt) {
TEST_ROUNDTRIP("[0]"); TEST_ROUNDTRIP("[0]");
TEST_ROUNDTRIP("[1]"); TEST_ROUNDTRIP("[1]");
TEST_ROUNDTRIP("[123]"); TEST_ROUNDTRIP("[123]");
TEST_ROUNDTRIP("[2147483647]"); TEST_ROUNDTRIP("[2147483647]");
TEST_ROUNDTRIP("[4294967295]"); TEST_ROUNDTRIP("[4294967295]");
} }
TEST(Writer, Int64) { TEST(Writer, Int64) {
TEST_ROUNDTRIP("[-1234567890123456789]"); TEST_ROUNDTRIP("[-1234567890123456789]");
TEST_ROUNDTRIP("[-9223372036854775808]"); TEST_ROUNDTRIP("[-9223372036854775808]");
} }
TEST(Writer, Uint64) { TEST(Writer, Uint64) {
TEST_ROUNDTRIP("[1234567890123456789]"); TEST_ROUNDTRIP("[1234567890123456789]");
TEST_ROUNDTRIP("[9223372036854775807]"); TEST_ROUNDTRIP("[9223372036854775807]");
} }
TEST(Writer, String) { TEST(Writer, String) {
TEST_ROUNDTRIP("[\"Hello\"]"); TEST_ROUNDTRIP("[\"Hello\"]");
TEST_ROUNDTRIP("[\"Hello\\u0000World\"]"); TEST_ROUNDTRIP("[\"Hello\\u0000World\"]");
TEST_ROUNDTRIP("[\"\\\"\\\\/\\b\\f\\n\\r\\t\"]"); TEST_ROUNDTRIP("[\"\\\"\\\\/\\b\\f\\n\\r\\t\"]");
#if RAPIDJSON_HAS_STDSTRING #if RAPIDJSON_HAS_STDSTRING
{ {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
writer.String(std::string("Hello\n")); writer.String(std::string("Hello\n"));
EXPECT_STREQ("\"Hello\\n\"", buffer.GetString()); EXPECT_STREQ("\"Hello\\n\"", buffer.GetString());
} }
#endif #endif
} }
TEST(Writer, Double) { TEST(Writer, Double) {
TEST_ROUNDTRIP("[1.2345,1.2345678,0.123456789012,1234567.8]"); TEST_ROUNDTRIP("[1.2345,1.2345678,0.123456789012,1234567.8]");
TEST_ROUNDTRIP("0.0"); TEST_ROUNDTRIP("0.0");
TEST_ROUNDTRIP("-0.0"); // Issue #289 TEST_ROUNDTRIP("-0.0"); // Issue #289
TEST_ROUNDTRIP("1e30"); TEST_ROUNDTRIP("1e30");
TEST_ROUNDTRIP("1.0"); TEST_ROUNDTRIP("1.0");
TEST_ROUNDTRIP("5e-324"); // Min subnormal positive double TEST_ROUNDTRIP("5e-324"); // Min subnormal positive double
TEST_ROUNDTRIP("2.225073858507201e-308"); // Max subnormal positive double TEST_ROUNDTRIP("2.225073858507201e-308"); // Max subnormal positive double
TEST_ROUNDTRIP("2.2250738585072014e-308"); // Min normal positive double TEST_ROUNDTRIP("2.2250738585072014e-308"); // Min normal positive double
TEST_ROUNDTRIP("1.7976931348623157e308"); // Max double TEST_ROUNDTRIP("1.7976931348623157e308"); // Max double
} }
TEST(Writer, Transcode) { TEST(Writer, Transcode) {
const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"dollar\":\"\x24\",\"cents\":\"\xC2\xA2\",\"euro\":\"\xE2\x82\xAC\",\"gclef\":\"\xF0\x9D\x84\x9E\"}"; const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"dollar\":\"\x24\",\"cents\":\"\xC2\xA2\",\"euro\":\"\xE2\x82\xAC\",\"gclef\":\"\xF0\x9D\x84\x9E\"}";
// UTF8 -> UTF16 -> UTF8 // UTF8 -> UTF16 -> UTF8
{ {
StringStream s(json); StringStream s(json);
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer, UTF16<>, UTF8<> > writer(buffer); Writer<StringBuffer, UTF16<>, UTF8<> > writer(buffer);
GenericReader<UTF8<>, UTF16<> > reader; GenericReader<UTF8<>, UTF16<> > reader;
reader.Parse(s, writer); reader.Parse(s, writer);
EXPECT_STREQ(json, buffer.GetString()); EXPECT_STREQ(json, buffer.GetString());
} }
// UTF8 -> UTF8 -> ASCII -> UTF8 -> UTF8 // UTF8 -> UTF8 -> ASCII -> UTF8 -> UTF8
{ {
StringStream s(json); StringStream s(json);
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer, UTF8<>, ASCII<> > writer(buffer); Writer<StringBuffer, UTF8<>, ASCII<> > writer(buffer);
Reader reader; Reader reader;
reader.Parse(s, writer); reader.Parse(s, writer);
StringBuffer buffer2; StringBuffer buffer2;
Writer<StringBuffer> writer2(buffer2); Writer<StringBuffer> writer2(buffer2);
GenericReader<ASCII<>, UTF8<> > reader2; GenericReader<ASCII<>, UTF8<> > reader2;
StringStream s2(buffer.GetString()); StringStream s2(buffer.GetString());
reader2.Parse(s2, writer2); reader2.Parse(s2, writer2);
EXPECT_STREQ(json, buffer2.GetString()); EXPECT_STREQ(json, buffer2.GetString());
} }
} }
#include <sstream> #include <sstream>
class OStreamWrapper { class OStreamWrapper {
public: public:
typedef char Ch; typedef char Ch;
OStreamWrapper(std::ostream& os) : os_(os) {} OStreamWrapper(std::ostream& os) : os_(os) {}
Ch Peek() const { assert(false); return '\0'; } Ch Peek() const { assert(false); return '\0'; }
Ch Take() { assert(false); return '\0'; } Ch Take() { assert(false); return '\0'; }
size_t Tell() const { return 0; } size_t Tell() const { return 0; }
Ch* PutBegin() { assert(false); return 0; } Ch* PutBegin() { assert(false); return 0; }
void Put(Ch c) { os_.put(c); } void Put(Ch c) { os_.put(c); }
void Flush() { os_.flush(); } void Flush() { os_.flush(); }
size_t PutEnd(Ch*) { assert(false); return 0; } size_t PutEnd(Ch*) { assert(false); return 0; }
private: private:
OStreamWrapper(const OStreamWrapper&); OStreamWrapper(const OStreamWrapper&);
OStreamWrapper& operator=(const OStreamWrapper&); OStreamWrapper& operator=(const OStreamWrapper&);
std::ostream& os_; std::ostream& os_;
}; };
TEST(Writer, OStreamWrapper) { TEST(Writer, OStreamWrapper) {
StringStream s("{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3], \"u64\": 1234567890123456789, \"i64\":-1234567890123456789 } "); StringStream s("{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3], \"u64\": 1234567890123456789, \"i64\":-1234567890123456789 } ");
std::stringstream ss; std::stringstream ss;
OStreamWrapper os(ss); OStreamWrapper os(ss);
Writer<OStreamWrapper> writer(os); Writer<OStreamWrapper> writer(os);
Reader reader; Reader reader;
reader.Parse<0>(s, writer); reader.Parse<0>(s, writer);
std::string actual = ss.str(); std::string actual = ss.str();
EXPECT_STREQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}", actual.c_str()); EXPECT_STREQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}", actual.c_str());
} }
TEST(Writer, AssertRootMayBeAnyValue) { TEST(Writer, AssertRootMayBeAnyValue) {
#define T(x)\ #define T(x)\
{\ {\
StringBuffer buffer;\ StringBuffer buffer;\
Writer<StringBuffer> writer(buffer);\ Writer<StringBuffer> writer(buffer);\
EXPECT_TRUE(x);\ EXPECT_TRUE(x);\
} }
T(writer.Bool(false)); T(writer.Bool(false));
T(writer.Bool(true)); T(writer.Bool(true));
T(writer.Null()); T(writer.Null());
T(writer.Int(0)); T(writer.Int(0));
T(writer.Uint(0)); T(writer.Uint(0));
T(writer.Int64(0)); T(writer.Int64(0));
T(writer.Uint64(0)); T(writer.Uint64(0));
T(writer.Double(0)); T(writer.Double(0));
T(writer.String("foo")); T(writer.String("foo"));
#undef T #undef T
} }
TEST(Writer, AssertIncorrectObjectLevel) { TEST(Writer, AssertIncorrectObjectLevel) {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
writer.StartObject(); writer.StartObject();
writer.EndObject(); writer.EndObject();
ASSERT_THROW(writer.EndObject(), AssertException); ASSERT_THROW(writer.EndObject(), AssertException);
} }
TEST(Writer, AssertIncorrectArrayLevel) { TEST(Writer, AssertIncorrectArrayLevel) {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
writer.StartArray(); writer.StartArray();
writer.EndArray(); writer.EndArray();
ASSERT_THROW(writer.EndArray(), AssertException); ASSERT_THROW(writer.EndArray(), AssertException);
} }
TEST(Writer, AssertIncorrectEndObject) { TEST(Writer, AssertIncorrectEndObject) {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
writer.StartObject(); writer.StartObject();
ASSERT_THROW(writer.EndArray(), AssertException); ASSERT_THROW(writer.EndArray(), AssertException);
} }
TEST(Writer, AssertIncorrectEndArray) { TEST(Writer, AssertIncorrectEndArray) {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
writer.StartObject(); writer.StartObject();
ASSERT_THROW(writer.EndArray(), AssertException); ASSERT_THROW(writer.EndArray(), AssertException);
} }
TEST(Writer, AssertObjectKeyNotString) { TEST(Writer, AssertObjectKeyNotString) {
#define T(x)\ #define T(x)\
{\ {\
StringBuffer buffer;\ StringBuffer buffer;\
Writer<StringBuffer> writer(buffer);\ Writer<StringBuffer> writer(buffer);\
writer.StartObject();\ writer.StartObject();\
ASSERT_THROW(x, AssertException); \ ASSERT_THROW(x, AssertException); \
} }
T(writer.Bool(false)); T(writer.Bool(false));
T(writer.Bool(true)); T(writer.Bool(true));
T(writer.Null()); T(writer.Null());
T(writer.Int(0)); T(writer.Int(0));
T(writer.Uint(0)); T(writer.Uint(0));
T(writer.Int64(0)); T(writer.Int64(0));
T(writer.Uint64(0)); T(writer.Uint64(0));
T(writer.Double(0)); T(writer.Double(0));
T(writer.StartObject()); T(writer.StartObject());
T(writer.StartArray()); T(writer.StartArray());
#undef T #undef T
} }
TEST(Writer, AssertMultipleRoot) { TEST(Writer, AssertMultipleRoot) {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
writer.StartObject(); writer.StartObject();
writer.EndObject(); writer.EndObject();
ASSERT_THROW(writer.StartObject(), AssertException); ASSERT_THROW(writer.StartObject(), AssertException);
writer.Reset(buffer); writer.Reset(buffer);
writer.Null(); writer.Null();
ASSERT_THROW(writer.Int(0), AssertException); ASSERT_THROW(writer.Int(0), AssertException);
writer.Reset(buffer); writer.Reset(buffer);
writer.String("foo"); writer.String("foo");
ASSERT_THROW(writer.StartArray(), AssertException); ASSERT_THROW(writer.StartArray(), AssertException);
writer.Reset(buffer); writer.Reset(buffer);
writer.StartArray(); writer.StartArray();
writer.EndArray(); writer.EndArray();
//ASSERT_THROW(writer.Double(3.14), AssertException); //ASSERT_THROW(writer.Double(3.14), AssertException);
} }
TEST(Writer, RootObjectIsComplete) { TEST(Writer, RootObjectIsComplete) {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
EXPECT_FALSE(writer.IsComplete()); EXPECT_FALSE(writer.IsComplete());
writer.StartObject(); writer.StartObject();
EXPECT_FALSE(writer.IsComplete()); EXPECT_FALSE(writer.IsComplete());
writer.String("foo"); writer.String("foo");
EXPECT_FALSE(writer.IsComplete()); EXPECT_FALSE(writer.IsComplete());
writer.Int(1); writer.Int(1);
EXPECT_FALSE(writer.IsComplete()); EXPECT_FALSE(writer.IsComplete());
writer.EndObject(); writer.EndObject();
EXPECT_TRUE(writer.IsComplete()); EXPECT_TRUE(writer.IsComplete());
} }
TEST(Writer, RootArrayIsComplete) { TEST(Writer, RootArrayIsComplete) {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
EXPECT_FALSE(writer.IsComplete()); EXPECT_FALSE(writer.IsComplete());
writer.StartArray(); writer.StartArray();
EXPECT_FALSE(writer.IsComplete()); EXPECT_FALSE(writer.IsComplete());
writer.String("foo"); writer.String("foo");
EXPECT_FALSE(writer.IsComplete()); EXPECT_FALSE(writer.IsComplete());
writer.Int(1); writer.Int(1);
EXPECT_FALSE(writer.IsComplete()); EXPECT_FALSE(writer.IsComplete());
writer.EndArray(); writer.EndArray();
EXPECT_TRUE(writer.IsComplete()); EXPECT_TRUE(writer.IsComplete());
} }
TEST(Writer, RootValueIsComplete) { TEST(Writer, RootValueIsComplete) {
#define T(x)\ #define T(x)\
{\ {\
StringBuffer buffer;\ StringBuffer buffer;\
Writer<StringBuffer> writer(buffer);\ Writer<StringBuffer> writer(buffer);\
EXPECT_FALSE(writer.IsComplete()); \ EXPECT_FALSE(writer.IsComplete()); \
x; \ x; \
EXPECT_TRUE(writer.IsComplete()); \ EXPECT_TRUE(writer.IsComplete()); \
} }
T(writer.Null()); T(writer.Null());
T(writer.Bool(true)); T(writer.Bool(true));
T(writer.Bool(false)); T(writer.Bool(false));
T(writer.Int(0)); T(writer.Int(0));
T(writer.Uint(0)); T(writer.Uint(0));
T(writer.Int64(0)); T(writer.Int64(0));
T(writer.Uint64(0)); T(writer.Uint64(0));
T(writer.Double(0)); T(writer.Double(0));
T(writer.String("")); T(writer.String(""));
#undef T #undef T
} }
TEST(Writer, InvalidEncoding) { TEST(Writer, InvalidEncoding) {
// Fail in decoding invalid UTF-8 sequence http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt // Fail in decoding invalid UTF-8 sequence http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
{ {
GenericStringBuffer<UTF16<> > buffer; GenericStringBuffer<UTF16<> > buffer;
Writer<GenericStringBuffer<UTF16<> >, UTF8<>, UTF16<> > writer(buffer); Writer<GenericStringBuffer<UTF16<> >, UTF8<>, UTF16<> > writer(buffer);
writer.StartArray(); writer.StartArray();
EXPECT_FALSE(writer.String("\xfe")); EXPECT_FALSE(writer.String("\xfe"));
EXPECT_FALSE(writer.String("\xff")); EXPECT_FALSE(writer.String("\xff"));
EXPECT_FALSE(writer.String("\xfe\xfe\xff\xff")); EXPECT_FALSE(writer.String("\xfe\xfe\xff\xff"));
writer.EndArray(); writer.EndArray();
} }
// Fail in encoding // Fail in encoding
{ {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer, UTF32<> > writer(buffer); Writer<StringBuffer, UTF32<> > writer(buffer);
static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF
EXPECT_FALSE(writer.String(s)); EXPECT_FALSE(writer.String(s));
} }
// Fail in unicode escaping in ASCII output // Fail in unicode escaping in ASCII output
{ {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer, UTF32<>, ASCII<> > writer(buffer); Writer<StringBuffer, UTF32<>, ASCII<> > writer(buffer);
static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF
EXPECT_FALSE(writer.String(s)); EXPECT_FALSE(writer.String(s));
} }
} }
TEST(Writer, ValidateEncoding) { TEST(Writer, ValidateEncoding) {
{ {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteValidateEncodingFlag> writer(buffer); Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteValidateEncodingFlag> writer(buffer);
writer.StartArray(); writer.StartArray();
EXPECT_TRUE(writer.String("\x24")); // Dollar sign U+0024 EXPECT_TRUE(writer.String("\x24")); // Dollar sign U+0024
EXPECT_TRUE(writer.String("\xC2\xA2")); // Cents sign U+00A2 EXPECT_TRUE(writer.String("\xC2\xA2")); // Cents sign U+00A2
EXPECT_TRUE(writer.String("\xE2\x82\xAC")); // Euro sign U+20AC EXPECT_TRUE(writer.String("\xE2\x82\xAC")); // Euro sign U+20AC
EXPECT_TRUE(writer.String("\xF0\x9D\x84\x9E")); // G clef sign U+1D11E EXPECT_TRUE(writer.String("\xF0\x9D\x84\x9E")); // G clef sign U+1D11E
writer.EndArray(); writer.EndArray();
EXPECT_STREQ("[\"\x24\",\"\xC2\xA2\",\"\xE2\x82\xAC\",\"\xF0\x9D\x84\x9E\"]", buffer.GetString()); EXPECT_STREQ("[\"\x24\",\"\xC2\xA2\",\"\xE2\x82\xAC\",\"\xF0\x9D\x84\x9E\"]", buffer.GetString());
} }
// Fail in decoding invalid UTF-8 sequence http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt // Fail in decoding invalid UTF-8 sequence http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
{ {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteValidateEncodingFlag> writer(buffer); Writer<StringBuffer, UTF8<>, UTF8<>, CrtAllocator, kWriteValidateEncodingFlag> writer(buffer);
writer.StartArray(); writer.StartArray();
EXPECT_FALSE(writer.String("\xfe")); EXPECT_FALSE(writer.String("\xfe"));
EXPECT_FALSE(writer.String("\xff")); EXPECT_FALSE(writer.String("\xff"));
EXPECT_FALSE(writer.String("\xfe\xfe\xff\xff")); EXPECT_FALSE(writer.String("\xfe\xfe\xff\xff"));
writer.EndArray(); writer.EndArray();
} }
} }
TEST(Writer, InvalidEventSequence) { TEST(Writer, InvalidEventSequence) {
// {] // {]
{ {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
writer.StartObject(); writer.StartObject();
EXPECT_THROW(writer.EndArray(), AssertException); EXPECT_THROW(writer.EndArray(), AssertException);
EXPECT_FALSE(writer.IsComplete()); EXPECT_FALSE(writer.IsComplete());
} }
// [} // [}
{ {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
writer.StartArray(); writer.StartArray();
EXPECT_THROW(writer.EndObject(), AssertException); EXPECT_THROW(writer.EndObject(), AssertException);
EXPECT_FALSE(writer.IsComplete()); EXPECT_FALSE(writer.IsComplete());
} }
// { 1: // { 1:
{ {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
writer.StartObject(); writer.StartObject();
EXPECT_THROW(writer.Int(1), AssertException); EXPECT_THROW(writer.Int(1), AssertException);
EXPECT_FALSE(writer.IsComplete()); EXPECT_FALSE(writer.IsComplete());
} }
} }
extern double zero; // clang -Wmissing-variable-declarations extern double zero; // clang -Wmissing-variable-declarations
double zero = 0.0; // Use global variable to prevent compiler warning double zero = 0.0; // Use global variable to prevent compiler warning
TEST(Writer, NaN) { TEST(Writer, NaN) {
double nan = zero / zero; double nan = zero / zero;
EXPECT_TRUE(internal::Double(nan).IsNan()); EXPECT_TRUE(internal::Double(nan).IsNan());
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
EXPECT_FALSE(writer.Double(nan)); EXPECT_FALSE(writer.Double(nan));
} }
TEST(Writer, Inf) { TEST(Writer, Inf) {
double inf = 1.0 / zero; double inf = 1.0 / zero;
EXPECT_TRUE(internal::Double(inf).IsInf()); EXPECT_TRUE(internal::Double(inf).IsInf());
StringBuffer buffer; StringBuffer buffer;
{ {
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
EXPECT_FALSE(writer.Double(inf)); EXPECT_FALSE(writer.Double(inf));
} }
{ {
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
EXPECT_FALSE(writer.Double(-inf)); EXPECT_FALSE(writer.Double(-inf));
} }
} }
TEST(Writer, RawValue) { TEST(Writer, RawValue) {
StringBuffer buffer; StringBuffer buffer;
Writer<StringBuffer> writer(buffer); Writer<StringBuffer> writer(buffer);
writer.StartObject(); writer.StartObject();
writer.Key("a"); writer.Key("a");
writer.Int(1); writer.Int(1);
writer.Key("raw"); writer.Key("raw");
const char json[] = "[\"Hello\\nWorld\", 123.456]"; const char json[] = "[\"Hello\\nWorld\", 123.456]";
writer.RawValue(json, strlen(json), kArrayType); writer.RawValue(json, strlen(json), kArrayType);
writer.EndObject(); writer.EndObject();
EXPECT_TRUE(writer.IsComplete()); EXPECT_TRUE(writer.IsComplete());
EXPECT_STREQ("{\"a\":1,\"raw\":[\"Hello\\nWorld\", 123.456]}", buffer.GetString()); EXPECT_STREQ("{\"a\":1,\"raw\":[\"Hello\\nWorld\", 123.456]}", buffer.GetString());
} }