appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::str Namespace Reference

String. More...

Typedefs

typedef std::vector< std::string > StringVec
 Useful types. More...
 

Functions

std::string okstr (bool b)
 Convert boolean to "ok" or "not-ok". More...
 
std::string space (unsigned n)
 Create space string. More...
 
size_t split (const std::string &str, const char delim, StringVec &elems)
 Split string. More...
 
StringVec split (const std::string &str, const char delim)
 Split string. More...
 
std::string & ltrim (std::string &str)
 Trim string in-place of leading whitespace. More...
 
std::string ltrim (const char *s)
 Trim copy of string of leading whitespace. More...
 
std::string & rtrim (std::string &str)
 Trim string in-place of trailing whitespace. More...
 
std::string rtrim (const char *s)
 Trim copy of string of trailing whitespace. More...
 
std::string & trim (std::string &str)
 Trim string in-place of leading and trailing whitespace. More...
 
std::string trim (const char *s)
 Trim copy of string of leading and trailing whitespace. More...
 
std::string & replace (const std::string &what, const std::string &with, std::string &str)
 In-place replace all whats in string with with. More...
 
std::string replace (const std::string &what, const std::string &with, const char *s)
 Copy replace all whats in string with with. More...
 
std::string lowercase (const std::string &str)
 Convert in-place string to lower case. More...
 
std::string lowercase (const char *s)
 Convert copy of string to lower case. More...
 
std::string uppercase (const std::string &str)
 Convert string to upper case. More...
 
std::string uppercase (const char *s)
 Convert copy of string to upper case. More...
 
size_t gcss (const std::string &str1, const std::string &str2, const size_t pos=0)
 Find the length of the Greatest Common SubString. More...
 
int tobool (const std::string &str, bool &val)
 Convert string to boolean. More...
 
int tolong (const std::string &str, long &val)
 Convert string to a long integer. More...
 
int todouble (const std::string &str, double &val)
 Convert string to a double-precision floating-point number. More...
 
std::string prettify (const std::string &str)
 Prettify string. More...
 
std::string c14n (const std::string &str)
 Simple canonicalization of a string. More...
 
std::string c14n (const str::StringVec &tokens)
 Canonicalization of a list of tokens into a string. More...
 
size_t split (const string &str, const char delim, StringVec &elems)
 
StringVec split (const string &str, const char delim)
 
string & replace (const string &what, const string &with, string &str)
 
string lowercase (const string &str)
 
string uppercase (const string &str)
 
size_t gcss (const string &str1, const string &str2, const size_t pos)
 
int tobool (const string &str, bool &val)
 
int tolong (const string &str, long &val)
 
int todouble (const string &str, double &val)
 
string prettify (const string &str)
 
string c14n (const string &str)
 

Variables

const char * FalseHood []
 Falsehood and truthhood strings. Each list termintate with a NULL. More...
 
const char * TruthHood []
 strings that equate to true More...
 

Detailed Description

String.

Typedef Documentation

typedef std::vector<std::string> rnr::str::StringVec

Useful types.

vector of strings type

Definition at line 83 of file StringTheory.h.

Function Documentation

std::string rnr::str::c14n ( const std::string &  str)

Simple canonicalization of a string.

The string is canonical when:

  • leading and trailing whitespace is stripped
  • only single space between words
Note
c14n is an cute abbreviation where 14 represents the number of letters between the 'c' and 'n' in the word "canonicalization".
Parameters
strString to canonicalize.
Returns
Return copy of string holding canonical form.

Referenced by rnr::cmd::CommandLine::addToHistory(), c14n(), rnr::cmd::CommandLine::end(), and uppercase().

string rnr::str::c14n ( const str::StringVec tokens)

Canonicalization of a list of tokens into a string.

Note
The name c14n is an cute abbreviation where 14 represents the number of letters between the 'c' and 'n' in the word "canonicalization".
Parameters
tokensString tokens to canonicalize.
Returns
Return string holding canonical form.

Definition at line 296 of file StringTheory.cxx.

References c14n(), and prettify().

297  {
298  stringstream ss;
299  string sep;
300 
301  for(size_t i = 0; i < tokens.size(); ++i)
302  {
303  ss << sep << prettify(tokens[i]);
304 
305  if( i == 0 )
306  {
307  sep = " ";
308  }
309  }
310 
311  return ss.str();
312  }
std::string prettify(const std::string &str)
Prettify string.
size_t rnr::str::gcss ( const std::string &  str1,
const std::string &  str2,
const size_t  pos = 0 
)

Find the length of the Greatest Common SubString.

Examples:

