tesseract 4.1.1
Loading...
Searching...
No Matches
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 * string () 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 103 of file strngs.cpp.

103 {
104 // Empty STRINGs contain just the "\0".
105 memcpy(AllocData(1, kMinCapacity), "", 1);
106}
const int kMinCapacity
Definition: strngs.cpp:49

◆ STRING() [2/4]

STRING::STRING ( const STRING string)

Definition at line 108 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);
114 assert(InvariantOk());
115}

◆ STRING() [3/4]

STRING::STRING ( const char *  string)

Definition at line 117 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 }
126 assert(InvariantOk());
127}

◆ STRING() [4/4]

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

Definition at line 129 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';
137 }
138}
int32_t length() const
Definition: strngs.cpp:189

◆ ~STRING()

STRING::~STRING ( )

Definition at line 140 of file strngs.cpp.

140 {
141 DiscardData();
142}

Member Function Documentation

◆ add_str_double()

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

Definition at line 387 of file strngs.cpp.

387 {
388 if (str != nullptr)
389 *this += str;
390 std::stringstream stream;
391 // Use "C" locale (needed for double value).
392 stream.imbue(std::locale::classic());
393 // Use 8 digits for double value.
394 stream.precision(8);
395 stream << number;
396 *this += stream.str().c_str();
397}

◆ add_str_int()

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

Definition at line 377 of file strngs.cpp.

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

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

205 {
206 return string();
207}
const char * string() const
Definition: strngs.cpp:194

◆ contains()

bool STRING::contains ( char  c) const

Definition at line 185 of file strngs.cpp.

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

◆ DeSerialize() [1/2]

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

Definition at line 159 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);
167 return tesseract::DeSerialize(fp, GetCStr(), len);
168}
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:185
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:28
void truncate_at(int32_t index)
Definition: strngs.cpp:265

◆ DeSerialize() [2/2]

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

Definition at line 171 of file strngs.cpp.

171 {
172 uint32_t len;
173 if (!fp->DeSerialize(&len)) return false;
174 truncate_at(len);
175 return fp->DeSerialize(GetCStr(), len);
176}
bool DeSerialize(char *data, size_t count=1)
Definition: serialis.cpp:104

◆ ensure()

void STRING::ensure ( int32_t  min_capacity)
inline

Definition at line 122 of file strngs.h.

122 {
123 ensure_cstr(min_capacity);
124 }

◆ length()

int32_t STRING::length ( ) const

Definition at line 189 of file strngs.cpp.

189 {
190 FixHeader();
191 return GetHeader()->used_ - 1;
192}

◆ operator!=() [1/2]

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

Definition at line 325 of file strngs.cpp.

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

◆ operator!=() [2/2]

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

Definition at line 313 of file strngs.cpp.

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

◆ operator+() [1/2]

STRING STRING::operator+ ( 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+() [2/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}

◆ operator+=() [1/3]

STRING & STRING::operator+= ( 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+=() [2/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+=() [3/3]

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

Definition at line 354 of file strngs.cpp.

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

◆ 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}

◆ operator=() [2/2]

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

Definition at line 338 of file strngs.cpp.

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

◆ operator==()

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

Definition at line 301 of file strngs.cpp.

301 {
302 FixHeader();
303 str.FixHeader();
304 const STRING_HEADER* str_header = str.GetHeader();
305 const STRING_HEADER* this_header = GetHeader();
306 const int this_used = this_header->used_;
307 const int str_used = str_header->used_;
308
309 return (this_used == str_used)
310 && (memcmp(GetCStr(), str.GetCStr(), this_used) == 0);
311}

◆ operator[]()

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

Definition at line 274 of file strngs.cpp.

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

◆ Serialize() [1/2]

bool STRING::Serialize ( FILE *  fp) const

Definition at line 146 of file strngs.cpp.

146 {
147 uint32_t len = length();
148 return tesseract::Serialize(fp, &len) &&
149 tesseract::Serialize(fp, GetCStr(), len);
150}
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:60

◆ Serialize() [2/2]

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

Definition at line 152 of file strngs.cpp.

152 {
153 uint32_t len = length();
154 return fp->Serialize(&len) &&
155 fp->Serialize(GetCStr(), len);
156}
bool Serialize(const char *data, size_t count=1)
Definition: serialis.cpp:148

◆ size()

int32_t STRING::size ( ) const
inline

Definition at line 68 of file strngs.h.

68 {
69 return length();
70 }

◆ SkipDeSerialize()

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

Definition at line 179 of file strngs.cpp.

179 {
180 uint32_t len;
181 if (!fp->DeSerialize(&len)) return false;
182 return fp->Skip(len);
183}
bool Skip(size_t count)
Definition: serialis.cpp:192

◆ split()

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

Definition at line 282 of file strngs.cpp.

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

◆ strdup()

char * STRING::strdup ( ) const
inline

Definition at line 80 of file strngs.h.

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

◆ string()

const char * STRING::string ( ) const

Definition at line 194 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;
202 return GetCStr();
203}

◆ truncate_at()

void STRING::truncate_at ( int32_t  index)

Definition at line 265 of file strngs.cpp.

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

◆ unsigned_size()

uint32_t STRING::unsigned_size ( ) const
inline

Definition at line 72 of file strngs.h.

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

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