cgma
CubitString Class Reference

String class that represents a UTF-8 string. More...

#include <CubitString.hpp>

List of all members.

Public Types

enum  CaseSensitivity { CaseInsensitive, CaseSensitive }

Public Member Functions

 CubitString ()
 Default constructor.
 ~CubitString ()
 Default destructor.
 CubitString (const CubitString &s)
 Copy Constructor.
 CubitString (const char *s)
 Create a string from a char*.
 CubitString (const std::string &s)
 CubitString (int i, char c)
static CubitString CubitStringoperator= (const CubitString &s)
CubitStringoperator+= (const CubitString &s)
bool operator== (const CubitString &s) const
bool operator!= (const CubitString &s) const
bool compare (const CubitString &s, CaseSensitivity cs=CaseSensitive)
char get_at (size_t pos) const
 get a character at a position
void put_at (size_t pos, char c)
 set a character at a position
CubitString substr (size_t first, size_t count=CubitString::npos) const
 get a substring starting at first and counting
void erase (size_t pos, size_t len=npos)
 remove len number of characters starting at a position pos.
void replace (size_t pos, size_t len, const CubitString &str)
 replace a portion of ths string with another string
void replace (const CubitString &to_find, const CubitString &to_replace)
 replace every occurance of string with another string
void to_lower ()
 convert to lower case (Note: this is not internationalized)
void to_upper ()
 convert to upper case (Note: this is not internationalized)
void trim ()
 trim off whitespace from the beginning and end of the string
void simplify ()
void tokenize (char delimiter, std::vector< CubitString > &strings) const
 split a string given a delimiter
size_t find (const CubitString &s, size_t pos=0) const
 find functions
size_t find_first_of (const CubitString &s, size_t pos=0) const
size_t find_first (char c, size_t pos=0) const
size_t find_last (char c, size_t pos=CubitString::npos) const
bool ends_with (const CubitString &str) const
 return whether the string ends with str
bool starts_with (const CubitString &str) const
 return whether the string starts with str
bool is_empty () const
 returns whether the string is empty
size_t length () const
 returns the length of the string
const char * c_str () const
 returns a const char* for the string
const std::string & str () const
 returns a std::string
void clear ()
 clear the string
void resize (size_t n)
 resize the string. If growing, it is filled with null characters.
void resize (size_t n, char c)
 resize the string. If growing, it is filled with the given character.

Static Public Member Functions

template<typename T >
static CubitString number (const T &i)
 Create a string from a integer or other stringstream recognized type.
template<typename T >
static T toNumber (const CubitString &str, bool *ok=NULL)
 Create a value from a CubitString.
