2012-09-17 13:35:55 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2012-09-17 13:35:55 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
#ifndef STRING_H
|
|
|
|
#define STRING_H
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/i2-base.hpp"
|
2014-12-12 15:19:23 +01:00
|
|
|
#include "base/object.hpp"
|
2015-08-27 15:50:14 +02:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
2014-08-25 15:12:39 +02:00
|
|
|
#include <boost/range/iterator.hpp>
|
2014-05-11 17:14:35 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
2015-03-29 00:03:47 +01:00
|
|
|
#include <iosfwd>
|
2013-03-15 18:21:29 +01:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
namespace icinga {
|
|
|
|
|
2013-11-07 13:41:24 +01:00
|
|
|
class Value;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
/**
|
|
|
|
* String class.
|
|
|
|
*
|
2012-09-17 13:35:55 +02:00
|
|
|
* Rationale for having this: The std::string class has an ambiguous assignment
|
2012-08-02 09:38:08 +02:00
|
|
|
* operator when used in conjunction with the Value class.
|
|
|
|
*/
|
|
|
|
class I2_BASE_API String
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef std::string::iterator Iterator;
|
|
|
|
typedef std::string::const_iterator ConstIterator;
|
|
|
|
|
2013-03-10 03:09:01 +01:00
|
|
|
typedef std::string::iterator iterator;
|
|
|
|
typedef std::string::const_iterator const_iterator;
|
|
|
|
|
2015-06-29 13:23:49 +02:00
|
|
|
typedef std::string::reverse_iterator ReverseIterator;
|
|
|
|
typedef std::string::const_reverse_iterator ConstReverseIterator;
|
|
|
|
|
|
|
|
typedef std::string::reverse_iterator reverse_iterator;
|
|
|
|
typedef std::string::const_reverse_iterator const_reverse_iterator;
|
|
|
|
|
2014-05-11 06:30:50 +02:00
|
|
|
typedef std::string::size_type SizeType;
|
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline String(void)
|
|
|
|
: m_Data()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
inline String(const char *data)
|
|
|
|
: m_Data(data)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
inline String(const std::string& data)
|
|
|
|
: m_Data(data)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
inline String(String::SizeType n, char c)
|
|
|
|
: m_Data(n, c)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
inline String(const String& other)
|
|
|
|
: m_Data(other.m_Data)
|
|
|
|
{ }
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2015-03-28 16:13:44 +01:00
|
|
|
inline ~String(void)
|
|
|
|
{ }
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
template<typename InputIterator>
|
|
|
|
String(InputIterator begin, InputIterator end)
|
|
|
|
: m_Data(begin, end)
|
|
|
|
{ }
|
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline String& operator=(const String& rhs)
|
|
|
|
{
|
|
|
|
m_Data = rhs.m_Data;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String& operator=(const std::string& rhs)
|
|
|
|
{
|
|
|
|
m_Data = rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String& operator=(const char *rhs)
|
|
|
|
{
|
|
|
|
m_Data = rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const char& operator[](SizeType pos) const
|
|
|
|
{
|
|
|
|
return m_Data[pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline char& operator[](SizeType pos)
|
|
|
|
{
|
|
|
|
return m_Data[pos];
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline String& operator+=(const String& rhs)
|
|
|
|
{
|
|
|
|
m_Data += rhs.m_Data;
|
|
|
|
return *this;
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline String& operator+=(const char *rhs)
|
|
|
|
{
|
|
|
|
m_Data += rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-11-07 13:41:24 +01:00
|
|
|
String& operator+=(const Value& rhs);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline String& operator+=(char rhs)
|
|
|
|
{
|
|
|
|
m_Data += rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsEmpty(void) const
|
|
|
|
{
|
|
|
|
return m_Data.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator<(const String& rhs) const
|
|
|
|
{
|
|
|
|
return m_Data < rhs.m_Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline operator const std::string&(void) const
|
|
|
|
{
|
|
|
|
return m_Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const char *CStr(void) const
|
|
|
|
{
|
|
|
|
return m_Data.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Clear(void)
|
|
|
|
{
|
|
|
|
m_Data.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline SizeType GetLength(void) const
|
|
|
|
{
|
|
|
|
return m_Data.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string& GetData(void)
|
|
|
|
{
|
|
|
|
return m_Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const std::string& GetData(void) const
|
|
|
|
{
|
|
|
|
return m_Data;
|
|
|
|
}
|
|
|
|
|
2014-11-11 23:48:17 +01:00
|
|
|
inline SizeType Find(const String& str, SizeType pos = 0) const
|
2014-11-11 23:46:06 +01:00
|
|
|
{
|
|
|
|
return m_Data.find(str, pos);
|
|
|
|
}
|
|
|
|
|
2014-11-11 23:48:17 +01:00
|
|
|
inline SizeType RFind(const String& str, SizeType pos = NPos) const
|
2014-11-11 23:46:06 +01:00
|
|
|
{
|
|
|
|
return m_Data.rfind(str, pos);
|
|
|
|
}
|
|
|
|
|
2014-11-11 23:48:17 +01:00
|
|
|
inline SizeType FindFirstOf(const char *s, SizeType pos = 0) const
|
2014-11-11 23:46:06 +01:00
|
|
|
{
|
|
|
|
return m_Data.find_first_of(s, pos);
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2014-11-11 23:48:17 +01:00
|
|
|
inline SizeType FindFirstOf(char ch, SizeType pos = 0) const
|
2014-11-11 23:46:06 +01:00
|
|
|
{
|
|
|
|
return m_Data.find_first_of(ch, pos);
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2014-11-11 23:48:17 +01:00
|
|
|
inline SizeType FindFirstNotOf(const char *s, SizeType pos = 0) const
|
2014-11-11 23:46:06 +01:00
|
|
|
{
|
|
|
|
return m_Data.find_first_not_of(s, pos);
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2014-11-11 23:48:17 +01:00
|
|
|
inline SizeType FindFirstNotOf(char ch, SizeType pos = 0) const
|
2014-11-11 23:46:06 +01:00
|
|
|
{
|
|
|
|
return m_Data.find_first_not_of(ch, pos);
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2015-10-13 09:38:31 +02:00
|
|
|
inline SizeType FindLastOf(const char *s, SizeType pos = NPos) const
|
|
|
|
{
|
|
|
|
return m_Data.find_last_of(s, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline SizeType FindLastOf(char ch, SizeType pos = NPos) const
|
|
|
|
{
|
|
|
|
return m_Data.find_last_of(ch, pos);
|
|
|
|
}
|
|
|
|
|
2014-11-11 23:48:17 +01:00
|
|
|
inline String SubStr(SizeType first, SizeType len = NPos) const
|
2014-11-11 23:46:06 +01:00
|
|
|
{
|
|
|
|
return m_Data.substr(first, len);
|
|
|
|
}
|
2013-11-05 18:52:13 +01:00
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline void Replace(SizeType first, SizeType second, const String& str)
|
|
|
|
{
|
|
|
|
m_Data.replace(first, second, str);
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2015-08-27 15:50:14 +02:00
|
|
|
inline String Trim(void) const
|
|
|
|
{
|
|
|
|
String t = m_Data;
|
|
|
|
boost::algorithm::trim(t);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String ToLower(void) const
|
|
|
|
{
|
|
|
|
String t = m_Data;
|
|
|
|
boost::algorithm::to_lower(t);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String ToUpper(void) const
|
|
|
|
{
|
|
|
|
String t = m_Data;
|
|
|
|
boost::algorithm::to_upper(t);
|
|
|
|
return t;
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2015-09-23 09:06:15 +02:00
|
|
|
inline String Reverse(void) const
|
|
|
|
{
|
|
|
|
String t = m_Data;
|
|
|
|
std::reverse(t.m_Data.begin(), t.m_Data.end());
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2015-09-30 10:54:34 +02:00
|
|
|
inline void Append(int count, char ch)
|
|
|
|
{
|
|
|
|
m_Data.append(count, ch);
|
|
|
|
}
|
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline bool Contains(const String& str) const
|
|
|
|
{
|
|
|
|
return (m_Data.find(str) != std::string::npos);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void swap(String& str)
|
|
|
|
{
|
|
|
|
m_Data.swap(str.m_Data);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Iterator erase(Iterator first, Iterator last)
|
|
|
|
{
|
|
|
|
return m_Data.erase(first, last);
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-03-10 03:09:01 +01:00
|
|
|
template<typename InputIterator>
|
|
|
|
void insert(Iterator p, InputIterator first, InputIterator last)
|
|
|
|
{
|
|
|
|
m_Data.insert(p, first, last);
|
|
|
|
}
|
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline Iterator Begin(void)
|
|
|
|
{
|
|
|
|
return m_Data.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ConstIterator Begin(void) const
|
|
|
|
{
|
|
|
|
return m_Data.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Iterator End(void)
|
|
|
|
{
|
|
|
|
return m_Data.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ConstIterator End(void) const
|
|
|
|
{
|
|
|
|
return m_Data.end();
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2015-06-29 13:23:49 +02:00
|
|
|
inline ReverseIterator RBegin(void)
|
|
|
|
{
|
|
|
|
return m_Data.rbegin();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ConstReverseIterator RBegin(void) const
|
|
|
|
{
|
|
|
|
return m_Data.rbegin();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ReverseIterator REnd(void)
|
|
|
|
{
|
|
|
|
return m_Data.rend();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ConstReverseIterator REnd(void) const
|
|
|
|
{
|
|
|
|
return m_Data.rend();
|
|
|
|
}
|
|
|
|
|
2014-05-11 06:30:50 +02:00
|
|
|
static const SizeType NPos;
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2014-12-12 15:19:23 +01:00
|
|
|
static Object::Ptr GetPrototype(void);
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
private:
|
|
|
|
std::string m_Data;
|
|
|
|
};
|
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline std::ostream& operator<<(std::ostream& stream, const String& str)
|
|
|
|
{
|
|
|
|
stream << static_cast<std::string>(str);
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::istream& operator>>(std::istream& stream, String& str)
|
|
|
|
{
|
|
|
|
std::string tstr;
|
|
|
|
stream >> tstr;
|
|
|
|
str = tstr;
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String operator+(const String& lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs)+static_cast<std::string>(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String operator+(const String& lhs, const char *rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs)+rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String operator+(const char *lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return lhs + static_cast<std::string>(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const String& lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs) == static_cast<std::string>(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const String& lhs, const char *rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs) == rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const char *lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return lhs == static_cast<std::string>(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator<(const String& lhs, const char *rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs) < rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator<(const char *lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return lhs < static_cast<std::string>(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator>(const String& lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs) > static_cast<std::string>(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator>(const String& lhs, const char *rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs) > rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator>(const char *lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return lhs > static_cast<std::string>(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator<=(const String& lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs) <= static_cast<std::string>(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator<=(const String& lhs, const char *rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs) <= rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator<=(const char *lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return lhs <= static_cast<std::string>(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator>=(const String& lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs) >= static_cast<std::string>(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator>=(const String& lhs, const char *rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs) >= rhs;
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline bool operator>=(const char *lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return lhs >= static_cast<std::string>(rhs);
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline bool operator!=(const String& lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs) != static_cast<std::string>(rhs);
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline bool operator!=(const String& lhs, const char *rhs)
|
|
|
|
{
|
|
|
|
return static_cast<std::string>(lhs) != rhs;
|
|
|
|
}
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline bool operator!=(const char *lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return lhs != static_cast<std::string>(rhs);
|
|
|
|
}
|
2013-03-10 15:11:32 +01:00
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline String::Iterator range_begin(String& x)
|
|
|
|
{
|
|
|
|
return x.Begin();
|
|
|
|
}
|
2012-08-06 12:03:38 +02:00
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline String::ConstIterator range_begin(const String& x)
|
|
|
|
{
|
|
|
|
return x.Begin();
|
|
|
|
}
|
2013-03-10 15:11:32 +01:00
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline String::Iterator range_end(String& x)
|
|
|
|
{
|
|
|
|
return x.End();
|
|
|
|
}
|
2013-03-10 15:11:32 +01:00
|
|
|
|
2014-11-11 23:46:06 +01:00
|
|
|
inline String::ConstIterator range_end(const String& x)
|
|
|
|
{
|
|
|
|
return x.End();
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2012-10-31 12:06:21 +01:00
|
|
|
struct string_iless : std::binary_function<String, String, bool>
|
|
|
|
{
|
|
|
|
bool operator()(const String& s1, const String& s2) const
|
|
|
|
{
|
2013-11-27 12:23:27 +01:00
|
|
|
return strcasecmp(s1.CStr(), s2.CStr()) < 0;
|
2013-01-28 09:43:54 +01:00
|
|
|
|
2013-11-27 12:23:27 +01:00
|
|
|
/* The "right" way would be to do this - however the
|
|
|
|
* overhead is _massive_ due to the repeated non-inlined
|
|
|
|
* function calls:
|
2013-01-28 09:43:54 +01:00
|
|
|
|
2013-11-27 12:23:27 +01:00
|
|
|
return lexicographical_compare(s1.Begin(), s1.End(),
|
|
|
|
s2.Begin(), s2.End(), boost::algorithm::is_iless());
|
2013-01-28 09:43:54 +01:00
|
|
|
|
2013-11-27 12:23:27 +01:00
|
|
|
*/
|
2013-05-08 10:46:50 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace boost
|
|
|
|
{
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct range_mutable_iterator<icinga::String>
|
|
|
|
{
|
|
|
|
typedef icinga::String::Iterator type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct range_const_iterator<icinga::String>
|
|
|
|
{
|
|
|
|
typedef icinga::String::ConstIterator type;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* STRING_H */
|