tesseract  4.00.00dev
STRING Class Reference

#include <strngs.h>

Public Member Functions

 STRING ()
 
 STRING (const STRING &string)
 
 STRING (const char *string)
 
 STRING (const char *data, int length)
 
 ~STRING ()
 
bool Serialize (FILE *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 
bool Serialize (tesseract::TFile *fp) const
 
bool DeSerialize (tesseract::TFile *fp)
 
BOOL8 contains (const char c) const
 
inT32 length () const
 
inT32 size () const
 
uinT32 unsigned_size () const
 
const char * string () const
 
const char * c_str () const
 
char * strdup () const
 
char & operator[] (inT32 index) const
 
void split (const char c, GenericVector< STRING > *splited)
 
void truncate_at (inT32 index)
 
BOOL8 operator== (const STRING &string) const
 
BOOL8 operator!= (const STRING &string) const
 
BOOL8 operator!= (const char *string) const
 
STRINGoperator= (const char *string)
 
STRINGoperator= (const STRING &string)
 
STRING operator+ (const STRING &string) const
 
STRING operator+ (const char ch) const
 
STRINGoperator+= (const char *string)
 
STRINGoperator+= (const STRING &string)
 
STRINGoperator+= (const char ch)
 
void assign (const char *cstr, int len)
 
void add_str_int (const char *str, int number)
 
void add_str_double (const char *str, double number)
 
void ensure (inT32 min_capacity)
 

Static Public Member Functions

static bool SkipDeSerialize (tesseract::TFile *fp)
 

Detailed Description

Definition at line 45 of file strngs.h.

Constructor & Destructor Documentation

◆ STRING() [1/4]

STRING::STRING ( )

Definition at line 105 of file strngs.cpp.

105  {
106  // Empty STRINGs contain just the "\0".
107  memcpy(AllocData(1, kMinCapacity), "", 1);
108 }
const int kMinCapacity
Definition: strngs.cpp:52

◆ STRING() [2/4]

STRING::STRING ( const STRING string)

Definition at line 110 of file strngs.cpp.

110  {
111  str.FixHeader();
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());
117 }

◆ STRING() [3/4]

STRING::STRING ( const char *  string)

Definition at line 119 of file strngs.cpp.

119  {
120  if (cstr == NULL) {
121  // Empty STRINGs contain just the "\0".
122  memcpy(AllocData(1, kMinCapacity), "", 1);
123  } else {
124  int len = strlen(cstr) + 1;
125  char* this_cstr = AllocData(len, len);
126  memcpy(this_cstr, cstr, len);
127  }
128  assert(InvariantOk());
129 }
const int kMinCapacity
Definition: strngs.cpp:52

◆ STRING() [4/4]

STRING::STRING ( const char *  data,
int  length 
)

Definition at line 131 of file strngs.cpp.

131  {
132  if (data == NULL) {
133  // Empty STRINGs contain just the "\0".
134  memcpy(AllocData(1, kMinCapacity), "", 1);
135  } else {
136  char* this_cstr = AllocData(length + 1, length + 1);
137  memcpy(this_cstr, data, length);
138  this_cstr[length] = '\0';
139  }
140 }
inT32 length() const
Definition: strngs.cpp:193
const int kMinCapacity
Definition: strngs.cpp:52

◆ ~STRING()

STRING::~STRING ( )

Definition at line 142 of file strngs.cpp.

142  {
143  DiscardData();
144 }

Member Function Documentation

◆ add_str_double()

void STRING::add_str_double ( const char *  str,
double  number 
)

Definition at line 391 of file strngs.cpp.

391  {
392  if (str != NULL)
393  *this += str;
394  // Allow space for the maximum possible length of %8g.
395  char num_buffer[kMaxDoubleSize];
396  snprintf(num_buffer, kMaxDoubleSize - 1, "%.8g", number);
397  num_buffer[kMaxDoubleSize - 1] = '\0';
398  *this += num_buffer;
399 }
const int kMaxDoubleSize
Definition: strngs.cpp:36

◆ add_str_int()

void STRING::add_str_int ( const char *  str,
int  number 
)

Definition at line 381 of file strngs.cpp.

