mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-06-12 02:52:13 +02:00
Merge pull request #195 from cdunn2001/len-in-czstring
Store length in CZString
This commit is contained in:
commit
2b9abc3ebf
@ -164,7 +164,7 @@ private:
|
|||||||
duplicateOnCopy
|
duplicateOnCopy
|
||||||
};
|
};
|
||||||
CZString(ArrayIndex index);
|
CZString(ArrayIndex index);
|
||||||
CZString(const char* cstr, DuplicationPolicy allocate);
|
CZString(char const* cstr, unsigned length, DuplicationPolicy allocate);
|
||||||
CZString(const CZString& other);
|
CZString(const CZString& other);
|
||||||
~CZString();
|
~CZString();
|
||||||
CZString& operator=(CZString other);
|
CZString& operator=(CZString other);
|
||||||
@ -176,8 +176,16 @@ private:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void swap(CZString& other);
|
void swap(CZString& other);
|
||||||
|
struct StringStorage {
|
||||||
|
DuplicationPolicy policy_: 2;
|
||||||
|
unsigned length_: 30; // 1GB max
|
||||||
|
};
|
||||||
|
|
||||||
const char* cstr_;
|
const char* cstr_;
|
||||||
ArrayIndex index_;
|
union {
|
||||||
|
ArrayIndex index_;
|
||||||
|
StringStorage storage_;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -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.
|
// a string is stored.
|
||||||
|
//
|
||||||
|
// TODO: Check for length > 1GB, in Reader.
|
||||||
|
|
||||||
Value::CZString::CZString(ArrayIndex index) : cstr_(0), index_(index) {}
|
Value::CZString::CZString(ArrayIndex index) : cstr_(0), index_(index) {}
|
||||||
|
|
||||||
Value::CZString::CZString(const char* cstr, DuplicationPolicy allocate)
|
Value::CZString::CZString(char const* str, unsigned length, DuplicationPolicy allocate)
|
||||||
: cstr_(allocate == duplicate ? duplicateStringValue(cstr) : cstr),
|
: cstr_(allocate == duplicate ? duplicateStringValue(str) : str),
|
||||||
index_(allocate) {}
|
storage_({allocate, length})
|
||||||
|
{}
|
||||||
|
|
||||||
Value::CZString::CZString(const CZString& other)
|
Value::CZString::CZString(const CZString& other)
|
||||||
: cstr_(other.index_ != noDuplication && other.cstr_ != 0
|
: cstr_(other.storage_.policy_ != noDuplication && other.cstr_ != 0
|
||||||
? duplicateStringValue(other.cstr_)
|
? duplicateStringValue(other.cstr_)
|
||||||
: other.cstr_),
|
: other.cstr_),
|
||||||
index_(other.cstr_
|
storage_({(other.cstr_
|
||||||
? static_cast<ArrayIndex>(other.index_ == noDuplication
|
? (other.storage_.policy_ == noDuplication
|
||||||
? noDuplication : duplicate)
|
? noDuplication : duplicate)
|
||||||
: other.index_) {}
|
: other.storage_.policy_), other.storage_.length_})
|
||||||
|
{}
|
||||||
|
|
||||||
Value::CZString::~CZString() {
|
Value::CZString::~CZString() {
|
||||||
if (cstr_ && index_ == duplicate)
|
if (cstr_ && storage_.policy_ == duplicate)
|
||||||
releaseStringValue(const_cast<char*>(cstr_));
|
releaseStringValue(const_cast<char*>(cstr_));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,22 +194,34 @@ Value::CZString& Value::CZString::operator=(CZString other) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Value::CZString::operator<(const CZString& other) const {
|
bool Value::CZString::operator<(const CZString& other) const {
|
||||||
if (cstr_)
|
if (!cstr_) return index_ < other.index_;
|
||||||
return strcmp(cstr_, other.cstr_) < 0;
|
//return strcmp(cstr_, other.cstr_) < 0;
|
||||||
return index_ < other.index_;
|
// Assume both are strings.
|
||||||
|
unsigned this_len = this->storage_.length_;
|
||||||
|
unsigned other_len = other.storage_.length_;
|
||||||
|
unsigned min_len = std::min(this_len, other_len);
|
||||||
|
int comp = memcmp(this->cstr_, other.cstr_, min_len);
|
||||||
|
if (comp < 0) return true;
|
||||||
|
if (comp > 0) return false;
|
||||||
|
return (this_len < other_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Value::CZString::operator==(const CZString& other) const {
|
bool Value::CZString::operator==(const CZString& other) const {
|
||||||
if (cstr_)
|
if (!cstr_) return index_ == other.index_;
|
||||||
return strcmp(cstr_, other.cstr_) == 0;
|
//return strcmp(cstr_, other.cstr_) == 0;
|
||||||
return index_ == other.index_;
|
// Assume both are strings.
|
||||||
|
unsigned this_len = this->storage_.length_;
|
||||||
|
unsigned other_len = other.storage_.length_;
|
||||||
|
if (this_len != other_len) return false;
|
||||||
|
int comp = memcmp(this->cstr_, other.cstr_, this_len);
|
||||||
|
return comp == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayIndex Value::CZString::index() const { return index_; }
|
ArrayIndex Value::CZString::index() const { return index_; }
|
||||||
|
|
||||||
const char* Value::CZString::c_str() const { return cstr_; }
|
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; }
|
||||||
|
|
||||||
// //////////////////////////////////////////////////////////////////
|
// //////////////////////////////////////////////////////////////////
|
||||||
// //////////////////////////////////////////////////////////////////
|
// //////////////////////////////////////////////////////////////////
|
||||||
@ -833,7 +849,7 @@ Value& Value::resolveReference(const char* key, bool isStatic) {
|
|||||||
if (type_ == nullValue)
|
if (type_ == nullValue)
|
||||||
*this = Value(objectValue);
|
*this = Value(objectValue);
|
||||||
CZString actualKey(
|
CZString actualKey(
|
||||||
key, isStatic ? CZString::noDuplication : CZString::duplicateOnCopy);
|
key, strlen(key), isStatic ? CZString::noDuplication : CZString::duplicateOnCopy);
|
||||||
ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
|
ObjectValues::iterator it = value_.map_->lower_bound(actualKey);
|
||||||
if (it != value_.map_->end() && (*it).first == actualKey)
|
if (it != value_.map_->end() && (*it).first == actualKey)
|
||||||
return (*it).second;
|
return (*it).second;
|
||||||
@ -857,7 +873,7 @@ const Value& Value::operator[](const char* key) const {
|
|||||||
"in Json::Value::operator[](char const*)const: requires objectValue");
|
"in Json::Value::operator[](char const*)const: requires objectValue");
|
||||||
if (type_ == nullValue)
|
if (type_ == nullValue)
|
||||||
return null;
|
return null;
|
||||||
CZString actualKey(key, CZString::noDuplication);
|
CZString actualKey(key, strlen(key), CZString::noDuplication);
|
||||||
ObjectValues::const_iterator it = value_.map_->find(actualKey);
|
ObjectValues::const_iterator it = value_.map_->find(actualKey);
|
||||||
if (it == value_.map_->end())
|
if (it == value_.map_->end())
|
||||||
return null;
|
return null;
|
||||||
@ -902,7 +918,7 @@ bool Value::removeMember(const char* key, Value* removed) {
|
|||||||
if (type_ != objectValue) {
|
if (type_ != objectValue) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
CZString actualKey(key, CZString::noDuplication);
|
CZString actualKey(key, strlen(key), CZString::noDuplication);
|
||||||
ObjectValues::iterator it = value_.map_->find(actualKey);
|
ObjectValues::iterator it = value_.map_->find(actualKey);
|
||||||
if (it == value_.map_->end())
|
if (it == value_.map_->end())
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user