54 char* STRING::AllocData(
int used,
int capacity) {
55 data_ = (STRING_HEADER *)
alloc_string(capacity +
sizeof(STRING_HEADER));
58 STRING_HEADER* header = GetHeader();
59 header->capacity_ = capacity;
64 void STRING::DiscardData() {
70 char* STRING::ensure_cstr(
inT32 min_capacity) {
71 STRING_HEADER* orig_header = GetHeader();
72 if (min_capacity <= orig_header->capacity_)
73 return ((
char *)this->data_) +
sizeof(STRING_HEADER);
78 if (min_capacity < 2 * orig_header->capacity_)
79 min_capacity = 2 * orig_header->capacity_;
81 int alloc =
sizeof(STRING_HEADER) + min_capacity;
82 STRING_HEADER* new_header = (STRING_HEADER*)(
alloc_string(alloc));
84 memcpy(&new_header[1], GetCStr(), orig_header->used_);
85 new_header->capacity_ = min_capacity;
86 new_header->used_ = orig_header->used_;
92 assert(InvariantOk());
93 return ((
char *)data_) +
sizeof(STRING_HEADER);
98 void STRING::FixHeader()
const {
99 const STRING_HEADER* header = GetHeader();
100 if (header->used_ < 0)
101 header->used_ = strlen(GetCStr()) + 1;
112 const STRING_HEADER* str_header = str.GetHeader();
113 int str_used = str_header->used_;
114 char *this_cstr = AllocData(str_used, str_used);
115 memcpy(this_cstr, str.GetCStr(), str_used);
116 assert(InvariantOk());
124 int len = strlen(cstr) + 1;
125 char* this_cstr = AllocData(len, len);
126 memcpy(this_cstr, cstr, len);
128 assert(InvariantOk());
136 char* this_cstr = AllocData(length + 1, length + 1);
137 memcpy(this_cstr, data, length);
150 if (fwrite(&len,
sizeof(len), 1, fp) != 1)
return false;
151 if (static_cast<int>(fwrite(GetCStr(), 1, len, fp)) != len)
return false;
157 if (fp->
FWrite(&len,
sizeof(len), 1) != 1)
return false;
158 if (fp->
FWrite(GetCStr(), 1, len) != len)
return false;
165 if (fread(&len,
sizeof(len), 1, fp) != 1)
return false;
169 if (static_cast<int>(fread(GetCStr(), 1, len, fp)) != len)
return false;
176 if (fp->
FRead(&len,
sizeof(len), 1) != 1)
return false;
180 if (fp->
FRead(GetCStr(), 1, len) != len)
return false;
185 return (c !=
'\0') && (strchr (GetCStr(), c) !=
NULL);
190 return GetHeader()->used_ - 1;
194 const STRING_HEADER* header = GetHeader();
195 if (header->used_ == 0)
215 #if STRING_IS_PROTECTED
217 return GetCStr()[index];
220 void STRING::insert_range(
inT32 index,
const char* str,
int len) {
223 STRING_HEADER* this_header = GetHeader();
224 int used = this_header->used_;
228 char* this_cstr = ensure_cstr(used + len + 1);
231 memmove(this_cstr + index + len,
233 this_header->used_ - index);
234 }
else if (len > 0) {
236 this_cstr[this_header->used_ + len - 1] =
'\0';
241 if (this_header->used_ == 0)
242 ++this_header->used_;
247 memcpy(this_cstr + index, str, len);
248 this_header->used_ += len;
250 assert(InvariantOk());
253 void STRING::erase_range(
inT32 index,
int len) {
254 char* this_cstr = GetCStr();
255 STRING_HEADER* this_header = GetHeader();
257 memcpy(this_cstr+index, this_cstr+index+len,
258 this_header->used_ - index - len);
259 this_header->used_ -= len;
260 assert(InvariantOk());
267 char* this_cstr = ensure_cstr(index + 1);
268 this_cstr[index] =
'\0';
269 GetHeader()->used_ = index + 1;
270 assert(InvariantOk());
276 GetHeader()->used_ = -1;
277 return ((
char *)GetCStr())[index];
284 for (
int i = 0; i < len; i++) {
285 if ((*
this)[i] == c) {
286 if (i != start_index) {
295 if (len != start_index) {
296 splited->
push_back(
STRING(GetCStr() + start_index, len - start_index));
303 const STRING_HEADER* str_header = str.GetHeader();
304 const STRING_HEADER* this_header = GetHeader();
305 int this_used = this_header->used_;
306 int str_used = str_header->used_;
308 return (this_used == str_used)
309 && (memcmp(GetCStr(), str.GetCStr(), this_used) == 0);
315 const STRING_HEADER* str_header = str.GetHeader();
316 const STRING_HEADER* this_header = GetHeader();
317 int this_used = this_header->used_;
318 int str_used = str_header->used_;
320 return (this_used != str_used)
321 || (memcmp(GetCStr(), str.GetCStr(), this_used) != 0);
326 const STRING_HEADER* this_header = GetHeader();
329 return this_header->used_ > 1;
332 return (this_header->used_ != length)
333 || (memcmp(GetCStr(), cstr, length) != 0);
339 const STRING_HEADER* str_header = str.GetHeader();
340 int str_used = str_header->used_;
342 GetHeader()->used_ = 0;
343 char* this_cstr = ensure_cstr(str_used);
344 STRING_HEADER* this_header = GetHeader();
346 memcpy(this_cstr, str.GetCStr(), str_used);
347 this_header->used_ = str_used;
349 assert(InvariantOk());
356 const STRING_HEADER* str_header = str.GetHeader();
357 const char* str_cstr = str.GetCStr();
358 int str_used = str_header->used_;
359 int this_used = GetHeader()->used_;
360 char* this_cstr = ensure_cstr(this_used + str_used);
362 STRING_HEADER* this_header = GetHeader();
365 memcpy(this_cstr + this_used - 1, str_cstr, str_used);
366 this_header->used_ += str_used - 1;
368 memcpy(this_cstr, str_cstr, str_used);
369 this_header->used_ = str_used;
372 assert(InvariantOk());
381 snprintf(num_buffer,
kMaxIntSize - 1,
"%d", number);
397 STRING_HEADER* this_header = GetHeader();
399 int len = strlen(cstr) + 1;
401 this_header->used_ = 0;
402 char* this_cstr = ensure_cstr(len);
403 this_header = GetHeader();
404 memcpy(this_cstr, cstr, len);
405 this_header->used_ = len;
413 assert(InvariantOk());
418 STRING_HEADER* this_header = GetHeader();
419 this_header->used_ = 0;
420 char* this_cstr = ensure_cstr(len + 1);
422 this_header = GetHeader();
423 memcpy(this_cstr, cstr, len);
424 this_cstr[len] =
'\0';
425 this_header->used_ = len + 1;
427 assert(InvariantOk());
434 assert(InvariantOk());
442 const STRING_HEADER* this_header = GetHeader();
443 int this_used = this_header->used_;
444 char* result_cstr = result.ensure_cstr(this_used + 1);
445 STRING_HEADER* result_header = result.GetHeader();
446 int result_used = result_header->used_;
449 memcpy(result_cstr, GetCStr(), this_used);
450 result_cstr[result_used] = ch;
451 result_cstr[result_used + 1] =
'\0';
452 ++result_header->used_;
454 assert(InvariantOk());
464 int len = strlen(str) + 1;
465 int this_used = GetHeader()->used_;
466 char* this_cstr = ensure_cstr(this_used + len);
467 STRING_HEADER* this_header = GetHeader();
472 memcpy(this_cstr + this_used - 1, str, len);
473 this_header->used_ += len - 1;
475 memcpy(this_cstr, str, len);
476 this_header->used_ = len;
479 assert(InvariantOk());
489 int this_used = GetHeader()->used_;
490 char* this_cstr = ensure_cstr(this_used + 1);
491 STRING_HEADER* this_header = GetHeader();
496 this_cstr[this_used++] = ch;
497 this_cstr[this_used++] =
'\0';
498 this_header->used_ = this_used;
500 assert(InvariantOk());
bool DeSerialize(bool swap, FILE *fp)
char * alloc_string(inT32 count)
void split(const char c, GenericVector< STRING > *splited)
STRING & operator+=(const char *string)
void truncate_at(inT32 index)
bool Serialize(FILE *fp) const
void free_string(char *string)
STRING operator+(const STRING &string) const
void add_str_int(const char *str, int number)
void ReverseN(void *ptr, int num_bytes)
int FWrite(const void *buffer, int size, int count)
void assign(const char *cstr, int len)
BOOL8 operator==(const STRING &string) const
void add_str_double(const char *str, double number)
STRING & operator=(const char *string)
BOOL8 operator!=(const STRING &string) const
const char * string() const
char & operator[](inT32 index) const
int FRead(void *buffer, int size, int count)
BOOL8 contains(const char c) const
const char * c_str() const