string escaping support functions
the split function now takes an additional escape parameter and does not split on separators that have the escaper before it. An utility escape function that adds escape before separators is also added.
This commit is contained in:
parent
d6425f1d50
commit
b5db256384
|
@ -5,7 +5,7 @@
|
|||
namespace Kakoune
|
||||
{
|
||||
|
||||
std::vector<String> split(const String& str, char separator)
|
||||
std::vector<String> split(const String& str, char separator, char escape)
|
||||
{
|
||||
auto begin = str.begin();
|
||||
auto end = str.begin();
|
||||
|
@ -13,12 +13,40 @@ std::vector<String> split(const String& str, char separator)
|
|||
std::vector<String> res;
|
||||
while (end != str.end())
|
||||
{
|
||||
while (end != str.end() and *end != separator)
|
||||
res.emplace_back();
|
||||
String& element = res.back();
|
||||
while (end != str.end())
|
||||
{
|
||||
auto c = *end;
|
||||
if (c == escape and end + 1 != end and *(end+1) == separator)
|
||||
{
|
||||
element += separator;
|
||||
end += 2;
|
||||
}
|
||||
else if (c == separator)
|
||||
{
|
||||
++end;
|
||||
res.push_back(String(begin, end));
|
||||
if (end == str.end())
|
||||
break;
|
||||
begin = ++end;
|
||||
}
|
||||
else
|
||||
{
|
||||
element += c;
|
||||
++end;
|
||||
}
|
||||
}
|
||||
begin = end;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
String escape(const String& str, char character, char escape)
|
||||
{
|
||||
String res;
|
||||
for (auto& c : str)
|
||||
{
|
||||
if (c == character)
|
||||
res += escape;
|
||||
res += c;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -84,7 +84,8 @@ inline String operator+(Codepoint lhs, const String& rhs)
|
|||
return String(lhs) + rhs;
|
||||
}
|
||||
|
||||
std::vector<String> split(const String& str, char separator);
|
||||
std::vector<String> split(const String& str, char separator, char escape = 0);
|
||||
String escape(const String& str, char character, char escape);
|
||||
|
||||
inline String operator"" _str(const char* str, size_t)
|
||||
{
|
||||
|
|
|
@ -120,11 +120,15 @@ void test_string()
|
|||
{
|
||||
kak_assert(String("youpi ") + "matin" == "youpi matin");
|
||||
|
||||
std::vector<String> splited = split("youpi:matin::tchou", ':');
|
||||
std::vector<String> splited = split("youpi:matin::tchou\\:kanaky:hihi\\:", ':', '\\');
|
||||
kak_assert(splited[0] == "youpi");
|
||||
kak_assert(splited[1] == "matin");
|
||||
kak_assert(splited[2] == "");
|
||||
kak_assert(splited[3] == "tchou");
|
||||
kak_assert(splited[3] == "tchou:kanaky");
|
||||
kak_assert(splited[4] == "hihi:");
|
||||
|
||||
String escaped = escape("youpi:matin:tchou:", ':', '\\');
|
||||
kak_assert(escaped == "youpi\\:matin\\:tchou\\:");
|
||||
}
|
||||
|
||||
void test_keys()
|
||||
|
|
Loading…
Reference in New Issue
Block a user