381  {
382  if (str != NULL)
383  *this += str;
384  // Allow space for the maximum possible length of inT64.
385  char num_buffer[kMaxIntSize];
386  snprintf(num_buffer, kMaxIntSize - 1, "%d", number);
387  num_buffer[kMaxIntSize - 1] = '\0';
388  *this += num_buffer;
389 }
const int kMaxIntSize
Definition: strngs.cpp:33

◆ assign()

void STRING::assign ( const char *  cstr,
int  len 
)

Definition at line 422 of file strngs.cpp.

422  {
423  STRING_HEADER* this_header = GetHeader();
424  this_header->used_ = 0; // don't bother copying data if need to realloc
425  char* this_cstr = ensure_cstr(len + 1); // +1 for '\0'
426 
427  this_header = GetHeader(); // for realloc
428  memcpy(this_cstr, cstr, len);
429  this_cstr[len] = '\0';
430  this_header->used_ = len + 1;
431 
432  assert(InvariantOk());
433 }

◆ c_str()

const char * STRING::c_str ( ) const

Definition at line 209 of file strngs.cpp.

209  {
210  return string();
211 }
const char * string() const
Definition: strngs.cpp:198

◆ contains()

BOOL8 STRING::contains ( const char  c) const

Definition at line 189 of file strngs.cpp.

189  {
190  return (c != '\0') && (strchr (GetCStr(), c) != NULL);
191 }

◆ DeSerialize() [1/2]

bool STRING::DeSerialize ( bool  swap,
FILE *  fp 
)

Definition at line 163 of file strngs.cpp.

163  {
164  inT32 len;
165  if (fread(&len, sizeof(len), 1, fp) != 1) return false;
166  if (swap)
167  ReverseN(&len, sizeof(len));
168  truncate_at(len);
169  if (static_cast<int>(fread(GetCStr(), 1, len, fp)) != len) return false;
170  return true;
171 }
void truncate_at(inT32 index)
Definition: strngs.cpp:269
int32_t inT32
Definition: host.h:38
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:184

◆ DeSerialize() [2/2]

bool STRING::DeSerialize ( tesseract::TFile fp)

Definition at line 174 of file strngs.cpp.

174  {
175  inT32 len;
176  if (fp->FReadEndian(&len, sizeof(len), 1) != 1) return false;
177  truncate_at(len);
178  if (fp->FRead(GetCStr(), 1, len) != len) return false;
179  return true;
180 }
void truncate_at(inT32 index)
Definition: strngs.cpp:269
int32_t inT32
Definition: host.h:38
int FReadEndian(void *buffer, int size, int count)
Definition: serialis.cpp:97
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:108

◆ ensure()

void STRING::ensure ( inT32  min_capacity)
inline

Definition at line 121 of file strngs.h.

121 { ensure_cstr(min_capacity); }

◆ length()

inT32 STRING::length ( ) const

Definition at line 193 of file strngs.cpp.

193  {
194  FixHeader();
195  return GetHeader()->used_ - 1;
196 }

◆ operator!=() [1/2]

BOOL8 STRING::operator!= ( const STRING string) const

Definition at line 317 of file strngs.cpp.

317  {
318  FixHeader();
319  str.FixHeader();
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_;
324 
325  return (this_used != str_used)
326  || (memcmp(GetCStr(), str.GetCStr(), this_used) != 0);
327 }

◆ operator!=() [2/2]

BOOL8 STRING::operator!= ( const char *  string) const

Definition at line 329 of file strngs.cpp.

329  {
330  FixHeader();
331  const STRING_HEADER* this_header = GetHeader();
332 
333  if (cstr == NULL)
334  return this_header->used_ > 1; // either '\0' or NULL
335  else {
336  inT32 length = strlen(cstr) + 1;
337  return (this_header->used_ != length)
338  || (memcmp(GetCStr(), cstr, length) != 0);
339  }
340 }
int32_t inT32
Definition: host.h:38
inT32 length() const
Definition: strngs.cpp:193

◆ operator+() [1/2]

STRING STRING::operator+ ( const STRING string) const

Definition at line 435 of file strngs.cpp.

435  {
436  STRING result(*this);
437  result += str;
438 
439  assert(InvariantOk());
440  return result;
441 }
Definition: strngs.h:45

◆ operator+() [2/2]

STRING STRING::operator+ ( const char  ch) const

