diff --git a/include/cantera/base/stringUtils.h b/include/cantera/base/stringUtils.h index 734283be8..cb0e386a7 100644 --- a/include/cantera/base/stringUtils.h +++ b/include/cantera/base/stringUtils.h @@ -11,12 +11,15 @@ #include "ct_defs.h" #include "cantera/base/fmt.h" +#include #include namespace Cantera { +namespace ba = boost::algorithm; + //! Convert a double into a c++ string /*! * @param x double to be converted @@ -55,6 +58,7 @@ std::string vec2str(const vector_fp& v, const std::string& fmt="%g", * * @param s Input string * @returns a copy of the string, stripped of leading and trailing white space + * @deprecated Use `boost::algorithm::trim_copy` instead */ std::string stripws(const std::string& s); diff --git a/src/base/ct2ctml.cpp b/src/base/ct2ctml.cpp index c1215f08a..e32bc423d 100644 --- a/src/base/ct2ctml.cpp +++ b/src/base/ct2ctml.cpp @@ -44,7 +44,7 @@ static string pypath() const char* py = getenv("PYTHON_CMD"); if (py) { - string sp = stripws(string(py)); + string sp = ba::trim_copy(string(py)); if (sp.size() > 0) { s = sp; } @@ -134,7 +134,7 @@ static std::string call_ctml_writer(const std::string& text, bool isfile) } python.close(); python_exit_code = python.exit_code(); - error_output = stripws(error_stream.str()); + error_output = ba::trim_copy(error_stream.str()); python_output = output_stream.str(); } catch (std::exception& err) { // Report failure to execute Python @@ -233,7 +233,7 @@ void ck2cti(const std::string& in_file, const std::string& thermo_file, } python.close(); python_exit_code = python.exit_code(); - python_output = stripws(output_stream.str()); + python_output = ba::trim_copy(output_stream.str()); } catch (std::exception& err) { // Report failure to execute Python stringstream message; diff --git a/src/base/stringUtils.cpp b/src/base/stringUtils.cpp index 4b98209aa..0b9abaa67 100644 --- a/src/base/stringUtils.cpp +++ b/src/base/stringUtils.cpp @@ -91,47 +91,11 @@ std::string lowercase(const std::string& s) return lc; } -//! Return the position of the first printable character in the string -/*! - * @param s input string - * @returns an int representing the first printable string. If - * none returns the size of the string. - */ -static int firstChar(const std::string& s) -{ - int i; - int n = static_cast(s.size()); - for (i = 0; i < n; i++) { - if (s[i] != ' ' && isprint(s[i])) { - break; - } - } - return i; -} - -//! Return the position of the last printable character in the string -/*! - * @param s input string - * @returns an int representing the first printable string. If - * none returns -1. - */ -static int lastChar(const std::string& s) -{ - int i; - int n = static_cast(s.size()); - for (i = n-1; i >= 0; i--) { - if (s[i] != ' ' && isprint(s[i])) { - break; - } - } - return i; -} - std::string stripws(const std::string& s) { - int ifirst = firstChar(s); - int ilast = lastChar(s); - return s.substr(ifirst, ilast - ifirst + 1); + warn_deprecated("stripws", "Use boost::algorithm::trim_copy instead. " + "To be removed after Cantera 2.3."); + return ba::trim_copy(s); } std::string stripnonprint(const std::string& s) @@ -162,7 +126,7 @@ compositionMap parseCompString(const std::string& ss, } size_t valstart = ss.find_first_not_of(" \t\n", colon+1); stop = ss.find_first_of(", ;\n\t", valstart); - std::string name = stripws(ss.substr(start, colon-start)); + std::string name = ba::trim_copy(ss.substr(start, colon-start)); if (!names.empty() && x.find(name) == x.end()) { throw CanteraError("parseCompString", "unknown species '" + name + "'"); @@ -174,7 +138,7 @@ compositionMap parseCompString(const std::string& ss, x[name] = fpValueCheck(ss.substr(valstart, stop-colon-1)); start = ss.find_first_not_of(", ;\n\t", stop+1); } - if (stop != npos && !stripws(ss.substr(stop)).empty()) { + if (stop != npos && !ba::trim_copy(ss.substr(stop)).empty()) { throw CanteraError("parseCompString", "Found non-key:value data " "in composition string: '" + ss.substr(stop) + "'"); } @@ -183,7 +147,7 @@ compositionMap parseCompString(const std::string& ss, int intValue(const std::string& val) { - return std::atoi(stripws(val).c_str()); + return std::atoi(ba::trim_copy(val).c_str()); } doublereal fpValue(const std::string& val) @@ -197,7 +161,7 @@ doublereal fpValue(const std::string& val) doublereal fpValueCheck(const std::string& val) { - std::string str = stripws(val); + std::string str = ba::trim_copy(val); if (str.empty()) { throw CanteraError("fpValueCheck", "string has zero length"); } @@ -262,7 +226,7 @@ std::string wrapString(const std::string& s, const int len) std::string parseSpeciesName(const std::string& nameStr, std::string& phaseName) { - std::string s = stripws(nameStr); + std::string s = ba::trim_copy(nameStr); phaseName = ""; size_t ibegin = s.find_first_not_of(" ;\n\t"); if (ibegin != std::string::npos) { diff --git a/src/base/xml.cpp b/src/base/xml.cpp index 727a87fe8..fed7a9510 100644 --- a/src/base/xml.cpp +++ b/src/base/xml.cpp @@ -192,11 +192,11 @@ int XML_Reader::findQuotedString(const std::string& s, std::string& rstring) con void XML_Reader::parseTag(const std::string& tag, std::string& name, std::map& attribs) const { - string s = stripws(tag); + string s = ba::trim_copy(tag); size_t iloc = s.find(' '); if (iloc != string::npos) { name = s.substr(0, iloc); - s = stripws(s.substr(iloc+1,s.size())); + s = ba::trim_copy(s.substr(iloc+1,s.size())); if (s[s.size()-1] == '/') { name += "/"; } @@ -207,17 +207,17 @@ void XML_Reader::parseTag(const std::string& tag, std::string& name, if (iloc == string::npos) { break; } - string attr = stripws(s.substr(0,iloc)); + string attr = ba::trim_copy(s.substr(0,iloc)); if (attr == "") { break; } - s = stripws(s.substr(iloc+1,s.size())); + s = ba::trim_copy(s.substr(iloc+1,s.size())); string val; iloc = findQuotedString(s, val); attribs[attr] = val; if (iloc != string::npos) { if (iloc < s.size()) { - s = stripws(s.substr(iloc,s.size())); + s = ba::trim_copy(s.substr(iloc,s.size())); } else { break; } @@ -301,7 +301,7 @@ std::string XML_Reader::readValue() tag += ch; } } - return stripws(tag); + return ba::trim_copy(tag); } ////////////////////////// XML_Node ///////////////////////////////// @@ -443,7 +443,7 @@ void XML_Node::addValue(const std::string& val) void XML_Node::addValue(const doublereal val, const std::string& fmt) { - m_value = stripws(fmt::sprintf(fmt, val)); + m_value = ba::trim_copy(fmt::sprintf(fmt, val)); } std::string XML_Node::value() const diff --git a/src/thermo/Elements.cpp b/src/thermo/Elements.cpp index f9eec3bfa..5c4306521 100644 --- a/src/thermo/Elements.cpp +++ b/src/thermo/Elements.cpp @@ -173,8 +173,8 @@ double getElementWeight(const std::string& ename) { int numElements = numElementsDefined(); int numIsotopes = numIsotopesDefined(); - string symbol = stripws(ename); - string name = lowercase(stripws(ename)); + string symbol = ba::trim_copy(ename); + string name = ba::to_lower_copy(symbol); for (int i = 0; i < numElements; i++) { if (symbol == atomicWeightTable[i].symbol) { return atomicWeightTable[i].atomicWeight; @@ -205,7 +205,7 @@ string getElementSymbol(const std::string& ename) { int numElements = numElementsDefined(); int numIsotopes = numIsotopesDefined(); - string name = lowercase(stripws(ename)); + string name = ba::to_lower_copy(ba::trim_copy(ename)); for (int i = 0; i < numElements; i++) { if (name == atomicWeightTable[i].fullName) { return atomicWeightTable[i].symbol; @@ -233,7 +233,7 @@ string getElementName(const std::string& ename) { int numElements = numElementsDefined(); int numIsotopes = numIsotopesDefined(); - string symbol = stripws(ename); + string symbol = ba::trim_copy(ename); for (int i = 0; i < numElements; i++) { if (symbol == atomicWeightTable[i].symbol) { return atomicWeightTable[i].fullName; @@ -261,8 +261,8 @@ int getAtomicNumber(const std::string& ename) { int numElements = numElementsDefined(); int numIsotopes = numIsotopesDefined(); - string symbol = stripws(ename); - string name = lowercase(stripws(ename)); + string symbol = ba::trim_copy(ename); + string name = ba::to_lower_copy(symbol); for (int i = 0; i < numElements; i++) { if (symbol == atomicWeightTable[i].symbol) { return i+1;