From 367bdba551a6054bdfd2ccdde537fd12138408b8 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 26 Oct 2016 23:24:38 -0400 Subject: [PATCH] Use boost::algorithm::split to implement tokenizeString --- src/base/stringUtils.cpp | 73 ++++++---------------------------------- 1 file changed, 10 insertions(+), 63 deletions(-) diff --git a/src/base/stringUtils.cpp b/src/base/stringUtils.cpp index 3fa9198e8..4b98209aa 100644 --- a/src/base/stringUtils.cpp +++ b/src/base/stringUtils.cpp @@ -22,9 +22,12 @@ #include "cantera/base/ctml.h" #include "cantera/base/utilities.h" +#include #include #include +namespace ba = boost::algorithm; + namespace Cantera { @@ -298,72 +301,16 @@ doublereal strSItoDbl(const std::string& strSI) return val * fp; } -//! Find the first white space in a string -/*! - * Returns the location of the first white space character in a string - * - * @param val Input string to be parsed - * @return In a size_type variable, return the location of the first white - * space character. Return npos if none is found - */ -static std::string::size_type findFirstWS(const std::string& val) +void tokenizeString(const std::string& in_val, std::vector& v) { - std::string::size_type ibegin = std::string::npos; - int j = 0; - for (const auto& ch : val) { - if (isspace(static_cast(ch))) { - ibegin = (std::string::size_type) j; - break; - } - j++; - } - return ibegin; -} - -//! Find the first non-white space in a string -/*! - * Returns the location of the first non-white space character in a string - * - * @param val Input string to be parsed - * @return In a size_type variable, return the location of the first - * nonwhite space character. Return npos if none is found - */ -static std::string::size_type findFirstNotOfWS(const std::string& val) -{ - std::string::size_type ibegin = std::string::npos; - int j = 0; - for (const auto& ch : val) { - if (!isspace(static_cast(ch))) { - ibegin = (std::string::size_type) j; - break; - } - j++; - } - return ibegin; -} - -void tokenizeString(const std::string& oval, - std::vector& v) -{ - std::string val(oval); - std::string::size_type ibegin, iend; + std::string val = ba::trim_copy(in_val); v.clear(); - while (true) { - ibegin = findFirstNotOfWS(val); - if (ibegin != std::string::npos) { - val = val.substr(ibegin,val.size()); - iend = findFirstWS(val); - if (iend == std::string::npos) { - v.push_back(val); - break; - } else { - v.push_back(val.substr(0,iend)); - val = val.substr(iend+1,val.size()); - } - } else { - break; - } + if (val.empty()) { + // In this case, prefer v to be empty instead of split's behavior of + // returning a vector with one element that is the empty string. + return; } + ba::split(v, val, ba::is_space(), ba::token_compress_on); } size_t copyString(const std::string& source, char* dest, size_t length)