2012-04-14 05:17:09 +04:00
|
|
|
#ifndef string_hh_INCLUDED
|
|
|
|
#define string_hh_INCLUDED
|
|
|
|
|
2015-05-01 20:47:22 +03:00
|
|
|
#include "array_view.hh"
|
|
|
|
#include "hash.hh"
|
|
|
|
#include "optional.hh"
|
2012-08-24 01:56:35 +04:00
|
|
|
#include "units.hh"
|
2012-10-11 02:41:48 +04:00
|
|
|
#include "utf8.hh"
|
2015-01-09 16:57:21 +03:00
|
|
|
#include "vector.hh"
|
2012-04-14 05:17:09 +04:00
|
|
|
|
2013-04-09 22:05:40 +04:00
|
|
|
#include <string>
|
2014-10-13 16:12:33 +04:00
|
|
|
#include <climits>
|
2013-04-09 22:05:40 +04:00
|
|
|
|
2012-04-14 05:17:09 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-05-11 15:44:51 +04:00
|
|
|
class StringView;
|
|
|
|
|
2015-03-10 22:33:46 +03:00
|
|
|
template<typename Type, typename CharType>
|
|
|
|
class StringOps
|
2015-02-11 02:09:30 +03:00
|
|
|
{
|
2015-03-10 22:33:46 +03:00
|
|
|
public:
|
|
|
|
using value_type = CharType;
|
|
|
|
|
|
|
|
friend inline size_t hash_value(const Type& str)
|
|
|
|
{
|
|
|
|
return hash_data(str.data(), (int)str.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
using iterator = CharType*;
|
|
|
|
using const_iterator = const CharType*;
|
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
iterator begin() { return type().data(); }
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
const_iterator begin() const { return type().data(); }
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
iterator end() { return type().data() + (int)type().length(); }
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
const_iterator end() const { return type().data() + (int)type().length(); }
|
|
|
|
|
|
|
|
reverse_iterator rbegin() { return reverse_iterator{end()}; }
|
|
|
|
const_reverse_iterator rbegin() const { return const_reverse_iterator{end()}; }
|
|
|
|
|
|
|
|
reverse_iterator rend() { return reverse_iterator{begin()}; }
|
|
|
|
const_reverse_iterator rend() const { return const_reverse_iterator{begin()}; }
|
|
|
|
|
|
|
|
CharType& front() { return *type().data(); }
|
|
|
|
const CharType& front() const { return *type().data(); }
|
|
|
|
CharType& back() { return type().data()[(int)type().length() - 1]; }
|
|
|
|
const CharType& back() const { return type().data()[(int)type().length() - 1]; }
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
CharType& operator[](ByteCount pos) { return type().data()[(int)pos]; }
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2015-03-11 22:41:44 +03:00
|
|
|
const CharType& operator[](ByteCount pos) const { return type().data()[(int)pos]; }
|
2015-02-11 02:09:30 +03:00
|
|
|
|
2015-03-10 22:33:46 +03:00
|
|
|
Codepoint operator[](CharCount pos) const
|
|
|
|
{ return utf8::codepoint(utf8::advance(begin(), end(), pos), end()); }
|
|
|
|
|
|
|
|
CharCount char_length() const { return utf8::distance(begin(), end()); }
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
bool empty() const { return type().length() == 0_byte; }
|
|
|
|
|
|
|
|
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); }
|
|
|
|
|
|
|
|
StringView substr(ByteCount from, ByteCount length = INT_MAX) const;
|
|
|
|
StringView substr(CharCount from, CharCount length = INT_MAX) const;
|
|
|
|
|
|
|
|
private:
|
2015-03-11 22:41:44 +03:00
|
|
|
[[gnu::always_inline]]
|
2015-03-10 22:33:46 +03:00
|
|
|
Type& type() { return *static_cast<Type*>(this); }
|
2015-03-11 22:41:44 +03:00
|
|
|
[[gnu::always_inline]]
|
2015-03-10 22:33:46 +03:00
|
|
|
const Type& type() const { return *static_cast<const Type*>(this); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class String : public StringOps<String, char>
|
2012-04-14 05:23:20 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-03-10 22:33:46 +03:00
|
|
|
using Content = std::basic_string<char, std::char_traits<char>,
|
|
|
|
Allocator<char, MemoryDomain::String>>;
|
|
|
|
|
2015-03-12 16:02:46 +03:00
|
|
|
String() {}
|
2015-03-10 22:33:46 +03:00
|
|
|
String(const char* content) : m_data(content) {}
|
2015-03-31 15:53:40 +03:00
|
|
|
String(const char* content, ByteCount len) : m_data(content, (size_t)(int)len) {}
|
2015-03-10 22:33:46 +03:00
|
|
|
explicit String(char content, CharCount count = 1) : m_data((size_t)(int)count, content) {}
|
2013-11-14 04:12:15 +04:00
|
|
|
explicit String(Codepoint cp, CharCount count = 1)
|
|
|
|
{
|
|
|
|
while (count-- > 0)
|
2015-03-10 22:33:46 +03:00
|
|
|
utf8::dump(std::back_inserter(*this), cp);
|
2013-11-14 04:12:15 +04:00
|
|
|
}
|
|
|
|
template<typename Iterator>
|
2015-03-10 22:33:46 +03:00
|
|
|
String(Iterator begin, Iterator end) : m_data(begin, end) {}
|
2013-11-14 04:12:15 +04:00
|
|
|
|
2015-03-10 22:33:46 +03:00
|
|
|
[[gnu::always_inline]]
|
|
|
|
char* data() { return &m_data[0]; }
|
2013-11-14 04:12:15 +04:00
|
|
|
|
2014-08-17 18:36:12 +04:00
|
|
|
[[gnu::always_inline]]
|
2015-03-10 22:33:46 +03:00
|
|
|
const char* data() const { return m_data.data(); }
|
|
|
|
|
2014-08-17 18:36:12 +04:00
|
|
|
[[gnu::always_inline]]
|
2015-11-11 22:05:32 +03:00
|
|
|
ByteCount length() const { return m_data.length(); }
|
2014-08-17 18:36:12 +04:00
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
2015-03-10 22:33:46 +03:00
|
|
|
const char* c_str() const { return m_data.c_str(); }
|
|
|
|
|
2015-03-11 22:41:44 +03:00
|
|
|
[[gnu::always_inline]]
|
2015-03-10 22:33:46 +03:00
|
|
|
void append(const char* data, ByteCount count) { m_data.append(data, (size_t)(int)count); }
|
2013-11-14 04:12:15 +04:00
|
|
|
|
2015-09-19 14:19:17 +03:00
|
|
|
void clear() { m_data.clear(); }
|
|
|
|
|
2015-03-10 22:33:46 +03:00
|
|
|
void push_back(char c) { m_data.push_back(c); }
|
|
|
|
void resize(ByteCount size) { m_data.resize((size_t)(int)size); }
|
|
|
|
void reserve(ByteCount size) { m_data.reserve((size_t)(int)size); }
|
2013-11-14 04:12:15 +04:00
|
|
|
|
2015-11-26 00:07:41 +03:00
|
|
|
static const String ms_empty;
|
|
|
|
|
2015-03-10 22:33:46 +03:00
|
|
|
private:
|
|
|
|
Content m_data;
|
2012-04-14 05:23:20 +04:00
|
|
|
};
|
|
|
|
|
2015-03-10 22:33:46 +03:00
|
|
|
class StringView : public StringOps<StringView, const char>
|
2014-04-18 16:45:33 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-03-11 22:41:44 +03:00
|
|
|
constexpr StringView() = default;
|
2014-04-18 16:45:33 +04:00
|
|
|
constexpr StringView(const char* data, ByteCount length)
|
|
|
|
: m_data{data}, m_length{length} {}
|
2015-06-01 21:06:35 +03:00
|
|
|
constexpr StringView(const char* data) : m_data{data}, m_length{data ? strlen(data) : 0} {}
|
2014-04-18 16:45:33 +04:00
|
|
|
constexpr StringView(const char* begin, const char* end) : m_data{begin}, m_length{(int)(end - begin)} {}
|
2015-03-10 22:33:46 +03:00
|
|
|
StringView(const String& str) : m_data{str.data()}, m_length{(int)str.length()} {}
|
2014-11-04 16:31:15 +03:00
|
|
|
StringView(const char& c) : m_data(&c), m_length(1) {}
|
2015-03-30 15:33:46 +03:00
|
|
|
StringView(int c) = delete;
|
2014-04-18 16:45:33 +04:00
|
|
|
|
2014-08-17 18:36:12 +04:00
|
|
|
[[gnu::always_inline]]
|
2015-03-11 22:41:44 +03:00
|
|
|
constexpr const char* data() const { return m_data; }
|
2014-04-18 16:45:33 +04:00
|
|
|
|
2014-08-17 18:36:12 +04:00
|
|
|
[[gnu::always_inline]]
|
2015-03-11 22:41:44 +03:00
|
|
|
constexpr ByteCount length() const { return m_length; }
|
2014-04-18 16:45:33 +04:00
|
|
|
|
2015-03-11 22:41:44 +03:00
|
|
|
String str() const { return {begin(), end()}; }
|
2014-04-18 16:45:33 +04:00
|
|
|
|
2014-04-20 15:16:32 +04:00
|
|
|
struct ZeroTerminatedString
|
|
|
|
{
|
|
|
|
ZeroTerminatedString(const char* begin, const char* end)
|
|
|
|
{
|
|
|
|
if (*end == '\0')
|
|
|
|
unowned = begin;
|
|
|
|
else
|
2015-03-10 22:33:46 +03:00
|
|
|
owned = String::Content(begin, end);
|
2014-04-20 15:16:32 +04:00
|
|
|
}
|
|
|
|
operator const char*() const { return unowned ? unowned : owned.c_str(); }
|
|
|
|
|
|
|
|
private:
|
2015-03-10 22:33:46 +03:00
|
|
|
String::Content owned;
|
2014-04-20 15:16:32 +04:00
|
|
|
const char* unowned = nullptr;
|
|
|
|
|
|
|
|
};
|
2015-03-11 22:41:44 +03:00
|
|
|
ZeroTerminatedString zstr() const { return {begin(), end()}; }
|
2014-04-20 15:16:32 +04:00
|
|
|
|
2015-04-08 01:21:14 +03:00
|
|
|
[[gnu::optimize(3)]] // this is recursive for constexpr reason
|
2015-03-10 22:33:46 +03:00
|
|
|
static constexpr ByteCount strlen(const char* s)
|
|
|
|
{
|
|
|
|
return *s == 0 ? 0 : strlen(s+1) + 1;
|
|
|
|
}
|
|
|
|
|
2015-05-29 15:35:54 +03:00
|
|
|
private:
|
2015-03-11 22:41:44 +03:00
|
|
|
const char* m_data = nullptr;
|
|
|
|
ByteCount m_length = 0;
|
2014-04-18 16:45:33 +04:00
|
|
|
};
|
|
|
|
|
2015-03-10 22:33:46 +03:00
|
|
|
template<typename Type, typename CharType>
|
|
|
|
inline StringView StringOps<Type, CharType>::substr(ByteCount from, ByteCount length) const
|
2014-04-18 16:45:33 +04:00
|
|
|
{
|
2014-04-20 15:03:57 +04:00
|
|
|
if (length < 0)
|
|
|
|
length = INT_MAX;
|
2015-03-10 22:33:46 +03:00
|
|
|
return StringView{ type().data() + (int)from, std::min(type().length() - from, length) };
|
2014-04-18 16:45:33 +04:00
|
|
|
}
|
|
|
|
|
2015-03-10 22:33:46 +03:00
|
|
|
template<typename Type, typename CharType>
|
|
|
|
inline StringView StringOps<Type, CharType>::substr(CharCount from, CharCount length) const
|
2014-04-18 16:45:33 +04:00
|
|
|
{
|
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) };
|
|
|
|
}
|
|
|
|
|
|
|
|
inline String& operator+=(String& lhs, StringView rhs)
|
|
|
|
{
|
2015-03-10 22:33:46 +03:00
|
|
|
lhs.append(rhs.data(), rhs.length());
|
2014-04-18 16:45:33 +04:00
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
2014-12-08 16:59:29 +03:00
|
|
|
inline String operator+(StringView lhs, StringView rhs)
|
|
|
|
{
|
2015-03-10 22:33:46 +03:00
|
|
|
String res;
|
|
|
|
res.reserve((int)(lhs.length() + rhs.length()));
|
2015-03-11 22:41:44 +03:00
|
|
|
res.append(lhs.data(), lhs.length());
|
|
|
|
res.append(rhs.data(), rhs.length());
|
2014-04-18 16:45:33 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-03-30 21:59:08 +03:00
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline bool operator==(const StringView& lhs, const StringView& rhs)
|
|
|
|
{
|
|
|
|
return lhs.length() == rhs.length() and
|
|
|
|
std::equal(lhs.begin(), lhs.end(), rhs.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline bool operator!=(const StringView& lhs, const StringView& rhs)
|
|
|
|
{ return not (lhs == rhs); }
|
|
|
|
|
|
|
|
inline bool operator<(const StringView& lhs, const StringView& rhs)
|
|
|
|
{
|
|
|
|
return std::lexicographical_compare(lhs.begin(), lhs.end(),
|
|
|
|
rhs.begin(), rhs.end());
|
|
|
|
}
|
|
|
|
|
2015-01-09 16:57:21 +03:00
|
|
|
Vector<String> split(StringView str, char separator, char escape);
|
|
|
|
Vector<StringView> split(StringView str, char separator);
|
2014-10-19 19:27:36 +04:00
|
|
|
|
2014-08-03 13:02:17 +04:00
|
|
|
String escape(StringView str, StringView characters, char escape);
|
2014-11-04 16:31:15 +03:00
|
|
|
String unescape(StringView str, StringView characters, char escape);
|
2012-05-29 09:19:50 +04:00
|
|
|
|
2015-02-19 16:54:03 +03:00
|
|
|
String indent(StringView str, StringView indent = " ");
|
|
|
|
|
2014-12-28 14:16:51 +03:00
|
|
|
template<typename Container>
|
2015-01-18 01:55:48 +03:00
|
|
|
String join(const Container& container, char joiner, bool esc_joiner = true)
|
2014-12-28 14:16:51 +03:00
|
|
|
{
|
|
|
|
String res;
|
|
|
|
for (const auto& str : container)
|
|
|
|
{
|
|
|
|
if (not res.empty())
|
|
|
|
res += joiner;
|
2015-01-18 01:55:48 +03:00
|
|
|
res += esc_joiner ? escape(str, joiner, '\\') : str;
|
2014-12-28 14:16:51 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-03-10 22:33:46 +03:00
|
|
|
String str;
|
|
|
|
utf8::dump(std::back_inserter(str), cp);
|
|
|
|
return str;
|
2013-01-17 21:47:53 +04:00
|
|
|
}
|
|
|
|
|
2015-05-01 20:47:22 +03:00
|
|
|
int str_to_int(StringView str); // throws on error
|
|
|
|
Optional<int> str_to_int_ifp(StringView str);
|
2013-05-13 16:23:07 +04:00
|
|
|
|
2015-03-31 15:53:40 +03:00
|
|
|
template<size_t N>
|
|
|
|
struct InplaceString
|
|
|
|
{
|
2015-08-18 23:06:53 +03:00
|
|
|
static_assert(N < 256, "InplaceString cannot handle sizes >= 256");
|
2015-03-31 15:53:40 +03:00
|
|
|
|
2015-08-18 23:06:53 +03:00
|
|
|
constexpr operator StringView() const { return {m_data, ByteCount{m_length}}; }
|
|
|
|
operator String() const { return {m_data, ByteCount{m_length}}; }
|
|
|
|
|
|
|
|
unsigned char m_length;
|
2015-03-31 15:53:40 +03:00
|
|
|
char m_data[N];
|
|
|
|
};
|
|
|
|
|
2015-06-22 15:56:00 +03:00
|
|
|
struct Hex { size_t val; };
|
|
|
|
inline Hex hex(size_t val) { return {val}; }
|
|
|
|
|
2015-08-18 23:06:53 +03:00
|
|
|
InplaceString<15> to_string(int val);
|
2015-09-25 01:36:29 +03:00
|
|
|
InplaceString<23> to_string(long int val);
|
2015-08-18 23:06:53 +03:00
|
|
|
InplaceString<23> to_string(size_t val);
|
|
|
|
InplaceString<23> to_string(Hex val);
|
|
|
|
InplaceString<23> to_string(float val);
|
|
|
|
InplaceString<7> to_string(Codepoint c);
|
2013-05-13 16:23:07 +04:00
|
|
|
|
|
|
|
template<typename RealType, typename ValueType>
|
2015-03-31 15:53:40 +03:00
|
|
|
decltype(to_string(std::declval<ValueType>()))
|
|
|
|
to_string(const StronglyTypedNumber<RealType, ValueType>& val)
|
2013-05-13 16:23:07 +04:00
|
|
|
{
|
|
|
|
return to_string((ValueType)val);
|
|
|
|
}
|
|
|
|
|
2014-12-08 16:46:07 +03:00
|
|
|
inline bool prefix_match(StringView str, StringView prefix)
|
|
|
|
{
|
|
|
|
return str.substr(0_byte, prefix.length()) == prefix;
|
|
|
|
}
|
|
|
|
|
2014-04-18 16:45:33 +04:00
|
|
|
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);
|
|
|
|
|
2015-01-09 16:57:21 +03:00
|
|
|
Vector<StringView> wrap_lines(StringView text, CharCount max_width);
|
2014-11-19 22:42:15 +03:00
|
|
|
|
2015-03-31 01:05:24 +03:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T> using IsString = std::is_convertible<T, StringView>;
|
|
|
|
|
2015-05-26 20:42:09 +03:00
|
|
|
template<typename T, class = typename std::enable_if<!IsString<T>::value>::type>
|
2015-03-31 15:53:40 +03:00
|
|
|
auto format_param(const T& val) -> decltype(to_string(val)) { return to_string(val); }
|
2015-03-31 01:05:24 +03:00
|
|
|
|
2015-05-26 20:42:09 +03:00
|
|
|
template<typename T, class = typename std::enable_if<IsString<T>::value>::type>
|
2015-03-31 01:05:24 +03:00
|
|
|
StringView format_param(const T& val) { return val; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
String format(StringView fmt, ArrayView<const StringView> params);
|
|
|
|
|
|
|
|
template<typename... Types>
|
|
|
|
String format(StringView fmt, Types... params)
|
|
|
|
{
|
2015-03-31 15:53:40 +03:00
|
|
|
return format(fmt, ArrayView<const StringView>{detail::format_param(params)...});
|
2015-03-31 01:05:24 +03:00
|
|
|
}
|
|
|
|
|
2015-04-22 15:19:46 +03:00
|
|
|
StringView format_to(ArrayView<char> buffer, StringView fmt, ArrayView<const StringView> params);
|
|
|
|
|
|
|
|
template<typename... Types>
|
|
|
|
StringView format_to(ArrayView<char> buffer, StringView fmt, Types... params)
|
|
|
|
{
|
|
|
|
return format_to(buffer, fmt, ArrayView<const StringView>{detail::format_param(params)...});
|
|
|
|
}
|
|
|
|
|
2012-04-14 05:23:20 +04:00
|
|
|
}
|
2012-04-14 05:17:09 +04:00
|
|
|
|
|
|
|
#endif // string_hh_INCLUDED
|