Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
STRING Class Reference

#include <strngs.h>

Public Member Functions

 STRING ()
 
 STRING (const STRING &string)
 
 STRING (const char *string)
 
 ~STRING ()
 
bool Serialize (FILE *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 
BOOL8 contains (const char c) const
 
inT32 length () const
 
inT32 size () const
 
const char * string () 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 ensure (inT32 min_capacity)
 

Detailed Description

Definition at line 40 of file strngs.h.

Constructor & Destructor Documentation

STRING::STRING ( )

Definition at line 98 of file strngs.cpp.

98  {
99  // Empty STRINGs contain just the "\0".
100  memcpy(AllocData(1, kMinCapacity), "", 1);
101 }
const int kMinCapacity
Definition: strngs.cpp:45
STRING::STRING ( const STRING string)

Definition at line 103 of file strngs.cpp.

103  {
104  str.FixHeader();
105  const STRING_HEADER* str_header = str.GetHeader();
106  int str_used = str_header->used_;
107  char *this_cstr = AllocData(str_used, str_used);
108  memcpy(this_cstr, str.GetCStr(), str_used);
109  assert(InvariantOk());
110 }
STRING::STRING ( const char *  string)

Definition at line 112 of file strngs.cpp.

112  {
113  if (cstr == NULL) {
114  // Empty STRINGs contain just the "\0".
115  memcpy(AllocData(1, kMinCapacity), "", 1);
116  } else {
117  int len = strlen(cstr) + 1;
118  char* this_cstr = AllocData(len, len);
119  memcpy(this_cstr, cstr, len);
120  }
121  assert(InvariantOk());
122 }
#define NULL
Definition: host.h:144
const int kMinCapacity
Definition: strngs.cpp:45
STRING::~STRING ( )

Definition at line 124 of file strngs.cpp.

124  {
125  DiscardData();
126 }

Member Function Documentation

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

Definition at line 334 of file strngs.cpp.

334  {
335  if (str != NULL)
336  *this += str;
337  // Allow space for the maximum possible length of inT64.
338  char num_buffer[kMaxIntSize];
339  snprintf(num_buffer, kMaxIntSize - 1, "%d", number);
340  num_buffer[kMaxIntSize - 1] = '\0';
341  *this += num_buffer;
342 }
#define NULL
Definition: host.h:144
const int kMaxIntSize
Definition: strngs.cpp:29
void STRING::assign ( const char *  cstr,
int  len 
)

Definition at line 365 of file strngs.cpp.

365  {
366  STRING_HEADER* this_header = GetHeader();
367  this_header->used_ = 0; // dont bother copying data if need to realloc
368  char* this_cstr = ensure_cstr(len + 1); // +1 for '\0'
369 
370  this_header = GetHeader(); // for realloc
371  memcpy(this_cstr, cstr, len);
372  this_cstr[len] = '\0';
373  this_header->used_ = len + 1;
374 
375  assert(InvariantOk());
376 }
BOOL8 STRING::contains ( const char  c) const

Definition at line 147 of file strngs.cpp.

147  {
148  return (c != '\0') && (strchr (GetCStr(), c) != NULL);
149 }
#define NULL
Definition: host.h:144
bool STRING::DeSerialize ( bool  swap,
FILE *  fp 
)

Definition at line 137 of file strngs.cpp.

137  {
138  inT32 len;
139  if (fread(&len, sizeof(len), 1, fp) != 1) return false;
140  if (swap)
141  ReverseN(&len, sizeof(len));
142  truncate_at(len);
143  if (fread(GetCStr(), 1, len, fp) != len) return false;
144  return true;
145 }
void truncate_at(inT32 index)
Definition: strngs.cpp:223
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:126
int inT32
Definition: host.h:102
void STRING::ensure ( inT32  min_capacity)
inline

Definition at line 99 of file strngs.h.

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

Definition at line 151 of file strngs.cpp.

151  {
152  FixHeader();
153  return GetHeader()->used_ - 1;
154 }
BOOL8 STRING::operator!= ( const STRING string) const

Definition at line 270 of file strngs.cpp.

270  {
271  FixHeader();
272  str.FixHeader();
273  const STRING_HEADER* str_header = str.GetHeader();
274  const STRING_HEADER* this_header = GetHeader();
275  int this_used = this_header->used_;
276  int str_used = str_header->used_;
277 
278  return (this_used != str_used)
279  || (memcmp(GetCStr(), str.GetCStr(), this_used) != 0);
280 }
BOOL8 STRING::operator!= ( const char *  string) const

Definition at line 282 of file strngs.cpp.

282  {
283  FixHeader();
284  const STRING_HEADER* this_header = GetHeader();
285 
286  if (cstr == NULL)
287  return this_header->used_ > 1; // either '\0' or NULL
288  else {
289  inT32 length = strlen(cstr) + 1;
290  return (this_header->used_ != length)
291  || (memcmp(GetCStr(), cstr, length) != 0);
292  }
293 }
inT32 length() const
Definition: strngs.cpp:151
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
STRING STRING::operator+ ( const STRING string) const

Definition at line 378 of file strngs.cpp.

378  {
379  STRING result(*this);
380  result += str;
381 
382  assert(InvariantOk());
383  return result;
384 }
Definition: strngs.h:40
STRING STRING::operator+ ( const char  ch) const

Definition at line 387 of file strngs.cpp.

387  {
388  STRING result;
389  FixHeader();
390  const STRING_HEADER* this_header = GetHeader();
391  int this_used = this_header->used_;
392  char* result_cstr = result.ensure_cstr(this_used + 1);
393  STRING_HEADER* result_header = result.GetHeader();
394  int result_used = result_header->used_;
395 
396  // copies '\0' but we'll overwrite that
397  memcpy(result_cstr, GetCStr(), this_used);
398  result_cstr[result_used] = ch; // overwrite old '\0'
399  result_cstr[result_used + 1] = '\0'; // append on '\0'
400  ++result_header->used_;
401 
402  assert(InvariantOk());
403  return result;
404 }
Definition: strngs.h:40
STRING & STRING::operator+= ( const char *  string)

Definition at line 407 of file strngs.cpp.

407  {
408  if (!str || !*str) // empty string has no effect
409  return *this;
410 
411  FixHeader();
412  int len = strlen(str) + 1;
413  int this_used = GetHeader()->used_;
414  char* this_cstr = ensure_cstr(this_used + len);
415  STRING_HEADER* this_header = GetHeader(); // after ensure for realloc
416 
417  // if we had non-empty string then append overwriting old '\0'
418  // otherwise replace
419  if (this_used > 0) {
420  memcpy(this_cstr + this_used - 1, str, len);
421  this_header->used_ += len - 1;
422  } else {
423  memcpy(this_cstr, str, len);
424  this_header->used_ = len;
425  }
426 
427  assert(InvariantOk());
428  return *this;
429 }
STRING & STRING::operator+= ( const STRING string)

Definition at line 311 of file strngs.cpp.

311  {
312  FixHeader();
313  str.FixHeader();
314  const STRING_HEADER* str_header = str.GetHeader();
315  const char* str_cstr = str.GetCStr();
316  int str_used = str_header->used_;
317  int this_used = GetHeader()->used_;
318  char* this_cstr = ensure_cstr(this_used + str_used);
319 
320  STRING_HEADER* this_header = GetHeader(); // after ensure for realloc
321 
322  if (this_used > 1) {
323  memcpy(this_cstr + this_used - 1, str_cstr, str_used);
324  this_header->used_ += str_used - 1; // overwrite '\0'
325  } else {
326  memcpy(this_cstr, str_cstr, str_used);
327  this_header->used_ = str_used;
328  }
329 
330  assert(InvariantOk());
331  return *this;
332 }
STRING & STRING::operator+= ( const char  ch)

Definition at line 432 of file strngs.cpp.

432  {
433  if (ch == '\0')
434  return *this;
435 
436  FixHeader();
437  int this_used = GetHeader()->used_;
438  char* this_cstr = ensure_cstr(this_used + 1);
439  STRING_HEADER* this_header = GetHeader();
440 
441  if (this_used > 0)
442  --this_used; // undo old empty null if there was one
443 
444  this_cstr[this_used++] = ch; // append ch to end
445  this_cstr[this_used++] = '\0'; // append '\0' after ch
446  this_header->used_ = this_used;
447 
448  assert(InvariantOk());
449  return *this;
450 }
STRING & STRING::operator= ( const char *  string)

Definition at line 344 of file strngs.cpp.

344  {
345  STRING_HEADER* this_header = GetHeader();
346  if (cstr) {
347  int len = strlen(cstr) + 1;
348 
349  this_header->used_ = 0; // dont bother copying data if need to realloc
350  char* this_cstr = ensure_cstr(len);
351  this_header = GetHeader(); // for realloc
352  memcpy(this_cstr, cstr, len);
353  this_header->used_ = len;
354  } else {
355  // Reallocate to same state as default constructor.
356  DiscardData();
357  // Empty STRINGs contain just the "\0".
358  memcpy(AllocData(1, kMinCapacity), "", 1);
359  }
360 
361  assert(InvariantOk());
362  return *this;
363 }
const int kMinCapacity
Definition: strngs.cpp:45
STRING & STRING::operator= ( const STRING string)

Definition at line 295 of file strngs.cpp.

295  {
296  str.FixHeader();
297  const STRING_HEADER* str_header = str.GetHeader();
298  int str_used = str_header->used_;
299 
300  GetHeader()->used_ = 0; // clear since ensure doesnt need to copy data
301  char* this_cstr = ensure_cstr(str_used);
302  STRING_HEADER* this_header = GetHeader();
303 
304  memcpy(this_cstr, str.GetCStr(), str_used);
305  this_header->used_ = str_used;
306 
307  assert(InvariantOk());
308  return *this;
309 }
BOOL8 STRING::operator== ( const STRING string) const

Definition at line 258 of file strngs.cpp.

258  {
259  FixHeader();
260  str.FixHeader();
261  const STRING_HEADER* str_header = str.GetHeader();
262  const STRING_HEADER* this_header = GetHeader();
263  int this_used = this_header->used_;
264  int str_used = str_header->used_;
265 
266  return (this_used == str_used)
267  && (memcmp(GetCStr(), str.GetCStr(), this_used) == 0);
268 }
char & STRING::operator[] ( inT32  index) const

Definition at line 230 of file strngs.cpp.

230  {
231  // Code is casting away this const and mutating the string,
232  // so mark used_ as -1 to flag it unreliable.
233  GetHeader()->used_ = -1;
234  return ((char *)GetCStr())[index];
235 }
bool STRING::Serialize ( FILE *  fp) const

Definition at line 129 of file strngs.cpp.

129  {
130  inT32 len = length();
131  if (fwrite(&len, sizeof(len), 1, fp) != 1) return false;
132  if (fwrite(GetCStr(), 1, len, fp) != len) return false;
133  return true;
134 }
inT32 length() const
Definition: strngs.cpp:151
int inT32
Definition: host.h:102
inT32 STRING::size ( ) const
inline

Definition at line 56 of file strngs.h.

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

Definition at line 238 of file strngs.cpp.

238  {
239  int start_index = 0;
240  for (int i = 0; i < length(); i++) {
241  if ((*this)[i] == c) {
242  if (i != start_index) {
243  (*this)[i] = '\0';
244  STRING tmp = GetCStr() + start_index;
245  splited->push_back(tmp);
246  (*this)[i] = c;
247  }
248  start_index = i + 1;
249  }
250  }
251 
252  if (length() != start_index) {
253  STRING tmp = GetCStr() + start_index;
254  splited->push_back(tmp);
255  }
256 }
inT32 length() const
Definition: strngs.cpp:151
int push_back(T object)
Definition: strngs.h:40
char* STRING::strdup ( ) const
inline

Definition at line 59 of file strngs.h.

59  {
60  inT32 len = length() + 1;
61  return strncpy(new char[len], GetCStr(), len);
62  }
inT32 length() const
Definition: strngs.cpp:151
int inT32
Definition: host.h:102
const char * STRING::string ( ) const

Definition at line 156 of file strngs.cpp.

156  {
157  const STRING_HEADER* header = GetHeader();
158  if (header->used_ == 0)
159  return NULL;
160 
161  // mark header length unreliable because tesseract might
162  // cast away the const and mutate the string directly.
163  header->used_ = -1;
164  return GetCStr();
165 }
#define NULL
Definition: host.h:144
void STRING::truncate_at ( inT32  index)

Definition at line 223 of file strngs.cpp.

223  {
224  char* this_cstr = ensure_cstr(index + 1);
225  this_cstr[index] = '\0';
226  GetHeader()->used_ = index + 1;
227  assert(InvariantOk());
228 }

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