2017-10-09 16:12:42 +02:00
|
|
|
#ifndef string_utils_hh_INCLUDED
|
|
|
|
#define string_utils_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "string.hh"
|
2019-06-23 01:58:31 +02:00
|
|
|
#include "enum.hh"
|
2017-10-09 16:12:42 +02:00
|
|
|
#include "vector.hh"
|
2019-11-23 11:50:58 +01:00
|
|
|
#include "ranges.hh"
|
2017-10-09 16:12:42 +02:00
|
|
|
#include "optional.hh"
|
2021-07-31 01:45:05 +02:00
|
|
|
#include "utils.hh"
|
2017-10-09 16:12:42 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2019-07-23 12:09:41 +02:00
|
|
|
String trim_indent(StringView str);
|
2017-10-09 16:12:42 +02:00
|
|
|
|
|
|
|
String escape(StringView str, StringView characters, char escape);
|
|
|
|
String unescape(StringView str, StringView characters, char escape);
|
|
|
|
|
2017-12-06 10:18:44 +01:00
|
|
|
template<char character, char escape>
|
2017-12-06 18:56:02 +01:00
|
|
|
String unescape(StringView str)
|
|
|
|
{
|
|
|
|
const char to_escape[2] = { character, escape };
|
|
|
|
return unescape(str, {to_escape, 2}, escape);
|
|
|
|
}
|
2017-12-06 10:18:44 +01:00
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
String indent(StringView str, StringView indent = " ");
|
|
|
|
|
|
|
|
String replace(StringView str, StringView substr, StringView replacement);
|
|
|
|
|
2020-05-19 09:16:37 +02:00
|
|
|
String left_pad(StringView str, ColumnCount size, Codepoint c = ' ');
|
|
|
|
String right_pad(StringView str, ColumnCount size, Codepoint c = ' ');
|
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
template<typename Container>
|
|
|
|
String join(const Container& container, char joiner, bool esc_joiner = true)
|
|
|
|
{
|
|
|
|
const char to_escape[2] = { joiner, '\\' };
|
|
|
|
String res;
|
|
|
|
for (const auto& str : container)
|
|
|
|
{
|
|
|
|
if (not res.empty())
|
|
|
|
res += joiner;
|
|
|
|
res += esc_joiner ? escape(str, {to_escape, 2}, '\\') : str;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-04-29 15:05:46 +02:00
|
|
|
template<typename Container>
|
|
|
|
String join(const Container& container, StringView joiner)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
for (const auto& str : container)
|
|
|
|
{
|
|
|
|
if (not res.empty())
|
|
|
|
res += joiner;
|
|
|
|
res += str;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
inline bool prefix_match(StringView str, StringView prefix)
|
|
|
|
{
|
|
|
|
return str.substr(0_byte, prefix.length()) == prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subsequence_match(StringView str, StringView subseq);
|
|
|
|
|
|
|
|
String expand_tabs(StringView line, ColumnCount tabstop, ColumnCount col = 0);
|
|
|
|
|
2019-11-23 11:50:58 +01:00
|
|
|
struct WrapView
|
|
|
|
{
|
|
|
|
struct Iterator : std::iterator<std::forward_iterator_tag, StringView>
|
|
|
|
{
|
|
|
|
Iterator(StringView text, ColumnCount max_width);
|
|
|
|
|
|
|
|
Iterator& operator++();
|
|
|
|
Iterator operator++(int) { auto copy = *this; ++(*this); return copy; }
|
|
|
|
|
|
|
|
bool operator==(Iterator other) const { return m_remaining == other.m_remaining and m_current == other.m_current; }
|
|
|
|
bool operator!=(Iterator other) const { return not (*this == other); }
|
|
|
|
|
|
|
|
StringView operator*() { return m_current; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
StringView m_current;
|
|
|
|
StringView m_remaining;
|
|
|
|
ColumnCount m_max_width;
|
|
|
|
};
|
|
|
|
|
|
|
|
Iterator begin() const { return {text, max_width}; }
|
|
|
|
Iterator end() const { return {{}, 1}; }
|
|
|
|
|
|
|
|
StringView text;
|
|
|
|
ColumnCount max_width;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline auto wrap_at(ColumnCount max_width)
|
|
|
|
{
|
|
|
|
return make_view_factory([=](StringView text) {
|
|
|
|
return WrapView{text, max_width};
|
|
|
|
});
|
|
|
|
}
|
2017-10-09 16:12:42 +02:00
|
|
|
|
|
|
|
int str_to_int(StringView str); // throws on error
|
|
|
|
Optional<int> str_to_int_ifp(StringView str);
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
struct InplaceString
|
|
|
|
{
|
|
|
|
static_assert(N < 256, "InplaceString cannot handle sizes >= 256");
|
|
|
|
|
|
|
|
constexpr operator StringView() const { return {m_data, ByteCount{m_length}}; }
|
|
|
|
operator String() const { return {m_data, ByteCount{m_length}}; }
|
|
|
|
|
|
|
|
unsigned char m_length;
|
|
|
|
char m_data[N];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Hex { size_t val; };
|
|
|
|
constexpr Hex hex(size_t val) { return {val}; }
|
|
|
|
|
|
|
|
InplaceString<15> to_string(int val);
|
2018-04-27 00:34:49 +02:00
|
|
|
InplaceString<15> to_string(unsigned val);
|
2017-10-09 16:12:42 +02:00
|
|
|
InplaceString<23> to_string(long int val);
|
2018-05-20 23:53:21 +02:00
|
|
|
InplaceString<23> to_string(unsigned long val);
|
2017-10-09 16:12:42 +02:00
|
|
|
InplaceString<23> to_string(long long int val);
|
|
|
|
InplaceString<23> to_string(Hex val);
|
|
|
|
InplaceString<23> to_string(float val);
|
|
|
|
InplaceString<7> to_string(Codepoint c);
|
|
|
|
|
|
|
|
template<typename RealType, typename ValueType>
|
|
|
|
decltype(auto) to_string(const StronglyTypedNumber<RealType, ValueType>& val)
|
|
|
|
{
|
|
|
|
return to_string((ValueType)val);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T> constexpr bool is_string = std::is_convertible<T, StringView>::value;
|
|
|
|
|
|
|
|
template<typename T, class = std::enable_if_t<not is_string<T>>>
|
|
|
|
decltype(auto) format_param(const T& val) { return to_string(val); }
|
|
|
|
|
|
|
|
template<typename T, class = std::enable_if_t<is_string<T>>>
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return format(fmt, ArrayView<const StringView>{detail::format_param(std::forward<Types>(params))...});
|
|
|
|
}
|
|
|
|
|
|
|
|
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(std::forward<Types>(params))...});
|
|
|
|
}
|
|
|
|
|
2021-07-31 01:45:05 +02:00
|
|
|
void format_with(FunctionRef<void (StringView)> append, StringView fmt, ArrayView<const StringView> params);
|
|
|
|
|
|
|
|
template<typename... Types>
|
|
|
|
void format_with(FunctionRef<void (StringView)> append, StringView fmt, Types&&... params)
|
|
|
|
{
|
|
|
|
return format_with(append, fmt, ArrayView<const StringView>{detail::format_param(std::forward<Types>(params))...});
|
|
|
|
}
|
|
|
|
|
2018-06-23 04:43:39 +02:00
|
|
|
String double_up(StringView s, StringView characters);
|
|
|
|
|
2018-05-31 12:59:21 +02:00
|
|
|
inline String quote(StringView s)
|
|
|
|
{
|
2018-06-23 04:43:39 +02:00
|
|
|
return format("'{}'", double_up(s, "'"));
|
2018-05-31 12:59:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-27 13:52:57 +02:00
|
|
|
inline String shell_quote(StringView s)
|
|
|
|
{
|
|
|
|
return format("'{}'", replace(s, "'", R"('\'')"));
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class Quoting
|
|
|
|
{
|
2019-06-18 14:18:17 +02:00
|
|
|
Raw,
|
2018-08-27 13:52:57 +02:00
|
|
|
Kakoune,
|
|
|
|
Shell
|
|
|
|
};
|
|
|
|
|
2019-06-23 01:58:31 +02:00
|
|
|
constexpr auto enum_desc(Meta::Type<Quoting>)
|
|
|
|
{
|
2019-10-17 13:48:22 +02:00
|
|
|
return make_array<EnumDesc<Quoting>>({
|
2019-06-23 01:58:31 +02:00
|
|
|
{ Quoting::Raw, "raw" },
|
|
|
|
{ Quoting::Kakoune, "kakoune" },
|
|
|
|
{ Quoting::Shell, "shell" }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:52:57 +02:00
|
|
|
inline auto quoter(Quoting quoting)
|
|
|
|
{
|
2019-06-18 14:18:17 +02:00
|
|
|
switch (quoting)
|
|
|
|
{
|
|
|
|
case Quoting::Kakoune: return "e;
|
|
|
|
case Quoting::Shell: return &shell_quote;
|
|
|
|
case Quoting::Raw:
|
|
|
|
default:
|
|
|
|
return +[](StringView s) { return s.str(); };
|
|
|
|
}
|
2018-08-27 13:52:57 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 14:18:17 +02:00
|
|
|
inline String option_to_string(StringView opt, Quoting quoting) { return quoter(quoting)(opt); }
|
|
|
|
inline Vector<String> option_to_strings(StringView opt) { return {opt.str()}; }
|
|
|
|
inline String option_from_string(Meta::Type<String>, StringView str) { return str.str(); }
|
|
|
|
inline bool option_add(String& opt, StringView val) { opt += val; return not val.empty(); }
|
2018-08-27 13:52:57 +02:00
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // string_utils_hh_INCLUDED
|