string 1 string 2 length
"abcd" "x" 0
"abcd" "bc" 2
"abcd" "bcx" 2
"abcd" "bcdef" 3
Parameters
str1String 1.
str2String 2.
posStarting position in str1.
Returns
Returns length of common substring.

Referenced by rnr::cmd::CommandLine::rlTabList(), and uppercase().

std::string rnr::str::lowercase ( const std::string &  str)

Convert in-place string to lower case.

Parameters
[in,out]strString to convert.
Returns
Lower case string.

Referenced by rnr::cmd::CmdArgDef::convert(), lowercase(), rnr::cmd::CmdArgDef::match(), rnr::cmd::CmdArgDef::matchLiteral(), replace(), and rnr::cmd::CommandLine::rlPartialMatch().

std::string rnr::str::lowercase ( const char *  s)
inline

Convert copy of string to lower case.

Parameters
[in]sNull-terminated character string. Cannot be NULL.
Returns
Lower case string.

Definition at line 265 of file StringTheory.h.

References lowercase(), and uppercase().

266  {
267  std::string str(s);
268  return lowercase(str);
269  }
std::string lowercase(const char *s)
Convert copy of string to lower case.
Definition: StringTheory.h:265
std::string& rnr::str::ltrim ( std::string &  str)
inline

Trim string in-place of leading whitespace.

Parameters
[in,out]strString to trim.
Returns
Left trimmed string.

Definition at line 143 of file StringTheory.h.

Referenced by ltrim(), and trim().

144  {
145  str.erase( str.begin(), std::find_if(str.begin(), str.end(),
146  std::not1(std::ptr_fun<int, int>(std::isspace))) );
147  return str;
148  }
std::string rnr::str::ltrim ( const char *  s)
inline

Trim copy of string of leading whitespace.

Parameters
[in]sNull-terminated character string. Cannot be NULL.
Returns
Left trimmed string.

Definition at line 157 of file StringTheory.h.

References ltrim().

158  {
159  std::string str(s);
160  return ltrim(str);
161  }
std::string ltrim(const char *s)
Trim copy of string of leading whitespace.
Definition: StringTheory.h:157
std::string rnr::str::prettify ( const std::string &  str)

Prettify string.

Binary characters are converted to ascii escape hex sequences. Control characters are converted to standard ascii escape sequences. If required, the string is double quoted ('"') to conform to sh(1) strings.

Parameters
strString to make pretty.
Returns
Prettified version of string.

Referenced by c14n(), rnr::cmd::CommandLine::c14n(), rnr::cmd::Token::printAnnotated(), and uppercase().

std::string& rnr::str::replace ( const std::string &  what,
const std::string &  with,
std::string &  str 
)

In-place replace all whats in string with with.

Todo:
Investigate using c++ function approach.
Parameters
whatWhat substrings to replace.
withWith this substring.
[in,out]strString before and after.
Returns
String with replacements.

Referenced by rnr::cmd::addons::execBtEnable(), rnr::cmd::addons::execQuit(), rnr::cmd::addons::printCmdHelp(), replace(), and trim().

std::string rnr::str::replace ( const std::string &  what,
const std::string &  with,
const char *  s 
)
inline

Copy replace all whats in string with with.

Todo:
Investigate using c++ function approach.
Parameters
whatWhat substrings to replace.
withWith this substring.
[in]sNull-terminated character string. Cannot be NULL.
Returns
String with replacements.

Definition at line 241 of file StringTheory.h.

References lowercase(), and replace().

244  {
245  std::string str(s);
246  return replace(what, with, str);
247  }
std::string replace(const std::string &what, const std::string &with, const char *s)
Copy replace all whats in string with with.
Definition: StringTheory.h:241
std::string& rnr::str::rtrim ( std::string &  str)
inline

Trim string in-place of trailing whitespace.

Parameters
[in,out]strString to trim.
Returns
Right trimmed string.

Definition at line 170 of file StringTheory.h.

Referenced by rnr::chronos::Time::operator>(), rtrim(), and trim().

171  {
172  str.erase( std::find_if(str.rbegin(), str.rend(),
173  std::not1(std::ptr_fun<int, int>(std::isspace))).base(), str.end() );
174  return str;
175  }
std::string rnr::str::rtrim ( const char *  s)
inline

Trim copy of string of trailing whitespace.

Parameters
[in]sNull-terminated character string. Cannot be NULL.
Returns
Right trimmed string.

Definition at line 184 of file StringTheory.h.

References rtrim().

185  {
186  std::string str(s);
187  return rtrim(str);
188  }
std::string rtrim(const char *s)
Trim copy of string of trailing whitespace.
Definition: StringTheory.h:184
std::string rnr::str::space ( unsigned  n)
inline

