libcamgm
Classes | Functions
ca_mgm::str Namespace Reference

Classes

struct  SafeBuf
 

Functions

std::string toString (bool b)
 
template<class _T >
std::string asString (const _T &t)
 
template<>
std::string asString (const std::string &t)
 
std::string form (const char *format,...)
 
std::string strerror (int errno_r)
 
std::string gsub (const std::string &sData, const std::string &sFrom, const std::string &sTo)
 Looks for text in a string and replaces it. More...
 
std::string & replaceAll (std::string &str, const std::string &from, const std::string &to)
 Looks for text in string and replaces it in place. More...
 
std::string stripFirstWord (std::string &line, const bool ltrim_first)
 
std::string stripLastWord (std::string &line, const bool rtrim_first)
 
std::string getline (std::istream &str, bool trim=false)
 
std::string getline (std::istream &str, const Trim trim_r)
 
std::string representation of number.

Optional second argument sets the minimal string width (' ' padded). Negative values will cause the number to be left adjusted within the string.

Default width is 0.

numstring(42) -> "42"
numstring(42, 4) -> " 42"
numstring(42,-4) -> "42 "
std::string numstring (char n, int w=0)
 
std::string numstring (unsigned char n, int w=0)
 
std::string numstring (short n, int w=0)
 
std::string numstring (unsigned short n, int w=0)
 
std::string numstring (int n, int w=0)
 
std::string numstring (unsigned n, int w=0)
 
std::string numstring (long n, int w=0)
 
std::string numstring (unsigned long n, int w=0)
 
std::string numstring (long long n, int w=0)
 
std::string numstring (unsigned long long n, int w=0)
 
std::string representation of number as hex value with leading '0x'.

Optional second argument sets the minimal string width (0 padded). Negative values will cause the number to be left adjusted within the string. Default width is 10 (4 for char).

hexstring(42)           -> "0x0000002a"
hexstring(42, 4)        -> "0x2a"
hexstring(42,-4)        -> "0x2a"
std::string hexstring (char n, int w=4)
 
std::string hexstring (unsigned char n, int w=4)
 
std::string hexstring (short n, int w=10)
 
std::string hexstring (unsigned short n, int w=10)
 
std::string hexstring (int n, int w=10)
 
std::string hexstring (unsigned n, int w=10)
 
std::string hexstring (long n, int w=10)
 
std::string hexstring (unsigned long n, int w=10)
 
std::string hexstring (long long n, int w=0)
 
std::string hexstring (unsigned long long n, int w=0)
 
std::string representation of number as octal value with leading '0'.

Optional second argument sets the minimal string width (0 padded). Negative values will cause the number to be left adjusted within the string. Default width is 5 (4 for char).

octstring(42)           -> "00052"
octstring(42, 4)        -> "0052"
octstring(42,-4)        -> "052 "
std::string octstring (char n, int w=4)
 
std::string octstring (unsigned char n, int w=4)
 
std::string octstring (short n, int w=5)
 
std::string octstring (unsigned short n, int w=5)
 
std::string octstring (int n, int w=5)
 
std::string octstring (unsigned n, int w=5)
 
std::string octstring (long n, int w=5)
 
std::string octstring (unsigned long n, int w=5)
 
std::string octstring (long long n, int w=0)
 
std::string octstring (unsigned long long n, int w=0)
 
template<typename _It >
_It strtonum (const C_Str &str)
 
template<>
short strtonum (const C_Str &str)
 
template<>
int strtonum (const C_Str &str)
 
template<>
long strtonum (const C_Str &str)
 
template<>
long long strtonum (const C_Str &str)
 
template<>
unsigned short strtonum (const C_Str &str)
 
template<>
unsigned strtonum (const C_Str &str)
 
template<>
unsigned long strtonum (const C_Str &str)
 
template<>
unsigned long long strtonum (const C_Str &str)
 
template<typename _It >
_It strtonum (const C_Str &str, _It &i)
 
bool strToTrue (const C_Str &str)
 
bool strToFalse (const C_Str &str)
 
bool strToBool (const C_Str &str, bool default_r)
 
bool strToBoolNodefault (const C_Str &str, bool &return_r)
 
