tesseract
3.03
|
#include <strngs.h>
Classes | |
struct | STRING_HEADER |
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 |
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 |
STRING & | operator= (const char *string) |
STRING & | operator= (const STRING &string) |
STRING | operator+ (const STRING &string) const |
STRING | operator+ (const char ch) const |
STRING & | operator+= (const char *string) |
STRING & | operator+= (const STRING &string) |
STRING & | operator+= (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) |
STRING::STRING | ( | ) |
Definition at line 100 of file strngs.cpp.
{ // Empty STRINGs contain just the "\0". memcpy(AllocData(1, kMinCapacity), "", 1); }
STRING::STRING | ( | const STRING & | string | ) |
Definition at line 105 of file strngs.cpp.
{ str.FixHeader(); const STRING_HEADER* str_header = str.GetHeader(); int str_used = str_header->used_; char *this_cstr = AllocData(str_used, str_used); memcpy(this_cstr, str.GetCStr(), str_used); assert(InvariantOk()); }
STRING::STRING | ( | const char * | string | ) |
Definition at line 114 of file strngs.cpp.
{ if (cstr == NULL) { // Empty STRINGs contain just the "\0". memcpy(AllocData(1, kMinCapacity), "", 1); } else { int len = strlen(cstr) + 1; char* this_cstr = AllocData(len, len); memcpy(this_cstr, cstr, len); } assert(InvariantOk()); }
STRING::~STRING | ( | ) |
Definition at line 126 of file strngs.cpp.
{ DiscardData(); }
void STRING::add_str_double | ( | const char * | str, |
double | number | ||
) |
Definition at line 352 of file strngs.cpp.
{ if (str != NULL) *this += str; // Allow space for the maximum possible length of %8g. char num_buffer[kMaxDoubleSize]; snprintf(num_buffer, kMaxDoubleSize - 1, "%.8g", number); num_buffer[kMaxDoubleSize - 1] = '\0'; *this += num_buffer; }
void STRING::add_str_int | ( | const char * | str, |
int | number | ||
) |
Definition at line 342 of file strngs.cpp.
{ if (str != NULL) *this += str; // Allow space for the maximum possible length of inT64. char num_buffer[kMaxIntSize]; snprintf(num_buffer, kMaxIntSize - 1, "%d", number); num_buffer[kMaxIntSize - 1] = '\0'; *this += num_buffer; }
void STRING::assign | ( | const char * | cstr, |
int | len | ||
) |
Definition at line 383 of file strngs.cpp.
{ STRING_HEADER* this_header = GetHeader(); this_header->used_ = 0; // dont bother copying data if need to realloc char* this_cstr = ensure_cstr(len + 1); // +1 for '\0' this_header = GetHeader(); // for realloc memcpy(this_cstr, cstr, len); this_cstr[len] = '\0'; this_header->used_ = len + 1; assert(InvariantOk()); }
const char * STRING::c_str | ( | ) | const |
Definition at line 169 of file strngs.cpp.
{ return string(); }
BOOL8 STRING::contains | ( | const char | c | ) | const |
Definition at line 149 of file strngs.cpp.
{ return (c != '\0') && (strchr (GetCStr(), c) != NULL); }
bool STRING::DeSerialize | ( | bool | swap, |
FILE * | fp | ||
) |
Definition at line 139 of file strngs.cpp.
{ inT32 len; if (fread(&len, sizeof(len), 1, fp) != 1) return false; if (swap) ReverseN(&len, sizeof(len)); truncate_at(len); if (static_cast<int>(fread(GetCStr(), 1, len, fp)) != len) return false; return true; }
void STRING::ensure | ( | inT32 | min_capacity | ) | [inline] |
inT32 STRING::length | ( | ) | const |
Definition at line 153 of file strngs.cpp.
{
FixHeader();
return GetHeader()->used_ - 1;
}
BOOL8 STRING::operator!= | ( | const STRING & | string | ) | const |
Definition at line 278 of file strngs.cpp.
{ FixHeader(); str.FixHeader(); const STRING_HEADER* str_header = str.GetHeader(); const STRING_HEADER* this_header = GetHeader(); int this_used = this_header->used_; int str_used = str_header->used_; return (this_used != str_used) || (memcmp(GetCStr(), str.GetCStr(), this_used) != 0); }
BOOL8 STRING::operator!= | ( | const char * | string | ) | const |
Definition at line 290 of file strngs.cpp.
Definition at line 396 of file strngs.cpp.
{ STRING result(*this); result += str; assert(InvariantOk()); return result; }
STRING STRING::operator+ | ( | const char | ch | ) | const |
Definition at line 405 of file strngs.cpp.
{ STRING result; FixHeader(); const STRING_HEADER* this_header = GetHeader(); int this_used = this_header->used_; char* result_cstr = result.ensure_cstr(this_used + 1); STRING_HEADER* result_header = result.GetHeader(); int result_used = result_header->used_; // copies '\0' but we'll overwrite that memcpy(result_cstr, GetCStr(), this_used); result_cstr[result_used] = ch; // overwrite old '\0' result_cstr[result_used + 1] = '\0'; // append on '\0' ++result_header->used_; assert(InvariantOk()); return result; }
STRING & STRING::operator+= | ( | const char * | string | ) |
Definition at line 425 of file strngs.cpp.
{ if (!str || !*str) // empty string has no effect return *this; FixHeader(); int len = strlen(str) + 1; int this_used = GetHeader()->used_; char* this_cstr = ensure_cstr(this_used + len); STRING_HEADER* this_header = GetHeader(); // after ensure for realloc // if we had non-empty string then append overwriting old '\0' // otherwise replace if (this_used > 0) { memcpy(this_cstr + this_used - 1, str, len); this_header->used_ += len - 1; } else { memcpy(this_cstr, str, len); this_header->used_ = len; } assert(InvariantOk()); return *this; }
Definition at line 319 of file strngs.cpp.
{ FixHeader(); str.FixHeader(); const STRING_HEADER* str_header = str.GetHeader(); const char* str_cstr = str.GetCStr(); int str_used = str_header->used_; int this_used = GetHeader()->used_; char* this_cstr = ensure_cstr(this_used + str_used); STRING_HEADER* this_header = GetHeader(); // after ensure for realloc if (this_used > 1) { memcpy(this_cstr + this_used - 1, str_cstr, str_used); this_header->used_ += str_used - 1; // overwrite '\0' } else { memcpy(this_cstr, str_cstr, str_used); this_header->used_ = str_used; } assert(InvariantOk()); return *this; }
STRING & STRING::operator+= | ( | const char | ch | ) |
Definition at line 450 of file strngs.cpp.
{ if (ch == '\0') return *this; FixHeader(); int this_used = GetHeader()->used_; char* this_cstr = ensure_cstr(this_used + 1); STRING_HEADER* this_header = GetHeader(); if (this_used > 0) --this_used; // undo old empty null if there was one this_cstr[this_used++] = ch; // append ch to end this_cstr[this_used++] = '\0'; // append '\0' after ch this_header->used_ = this_used; assert(InvariantOk()); return *this; }
STRING & STRING::operator= | ( | const char * | string | ) |
Definition at line 362 of file strngs.cpp.
{ STRING_HEADER* this_header = GetHeader(); if (cstr) { int len = strlen(cstr) + 1; this_header->used_ = 0; // dont bother copying data if need to realloc char* this_cstr = ensure_cstr(len); this_header = GetHeader(); // for realloc memcpy(this_cstr, cstr, len); this_header->used_ = len; } else { // Reallocate to same state as default constructor. DiscardData(); // Empty STRINGs contain just the "\0". memcpy(AllocData(1, kMinCapacity), "", 1); } assert(InvariantOk()); return *this; }
Definition at line 303 of file strngs.cpp.
{ str.FixHeader(); const STRING_HEADER* str_header = str.GetHeader(); int str_used = str_header->used_; GetHeader()->used_ = 0; // clear since ensure doesnt need to copy data char* this_cstr = ensure_cstr(str_used); STRING_HEADER* this_header = GetHeader(); memcpy(this_cstr, str.GetCStr(), str_used); this_header->used_ = str_used; assert(InvariantOk()); return *this; }
Definition at line 266 of file strngs.cpp.
{ FixHeader(); str.FixHeader(); const STRING_HEADER* str_header = str.GetHeader(); const STRING_HEADER* this_header = GetHeader(); int this_used = this_header->used_; int str_used = str_header->used_; return (this_used == str_used) && (memcmp(GetCStr(), str.GetCStr(), this_used) == 0); }
char & STRING::operator[] | ( | inT32 | index | ) | const |
Definition at line 238 of file strngs.cpp.
{ // Code is casting away this const and mutating the string, // so mark used_ as -1 to flag it unreliable. GetHeader()->used_ = -1; return ((char *)GetCStr())[index]; }
bool STRING::Serialize | ( | FILE * | fp | ) | const |
Definition at line 131 of file strngs.cpp.
inT32 STRING::size | ( | ) | const [inline] |
void STRING::split | ( | const char | c, |
GenericVector< STRING > * | splited | ||
) |
Definition at line 246 of file strngs.cpp.
{ int start_index = 0; for (int i = 0; i < length(); i++) { if ((*this)[i] == c) { if (i != start_index) { (*this)[i] = '\0'; STRING tmp = GetCStr() + start_index; splited->push_back(tmp); (*this)[i] = c; } start_index = i + 1; } } if (length() != start_index) { STRING tmp = GetCStr() + start_index; splited->push_back(tmp); } }
char* STRING::strdup | ( | ) | const [inline] |
const char * STRING::string | ( | ) | const |
Definition at line 158 of file strngs.cpp.
{ const STRING_HEADER* header = GetHeader(); if (header->used_ == 0) return NULL; // mark header length unreliable because tesseract might // cast away the const and mutate the string directly. header->used_ = -1; return GetCStr(); }
void STRING::truncate_at | ( | inT32 | index | ) |
Definition at line 229 of file strngs.cpp.
{ ASSERT_HOST(index >= 0); FixHeader(); char* this_cstr = ensure_cstr(index + 1); this_cstr[index] = '\0'; GetHeader()->used_ = index + 1; assert(InvariantOk()); }