Tesseract  3.02
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 & 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 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.

               {
  // 0 indicates old NULL -- it doesnt even have '\0'
  AllocData(0, kMinCapacity);
}
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) {
    AllocData(0, 0);
  } else {
    int len = strlen(cstr) + 1;
    char* this_cstr = AllocData(len, len);
    memcpy(this_cstr, cstr, len);
  }
  assert(InvariantOk());
}
STRING::~STRING ( )

Definition at line 123 of file strngs.cpp.

                {
  DiscardData();
}

Member Function Documentation

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

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

Definition at line 146 of file strngs.cpp.

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

Definition at line 136 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 (fread(GetCStr(), 1, len, fp) != len) return false;
  return true;
}
void STRING::ensure ( inT32  min_capacity) [inline]

Definition at line 91 of file strngs.h.

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

Definition at line 150 of file strngs.cpp.

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

Definition at line 269 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 281 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 366 of file strngs.cpp.

                                                {
  STRING result(*this);
  result += str;

  assert(InvariantOk());
  return result;
}
STRING STRING::operator+ ( const char  ch) const

Definition at line 375 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 395 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 310 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 420 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 343 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 zero capacity buffer, consistent with the corresponding
    // copy constructor.
    DiscardData();
    AllocData(0, 0);
  }

  assert(InvariantOk());
  return *this;
}
STRING & STRING::operator= ( const STRING string)

Definition at line 294 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 257 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 229 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 128 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 237 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);
  }
}
const char * STRING::string ( ) const

Definition at line 155 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 222 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: