StringHelpers.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX::
19 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
20 * @date 2014
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
28 
29 #include <unordered_map>
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 #include <deque>
34 #include <map>
35 
36 namespace armarx
37 {
38  using std::to_string;
39 
40  inline const std::string& to_string(const std::string& s)
41  {
42  return s;
43  }
44 
45  inline std::string to_string(std::string s)
46  {
47  return s;
48  }
49 
50  /**
51  * @brief Converts a string to float and uses always *dot* as seperator.
52  */
53  float toFloat(const std::string& input);
54  int toInt(const std::string& input);
55  unsigned int toUInt(const std::string& input);
56 
57  template <typename T>
58  std::string ValueToString(const T& value)
59  {
60  std::stringstream str;
61  str << value;
62  return str.str();
63  }
64 
65  bool Contains(const std::string& haystack, const std::string& needle, bool caseInsensitive = false);
66 
67  std::vector<std::string> Split(const std::string& source, const std::string& splitBy, bool trimElements = false, bool removeEmptyElements = false);
68  std::vector<std::string> split(const std::string& source, const std::string& splitBy, bool trimElements = false, bool removeEmptyElements = false);
69  bool starts_with(const std::string& haystack, const std::string& needle);
70  bool ends_with(const std::string& haystack, const std::string& needle);
71 
72  inline void EncodeInline(std::string& data)
73  {
74  std::string buffer;
75  buffer.reserve(data.size());
76  for (size_t pos = 0; pos != data.size(); ++pos)
77  {
78  switch (data[pos])
79  {
80  case '&':
81  buffer.append("&amp;");
82  break;
83  case '\"':
84  buffer.append("&quot;");
85  break;
86  case '\'':
87  buffer.append("&apos;");
88  break;
89  case '<':
90  buffer.append("&lt;");
91  break;
92  case '>':
93  buffer.append("&gt;");
94  break;
95  default:
96  buffer.append(&data[pos], 1);
97  break;
98  }
99  }
100  data.swap(buffer);
101  }
102 
103  std::string Encode(const std::string& data);
104 }
105 
106 namespace std
107 {
108  template <typename T>
109  std::string& operator <<(std::string& str, const T& value)
110  {
112  return str;
113  }
114 
115  template<typename T>
116  ostream&
117  operator<<(ostream& str, const std::vector<T>& vector)
118  {
119  str << "Vector<" << armarx::GetTypeString<T>() << ">(" << vector.size() << "):\n";
120 
121  for (unsigned int i = 0; i < vector.size(); ++i)
122  {
123  str << "\t(" << i << "): " << vector.at(i) << "\n";
124  }
125 
126  return str;
127  }
128 
129  template<typename T>
130  ostream&
131  operator<<(ostream& str, const std::deque<T>& deque)
132  {
133  str << "Deque<" << armarx::GetTypeString<T>() << ">(" << deque.size() << "):\n";
134 
135  for (unsigned int i = 0; i < deque.size(); ++i)
136  {
137  str << "\t(" << i << "): " << deque.at(i) << "\n";
138  }
139 
140  return str;
141  }
142 
143  template<typename T1, typename T2>
144  ostream&
145  operator<<(ostream& str, const std::pair<T1, T2>& pair)
146  {
147  str << "Pair<" << armarx::GetTypeString<T1>() << ", " << armarx::GetTypeString<T2>() << ">:\n";
148  str << "\t(" << pair.first << ", " << pair.second << ")\n";
149  return str;
150  }
151 
152  template<typename T1, typename T2>
153  ostream&
154  operator<<(ostream& str, const std::map<T1, T2>& map)
155  {
156  str << "Map<" << armarx::GetTypeString<T1>() << ", " << armarx::GetTypeString<T2>() << ">(" << map.size() << "):\n";
157  for (const auto& pair : map)
158  {
159  str << "\t" << pair.first << ": " << pair.second << "\n";
160  }
161  return str;
162  }
163 
164  template<typename T1, typename T2>
165  ostream&
166  operator<<(ostream& str, const std::unordered_map<T1, T2>& map)
167  {
168  str << "Unordered Map<" << armarx::GetTypeString<T1>() << ", " << armarx::GetTypeString<T2>() << ">(" << map.size() << "):\n";
169  for (const auto& pair : map)
170  {
171  str << "\t" << pair.first << ": " << pair.second << "\n";
172  }
173  return str;
174  }
175 
176  inline ostream& operator<<(ostream& str, std::nullptr_t)
177  {
178  return str << static_cast<void*>(nullptr);
179  }
180 }
181 
182 #define VAROUT(x) std::string(std::string(#x) +": " + armarx::ValueToString(x)) + " "
183 
armarx::toUInt
unsigned int toUInt(const std::string &input)
Definition: StringHelpers.cpp:118
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
GetTypeString.h
armarx::toInt
int toInt(const std::string &input)
Definition: StringHelpers.cpp:108
armarx::Contains
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition: algorithm.h:295
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:35
armarx::toFloat
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.
Definition: StringHelpers.cpp:97
armarx::Encode
std::string Encode(const std::string &data)
Definition: StringHelpers.cpp:65
armarx::starts_with
bool starts_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:43
armarx::EncodeInline
void EncodeInline(std::string &data)
Definition: StringHelpers.h:72
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::ValueToString
std::string ValueToString(const T &value)
Definition: StringHelpers.h:58
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::to_string
std::string to_string(std::string s)
Definition: StringHelpers.h:45
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
std::operator<<
ARMARXCORE_IMPORT_EXPORT ostream & operator<<(ostream &stream, const armarx::RunningTaskIceBase &task)
std
Definition: Application.h:66
armarx::ends_with
bool ends_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:50
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36