ESyS-Particle  2.3.2
StringUtil.h
Go to the documentation of this file.
1 
2 // //
3 // Copyright (c) 2003-2014 by The University of Queensland //
4 // Centre for Geoscience Computing //
5 // http://earth.uq.edu.au/centre-geoscience-computing //
6 // //
7 // Primary Business: Brisbane, Queensland, Australia //
8 // Licensed under the Open Software License version 3.0 //
9 // http://www.opensource.org/licenses/osl-3.0.php //
10 // //
12 
13 
14 #ifndef ESYS_LSMSTRINGUTIL_H
15 #define ESYS_LSMSTRINGUTIL_H
16 
17 #include <string>
18 #include <vector>
19 #include <sstream>
20 
21 namespace esys
22 {
23  namespace lsm
24  {
25  typedef std::vector<std::string> StringVector;
29  namespace StringUtil
30  {
32  template <class TmplIterator>
34  {
35  public:
36  std::string operator()(const TmplIterator &it) const
37  {
38  std::stringstream sStream;
39  sStream << *it;
40 
41  return sStream.str();
42  }
43  };
44 
45  template<class TmplIterator, class TmplStringOperator>
46  inline
47  std::string join(
48  TmplIterator begin,
49  TmplIterator end,
50  const std::string &delim,
51  TmplStringOperator toStringOp = StdOStreamOp<TmplIterator>())
52  {
53  std::string str;
54  if (begin != end)
55  {
56  TmplIterator it = begin;
57  str = toStringOp(begin);
58  for (it++; it != end; it++)
59  {
60  str += delim + toStringOp(it);
61  }
62  }
63  return str;
64  }
65 
66  template<class TmplContainer, class TmplStringOperator>
67  inline
68  std::string join(
69  const TmplContainer &container,
70  const std::string &delim,
71  TmplStringOperator toStringOp = StdOStreamOp<typename TmplContainer::const_iterator>())
72  {
73  typename TmplContainer::const_iterator it = container.begin();
74  typename TmplContainer::const_iterator end = container.end();
75  std::string str;
76  if (it != end)
77  {
78  str = toStringOp(it);
79  for (it++; it != end; it++)
80  {
81  str += delim + toStringOp(it);
82  }
83  }
84  return str;
85  }
86 
87  inline
88  std::string joinStringVector(
89  const StringVector &container,
90  const std::string &delim
91  )
92  {
93  StringVector::const_iterator it = container.begin();
94  StringVector::const_iterator end = container.end();
95  std::string str;
96  if (it != end)
97  {
98  str = *it;
99  for (it++; it != end; it++)
100  {
101  str += delim + *it;
102  }
103  }
104  return str;
105  }
106 
107  template <class TmplData>
109  {
110  public:
111  TmplData operator()(const std::string &str) const
112  {
113  std::stringstream sStream(str);
114  TmplData data;
115  sStream >> data;
116 
117  return data;
118  }
119  };
120 
121  template <typename TmplData>
122  TmplData to(const std::string &str)
123  {
124  return StdIStreamOp<TmplData>()(str);
125  }
126 
127  template <typename TmplData>
128  std::string toString(const TmplData &data)
129  {
130  std::stringstream sStream;
131  sStream << data;
132  return sStream.str();
133  }
134 
135  template<class TmplData, class TmplStdStreamOp>
136  inline
137  std::vector<TmplData> split(
138  const std::string &str,
139  const std::string &delim,
140  TmplStdStreamOp fromStringOp = StdIStreamOp<TmplData>()
141  )
142  {
143  std::vector<TmplData> vec;
144  std::string::size_type prevPos = 0;
145  std::string::size_type currPos = str.find(delim);
146  while (currPos != std::string::npos)
147  {
148  vec.push_back(
149  fromStringOp(str.substr(prevPos, currPos - prevPos))
150  );
151  prevPos = currPos + delim.length();
152  currPos = str.find(delim, prevPos);
153  }
154  if (prevPos < str.length())
155  {
156  vec.push_back(
157  fromStringOp(str.substr(prevPos, str.length()))
158  );
159  }
160  else if (prevPos == str.length())
161  {
162  vec.push_back(fromStringOp(""));
163  }
164 
165  return vec;
166  }
167 
168  inline
169  StringVector splitStrings(const std::string &str, const std::string &delim)
170  {
171  return split<std::string, StdIStreamOp<std::string> >(str, delim, StdIStreamOp<std::string>());
172  }
173 
174  inline
175  std::string trim(const std::string &str)
176  {
177  size_t b = 0;
178  for (; (b < str.size()) && std::isspace(str.at(b)); b++)
179  {
180  // continue
181  }
182 
183  size_t e = 0;
184  if (str.size() > 0)
185  {
186  e = str.size()-1;
187  for (; (e > b) && std::isspace(str.at(e)); e--)
188  {
189  // continue
190  }
191  }
192  return ((e >= b) ? str.substr(b, e-b+1) : "");
193  }
194  }
195  }
196 }
197 
198 #endif