2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-09-17 07:35:55 -04:00
|
|
|
|
2012-08-02 03:38:08 -04:00
|
|
|
#ifndef STRING_H
|
|
|
|
|
#define STRING_H
|
|
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/i2-base.hpp"
|
2014-12-12 09:19:23 -05:00
|
|
|
#include "base/object.hpp"
|
2023-01-04 11:02:19 -05:00
|
|
|
#include <boost/beast/core.hpp>
|
2014-08-25 09:12:39 -04:00
|
|
|
#include <boost/range/iterator.hpp>
|
2020-12-22 08:36:48 -05:00
|
|
|
#include <boost/utility/string_view.hpp>
|
2022-10-18 04:26:13 -04:00
|
|
|
#include <functional>
|
2014-05-11 11:14:35 -04:00
|
|
|
#include <string>
|
2015-03-28 19:03:47 -04:00
|
|
|
#include <iosfwd>
|
2013-03-15 13:21:29 -04:00
|
|
|
|
2012-08-02 03:38:08 -04:00
|
|
|
namespace icinga {
|
|
|
|
|
|
2013-11-07 07:41:24 -05:00
|
|
|
class Value;
|
|
|
|
|
|
2012-08-02 03:38:08 -04:00
|
|
|
/**
|
|
|
|
|
* String class.
|
|
|
|
|
*
|
2012-09-17 07:35:55 -04:00
|
|
|
* Rationale for having this: The std::string class has an ambiguous assignment
|
2012-08-02 03:38:08 -04:00
|
|
|
* operator when used in conjunction with the Value class.
|
|
|
|
|
*/
|
2017-12-31 01:22:16 -05:00
|
|
|
class String
|
2012-08-02 03:38:08 -04:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef std::string::iterator Iterator;
|
|
|
|
|
typedef std::string::const_iterator ConstIterator;
|
|
|
|
|
|
2013-03-09 21:09:01 -05:00
|
|
|
typedef std::string::iterator iterator;
|
|
|
|
|
typedef std::string::const_iterator const_iterator;
|
|
|
|
|
|
2015-06-29 07:23:49 -04: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 00:30:50 -04:00
|
|
|
typedef std::string::size_type SizeType;
|
|
|
|
|
|
2018-01-04 03:43:49 -05:00
|
|
|
String() = default;
|
2018-01-03 00:01:02 -05:00
|
|
|
String(const char *data);
|
2018-01-04 02:54:18 -05:00
|
|
|
String(std::string data);
|
2018-01-03 00:01:02 -05:00
|
|
|
String(String::SizeType n, char c);
|
|
|
|
|
String(const String& other);
|
2025-02-27 11:20:07 -05:00
|
|
|
String(String&& other) noexcept;
|
2016-08-26 02:58:49 -04:00
|
|
|
|
2016-08-31 07:38:55 -04:00
|
|
|
#ifndef _MSC_VER
|
2016-08-31 06:48:49 -04:00
|
|
|
String(Value&& other);
|
2016-08-31 07:38:55 -04:00
|
|
|
#endif /* _MSC_VER */
|
2016-08-31 06:48:49 -04:00
|
|
|
|
2012-08-02 03:38:08 -04:00
|
|
|
template<typename InputIterator>
|
|
|
|
|
String(InputIterator begin, InputIterator end)
|
|
|
|
|
: m_Data(begin, end)
|
|
|
|
|
{ }
|
|
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
String& operator=(const String& rhs);
|
2025-02-27 11:20:07 -05:00
|
|
|
String& operator=(String&& rhs) noexcept;
|
2016-08-31 06:48:49 -04:00
|
|
|
String& operator=(Value&& rhs);
|
2018-01-03 00:01:02 -05:00
|
|
|
String& operator=(const std::string& rhs);
|
|
|
|
|
String& operator=(const char *rhs);
|
2016-08-31 06:48:49 -04:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
const char& operator[](SizeType pos) const;
|
|
|
|
|
char& operator[](SizeType pos);
|
2012-08-02 03:38:08 -04:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
String& operator+=(const String& rhs);
|
|
|
|
|
String& operator+=(const char *rhs);
|
2013-11-07 07:41:24 -05:00
|
|
|
String& operator+=(const Value& rhs);
|
2018-01-03 00:01:02 -05:00
|
|
|
String& operator+=(char rhs);
|
2012-08-02 03:38:08 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
bool IsEmpty() const;
|
2014-11-11 17:46:06 -05:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
bool operator<(const String& rhs) const;
|
2014-11-11 17:46:06 -05:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
operator const std::string&() const;
|
2023-01-04 11:02:19 -05:00
|
|
|
operator boost::beast::string_view() const;
|
2014-11-11 17:46:06 -05:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
const char *CStr() const;
|
2014-11-11 17:46:06 -05:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void Clear();
|
2014-11-11 17:46:06 -05:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
SizeType GetLength() const;
|
2014-11-11 17:46:06 -05:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
std::string& GetData();
|
|
|
|
|
const std::string& GetData() const;
|
2012-08-02 03:38:08 -04:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
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;
|
2012-08-02 03:38:08 -04:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
String SubStr(SizeType first, SizeType len = NPos) const;
|
2012-08-02 03:38:08 -04:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
std::vector<String> Split(const char *separators) const;
|
2012-08-02 03:38:08 -04:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
void Replace(SizeType first, SizeType second, const String& str);
|
2015-10-13 03:38:31 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
String Trim() const;
|
2015-10-13 03:38:31 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
String ToLower() const;
|
2013-11-05 12:52:13 -05:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
String ToUpper() const;
|
2017-03-27 08:26:56 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
String Reverse() const;
|
2012-08-02 03:38:08 -04:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
void Append(int count, char ch);
|
2015-08-27 09:50:14 -04:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
bool Contains(const String& str) const;
|
2015-08-27 09:50:14 -04:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
void swap(String& str);
|
2012-08-02 03:38:08 -04:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
Iterator erase(Iterator first, Iterator last);
|
2012-08-02 03:38:08 -04:00
|
|
|
|
2013-03-09 21:09:01 -05:00
|
|
|
template<typename InputIterator>
|
|
|
|
|
void insert(Iterator p, InputIterator first, InputIterator last)
|
|
|
|
|
{
|
|
|
|
|
m_Data.insert(p, first, last);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
Iterator Begin();
|
|
|
|
|
ConstIterator Begin() const;
|
|
|
|
|
Iterator End();
|
|
|
|
|
ConstIterator End() const;
|
|
|
|
|
ReverseIterator RBegin();
|
|
|
|
|
ConstReverseIterator RBegin() const;
|
|
|
|
|
ReverseIterator REnd();
|
|
|
|
|
ConstReverseIterator REnd() const;
|
2015-06-29 07:23:49 -04:00
|
|
|
|
2014-05-11 00:30:50 -04:00
|
|
|
static const SizeType NPos;
|
2012-08-02 03:38:08 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
static Object::Ptr GetPrototype();
|
2014-12-12 09:19:23 -05:00
|
|
|
|
2012-08-02 03:38:08 -04:00
|
|
|
private:
|
|
|
|
|
std::string m_Data;
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
std::ostream& operator<<(std::ostream& stream, const String& str);
|
|
|
|
|
std::istream& operator>>(std::istream& stream, String& str);
|
2014-11-11 17:46:06 -05:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
String operator+(const String& lhs, const String& rhs);
|
|
|
|
|
String operator+(const String& lhs, const char *rhs);
|
|
|
|
|
String operator+(const char *lhs, const String& rhs);
|
2014-11-11 17:46:06 -05:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
bool operator==(const String& lhs, const String& rhs);
|
|
|
|
|
bool operator==(const String& lhs, const char *rhs);
|
|
|
|
|
bool operator==(const char *lhs, const String& rhs);
|
2014-11-11 17:46:06 -05:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
bool operator<(const String& lhs, const char *rhs);
|
|
|
|
|
bool operator<(const char *lhs, const String& rhs);
|
2014-11-11 17:46:06 -05:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
bool operator>(const String& lhs, const String& rhs);
|
|
|
|
|
bool operator>(const String& lhs, const char *rhs);
|
|
|
|
|
bool operator>(const char *lhs, const String& rhs);
|
2014-11-11 17:46:06 -05:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
bool operator<=(const String& lhs, const String& rhs);
|
|
|
|
|
bool operator<=(const String& lhs, const char *rhs);
|
|
|
|
|
bool operator<=(const char *lhs, const String& rhs);
|
2014-11-11 17:46:06 -05:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
bool operator>=(const String& lhs, const String& rhs);
|
|
|
|
|
bool operator>=(const String& lhs, const char *rhs);
|
|
|
|
|
bool operator>=(const char *lhs, const String& rhs);
|
2012-08-02 03:38:08 -04:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
bool operator!=(const String& lhs, const String& rhs);
|
|
|
|
|
bool operator!=(const String& lhs, const char *rhs);
|
|
|
|
|
bool operator!=(const char *lhs, const String& rhs);
|
2012-08-02 03:38:08 -04:00
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
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-02 03:38:08 -04:00
|
|
|
|
2014-11-11 17:46:06 -05:00
|
|
|
}
|
2012-08-03 07:19:55 -04:00
|
|
|
|
2022-10-18 04:26:13 -04:00
|
|
|
template<>
|
|
|
|
|
struct std::hash<icinga::String>
|
|
|
|
|
{
|
|
|
|
|
std::size_t operator()(const icinga::String& s) const noexcept;
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-03 00:01:02 -05:00
|
|
|
extern template class std::vector<icinga::String>;
|
2012-08-02 03:38:08 -04: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 */
|