cgma
|
String class that represents a UTF-8 string. More...
#include <CubitString.hpp>
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 CubitString & | operator= (const CubitString &s) |
CubitString & | operator+= (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 &) |
String class that represents a UTF-8 string.
Definition at line 34 of file CubitString.hpp.
Definition at line 40 of file CubitString.hpp.
{ CaseInsensitive, CaseSensitive };
CubitString::CubitString | ( | const CubitString & | s | ) |
CubitString::CubitString | ( | const char * | 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) { }
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(); }
void CubitString::clear | ( | ) |
bool CubitString::compare | ( | const CubitString & | s, |
CaseSensitivity | cs = CaseSensitive |
||
) |
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 |
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.
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.
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.
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(); }
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.
CubitString & CubitString::operator= | ( | const CubitString & | s | ) |
Definition at line 118 of file CubitString.cpp.
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 | ||
) |
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.
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.
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); }
void CubitString::simplify | ( | ) |
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.
bool CubitString::starts_with | ( | const CubitString & | str | ) | const |
const std::string & CubitString::str | ( | ) | const |
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(); }
void CubitString::to_lower | ( | ) |
convert to lower case (Note: this is not internationalized)
Definition at line 284 of file CubitString.cpp.
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++; } }
void CubitString::to_upper | ( | ) |
convert to upper case (Note: this is not internationalized)
Definition at line 292 of file CubitString.cpp.
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; }
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.
void CubitString::trim | ( | ) |
trim off whitespace from the beginning and end of the string
Definition at line 322 of file CubitString.cpp.
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.
CUBIT_UTIL_EXPORT friend bool operator<= | ( | const CubitString & | s1, |
const CubitString & | s2 | ||
) | [friend] |
Definition at line 363 of file CubitString.cpp.
CUBIT_UTIL_EXPORT friend bool operator> | ( | const CubitString & | s1, |
const CubitString & | s2 | ||
) | [friend] |
Definition at line 378 of file CubitString.cpp.
CUBIT_UTIL_EXPORT friend bool operator>= | ( | const CubitString & | s1, |
const CubitString & | s2 | ||
) | [friend] |
Definition at line 368 of file CubitString.cpp.
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.