Cantera  3.1.0b1
Loading...
Searching...
No Matches
String Conversion

Utility functions for conversion of formatted strings. More...

Collaboration diagram for String Conversion:

Detailed Description

Utility functions for conversion of formatted strings.

Functions

string vec2str (const vector< double > &v, const string &fmt="%g", const string &sep=", ")
 Convert a vector to a string (separated by commas)
 
string stripnonprint (const string &s)
 Strip non-printing characters wherever they are.
 
Composition parseCompString (const string &ss, const vector< string > &names=vector< string >())
 Parse a composition string into a map consisting of individual key:composition pairs.
 
double fpValue (const string &val)
 Translate a string into one double value.
 
double fpValueCheck (const string &val)
 Translate a string into one double value, with error checking.
 
void tokenizeString (const string &oval, vector< string > &v)
 This function separates a string up into tokens according to the location of white space.
 
void tokenizePath (const string &oval, vector< string > &v)
 This function separates a string up into tokens according to the location of path separators.
 
size_t copyString (const string &source, char *dest, size_t length)
 Copy the contents of a string into a char array of a given length.
 
string trimCopy (const string &input)
 Trim.
 
string toLowerCopy (const string &input)
 Convert to lower case.
 
bool caseInsensitiveEquals (const string &input, const string &test)
 Case insensitive equality predicate.
 

Function Documentation

◆ vec2str()

string vec2str ( const vector< double > &  v,
const string &  fmt = "%g",
const string &  sep = ", " 
)

Convert a vector to a string (separated by commas)

Parameters
vvector to be converted
fmtFormat to be used (printf style) for each element
sepSeparator

Definition at line 33 of file stringUtils.cpp.

◆ stripnonprint()

string stripnonprint ( const string &  s)

Strip non-printing characters wherever they are.

Parameters
sInput string
Returns
a copy of the string, stripped of all non- printing characters.

Definition at line 47 of file stringUtils.cpp.

◆ parseCompString()

Composition parseCompString ( const string &  ss,
const vector< string > &  names = vector< string >() 
)

Parse a composition string into a map consisting of individual key:composition pairs.

Elements present in names but not in the composition string will have a value of 0. Elements present in the composition string but not in names will generate an exception. The composition is a double. Example:

Input is

"ice:1 snow:2" names = ["fire", "ice", "snow"]

Output is x["fire"] = 0 x["ice"] = 1 x["snow"] = 2

Parameters
ssoriginal string consisting of multiple key:composition pairs on multiple lines
names(optional) valid names for elements in the composition map. If empty or unspecified, all values are allowed.
Returns
map of names to values

Definition at line 58 of file stringUtils.cpp.

◆ fpValue()

double fpValue ( const string &  val)

Translate a string into one double value.

No error checking is done on the conversion.

Parameters
valString value of the double
Returns
a double

Definition at line 121 of file stringUtils.cpp.

◆ fpValueCheck()

double fpValueCheck ( const string &  val)

Translate a string into one double value, with error checking.

fpValueCheck is a wrapper around the C++ stringstream double parser. It does quite a bit more error checking than atof() or strtod(), and is quite a bit more restrictive.

First it interprets both E, e, d, and D as exponents. stringstreams only interpret e or E as an exponent character.

It only accepts a string as well formed if it consists as a single token. Multiple words will raise an exception. It will raise a CanteraError for NAN and inf entries as well, in contrast to atof() or strtod(). The user needs to know that a serious numerical issue has occurred.

It does not accept hexadecimal numbers.

It always use the C locale, regardless of any locale settings.

Parameters
valString representation of the number
Returns
a double

Definition at line 130 of file stringUtils.cpp.

◆ tokenizeString()

void tokenizeString ( const string &  oval,
vector< string > &  v 
)

This function separates a string up into tokens according to the location of white space.

White space includes the new line character. tokens are stripped of leading and trailing white space.

The separate tokens are returned in a string vector, v.

Parameters
ovalString to be broken up
vOutput vector of tokens.

Definition at line 184 of file stringUtils.cpp.

◆ tokenizePath()

void tokenizePath ( const string &  oval,
vector< string > &  v 
)

This function separates a string up into tokens according to the location of path separators.

The separate tokens are returned in a string vector, v.

Parameters
ovalString to be broken up
vOutput vector of tokens.
Since
New in Cantera 3.0.

Definition at line 196 of file stringUtils.cpp.

◆ copyString()

size_t copyString ( const string &  source,
char *  dest,
size_t  length 
)

Copy the contents of a string into a char array of a given length.

If length is less than the size of source, the string will be truncated and the function will return the length of the buffer required to hold source. Otherwise, returns 0.

Definition at line 203 of file stringUtils.cpp.

◆ trimCopy()

string trimCopy ( const string &  input)

Trim.

Remove all leading and trailing spaces (with default locale).

Definition at line 215 of file stringUtils.cpp.

◆ toLowerCopy()

string toLowerCopy ( const string &  input)

Convert to lower case.

Convert the given string to lower case (with default locale).

Definition at line 219 of file stringUtils.cpp.

◆ caseInsensitiveEquals()

bool caseInsensitiveEquals ( const string &  input,
const string &  test 
)

Case insensitive equality predicate.

Returns true if and only if all elements in both strings are the same when compared case insensitively (with default locale).

Definition at line 223 of file stringUtils.cpp.