tesseract  3.05.02
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 (bool swap, tesseract::TFile *fp)
 
BOOL8 contains (const char c) const
 
inT32 length () const
 
inT32 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 (bool swap, tesseract::TFile *fp)
 

Detailed Description

Definition at line 44 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 }
const int kMinCapacity
Definition: strngs.cpp:52
inT32 length() const
Definition: strngs.cpp:196

◆ ~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 394 of file strngs.cpp.

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

◆ add_str_int()

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

Definition at line 384 of file strngs.cpp.

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

◆ assign()

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

Definition at line 425 of file strngs.cpp.

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

◆ c_str()

const char * STRING::c_str ( ) const

Definition at line 212 of file strngs.cpp.

212  {
213  return string();
214 }
const char * string() const
Definition: strngs.cpp:201

◆ contains()

BOOL8 STRING::contains ( const char  c) const

Definition at line 192 of file strngs.cpp.

192  {
193  return (c != '\0') && (strchr (GetCStr(), c) != NULL);
194 }

◆ 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:272
int inT32
Definition: host.h:35
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177

◆ DeSerialize() [2/2]

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

Definition at line 174 of file strngs.cpp.

174  {
175  inT32 len;
176  if (fp->FRead(&len, sizeof(len), 1) != 1) return false;
177  if (swap)
178  ReverseN(&len, sizeof(len));
179  truncate_at(len);
180  if (fp->FRead(GetCStr(), 1, len) != len) return false;
181  return true;
182 }
void truncate_at(inT32 index)
Definition: strngs.cpp:272
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:91
int inT32
Definition: host.h:35
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177

◆ ensure()

void STRING::ensure ( inT32  min_capacity)
inline

Definition at line 114 of file strngs.h.

114 { ensure_cstr(min_capacity); }

◆ length()

inT32 STRING::length ( ) const

Definition at line 196 of file strngs.cpp.

196  {
197  FixHeader();
198  return GetHeader()->used_ - 1;
199 }

◆ operator!=() [1/2]

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

Definition at line 320 of file strngs.cpp.

320  {
321  FixHeader();
322  str.FixHeader();
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_;
327 
328  return (this_used != str_used)
329  || (memcmp(GetCStr(), str.GetCStr(), this_used) != 0);
330 }

◆ operator!=() [2/2]

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

Definition at line 332 of file strngs.cpp.

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

◆ operator+() [1/2]

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

Definition at line 438 of file strngs.cpp.

438  {
439  STRING result(*this);
440  result += str;
441 
442  assert(InvariantOk());
443  return result;
444 }
Definition: strngs.h:44

◆ operator+() [2/2]

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

Definition at line 447 of file strngs.cpp.

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

◆ operator+=() [1/3]

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

Definition at line 467 of file strngs.cpp.

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

◆ operator+=() [2/3]

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

Definition at line 361 of file strngs.cpp.

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

◆ operator+=() [3/3]

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

Definition at line 492 of file strngs.cpp.

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

◆ operator=() [1/2]

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

Definition at line 404 of file strngs.cpp.

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

◆ operator=() [2/2]

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

Definition at line 345 of file strngs.cpp.

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

◆ operator==()

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

Definition at line 308 of file strngs.cpp.

308  {
309  FixHeader();
310  str.FixHeader();
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_;
315 
316  return (this_used == str_used)
317  && (memcmp(GetCStr(), str.GetCStr(), this_used) == 0);
318 }

◆ operator[]()

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

Definition at line 281 of file strngs.cpp.

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

◆ 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 length() const
Definition: strngs.cpp:196
int inT32
Definition: host.h:35

◆ 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 length() const
Definition: strngs.cpp:196
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:131
int inT32
Definition: host.h:35

◆ size()

inT32 STRING::size ( ) const
inline

Definition at line 68 of file strngs.h.

68 { return length(); }
inT32 length() const
Definition: strngs.cpp:196

◆ SkipDeSerialize()

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

Definition at line 185 of file strngs.cpp.

185  {
186  inT32 len;
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;
190 }
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:91
int inT32
Definition: host.h:35
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177

◆ split()

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

Definition at line 289 of file strngs.cpp.

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

◆ strdup()

char* STRING::strdup ( ) const
inline

Definition at line 72 of file strngs.h.

72  {
73  inT32 len = length() + 1;
74  return strncpy(new char[len], GetCStr(), len);
75  }
inT32 length() const
Definition: strngs.cpp:196
int inT32
Definition: host.h:35

◆ string()

const char * STRING::string ( ) const

Definition at line 201 of file strngs.cpp.

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

◆ truncate_at()

void STRING::truncate_at ( inT32  index)

Definition at line 272 of file strngs.cpp.

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

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