Definition at line 444 of file strngs.cpp.

444  {
445  STRING result;
446  FixHeader();
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_;
452 
453  // copies '\0' but we'll overwrite that
454  memcpy(result_cstr, GetCStr(), this_used);
455  result_cstr[result_used] = ch; // overwrite old '\0'
456  result_cstr[result_used + 1] = '\0'; // append on '\0'
457  ++result_header->used_;
458 
459  assert(InvariantOk());
460  return result;
461 }
Definition: strngs.h:45

◆ operator+=() [1/3]

STRING & STRING::operator+= ( const char *  string)

Definition at line 464 of file strngs.cpp.

464  {
465  if (!str || !*str) // empty string has no effect
466  return *this;
467 
468  FixHeader();
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(); // after ensure for realloc
473 
474  // if we had non-empty string then append overwriting old '\0'
475  // otherwise replace
476  if (this_used > 0) {
477  memcpy(this_cstr + this_used - 1, str, len);
478  this_header->used_ += len - 1;
479  } else {
480  memcpy(this_cstr, str, len);
481  this_header->used_ = len;
482  }
483 
484  assert(InvariantOk());
485  return *this;
486 }

◆ operator+=() [2/3]

STRING & STRING::operator+= ( const STRING string)

Definition at line 358 of file strngs.cpp.

358  {
359  FixHeader();
360  str.FixHeader();
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);
366 
367  STRING_HEADER* this_header = GetHeader(); // after ensure for realloc
368 
369  if (this_used > 1) {
370  memcpy(this_cstr + this_used - 1, str_cstr, str_used);
371  this_header->used_ += str_used - 1; // overwrite '\0'
372  } else {
373  memcpy(this_cstr, str_cstr, str_used);
374  this_header->used_ = str_used;
375  }
376 
377  assert(InvariantOk());
378  return *this;
379 }

◆ operator+=() [3/3]

STRING & STRING::operator+= ( const char  ch)

Definition at line 489 of file strngs.cpp.

489  {
490  if (ch == '\0')
491  return *this;
492 
493  FixHeader();
494  int this_used = GetHeader()->used_;
495  char* this_cstr = ensure_cstr(this_used + 1);
496  STRING_HEADER* this_header = GetHeader();
497 
498  if (this_used > 0)
499  --this_used; // undo old empty null if there was one
500 
501  this_cstr[this_used++] = ch; // append ch to end
502  this_cstr[this_used++] = '\0'; // append '\0' after ch
503  this_header->used_ = this_used;
504 
505  assert(InvariantOk());
506  return *this;
507 }

◆ operator=() [1/2]

STRING & STRING::operator= ( const char *  string)

Definition at line 401 of file strngs.cpp.

401  {
402  STRING_HEADER* this_header = GetHeader();
403  if (cstr) {
404  int len = strlen(cstr) + 1;
405 
406  this_header->used_ = 0; // don't bother copying data if need to realloc
407  char* this_cstr = ensure_cstr(len);
408  this_header = GetHeader(); // for realloc
409  memcpy(this_cstr, cstr, len);
410  this_header->used_ = len;
411  } else {
412  // Reallocate to same state as default constructor.
413  DiscardData();
414  // Empty STRINGs contain just the "\0".
415  memcpy(AllocData(1, kMinCapacity), "", 1);
416  }
417 
418  assert(InvariantOk());
419  return *this;
420 }
const int kMinCapacity
Definition: strngs.cpp:52

◆ operator=() [2/2]

STRING & STRING::operator= ( const STRING string)

Definition at line 342 of file strngs.cpp.

342  {
343  str.FixHeader();
344  const STRING_HEADER* str_header = str.GetHeader();
345  int str_used = str_header->used_;
346 
347  GetHeader()->used_ = 0; // clear since ensure doesn't need to copy data
348  char* this_cstr = ensure_cstr(str_used);
349  STRING_HEADER* this_header = GetHeader();
350 
351  memcpy(this_cstr, str.GetCStr(), str_used);
352  this_header->used_ = str_used;
353 
354  assert(InvariantOk());
355  return *this;
356 }

◆ operator==()

BOOL8 STRING::operator== ( const STRING string) const

Definition at line 305 of file strngs.cpp.

