VPTissue Reference Manual
StringUtils.h
Go to the documentation of this file.
1 #ifndef INC_STRINGUTILS_H
2 #define INC_STRINGUTILS_H
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
22 #include <algorithm>
23 #include <cctype>
24 #include <iomanip>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 
29 namespace SimPT_Sim {
30 namespace Util {
31 
32 // ================================
33 // Utilities to manipulate strings.
34 // ================================
35 
37 template<typename T>
38 inline static T FromString(const std::string& s)
39 {
40  std::stringstream ss(s);
41  T t;
42  ss >> t;
43  return t;
44 }
45 
47 static std::vector<std::string> Tokenize(const std::string& str, const std::string& delimiters)
48 {
49  std::vector<std::string> tokens;
50 
51  // Skip delimiters at beginning.
52  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
53  // Find first non-delimiter.
54  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
55 
56  while (std::string::npos != pos || std::string::npos != lastPos) {
57  // Found a token, add it to the vector.
58  tokens.push_back(str.substr(lastPos, pos - lastPos));
59  // Skip delimiters.
60  lastPos = str.find_first_not_of(delimiters, pos);
61  // Find next non-delimiter.
62  pos = str.find_first_of(delimiters, lastPos);
63  }
64 
65  return tokens;
66 }
67 
69 template<typename T>
70 inline static std::string ToString(const T& value)
71 {
72  std::stringstream ss;
73  ss << value;
74  return ss.str();
75 }
76 
78 template<typename T>
79 inline static std::string ToString(const T& value, int width, char fill = ' ')
80 {
81  std::stringstream ss;
82  ss << std::setw(width) << std::setfill(fill) << value;
83  return ss.str();
84 }
85 
87 static std::string ToUpper(const std::string& source)
88 {
89  auto upper = [](int c)->int {return std::toupper(c);};
90  std::string copy;
91  std::transform(source.begin(), source.end(), std::back_inserter(copy), upper);
92  return copy;
93 }
94 
96 static std::string TrimRight(const std::string& source, const std::string& t = " ")
97 {
98  std::string str = source;
99  return str.erase(str.find_last_not_of(t) + 1);
100 }
101 
103 static std::string TrimLeft(std::string const& source, const std::string& t = " ")
104 {
105  std::string str = source;
106  return str.erase(0, source.find_first_not_of(t));
107 }
108 
110 static std::string Trim(const std::string& source, const std::string& t = " ")
111 {
112  std::string str = source;
113  return TrimLeft(TrimRight(str, t), t);
114 }
115 
116 } // namespace
117 } // namespace
118 
119 #endif // end-of-include-guard
static std::string TrimRight(const std::string &source, const std::string &t=" ")
Trim characters at right end of string.
Definition: StringUtils.h:96
static std::string ToUpper(const std::string &source)
Builds a string with upper case characters only.
Definition: StringUtils.h:87
Namespace for the core simulator.
static std::vector< std::string > Tokenize(const std::string &str, const std::string &delimiters)
Tokenize a string (in order of occurence) by splitting it on the given delimiters.
Definition: StringUtils.h:47
static std::string ToString(const T &value)
Builds a string representation of a value of type T.
Definition: StringUtils.h:70
static T FromString(const std::string &s)
Builds a value of type T representation from a string.
Definition: StringUtils.h:38
static std::string TrimLeft(std::string const &source, const std::string &t=" ")
Trim characters at left end of string.
Definition: StringUtils.h:103
static std::string Trim(const std::string &source, const std::string &t=" ")
Trim characters at both ends of string.
Definition: StringUtils.h:110