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)
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; }
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; }
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 {
105 inline iterator operator --(
int) json_nothrow {
110 inline iterator operator +(
long i)
const json_nothrow {
115 inline iterator operator -(
long i)
const json_nothrow {
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; }
141 const json_char * it;
153 iterator res = iterator(data() + len,
this);
157 const_iterator res = const_iterator(data(),
this);
161 const_iterator res = const_iterator(data() + len,
this);
167 return _str -> mystring.begin() + offset;
170 return std_begin() + len;
174 return _str -> mystring.begin() + offset;
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)) {}
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){
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);
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; }
223 if ((other._str == _str) && (other.len == len) && (other.offset == offset))
return false;
224 return other.toString() != toString();
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();
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;
252 if (other._str != _str){
257 offset = other.offset + _offset;
266 if (other._str != _str){
271 offset = other.offset;
287 size_t refCount PACKED(20);
289 inline void deref(
void){
290 if (--_str -> refCount == 0){
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,)
Definition: JSONSharedString.h:93
Definition: StringTest.h:68
Definition: JSONSharedString.h:40
Definition: StringTest.h:24
Definition: JSONSharedString.h:35
Definition: StringTest.h:22
Definition: JSONSharedString.h:284