Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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, const std::string& str,
32  const std::string delimiters, bool trim)
33 {
34  using std::string;
35 
36  // Skip delimiters at beginning.
37  string::size_type lastPos = str.find_first_not_of(delimiters, 0);
38  // Find first "non-delimiter".
39  string::size_type pos = str.find_first_of(delimiters, lastPos);
40 
41  while (string::npos != pos || string::npos != lastPos) {
42  // grab token, trim if desired
43  std::string token = str.substr(lastPos, pos - lastPos);
44  if (trim) Tempus::trim(token);
45 
46  // Found a token, add it to the vector.
47  tokens.push_back(token);
48 
49  if (pos == string::npos) break;
50 
51  // Skip delimiters. Note the "not_of"
52  lastPos = str.find_first_not_of(delimiters, pos);
53  // Find next "non-delimiter"
54  pos = str.find_first_of(delimiters, lastPos);
55  }
56 }
57 
58 void TokensToDoubles(std::vector<double>& values,
59  const std::vector<std::string>& tokens)
60 {
61  // turn tokens into doubles (its a miracle!)
62  for (std::size_t i = 0; i < tokens.size(); i++) {
63  double value = 0.0;
64  std::stringstream ss;
65  ss << tokens[i];
66  ss >> value;
67 
68  values.push_back(value);
69  }
70 }
71 
72 void TokensToInts(std::vector<int>& values,
73  const std::vector<std::string>& tokens)
74 {
75  // turn tokens into doubles (its a miracle!)
76  for (std::size_t i = 0; i < tokens.size(); i++) {
77  int value = 0;
78  std::stringstream ss;
79  ss << tokens[i];
80  ss >> value;
81 
82  values.push_back(value);
83  }
84 }
85 } // namespace Tempus
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.