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
27#include <deque>
28#include <map>
29#include <sstream>
30#include <string>
31#include <unordered_map>
32#include <vector>
33
35
36namespace armarx
37{
38 using std::to_string;
39
40 inline const std::string&
41 to_string(const std::string& s)
42 {
43 return s;
44 }
45
46 inline std::string
47 to_string(std::string s)
48 {
49 return s;
50 }
51
52 /**
53 * @brief Converts a string to float and uses always *dot* as seperator.
54 */
55 float toFloat(const std::string& input);
56 int toInt(const std::string& input);
57 unsigned int toUInt(const std::string& input);
58
59 template <typename T>
60 std::string
61 ValueToString(const T& value)
62 {
63 std::stringstream str;
64 str << value;
65 return str.str();
66 }
67
68 bool
69 Contains(const std::string& haystack, const std::string& needle, bool caseInsensitive = false);
70
71 std::vector<std::string> Split(const std::string& source,
72 const std::string& splitBy,
73 bool trimElements = false,
74 bool removeEmptyElements = false);
75 std::vector<std::string> split(const std::string& source,
76 const std::string& splitBy,
77 bool trimElements = false,
78 bool removeEmptyElements = false);
79 bool starts_with(const std::string& haystack, const std::string& needle);
80 bool ends_with(const std::string& haystack, const std::string& needle);
81
82 inline void
83 EncodeInline(std::string& data)
84 {
85 std::string buffer;
86 buffer.reserve(data.size());
87 for (size_t pos = 0; pos != data.size(); ++pos)
88 {
89 switch (data[pos])
90 {
91 case '&':
92 buffer.append("&amp;");
93 break;
94 case '\"':
95 buffer.append("&quot;");
96 break;
97 case '\'':
98 buffer.append("&apos;");
99 break;
100 case '<':
101 buffer.append("&lt;");
102 break;
103 case '>':
104 buffer.append("&gt;");
105 break;
106 default:
107 buffer.append(&data[pos], 1);
108 break;
109 }
110 }
111 data.swap(buffer);
112 }
113
114 std::string Encode(const std::string& data);
115} // namespace armarx
116
117namespace std
118{
119 template <typename T>
120 std::string&
121 operator<<(std::string& str, const T& value)
122 {
123 str += armarx::ValueToString(value);
124 return str;
125 }
126
127 template <typename T>
128 ostream&
129 operator<<(ostream& str, const std::vector<T>& vector)
130 {
131 str << "Vector<" << armarx::GetTypeString<T>() << ">(" << vector.size() << "):\n";
132
133 for (unsigned int i = 0; i < vector.size(); ++i)
134 {
135 str << "\t(" << i << "): " << vector.at(i) << "\n";
136 }
137
138 return str;
139 }
140
141 template <typename T>
142 ostream&
143 operator<<(ostream& str, const std::deque<T>& deque)
144 {
145 str << "Deque<" << armarx::GetTypeString<T>() << ">(" << deque.size() << "):\n";
146
147 for (unsigned int i = 0; i < deque.size(); ++i)
148 {
149 str << "\t(" << i << "): " << deque.at(i) << "\n";
150 }
151
152 return str;
153 }
154
155 template <typename T1, typename T2>
156 ostream&
157 operator<<(ostream& str, const std::pair<T1, T2>& pair)
158 {
159 str << "Pair<" << armarx::GetTypeString<T1>() << ", " << armarx::GetTypeString<T2>()
160 << ">:\n";
161 str << "\t(" << pair.first << ", " << pair.second << ")\n";
162 return str;
163 }
164
165 template <typename T1, typename T2>
166 ostream&
167 operator<<(ostream& str, const std::map<T1, T2>& map)
168 {
169 str << "Map<" << armarx::GetTypeString<T1>() << ", " << armarx::GetTypeString<T2>() << ">("
170 << map.size() << "):\n";
171 for (const auto& pair : map)
172 {
173 str << "\t" << pair.first << ": " << pair.second << "\n";
174 }
175 return str;
176 }
177
178 template <typename T1, typename T2>
179 ostream&
180 operator<<(ostream& str, const std::unordered_map<T1, T2>& map)
181 {
182 str << "Unordered Map<" << armarx::GetTypeString<T1>() << ", "
183 << armarx::GetTypeString<T2>() << ">(" << map.size() << "):\n";
184 for (const auto& pair : map)
185 {
186 str << "\t" << pair.first << ": " << pair.second << "\n";
187 }
188 return str;
189 }
190
191 inline ostream&
192 operator<<(ostream& str, std::nullptr_t)
193 {
194 return str << static_cast<void*>(nullptr);
195 }
196} // namespace std
197
198#define VAROUT(x) std::string(std::string(#x) + ": " + armarx::ValueToString(x)) + " "
199#define QUOTED(x) std::string("`" + armarx::ValueToString(x) + "`")
uint8_t data[1]
std::string str(const T &t)
This file offers overloads of toIce() and fromIce() functions for STL container types.
void EncodeInline(std::string &data)
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
bool ends_with(const std::string &haystack, const std::string &needle)
unsigned int toUInt(const std::string &input)
std::string Encode(const std::string &data)
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition algorithm.h:330
int toInt(const std::string &input)
bool starts_with(const std::string &haystack, const std::string &needle)
const std::string & to_string(const std::string &s)
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.
std::string ValueToString(const T &value)
ARMARXCORE_IMPORT_EXPORT ostream & operator<<(ostream &stream, const armarx::RunningTaskIceBase &task)