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

#include <strngs.h>

List of all members.

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
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.

{
// Empty STRINGs contain just the "\0".
memcpy(AllocData(1, kMinCapacity), "", 1);
}
STRING::STRING ( const STRING string)

Definition at line 103 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 112 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 124 of file strngs.cpp.

{
DiscardData();
}

Member Function Documentation

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

Definition at line 334 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 365 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());
}
BOOL8 STRING::contains ( const char  c) const

Definition at line 147 of file strngs.cpp.

{
return (c != '\0') && (strchr (GetCStr(), c) != NULL);
}
bool STRING::DeSerialize ( bool  swap,
FILE *  fp 
)

Definition at line 137 of file strngs.cpp.

{
inT32 len;
if (fread(&len, sizeof(len), 1, fp) != 1) return false;
if (swap)
ReverseN(&len, sizeof(len));
if (fread(GetCStr(), 1, len, fp) != len) return false;
return true;
}
void STRING::ensure ( inT32  min_capacity)
inline

Definition at line 99 of file strngs.h.

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

Definition at line 151 of file strngs.cpp.

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

Definition at line 270 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 282 of file strngs.cpp.

{
FixHeader();
const STRING_HEADER* this_header = GetHeader();
if (cstr == NULL)
return this_header->used_ > 1; // either '\0' or NULL
else {
inT32 length = strlen(cstr) + 1;
return (this_header->used_ != length)
|| (memcmp(GetCStr(), cstr, length) != 0);
}
}
STRING STRING::operator+ ( const STRING string) const

Definition at line 378 of file strngs.cpp.

{
STRING result(*this);
result += str;
assert(InvariantOk());
return result;
}
STRING STRING::operator+ ( const char  ch) const

Definition at line 387 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 407 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;
}
STRING & STRING::operator+= ( const STRING string)

Definition at line 311 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 432 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 344 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;
}
STRING & STRING::operator= ( const STRING string)

Definition at line 295 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;
}
BOOL8 STRING::operator== ( const STRING string) const

Definition at line 258 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 230 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 129 of file strngs.cpp.

{
inT32 len = length();
if (fwrite(&len, sizeof(len), 1, fp) != 1) return false;
if (fwrite(GetCStr(), 1, len, fp) != len) return false;
return true;
}
inT32 STRING::size ( ) const
inline

Definition at line 56 of file strngs.h.

{ return length(); }
void STRING::split ( const char  c,
GenericVector< STRING > *  splited 
)

Definition at line 238 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

Definition at line 59 of file strngs.h.

{
inT32 len = length() + 1;
return strncpy(new char[len], GetCStr(), len);
}
const char * STRING::string ( ) const

Definition at line 156 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 223 of file strngs.cpp.

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

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