2012-09-17 13:35:55 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2013-09-25 07:43:57 +02:00
|
|
|
* Copyright (C) 2012-2013 Icinga Development Team (http://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
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/i2-base.h"
|
2013-03-18 17:04:22 +01:00
|
|
|
#include <boost/range/iterator.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <ostream>
|
|
|
|
#include <istream>
|
2013-05-08 10:46:50 +02:00
|
|
|
#include <utility>
|
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;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String(void);
|
|
|
|
String(const char *data);
|
|
|
|
String(const std::string& data);
|
2013-03-10 03:09:01 +01:00
|
|
|
String(size_t n, char c);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
|
|
|
template<typename InputIterator>
|
|
|
|
String(InputIterator begin, InputIterator end)
|
|
|
|
: m_Data(begin, end)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
String(const String& other);
|
|
|
|
|
|
|
|
String& operator=(const String& rhs);
|
|
|
|
String& operator=(const std::string& rhs);
|
|
|
|
String& operator=(const char *rhs);
|
|
|
|
|
|
|
|
const char& operator[](size_t pos) const;
|
|
|
|
char& operator[](size_t pos);
|
|
|
|
|
|
|
|
String& operator+=(const String& rhs);
|
|
|
|
String& operator+=(const char *rhs);
|
2013-11-07 13:41:24 +01:00
|
|
|
String& operator+=(const Value& rhs);
|
2013-03-22 10:58:47 +01:00
|
|
|
String& operator+=(char rhs);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
|
|
|
bool IsEmpty(void) const;
|
|
|
|
|
|
|
|
bool operator<(const String& rhs) const;
|
|
|
|
|
|
|
|
operator const std::string&(void) const;
|
|
|
|
|
|
|
|
const char *CStr(void) const;
|
|
|
|
void Clear(void);
|
|
|
|
size_t GetLength(void) const;
|
|
|
|
|
2013-11-05 18:52:13 +01:00
|
|
|
std::string& GetData(void);
|
|
|
|
|
2013-04-17 14:26:04 +02:00
|
|
|
size_t Find(const String& str, size_t pos = 0) const;
|
2013-11-17 03:29:43 +01:00
|
|
|
size_t RFind(const String& str, size_t pos = NPos) const;
|
2012-08-02 09:38:08 +02:00
|
|
|
size_t FindFirstOf(const char *s, size_t pos = 0) const;
|
2013-03-22 10:58:47 +01:00
|
|
|
size_t FindFirstOf(char ch, size_t pos = 0) const;
|
2013-11-07 12:17:06 +01:00
|
|
|
size_t FindFirstNotOf(const char *s, size_t pos = 0) const;
|
|
|
|
size_t FindFirstNotOf(char ch, size_t pos = 0) const;
|
2013-03-10 03:09:01 +01:00
|
|
|
String SubStr(size_t first, size_t len = NPos) const;
|
2012-08-02 09:38:08 +02:00
|
|
|
void Replace(size_t first, size_t second, const String& str);
|
|
|
|
|
|
|
|
void Trim(void);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Iterator Begin(void);
|
|
|
|
ConstIterator Begin(void) const;
|
|
|
|
Iterator End(void);
|
|
|
|
ConstIterator End(void) const;
|
|
|
|
|
|
|
|
static const size_t NPos;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string m_Data;
|
|
|
|
};
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
I2_BASE_API std::ostream& operator<<(std::ostream& stream, const String& str);
|
|
|
|
I2_BASE_API std::istream& operator>>(std::istream& stream, String& str);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
|
|
|
I2_BASE_API String operator+(const String& lhs, const String& rhs);
|
|
|
|
I2_BASE_API String operator+(const String& lhs, const char *rhs);
|
|
|
|
I2_BASE_API String operator+(const char *lhs, const String& rhs);
|
|
|
|
|
|
|
|
I2_BASE_API bool operator==(const String& lhs, const String& rhs);
|
|
|
|
I2_BASE_API bool operator==(const String& lhs, const char *rhs);
|
|
|
|
I2_BASE_API bool operator==(const char *lhs, const String& rhs);
|
|
|
|
|
2012-08-03 13:19:55 +02:00
|
|
|
I2_BASE_API bool operator!=(const String& lhs, const String& rhs);
|
|
|
|
I2_BASE_API bool operator!=(const String& lhs, const char *rhs);
|
|
|
|
I2_BASE_API bool operator!=(const char *lhs, const String& rhs);
|
|
|
|
|
2012-08-06 12:03:38 +02:00
|
|
|
I2_BASE_API bool operator<(const String& lhs, const char *rhs);
|
|
|
|
I2_BASE_API bool operator<(const char *lhs, const String& rhs);
|
2013-03-10 15:11:32 +01:00
|
|
|
|
|
|
|
I2_BASE_API bool operator>(const String& lhs, const String& rhs);
|
2012-08-06 12:03:38 +02:00
|
|
|
I2_BASE_API bool operator>(const String& lhs, const char *rhs);
|
|
|
|
I2_BASE_API bool operator>(const char *lhs, const String& rhs);
|
|
|
|
|
2013-03-10 15:11:32 +01:00
|
|
|
I2_BASE_API bool operator<=(const String& lhs, const String& rhs);
|
|
|
|
I2_BASE_API bool operator<=(const String& lhs, const char *rhs);
|
|
|
|
I2_BASE_API bool operator<=(const char *lhs, const String& rhs);
|
|
|
|
|
|
|
|
I2_BASE_API bool operator>=(const String& lhs, const String& rhs);
|
|
|
|
I2_BASE_API bool operator>=(const String& lhs, const char *rhs);
|
|
|
|
I2_BASE_API bool operator>=(const char *lhs, const String& rhs);
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
I2_BASE_API String::Iterator range_begin(String& x);
|
|
|
|
I2_BASE_API String::ConstIterator range_begin(const String& x);
|
|
|
|
I2_BASE_API String::Iterator range_end(String& x);
|
|
|
|
I2_BASE_API String::ConstIterator range_end(const String& x);
|
|
|
|
|
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 */
|