2012-04-14 03:17:09 +02:00
|
|
|
#ifndef string_hh_INCLUDED
|
|
|
|
#define string_hh_INCLUDED
|
|
|
|
|
|
|
|
#include <string>
|
2012-04-14 03:23:20 +02:00
|
|
|
#include <iosfwd>
|
2012-08-29 21:49:36 +02:00
|
|
|
#include <boost/regex.hpp>
|
2012-04-14 03:23:20 +02:00
|
|
|
|
|
|
|
#include "memoryview.hh"
|
2012-08-23 23:56:35 +02:00
|
|
|
#include "units.hh"
|
2012-04-14 03:17:09 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-04-14 03:23:20 +02:00
|
|
|
typedef wchar_t Character;
|
2012-08-29 21:49:36 +02:00
|
|
|
typedef boost::basic_regex<Character> Regex;
|
2012-04-14 03:23:20 +02:00
|
|
|
|
|
|
|
class String
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
String() {}
|
|
|
|
String(const char* content) : m_content(content) {}
|
2012-06-27 14:26:29 +02:00
|
|
|
String(std::string content) : m_content(std::move(content)) {}
|
|
|
|
String(const String& string) = default;
|
|
|
|
String(String&& string) = default;
|
2012-04-14 03:23:20 +02:00
|
|
|
explicit String(Character content) : m_content(std::string() + (char)content) {}
|
|
|
|
template<typename Iterator>
|
|
|
|
String(Iterator begin, Iterator end) : m_content(begin, end) {}
|
|
|
|
|
2012-08-23 23:56:35 +02:00
|
|
|
Character operator[](CharCount pos) const { return static_cast<Character>(m_content[(int)pos]); }
|
|
|
|
CharCount length() const { return m_content.length(); }
|
2012-04-14 03:23:20 +02:00
|
|
|
bool empty() const { return m_content.empty(); }
|
|
|
|
|
|
|
|
bool operator== (const String& other) const { return m_content == other.m_content; }
|
|
|
|
bool operator!= (const String& other) const { return m_content != other.m_content; }
|
|
|
|
bool operator< (const String& other) const { return m_content < other.m_content; }
|
|
|
|
|
|
|
|
String& operator= (const String& other) { m_content = other.m_content; return *this; }
|
2012-06-27 14:26:29 +02:00
|
|
|
String& operator= (String&& other) { m_content = std::move(other.m_content); return *this; }
|
2012-04-14 03:23:20 +02:00
|
|
|
|
|
|
|
String operator+ (const String& other) const { return String(m_content + other.m_content); }
|
|
|
|
String& operator+= (const String& other) { m_content += other.m_content; return *this; }
|
|
|
|
|
|
|
|
String operator+ (Character character) const { return String(m_content + (char)character); }
|
|
|
|
String& operator+= (Character character) { m_content += (char)character; return *this; }
|
|
|
|
|
|
|
|
memoryview<char> data() const { return memoryview<char>(m_content.data(), m_content.size()); }
|
|
|
|
const char* c_str() const { return m_content.c_str(); }
|
|
|
|
|
2012-08-23 23:56:35 +02:00
|
|
|
String substr(CharCount pos, CharCount length = -1) const { return String(m_content.substr((int)pos, (int)length)); }
|
2012-08-29 21:50:48 +02:00
|
|
|
String replace(const String& expression, const String& replacement) const;
|
2012-04-14 03:23:20 +02:00
|
|
|
|
|
|
|
class iterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef Character value_type;
|
|
|
|
typedef const value_type* pointer;
|
|
|
|
typedef const value_type& reference;
|
|
|
|
typedef size_t difference_type;
|
|
|
|
typedef std::random_access_iterator_tag iterator_category;
|
|
|
|
|
|
|
|
iterator() {}
|
|
|
|
iterator(const std::string::const_iterator& it) : m_iterator(it) {}
|
|
|
|
|
|
|
|
Character operator*()
|
|
|
|
{ return static_cast<Character>(*m_iterator); }
|
|
|
|
|
|
|
|
iterator& operator++ () { ++m_iterator; return *this; }
|
|
|
|
iterator& operator-- () { --m_iterator; return *this; }
|
|
|
|
|
|
|
|
iterator operator+ (size_t size) { return iterator(m_iterator + size); }
|
|
|
|
iterator operator- (size_t size) { return iterator(m_iterator - size); }
|
|
|
|
|
|
|
|
iterator& operator+= (size_t size) { m_iterator += size; return *this; }
|
|
|
|
iterator& operator-= (size_t size) { m_iterator -= size; return *this; }
|
|
|
|
|
|
|
|
size_t operator- (const iterator& other) const { return m_iterator - other.m_iterator; }
|
|
|
|
|
|
|
|
bool operator== (const iterator& other) const { return m_iterator == other.m_iterator; }
|
|
|
|
bool operator!= (const iterator& other) const { return m_iterator != other.m_iterator; }
|
|
|
|
bool operator< (const iterator& other) const { return m_iterator < other.m_iterator; }
|
|
|
|
bool operator<= (const iterator& other) const { return m_iterator <= other.m_iterator; }
|
|
|
|
bool operator> (const iterator& other) const { return m_iterator > other.m_iterator; }
|
|
|
|
bool operator>= (const iterator& other) const { return m_iterator >= other.m_iterator; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string::const_iterator m_iterator;
|
|
|
|
};
|
|
|
|
|
|
|
|
iterator begin() const { return iterator(m_content.begin()); }
|
|
|
|
iterator end() const { return iterator(m_content.end()); }
|
|
|
|
|
|
|
|
Character front() const { return Character(m_content.front()); }
|
|
|
|
Character back() const { return Character(m_content.back()); }
|
|
|
|
|
|
|
|
size_t hash() const { return std::hash<std::string>()(m_content); }
|
|
|
|
|
|
|
|
inline friend std::ostream& operator<<(std::ostream& os, const String& str)
|
|
|
|
{
|
|
|
|
return os << str.m_content;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum { npos = -1 };
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string m_content;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline String operator+(const char* lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return String(lhs) + rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String operator+(Character lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return String(lhs) + rhs;
|
|
|
|
}
|
2012-04-14 03:17:09 +02:00
|
|
|
|
2012-05-29 07:19:50 +02:00
|
|
|
String int_to_str(int value);
|
2012-06-27 14:26:29 +02:00
|
|
|
int str_to_int(const String& str);
|
2012-05-29 07:19:50 +02:00
|
|
|
std::vector<String> split(const String& str, Character separator);
|
|
|
|
|
2012-09-30 16:23:18 +02:00
|
|
|
inline bool is_word(Character c)
|
|
|
|
{
|
|
|
|
return (c >= '0' and c <= '9') or
|
|
|
|
(c >= 'a' and c <= 'z') or
|
|
|
|
(c >= 'A' and c <= 'Z') or
|
|
|
|
c == '_';
|
|
|
|
}
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
}
|
|
|
|
|
2012-04-14 03:23:20 +02:00
|
|
|
namespace std
|
|
|
|
{
|
2012-05-30 14:20:21 +02:00
|
|
|
template<>
|
|
|
|
struct hash<Kakoune::String>
|
|
|
|
{
|
|
|
|
size_t operator()(const Kakoune::String& str) const
|
|
|
|
{
|
|
|
|
return str.hash();
|
|
|
|
}
|
|
|
|
};
|
2012-04-14 03:23:20 +02:00
|
|
|
}
|
2012-04-14 03:17:09 +02:00
|
|
|
|
|
|
|
#endif // string_hh_INCLUDED
|
|
|
|
|