1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
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. *
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. *
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 ******************************************************************************/
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>
29 using namespace icinga;
31 template class std::vector<String>;
33 REGISTER_BUILTIN_TYPE(String, String::GetPrototype());
35 const String::SizeType String::NPos = std::string::npos;
37 String::String(const char *data)
41 String::String(std::string data)
42 : m_Data(std::move(data))
45 String::String(String::SizeType n, char c)
49 String::String(const String& other)
53 String::String(String&& other)
54 : m_Data(std::move(other.m_Data))
58 String::String(Value&& other)
60 *this = std::move(other);
64 String& String::operator=(Value&& other)
67 m_Data = std::move(other.Get<String>());
69 *this = static_cast<String>(other);
74 String& String::operator+=(const Value& rhs)
76 m_Data += static_cast<String>(rhs);
80 String& String::operator=(const String& rhs)
86 String& String::operator=(String&& rhs)
88 m_Data = std::move(rhs.m_Data);
92 String& String::operator=(const std::string& rhs)
98 String& String::operator=(const char *rhs)
104 const char& String::operator[](String::SizeType pos) const
109 char& String::operator[](String::SizeType pos)
114 String& String::operator+=(const String& rhs)
116 m_Data += rhs.m_Data;
120 String& String::operator+=(const char *rhs)
126 String& String::operator+=(char rhs)
132 bool String::IsEmpty() const
134 return m_Data.empty();
137 bool String::operator<(const String& rhs) const
139 return m_Data < rhs.m_Data;
142 String::operator const std::string&() const
147 const char *String::CStr() const
149 return m_Data.c_str();
157 String::SizeType String::GetLength() const
159 return m_Data.size();
162 std::string& String::GetData()
167 const std::string& String::GetData() const
172 String::SizeType String::Find(const String& str, String::SizeType pos) const
174 return m_Data.find(str, pos);
177 String::SizeType String::RFind(const String& str, String::SizeType pos) const
179 return m_Data.rfind(str, pos);
182 String::SizeType String::FindFirstOf(const char *s, String::SizeType pos) const
184 return m_Data.find_first_of(s, pos);
187 String::SizeType String::FindFirstOf(char ch, String::SizeType pos) const
189 return m_Data.find_first_of(ch, pos);
192 String::SizeType String::FindFirstNotOf(const char *s, String::SizeType pos) const
194 return m_Data.find_first_not_of(s, pos);
197 String::SizeType String::FindFirstNotOf(char ch, String::SizeType pos) const
199 return m_Data.find_first_not_of(ch, pos);
202 String::SizeType String::FindLastOf(const char *s, String::SizeType pos) const
204 return m_Data.find_last_of(s, pos);
207 String::SizeType String::FindLastOf(char ch, String::SizeType pos) const
209 return m_Data.find_last_of(ch, pos);
212 String String::SubStr(String::SizeType first, String::SizeType len) const
214 return m_Data.substr(first, len);
217 std::vector<String> String::Split(const char *separators) const
219 std::vector<String> result;
220 boost::algorithm::split(result, m_Data, boost::is_any_of(separators));
224 void String::Replace(String::SizeType first, String::SizeType second, const String& str)
226 m_Data.replace(first, second, str);
229 String String::Trim() const
232 boost::algorithm::trim(t);
236 String String::ToLower() const
239 boost::algorithm::to_lower(t);
243 String String::ToUpper() const
246 boost::algorithm::to_upper(t);
250 String String::Reverse() const
253 std::reverse(t.m_Data.begin(), t.m_Data.end());
257 void String::Append(int count, char ch)
259 m_Data.append(count, ch);
262 bool String::Contains(const String& str) const
264 return (m_Data.find(str) != std::string::npos);
267 void String::swap(String& str)
269 m_Data.swap(str.m_Data);
272 String::Iterator String::erase(String::Iterator first, String::Iterator last)
274 return m_Data.erase(first, last);
277 String::Iterator String::Begin()
279 return m_Data.begin();
282 String::ConstIterator String::Begin() const
284 return m_Data.begin();
287 String::Iterator String::End()
292 String::ConstIterator String::End() const
297 String::ReverseIterator String::RBegin()
299 return m_Data.rbegin();
302 String::ConstReverseIterator String::RBegin() const
304 return m_Data.rbegin();
307 String::ReverseIterator String::REnd()
309 return m_Data.rend();
312 String::ConstReverseIterator String::REnd() const
314 return m_Data.rend();
317 std::ostream& icinga::operator<<(std::ostream& stream, const String& str)
319 stream << str.GetData();
323 std::istream& icinga::operator>>(std::istream& stream, String& str)
331 String icinga::operator+(const String& lhs, const String& rhs)
333 return lhs.GetData() + rhs.GetData();
336 String icinga::operator+(const String& lhs, const char *rhs)
338 return lhs.GetData() + rhs;
341 String icinga::operator+(const char *lhs, const String& rhs)
343 return lhs + rhs.GetData();
346 bool icinga::operator==(const String& lhs, const String& rhs)
348 return lhs.GetData() == rhs.GetData();
351 bool icinga::operator==(const String& lhs, const char *rhs)
353 return lhs.GetData() == rhs;
356 bool icinga::operator==(const char *lhs, const String& rhs)
358 return lhs == rhs.GetData();
361 bool icinga::operator<(const String& lhs, const char *rhs)
363 return lhs.GetData() < rhs;
366 bool icinga::operator<(const char *lhs, const String& rhs)
368 return lhs < rhs.GetData();
371 bool icinga::operator>(const String& lhs, const String& rhs)
373 return lhs.GetData() > rhs.GetData();
376 bool icinga::operator>(const String& lhs, const char *rhs)
378 return lhs.GetData() > rhs;
381 bool icinga::operator>(const char *lhs, const String& rhs)
383 return lhs > rhs.GetData();
386 bool icinga::operator<=(const String& lhs, const String& rhs)
388 return lhs.GetData() <= rhs.GetData();
391 bool icinga::operator<=(const String& lhs, const char *rhs)
393 return lhs.GetData() <= rhs;
396 bool icinga::operator<=(const char *lhs, const String& rhs)
398 return lhs <= rhs.GetData();
401 bool icinga::operator>=(const String& lhs, const String& rhs)
403 return lhs.GetData() >= rhs.GetData();
406 bool icinga::operator>=(const String& lhs, const char *rhs)
408 return lhs.GetData() >= rhs;
411 bool icinga::operator>=(const char *lhs, const String& rhs)
413 return lhs >= rhs.GetData();
416 bool icinga::operator!=(const String& lhs, const String& rhs)
418 return lhs.GetData() != rhs.GetData();
421 bool icinga::operator!=(const String& lhs, const char *rhs)
423 return lhs.GetData() != rhs;
426 bool icinga::operator!=(const char *lhs, const String& rhs)
428 return lhs != rhs.GetData();
431 String::Iterator icinga::begin(String& x)
436 String::ConstIterator icinga::begin(const String& x)
441 String::Iterator icinga::end(String& x)
446 String::ConstIterator icinga::end(const String& x)
450 String::Iterator icinga::range_begin(String& x)
455 String::ConstIterator icinga::range_begin(const String& x)
460 String::Iterator icinga::range_end(String& x)
465 String::ConstIterator icinga::range_end(const String& x)