Create space string.

Parameters
nNumber of spaces.
Returns
String.

Definition at line 110 of file StringTheory.h.

References split().

111  {
112  return std::string(n, ' ');
113  }
size_t rnr::str::split ( const std::string &  str,
const char  delim,
StringVec elems 
)

Split string.

Parameters
[in]strString to split.
[in]delimCharacter delimiter.
[in,out]elemsVector of split strings.
Returns
Returns number of element strings appended to elems.

Referenced by rnr::cmd::CommandLine::addCommand(), rnr::cmd::CommandLine::parseVarRangeExpr(), rnr::cmd::addons::printCmdHelp(), and space().

StringVec rnr::str::split ( const std::string &  str,
const char  delim 
)

Split string.

Parameters
[in]strString to split.
[in]delimCharacter delimiter.
Returns
Vector of split strings.
int rnr::str::tobool ( const std::string &  str,
bool &  val 
)

Convert string to boolean.

false: "0" "false" "f" "off" "low" "disable" "open" true: "1" "true" "t" "on" "high" "enable" "close"

Parameters
[in]strString in hex, decimal, or octal format.
[out]valConverted boolean value.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Referenced by rnr::cmd::CmdArgDef::convert(), rnr::cmd::CmdArgDef::match(), and uppercase().

int rnr::str::todouble ( const std::string &  str,
double &  val 
)

Convert string to a double-precision floating-point number.

Parameters
[in]strString in hex, decimal, or octal format.
[out]valConverted double value.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Referenced by rnr::cmd::CmdArgDef::convert(), rnr::cmd::CmdArgDef::match(), rnr::cmd::CommandLine::parseVarRangeExpr(), and uppercase().

int rnr::str::tolong ( const std::string &  str,
long &  val 
)

Convert string to a long integer.

Parameters
[in]strString in hex, decimal, or octal format.
[out]valConverted long value.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Referenced by rnr::cmd::CmdArgDef::convert(), rnr::cmd::CmdArgDef::match(), and uppercase().

std::string& rnr::str::trim ( std::string &  str)
inline

Trim string in-place of leading and trailing whitespace.

Parameters
[in,out]strString to trim.
Returns
Trimmed string.

Definition at line 197 of file StringTheory.h.

References ltrim(), and rtrim().

Referenced by rnr::cmd::ReadLine::freadLine(), rnr::cmd::ReadLine::generatorWrapper(), and trim().

198  {
199  return ltrim(rtrim(str));
200  }
std::string rtrim(const char *s)
Trim copy of string of trailing whitespace.
Definition: StringTheory.h:184
std::string ltrim(const char *s)
Trim copy of string of leading whitespace.
Definition: StringTheory.h:157
std::string rnr::str::trim ( const char *  s)
inline

Trim copy of string of leading and trailing whitespace.

Parameters
[in]sNull-terminated character string. Cannot be NULL.
Returns
Trimmed string.

Definition at line 209 of file StringTheory.h.

References replace(), and trim().

210  {
211  std::string str(s);
212  return trim(str);
213  }
std::string trim(const char *s)
Trim copy of string of leading and trailing whitespace.
Definition: StringTheory.h:209
std::string rnr::str::uppercase ( const std::string &  str)

Convert string to upper case.

Parameters
[in,out]strString to convert.
Returns
Upper case string.

Referenced by lowercase(), and uppercase().

std::string rnr::str::uppercase ( const char *  s)
inline

Convert copy of string to upper case.

Parameters
[in]sNull-terminated character string. Cannot be NULL.
Returns
Lower case string.

Definition at line 287 of file StringTheory.h.

References c14n(), gcss(), prettify(), tobool(), todouble(), tolong(), and uppercase().

288  {
289  std::string str(s);
290  return uppercase(str);
291  }
std::string uppercase(const char *s)
Convert copy of string to upper case.
Definition: StringTheory.h:287

Variable Documentation

const char * rnr::str::FalseHood
Initial value:
=
{
"0", "false", "f", "off", "low", "disable", "no", NULL
}

Falsehood and truthhood strings. Each list termintate with a NULL.

strings that equate to false

Definition at line 77 of file StringTheory.cxx.

Referenced by rnr::cmd::CommandLine::rlTabList().

const char * rnr::str::TruthHood
Initial value:
=
{
"1", "true", "t", "on", "high", "enable", "yes", NULL
}

strings that equate to true

Definition at line 82 of file StringTheory.cxx.

Referenced by rnr::cmd::CommandLine::rlTabList().