1 #ifndef JSON_SHARED_STRING_H
2 #define JSON_SHARED_STRING_H
14 #include "JSONDebug.h"
15 #include "JSONGlobals.h"
16 #include "JSONMemory.h"
27 #ifdef JSON_LESS_MEMORY
31 #pragma pack(push, json_shared_string_pack, 1)
35 class json_shared_string {
40 struct const_iterator {
41 const_iterator(
const json_char * p,
const json_shared_string * pa) : parent(pa), it(p){}
43 inline const_iterator& operator ++(
void) json_nothrow { ++it;
return *
this; }
44 inline const_iterator& operator --(
void) json_nothrow { --it;
return *
this; }
45 inline const_iterator& operator +=(
long i) json_nothrow { it += i;
return *
this; }
46 inline const_iterator& operator -=(
long i) json_nothrow { it -= i;
return *
this; }
47 inline const_iterator operator ++(
int) json_nothrow {
48 const_iterator result(*
this);
52 inline const_iterator operator --(
int) json_nothrow {
53 const_iterator result(*
this);
57 inline const_iterator operator +(
long i)
const json_nothrow {
58 const_iterator result(*
this);
62 inline const_iterator operator -(
long i)
const json_nothrow {
63 const_iterator result(*
this);
67 inline const json_char & operator [](
size_t pos)
const json_nothrow {
return it[pos]; };
68 inline const json_char & operator *(
void) const json_nothrow {
return *it; }
69 inline const json_char * operator ->(
void) const json_nothrow {
return it; }
70 inline bool operator == (
const const_iterator & other)
const json_nothrow {
return it == other.it; }
71 inline bool operator != (
const const_iterator & other)
const json_nothrow {
return it != other.it; }
72 inline bool operator > (
const const_iterator & other)
const json_nothrow {
return it > other.it; }
73 inline bool operator >= (
const const_iterator & other)
const json_nothrow {
return it >= other.it; }
74 inline bool operator < (
const const_iterator & other)
const json_nothrow {
return it < other.it; }
75 inline bool operator <= (
const const_iterator & other)
const json_nothrow {
return it <= other.it; }
77 inline bool operator == (
const iterator & other)
const json_nothrow {
return it == other.it; }
78 inline bool operator != (
const iterator & other)
const json_nothrow {
return it != other.it; }
79 inline bool operator > (
const iterator & other)
const json_nothrow {
return it > other.it; }
80 inline bool operator >= (
const iterator & other)
const json_nothrow {
return it >= other.it; }
81 inline bool operator < (
const iterator & other)
const json_nothrow {
return it < other.it; }
82 inline bool operator <= (
const iterator & other)
const json_nothrow {
return it <= other.it; }
84 inline const_iterator & operator =(
const const_iterator & orig) json_nothrow { it = orig.it;
return *
this; }
85 const_iterator (
const const_iterator & orig) json_nothrow : it(orig.it) {}
87 const json_shared_string * parent;
89 friend class json_shared_string;
90 friend struct iterator;
94 iterator(
const json_char * p,
const json_shared_string * pa) : parent(pa), it(p){}
96 inline iterator& operator ++(
void) json_nothrow { ++it;
return *
this; }
97 inline iterator& operator --(
void) json_nothrow { --it;
return *
this; }
98 inline iterator& operator +=(
long i) json_nothrow { it += i;
return *
this; }
99 inline iterator& operator -=(
long i) json_nothrow { it -= i;
return *
this; }
100 inline iterator operator ++(
int) json_nothrow {
101 iterator result(*
this);
105 inline iterator operator --(
int) json_nothrow {
106 iterator result(*
this);
110 inline iterator operator +(
long i)
const json_nothrow {
111 iterator result(*
this);
115 inline iterator operator -(
long i)
const json_nothrow {
116 iterator result(*
this);
120 inline const json_char & operator [](
size_t pos)
const json_nothrow {
return it[pos]; };
121 inline const json_char & operator *(
void) const json_nothrow {
return *it; }
122 inline const json_char * operator ->(
void) const json_nothrow {
return it; }
123 inline bool operator == (
const const_iterator & other)
const json_nothrow {
return it == other.it; }
124 inline bool operator != (
const const_iterator & other)
const json_nothrow {
return it != other.it; }
125 inline bool operator > (
const const_iterator & other)
const json_nothrow {
return it > other.it; }
126 inline bool operator >= (
const const_iterator & other)
const json_nothrow {
return it >= other.it; }
127 inline bool operator < (
const const_iterator & other)
const json_nothrow {
return it < other.it; }
128 inline bool operator <= (
const const_iterator & other)
const json_nothrow {
return it <= other.it; }
130 inline bool operator == (
const iterator & other)
const json_nothrow {
return it == other.it; }
131 inline bool operator != (
const iterator & other)
const json_nothrow {
return it != other.it; }
132 inline bool operator > (
const iterator & other)
const json_nothrow {
return it > other.it; }
133 inline bool operator >= (
const iterator & other)
const json_nothrow {
return it >= other.it; }
134 inline bool operator < (
const iterator & other)
const json_nothrow {
return it < other.it; }
135 inline bool operator <= (
const iterator & other)
const json_nothrow {
return it <= other.it; }
137 inline iterator & operator =(
const iterator & orig) json_nothrow { it = orig.it;
return *
this; }
138 iterator (
const iterator & orig) json_nothrow : it(orig.it) {}
140 const json_shared_string * parent;
141 const json_char * it;
142 friend class json_shared_string;
143 friend struct const_iterator;
148 inline json_shared_string::iterator begin(
void){
149 iterator res = iterator(data(),
this);
152 inline json_shared_string::iterator end(
void){
153 iterator res = iterator(data() + len,
this);
156 inline json_shared_string::const_iterator begin(
void)
const {
157 const_iterator res = const_iterator(data(),
this);
160 inline json_shared_string::const_iterator end(
void)
const {
161 const_iterator res = const_iterator(data() + len,
this);
166 inline json_string::iterator std_begin(
void){
167 return _str -> mystring.begin() + offset;
169 inline json_string::iterator std_end(
void){
170 return std_begin() + len;
173 inline json_string::const_iterator std_begin(
void)
const{
174 return _str -> mystring.begin() + offset;
176 inline json_string::const_iterator std_end(
void)
const{
177 return std_begin() + len;
180 inline json_shared_string(
void) : offset(0), len(0), _str(new(json_malloc<json_shared_string_internal>(1)) json_shared_string_internal(json_global(EMPTY_JSON_STRING))) {}
182 inline json_shared_string(
const json_string & str) : offset(0), len(str.length()), _str(new(json_malloc<json_shared_string_internal>(1)) json_shared_string_internal(str)) {}
184 inline json_shared_string(
const json_shared_string & str,
size_t _offset,
size_t _len) : _str(str._str), offset(str.offset + _offset), len(_len) {
188 inline json_shared_string(
const json_shared_string & str,
size_t _offset) : _str(str._str), offset(str.offset + _offset), len(str.len - _offset) {
192 inline json_shared_string(
const iterator & s,
const iterator & e) : _str(s.parent -> _str), offset(s.it - s.parent -> _str -> mystring.data()), len(e.it - s.it){
196 inline ~json_shared_string(
void){
200 inline bool empty(
void)
const {
return len == 0; }
202 size_t find(json_char ch,
size_t pos = 0)
const {
203 if (_str -> refCount == 1)
return _str -> mystring.find(ch, pos);
204 json_string::const_iterator e = std_end();
205 for(json_string::const_iterator b = std_begin() + pos; b != e; ++b){
206 if (*b == ch)
return b - std_begin();
208 return json_string::npos;
211 inline json_char & operator[] (
size_t loc){
212 return _str -> mystring[loc + offset];
214 inline json_char operator[] (
size_t loc)
const {
215 return _str -> mystring[loc + offset];
217 inline void clear(){ len = 0; }
218 inline size_t length()
const {
return len; }
219 inline const json_char * c_str()
const {
return toString().c_str(); }
220 inline const json_char * data()
const {
return _str -> mystring.data() + offset; }
222 inline bool operator != (
const json_shared_string & other)
const {
223 if ((other._str == _str) && (other.len == len) && (other.offset == offset))
return false;
224 return other.toString() != toString();
227 inline bool operator == (
const json_shared_string & other)
const {
228 if ((other._str == _str) && (other.len == len) && (other.offset == offset))
return true;
229 return other.toString() == toString();
232 inline bool operator == (
const json_string & other)
const {
233 return other == toString();
236 json_string & toString(
void)
const {
238 if (_str -> refCount == 1){
239 if (offset || len != _str -> mystring.length()){
240 _str -> mystring = json_string(std_begin(), std_end());
242 }
else if (offset || len != _str -> mystring.length()){
244 _str =
new(json_malloc<json_shared_string_internal>(1)) json_shared_string_internal(json_string(std_begin(), std_end()));
247 return _str -> mystring;
251 inline void assign(
const json_shared_string & other,
size_t _offset,
size_t _len){
252 if (other._str != _str){
257 offset = other.offset + _offset;
261 json_shared_string(
const json_shared_string & other) : _str(other._str), offset(other.offset), len(other.len){
265 json_shared_string & operator =(
const json_shared_string & other){
266 if (other._str != _str){
271 offset = other.offset;
276 json_shared_string & operator += (
const json_char c){
284 struct json_shared_string_internal {
285 inline json_shared_string_internal(
const json_string & _mystring) : mystring(_mystring), refCount(1) {}
286 json_string mystring;
287 size_t refCount PACKED(20);
289 inline void deref(
void){
290 if (--_str -> refCount == 0){
291 _str -> ~json_shared_string_internal();
292 libjson_free<json_shared_string_internal>(_str);
295 mutable json_shared_string_internal * _str;
296 mutable size_t offset PACKED(20);
297 mutable size_t len PACKED(20);
300 #ifdef JSON_LESS_MEMORY
304 #pragma pack(pop, json_shared_string_pack,)
bool TTFOUNDATION_EXPORT operator!=(const TTObject &anObject, const TTObject &anotherObject)
Compare two objects for inequality.
bool TTFOUNDATION_EXPORT operator==(const TTObject &anObject, const TTObject &anotherObject)
Compare two objects for equality.