2012-04-14 05:17:09 +04:00
|
|
|
#ifndef string_hh_INCLUDED
|
|
|
|
#define string_hh_INCLUDED
|
|
|
|
|
2012-04-14 05:23:20 +04:00
|
|
|
#include "memoryview.hh"
|
2012-08-24 01:56:35 +04:00
|
|
|
#include "units.hh"
|
2012-10-11 02:41:48 +04:00
|
|
|
#include "utf8.hh"
|
2012-04-14 05:17:09 +04:00
|
|
|
|
2013-04-09 22:05:40 +04:00
|
|
|
#include <string>
|
|
|
|
#include <boost/regex.hpp>
|
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-01-09 23:50:01 +04:00
|
|
|
using Regex = boost::regex;
|
2012-04-14 05:23:20 +04:00
|
|
|
|
2014-05-11 15:44:51 +04:00
|
|
|
class StringView;
|
|
|
|
|
2013-03-29 17:21:55 +04:00
|
|
|
class String : public std::string
|
2012-04-14 05:23:20 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-11-14 04:12:15 +04:00
|
|
|
String() {}
|
|
|
|
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) {}
|
|
|
|
explicit String(Codepoint cp, CharCount count = 1)
|
|
|
|
{
|
|
|
|
while (count-- > 0)
|
|
|
|
utf8::dump(back_inserter(*this), cp);
|
|
|
|
}
|
|
|
|
template<typename Iterator>
|
|
|
|
String(Iterator begin, Iterator end) : std::string(begin, end) {}
|
|
|
|
|
|
|
|
std::string& stdstr() { return *this; }
|
|
|
|
const std::string& stdstr() const { return *this; }
|
|
|
|
|
2014-08-17 18:36:12 +04:00
|
|
|
[[gnu::always_inline]]
|
2013-11-14 04:12:15 +04:00
|
|
|
char operator[](ByteCount pos) const { return std::string::operator[]((int)pos); }
|
2014-08-17 18:36:12 +04:00
|
|
|
[[gnu::always_inline]]
|
2013-11-14 04:12:15 +04:00
|
|
|
char& operator[](ByteCount pos) { return std::string::operator[]((int)pos); }
|
2014-08-11 22:40:35 +04:00
|
|
|
Codepoint operator[](CharCount pos) { return utf8::codepoint(utf8::advance(begin(), end(), pos), end()); }
|
2014-08-17 18:36:12 +04:00
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2013-11-14 04:12:15 +04:00
|
|
|
ByteCount length() const { return ByteCount{(int)std::string::length()}; }
|
|
|
|
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); }
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
2014-05-11 15:44:51 +04:00
|
|
|
StringView substr(ByteCount pos, ByteCount length = INT_MAX) const;
|
|
|
|
StringView substr(CharCount pos, CharCount length = INT_MAX) const;
|
2012-04-14 05:23:20 +04:00
|
|
|
};
|
|
|
|
|
2014-04-18 16:45:33 +04:00
|
|
|
class StringView
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
constexpr StringView() : m_data{nullptr}, m_length{0} {}
|
|
|
|
constexpr StringView(const char* data, ByteCount length)
|
|
|
|
: m_data{data}, m_length{length} {}
|
2014-04-30 22:07:32 +04:00
|
|
|
StringView(const char* data) : m_data{data}, m_length{(int)strlen(data)} {}
|
2014-04-18 16:45:33 +04:00
|
|
|
constexpr StringView(const char* begin, const char* end) : m_data{begin}, m_length{(int)(end - begin)} {}
|
2014-08-03 13:02:17 +04:00
|
|
|
StringView(const std::string& str) : m_data{str.data()}, m_length{(int)str.length()} {}
|
2014-04-18 16:45:33 +04:00
|
|
|
|
|
|
|
bool operator==(StringView other) const;
|
|
|
|
bool operator!=(StringView other) const;
|
|
|
|
|
2014-08-17 18:36:12 +04:00
|
|
|
[[gnu::always_inline]]
|
2014-04-18 16:45:33 +04:00
|
|
|
const char* data() const { return m_data; }
|
|
|
|
|
|
|
|
using iterator = const char*;
|
|
|
|
using reverse_iterator = std::reverse_iterator<const char*>;
|
|
|
|
|
|
|
|
iterator begin() const { return m_data; }
|
|
|
|
iterator end() const { return m_data + (int)m_length; }
|
|
|
|
|
|
|
|
reverse_iterator rbegin() const { return reverse_iterator{m_data + (int)m_length}; }
|
|
|
|
reverse_iterator rend() const { return reverse_iterator{m_data}; }
|
|
|
|
|
|
|
|
char front() const { return *m_data; }
|
|
|
|
char back() const { return m_data[(int)m_length - 1]; }
|
|
|
|
|
2014-08-17 18:36:12 +04:00
|
|
|
[[gnu::always_inline]]
|
2014-04-18 16:45:33 +04:00
|
|
|
char operator[](ByteCount pos) const { return m_data[(int)pos]; }
|
2014-08-11 22:40:35 +04:00
|
|
|
Codepoint operator[](CharCount pos) { return utf8::codepoint(utf8::advance(begin(), end(), pos), end()); }
|
2014-04-18 16:45:33 +04:00
|
|
|
|
2014-08-17 18:36:12 +04:00
|
|
|
[[gnu::always_inline]]
|
2014-04-18 16:45:33 +04:00
|
|
|
ByteCount length() const { return m_length; }
|
|
|
|
CharCount char_length() const { return utf8::distance(begin(), end()); }
|
2014-08-17 18:36:12 +04:00
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2014-04-18 16:45:33 +04:00
|
|
|
bool empty() { return m_length == 0_byte; }
|
|
|
|
|
|
|
|
ByteCount byte_count_to(CharCount count) const;
|
|
|
|
CharCount char_count_to(ByteCount count) const;
|
|
|
|
|
|
|
|
StringView substr(ByteCount from, ByteCount length = INT_MAX) const;
|
|
|
|
StringView substr(CharCount from, CharCount length = INT_MAX) const;
|
|
|
|
|
|
|
|
String str() const { return String{begin(), end()}; }
|
|
|
|
|
|
|
|
operator String() const { return str(); } // to remove
|
|
|
|
|
2014-04-20 15:16:32 +04:00
|
|
|
struct ZeroTerminatedString
|
|
|
|
{
|
|
|
|
ZeroTerminatedString(const char* begin, const char* end)
|
|
|
|
{
|
|
|
|
if (*end == '\0')
|
|
|
|
unowned = begin;
|
|
|
|
else
|
|
|
|
owned = std::string(begin, end);
|
|
|
|
}
|
|
|
|
operator const char*() const { return unowned ? unowned : owned.c_str(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string owned;
|
|
|
|
const char* unowned = nullptr;
|
|
|
|
|
|
|
|
};
|
|
|
|
ZeroTerminatedString zstr() const { return ZeroTerminatedString{begin(), end()}; }
|
|
|
|
|
2014-04-18 16:45:33 +04:00
|
|
|
private:
|
|
|
|
const char* m_data;
|
|
|
|
ByteCount m_length;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool StringView::operator==(StringView other) const
|
|
|
|
{
|
|
|
|
return m_length == other.m_length and memcmp(m_data, other.m_data, (int)m_length) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool StringView::operator!=(StringView other) const
|
|
|
|
{
|
|
|
|
return !this->operator==(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const char* lhs, StringView rhs)
|
|
|
|
{
|
|
|
|
return StringView{lhs} == rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const char* lhs, StringView rhs)
|
|
|
|
{
|
|
|
|
return StringView{lhs} != rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const std::string& lhs, StringView rhs)
|
|
|
|
{
|
|
|
|
return StringView{lhs} == rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const std::string& lhs, StringView rhs)
|
|
|
|
{
|
|
|
|
return StringView{lhs} != rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ByteCount StringView::byte_count_to(CharCount count) const
|
|
|
|
{
|
|
|
|
return utf8::advance(begin(), end(), (int)count) - begin();
|
|
|
|
}
|
|
|
|
inline CharCount StringView::char_count_to(ByteCount count) const
|
|
|
|
{
|
|
|
|
return utf8::distance(begin(), begin() + (int)count);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline StringView StringView::substr(ByteCount from, ByteCount length) const
|
|
|
|
{
|
2014-04-20 15:03:57 +04:00
|
|
|
if (length < 0)
|
|
|
|
length = INT_MAX;
|
2014-04-18 16:45:33 +04:00
|
|
|
return StringView{ m_data + (int)from, std::min(m_length - from, length) };
|
|
|
|
}
|
|
|
|
|
|
|
|
inline StringView StringView::substr(CharCount from, CharCount length) const
|
|
|
|
{
|
2014-04-20 15:03:57 +04:00
|
|
|
if (length < 0)
|
|
|
|
length = INT_MAX;
|
2014-04-18 16:45:33 +04:00
|
|
|
auto beg = utf8::advance(begin(), end(), (int)from);
|
|
|
|
return StringView{ beg, utf8::advance(beg, end(), length) };
|
|
|
|
}
|
|
|
|
|
2014-05-11 15:44:51 +04:00
|
|
|
inline StringView String::substr(ByteCount pos, ByteCount length) const
|
|
|
|
{
|
|
|
|
return StringView{*this}.substr(pos, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline StringView String::substr(CharCount pos, CharCount length) const
|
|
|
|
{
|
|
|
|
return StringView{*this}.substr(pos, length);
|
|
|
|
}
|
|
|
|
|
2014-04-18 16:45:33 +04:00
|
|
|
inline const char* begin(StringView str)
|
|
|
|
{
|
|
|
|
return str.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const char* end(StringView str)
|
|
|
|
{
|
|
|
|
return str.end();
|
|
|
|
}
|
|
|
|
|
2012-04-14 05:23:20 +04:00
|
|
|
inline String operator+(const char* lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return String(lhs) + rhs;
|
|
|
|
}
|
|
|
|
|
2013-05-13 16:23:07 +04: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);
|
|
|
|
}
|
|
|
|
|
2014-04-18 16:45:33 +04:00
|
|
|
inline String& operator+=(String& lhs, StringView rhs)
|
|
|
|
{
|
|
|
|
lhs.append(rhs.data(), (size_t)(int)rhs.length());
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String operator+(const char* lhs, StringView rhs)
|
|
|
|
{
|
|
|
|
String res = lhs;
|
|
|
|
res += rhs;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String operator+(const String& lhs, StringView rhs)
|
|
|
|
{
|
|
|
|
String res = lhs;
|
|
|
|
res += rhs;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String operator+(StringView lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
String res{lhs.begin(), lhs.end()};
|
|
|
|
res.append(rhs);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String operator+(StringView lhs, const char* rhs)
|
|
|
|
{
|
|
|
|
String res{lhs.begin(), lhs.end()};
|
|
|
|
res.append(rhs);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-10-01 22:20:08 +04:00
|
|
|
inline String operator+(char lhs, const String& rhs)
|
2012-04-14 05:23:20 +04:00
|
|
|
{
|
|
|
|
return String(lhs) + rhs;
|
|
|
|
}
|
2012-04-14 05:17:09 +04:00
|
|
|
|
2013-03-29 17:21:55 +04:00
|
|
|
inline String operator+(Codepoint lhs, const String& rhs)
|
|
|
|
{
|
|
|
|
return String(lhs) + rhs;
|
|
|
|
}
|
|
|
|
|
2014-05-11 15:44:51 +04:00
|
|
|
inline String operator+(StringView lhs, StringView rhs)
|
|
|
|
{
|
|
|
|
String res{lhs.begin(), lhs.end()};
|
|
|
|
res += rhs;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-08-03 13:02:17 +04:00
|
|
|
std::vector<String> split(StringView str, char separator, char escape = 0);
|
|
|
|
String escape(StringView str, char character, char escape);
|
|
|
|
String escape(StringView str, StringView characters, char escape);
|
2012-05-29 09:19:50 +04:00
|
|
|
|
2012-10-16 17:11:22 +04:00
|
|
|
inline String operator"" _str(const char* str, size_t)
|
|
|
|
{
|
|
|
|
return String(str);
|
|
|
|
}
|
|
|
|
|
2013-01-17 21:47:53 +04:00
|
|
|
inline String codepoint_to_str(Codepoint cp)
|
|
|
|
{
|
|
|
|
std::string str;
|
2013-02-28 00:36:28 +04:00
|
|
|
utf8::dump(back_inserter(str), cp);
|
2013-01-17 21:47:53 +04:00
|
|
|
return String(str);
|
|
|
|
}
|
|
|
|
|
2013-03-29 22:31:06 +04:00
|
|
|
String option_to_string(const Regex& re);
|
2014-08-03 13:02:17 +04:00
|
|
|
void option_from_string(StringView str, Regex& re);
|
2013-03-29 22:31:06 +04:00
|
|
|
|
2014-08-03 13:02:17 +04:00
|
|
|
int str_to_int(StringView str);
|
2013-05-13 16:23:07 +04:00
|
|
|
|
2013-06-19 00:11:44 +04:00
|
|
|
String to_string(int val);
|
2013-05-13 16:23:07 +04:00
|
|
|
|
|
|
|
template<typename RealType, typename ValueType>
|
2013-06-19 00:11:44 +04:00
|
|
|
String to_string(const StronglyTypedNumber<RealType, ValueType>& val)
|
2013-05-13 16:23:07 +04:00
|
|
|
{
|
|
|
|
return to_string((ValueType)val);
|
|
|
|
}
|
|
|
|
|
2014-04-18 16:45:33 +04:00
|
|
|
bool prefix_match(StringView str, StringView prefix);
|
|
|
|
bool subsequence_match(StringView str, StringView subseq);
|
2013-09-23 23:16:25 +04:00
|
|
|
|
2014-04-28 22:49:00 +04:00
|
|
|
String expand_tabs(StringView line, CharCount tabstop, CharCount col = 0);
|
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
}
|
|
|
|
|
2012-04-14 05:23:20 +04:00
|
|
|
namespace std
|
|
|
|
{
|
2012-05-30 16:20:21 +04:00
|
|
|
template<>
|
2013-03-29 17:21:55 +04:00
|
|
|
struct hash<Kakoune::String> : hash<std::string>
|
2012-05-30 16:20:21 +04:00
|
|
|
{
|
|
|
|
size_t operator()(const Kakoune::String& str) const
|
|
|
|
{
|
2013-03-29 17:21:55 +04:00
|
|
|
return hash<std::string>::operator()(str);
|
2012-05-30 16:20:21 +04:00
|
|
|
}
|
|
|
|
};
|
2012-04-14 05:23:20 +04:00
|
|
|
}
|
2012-04-14 05:17:09 +04:00
|
|
|
|
|
|
|
#endif // string_hh_INCLUDED
|
|
|
|
|