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