More performant escape/unescape
This commit is contained in:
parent
7a36a4644e
commit
d2b82f507f
|
@ -61,11 +61,21 @@ String escape(StringView str, StringView characters, char escape)
|
||||||
{
|
{
|
||||||
String res;
|
String res;
|
||||||
res.reserve(str.length());
|
res.reserve(str.length());
|
||||||
for (auto& c : str)
|
for (auto it = str.begin(), end = str.end(); it != end; )
|
||||||
{
|
{
|
||||||
if (contains(characters, c))
|
auto next = std::find_if(it, end, [&characters](char c) { return contains(characters, c); });
|
||||||
res += escape;
|
if (next != end)
|
||||||
res += c;
|
{
|
||||||
|
res += StringView{it, next+1};
|
||||||
|
res.back() = escape;
|
||||||
|
res += *next;
|
||||||
|
it = next+1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res += StringView{it, next};
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -74,12 +84,17 @@ String unescape(StringView str, StringView characters, char escape)
|
||||||
{
|
{
|
||||||
String res;
|
String res;
|
||||||
res.reserve(str.length());
|
res.reserve(str.length());
|
||||||
for (auto& c : str)
|
for (auto it = str.begin(), end = str.end(); it != end; )
|
||||||
{
|
{
|
||||||
if (contains(characters, c) and not res.empty() and res.back() == escape)
|
auto next = std::find(it, end, escape);
|
||||||
res.back() = c;
|
if (next != end and next+1 != end and contains(characters, *(next+1)))
|
||||||
|
{
|
||||||
|
res += StringView{it, next+1};
|
||||||
|
res.back() = *(next+1);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
res += c;
|
res += StringView{it, next == end ? next : next + 1};
|
||||||
|
it = next == end ? next : next + 1;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user