2012-04-14 03:17:09 +02:00
|
|
|
#ifndef string_hh_INCLUDED
|
|
|
|
#define string_hh_INCLUDED
|
|
|
|
|
2012-04-14 03:23:20 +02:00
|
|
|
#include "memoryview.hh"
|
2012-08-23 23:56:35 +02:00
|
|
|
#include "units.hh"
|
2012-10-11 00:41:48 +02:00
|
|
|
#include "utf8.hh"
|
2012-04-14 03:17:09 +02:00
|
|
|
|
2013-04-09 20:05:40 +02:00
|
|
|
#include <string>
|
|
|
|
#include <boost/regex.hpp>
|
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-10-01 20:20:08 +02:00
|
|
|
typedef boost::regex Regex;
|
2012-04-14 03:23:20 +02:00
|
|
|
|
2013-03-29 14:21:55 +01:00
|
|
|
class String : public std::string
|
2012-04-14 03:23:20 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
String() {}
|
2013-03-29 14:21:55 +01:00
|
|
|
String(const char* content) : std::string(content) {}
|
|
|
|
String(std::string content) : std::string(std::move(content)) {}
|
|
|
|
explicit String(char content, CharCount count = 1) : std::string((size_t)(int)count, content) {}
|
2013-04-02 13:42:24 +02:00
|
|
|
explicit String(Codepoint cp, CharCount count = 1)
|
|
|
|
{
|
|
|
|
while (count-- > 0)
|
|
|
|
utf8::dump(back_inserter(*this), cp);
|
|
|
|
}
|
2012-04-14 03:23:20 +02:00
|
|
|
template<typename Iterator>
|
2013-03-29 14:21:55 +01:00
|
|
|
String(Iterator begin, Iterator end) : std::string(begin, end) {}
|
|
|
|
|
|
|
|
std::string& stdstr() { return *this; }
|
|
|
|
const std::string& stdstr() const { return *this; }
|
2012-04-14 03:23:20 +02:00
|
|
|
|
2013-03-29 14:21:55 +01:00
|
|
|
char operator[](ByteCount pos) const { return std::string::operator[]((int)pos); }
|
|
|
|
char& operator[](ByteCount pos) { return std::string::operator[]((int)pos); }
|
|
|
|
ByteCount length() const { return ByteCount{(int)std::string::length()}; }
|
2012-10-11 00:41:48 +02:00
|
|
|
CharCount char_length() const { return utf8::distance(begin(), end()); }
|
|
|
|
ByteCount byte_count_to(CharCount count) const { return utf8::advance(begin(), end(), (int)count) - begin(); }
|
|
|
|
CharCount char_count_to(ByteCount count) const { return utf8::distance(begin(), begin() + (int)count); }
|
2012-04-14 03:23:20 +02:00
|
|
|
|
2013-03-29 14:21:55 +01:00
|
|
|
String operator+(const String& other) const { return String{stdstr() + other.stdstr()}; }
|
|
|
|
String& operator+=(const String& other) { std::string::operator+=(other); return *this; }
|
|
|
|
String operator+(const char* other) const { return String{stdstr() + other}; }
|
|
|
|
String& operator+=(const char* other) { std::string::operator+=(other); return *this; }
|
|
|
|
String operator+(char other) const { return String{stdstr() + other}; }
|
|
|
|
String& operator+=(char other) { std::string::operator+=(other); return *this; }
|
|
|
|
String operator+(Codepoint cp) const { String res = *this; utf8::dump(back_inserter(res), cp); return res; }
|
|
|
|
String& operator+=(Codepoint cp) { utf8::dump(back_inserter(*this), cp); return *this; }
|
2012-04-14 03:23:20 +02:00
|
|
|
|
2013-03-29 14:21:55 +01:00
|
|
|
memoryview<char> data() const { return memoryview<char>(std::string::data(), size()); }
|
2012-04-14 03:23:20 +02:00
|
|
|
|
2013-03-29 14:21:55 +01:00
|
|
|
String substr(ByteCount pos, ByteCount length = -1) const { return String{std::string::substr((int)pos, (int)length)}; }
|
2012-10-11 00:41:48 +02:00
|
|
|
String substr(CharCount pos, CharCount length = INT_MAX) const
|
|
|
|
{
|
|
|
|
auto b = utf8::advance(begin(), end(), (int)pos);
|
|
|
|
auto e = utf8::advance(b, end(), (int)length);
|
|
|
|
return String(b,e);
|
|
|
|
}
|
2013-04-12 19:16:55 +02:00
|
|
|
String replace(const Regex& expression, const String& replacement) const;
|
2012-04-14 03:23:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
inline String operator+(const char* lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return String(lhs) + rhs;
|
|
|
|
}
|
|
|
|
|
2013-05-13 14:23:07 +02:00
|
|
|
inline String operator+(const std::string& lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return String(lhs) + rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String operator+(const String& lhs, const std::string& rhs)
|
|
|
|
{
|
|
|
|
return lhs + String(rhs);
|
|
|
|
}
|
|
|
|
|
2012-10-01 20:20:08 +02:00
|
|
|
inline String operator+(char lhs, const String& rhs)
|
2012-04-14 03:23:20 +02:00
|
|
|
{
|
|
|
|
return String(lhs) + rhs;
|
|
|
|
}
|
2012-04-14 03:17:09 +02:00
|
|
|
|
2013-03-29 14:21:55 +01:00
|
|
|
inline String operator+(Codepoint lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return String(lhs) + rhs;
|
|
|
|
}
|
|
|
|
|
2012-10-01 20:20:08 +02:00
|
|
|
std::vector<String> split(const String& str, char separator);
|
2012-05-29 07:19:50 +02:00
|
|
|
|
2012-10-16 15:11:22 +02:00
|
|
|
inline String operator"" _str(const char* str, size_t)
|
|
|
|
{
|
|
|
|
return String(str);
|
|
|
|
}
|
|
|
|
|
2013-01-17 18:47:53 +01:00
|
|
|
inline String codepoint_to_str(Codepoint cp)
|
|
|
|
{
|
|
|
|
std::string str;
|
2013-02-27 21:36:28 +01:00
|
|
|
utf8::dump(back_inserter(str), cp);
|
2013-01-17 18:47:53 +01:00
|
|
|
return String(str);
|
|
|
|
}
|
|
|
|
|
2013-03-29 19:31:06 +01:00
|
|
|
String option_to_string(const Regex& re);
|
|
|
|
void option_from_string(const String& str, Regex& re);
|
|
|
|
|
2013-05-17 14:09:42 +02:00
|
|
|
int str_to_int(const String& str);
|
2013-05-13 14:23:07 +02:00
|
|
|
|
|
|
|
using std::to_string;
|
|
|
|
|
|
|
|
template<typename RealType, typename ValueType>
|
|
|
|
std::string to_string(const StronglyTypedNumber<RealType, ValueType>& val)
|
|
|
|
{
|
|
|
|
return to_string((ValueType)val);
|
|
|
|
}
|
|
|
|
|
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<>
|
2013-03-29 14:21:55 +01:00
|
|
|
struct hash<Kakoune::String> : hash<std::string>
|
2012-05-30 14:20:21 +02:00
|
|
|
{
|
|
|
|
size_t operator()(const Kakoune::String& str) const
|
|
|
|
{
|
2013-03-29 14:21:55 +01:00
|
|
|
return hash<std::string>::operator()(str);
|
2012-05-30 14:20:21 +02:00
|
|
|
}
|
|
|
|
};
|
2012-04-14 03:23:20 +02:00
|
|
|
}
|
2012-04-14 03:17:09 +02:00
|
|
|
|
|
|
|
#endif // string_hh_INCLUDED
|
|
|
|
|