Skip to content

Commit

Permalink
allow length in CZString
Browse files Browse the repository at this point in the history
  • Loading branch information
cdunn2001 committed Mar 3, 2015
1 parent b383fdc commit 57ad051
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
10 changes: 9 additions & 1 deletion include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,16 @@ class JSON_API Value {

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:
Expand Down
20 changes: 12 additions & 8 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArrayIndex>(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<char*>(cstr_));
}

Expand Down Expand Up @@ -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; }

// //////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 57ad051

Please sign in to comment.