Tempus Version of the Day
Time Integration
Loading...
Searching...
No Matches
Tempus_String_Utilities.cpp
Go to the documentation of this file.
1// @HEADER
2// ****************************************************************************
3// Tempus: Copyright (2017) Sandia Corporation
4//
5// Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6// ****************************************************************************
7// @HEADER
8
10#include <sstream>
11
12namespace Tempus {
13
14 void trim(std::string& str)
15 {
16 const std::string whitespace(" \t\n");
17
18 const auto strBegin = str.find_first_not_of(whitespace);
19 if (strBegin == std::string::npos) {
20 str = "";
21 return; // no content
22 }
23
24 const auto strEnd = str.find_last_not_of(whitespace);
25 const auto strRange = strEnd - strBegin + 1;
26
27 str = str.substr(strBegin, strRange);
28 return;
29 }
30
31 void StringTokenizer(std::vector<std::string>& tokens,
32 const std::string& str,
33 const std::string delimiters,bool trim)
34 {
35 using std::string;
36
37 // Skip delimiters at beginning.
38 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
39 // Find first "non-delimiter".
40 string::size_type pos = str.find_first_of(delimiters, lastPos);
41
42 while (string::npos != pos || string::npos != lastPos) {
43
44 // grab token, trim if desired
45 std::string token = str.substr(lastPos,pos-lastPos);
46 if(trim) Tempus::trim(token);
47
48 // Found a token, add it to the vector.
49 tokens.push_back(token);
50
51 if(pos==string::npos)
52 break;
53
54 // Skip delimiters. Note the "not_of"
55 lastPos = str.find_first_not_of(delimiters, pos);
56 // Find next "non-delimiter"
57 pos = str.find_first_of(delimiters, lastPos);
58 }
59
60 }
61
62 void TokensToDoubles(std::vector<double> & values,
63 const std::vector<std::string> & tokens)
64 {
65 // turn tokens into doubles (its a miracle!)
66 for(std::size_t i=0;i<tokens.size();i++) {
67 double value = 0.0;
68 std::stringstream ss;
69 ss << tokens[i];
70 ss >> value;
71
72 values.push_back(value);
73 }
74 }
75
76 void TokensToInts(std::vector<int> & values,
77 const std::vector<std::string> & tokens)
78 {
79 // turn tokens into doubles (its a miracle!)
80 for(std::size_t i=0;i<tokens.size();i++) {
81 int value = 0;
82 std::stringstream ss;
83 ss << tokens[i];
84 ss >> value;
85
86 values.push_back(value);
87 }
88 }
89}
void TokensToDoubles(std::vector< double > &values, const std::vector< std::string > &tokens)
Turn a vector of tokens into a vector of doubles.
void trim(std::string &str)
Removes whitespace at beginning and end of string.
void StringTokenizer(std::vector< std::string > &tokens, const std::string &str, const std::string delimiters, bool trim)
Tokenize a string, put tokens in a vector.
void TokensToInts(std::vector< int > &values, const std::vector< std::string > &tokens)
Turn a vector of tokens into a vector of ints.