mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
i18n
- Added function utf8_text_length which calculates the length of text in characters, not bytes, and excludes color control codes.
This commit is contained in:
parent
46b799a5b7
commit
291818c33d
3 changed files with 42 additions and 3 deletions
38
src/utf8.cpp
38
src/utf8.cpp
|
@ -185,3 +185,41 @@ int utf8_length (const std::string& str)
|
|||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int utf8_text_length (const std::string& str)
|
||||
{
|
||||
int byteLength = str.length ();
|
||||
int charLength = byteLength;
|
||||
const char* data = str.data ();
|
||||
bool in_color = false;
|
||||
|
||||
// Decrement the number of bytes for each byte that matches 0b10??????
|
||||
// this way only the first byte of any utf8 sequence is counted.
|
||||
for (int i = 0; i < byteLength; i++)
|
||||
{
|
||||
if (in_color)
|
||||
{
|
||||
if (data[i] == 'm')
|
||||
in_color = false;
|
||||
|
||||
--charLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (data[i] == 033)
|
||||
{
|
||||
in_color = true;
|
||||
--charLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Extract the first two bits and check whether they are 10
|
||||
if ((data[i] & 0xC0) == 0x80)
|
||||
--charLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return charLength;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue