]> granicus.if.org Git - icinga2/blob - lib/base/string.cpp
Merge pull request #7002 from Icinga/bugfix/check_network-percent-6155
[icinga2] / lib / base / string.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/string.hpp"
4 #include "base/value.hpp"
5 #include "base/primitivetype.hpp"
6 #include "base/dictionary.hpp"
7 #include <boost/algorithm/string/case_conv.hpp>
8 #include <boost/algorithm/string/trim.hpp>
9 #include <boost/algorithm/string/split.hpp>
10 #include <ostream>
11
12 using namespace icinga;
13
14 template class std::vector<String>;
15
16 REGISTER_BUILTIN_TYPE(String, String::GetPrototype());
17
18 const String::SizeType String::NPos = std::string::npos;
19
20 String::String(const char *data)
21         : m_Data(data)
22 { }
23
24 String::String(std::string data)
25         : m_Data(std::move(data))
26 { }
27
28 String::String(String::SizeType n, char c)
29         : m_Data(n, c)
30 { }
31
32 String::String(const String& other)
33         : m_Data(other)
34 { }
35
36 String::String(String&& other)
37         : m_Data(std::move(other.m_Data))
38 { }
39
40 #ifndef _MSC_VER
41 String::String(Value&& other)
42 {
43         *this = std::move(other);
44 }
45 #endif /* _MSC_VER */
46
47 String& String::operator=(Value&& other)
48 {
49         if (other.IsString())
50                 m_Data = std::move(other.Get<String>());
51         else
52                 *this = static_cast<String>(other);
53
54         return *this;
55 }
56
57 String& String::operator+=(const Value& rhs)
58 {
59         m_Data += static_cast<String>(rhs);
60         return *this;
61 }
62
63 String& String::operator=(const String& rhs)
64 {
65         m_Data = rhs.m_Data;
66         return *this;
67 }
68
69 String& String::operator=(String&& rhs)
70 {
71         m_Data = std::move(rhs.m_Data);
72         return *this;
73 }
74
75 String& String::operator=(const std::string& rhs)
76 {
77         m_Data = rhs;
78         return *this;
79 }
80
81 String& String::operator=(const char *rhs)
82 {
83         m_Data = rhs;
84         return *this;
85 }
86
87 const char& String::operator[](String::SizeType pos) const
88 {
89         return m_Data[pos];
90 }
91
92 char& String::operator[](String::SizeType pos)
93 {
94         return m_Data[pos];
95 }
96
97 String& String::operator+=(const String& rhs)
98 {
99         m_Data += rhs.m_Data;
100         return *this;
101 }
102
103 String& String::operator+=(const char *rhs)
104 {
105         m_Data += rhs;
106         return *this;
107 }
108
109 String& String::operator+=(char rhs)
110 {
111         m_Data += rhs;
112         return *this;
113 }
114
115 bool String::IsEmpty() const
116 {
117         return m_Data.empty();
118 }
119
120 bool String::operator<(const String& rhs) const
121 {
122         return m_Data < rhs.m_Data;
123 }
124
125 String::operator const std::string&() const
126 {
127         return m_Data;
128 }
129
130 const char *String::CStr() const
131 {
132         return m_Data.c_str();
133 }
134
135 void String::Clear()
136 {
137         m_Data.clear();
138 }
139
140 String::SizeType String::GetLength() const
141 {
142         return m_Data.size();
143 }
144
145 std::string& String::GetData()
146 {
147         return m_Data;
148 }
149
150 const std::string& String::GetData() const
151 {
152         return m_Data;
153 }
154
155 String::SizeType String::Find(const String& str, String::SizeType pos) const
156 {
157         return m_Data.find(str, pos);
158 }
159
160 String::SizeType String::RFind(const String& str, String::SizeType pos) const
161 {
162         return m_Data.rfind(str, pos);
163 }
164
165 String::SizeType String::FindFirstOf(const char *s, String::SizeType pos) const
166 {
167         return m_Data.find_first_of(s, pos);
168 }
169
170 String::SizeType String::FindFirstOf(char ch, String::SizeType pos) const
171 {
172         return m_Data.find_first_of(ch, pos);
173 }
174
175 String::SizeType String::FindFirstNotOf(const char *s, String::SizeType pos) const
176 {
177         return m_Data.find_first_not_of(s, pos);
178 }
179
180 String::SizeType String::FindFirstNotOf(char ch, String::SizeType pos) const
181 {
182         return m_Data.find_first_not_of(ch, pos);
183 }
184
185 String::SizeType String::FindLastOf(const char *s, String::SizeType pos) const
186 {
187         return m_Data.find_last_of(s, pos);
188 }
189
190 String::SizeType String::FindLastOf(char ch, String::SizeType pos) const
191 {
192         return m_Data.find_last_of(ch, pos);
193 }
194
195 String String::SubStr(String::SizeType first, String::SizeType len) const
196 {
197         return m_Data.substr(first, len);
198 }
199
200 std::vector<String> String::Split(const char *separators) const
201 {
202         std::vector<String> result;
203         boost::algorithm::split(result, m_Data, boost::is_any_of(separators));
204         return result;
205 }
206
207 void String::Replace(String::SizeType first, String::SizeType second, const String& str)
208 {
209         m_Data.replace(first, second, str);
210 }
211
212 String String::Trim() const
213 {
214         String t = m_Data;
215         boost::algorithm::trim(t);
216         return t;
217 }
218
219 String String::ToLower() const
220 {
221         String t = m_Data;
222         boost::algorithm::to_lower(t);
223         return t;
224 }
225
226 String String::ToUpper() const
227 {
228         String t = m_Data;
229         boost::algorithm::to_upper(t);
230         return t;
231 }
232
233 String String::Reverse() const
234 {
235         String t = m_Data;
236         std::reverse(t.m_Data.begin(), t.m_Data.end());
237         return t;
238 }
239
240 void String::Append(int count, char ch)
241 {
242         m_Data.append(count, ch);
243 }
244
245 bool String::Contains(const String& str) const
246 {
247         return (m_Data.find(str) != std::string::npos);
248 }
249
250 void String::swap(String& str)
251 {
252         m_Data.swap(str.m_Data);
253 }
254
255 String::Iterator String::erase(String::Iterator first, String::Iterator last)
256 {
257         return m_Data.erase(first, last);
258 }
259
260 String::Iterator String::Begin()
261 {
262         return m_Data.begin();
263 }
264
265 String::ConstIterator String::Begin() const
266 {
267         return m_Data.begin();
268 }
269
270 String::Iterator String::End()
271 {
272         return m_Data.end();
273 }
274
275 String::ConstIterator String::End() const
276 {
277         return m_Data.end();
278 }
279
280 String::ReverseIterator String::RBegin()
281 {
282         return m_Data.rbegin();
283 }
284
285 String::ConstReverseIterator String::RBegin() const
286 {
287         return m_Data.rbegin();
288 }
289
290 String::ReverseIterator String::REnd()
291 {
292         return m_Data.rend();
293 }
294
295 String::ConstReverseIterator String::REnd() const
296 {
297         return m_Data.rend();
298 }
299
300 std::ostream& icinga::operator<<(std::ostream& stream, const String& str)
301 {
302         stream << str.GetData();
303         return stream;
304 }
305
306 std::istream& icinga::operator>>(std::istream& stream, String& str)
307 {
308         std::string tstr;
309         stream >> tstr;
310         str = tstr;
311         return stream;
312 }
313
314 String icinga::operator+(const String& lhs, const String& rhs)
315 {
316         return lhs.GetData() + rhs.GetData();
317 }
318
319 String icinga::operator+(const String& lhs, const char *rhs)
320 {
321         return lhs.GetData() + rhs;
322 }
323
324 String icinga::operator+(const char *lhs, const String& rhs)
325 {
326         return lhs + rhs.GetData();
327 }
328
329 bool icinga::operator==(const String& lhs, const String& rhs)
330 {
331         return lhs.GetData() == rhs.GetData();
332 }
333
334 bool icinga::operator==(const String& lhs, const char *rhs)
335 {
336         return lhs.GetData() == rhs;
337 }
338
339 bool icinga::operator==(const char *lhs, const String& rhs)
340 {
341         return lhs == rhs.GetData();
342 }
343
344 bool icinga::operator<(const String& lhs, const char *rhs)
345 {
346         return lhs.GetData() < rhs;
347 }
348
349 bool icinga::operator<(const char *lhs, const String& rhs)
350 {
351         return lhs < rhs.GetData();
352 }
353
354 bool icinga::operator>(const String& lhs, const String& rhs)
355 {
356         return lhs.GetData() > rhs.GetData();
357 }
358
359 bool icinga::operator>(const String& lhs, const char *rhs)
360 {
361         return lhs.GetData() > rhs;
362 }
363
364 bool icinga::operator>(const char *lhs, const String& rhs)
365 {
366         return lhs > rhs.GetData();
367 }
368
369 bool icinga::operator<=(const String& lhs, const String& rhs)
370 {
371         return lhs.GetData() <= rhs.GetData();
372 }
373
374 bool icinga::operator<=(const String& lhs, const char *rhs)
375 {
376         return lhs.GetData() <= rhs;
377 }
378
379 bool icinga::operator<=(const char *lhs, const String& rhs)
380 {
381         return lhs <= rhs.GetData();
382 }
383
384 bool icinga::operator>=(const String& lhs, const String& rhs)
385 {
386         return lhs.GetData() >= rhs.GetData();
387 }
388
389 bool icinga::operator>=(const String& lhs, const char *rhs)
390 {
391         return lhs.GetData() >= rhs;
392 }
393
394 bool icinga::operator>=(const char *lhs, const String& rhs)
395 {
396         return lhs >= rhs.GetData();
397 }
398
399 bool icinga::operator!=(const String& lhs, const String& rhs)
400 {
401         return lhs.GetData() != rhs.GetData();
402 }
403
404 bool icinga::operator!=(const String& lhs, const char *rhs)
405 {
406         return lhs.GetData() != rhs;
407 }
408
409 bool icinga::operator!=(const char *lhs, const String& rhs)
410 {
411         return lhs != rhs.GetData();
412 }
413
414 String::Iterator icinga::begin(String& x)
415 {
416         return x.Begin();
417 }
418
419 String::ConstIterator icinga::begin(const String& x)
420 {
421         return x.Begin();
422 }
423
424 String::Iterator icinga::end(String& x)
425 {
426         return x.End();
427 }
428
429 String::ConstIterator icinga::end(const String& x)
430 {
431         return x.End();
432 }
433 String::Iterator icinga::range_begin(String& x)
434 {
435         return x.Begin();
436 }
437
438 String::ConstIterator icinga::range_begin(const String& x)
439 {
440         return x.Begin();
441 }
442
443 String::Iterator icinga::range_end(String& x)
444 {
445         return x.End();
446 }
447
448 String::ConstIterator icinga::range_end(const String& x)
449 {
450         return x.End();
451 }