Cantera  2.3.0
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 http://www.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 #include "cantera/base/fmt.h"
14 #include <boost/algorithm/string.hpp>
15 
16 #include <string>
17 
18 namespace Cantera
19 {
20 
21 namespace ba = boost::algorithm;
22 
23 //! Convert a double into a c++ string
24 /*!
25  * @param x double to be converted
26  * @param fmt Format to be used (printf style)
27  * @deprecated Unused. To be removed after Cantera 2.3. Use fmt::format instead
28  */
29 std::string fp2str(const double x, const std::string& fmt="%g");
30 
31 //! Convert an int to a string using a format converter
32 /*!
33  * @param n int to be converted
34  * @param fmt format converter for an int int the printf command
35  * @deprecated Unused. To be removed after Cantera 2.3. Use fmt::format instead
36  */
37 std::string int2str(const int n, const std::string& fmt="%d");
38 
39 //! Convert an unsigned integer to a string
40 /*!
41  * @param n int to be converted
42  * @deprecated Unused. To be removed after Cantera 2.3. Use fmt::format instead
43  */
44 std::string int2str(const size_t n);
45 
46 //! Convert a vector to a string (separated by commas)
47 /*!
48  * @param v vector to be converted
49  * @param fmt Format to be used (printf style) for each element
50  * @param sep Separator
51  */
52 std::string vec2str(const vector_fp& v, const std::string& fmt="%g",
53  const std::string& sep=", ");
54 
55 //! Strip the leading and trailing white space from a string
56 /*!
57  * The command isprint() is used to determine printable characters.
58  *
59  * @param s Input string
60  * @returns a copy of the string, stripped of leading and trailing white space
61  * @deprecated Use `boost::algorithm::trim_copy` instead
62  */
63 std::string stripws(const std::string& s);
64 
65 //! Strip non-printing characters wherever they are
66 /*!
67  * @param s Input string
68  * @returns a copy of the string, stripped of all non- printing characters.
69  */
70 std::string stripnonprint(const std::string& s);
71 
72 //! Cast a copy of a string to lower case
73 /*!
74  * @param s Input string
75  * @returns a copy of the string, with all characters lowercase.
76  * @deprecated Use boost::algorithm::to_lower_copy instead. To be removed after
77  * Cantera 2.3.
78  */
79 std::string lowercase(const std::string& s);
80 
81 //! Parse a composition string into a map consisting of individual
82 //! key:composition pairs.
83 /*!
84  * Elements present in *names* but not in the composition string will have
85  * a value of 0. Elements present in the composition string but not in *names*
86  * will generate an exception. The composition is a double. Example:
87  *
88  * Input is
89  *
90  * "ice:1 snow:2"
91  * names = ["fire", "ice", "snow"]
92  *
93  * Output is
94  * x["fire"] = 0
95  * x["ice"] = 1
96  * x["snow"] = 2
97  *
98  * @param ss original string consisting of multiple key:composition
99  * pairs on multiple lines
100  * @param names (optional) valid names for elements in the composition map. If
101  * empty or unspecified, all values are allowed.
102  * @return map of names to values
103  */
104 compositionMap parseCompString(const std::string& ss,
105  const std::vector<std::string>& names=std::vector<std::string>());
106 
107 //! Translate a string into one integer value
108 /*!
109  * No error checking is done on the conversion. The c stdlib function atoi() is
110  * used.
111  *
112  * @param val String value of the integer
113  * @returns an integer
114  */
115 int intValue(const std::string& val);
116 
117 //! Translate a string into one doublereal value
118 /*!
119  * No error checking is done on the conversion.
120  *
121  * @param val String value of the double
122  * @returns a double
123  */
124 doublereal fpValue(const std::string& val);
125 
126 //! Translate a string into one doublereal value, with error checking
127 /*!
128  * fpValueCheck is a wrapper around the C++ stringstream double parser. It
129  * does quite a bit more error checking than atof() or strtod(), and is quite
130  * a bit more restrictive.
131  *
132  * First it interprets both E, e, d, and D as exponents. stringstreams only
133  * interpret e or E as an exponent character.
134  *
135  * It only accepts a string as well formed if it consists as a single token.
136  * Multiple words will raise an exception. It will raise a CanteraError for
137  * NAN and inf entries as well, in contrast to atof() or strtod(). The user
138  * needs to know that a serious numerical issue has occurred.
139  *
140  * It does not accept hexadecimal numbers.
141  *
142  * It always use the C locale, regardless of any locale settings.
143  *
144  * @param val String representation of the number
145  * @returns a double
146  */
147 doublereal fpValueCheck(const std::string& val);
148 
149 //! Parse a name string, separating out the phase name from the species name
150 /*!
151  * Name strings must not contain these internal characters "; \n \t ," Only one
152  * colon is allowed, the one separating the phase name from the species name.
153  * Therefore, names may not include a colon.
154  *
155  * @param[in] nameStr Name string containing the phase name and the species
156  * name separated by a colon. The phase name is optional.
157  * example: "silane:SiH4"
158  * @param[out] phaseName Name of the phase, if specified. If not specified, a
159  * blank string is returned.
160  * @returns species name. If nameStr is blank an empty string is returned.
161  */
162 std::string parseSpeciesName(const std::string& nameStr, std::string& phaseName);
163 
164 //! Line wrap a string via a copy operation
165 /*!
166  * @param s Input string to be line wrapped
167  * @param len Length at which to wrap. The default is 70.
168  * @deprecated Unused. To be removed after Cantera 2.3.
169  */
170 std::string wrapString(const std::string& s,
171  const int len=70);
172 
173 //! Interpret one or two token string as a single double
174 /*!
175  * This is similar to atof(). However, the second token is interpreted as an
176  * MKS units string and a conversion factor to MKS is applied.
177  *
178  * Example: "1.0 atm" results in the number 1.01325e5.
179  *
180  * @param strSI string to be converted. One or two tokens
181  * @returns a converted double
182  */
183 doublereal strSItoDbl(const std::string& strSI);
184 
185 //! This function separates a string up into tokens according to the location of
186 //! white space.
187 /*!
188  * White space includes the new line character. tokens are stripped of leading
189  * and trailing white space.
190  *
191  * The separate tokens are returned in a string vector, v.
192  *
193  * @param oval String to be broken up
194  * @param v Output vector of tokens.
195  */
196 void tokenizeString(const std::string& oval,
197  std::vector<std::string>& v);
198 
199 //! Copy the contents of a std::string into a char array of a given length
200 /*!
201  * If *length* is less than the size of *source*, the string will be truncated
202  * and the function will return the length of the buffer required to hold
203  * *source*. Otherwise, returns 0.
204  */
205 size_t copyString(const std::string& source, char* dest, size_t length);
206 
207 }
208 
209 #endif
Wrapper for either system-installed or local headers for fmt.
std::map< std::string, doublereal > compositionMap
Map connecting a string name with a double.
Definition: ct_defs.h:149
doublereal fpValue(const std::string &val)
Translate a string into one doublereal value.
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
Definition: stringUtils.cpp:47
std::string vec2str(const vector_fp &v, const std::string &fmt, const std::string &sep)
Convert a vector to a string (separated by commas)
Definition: stringUtils.cpp:69
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
std::string lowercase(const std::string &s)
Cast a copy of a string to lower case.
Definition: stringUtils.cpp:85
std::string stripws(const std::string &s)
Strip the leading and trailing white space from a string.
Definition: stringUtils.cpp:96
std::string fp2str(const double x, const std::string &fmt)
Convert a double into a c++ string.
Definition: stringUtils.cpp:34
void tokenizeString(const std::string &in_val, std::vector< std::string > &v)
This function separates a string up into tokens according to the location of white space...
std::string parseSpeciesName(const std::string &nameStr, std::string &phaseName)
Parse a name string, separating out the phase name from the species name.
size_t copyString(const std::string &source, char *dest, size_t length)
Copy the contents of a std::string into a char array of a given length.
std::string wrapString(const std::string &s, const int len)
Line wrap a string via a copy operation.
int intValue(const std::string &val)
Translate a string into one integer value.
compositionMap parseCompString(const std::string &ss, const std::vector< std::string > &names)
Parse a composition string into a map consisting of individual key:composition pairs.
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:157
Namespace for the Cantera kernel.
Definition: application.cpp:29
doublereal strSItoDbl(const std::string &strSI)
Interpret one or two token string as a single double.
std::string stripnonprint(const std::string &s)
Strip non-printing characters wherever they are.
doublereal fpValueCheck(const std::string &val)
Translate a string into one doublereal value, with error checking.