static CubitString number (double f, unsigned int s_length=0, unsigned int sig_digits=0)
static CubitString format (const char *fmt,...) CUBIT_STRING_PRINTF_FORMAT(1
 set a formated string with arguments
static void to_lower (char *string)
static void to_upper (char *string)
static CubitString toNative (const char *str)
static CubitString toNative (const CubitString &str)
static std::wstring toWide (const CubitString &str)
static CubitString toNarrow (const std::wstring &str)

Static Public Attributes

static const size_t npos = std::string::npos
 max size of a string

Private Attributes

std::string rep

Friends

CUBIT_UTIL_EXPORT friend
CubitString 
operator+ (const CubitString &s1, const CubitString &s2)
CUBIT_UTIL_EXPORT friend bool operator<= (const CubitString &, const CubitString &)
CUBIT_UTIL_EXPORT friend bool operator>= (const CubitString &, const CubitString &)
CUBIT_UTIL_EXPORT friend bool operator< (const CubitString &, const CubitString &)
CUBIT_UTIL_EXPORT friend bool operator> (const CubitString &, const CubitString &)

Detailed Description

String class that represents a UTF-8 string.

Definition at line 34 of file CubitString.hpp.


Member Enumeration Documentation

Enumerator:
CaseInsensitive 
CaseSensitive 

Definition at line 40 of file CubitString.hpp.


Constructor & Destructor Documentation

Default constructor.

Definition at line 26 of file CubitString.cpp.

{
}

Default destructor.

Definition at line 35 of file CubitString.cpp.

{
}

Copy Constructor.

Definition at line 30 of file CubitString.cpp.

  : rep(s.rep)
{
}
CubitString::CubitString ( const char *  s)

Create a string from a char*.

Definition at line 39 of file CubitString.cpp.

{
  if(s)
  {
    rep = s;
  }
}
CubitString::CubitString ( const std::string &  s)

Definition at line 47 of file CubitString.cpp.

  : rep(s)
{
}
CubitString::CubitString ( int  i,
char  c 
)

Definition at line 52 of file CubitString.cpp.

  : rep(i,c)
{
}

Member Function Documentation

const char * CubitString::c_str ( ) const

returns a const char* for the string

Definition at line 158 of file CubitString.cpp.

{
  return rep.c_str();
}

clear the string

Definition at line 168 of file CubitString.cpp.

{
  rep.clear();
}

compare this string with another and return whether they are equal. one can also specify case sensitivity

Definition at line 128 of file CubitString.cpp.

{
  if(cs == CaseSensitive)
    return this->rep == s.rep;

  // someday we might need to fix this to be unicode aware
  if(strcasecmp(this->rep.c_str(), s.rep.c_str()) == 0)
    return true;
  return false;
}
bool CubitString::ends_with ( const CubitString str) const

return whether the string ends with str

Definition at line 260 of file CubitString.cpp.

{
  if(rep.size() >= str.rep.size())
  {
    size_t offset = rep.size() - str.rep.size();
    return std::equal(str.rep.begin(), str.rep.end(), rep.begin() + offset);
  }
  return false;
}
void CubitString::erase ( size_t  pos,
size_t  len = npos 
)

remove len number of characters starting at a position pos.

Definition at line 239 of file CubitString.cpp.

{
  rep.erase(pos, len);
}
size_t CubitString::find ( const CubitString s,
size_t  pos = 0 
) const

find functions

Definition at line 205 of file CubitString.cpp.

{
  if(pos != 0 && pos >= rep.size())
    throw std::out_of_range("find index out of range");
  return rep.find(s.rep, pos);
}
size_t CubitString::find_first ( char  c,
size_t  pos = 0 
) const

Definition at line 217 of file CubitString.cpp.

{
  return rep.find(c, pos);
}
size_t CubitString::find_first_of ( const CubitString s,
size_t  pos = 0 
) const

Definition at line 212 of file CubitString.cpp.

{
  return rep.find_first_of(s.rep, pos);
}
size_t CubitString::find_last ( char  c,
size_t  pos = CubitString::npos 
) const

Definition at line 222 of file CubitString.cpp.

{
  return rep.find_last_of(c, pos);
}
CubitString CubitString::format ( const char *  fmt,
  ... 
) [static]

set a formated string with arguments

Definition at line 92 of file CubitString.cpp.

{
  va_list args, args2;
  va_start(args, format);
  // copy because first vsnprintf modifies args
  va_copy(args2, args);

  // how much room do we need for our string?
  int num = vsnprintf(NULL, 0, format, args);

  // string plus null terminator
  std::vector<char> str;
  str.resize(num+1);

  // print string
  num = vsnprintf(&str[0], num+1, format, args2);

  // remove extra null terminator
  str.resize(num);

  va_end(args);
  va_end(args2);

  return str.empty() ? CubitString() : CubitString(&str[0]);
}
char CubitString::get_at ( size_t  pos) const

get a character at a position

Definition at line 139 of file CubitString.cpp.

{
  // some legacy code wants to look for the null terminator
  if(pos == rep.size())
    return 0;

  return rep.at(pos);
}
bool CubitString::is_empty ( ) const

returns whether the string is empty

Definition at line 148 of file CubitString.cpp.

{
  return rep.empty();
}
size_t CubitString::length ( ) const

returns the length of the string

Definition at line 153 of file CubitString.cpp.

{
  return rep.length();
}
template<typename T >
static CubitString CubitString::number ( const T &  i) [inline, static]

Create a string from a integer or other stringstream recognized type.

Definition at line 66 of file CubitString.hpp.

  {
    std::stringstream si;
    si << i;
    return CubitString(si.str().c_str());
  }
CubitString CubitString::number ( double  f,
unsigned int  s_length = 0,
unsigned int  sig_digits = 0 
) [static]

Create a string from a double. Use either fixed point or scientific notation, whichever is shorter. s_length is the maximum string length: If s_length > 0, then string will contain no spaces and be close to s_length long without going over. Hence precision is variable.

Definition at line 57 of file CubitString.cpp.

{
  std::stringstream str;
  if(max_length)
  {
    str.width(max_length);
  }
  if(sig_digits)
  {
    str.precision(sig_digits);
  }
  str << f;
  std::string ret = str.str();
  size_t i = ret.find_first_not_of(' ');
  if(i != std::string::npos)
    ret = ret.substr(i);

  // change precision to be short enough
  if (max_length) 
  {
    if(sig_digits == 0)
      sig_digits = max_length;

    while(ret.length() > max_length && sig_digits)
    {
      sig_digits--;
      str.precision(sig_digits);
      str.str(std::string());
      str << f;
      ret = str.str();
    }
  }
  return ret.c_str();
}
bool CubitString::operator!= ( const CubitString s) const

Definition at line 358 of file CubitString.cpp.

{
  return rep != s2.rep;
}
CubitString & CubitString::operator+= ( const CubitString s)

Definition at line 199 of file CubitString.cpp.

{
  rep += s.rep;
  return *this;
}
CubitString & CubitString::operator= ( const CubitString s)

Definition at line 118 of file CubitString.cpp.

{
  rep = s.rep;
  return *this;
}
bool CubitString::operator== ( const CubitString s) const

Definition at line 353 of file CubitString.cpp.

{
  return rep == s2.rep;
}
void CubitString::put_at ( size_t  pos,
char  c 
)

set a character at a position

Definition at line 279 of file CubitString.cpp.

{
  rep.at(pos) = c;
}
void CubitString::replace ( size_t  pos,
size_t  len,
const CubitString str 
)

replace a portion of ths string with another string

Definition at line 244 of file CubitString.cpp.

{
  rep.replace(pos, len, str.rep);
}
void CubitString::replace ( const CubitString to_find,
const CubitString to_replace 
)

replace every occurance of string with another string

Definition at line 249 of file CubitString.cpp.

{
  size_t len = to_find.length();
  size_t len2 = to_replace.length();
  for(size_t idx = rep.find(to_find.c_str()); idx != npos; idx = rep.find(to_find.c_str(), idx))
  {
    rep.replace(idx, len, to_replace.rep);
    idx += len2;
  }
}
void CubitString::resize ( size_t  n)

resize the string. If growing, it is filled with null characters.

Definition at line 173 of file CubitString.cpp.

{
  rep.resize(n);
}
void CubitString::resize ( size_t  n,
char  c 
)

resize the string. If growing, it is filled with the given character.

Definition at line 178 of file CubitString.cpp.

{
  rep.resize(n, c);
}

trim whitespace from the beginning and end of string, and each sequence of internal white space is replaced with a single space

Definition at line 330 of file CubitString.cpp.

{
  trim();
  for(std::string::size_type i = rep.find_first_of(" \t\n\v\f\r"); i != std::string::npos;
      i = rep.find_first_of(" \t\n\v\f\r", i+1))
  {
    std::string::size_type next_i = rep.find_first_not_of(" \t\n\v\f\r", i);
    if(next_i > i+1)
    {
      rep.erase(i+1, next_i-i-1);
      rep.at(i) = ' ';
    }
  }
}
bool CubitString::starts_with ( const CubitString str) const

return whether the string starts with str

Definition at line 270 of file CubitString.cpp.

{
  if(rep.size() >= str.rep.size())
  {
    return std::equal(str.rep.begin(), str.rep.end(), rep.begin());
  }
  return false;
}
const std::string & CubitString::str ( ) const

returns a std::string

Definition at line 163 of file CubitString.cpp.

{
  return rep;
}
CubitString CubitString::substr ( size_t  first,
size_t  count = CubitString::npos 
) const

get a substring starting at first and counting

Definition at line 228 of file CubitString.cpp.

{
  if(first >= rep.size())
    return CubitString();

  if(count > rep.size() - first)
    count = rep.size() - first;

  return rep.substr(first, count).c_str();
}

convert to lower case (Note: this is not internationalized)

Definition at line 284 of file CubitString.cpp.

{
  for(size_t i=0; i<rep.size(); i++)
  {
    rep.at(i) = tolower(rep.at(i));
  }
}
void CubitString::to_lower ( char *  string) [static]

Definition at line 300 of file CubitString.cpp.

{
    // convert this string to lower case
  char *p = string;
  while (*p != '\0')
  {
    *p = tolower (*p);
    p++;
  }
}

convert to upper case (Note: this is not internationalized)

Definition at line 292 of file CubitString.cpp.

{
  for(size_t i=0; i<rep.size(); i++)
  {
    rep.at(i) = toupper(rep.at(i));
  }
}
void CubitString::to_upper ( char *  string) [static]

Definition at line 311 of file CubitString.cpp.

{
    // convert this string to upper case
  char *p = string;
  while (*p != '\0')
  {
    *p = toupper (*p);
    p++;
  }
}
void CubitString::tokenize ( char  delimiter,
std::vector< CubitString > &  strings 
) const

split a string given a delimiter

Definition at line 345 of file CubitString.cpp.

{
  std::stringstream ss(rep);
  std::string s;
  while(std::getline(ss, s, delimiter))
    strings.push_back(CubitString(s.c_str()));
}
CubitString CubitString::toNarrow ( const std::wstring &  str) [static]

Definition at line 462 of file CubitString.cpp.

{
#ifdef _WIN32
  return toUtf8(wstr.c_str()).c_str();
#else
  CubitString str;
  size_t len = wcstombs(NULL, wstr.c_str(), 0) + 1;
  if(len > 0)
  {
    std::vector<char> chars(len);
    wcstombs(&chars[0], wstr.c_str(), len);
    str = &chars[0];
  }
  return str;
#endif
}
CubitString CubitString::toNative ( const char *  str) [static]

Definition at line 433 of file CubitString.cpp.

{
  return str;
}
CubitString CubitString::toNative ( const CubitString str) [static]

Definition at line 438 of file CubitString.cpp.

{
  return str;
}
template<typename T >
static T CubitString::toNumber ( const CubitString str,
bool *  ok = NULL 
) [inline, static]

Create a value from a CubitString.

Definition at line 75 of file CubitString.hpp.

  {
    std::istringstream ss(str.c_str());
    T result;
    bool temp_ok = ss >> result;
    if(ok)
      (*ok) = temp_ok;
    return result;
  }
std::wstring CubitString::toWide ( const CubitString str) [static]

Definition at line 445 of file CubitString.cpp.

{
#ifdef _WIN32
  return toUtf16(str);
#else
  std::wstring wstr;
  size_t len = mbstowcs(NULL, str.c_str(), 0) + 1;
  if(len > 0)
  {
    std::vector<wchar_t> wchar(len);
    mbstowcs(&wchar[0], str.c_str(), len);
    wstr = &wchar[0];
  }
  return wstr;
#endif
}

trim off whitespace from the beginning and end of the string

Definition at line 322 of file CubitString.cpp.

{
  std::string::size_type start = rep.find_first_not_of(" \t\n\v\f\r");
  rep.erase(0, start);
  std::string::size_type end = rep.find_last_not_of(" \t\n\v\f\r");
  rep.erase(end+1, std::string::npos);
}

Friends And Related Function Documentation

CUBIT_UTIL_EXPORT friend CubitString operator+ ( const CubitString s1,
const CubitString s2 
) [friend]

Definition at line 184 of file CubitString.cpp.

{
  return CubitString(s1) += s2;
}
CUBIT_UTIL_EXPORT friend bool operator< ( const CubitString s1,
const CubitString s2 
) [friend]

Definition at line 373 of file CubitString.cpp.

{
  return s1.rep < s2.rep;
}
CUBIT_UTIL_EXPORT friend bool operator<= ( const CubitString s1,
const CubitString s2 
) [friend]

Definition at line 363 of file CubitString.cpp.

{
  return s1.rep <= s2.rep;
}
CUBIT_UTIL_EXPORT friend bool operator> ( const CubitString s1,
const CubitString s2 
) [friend]

Definition at line 378 of file CubitString.cpp.

{
  return s1.rep > s2.rep;
}
CUBIT_UTIL_EXPORT friend bool operator>= ( const CubitString s1,
const CubitString s2 
) [friend]

Definition at line 368 of file CubitString.cpp.

{
  return s1.rep >= s2.rep;
}

Member Data Documentation

const size_t CubitString::npos = std::string::npos [static]

max size of a string

Definition at line 38 of file CubitString.hpp.

std::string CubitString::rep [private]

Definition at line 203 of file CubitString.hpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines