53 char* STRING::AllocData(
int used,
int capacity) {
54 data_ = (STRING_HEADER *)malloc(capacity +
sizeof(STRING_HEADER));
57 STRING_HEADER* header = GetHeader();
58 header->capacity_ = capacity;
63 void STRING::DiscardData() {
70 char* STRING::ensure_cstr(int32_t 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*)(malloc(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 const 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());
120 if (cstr ==
nullptr) {
124 const int len = strlen(cstr) + 1;
125 char* this_cstr = AllocData(len, len);
126 memcpy(this_cstr, cstr, len);
128 assert(InvariantOk());
132 if (data ==
nullptr) {
137 memcpy(this_cstr, data,
length);
167 if (len > UINT16_MAX)
return false;
184 return fp->
Skip(len);
188 return (c !=
'\0') && (strchr (GetCStr(), c) !=
nullptr);
193 return GetHeader()->used_ - 1;
197 const STRING_HEADER* header = GetHeader();
198 if (header->used_ == 0)
218 #if STRING_IS_PROTECTED 220 return GetCStr()[index];
223 void STRING::insert_range(int32_t index,
const char* str,
int len) {
226 STRING_HEADER* this_header = GetHeader();
227 int used = this_header->used_;
231 char* this_cstr = ensure_cstr(used + len + 1);
234 memmove(this_cstr + index + len,
236 this_header->used_ - index);
237 }
else if (len > 0) {
239 this_cstr[this_header->used_ + len - 1] =
'\0';
244 if (this_header->used_ == 0)
245 ++this_header->used_;
250 memcpy(this_cstr + index, str, len);
251 this_header->used_ += len;
253 assert(InvariantOk());
256 void STRING::erase_range(int32_t index,
int len) {
257 char* this_cstr = GetCStr();
258 STRING_HEADER* this_header = GetHeader();
260 memcpy(this_cstr+index, this_cstr+index+len,
261 this_header->used_ - index - len);
262 this_header->used_ -= len;
263 assert(InvariantOk());
270 char* this_cstr = ensure_cstr(index + 1);
271 this_cstr[index] =
'\0';
272 GetHeader()->used_ = index + 1;
273 assert(InvariantOk());
279 GetHeader()->used_ = -1;
280 return ((
char *)GetCStr())[index];
287 for (
int i = 0; i < len; i++) {
288 if ((*
this)[i] == c) {
289 if (i != start_index) {
298 if (len != start_index) {
299 splited->
push_back(
STRING(GetCStr() + start_index, len - start_index));
306 const STRING_HEADER* str_header = str.GetHeader();
307 const STRING_HEADER* this_header = GetHeader();
308 const int this_used = this_header->used_;
309 const int str_used = str_header->used_;
311 return (this_used == str_used)
312 && (memcmp(GetCStr(), str.GetCStr(), this_used) == 0);
318 const STRING_HEADER* str_header = str.GetHeader();
319 const STRING_HEADER* this_header = GetHeader();
320 const int this_used = this_header->used_;
321 const int str_used = str_header->used_;
323 return (this_used != str_used)
324 || (memcmp(GetCStr(), str.GetCStr(), this_used) != 0);
329 const STRING_HEADER* this_header = GetHeader();
332 return this_header->used_ > 1;
334 const int32_t
length = strlen(cstr) + 1;
335 return (this_header->used_ !=
length)
336 || (memcmp(GetCStr(), cstr,
length) != 0);
342 const STRING_HEADER* str_header = str.GetHeader();
343 const int str_used = str_header->used_;
345 GetHeader()->used_ = 0;
346 char* this_cstr = ensure_cstr(str_used);
347 STRING_HEADER* this_header = GetHeader();
349 memcpy(this_cstr, str.GetCStr(), str_used);
350 this_header->used_ = str_used;
352 assert(InvariantOk());
359 const STRING_HEADER* str_header = str.GetHeader();
360 const char* str_cstr = str.GetCStr();
361 const int str_used = str_header->used_;
362 const int this_used = GetHeader()->used_;
363 char* this_cstr = ensure_cstr(this_used + str_used);
365 STRING_HEADER* this_header = GetHeader();
368 memcpy(this_cstr + this_used - 1, str_cstr, str_used);
369 this_header->used_ += str_used - 1;
371 memcpy(this_cstr, str_cstr, str_used);
372 this_header->used_ = str_used;
375 assert(InvariantOk());
384 snprintf(num_buffer,
kMaxIntSize - 1,
"%d", number);
400 STRING_HEADER* this_header = GetHeader();
402 const int len = strlen(cstr) + 1;
404 this_header->used_ = 0;
405 char* this_cstr = ensure_cstr(len);
406 this_header = GetHeader();
407 memcpy(this_cstr, cstr, len);
408 this_header->used_ = len;
416 assert(InvariantOk());
421 STRING_HEADER* this_header = GetHeader();
422 this_header->used_ = 0;
423 char* this_cstr = ensure_cstr(len + 1);
425 this_header = GetHeader();
426 memcpy(this_cstr, cstr, len);
427 this_cstr[len] =
'\0';
428 this_header->used_ = len + 1;
430 assert(InvariantOk());
437 assert(InvariantOk());
445 const STRING_HEADER* this_header = GetHeader();
446 const int this_used = this_header->used_;
447 char* result_cstr = result.ensure_cstr(this_used + 1);
448 STRING_HEADER* result_header = result.GetHeader();
449 const int result_used = result_header->used_;
452 memcpy(result_cstr, GetCStr(), this_used);
453 result_cstr[result_used] = ch;
454 result_cstr[result_used + 1] =
'\0';
455 ++result_header->used_;
457 assert(InvariantOk());
467 const int len = strlen(str) + 1;
468 const int this_used = GetHeader()->used_;
469 char* this_cstr = ensure_cstr(this_used + len);
470 STRING_HEADER* this_header = GetHeader();
475 memcpy(this_cstr + this_used - 1, str, len);
476 this_header->used_ += len - 1;
478 memcpy(this_cstr, str, len);
479 this_header->used_ = len;
482 assert(InvariantOk());
492 int this_used = GetHeader()->used_;
493 char* this_cstr = ensure_cstr(this_used + 1);
494 STRING_HEADER* this_header = GetHeader();
499 this_cstr[this_used++] = ch;
500 this_cstr[this_used++] =
'\0';
501 this_header->used_ = this_used;
503 assert(InvariantOk());
bool Serialize(FILE *fp) const
STRING & operator+=(const char *string)
bool operator==(const STRING &string) const
bool DeSerialize(bool swap, FILE *fp)
const char * string() const
bool DeSerialize(char *data, size_t count=1)
char & operator[](int32_t index) const
STRING operator+(const STRING &string) const
bool Serialize(FILE *fp, const char *data, size_t n)
static bool SkipDeSerialize(tesseract::TFile *fp)
STRING & operator=(const char *string)
void ReverseN(void *ptr, int num_bytes)
const char * c_str() const
void split(const char c, GenericVector< STRING > *splited)
void add_str_double(const char *str, double number)
bool Serialize(const char *data, size_t count=1)
void truncate_at(int32_t index)
void add_str_int(const char *str, int number)
bool contains(const char c) const
bool operator!=(const STRING &string) const
bool DeSerialize(FILE *fp, char *data, size_t n)
void assign(const char *cstr, int len)