- One does not simply std::string::substr a UTF8 string, like a bloody caveman.
  Implemented utf8_substr, and added unit tests.
This commit is contained in:
Paul Beckingham 2013-01-16 18:35:37 -05:00
parent ae58b85339
commit 01e589a172
9 changed files with 108 additions and 16 deletions

View file

@ -231,3 +231,29 @@ unsigned int utf8_text_length (const std::string& str)
}
////////////////////////////////////////////////////////////////////////////////
const std::string utf8_substr (
const std::string& input,
unsigned int start,
unsigned int length /*=0*/)
{
// Find the starting index.
std::string::size_type index_start = 0;
for (unsigned int i = 0; i < start; i++)
utf8_next_char (input, index_start);
std::string result;
if (length)
{
std::string::size_type index_end = index_start;
for (unsigned int i = 0; i < length; i++)
utf8_next_char (input, index_end);
result = input.substr (index_start, index_end - index_start);
}
else
result = input.substr (index_start);
return result;
}
////////////////////////////////////////////////////////////////////////////////