Skip to content
Snippets Groups Projects
Commit 646c2b27 authored by Franck Dary's avatar Franck Dary
Browse files

Fixed memory bug in prefix and suffix functions

parent 07592b27
No related branches found
No related tags found
No related merge requests found
......@@ -58,9 +58,12 @@ util::utf8string util::splitAsUtf8(std::string_view s)
}
if (currentPtr - beginPtr > 4 || currentPtr - beginPtr == 0)
myThrow(fmt::format("Invalid utf8 character at index {}", beginPtr-s.data()));
utf8char c;
for (int i = 0; i < currentPtr - beginPtr; i++)
((char*)&c)[i] = beginPtr[i];
c[i] = beginPtr[i];
beginPtr = currentPtr;
result.push_back(c);
}
......
......@@ -67,6 +67,9 @@ std::function<std::string(const std::string &)> Submodule::getFunction(const std
{
static auto prefix = [](const std::string & s, int length)
{
if (s.size() == 0)
return s;
util::utf8string utf8s = util::splitAsUtf8(s);
util::utf8string prefix(utf8s.begin(), std::min(utf8s.end(),utf8s.begin()+length));
return fmt::format("{}", prefix);
......@@ -74,6 +77,9 @@ std::function<std::string(const std::string &)> Submodule::getFunction(const std
static auto suffix = [](const std::string & s, int length)
{
if (s.size() == 0)
return s;
util::utf8string utf8s = util::splitAsUtf8(s);
util::utf8string suffix(std::max(utf8s.begin(), utf8s.end()-length), utf8s.end());
return fmt::format("{}", suffix);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment