All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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)
 

Detailed Description

Definition at line 44 of file strngs.h.

Constructor & Destructor Documentation

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::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::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
#define NULL
Definition: host.h:144
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:188
#define NULL
Definition: host.h:144
STRING::~STRING ( )

Definition at line 142 of file strngs.cpp.

142  {
143  DiscardData();
144 }

Member Function Documentation

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

Definition at line 386 of file strngs.cpp.

386  {
387  if (str != NULL)
388  *this += str;
389  // Allow space for the maximum possible length of %8g.
390  char num_buffer[kMaxDoubleSize];
391  snprintf(num_buffer, kMaxDoubleSize - 1, "%.8g", number);
392  num_buffer[kMaxDoubleSize - 1] = '\0';
393  *this += num_buffer;
394 }
#define NULL
Definition: host.h:144
const int kMaxDoubleSize
Definition: strngs.cpp:36
void STRING::add_str_int ( const char *  str,
int  number 
)

Definition at line 376 of file strngs.cpp.

376  {
377  if (str != NULL)
378  *this += str;
379  // Allow space for the maximum possible length of inT64.
380  char num_buffer[kMaxIntSize];
381  snprintf(num_buffer, kMaxIntSize - 1, "%d", number);
382  num_buffer[kMaxIntSize - 1] = '\0';
383  *this += num_buffer;
384 }
#define NULL
Definition: host.h:144
const int kMaxIntSize
Definition: strngs.cpp:33
void STRING::assign ( const char *  cstr,
int  len 
)

Definition at line 417 of file strngs.cpp.

417  {
418  STRING_HEADER* this_header = GetHeader();
419  this_header->used_ = 0; // dont bother copying data if need to realloc
420  char* this_cstr = ensure_cstr(len + 1); // +1 for '\0'
421 
422  this_header = GetHeader(); // for realloc
423  memcpy(this_cstr, cstr, len);
424  this_cstr[len] = '\0';
425  this_header->used_ = len + 1;
426 
427  assert(InvariantOk());
428 }
const char * STRING::c_str ( ) const

Definition at line 204 of file strngs.cpp.

204  {
205  return string();
206 }
const char * string() const
Definition: strngs.cpp:193
BOOL8 STRING::contains ( const char  c) const

Definition at line 184 of file strngs.cpp.

184  {
185  return (c != '\0') && (strchr (GetCStr(), c) != NULL);
186 }
#define NULL
Definition: host.h:144
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:264
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177
int inT32
Definition: host.h:102
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:264
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:91
int inT32
Definition: host.h:102
void STRING::ensure ( inT32  min_capacity)
inline

Definition at line 112 of file strngs.h.

112 { ensure_cstr(min_capacity); }
inT32 STRING::length ( ) const

Definition at line 188 of file strngs.cpp.

188  {
189  FixHeader();
190  return GetHeader()->used_ - 1;
191 }
BOOL8 STRING::operator!= ( const STRING string) const

Definition at line 312 of file strngs.cpp.

312  {
313  FixHeader();
314  str.FixHeader();
315  const STRING_HEADER* str_header = str.GetHeader();
316  const STRING_HEADER* this_header = GetHeader();
317  int this_used = this_header->used_;
318  int str_used = str_header->used_;
319 
320  return (this_used != str_used)
321  || (memcmp(GetCStr(), str.GetCStr(), this_used) != 0);
322 }
BOOL8 STRING::operator!= ( const char *  string) const

Definition at line 324 of file strngs.cpp.

324  {
325  FixHeader();
326  const STRING_HEADER* this_header = GetHeader();
327 
328  if (cstr == NULL)
329  return this_header->used_ > 1; // either '\0' or NULL
330  else {
331  inT32 length = strlen(cstr) + 1;
332  return (this_header->used_ != length)
333  || (memcmp(GetCStr(), cstr, length) != 0);
334  }
335 }
inT32 length() const
Definition: strngs.cpp:188
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
STRING STRING::operator+ ( const STRING string) const

Definition at line 430 of file strngs.cpp.

430  {
431  STRING result(*this);
432  result += str;
433 
434  assert(InvariantOk());
435  return result;
436 }
Definition: strngs.h:44
STRING STRING::operator+ ( const char  ch) const

Definition at line 439 of file strngs.cpp.

