mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-08-24 08:56:43 +02:00
I18N
- 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:
parent
ae58b85339
commit
01e589a172
9 changed files with 108 additions and 16 deletions
26
src/utf8.cpp
26
src/utf8.cpp
|
@ -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;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue