tesseract  4.0.0-1-g2a2b
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)
 
bool contains (const char c) const
 
int32_t length () const
 
int32_t size () const
 
uint32_t unsigned_size () const
 
const char * string () const
 
const char * c_str () const
 
char * strdup () const
 
char & operator[] (int32_t index) const
 
void split (const char c, GenericVector< STRING > *splited)
 
void truncate_at (int32_t index)
 
bool operator== (const STRING &string) const
 
bool operator!= (const STRING &string) const
 
bool 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_t 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:51

◆ 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  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());
117 }

◆ STRING() [3/4]

STRING::STRING ( const char *  string)

Definition at line 119 of file strngs.cpp.

119  {
120  if (cstr == nullptr) {
121  // Empty STRINGs contain just the "\0".
122  memcpy(AllocData(1, kMinCapacity), "", 1);
123  } else {
124  const 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:51

◆ STRING() [4/4]

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

Definition at line 131 of file strngs.cpp.

131  {
132  if (data == nullptr) {
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:51
int32_t length() const
Definition: strngs.cpp:191

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

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

◆ add_str_int()

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

Definition at line 379 of file strngs.cpp.

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

◆ assign()

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

Definition at line 420 of file strngs.cpp.

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

◆ c_str()

const char * STRING::c_str ( ) const

Definition at line 207 of file strngs.cpp.

207  {
208  return string();
209 }
const char * string() const
Definition: strngs.cpp:196

◆ contains()

bool STRING::contains ( const char  c) const

Definition at line 187 of file strngs.cpp.

187  {
188  return (c != '\0') && (strchr (GetCStr(), c) != nullptr);
189 }

◆ DeSerialize() [1/2]

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

Definition at line 161 of file strngs.cpp.

161  {
162  uint32_t len;
163  if (!tesseract::DeSerialize(fp, &len)) return false;
164  if (swap)
165  ReverseN(&len, sizeof(len));
166  // Arbitrarily limit the number of characters to protect against bad data.
167  if (len > UINT16_MAX) return false;
168  truncate_at(len);
169  return tesseract::DeSerialize(fp, GetCStr(), len);
170 }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:178
void truncate_at(int32_t index)
Definition: strngs.cpp:267
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:27

◆ DeSerialize() [2/2]

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

Definition at line 173 of file strngs.cpp.

173  {
174  uint32_t len;
175  if (!fp->DeSerialize(&len)) return false;
176  truncate_at(len);
177  return fp->DeSerialize(GetCStr(), len);
178 }
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:103
void truncate_at(int32_t index)
Definition: strngs.cpp:267

◆ ensure()

void STRING::ensure ( int32_t  min_capacity)
inline

Definition at line 121 of file strngs.h.

121 { ensure_cstr(min_capacity); }

◆ length()

int32_t STRING::length ( ) const

Definition at line 191 of file strngs.cpp.

191  {
192  FixHeader();
193  return GetHeader()->used_ - 1;
194 }

◆ operator!=() [1/2]

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

Definition at line 315 of file strngs.cpp.

315  {
316  FixHeader();
317  str.FixHeader();
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_;
322 
323  return (this_used != str_used)
324  || (memcmp(GetCStr(), str.GetCStr(), this_used) != 0);
325 }

◆ operator!=() [2/2]

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

Definition at line 327 of file strngs.cpp.

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

◆ operator+() [1/2]

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

Definition at line 433 of file strngs.cpp.

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

◆ operator+() [2/2]

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

Definition at line 442 of file strngs.cpp.

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

◆ operator+=() [1/3]

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

Definition at line 462 of file strngs.cpp.

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

◆ operator+=() [2/3]

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

Definition at line 356 of file strngs.cpp.

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

◆ operator+=() [3/3]

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

Definition at line 487 of file strngs.cpp.

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

◆ operator=() [1/2]

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

Definition at line 399 of file strngs.cpp.

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

◆ operator=() [2/2]

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

Definition at line 340 of file strngs.cpp.

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

◆ operator==()

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

Definition at line 303 of file strngs.cpp.

303  {
304  FixHeader();
305  str.FixHeader();
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_;
310 
311  return (this_used == str_used)
312  && (memcmp(GetCStr(), str.GetCStr(), this_used) == 0);
313 }

◆ operator[]()

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

Definition at line 276 of file strngs.cpp.

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

◆ Serialize() [1/2]

bool STRING::Serialize ( FILE *  fp) const

Definition at line 148 of file strngs.cpp.

148  {
149  uint32_t len = length();
150  return tesseract::Serialize(fp, &len) &&
151  tesseract::Serialize(fp, GetCStr(), len);
152 }
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59
int32_t length() const
Definition: strngs.cpp:191

◆ Serialize() [2/2]

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

Definition at line 154 of file strngs.cpp.

154  {
155  uint32_t len = length();
156  return fp->Serialize(&len) &&
157  fp->Serialize(GetCStr(), len);
158 }
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:147
int32_t length() const
Definition: strngs.cpp:191

◆ size()

int32_t STRING::size ( ) const
inline

Definition at line 69 of file strngs.h.

69 { return length(); }
int32_t length() const
Definition: strngs.cpp:191

◆ SkipDeSerialize()

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

Definition at line 181 of file strngs.cpp.

181  {
182  uint32_t len;
183  if (!fp->DeSerialize(&len)) return false;
184  return fp->Skip(len);
185 }
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:103
bool Skip(size_t count)
Definition: serialis.cpp:191

◆ split()

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

Definition at line 284 of file strngs.cpp.

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

◆ strdup()

char* STRING::strdup ( ) const
inline

Definition at line 79 of file strngs.h.

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

◆ string()

const char * STRING::string ( ) const

Definition at line 196 of file strngs.cpp.

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

◆ truncate_at()

void STRING::truncate_at ( int32_t  index)

Definition at line 267 of file strngs.cpp.

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

◆ unsigned_size()

uint32_t STRING::unsigned_size ( ) const
inline

Definition at line 71 of file strngs.h.

71  {
72  const int32_t len = length();
73  assert(0 <= len);
74  return static_cast<uint32_t>(len);
75  }
int32_t length() const
Definition: strngs.cpp:191

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