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: Time Integration and Sensitivity Analysis Package
4 //
5 // Copyright 2017 NTESS and the Tempus contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 //@HEADER
9 
11 #include <sstream>
12 
13 namespace Tempus {
14 
15 void trim(std::string& str)
16 {
17  const std::string whitespace(" \t\n");
18 
19  const auto strBegin = str.find_first_not_of(whitespace);
20  if (strBegin == std::string::npos) {
21  str = "";
22  return; // no content
23  }
24 
25  const auto strEnd = str.find_last_not_of(whitespace);
26  const auto strRange = strEnd - strBegin + 1;
27 
28  str = str.substr(strBegin, strRange);
29  return;
30 }
31 
32 void StringTokenizer(std::vector<std::string>& tokens, 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  // grab token, trim if desired
44  std::string token = str.substr(lastPos, pos - lastPos);
45  if (trim) Tempus::trim(token);
46 
47  // Found a token, add it to the vector.
48  tokens.push_back(token);
49 
50  if (pos == string::npos) break;
51 
52  // Skip delimiters. Note the "not_of"
53  lastPos = str.find_first_not_of(delimiters, pos);
54  // Find next "non-delimiter"
55  pos = str.find_first_of(delimiters, lastPos);
56  }
57 }
58 
59 void TokensToDoubles(std::vector<double>& values,
60  const std::vector<std::string>& tokens)
61 {
62  // turn tokens into doubles (its a miracle!)
63  for (std::size_t i = 0; i < tokens.size(); i++) {
64  double value = 0.0;
65  std::stringstream ss;
66  ss << tokens[i];
67  ss >> value;
68 
69  values.push_back(value);
70  }
71 }
72 
73 void TokensToInts(std::vector<int>& values,
74  const std::vector<std::string>& tokens)
75 {
76  // turn tokens into doubles (its a miracle!)
77  for (std::size_t i = 0; i < tokens.size(); i++) {
78  int value = 0;
79  std::stringstream ss;
80  ss << tokens[i];
81  ss >> value;
82 
83  values.push_back(value);
84  }
85 }
86 } // 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.