Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
12 namespace 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)
47  Tempus::trim(token);
48 
49  // Found a token, add it to the vector.
50  tokens.push_back(token);
51 
52  if(pos==string::npos)
53  break;
54 
55  // Skip delimiters. Note the "not_of"
56  lastPos = str.find_first_not_of(delimiters, pos);
57  // Find next "non-delimiter"
58  pos = str.find_first_of(delimiters, lastPos);
59  }
60 
61  }
62 
63  void TokensToDoubles(std::vector<double> & values,
64  const std::vector<std::string> & tokens)
65  {
66  // turn tokens into doubles (its a miracle!)
67  for(std::size_t i=0;i<tokens.size();i++) {
68  double value = 0.0;
69  std::stringstream ss;
70  ss << tokens[i];
71  ss >> value;
72 
73  values.push_back(value);
74  }
75  }
76 
77  void TokensToInts(std::vector<int> & values,
78  const std::vector<std::string> & tokens)
79  {
80  // turn tokens into doubles (its a miracle!)
81  for(std::size_t i=0;i<tokens.size();i++) {
82  int value = 0;
83  std::stringstream ss;
84  ss << tokens[i];
85  ss >> value;
86 
87  values.push_back(value);
88  }
89  }
90 }
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.