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->
FReadEndian(&len,
sizeof(len), 1) != 1)
return false;
178 if (fp->
FRead(GetCStr(), 1, len) != len)
return false;
185 if (fp->
FReadEndian(&len,
sizeof(len), 1) != 1)
return false;
186 return fp->
FRead(NULL, 1, len) == len;
190 return (c !=
'\0') && (strchr (GetCStr(), c) != NULL);
195 return GetHeader()->used_ - 1;
199 const STRING_HEADER* header = GetHeader();
200 if (header->used_ == 0)
220 #if STRING_IS_PROTECTED 222 return GetCStr()[index];
225 void STRING::insert_range(
inT32 index,
const char* str,
int len) {
228 STRING_HEADER* this_header = GetHeader();
229 int used = this_header->used_;
233 char* this_cstr = ensure_cstr(used + len + 1);
236 memmove(this_cstr + index + len,
238 this_header->used_ - index);
239 }
else if (len > 0) {
241 this_cstr[this_header->used_ + len - 1] =
'\0';
246 if (this_header->used_ == 0)
247 ++this_header->used_;
252 memcpy(this_cstr + index, str, len);
253 this_header->used_ += len;
255 assert(InvariantOk());
258 void STRING::erase_range(
inT32 index,
int len) {
259 char* this_cstr = GetCStr();
260 STRING_HEADER* this_header = GetHeader();
262 memcpy(this_cstr+index, this_cstr+index+len,
263 this_header->used_ - index - len);
264 this_header->used_ -= len;
265 assert(InvariantOk());
272 char* this_cstr = ensure_cstr(index + 1);
273 this_cstr[index] =
'\0';
274 GetHeader()->used_ = index + 1;
275 assert(InvariantOk());
281 GetHeader()->used_ = -1;
282 return ((
char *)GetCStr())[index];
289 for (
int i = 0; i < len; i++) {
290 if ((*
this)[i] == c) {
291 if (i != start_index) {
300 if (len != start_index) {
301 splited->
push_back(
STRING(GetCStr() + start_index, len - start_index));
308 const STRING_HEADER* str_header = str.GetHeader();
309 const STRING_HEADER* this_header = GetHeader();
310 int this_used = this_header->used_;
311 int str_used = str_header->used_;
313 return (this_used == str_used)
314 && (memcmp(GetCStr(), str.GetCStr(), this_used) == 0);
320 const STRING_HEADER* str_header = str.GetHeader();
321 const STRING_HEADER* this_header = GetHeader();
322 int this_used = this_header->used_;
323 int str_used = str_header->used_;
325 return (this_used != str_used)
326 || (memcmp(GetCStr(), str.GetCStr(), this_used) != 0);
331 const STRING_HEADER* this_header = GetHeader();
334 return this_header->used_ > 1;
337 return (this_header->used_ != length)
338 || (memcmp(GetCStr(), cstr, length) != 0);
344 const STRING_HEADER* str_header = str.GetHeader();
345 int str_used = str_header->used_;
347 GetHeader()->used_ = 0;
348 char* this_cstr = ensure_cstr(str_used);
349 STRING_HEADER* this_header = GetHeader();
351 memcpy(this_cstr, str.GetCStr(), str_used);
352 this_header->used_ = str_used;
354 assert(InvariantOk());
361 const STRING_HEADER* str_header = str.GetHeader();
362 const char* str_cstr = str.GetCStr();
363 int str_used = str_header->used_;
364 int this_used = GetHeader()->used_;
365 char* this_cstr = ensure_cstr(this_used + str_used);
367 STRING_HEADER* this_header = GetHeader();
370 memcpy(this_cstr + this_used - 1, str_cstr, str_used);
371 this_header->used_ += str_used - 1;
373 memcpy(this_cstr, str_cstr, str_used);
374 this_header->used_ = str_used;
377 assert(InvariantOk());
386 snprintf(num_buffer,
kMaxIntSize - 1,
"%d", number);
402 STRING_HEADER* this_header = GetHeader();
404 int len = strlen(cstr) + 1;
406 this_header->used_ = 0;
407 char* this_cstr = ensure_cstr(len);
408 this_header = GetHeader();
409 memcpy(this_cstr, cstr, len);
410 this_header->used_ = len;
418 assert(InvariantOk());
423 STRING_HEADER* this_header = GetHeader();
424 this_header->used_ = 0;
425 char* this_cstr = ensure_cstr(len + 1);
427 this_header = GetHeader();
428 memcpy(this_cstr, cstr, len);
429 this_cstr[len] =
'\0';
430 this_header->used_ = len + 1;
432 assert(InvariantOk());
439 assert(InvariantOk());
447 const STRING_HEADER* this_header = GetHeader();
448 int this_used = this_header->used_;
449 char* result_cstr = result.ensure_cstr(this_used + 1);
450 STRING_HEADER* result_header = result.GetHeader();
451 int result_used = result_header->used_;
454 memcpy(result_cstr, GetCStr(), this_used);
455 result_cstr[result_used] = ch;
456 result_cstr[result_used + 1] =
'\0';
457 ++result_header->used_;
459 assert(InvariantOk());
469 int len = strlen(str) + 1;
470 int this_used = GetHeader()->used_;
471 char* this_cstr = ensure_cstr(this_used + len);
472 STRING_HEADER* this_header = GetHeader();
477 memcpy(this_cstr + this_used - 1, str, len);
478 this_header->used_ += len - 1;
480 memcpy(this_cstr, str, len);
481 this_header->used_ = len;
484 assert(InvariantOk());
494 int this_used = GetHeader()->used_;
495 char* this_cstr = ensure_cstr(this_used + 1);
496 STRING_HEADER* this_header = GetHeader();
501 this_cstr[this_used++] = ch;
502 this_cstr[this_used++] =
'\0';
503 this_header->used_ = this_used;
505 assert(InvariantOk());
STRING & operator+=(const char *string)
void add_str_int(const char *str, int number)
char & operator[](inT32 index) const
void truncate_at(inT32 index)
STRING operator+(const STRING &string) const
bool DeSerialize(bool swap, FILE *fp)
const char * string() const
int FReadEndian(void *buffer, int size, int count)
void assign(const char *cstr, int len)
BOOL8 contains(const char c) const
void add_str_double(const char *str, double number)
bool Serialize(FILE *fp) const
int FWrite(const void *buffer, int size, int count)
char * alloc_string(inT32 count)
BOOL8 operator!=(const STRING &string) const
BOOL8 operator==(const STRING &string) const
void free_string(char *string)
const char * c_str() const
static bool SkipDeSerialize(tesseract::TFile *fp)
STRING & operator=(const char *string)
void split(const char c, GenericVector< STRING > *splited)
void ReverseN(void *ptr, int num_bytes)
int FRead(void *buffer, int size, int count)