QMCPACK
string_utils.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
8 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
9 //
10 // File created by: Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 #ifndef STRING_UTILS_H
15 #define STRING_UTILS_H
16 
17 
18 #include <cstdio>
19 #include <sstream>
20 #include <typeinfo>
21 #include <stdexcept>
22 #include <vector>
23 
24 namespace qmcplusplus
25 {
26 inline std::string strip(const std::string& s)
27 {
28  int start = s.length();
29  int end = 0;
30  int i;
31  for (i = 0; i < s.length(); i++)
32  {
33  if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t')
34  {
35  start = i;
36  break;
37  }
38  }
39  for (i = s.length() - 1; i > 0; i--)
40  {
41  if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t')
42  {
43  end = i;
44  break;
45  }
46  }
47  //app_log()<<"strip got '"<<s<<"'"<< std::endl;
48  //app_log()<<"start,end "<<start<<","<<end<<" "<<s[start]<<" "<<s[end]<< std::endl;
49  //app_log()<<"returning '"<<s.substr(start,end-start+1)<<"'"<< std::endl;
50  return s.substr(start, end - start + 1);
51 }
52 
53 
54 inline bool whitespace(char c) { return (c == ' ' || c == '\n' || c == '\t'); }
55 
56 
57 inline std::vector<std::string> split(const std::string& s)
58 {
59  std::vector<std::string> tokens;
60  int i = 0;
61  while (i < s.length())
62  {
63  while (i < s.length() && whitespace(s[i]))
64  i++;
65  int start = i;
66  while (i < s.length() && !whitespace(s[i]))
67  i++;
68  int end = i;
69  int len = end - start;
70  if (len > 0)
71  tokens.push_back(s.substr(start, len));
72  }
73  return tokens;
74 }
75 
76 
77 inline std::vector<std::string> split(const std::string& s, const std::string& pattern)
78 {
79  int sloc = 0;
80  int eloc;
81  int plen = pattern.length();
82  std::string ss;
83  std::vector<std::string> tokens;
84  //app_log() << "split got string:" << std::endl<<"'"<<s<<"'"<< std::endl;
85  while (true)
86  {
87  eloc = s.find(pattern, sloc);
88  if (eloc != std::string::npos)
89  {
90  ss = s.substr(sloc, eloc - sloc);
91  if (ss != "")
92  {
93  //app_log()<<" adding token: "<< std::endl;
94  //app_log()<<" '"<< ss <<"'" << std::endl;
95  tokens.push_back(ss);
96  }
97  sloc = eloc + plen;
98  }
99  else
100  {
101  eloc = s.length();
102  ss = s.substr(sloc, eloc - sloc);
103  if (ss != "")
104  {
105  //app_log()<<" adding token: "<< std::endl;
106  //app_log()<<" '"<< ss <<"'" << std::endl;
107  tokens.push_back(ss);
108  }
109  break;
110  }
111  }
112  return tokens;
113 }
114 
115 inline int string2int(const std::string& s) { return atoi(s.c_str()); }
116 
117 inline double string2real(const std::string& s) { return atof(s.c_str()); }
118 
119 inline std::string int2string(const int& i)
120 {
121  std::stringstream ss;
122  ss << i;
123  return ss.str();
124 }
125 
126 inline std::string real2string(const double& r)
127 {
128  std::stringstream ss;
129  ss << r;
130  return ss.str();
131 }
132 
133 inline bool string2bool(const std::string& s)
134 {
135  if (s == "true" || s == "yes" || s == "1")
136  {
137  return true;
138  }
139  else if (s == "false" || s == "no" || s == "0")
140  {
141  return false;
142  }
143  else
144  {
145  throw std::runtime_error("string2bool received non-boolean string: " + s);
146  return false;
147  }
148 }
149 
150 /// extract the contents of a string to a vector of something. separator is white spaces.
151 template<class T>
152 inline std::vector<T> convertStrToVec(const std::string& s)
153 {
154  std::istringstream stream(s);
155  std::vector<T> b;
156  while (!stream.eof())
157  {
158  if (T t; stream >> t)
159  b.push_back(t);
160  else if (!stream.eof() && stream.fail())
161  {
162  std::ostringstream msg;
163  msg << "Error parsing string '" << s << "' for type (type_info::name) " << typeid(T).name() << "." << std::endl;
164  throw std::runtime_error(msg.str());
165  }
166  }
167  return b;
168 }
169 
170 
171 //strings for input (OhmmsAttributeSet)
172 struct astring
173 {
174  std::string s;
175 };
176 
177 inline std::istream& operator>>(std::istream& is, astring& rhs)
178 {
179  char buf[256];
180  is.getline(buf, 256);
181  rhs.s.assign(buf);
182  return is;
183 }
184 
185 inline std::ostream& operator<<(std::ostream& os, const astring& rhs)
186 {
187  os << rhs.s << std::endl;
188  return os;
189 }
190 
191 inline bool operator==(const astring& lhs, const astring& rhs) { return lhs.s == rhs.s; }
192 } // namespace qmcplusplus
193 
194 #endif
bool whitespace(char c)
Definition: string_utils.h:54
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
std::vector< T > convertStrToVec(const std::string &s)
extract the contents of a string to a vector of something. separator is white spaces.
Definition: string_utils.h:152
std::string int2string(const int &i)
Definition: string_utils.h:119
bool operator==(const Matrix< T, Alloc > &lhs, const Matrix< T, Alloc > &rhs)
Definition: OhmmsMatrix.h:388
std::string strip(const std::string &s)
Definition: string_utils.h:26
int string2int(const std::string &s)
Definition: string_utils.h:115
std::vector< std::string > split(const std::string &s)
Definition: string_utils.h:57
std::string real2string(const double &r)
Definition: string_utils.h:126
std::ostream & operator<<(std::ostream &out, const AntiSymTensor< T, D > &rhs)
double string2real(const std::string &s)
Definition: string_utils.h:117
bool string2bool(const std::string &s)
Definition: string_utils.h:133
std::istream & operator>>(std::istream &is, Matrix< T, Alloc > &rhs)
Definition: OhmmsMatrix.h:427