2012-04-14 03:17:09 +02:00
|
|
|
#ifndef string_hh_INCLUDED
|
|
|
|
#define string_hh_INCLUDED
|
|
|
|
|
2012-08-23 23:56:35 +02:00
|
|
|
#include "units.hh"
|
2012-10-11 00:41:48 +02:00
|
|
|
#include "utf8.hh"
|
2014-12-16 19:57:19 +01:00
|
|
|
#include "hash.hh"
|
2015-01-09 14:57:21 +01:00
|
|
|
#include "vector.hh"
|
2012-04-14 03:17:09 +02:00
|
|
|
|
2013-04-09 20:05:40 +02:00
|
|
|
#include <string>
|
2014-10-13 14:12:33 +02:00
|
|
|
#include <climits>
|
2013-04-09 20:05:40 +02:00
|
|
|
|
2012-04-14 03:17:09 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-05-11 13:44:51 +02:00
|
|
|
class StringView;
|
|
|
|
|
2015-01-09 14:57:21 +01:00
|
|
|
using StringBase = std::basic_string<char, std::char_traits<char>,
|
|
|
|
Allocator<char, MemoryDomain::String>>;
|
|
|
|
|
2015-02-11 00:09:30 +01:00
|
|
|
constexpr ByteCount strlen(const char* s)
|
|
|
|
{
|
|
|
|
return *s == 0 ? 0 : strlen(s+1) + 1;
|
|
|
|
}
|
|
|
|
|
2015-01-09 14:57:21 +01:00
|
|
|
class String : public StringBase
|
2012-04-14 03:23:20 +02:00
|
|
|
{
|
|
|
|
public:
|
2013-11-14 01:12:15 +01:00
|
|
|
String() {}
|
2015-01-09 14:57:21 +01:00
|
|
|
String(const char* content) : StringBase(content) {}
|
|
|
|
String(StringBase content) : StringBase(std::move(content)) {}
|
|
|
|
template<typename Char, typename Traits, typename Alloc>
|
|
|
|
String(const std::basic_string<Char, Traits, Alloc>& content) : StringBase(content.begin(), content.end()) {}
|
|
|
|
explicit String(char content, CharCount count = 1) : StringBase((size_t)(int)count, content) {}
|
2013-11-14 01:12:15 +01:00
|
|
|
explicit String(Codepoint cp, CharCount count = 1)
|
|
|
|
{
|
|
|
|
while (count-- > 0)
|
|
|
|
utf8::dump(back_inserter(*this), cp);
|
|
|
|
}
|
|
|
|
template<typename Iterator>
|
2015-01-09 14:57:21 +01:00
|
|
|
String(Iterator begin, Iterator end) : StringBase(begin, end) {}
|
2013-11-14 01:12:15 +01:00
|
|
|
|
2015-01-09 14:57:21 +01:00
|
|
|
StringBase& stdstr() { return *this; }
|
|
|
|
const StringBase& stdstr() const { return *this; }
|
2013-11-14 01:12:15 +01:00
|
|
|
|
2014-08-17 16:36:12 +02:00
|
|
|
[[gnu::always_inline]]
|
2015-01-09 14:57:21 +01:00
|
|
|
char operator[](ByteCount pos) const { return StringBase::operator[]((int)pos); }
|
2014-08-17 16:36:12 +02:00
|
|
|
[[gnu::always_inline]]
|
2015-01-09 14:57:21 +01:00
|
|
|
char& operator[](ByteCount pos) { return StringBase::operator[]((int)pos); }
|
2014-08-11 20:40:35 +02:00
|
|
|
Codepoint operator[](CharCount pos) { return utf8::codepoint(utf8::advance(begin(), end(), pos), end()); }
|
2014-08-17 16:36:12 +02:00
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2015-01-09 14:57:21 +01:00
|
|
|
ByteCount length() const { return ByteCount{(int)StringBase::length()}; }
|
2013-11-14 01:12:15 +01: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); }
|
|
|
|
|
2015-01-09 14:57:21 +01:00
|
|
|
String& operator+=(const String& other) { StringBase::operator+=(other); return *this; }
|
|
|
|
String& operator+=(const char* other) { StringBase::operator+=(other); return *this; }
|
|
|
|
String& operator+=(char other) { StringBase::operator+=(other); return *this; }
|
2013-11-14 01:12:15 +01:00
|
|
|
String& operator+=(Codepoint cp) { utf8::dump(back_inserter(*this), cp); return *this; }
|
|
|
|
|
2014-05-11 13:44:51 +02:00
|
|
|
StringView substr(ByteCount pos, ByteCount length = INT_MAX) const;
|
|
|
|
StringView substr(CharCount pos, CharCount length = INT_MAX) const;
|
2012-04-14 03:23:20 +02:00
|
|
|
};
|
|
|
|
|
2014-04-18 14:45:33 +02: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} {}
|
2015-02-11 00:09:30 +01:00
|
|
|
constexpr StringView(const char* data) : m_data{data}, m_length{strlen(data)} {}
|
2014-04-18 14:45:33 +02:00
|
|
|
constexpr StringView(const char* begin, const char* end) : m_data{begin}, m_length{(int)(end - begin)} {}
|
2015-01-09 14:57:21 +01:00
|
|
|
template<typename Char, typename Traits, typename Alloc>
|
|
|
|
StringView(const std::basic_string<Char, Traits, Alloc>& str) : m_data{str.data()}, m_length{(int)str.length()} {}
|
2014-11-04 14:31:15 +01:00
|
|
|
StringView(const char& c) : m_data(&c), m_length(1) {}
|
2014-04-18 14:45:33 +02:00
|
|
|
|
2014-12-08 14:46:07 +01:00
|
|
|
friend bool operator==(StringView lhs, StringView rhs);
|
2014-04-18 14:45:33 +02:00
|
|
|
|
2014-08-17 16:36:12 +02:00
|
|
|
[[gnu::always_inline]]
|
2014-04-18 14:45:33 +02:00
|
|
|
const char* data() const { return m_data; }
|
|
|
|
|
|
|
|
using iterator = const char*;
|
|
|
|
using reverse_iterator = std::reverse_iterator<const char*>;
|
|
|
|
|
2014-12-09 14:56:05 +01:00
|
|
|
[[gnu::always_inline]]
|
2014-04-18 14:45:33 +02:00
|
|
|
iterator begin() const { return m_data; }
|
2014-12-09 14:56:05 +01:00
|
|
|
[[gnu::always_inline]]
|
2014-04-18 14:45:33 +02:00
|
|
|
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 16:36:12 +02:00
|
|
|
[[gnu::always_inline]]
|
2014-04-18 14:45:33 +02:00
|
|
|
char operator[](ByteCount pos) const { return m_data[(int)pos]; }
|
2014-08-11 20:40:35 +02:00
|
|
|
Codepoint operator[](CharCount pos) { return utf8::codepoint(utf8::advance(begin(), end(), pos), end()); }
|
2014-04-18 14:45:33 +02:00
|
|
|
|
2014-08-17 16:36:12 +02:00
|
|
|
[[gnu::always_inline]]
|
2014-04-18 14:45:33 +02:00
|
|
|
ByteCount length() const { return m_length; }
|
|
|
|
CharCount char_length() const { return utf8::distance(begin(), end()); }
|
2014-08-17 16:36:12 +02:00
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2014-04-18 14:45:33 +02: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 13:16:32 +02:00
|
|
|
struct ZeroTerminatedString
|
|
|
|
{
|
|
|
|
ZeroTerminatedString(const char* begin, const char* end)
|
|
|
|
{
|
|
|
|
if (*end == '\0')
|
|
|
|
unowned = begin;
|
|
|
|
else
|
2015-01-09 14:57:21 +01:00
|
|
|
owned = StringBase(begin, end);
|
2014-04-20 13:16:32 +02:00
|
|
|
}
|
|
|
|
operator const char*() const { return unowned ? unowned : owned.c_str(); }
|
|
|
|
|
|
|
|
private:
|
2015-01-09 14:57:21 +01:00
|
|
|
StringBase owned;
|
2014-04-20 13:16:32 +02:00
|
|
|
const char* unowned = nullptr;
|
|
|
|
|
|
|
|
};
|
|
|
|
ZeroTerminatedString zstr() const { return ZeroTerminatedString{begin(), end()}; }
|
|
|
|
|
2014-04-18 14:45:33 +02:00
|
|
|
private:
|
|
|
|
const char* m_data;
|
|
|
|
ByteCount m_length;
|
|
|
|
};
|
|
|
|
|
2014-12-08 14:46:07 +01:00
|
|
|
inline bool operator==(StringView lhs, StringView rhs)
|
2014-04-18 14:45:33 +02:00
|
|
|
{
|
2015-02-11 00:09:30 +01:00
|
|
|
return lhs.m_length == rhs.m_length and
|
|
|
|
std::equal(lhs.begin(), lhs.end(), rhs.begin());
|
2014-04-18 14:45:33 +02:00
|
|
|
}
|
|
|
|
|
2014-12-08 14:46:07 +01:00
|
|
|
inline bool operator!=(StringView lhs, StringView rhs)
|
2014-04-18 14:45:33 +02:00
|
|
|
{
|
2014-12-08 14:46:07 +01:00
|
|
|
return not (lhs == rhs);
|
2014-04-18 14:45:33 +02:00
|
|
|
}
|
|
|
|
|
2014-10-01 01:20:12 +02:00
|
|
|
bool operator<(StringView lhs, StringView rhs);
|
|
|
|
|
2014-04-18 14:45:33 +02:00
|
|
|
inline ByteCount StringView::byte_count_to(CharCount count) const
|
|
|
|
{
|
|
|
|
return utf8::advance(begin(), end(), (int)count) - begin();
|
|
|
|
}
|
2014-12-08 14:46:07 +01:00
|
|
|
|
2014-04-18 14:45:33 +02:00
|
|
|
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 13:03:57 +02:00
|
|
|
if (length < 0)
|
|
|
|
length = INT_MAX;
|
2014-04-18 14:45:33 +02: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 13:03:57 +02:00
|
|
|
if (length < 0)
|
|
|
|
length = INT_MAX;
|
2014-04-18 14:45:33 +02:00
|
|
|
auto beg = utf8::advance(begin(), end(), (int)from);
|
|
|
|
return StringView{ beg, utf8::advance(beg, end(), length) };
|
|
|
|
}
|
|
|
|
|
2014-05-11 13:44:51 +02: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 14:45:33 +02:00
|
|
|
inline String& operator+=(String& lhs, StringView rhs)
|
|
|
|
{
|
|
|
|
lhs.append(rhs.data(), (size_t)(int)rhs.length());
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
2014-12-08 14:59:29 +01:00
|
|
|
inline String operator+(const String& lhs, const String& rhs)
|
2014-04-18 14:45:33 +02:00
|
|
|
{
|
|
|
|
String res = lhs;
|
|
|
|
res += rhs;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-12-08 14:59:29 +01:00
|
|
|
inline String operator+(StringView lhs, StringView rhs)
|
|
|
|
{
|
|
|
|
String res{lhs.begin(), lhs.end()};
|
|
|
|
res += rhs;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-04-18 14:45:33 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-08 14:59:29 +01:00
|
|
|
inline String operator+(const char* lhs, StringView rhs)
|
2014-04-18 14:45:33 +02:00
|
|
|
{
|
2014-12-08 14:59:29 +01:00
|
|
|
return StringView{lhs} + rhs;
|
2014-04-18 14:45:33 +02:00
|
|
|
}
|
|
|
|
|
2014-12-08 14:59:29 +01:00
|
|
|
inline String operator+(StringView lhs, const char* rhs)
|
2012-04-14 03:23:20 +02:00
|
|
|
{
|
2014-12-08 14:59:29 +01:00
|
|
|
return lhs + StringView{rhs};
|
2012-04-14 03:23:20 +02:00
|
|
|
}
|
2012-04-14 03:17:09 +02:00
|
|
|
|
2014-12-08 14:59:29 +01:00
|
|
|
inline String operator+(const char* lhs, const String& rhs)
|
2013-03-29 14:21:55 +01:00
|
|
|
{
|
2014-12-08 14:59:29 +01:00
|
|
|
return StringView{lhs} + rhs;
|
2013-03-29 14:21:55 +01:00
|
|
|
}
|
|
|
|
|
2014-12-08 14:59:29 +01:00
|
|
|
inline String operator+(const String& lhs, const char* rhs)
|
2014-05-11 13:44:51 +02:00
|
|
|
{
|
2014-12-08 14:59:29 +01:00
|
|
|
return lhs + StringView{rhs};
|
2014-05-11 13:44:51 +02:00
|
|
|
}
|
|
|
|
|
2015-01-09 14:57:21 +01:00
|
|
|
Vector<String> split(StringView str, char separator, char escape);
|
|
|
|
Vector<StringView> split(StringView str, char separator);
|
2014-10-19 17:27:36 +02:00
|
|
|
|
2014-08-03 11:02:17 +02:00
|
|
|
String escape(StringView str, StringView characters, char escape);
|
2014-11-04 14:31:15 +01:00
|
|
|
String unescape(StringView str, StringView characters, char escape);
|
2012-05-29 07:19:50 +02:00
|
|
|
|
2015-02-19 14:54:03 +01:00
|
|
|
String indent(StringView str, StringView indent = " ");
|
|
|
|
|
2014-12-28 12:16:51 +01:00
|
|
|
template<typename Container>
|
2015-01-17 23:55:48 +01:00
|
|
|
String join(const Container& container, char joiner, bool esc_joiner = true)
|
2014-12-28 12:16:51 +01:00
|
|
|
{
|
|
|
|
String res;
|
|
|
|
for (const auto& str : container)
|
|
|
|
{
|
|
|
|
if (not res.empty())
|
|
|
|
res += joiner;
|
2015-01-17 23:55:48 +01:00
|
|
|
res += esc_joiner ? escape(str, joiner, '\\') : str;
|
2014-12-28 12:16:51 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-10-16 15:11:22 +02:00
|
|
|
inline String operator"" _str(const char* str, size_t)
|
|
|
|
{
|
|
|
|
return String(str);
|
|
|
|
}
|
|
|
|
|
2015-01-13 14:47:46 +01:00
|
|
|
inline StringView operator"" _sv(const char* str, size_t len)
|
|
|
|
{
|
|
|
|
return StringView(str, (int)len);
|
|
|
|
}
|
|
|
|
|
2013-01-17 18:47:53 +01:00
|
|
|
inline String codepoint_to_str(Codepoint cp)
|
|
|
|
{
|
2015-01-09 14:57:21 +01:00
|
|
|
StringBase 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);
|
|
|
|
}
|
|
|
|
|
2014-08-03 11:02:17 +02:00
|
|
|
int str_to_int(StringView str);
|
2013-05-13 14:23:07 +02:00
|
|
|
|
2013-06-18 22:11:44 +02:00
|
|
|
String to_string(int val);
|
2015-01-13 14:47:46 +01:00
|
|
|
String to_string(size_t val);
|
|
|
|
String to_string(float val);
|
2013-05-13 14:23:07 +02:00
|
|
|
|
|
|
|
template<typename RealType, typename ValueType>
|
2013-06-18 22:11:44 +02:00
|
|
|
String to_string(const StronglyTypedNumber<RealType, ValueType>& val)
|
2013-05-13 14:23:07 +02:00
|
|
|
{
|
|
|
|
return to_string((ValueType)val);
|
|
|
|
}
|
|
|
|
|
2014-12-08 14:46:07 +01:00
|
|
|
inline bool prefix_match(StringView str, StringView prefix)
|
|
|
|
{
|
|
|
|
return str.substr(0_byte, prefix.length()) == prefix;
|
|
|
|
}
|
|
|
|
|
2014-04-18 14:45:33 +02:00
|
|
|
bool subsequence_match(StringView str, StringView subseq);
|
2013-09-23 21:16:25 +02:00
|
|
|
|
2014-04-28 20:49:00 +02:00
|
|
|
String expand_tabs(StringView line, CharCount tabstop, CharCount col = 0);
|
|
|
|
|
2015-01-09 14:57:21 +01:00
|
|
|
Vector<StringView> wrap_lines(StringView text, CharCount max_width);
|
2014-11-19 20:42:15 +01:00
|
|
|
|
2014-12-16 19:57:19 +01:00
|
|
|
inline size_t hash_value(const Kakoune::String& str)
|
|
|
|
{
|
|
|
|
return hash_data(str.data(), (int)str.length());
|
2012-04-14 03:17:09 +02:00
|
|
|
}
|
|
|
|
|
2014-12-16 19:57:19 +01:00
|
|
|
inline size_t hash_value(const Kakoune::StringView& str)
|
2012-04-14 03:23:20 +02:00
|
|
|
{
|
2014-12-16 19:57:19 +01:00
|
|
|
return hash_data(str.data(), (int)str.length());
|
|
|
|
}
|
2014-10-01 01:20:12 +02:00
|
|
|
|
2012-04-14 03:23:20 +02:00
|
|
|
}
|
2012-04-14 03:17:09 +02:00
|
|
|
|
|
|
|
#endif // string_hh_INCLUDED
|