tesseract  5.0.0-alpha-619-ge9db
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 (char c) const
 
int32_t length () const
 
int32_t size () const
 
uint32_t unsigned_size () const
 
const char * c_str () const
 
char * strdup () const
 
char & operator[] (int32_t index) const
 
void split (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+ (char ch) const
 
STRINGoperator+= (const char *string)
 
STRINGoperator+= (const STRING &string)
 
STRINGoperator+= (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 101 of file strngs.cpp.

103  {
104  // Empty STRINGs contain just the "\0".

◆ STRING() [2/4]

STRING::STRING ( const STRING string)

Definition at line 106 of file strngs.cpp.

108  {
109  str.FixHeader();
110  const STRING_HEADER* str_header = str.GetHeader();
111  const int str_used = str_header->used_;
112  char *this_cstr = AllocData(str_used, str_used);
113  memcpy(this_cstr, str.GetCStr(), str_used);

◆ STRING() [3/4]

STRING::STRING ( const char *  string)

Definition at line 115 of file strngs.cpp.

117  {
118  if (cstr == nullptr) {
119  // Empty STRINGs contain just the "\0".
120  memcpy(AllocData(1, kMinCapacity), "", 1);
121  } else {
122  const int len = strlen(cstr) + 1;
123  char* this_cstr = AllocData(len, len);
124  memcpy(this_cstr, cstr, len);
125  }

◆ STRING() [4/4]

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

Definition at line 127 of file strngs.cpp.

129  {
130  if (data == nullptr) {
131  // Empty STRINGs contain just the "\0".
132  memcpy(AllocData(1, kMinCapacity), "", 1);
133  } else {
134  char* this_cstr = AllocData(length + 1, length + 1);
135  memcpy(this_cstr, data, length);
136  this_cstr[length] = '\0';

◆ ~STRING()

STRING::~STRING ( )

Definition at line 138 of file strngs.cpp.

140  {

Member Function Documentation

◆ add_str_double()

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

Definition at line 380 of file strngs.cpp.

383  {
384  if (str != nullptr)
385  *this += str;
386  std::stringstream stream;
387  // Use "C" locale (needed for double value).
388  stream.imbue(std::locale::classic());
389  // Use 8 digits for double value.
390  stream.precision(8);

◆ add_str_int()

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

Definition at line 370 of file strngs.cpp.

373  {
374  if (str != nullptr)
375  *this += str;
376  // Allow space for the maximum possible length of int64_t.
377  char num_buffer[kMaxIntSize];
378  snprintf(num_buffer, kMaxIntSize - 1, "%d", number);

◆ assign()

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

Definition at line 413 of file strngs.cpp.

416  {
417  STRING_HEADER* this_header = GetHeader();
418  this_header->used_ = 0; // don't bother copying data if need to realloc
419  char* this_cstr = ensure_cstr(len + 1); // +1 for '\0'
420 
421  this_header = GetHeader(); // for realloc
422  memcpy(this_cstr, cstr, len);
423  this_cstr[len] = '\0';
424  this_header->used_ = len + 1;

◆ c_str()

const char * STRING::c_str ( ) const

Definition at line 192 of file strngs.cpp.

194  {
195  const STRING_HEADER* header = GetHeader();
196  if (!header || header->used_ == 0)
197  return nullptr;
198 
199  // mark header length unreliable because tesseract might
200  // cast away the const and mutate the string directly.
201  header->used_ = -1;

◆ contains()

bool STRING::contains ( char  c) const

Definition at line 183 of file strngs.cpp.

185  {

◆ DeSerialize() [1/2]

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

Definition at line 157 of file strngs.cpp.

159  {
160  uint32_t len;
161  if (!tesseract::DeSerialize(fp, &len)) return false;
162  if (swap)
163  ReverseN(&len, sizeof(len));
164  // Arbitrarily limit the number of characters to protect against bad data.
165  if (len > UINT16_MAX) return false;
166  truncate_at(len);

◆ DeSerialize() [2/2]

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

Definition at line 169 of file strngs.cpp.

171  {
172  uint32_t len;
173  if (!fp->DeSerialize(&len)) return false;
174  truncate_at(len);

◆ ensure()

void STRING::ensure ( int32_t  min_capacity)
inline

Definition at line 121 of file strngs.h.

122  {
123  ensure_cstr(min_capacity);

◆ length()

int32_t STRING::length ( ) const

Definition at line 187 of file strngs.cpp.

189  {
190  FixHeader();

◆ operator!=() [1/2]

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

Definition at line 318 of file strngs.cpp.

321  {
322  FixHeader();
323  const STRING_HEADER* this_header = GetHeader();
324 
325  if (cstr == nullptr)
326  return this_header->used_ > 1; // either '\0' or nullptr
327  else {
328  const int32_t length = strlen(cstr) + 1;
329  return (this_header->used_ != length)

◆ operator!=() [2/2]

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

Definition at line 306 of file strngs.cpp.

309  {
310  FixHeader();
311  str.FixHeader();
312  const STRING_HEADER* str_header = str.GetHeader();
313  const STRING_HEADER* this_header = GetHeader();
314  const int this_used = this_header->used_;
315  const int str_used = str_header->used_;
316 

◆ operator+() [1/2]

STRING STRING::operator+ ( char  ch) const

Definition at line 435 of file strngs.cpp.

438  {
439  STRING result;
440  FixHeader();
441  const STRING_HEADER* this_header = GetHeader();
442  const int this_used = this_header->used_;
443  char* result_cstr = result.ensure_cstr(this_used + 1);
444  STRING_HEADER* result_header = result.GetHeader();
445  const int result_used = result_header->used_;
446 
447  // copies '\0' but we'll overwrite that
448  memcpy(result_cstr, GetCStr(), this_used);
449  result_cstr[result_used] = ch; // overwrite old '\0'
450  result_cstr[result_used + 1] = '\0'; // append on '\0'
451  ++result_header->used_;
452 

◆ operator+() [2/2]

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

Definition at line 426 of file strngs.cpp.

429  {
430  STRING result(*this);
431  result += str;
432 

◆ operator+=() [1/3]

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

Definition at line 480 of file strngs.cpp.

483  {
484  if (ch == '\0')
485  return *this;
486 
487  FixHeader();
488  int this_used = GetHeader()->used_;
489  char* this_cstr = ensure_cstr(this_used + 1);
490  STRING_HEADER* this_header = GetHeader();
491 
492  if (this_used > 0)
493  --this_used; // undo old empty null if there was one
494 
495  this_cstr[this_used++] = ch; // append ch to end
496  this_cstr[this_used++] = '\0'; // append '\0' after ch
497  this_header->used_ = this_used;
498 

◆ operator+=() [2/3]

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

Definition at line 455 of file strngs.cpp.

458  {
459  if (!str || !*str) // empty string has no effect
460  return *this;
461 
462  FixHeader();
463  const int len = strlen(str) + 1;
464  const int this_used = GetHeader()->used_;
465  char* this_cstr = ensure_cstr(this_used + len);
466  STRING_HEADER* this_header = GetHeader(); // after ensure for realloc
467 
468  // if we had non-empty string then append overwriting old '\0'
469  // otherwise replace
470  if (this_used > 0) {
471  memcpy(this_cstr + this_used - 1, str, len);
472  this_header->used_ += len - 1;
473  } else {
474  memcpy(this_cstr, str, len);
475  this_header->used_ = len;
476  }
477 

◆ operator+=() [3/3]

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

Definition at line 347 of file strngs.cpp.

350  {
351  FixHeader();
352  str.FixHeader();
353  const STRING_HEADER* str_header = str.GetHeader();
354  const char* str_cstr = str.GetCStr();
355  const int str_used = str_header->used_;
356  const int this_used = GetHeader()->used_;
357  char* this_cstr = ensure_cstr(this_used + str_used);
358 
359  STRING_HEADER* this_header = GetHeader(); // after ensure for realloc
360 
361  if (this_used > 1) {
362  memcpy(this_cstr + this_used - 1, str_cstr, str_used);
363  this_header->used_ += str_used - 1; // overwrite '\0'
364  } else {
365  memcpy(this_cstr, str_cstr, str_used);
366  this_header->used_ = str_used;
367  }
368 

◆ operator=() [1/2]

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

Definition at line 392 of file strngs.cpp.

395  {
396  STRING_HEADER* this_header = GetHeader();
397  if (cstr) {
398  const int len = strlen(cstr) + 1;
399 
400  this_header->used_ = 0; // don't bother copying data if need to realloc
401  char* this_cstr = ensure_cstr(len);
402  this_header = GetHeader(); // for realloc
403  memcpy(this_cstr, cstr, len);
404  this_header->used_ = len;
405  } else {
406  // Reallocate to same state as default constructor.
407  DiscardData();
408  // Empty STRINGs contain just the "\0".
409  memcpy(AllocData(1, kMinCapacity), "", 1);
410  }
411 

◆ operator=() [2/2]

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

Definition at line 331 of file strngs.cpp.

334  {
335  str.FixHeader();
336  const STRING_HEADER* str_header = str.GetHeader();
337  const int str_used = str_header->used_;
338 
339  GetHeader()->used_ = 0; // clear since ensure doesn't need to copy data
340  char* this_cstr = ensure_cstr(str_used);
341  STRING_HEADER* this_header = GetHeader();
342 
343  memcpy(this_cstr, str.GetCStr(), str_used);
344  this_header->used_ = str_used;
345 

◆ operator==()

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

Definition at line 294 of file strngs.cpp.

297  {
298  FixHeader();
299  str.FixHeader();
300  const STRING_HEADER* str_header = str.GetHeader();
301  const STRING_HEADER* this_header = GetHeader();
302  const int this_used = this_header->used_;
303  const int str_used = str_header->used_;
304 

◆ operator[]()

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

Definition at line 267 of file strngs.cpp.

270  {
271  // Code is casting away this const and mutating the string,
272  // so mark used_ as -1 to flag it unreliable.

◆ Serialize() [1/2]

bool STRING::Serialize ( FILE *  fp) const

Definition at line 144 of file strngs.cpp.

146  {
147  uint32_t len = length();
148  return tesseract::Serialize(fp, &len) &&

◆ Serialize() [2/2]

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

Definition at line 150 of file strngs.cpp.

152  {
153  uint32_t len = length();
154  return fp->Serialize(&len) &&

◆ size()

int32_t STRING::size ( ) const
inline

Definition at line 68 of file strngs.h.

69  {
70  return length();

◆ SkipDeSerialize()

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

Definition at line 177 of file strngs.cpp.

179  {
180  uint32_t len;
181  if (!fp->DeSerialize(&len)) return false;

◆ split()

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

Definition at line 275 of file strngs.cpp.

278  {
279  int start_index = 0;
280  const int len = length();
281  for (int i = 0; i < len; i++) {
282  if ((*this)[i] == c) {
283  if (i != start_index) {
284  (*this)[i] = '\0';
285  splited->push_back(STRING(GetCStr() + start_index, i - start_index));
286  (*this)[i] = c;
287  }
288  start_index = i + 1;
289  }
290  }
291 
292  if (len != start_index) {

◆ strdup()

char* STRING::strdup ( ) const
inline

Definition at line 79 of file strngs.h.

80  {
81  int32_t len = length() + 1;
82  return strncpy(new char[len], GetCStr(), len);

◆ truncate_at()

void STRING::truncate_at ( int32_t  index)

Definition at line 258 of file strngs.cpp.

261  {
262  ASSERT_HOST(index >= 0);
263  FixHeader();
264  char* this_cstr = ensure_cstr(index + 1);
265  this_cstr[index] = '\0';

◆ unsigned_size()

uint32_t STRING::unsigned_size ( ) const
inline

Definition at line 72 of file strngs.h.

73  {
74  const int32_t len = length();
75  assert(0 <= len);
76  return static_cast<uint32_t>(len);

The documentation for this class was generated from the following files:
kMaxIntSize
const int kMaxIntSize
Definition: strngs.cpp:32
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
STRING
Definition: strngs.h:45
STRING::truncate_at
void truncate_at(int32_t index)
Definition: strngs.cpp:258
kMinCapacity
const int kMinCapacity
Definition: strngs.cpp:47
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
tesseract::TFile::DeSerialize
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:117
tesseract::TFile::Serialize
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:161
STRING::length
int32_t length() const
Definition: strngs.cpp:187
STRING::STRING
STRING()
Definition: strngs.cpp:101
ReverseN
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:183
tesseract::DeSerialize
bool DeSerialize(FILE *fp, char *data, size_t n=1)
Definition: serialis.cpp:41
tesseract::Serialize
bool Serialize(FILE *fp, const char *data, size_t n=1)
Definition: serialis.cpp:73