From 57ad051f67b169cd35d6c4375e560a935e131858 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Mon, 2 Mar 2015 12:10:35 -0600 Subject: [PATCH] allow length in CZString --- include/json/value.h | 10 +++++++++- src/lib_json/json_value.cpp | 20 ++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/include/json/value.h b/include/json/value.h index f8fe824..439f05d 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -176,8 +176,16 @@ private: private: void swap(CZString& other); + struct StringStorage { + DuplicationPolicy policy_: 2; + unsigned length_: 30; // 1GB max + }; + const char* cstr_; - ArrayIndex index_; + union { + ArrayIndex index_; + StringStorage storage_; + }; }; public: diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 24f8f6d..05f2c87 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -156,26 +156,30 @@ void Value::CommentInfo::setComment(const char* text, size_t len) { // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// -// Notes: index_ indicates if the string was allocated when +// Notes: policy_ indicates if the string was allocated when // a string is stored. +// +// TODO: Check for length > 1GB, in Reader. Value::CZString::CZString(ArrayIndex index) : cstr_(0), index_(index) {} Value::CZString::CZString(const char* cstr, DuplicationPolicy allocate) : cstr_(allocate == duplicate ? duplicateStringValue(cstr) : cstr), - index_(allocate) {} + storage_({allocate, 0}) +{} Value::CZString::CZString(const CZString& other) - : cstr_(other.index_ != noDuplication && other.cstr_ != 0 + : cstr_(other.storage_.policy_ != noDuplication && other.cstr_ != 0 ? duplicateStringValue(other.cstr_) : other.cstr_), - index_(other.cstr_ - ? static_cast(other.index_ == noDuplication + storage_({(other.cstr_ + ? (other.storage_.policy_ == noDuplication ? noDuplication : duplicate) - : other.index_) {} + : other.storage_.policy_), 0}) +{} Value::CZString::~CZString() { - if (cstr_ && index_ == duplicate) + if (cstr_ && storage_.policy_ == duplicate) releaseStringValue(const_cast(cstr_)); } @@ -217,7 +221,7 @@ ArrayIndex Value::CZString::index() const { return index_; } const char* Value::CZString::c_str() const { return cstr_; } -bool Value::CZString::isStaticString() const { return index_ == noDuplication; } +bool Value::CZString::isStaticString() const { return storage_.policy_ == noDuplication; } // ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////