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());
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;
187 if (fp->
FRead(&len,
sizeof(len), 1) != 1)
return false;
188 if (swap)
ReverseN(&len,
sizeof(len));
189 return fp->
FRead(NULL, 1, len) == len;
193 return (c !=
'\0') && (strchr (GetCStr(), c) != NULL);
198 return GetHeader()->used_ - 1;
202 const STRING_HEADER* header = GetHeader();
203 if (header->used_ == 0)
223 #if STRING_IS_PROTECTED 225 return GetCStr()[index];
228 void STRING::insert_range(
inT32 index,
const char* str,
int len) {
231 STRING_HEADER* this_header = GetHeader();
232 int used = this_header->used_;
236 char* this_cstr = ensure_cstr(used + len + 1);
239 memmove(this_cstr + index + len,
241 this_header->used_ - index);
242 }
else if (len > 0) {
244 this_cstr[this_header->used_ + len - 1] =
'\0';
249 if (this_header->used_ == 0)
250 ++this_header->used_;
255 memcpy(this_cstr + index, str, len);
256 this_header->used_ += len;
258 assert(InvariantOk());
261 void STRING::erase_range(
inT32 index,
int len) {
262 char* this_cstr = GetCStr();
263 STRING_HEADER* this_header = GetHeader();
265 memcpy(this_cstr+index, this_cstr+index+len,
266 this_header->used_ - index - len);
267 this_header->used_ -= len;
268 assert(InvariantOk());
275 char* this_cstr = ensure_cstr(index + 1);
276 this_cstr[index] =
'\0';
277 GetHeader()->used_ = index + 1;
278 assert(InvariantOk());
284 GetHeader()->used_ = -1;
285 return ((
char *)GetCStr())[index];
292 for (
int i = 0; i < len; i++) {
293 if ((*
this)[i] == c) {
294 if (i != start_index) {
303 if (len != start_index) {
304 splited->
push_back(
STRING(GetCStr() + start_index, len - start_index));
311 const STRING_HEADER* str_header = str.GetHeader();
312 const STRING_HEADER* this_header = GetHeader();
313 int this_used = this_header->used_;
314 int str_used = str_header->used_;
316 return (this_used == str_used)
317 && (memcmp(GetCStr(), str.GetCStr(), this_used) == 0);
323 const STRING_HEADER* str_header = str.GetHeader();
324 const STRING_HEADER* this_header = GetHeader();
325 int this_used = this_header->used_;
326 int str_used = str_header->used_;
328 return (this_used != str_used)
329 || (memcmp(GetCStr(), str.GetCStr(), this_used) != 0);
334 const STRING_HEADER* this_header = GetHeader();
337 return this_header->used_ > 1;
340 return (this_header->used_ !=
length)
341 || (memcmp(GetCStr(), cstr,
length) != 0);
347 const STRING_HEADER* str_header = str.GetHeader();
348 int str_used = str_header->used_;
350 GetHeader()->used_ = 0;
351 char* this_cstr = ensure_cstr(str_used);
352 STRING_HEADER* this_header = GetHeader();
354 memcpy(this_cstr, str.GetCStr(), str_used);
355 this_header->used_ = str_used;
357 assert(InvariantOk());
364 const STRING_HEADER* str_header = str.GetHeader();
365 const char* str_cstr = str.GetCStr();
366 int str_used = str_header->used_;
367 int this_used = GetHeader()->used_;
368 char* this_cstr = ensure_cstr(this_used + str_used);
370 STRING_HEADER* this_header = GetHeader();
373 memcpy(this_cstr + this_used - 1, str_cstr, str_used);
374 this_header->used_ += str_used - 1;
376 memcpy(this_cstr, str_cstr, str_used);
377 this_header->used_ = str_used;
380 assert(InvariantOk());
389 snprintf(num_buffer,
kMaxIntSize - 1,
"%d", number);
405 STRING_HEADER* this_header = GetHeader();
407 int len = strlen(cstr) + 1;
409 this_header->used_ = 0;
410 char* this_cstr = ensure_cstr(len);
411 this_header = GetHeader();
412 memcpy(this_cstr, cstr, len);
413 this_header->used_ = len;
421 assert(InvariantOk());
426 STRING_HEADER* this_header = GetHeader();
427 this_header->used_ = 0;
428 char* this_cstr = ensure_cstr(len + 1);
430 this_header = GetHeader();
431 memcpy(this_cstr, cstr, len);
432 this_cstr[len] =
'\0';
433 this_header->used_ = len + 1;
435 assert(InvariantOk());
442 assert(InvariantOk());
450 const STRING_HEADER* this_header = GetHeader();
451 int this_used = this_header->used_;
452 char* result_cstr = result.ensure_cstr(this_used + 1);
453 STRING_HEADER* result_header = result.GetHeader();
454 int result_used = result_header->used_;
457 memcpy(result_cstr, GetCStr(), this_used);
458 result_cstr[result_used] = ch;
459 result_cstr[result_used + 1] =
'\0';
460 ++result_header->used_;
462 assert(InvariantOk());
472 int len = strlen(str) + 1;
473 int this_used = GetHeader()->used_;
474 char* this_cstr = ensure_cstr(this_used + len);
475 STRING_HEADER* this_header = GetHeader();
480 memcpy(this_cstr + this_used - 1, str, len);
481 this_header->used_ += len - 1;
483 memcpy(this_cstr, str, len);
484 this_header->used_ = len;
487 assert(InvariantOk());
497 int this_used = GetHeader()->used_;
498 char* this_cstr = ensure_cstr(this_used + 1);
499 STRING_HEADER* this_header = GetHeader();
504 this_cstr[this_used++] = ch;
505 this_cstr[this_used++] =
'\0';
506 this_header->used_ = this_used;
508 assert(InvariantOk());
BOOL8 operator==(const STRING &string) const
void split(const char c, GenericVector< STRING > *splited)
char * alloc_string(inT32 count)
int FWrite(const void *buffer, int size, int count)
void free_string(char *string)
static bool SkipDeSerialize(bool swap, tesseract::TFile *fp)
bool DeSerialize(bool swap, FILE *fp)
void add_str_double(const char *str, double number)
void add_str_int(const char *str, int number)
char & operator[](inT32 index) const
void truncate_at(inT32 index)
const char * string() const
STRING & operator=(const char *string)
BOOL8 operator!=(const STRING &string) const
int FRead(void *buffer, int size, int count)
void assign(const char *cstr, int len)
const char * c_str() const
STRING & operator+=(const char *string)
BOOL8 contains(const char c) const
bool Serialize(FILE *fp) const
STRING operator+(const STRING &string) const
void ReverseN(void *ptr, int num_bytes)