diff options
author | Willem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl> | 2017-11-22 16:41:34 +0100 |
---|---|---|
committer | Willem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl> | 2017-11-22 16:41:34 +0100 |
commit | a527cc9e29cae256bd095b032f34c80957e84907 (patch) | |
tree | e68dd547d6a88c188eca4798423adf084ba58124 /src/Utilities.cpp | |
parent | 6a7b605102f1c22224b516906cb4a848cda50a3b (diff) | |
parent | bd2798bed2fddfe00dac006013a9fb1363417f20 (diff) | |
download | astra-a527cc9e29cae256bd095b032f34c80957e84907.tar.gz astra-a527cc9e29cae256bd095b032f34c80957e84907.tar.bz2 astra-a527cc9e29cae256bd095b032f34c80957e84907.tar.xz astra-a527cc9e29cae256bd095b032f34c80957e84907.zip |
Merge branch 'master' into parallel_vec
Diffstat (limited to 'src/Utilities.cpp')
-rw-r--r-- | src/Utilities.cpp | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/Utilities.cpp b/src/Utilities.cpp index eb06d8b..2ae1b66 100644 --- a/src/Utilities.cpp +++ b/src/Utilities.cpp @@ -66,20 +66,44 @@ double stringToDouble(const std::string& s) template<> float stringTo(const std::string& s) { return stringToFloat(s); } template<> double stringTo(const std::string& s) { return stringToDouble(s); } -std::vector<float> stringToFloatVector(const std::string &s) +template<typename T> +std::vector<T> stringToNumericVector(const std::string &s) { - return stringToVector<float>(s); + std::vector<T> out; + out.reserve(100); + std::istringstream iss; + iss.imbue(std::locale::classic()); + size_t length = s.size(); + size_t current = 0; + size_t next; + do { + next = s.find_first_of(",;", current); + std::string t = s.substr(current, next - current); + iss.str(t); + iss.clear(); + T f; + iss >> f; + out.push_back(f); + current = next + 1; + } while (next != std::string::npos && current != length); + + return out; } +std::vector<float> stringToFloatVector(const std::string &s) +{ + return stringToNumericVector<float>(s); +} std::vector<double> stringToDoubleVector(const std::string &s) { - return stringToVector<double>(s); + return stringToNumericVector<double>(s); } template<typename T> std::vector<T> stringToVector(const std::string& s) { std::vector<T> out; + size_t length = s.size(); size_t current = 0; size_t next; do { @@ -87,7 +111,7 @@ std::vector<T> stringToVector(const std::string& s) std::string t = s.substr(current, next - current); out.push_back(stringTo<T>(t)); current = next + 1; - } while (next != std::string::npos); + } while (next != std::string::npos && current != length); return out; } @@ -117,13 +141,14 @@ void splitString(std::vector<std::string> &items, const std::string& s, const char *delim) { items.clear(); + size_t length = s.size(); size_t current = 0; size_t next; do { next = s.find_first_of(delim, current); items.push_back(s.substr(current, next - current)); current = next + 1; - } while (next != std::string::npos); + } while (next != std::string::npos && current != length); } } |