Split.
template<class _OutputIterator >
unsigned split (const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t")
 
template<class _OutputIterator >
unsigned splitEscaped (const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t", bool withEmpty=false)
 
template<class _OutputIterator >
unsigned splitFields (const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=":")
 
template<class _OutputIterator >
unsigned splitFieldsEscaped (const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=":")
 
Join.
template<class _Iterator >
std::string join (_Iterator begin, _Iterator end, const C_Str &sep_r=" ")
 
template<class _Container >
std::string join (const _Container &cont_r, const C_Str &sep_r=" ")
 
template<class _Iterator >
std::string joinEscaped (_Iterator begin, _Iterator end, const char sep_r= ' ')
 
std::string escape (const std::string &str_r, const char c= ' ')
 
Hexencode.
Todo:
unsecape()

Encode all characters other than [a-zA-Z0-9] as XX. This includes the % character itself, which becomes %25.

std::string hexencode (const C_Str &str_r)
 
std::string hexdecode (const C_Str &str_r)
 
Case conversion.
std::string toLower (const std::string &s)
 
std::string toLower (const char *s)
 
std::string toUpper (const std::string &s)
 
std::string toUpper (const char *s)
 
Case insensitive comparison.
int compareCI (const C_Str &lhs, const C_Str &rhs)
 
Locate substring.
bool contains (const C_Str &str_r, const C_Str &val_r)
 
bool containsCI (const C_Str &str_r, const C_Str &val_r)
 
std::string prefix/suffix handling.
bool hasPrefix (const C_Str &str_r, const C_Str &prefix_r)
 
bool hasPrefixCI (const C_Str &str_r, const C_Str &prefix_r)
 
std::string stripPrefix (const C_Str &str_r, const C_Str &prefix_r)
 
bool hasSuffix (const C_Str &str_r, const C_Str &suffix_r)
 
bool hasSuffixCI (const C_Str &str_r, const C_Str &suffix_r)
 
std::string stripSuffix (const C_Str &str_r, const C_Str &suffix_r)
 
bool startsWith (const C_Str &str_r, const C_Str &prefix_r)
 
bool startsWithCI (const C_Str &str_r, const C_Str &prefix_r)
 
bool endsWith (const C_Str &str_r, const C_Str &prefix_r)
 
bool endsWithCI (const C_Str &str_r, const C_Str &prefix_r)
 

Trimming whitepace.

Todo:
optimize l/r trim.
enum  Trim { NO_TRIM = 0x00, L_TRIM = 0x01, R_TRIM = 0x02, TRIM = (L_TRIM|R_TRIM) }
 
std::string trim (const std::string &s, const Trim trim_r=TRIM)
 
std::string ltrim (const std::string &s)
 
std::string rtrim (const std::string &s)
 

Detailed Description

std::string related utilities and ZYPP_STR_REGEX.

See Also
ZYPP_STR_REGEX

Enumeration Type Documentation

To define how to trim.

Enumerator
NO_TRIM 
L_TRIM 
R_TRIM 
TRIM 

Function Documentation

template<class _T >
std::string ca_mgm::str::asString ( const _T &  t)
inline

Referenced by join(), and joinEscaped().

template<>
std::string ca_mgm::str::asString ( const std::string &  t)
inline
int ca_mgm::str::compareCI ( const C_Str &  lhs,
const C_Str &  rhs 
)
inline
bool ca_mgm::str::contains ( const C_Str &  str_r,
const C_Str &  val_r 
)
inline

Locate substring case sensitive.

bool ca_mgm::str::containsCI ( const C_Str &  str_r,
const C_Str &  val_r 
)
inline

Locate substring case insensitive.

bool ca_mgm::str::endsWith ( const C_Str &  str_r,
const C_Str &  prefix_r 
)
inline

alias for hasSuffix

References hasSuffix().

bool ca_mgm::str::endsWithCI ( const C_Str &  str_r,
const C_Str &  prefix_r 
)
inline

Return whether str_r has prefix prefix_r.

References hasSuffixCI().

std::string ca_mgm::str::escape ( const std::string &  str_r,
const char  c = ' ' 
)

Escape desired character c using a backslash.

For use when printing c separated values, and where joinEscaped() is too heavy.

Todo:
use C_Str instead of std::string to prevent unnecessary promotion to string if used with "string".
Todo:
shoud not be documented in doxy-group 'Join'
std::string ca_mgm::str::form ( const char *  format,
  ... 
)
std::string ca_mgm::str::getline ( std::istream &  str,
bool  trim = false 
)
std::string ca_mgm::str::getline ( std::istream &  str,
const Trim  trim_r 
)
std::string ca_mgm::str::gsub ( const std::string &  sData,
const std::string &  sFrom,
const std::string &  sTo 
)

Looks for text in a string and replaces it.

Note
It only perform substtution in one pass
bool ca_mgm::str::hasPrefix ( const C_Str &  str_r,
const C_Str &  prefix_r 
)
inline

Return whether str_r has prefix prefix_r.

References ca_mgm::C_Str::size().

Referenced by startsWith(), and stripPrefix().

bool ca_mgm::str::hasPrefixCI ( const C_Str &  str_r,
const C_Str &  prefix_r 
)
inline

Return whether str_r has prefix prefix_r.

References ca_mgm::C_Str::size().

Referenced by startsWithCI().

bool ca_mgm::str::hasSuffix ( const C_Str &  str_r,
const C_Str &  suffix_r 
)
inline

Return whether str_r has suffix suffix_r.

References ca_mgm::C_Str::size().

Referenced by endsWith(), and stripSuffix().

bool ca_mgm::str::hasSuffixCI ( const C_Str &  str_r,
const C_Str &  suffix_r 
)
inline

Return whether str_r has prefix prefix_r.

References ca_mgm::C_Str::size().

Referenced by endsWithCI().

std::string ca_mgm::str::hexdecode ( const C_Str &  str_r)

Decode hexencoded XX sequences.

std::string ca_mgm::str::hexencode ( const C_Str &  str_r)

Encode all characters other than [a-zA-Z0-9] as XX. This includes the % character itself, which becomes %25.

std::string ca_mgm::str::hexstring ( char  n,
int  w = 4 
)
inline

References form().

std::string ca_mgm::str::hexstring ( unsigned char  n,
int  w = 4 
)
inline

References form().

std::string ca_mgm::str::hexstring ( short  n,
int  w = 10 
)
inline

References form().

std::string ca_mgm::str::hexstring ( unsigned short  n,
int  w = 10 
)
inline

References form().

std::string ca_mgm::str::hexstring ( int  n,
int  w = 10 
)
inline

References form().

std::string ca_mgm::str::hexstring ( unsigned  n,
int  w = 10 
)
inline

References form().

std::string ca_mgm::str::hexstring ( long  n,
int  w = 10 
)
inline

References form().

std::string ca_mgm::str::hexstring ( unsigned long  n,
int  w = 10 
)
inline

References form().

std::string ca_mgm::str::hexstring ( long long  n,
int  w = 0 
)
inline

References form().

std::string ca_mgm::str::hexstring ( unsigned long long  n,
int  w = 0 
)
inline

References form().

template<class _Iterator >
std::string ca_mgm::str::join ( _Iterator  begin,
_Iterator  end,
const C_Str &  sep_r = " " 
)

Join strings using separator sep_r (defaults to BLANK).

References asString().

Referenced by join().

template<class _Container >
std::string ca_mgm::str::join ( const _Container &  cont_r,
const C_Str &  sep_r = " " 
)

Join strings using separator sep_r (defaults to BLANK).

References join().

template<class _Iterator >
std::string ca_mgm::str::joinEscaped ( _Iterator  begin,
_Iterator  end,
const char  sep_r = ' ' 
)

Join strings using separator sep_r, quoting or escaping the values. Separator defaults to BLANK. Use splitEscaped to restore the values.

References asString(), and for_.

std::string ca_mgm::str::ltrim ( const std::string &  s)
inline

To define how to trim.

References L_TRIM, and trim().

std::string ca_mgm::str::numstring ( char  n,
int  w = 0 
)
inline

References form().

std::string ca_mgm::str::numstring ( unsigned char  n,
int  w = 0 
)
inline

References form().

std::string ca_mgm::str::numstring ( short  n,
int  w = 0 
)
inline

References form().

std::string ca_mgm::str::numstring ( unsigned short  n,
int  w = 0 
)
inline

References form().

std::string ca_mgm::str::numstring ( int  n,
int  w = 0 
)
inline

References form().

std::string ca_mgm::str::numstring ( unsigned  n,
int  w = 0 
)
inline

References form().

std::string ca_mgm::str::numstring ( long  n,
int  w = 0 
)
inline

References form().

std::string ca_mgm::str::numstring ( unsigned long  n,
int  w = 0 
)
inline

References form().

std::string ca_mgm::str::numstring ( long long  n,
int  w = 0 
)
inline

References form().

std::string ca_mgm::str::numstring ( unsigned long long  n,
int  w = 0 
)
inline

References form().

std::string ca_mgm::str::octstring ( char  n,
int  w = 4 
)
inline

References form().

std::string ca_mgm::str::octstring ( unsigned char  n,
int  w = 4 
)
inline

References form().

std::string ca_mgm::str::octstring ( short  n,
int  w = 5 
)
inline

References form().

std::string ca_mgm::str::octstring ( unsigned short  n,
int  w = 5 
)
inline

References form().

std::string ca_mgm::str::octstring ( int  n,
int  w = 5 
)
inline

References form().

std::string ca_mgm::str::octstring ( unsigned  n,
int  w = 5 
)
inline

References form().

std::string ca_mgm::str::octstring ( long  n,
int  w = 5 
)
inline

References form().

std::string ca_mgm::str::octstring ( unsigned long  n,
int  w = 5 
)
inline

References form().

std::string ca_mgm::str::octstring ( long long  n,
int  w = 0 
)
inline

References form().

std::string ca_mgm::str::octstring ( unsigned long long  n,
int  w = 0 
)
inline

References form().

std::string& ca_mgm::str::replaceAll ( std::string &  str,
const std::string &  from,
const std::string &  to 
)

Looks for text in string and replaces it in place.

Note
It only perform substtution in one pass
use only if you replace same lenght strings, otherwise use gsub

Referenced by splitEscaped().

std::string ca_mgm::str::rtrim ( const std::string &  s)
inline

To define how to trim.

References R_TRIM, and trim().

template<class _OutputIterator >
unsigned ca_mgm::str::split ( const C_Str &  line_r,
_OutputIterator  result_r,
const C_Str &  sepchars_r = " \t" 
)

Split line_r into words. Any sequence of characters in sepchars_r is treated as delimiter. The words are passed to OutputIterator result_r.

std::vector<std::string> words;
str::split( "some line", std::back_inserter(words) )
template<class _OutputIterator >
unsigned ca_mgm::str::splitEscaped ( const C_Str &  line_r,
_OutputIterator  result_r,
const C_Str &  sepchars_r = " \t",
bool  withEmpty = false 
)

Split line_r into words with respect to escape delimeters. Any sequence of characters in sepchars_r is treated as delimiter if not inside "" or "" or escaped by \, but not The words are passed to OutputIterator result_r.

See Also
splitEscaped
std::vector<std::string> words;
str::splitEscaped( "some line", std::back_inserter(words) )
example splitted strings
normal line -> 2 elements ( "normal", "line" )
escaped\ line -> 1 element( "escaped line" )
"quoted line" -> 1 element same as above
'quoted line' -> 1 element same as above
"escaped quote\'" -> 1 element ( "escaped quote'" )
\param line_r The string to parse.
\param result_r
\param sepchars_r std::string of separator characters.
\param withEmpty Whether to include empty fields between separators in the result.

References replaceAll().

Referenced by splitFieldsEscaped().

template<class _OutputIterator >
unsigned ca_mgm::str::splitFields ( const C_Str &  line_r,
_OutputIterator  result_r,
const C_Str &  sepchars_r = ":" 
)

Split line_r into fields. Any single character in sepchars_r is treated as a field separator. The words are passed to OutputIterator result_r.

"" -> words 0
":" -> words 2 |||
"a" -> words 1 |a|
":a" -> words 2 ||a|
"a:" -> words 2 |a||
":a:" -> words 3 ||a||
std::vector<std::string> words;
str::split( "some line", std::back_inserter(words) )
template<class _OutputIterator >
unsigned ca_mgm::str::splitFieldsEscaped ( const C_Str &  line_r,
_OutputIterator  result_r,
const C_Str &  sepchars_r = ":" 
)

Split line_r into fields handling also escaped separators.

See Also
splitFields()
splitEscaped()

References splitEscaped().

bool ca_mgm::str::startsWith ( const C_Str &  str_r,
const C_Str &  prefix_r 
)
inline

alias for hasPrefix

References hasPrefix().

bool ca_mgm::str::startsWithCI ( const C_Str &  str_r,
const C_Str &  prefix_r 
)
inline

alias for hasPrefixCI

References hasPrefixCI().

std::string ca_mgm::str::strerror ( int  errno_r)

Return string describing the error_r code. Like strerror, but the numerical value is included in the string as well.

Referenced by ca_mgm::ExceptionDetail::Errno< exType >::format(), and ca_mgm::ExceptionDetail::Errno< exType >::simple().

std::string ca_mgm::str::stripFirstWord ( std::string &  line,
const bool  ltrim_first 
)
std::string ca_mgm::str::stripLastWord ( std::string &  line,
const bool  rtrim_first 
)
std::string ca_mgm::str::stripPrefix ( const C_Str &  str_r,
const C_Str &  prefix_r 
)
inline

Strip a prefix_r from str_r and return the resulting string.

References ca_mgm::C_Str::c_str(), hasPrefix(), and ca_mgm::C_Str::size().

std::string ca_mgm::str::stripSuffix ( const C_Str &  str_r,
const C_Str &  suffix_r 
)
inline

Strip a suffix_r from str_r and return the resulting string.

References ca_mgm::C_Str::c_str(), hasSuffix(), and ca_mgm::C_Str::size().

bool ca_mgm::str::strToBool ( const C_Str &  str,
bool  default_r 
)
inline

Parse str into a bool depending on the default value. If the default is true, look for a legal false string. If the default is false, look for a legal true string.

References strToFalse(), and strToTrue().

bool ca_mgm::str::strToBoolNodefault ( const C_Str &  str,
bool &  return_r 
)
inline

Parse str into a bool if it's a legal true or false string. If str is not a recognized true or false string, return_r is left unchanged.

References strToFalse(), and strToTrue().

bool ca_mgm::str::strToFalse ( const C_Str &  str)

Return false if str is 0, false, no, off.

Referenced by strToBool(), and strToBoolNodefault().

template<typename _It >
_It ca_mgm::str::strtonum ( const C_Str &  str)
inline

Parsing numbers from string. std::string to integer type determined by template arg.

Note
Only specializations are defined.
time_t t = strtonum<time_t>( "42" );
template<>
short ca_mgm::str::strtonum ( const C_Str &  str)
inline

Parsing numbers from string. std::string to integer type determined by template arg.

Note
Only specializations are defined.
time_t t = strtonum<time_t>( "42" );
template<>
int ca_mgm::str::strtonum ( const C_Str &  str)
inline

Parsing numbers from string. std::string to integer type determined by template arg.

Note
Only specializations are defined.
time_t t = strtonum<time_t>( "42" );
template<>
long ca_mgm::str::strtonum ( const C_Str &  str)
inline

Parsing numbers from string. std::string to integer type determined by template arg.

Note
Only specializations are defined.
time_t t = strtonum<time_t>( "42" );
template<>
long long ca_mgm::str::strtonum ( const C_Str &  str)
inline

Parsing numbers from string. std::string to integer type determined by template arg.

Note
Only specializations are defined.
time_t t = strtonum<time_t>( "42" );
template<>
unsigned short ca_mgm::str::strtonum ( const C_Str &  str)
inline

Parsing numbers from string. std::string to integer type determined by template arg.

Note
Only specializations are defined.
time_t t = strtonum<time_t>( "42" );
template<>
unsigned ca_mgm::str::strtonum ( const C_Str &  str)
inline

Parsing numbers from string. std::string to integer type determined by template arg.

Note
Only specializations are defined.
time_t t = strtonum<time_t>( "42" );
template<>
unsigned long ca_mgm::str::strtonum ( const C_Str &  str)
inline

Parsing numbers from string. std::string to integer type determined by template arg.

Note
Only specializations are defined.
time_t t = strtonum<time_t>( "42" );
template<>
unsigned long long ca_mgm::str::strtonum ( const C_Str &  str)
inline

Parsing numbers from string. std::string to integer type determined by template arg.

Note
Only specializations are defined.
time_t t = strtonum<time_t>( "42" );
template<typename _It >
_It ca_mgm::str::strtonum ( const C_Str &  str,
_It &  i 
)
inline

std::string to integer type detemined 2nd function arg i.

time_t t; strtonum( "42", t );
bool ca_mgm::str::strToTrue ( const C_Str &  str)

Parsing boolean from string.Return true if str is 1, true, yes, on (or a nonzero number).

Referenced by strToBool(), and strToBoolNodefault().

std::string ca_mgm::str::toLower ( const std::string &  s)

Return lowercase version of s

Todo:
improve

Referenced by toLower().

std::string ca_mgm::str::toLower ( const char *  s)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

References toLower().

std::string ca_mgm::str::toString ( bool  b)
inline

Global asString() that works with std::string too

Referenced by ca_mgm::path::PathName::asString().

std::string ca_mgm::str::toUpper ( const std::string &  s)

Return uppercase version of s

Todo:
improve

Referenced by toUpper().

std::string ca_mgm::str::toUpper ( const char *  s)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

References toUpper().

std::string ca_mgm::str::trim ( const std::string &  s,
const Trim  trim_r = TRIM 
)

To define how to trim.

Referenced by ltrim(), and rtrim().