Cantera  3.1.0a1
stringUtils.h
Go to the documentation of this file.
1 /**
2  * @file stringUtils.h Contains declarations for string manipulation
3  * functions within Cantera.
4  */
5 
6 // This file is part of Cantera. See License.txt in the top-level directory or
7 // at https://cantera.org/license.txt for license and copyright information.
8 
9 #ifndef CT_STRINGUTILS_H
10 #define CT_STRINGUTILS_H
11 
12 #include "ct_defs.h"
13 
14 namespace Cantera
15 {
16 
17 //! @defgroup StringConversion String Conversion
18 //! Utility functions for conversion of formatted strings.
19 //! @ingroup globalUtilFuncs
20 //! @{
21 
22 //! Convert a vector to a string (separated by commas)
23 /*!
24  * @param v vector to be converted
25  * @param fmt Format to be used (printf style) for each element
26  * @param sep Separator
27  */
28 string vec2str(const vector<double>& v, const string& fmt="%g", const string& sep=", ");
29 
30 //! Strip non-printing characters wherever they are
31 /*!
32  * @param s Input string
33  * @returns a copy of the string, stripped of all non- printing characters.
34  */
35 string stripnonprint(const string& s);
36 
37 //! Parse a composition string into a map consisting of individual
38 //! key:composition pairs.
39 /*!
40  * Elements present in *names* but not in the composition string will have
41  * a value of 0. Elements present in the composition string but not in *names*
42  * will generate an exception. The composition is a double. Example:
43  *
44  * Input is
45  *
46  * "ice:1 snow:2"
47  * names = ["fire", "ice", "snow"]
48  *
49  * Output is
50  * x["fire"] = 0
51  * x["ice"] = 1
52  * x["snow"] = 2
53  *
54  * @param ss original string consisting of multiple key:composition
55  * pairs on multiple lines
56  * @param names (optional) valid names for elements in the composition map. If
57  * empty or unspecified, all values are allowed.
58  * @return map of names to values
59  */
60 Composition parseCompString(const string& ss,
61  const vector<string>& names=vector<string>());
62 
63 //! Translate a string into one double value
64 /*!
65  * No error checking is done on the conversion.
66  *
67  * @param val String value of the double
68  * @returns a double
69  */
70 double fpValue(const string& val);
71 
72 //! Translate a string into one double value, with error checking
73 /*!
74  * fpValueCheck is a wrapper around the C++ stringstream double parser. It
75  * does quite a bit more error checking than atof() or strtod(), and is quite
76  * a bit more restrictive.
77  *
78  * First it interprets both E, e, d, and D as exponents. stringstreams only
79  * interpret e or E as an exponent character.
80  *
81  * It only accepts a string as well formed if it consists as a single token.
82  * Multiple words will raise an exception. It will raise a CanteraError for
83  * NAN and inf entries as well, in contrast to atof() or strtod(). The user
84  * needs to know that a serious numerical issue has occurred.
85  *
86  * It does not accept hexadecimal numbers.
87  *
88  * It always use the C locale, regardless of any locale settings.
89  *
90  * @param val String representation of the number
91  * @returns a double
92  */
93 double fpValueCheck(const string& val);
94 
95 //! This function separates a string up into tokens according to the location of
96 //! white space.
97 /*!
98  * White space includes the new line character. tokens are stripped of leading
99  * and trailing white space.
100  *
101  * The separate tokens are returned in a string vector, v.
102  *
103  * @param oval String to be broken up
104  * @param v Output vector of tokens.
105  */
106 void tokenizeString(const string& oval, vector<string>& v);
107 
108 //! This function separates a string up into tokens according to the location of
109 //! path separators.
110 /*!
111  * The separate tokens are returned in a string vector, v.
112  *
113  * @param oval String to be broken up
114  * @param v Output vector of tokens.
115  *
116  * @since New in %Cantera 3.0.
117  */
118 void tokenizePath(const string& oval, vector<string>& v);
119 
120 //! Copy the contents of a string into a char array of a given length
121 /*!
122  * If *length* is less than the size of *source*, the string will be truncated
123  * and the function will return the length of the buffer required to hold
124  * *source*. Otherwise, returns 0.
125  */
126 size_t copyString(const string& source, char* dest, size_t length);
127 
128 //! Trim.
129 /*!
130  * Remove all leading and trailing spaces (with default locale).
131  */
132 string trimCopy(const string &input);
133 
134 //! Convert to lower case.
135 /*!
136  * Convert the given string to lower case (with default locale).
137  */
138 string toLowerCopy(const string& input);
139 
140 //! Case insensitive equality predicate.
141 /*!
142  * Returns true if and only if all elements in both strings are the same
143  * when compared case insensitively (with default locale).
144  */
145 bool caseInsensitiveEquals(const string &input, const string &test);
146 
147 //! @}
148 
149 }
150 
151 #endif
This file contains definitions of constants, types and terms that are used in internal routines and a...
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.
bool caseInsensitiveEquals(const string &input, const string &test)
Case insensitive equality predicate.
string stripnonprint(const string &s)
Strip non-printing characters wherever they are.
Definition: stringUtils.cpp:47
string vec2str(const vector< double > &v, const string &fmt, const string &sep)
Convert a vector to a string (separated by commas)
Definition: stringUtils.cpp:33
double fpValue(const string &val)
Translate a string into one double value.
string trimCopy(const string &input)
Trim.
void tokenizePath(const string &in_val, vector< string > &v)
This function separates a string up into tokens according to the location of path separators.
double fpValueCheck(const string &val)
Translate a string into one double value, with error checking.
string toLowerCopy(const string &input)
Convert to lower case.
Composition parseCompString(const string &ss, const vector< string > &names)
Parse a composition string into a map consisting of individual key:composition pairs.
Definition: stringUtils.cpp:58
void tokenizeString(const string &in_val, vector< string > &v)
This function separates a string up into tokens according to the location of white space.
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:564
map< string, double > Composition
Map from string names to doubles.
Definition: ct_defs.h:177