Cantera 2.6.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 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
14namespace Cantera
15{
16
17//! Convert a vector to a string (separated by commas)
18/*!
19 * @param v vector to be converted
20 * @param fmt Format to be used (printf style) for each element
21 * @param sep Separator
22 */
23std::string vec2str(const vector_fp& v, const std::string& fmt="%g",
24 const std::string& sep=", ");
25
26//! Strip non-printing characters wherever they are
27/*!
28 * @param s Input string
29 * @returns a copy of the string, stripped of all non- printing characters.
30 */
31std::string stripnonprint(const std::string& s);
32
33//! Parse a composition string into a map consisting of individual
34//! key:composition pairs.
35/*!
36 * Elements present in *names* but not in the composition string will have
37 * a value of 0. Elements present in the composition string but not in *names*
38 * will generate an exception. The composition is a double. Example:
39 *
40 * Input is
41 *
42 * "ice:1 snow:2"
43 * names = ["fire", "ice", "snow"]
44 *
45 * Output is
46 * x["fire"] = 0
47 * x["ice"] = 1
48 * x["snow"] = 2
49 *
50 * @param ss original string consisting of multiple key:composition
51 * pairs on multiple lines
52 * @param names (optional) valid names for elements in the composition map. If
53 * empty or unspecified, all values are allowed.
54 * @return map of names to values
55 */
56compositionMap parseCompString(const std::string& ss,
57 const std::vector<std::string>& names=std::vector<std::string>());
58
59//! Translate a string into one integer value
60/*!
61 * No error checking is done on the conversion. The c stdlib function atoi() is
62 * used.
63 *
64 * @param val String value of the integer
65 * @returns an integer
66 * @deprecated To be removed after Cantera 2.6. Use std::stoi instead.
67 */
68int intValue(const std::string& val);
69
70//! Translate a string into one doublereal value
71/*!
72 * No error checking is done on the conversion.
73 *
74 * @param val String value of the double
75 * @returns a double
76 */
77doublereal fpValue(const std::string& val);
78
79//! Translate a string into one doublereal value, with error checking
80/*!
81 * fpValueCheck is a wrapper around the C++ stringstream double parser. It
82 * does quite a bit more error checking than atof() or strtod(), and is quite
83 * a bit more restrictive.
84 *
85 * First it interprets both E, e, d, and D as exponents. stringstreams only
86 * interpret e or E as an exponent character.
87 *
88 * It only accepts a string as well formed if it consists as a single token.
89 * Multiple words will raise an exception. It will raise a CanteraError for
90 * NAN and inf entries as well, in contrast to atof() or strtod(). The user
91 * needs to know that a serious numerical issue has occurred.
92 *
93 * It does not accept hexadecimal numbers.
94 *
95 * It always use the C locale, regardless of any locale settings.
96 *
97 * @param val String representation of the number
98 * @returns a double
99 */
100doublereal fpValueCheck(const std::string& val);
101
102//! Parse a name string, separating out the phase name from the species name
103/*!
104 * Name strings must not contain these internal characters "; \n \t ," Only one
105 * colon is allowed, the one separating the phase name from the species name.
106 * Therefore, names may not include a colon.
107 *
108 * @param[in] nameStr Name string containing the phase name and the species
109 * name separated by a colon. The phase name is optional.
110 * example: "silane:SiH4"
111 * @param[out] phaseName Name of the phase, if specified. If not specified, a
112 * blank string is returned.
113 * @returns species name. If nameStr is blank an empty string is returned.
114 * @deprecated To be removed after Cantera 2.6.
115 */
116std::string parseSpeciesName(const std::string& nameStr, std::string& phaseName);
117
118//! Interpret one or two token string as a single double
119/*!
120 * This is similar to atof(). However, the second token is interpreted as an
121 * MKS units string and a conversion factor to MKS is applied.
122 *
123 * Example: "1.0 atm" results in the number 1.01325e5.
124 *
125 * @param strSI string to be converted. One or two tokens
126 * @returns a converted double
127 * @deprecated To be removed after Cantera 2.6.
128 */
129doublereal strSItoDbl(const std::string& strSI);
130
131//! This function separates a string up into tokens according to the location of
132//! white space.
133/*!
134 * White space includes the new line character. tokens are stripped of leading
135 * and trailing white space.
136 *
137 * The separate tokens are returned in a string vector, v.
138 *
139 * @param oval String to be broken up
140 * @param v Output vector of tokens.
141 */
142void tokenizeString(const std::string& oval,
143 std::vector<std::string>& v);
144
145//! Copy the contents of a std::string into a char array of a given length
146/*!
147 * If *length* is less than the size of *source*, the string will be truncated
148 * and the function will return the length of the buffer required to hold
149 * *source*. Otherwise, returns 0.
150 */
151size_t copyString(const std::string& source, char* dest, size_t length);
152
153//! Trim.
154/*!
155 * Remove all leading and trailing spaces (with default locale).
156 */
157std::string trimCopy(const std::string &input);
158
159//! Convert to lower case.
160/*!
161 * Convert the given string to lower case (with default locale).
162 */
163std::string toLowerCopy(const std::string& input);
164
165//! Case insensitive equality predicate.
166/*!
167 * Returns true if and only if all elements in both strings are the same
168 * when compared case insensitively (with default locale).
169 */
170bool caseInsensitiveEquals(const std::string &input, const std::string &test);
171
172}
173
174#endif
This file contains definitions of constants, types and terms that are used in internal routines and a...
Namespace for the Cantera kernel.
Definition: AnyMap.h:29
int intValue(const std::string &val)
Translate a string into one integer value.
doublereal fpValue(const std::string &val)
Translate a string into one doublereal value.
bool caseInsensitiveEquals(const std::string &input, const std::string &test)
Case insensitive equality predicate.
std::string vec2str(const vector_fp &v, const std::string &fmt="%g", const std::string &sep=", ")
Convert a vector to a string (separated by commas)
Definition: stringUtils.cpp:33
std::string trimCopy(const std::string &input)
Trim.
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 toLowerCopy(const std::string &input)
Convert to lower case.
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:184
std::string parseSpeciesName(const std::string &nameStr, std::string &phaseName)
Parse a name string, separating out the phase name from the species name.
doublereal fpValueCheck(const std::string &val)
Translate a string into one doublereal value, with error checking.
doublereal strSItoDbl(const std::string &strSI)
Interpret one or two token string as a single double.
void tokenizeString(const std::string &oval, std::vector< std::string > &v)
This function separates a string up into tokens according to the location of white space.
std::map< std::string, double > compositionMap
Map connecting a string name with a double.
Definition: ct_defs.h:176
std::string stripnonprint(const std::string &s)
Strip non-printing characters wherever they are.
Definition: stringUtils.cpp:48
compositionMap parseCompString(const std::string &ss, const std::vector< std::string > &names=std::vector< std::string >())
Parse a composition string into a map consisting of individual key:composition pairs.
Definition: stringUtils.cpp:59
Versions 6.2.0 and 6.2.1 of fmtlib do not include this define before they include windows....
Definition: fmt.h:31