Stride Reference Manual  1.0
StringUtils.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * any later version.
7  * The software is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License
12  * along with the software. If not, see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright 2017, Willem L, Kuylen E, Stijven S & Broeckhove J
15  */
16 
22 #include <boost/algorithm/string.hpp>
23 #include <boost/algorithm/string/replace.hpp>
24 #include <algorithm>
25 #include <cctype>
26 #include <iomanip>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 
31 namespace stride {
32 namespace util {
33 
37 class StringUtils {
38 public:
40  template<typename T>
41  inline static T fromString(std::string const& s) {
42  std::stringstream ss(s);
43  T t;
44  ss >> t;
45  return t;
46  }
47 
49  static std::vector<std::string> split(const std::string& str, const std::string& delimiters) {
50  std::vector<std::string> tokens;
51  boost::algorithm::split(tokens, str, boost::is_any_of(delimiters));
52  return tokens;
53  }
54 
58  static std::vector<std::string> tokenize(const std::string& str, const std::string& delimiters) {
59  std::vector<std::string> tokens;
60 
61  // Skip delimiters at beginning.
62  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
63  // Find first non-delimiter.
64  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
65 
66  while (std::string::npos != pos || std::string::npos != lastPos) {
67  // Found a token, add it to the vector.
68  tokens.push_back(str.substr(lastPos, pos - lastPos));
69  // Skip delimiters.
70  lastPos = str.find_first_not_of(delimiters, pos);
71  // Find next non-delimiter.
72  pos = str.find_first_of(delimiters, lastPos);
73  }
74 
75  return tokens;
76  }
77 
79  template<typename T>
80  inline static std::string toString(T const& value) {
81  std::stringstream ss;
82  ss << value;
83  return ss.str();
84  }
85 
87  template<typename T>
88  inline static std::string toString(T const& value, int width, char fill = ' ') {
89  std::stringstream ss;
90  ss << std::setw(width) << std::setfill(fill) << value;
91  return ss.str();
92  }
93 
95  static std::string toLower(std::string const& source) {
96  auto lower = [](int c) -> int { return std::toupper(c); };
97  std::string copy;
98  std::transform(source.begin(), source.end(), std::back_inserter(copy), lower);
99  return copy;
100  }
101 
103  static std::string toUpper(std::string const& source) {
104  auto upper = [](int c) -> int { return std::toupper(c); };
105  std::string copy;
106  std::transform(source.begin(), source.end(), std::back_inserter(copy), upper);
107  return copy;
108  }
109 
111  static std::string trimRight(std::string const& source, std::string const& t = " ") {
112  std::string str = source;
113  return str.erase(str.find_last_not_of(t) + 1);
114  }
115 
117  static std::string trimLeft(std::string const& source, std::string const& t = " ") {
118  std::string str = source;
119  return str.erase(0, source.find_first_not_of(t));
120  }
121 
123  static std::string trim(std::string const& source, std::string const& t = " ") {
124  std::string str = source;
125  return trimLeft(trimRight(str, t), t);
126  }
127 
129  static std::string replace(std::string source, std::string from, std::string to) {
130  return boost::replace_all_copy(source, from, to);
131  }
132 };
133 
134 }
135 }
136 
static std::string replace(std::string source, std::string from, std::string to)
Replace all occurences of a string with another.
Definition: StringUtils.h:129
static 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:58
static std::string toUpper(std::string const &source)
Builds a string with upper case characters only.
Definition: StringUtils.h:103
String utilities.
Definition: StringUtils.h:37
Time Dependent Person DataType.
Definition: NoBehaviour.h:17
static std::string toString(T const &value)
Builds a string representation of a value of type T.
Definition: StringUtils.h:80
static std::string toLower(std::string const &source)
Builds a string with lower case characters only.
Definition: StringUtils.h:95
static std::string trimLeft(std::string const &source, std::string const &t=" ")
Trim characters at left end of string.
Definition: StringUtils.h:117
static std::string trimRight(std::string const &source, std::string const &t=" ")
Trim characters at right end of string.
Definition: StringUtils.h:111
static T fromString(std::string const &s)
Builds a value of type T representation from a string.
Definition: StringUtils.h:41
static std::string trim(std::string const &source, std::string const &t=" ")
Trim characters at both ends of string.
Definition: StringUtils.h:123
static std::string toString(T const &value, int width, char fill= ' ')
Builds a string representation with minimum width of a value of type T.
Definition: StringUtils.h:88
static std::vector< std::string > split(const std::string &str, const std::string &delimiters)
Split a string (in order of occurence) by splitting it on the given delimiters.
Definition: StringUtils.h:49