439  {
440  STRING result;
441  FixHeader();
442  const STRING_HEADER* this_header = GetHeader();
443  int this_used = this_header->used_;
444  char* result_cstr = result.ensure_cstr(this_used + 1);
445  STRING_HEADER* result_header = result.GetHeader();
446  int result_used = result_header->used_;
447 
448  // copies '\0' but we'll overwrite that
449  memcpy(result_cstr, GetCStr(), this_used);
450  result_cstr[result_used] = ch; // overwrite old '\0'
451  result_cstr[result_used + 1] = '\0'; // append on '\0'
452  ++result_header->used_;
453 
454  assert(InvariantOk());
455  return result;
456 }
Definition: strngs.h:44
STRING & STRING::operator+= ( const char *  string)

Definition at line 459 of file strngs.cpp.

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

Definition at line 353 of file strngs.cpp.

353  {
354  FixHeader();
355  str.FixHeader();
356  const STRING_HEADER* str_header = str.GetHeader();
357  const char* str_cstr = str.GetCStr();
358  int str_used = str_header->used_;
359  int this_used = GetHeader()->used_;
360  char* this_cstr = ensure_cstr(this_used + str_used);
361 
362  STRING_HEADER* this_header = GetHeader(); // after ensure for realloc
363 
364  if (this_used > 1) {
365  memcpy(this_cstr + this_used - 1, str_cstr, str_used);
366  this_header->used_ += str_used - 1; // overwrite '\0'
367  } else {
368  memcpy(this_cstr, str_cstr, str_used);
369  this_header->used_ = str_used;
370  }
371 
372  assert(InvariantOk());
373  return *this;
374 }
STRING & STRING::operator+= ( const char  ch)

Definition at line 484 of file strngs.cpp.

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

Definition at line 396 of file strngs.cpp.

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

Definition at line 337 of file strngs.cpp.

337  {
338  str.FixHeader();
339  const STRING_HEADER* str_header = str.GetHeader();
340  int str_used = str_header->used_;
341 
342  GetHeader()->used_ = 0; // clear since ensure doesnt need to copy data
343  char* this_cstr = ensure_cstr(str_used);
344  STRING_HEADER* this_header = GetHeader();
345 
346  memcpy(this_cstr, str.GetCStr(), str_used);
347  this_header->used_ = str_used;
348 
349  assert(InvariantOk());
350  return *this;
351 }
BOOL8 STRING::operator== ( const STRING string) const

Definition at line 300 of file strngs.cpp.

300  {
301  FixHeader();
302  str.FixHeader();
303  const STRING_HEADER* str_header = str.GetHeader();
304  const STRING_HEADER* this_header = GetHeader();
305  int this_used = this_header->used_;
306  int str_used = str_header->used_;
307 
308  return (this_used == str_used)
309  && (memcmp(GetCStr(), str.GetCStr(), this_used) == 0);
310 }
char & STRING::operator[] ( inT32  index) const

Definition at line 273 of file strngs.cpp.

273  {
274  // Code is casting away this const and mutating the string,
275  // so mark used_ as -1 to flag it unreliable.
276  GetHeader()->used_ = -1;
277  return ((char *)GetCStr())[index];
278 }
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:188
int inT32
Definition: host.h:102
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:188
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:131
int inT32
Definition: host.h:102
inT32 STRING::size ( ) const
inline

Definition at line 66 of file strngs.h.

66 { return length(); }
inT32 length() const
Definition: strngs.cpp:188
void STRING::split ( const char  c,
GenericVector< STRING > *  splited 
)

Definition at line 281 of file strngs.cpp.

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

Definition at line 70 of file strngs.h.

70  {
71  inT32 len = length() + 1;
72  return strncpy(new char[len], GetCStr(), len);
73  }
inT32 length() const
Definition: strngs.cpp:188
int inT32
Definition: host.h:102
const char * STRING::string ( ) const

Definition at line 193 of file strngs.cpp.

193  {
194  const STRING_HEADER* header = GetHeader();
195  if (header->used_ == 0)
196  return NULL;
197 
198  // mark header length unreliable because tesseract might
199  // cast away the const and mutate the string directly.
200  header->used_ = -1;
201  return GetCStr();
202 }
#define NULL
Definition: host.h:144
void STRING::truncate_at ( inT32  index)

Definition at line 264 of file strngs.cpp.

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

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