Stride Reference Manual  - generated for commit 9643b11
StringUtils.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #pragma once
22 
23 #include <boost/algorithm/string.hpp>
24 #include <algorithm>
25 #include <cctype>
26 #include <iomanip>
27 #include <sstream>
28 #include <string>
29 
30 #include <vector>
31 
32 namespace stride {
33 namespace util {
34 
36 inline bool CheckAllDigits(const std::string& s)
37 {
38  bool status = true;
39  for (const auto& e : s) {
40  status = (status && isdigit(e));
41  if (!status)
42  break;
43  }
44  return status;
45 }
46 
48 template <typename T>
49 inline T FromString(const std::string& s)
50 {
51  std::stringstream ss(s);
52  T t;
53  ss >> t;
54  return t;
55 }
56 
58 inline std::vector<std::string> Split(const std::string& s, const std::string& delimiters)
59 {
60  std::vector<std::string> tokens;
61  boost::algorithm::split(tokens, s, boost::is_any_of(delimiters));
62  return tokens;
63 }
64 
67 inline std::vector<std::string> Tokenize(const std::string& str, const std::string& delimiters)
68 {
69  std::vector<std::string> tokens;
70 
71  // Skip delimiters at beginning.
72  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
73  // Find first non-delimiter.
74  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
75 
76  while (std::string::npos != pos || std::string::npos != lastPos) {
77  // Found a token, add it to the vector.
78  tokens.push_back(str.substr(lastPos, pos - lastPos));
79  // Skip delimiters.
80  lastPos = str.find_first_not_of(delimiters, pos);
81  // Find next non-delimiter.
82  pos = str.find_first_of(delimiters, lastPos);
83  }
84 
85  return tokens;
86 }
87 
89 template <typename T>
90 inline std::string ToString(const T& value)
91 {
92  std::stringstream ss;
93  ss << value;
94  return ss.str();
95 }
96 
97 template <>
98 inline std::string ToString<std::string>(const std::string& value)
99 {
100  return value;
101 }
102 
104 template <typename It>
105 inline std::vector<std::string> ToString(
106  typename std::enable_if<!std::is_same<typename It::value_type, std::string>::value, It>::type first, It last)
107 {
108  std::vector<std::string> v;
109  for (It it = first; it < last; ++it) {
110  v.emplace_back(ToString(*it));
111  }
112  return v;
113 }
114 
116 template <typename It>
117 inline std::vector<std::string> ToString(
118  typename std::enable_if<std::is_same<typename It::value_type, std::string>::value, It>::type first, It last)
119 {
120  std::vector<std::string> v;
121  std::copy(first, last, back_inserter(v));
122  return v;
123 }
124 
126 template <typename T>
127 inline std::string ToString(const T& value, int width, char fill = ' ')
128 {
129  std::stringstream ss;
130  ss << std::setw(width) << std::setfill(fill) << value;
131  return ss.str();
132 }
133 
135 inline std::string ToLower(const std::string& source)
136 {
137  auto lower = [](int c) -> int { return std::tolower(c); };
138  std::string copy;
139  std::transform(source.begin(), source.end(), std::back_inserter(copy), lower);
140  return copy;
141 }
142 
144 inline std::string ToUpper(const std::string& source)
145 {
146  auto upper = [](int c) -> int { return std::toupper(c); };
147  std::string copy;
148  std::transform(source.begin(), source.end(), std::back_inserter(copy), upper);
149  return copy;
150 }
151 
153 inline std::string TrimRight(const std::string& source, const std::string& t = " ")
154 {
155  std::string str = source;
156  return str.erase(str.find_last_not_of(t) + 1);
157 }
158 
160 inline std::string TrimLeft(const std::string& source, const std::string& t = " ")
161 {
162  std::string str = source;
163  return str.erase(0, source.find_first_not_of(t));
164 }
165 
167 inline std::string Trim(const std::string& source, const std::string& t = " ")
168 {
169  return TrimLeft(TrimRight(source, t), t);
170 }
171 
172 template <typename T>
173 inline std::string intToDottedString(const T& value)
174 {
175  std::string valueStr = std::to_string(value);
176 
177  std::string res;
178  std::size_t rest = valueStr.length() % 3;
179 
180  res += valueStr.substr(0, rest);
181 
182  for (size_t i = rest; i < valueStr.length(); i += 3) {
183  res += "." + valueStr.substr(i, 3);
184  }
185 
186  if (res[0] == '.') {
187  return res.substr(1);
188  }
189 
190  return res;
191 }
192 
193 } // namespace util
194 } // namespace stride
std::string ToString(const T &value)
Builds a string representation of a value of type T.
Definition: StringUtils.h:90
T FromString(const std::string &s)
Builds a value of type T representation from a string.
Definition: StringUtils.h:49
std::string ToUpper(const std::string &source)
Builds a string with upper case characters only.
Definition: StringUtils.h:144
std::string intToDottedString(const T &value)
Definition: StringUtils.h:173
std::vector< std::string > Split(const std::string &s, const std::string &delimiters)
Split a string (in order of occurence) by splitting it on the given delimiters.
Definition: StringUtils.h:58
std::vector< std::string > Tokenize(const std::string &str, const std::string &delimiters)
Tokenize a string (in order of occurence) with the given delimiters.
Definition: StringUtils.h:67
std::string TrimLeft(const std::string &source, const std::string &t=" ")
Trim characters at left end of string.
Definition: StringUtils.h:160
std::string TrimRight(const std::string &source, const std::string &t=" ")
Trim characters at right end of string.
Definition: StringUtils.h:153
std::string Trim(const std::string &source, const std::string &t=" ")
Trim characters at both ends of string.
Definition: StringUtils.h:167
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
std::string ToLower(const std::string &source)
Builds a string with lower case characters only.
Definition: StringUtils.h:135
bool CheckAllDigits(const std::string &s)
All characters in string are digits (or string is empty)
Definition: StringUtils.h:36