305  {
306  FixHeader();
307  str.FixHeader();
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_;
312 
313  return (this_used == str_used)
314  && (memcmp(GetCStr(), str.GetCStr(), this_used) == 0);
315 }

◆ operator[]()

char & STRING::operator[] ( inT32  index) const

Definition at line 278 of file strngs.cpp.

278  {
279  // Code is casting away this const and mutating the string,
280  // so mark used_ as -1 to flag it unreliable.
281  GetHeader()->used_ = -1;
282  return ((char *)GetCStr())[index];
283 }

◆ Serialize() [1/2]

bool STRING::Serialize ( FILE *  fp) const

Definition at line 148 of file strngs.cpp.

148  {
149  inT32 len = 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;
152  return true;
153 }
int32_t inT32
Definition: host.h:38
inT32 length() const
Definition: strngs.cpp:193

◆ Serialize() [2/2]

bool STRING::Serialize ( tesseract::TFile fp) const

Definition at line 155 of file strngs.cpp.

155  {
156  inT32 len = length();
157  if (fp->FWrite(&len, sizeof(len), 1) != 1) return false;
158  if (fp->FWrite(GetCStr(), 1, len) != len) return false;
159  return true;
160 }
int32_t inT32
Definition: host.h:38
inT32 length() const
Definition: strngs.cpp:193
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:148

◆ size()

inT32 STRING::size ( ) const
inline

Definition at line 69 of file strngs.h.

69 { return length(); }
inT32 length() const
Definition: strngs.cpp:193

◆ SkipDeSerialize()

bool STRING::SkipDeSerialize ( tesseract::TFile fp)
static

Definition at line 183 of file strngs.cpp.

183  {
184  inT32 len;
185  if (fp->FReadEndian(&len, sizeof(len), 1) != 1) return false;
186  return fp->FRead(NULL, 1, len) == len;
187 }
int32_t inT32
Definition: host.h:38
int FReadEndian(void *buffer, int size, int count)
Definition: serialis.cpp:97
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:108

◆ split()

void STRING::split ( const char  c,
GenericVector< STRING > *  splited 
)

Definition at line 286 of file strngs.cpp.

286  {
287  int start_index = 0;
288  int len = length();
289  for (int i = 0; i < len; i++) {
290  if ((*this)[i] == c) {
291  if (i != start_index) {
292  (*this)[i] = '\0';
293  splited->push_back(STRING(GetCStr() + start_index, i - start_index));
294  (*this)[i] = c;
295  }
296  start_index = i + 1;
297  }
298  }
299 
300  if (len != start_index) {
301  splited->push_back(STRING(GetCStr() + start_index, len - start_index));
302  }
303 }
int push_back(T object)
inT32 length() const
Definition: strngs.cpp:193
STRING()
Definition: strngs.cpp:105

◆ strdup()

char* STRING::strdup ( ) const
inline

Definition at line 79 of file strngs.h.

79  {
80  inT32 len = length() + 1;
81  return strncpy(new char[len], GetCStr(), len);
82  }
int32_t inT32
Definition: host.h:38
inT32 length() const
Definition: strngs.cpp:193

◆ string()

const char * STRING::string ( ) const

Definition at line 198 of file strngs.cpp.

198  {
199  const STRING_HEADER* header = GetHeader();
200  if (header->used_ == 0)
201  return NULL;
202 
203  // mark header length unreliable because tesseract might
204  // cast away the const and mutate the string directly.
205  header->used_ = -1;
206  return GetCStr();
207 }

◆ truncate_at()

void STRING::truncate_at ( inT32  index)

Definition at line 269 of file strngs.cpp.

269  {
270  ASSERT_HOST(index >= 0);
271  FixHeader();
272  char* this_cstr = ensure_cstr(index + 1);
273  this_cstr[index] = '\0';
274  GetHeader()->used_ = index + 1;
275  assert(InvariantOk());
276 }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ unsigned_size()

uinT32 STRING::unsigned_size ( ) const
inline

Definition at line 71 of file strngs.h.

71  {
72  const inT32 len = length();
73  assert(0 <= len);
74  return static_cast<uinT32>(len);
75  }
int32_t inT32
Definition: host.h:38
inT32 length() const
Definition: strngs.cpp:193
uint32_t uinT32
Definition: host.h:39

The documentation for this class was generated from the following files: