icinga2/lib/base/string.hpp

216 lines
6.4 KiB
C++
Raw Normal View History

2012-09-17 13:35:55 +02:00
/******************************************************************************
* Icinga 2 *
2018-10-18 09:27:04 +02:00
* Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
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. *
******************************************************************************/
#ifndef STRING_H
#define STRING_H
2014-05-25 16:23:35 +02:00
#include "base/i2-base.hpp"
#include "base/object.hpp"
2014-08-25 15:12:39 +02:00
#include <boost/range/iterator.hpp>
#include <string>
2015-03-29 00:03:47 +01:00
#include <iosfwd>
2013-03-15 18:21:29 +01:00
namespace icinga {
class Value;
/**
* String class.
*
2012-09-17 13:35:55 +02:00
* Rationale for having this: The std::string class has an ambiguous assignment
* operator when used in conjunction with the Value class.
*/
2017-12-31 07:22:16 +01:00
class 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;
typedef std::string::size_type SizeType;
String() = default;
String(const char *data);
String(std::string data);
String(String::SizeType n, char c);
String(const String& other);
String(String&& other);
#ifndef _MSC_VER
String(Value&& other);
#endif /* _MSC_VER */
template<typename InputIterator>
String(InputIterator begin, InputIterator end)
: m_Data(begin, end)
{ }
String& operator=(const String& rhs);
String& operator=(String&& rhs);
String& operator=(Value&& rhs);
String& operator=(const std::string& rhs);
String& operator=(const char *rhs);
const char& operator[](SizeType pos) const;
char& operator[](SizeType pos);
String& operator+=(const String& rhs);
String& operator+=(const char *rhs);
String& operator+=(const Value& rhs);
String& operator+=(char rhs);
bool IsEmpty() const;
bool operator<(const String& rhs) const;
operator const std::string&() const;
const char *CStr() const;
void Clear();
SizeType GetLength() const;
std::string& GetData();
const std::string& GetData() const;
SizeType Find(const String& str, SizeType pos = 0) const;
SizeType RFind(const String& str, SizeType pos = NPos) const;
SizeType FindFirstOf(const char *s, SizeType pos = 0) const;
SizeType FindFirstOf(char ch, SizeType pos = 0) const;
SizeType FindFirstNotOf(const char *s, SizeType pos = 0) const;
SizeType FindFirstNotOf(char ch, SizeType pos = 0) const;
SizeType FindLastOf(const char *s, SizeType pos = NPos) const;
SizeType FindLastOf(char ch, SizeType pos = NPos) const;
String SubStr(SizeType first, SizeType len = NPos) const;
std::vector<String> Split(const char *separators) const;
void Replace(SizeType first, SizeType second, const String& str);
String Trim() const;
String ToLower() const;
2013-11-05 18:52:13 +01:00
String ToUpper() const;
String Reverse() const;
void Append(int count, char ch);
bool Contains(const String& str) const;
void swap(String& str);
Iterator erase(Iterator first, Iterator last);
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);
}
Iterator Begin();
ConstIterator Begin() const;
Iterator End();
ConstIterator End() const;
ReverseIterator RBegin();
ConstReverseIterator RBegin() const;
ReverseIterator REnd();
ConstReverseIterator REnd() const;
2015-06-29 13:23:49 +02:00
static const SizeType NPos;
static Object::Ptr GetPrototype();
private:
std::string m_Data;
};
std::ostream& operator<<(std::ostream& stream, const String& str);
std::istream& operator>>(std::istream& stream, String& str);
String operator+(const String& lhs, const String& rhs);
String operator+(const String& lhs, const char *rhs);
String operator+(const char *lhs, const String& rhs);
bool operator==(const String& lhs, const String& rhs);
bool operator==(const String& lhs, const char *rhs);
bool operator==(const char *lhs, const String& rhs);
bool operator<(const String& lhs, const char *rhs);
bool operator<(const char *lhs, const String& rhs);
bool operator>(const String& lhs, const String& rhs);
bool operator>(const String& lhs, const char *rhs);
bool operator>(const char *lhs, const String& rhs);
bool operator<=(const String& lhs, const String& rhs);
bool operator<=(const String& lhs, const char *rhs);
bool operator<=(const char *lhs, const String& rhs);
bool operator>=(const String& lhs, const String& rhs);
bool operator>=(const String& lhs, const char *rhs);
bool operator>=(const char *lhs, const String& rhs);
bool operator!=(const String& lhs, const String& rhs);
bool operator!=(const String& lhs, const char *rhs);
bool operator!=(const char *lhs, const String& rhs);
String::Iterator begin(String& x);
String::ConstIterator begin(const String& x);
String::Iterator end(String& x);
String::ConstIterator end(const String& x);
String::Iterator range_begin(String& x);
String::ConstIterator range_begin(const String& x);
String::Iterator range_end(String& x);
String::ConstIterator range_end(const String& x);
}
2012-08-03 13:19:55 +02:00
extern template class std::vector<icinga::String>;
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 */