- 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:
Paul Beckingham 2011-05-13 18:01:02 -04:00
parent 46b799a5b7
commit 291818c33d
3 changed files with 42 additions and 3 deletions

View file

@ -254,7 +254,7 @@ void extractLine (std::string& text, std::string& line, int length)
// Special case: no \n, and less than length characters total.
// special case: text.find ("\n") == std::string::npos && text.length () < length
if (eol == std::string::npos && utf8_length (text) <= length)
if (eol == std::string::npos && utf8_text_length (text) <= length)
{
line = text;
text = "";
@ -730,7 +730,7 @@ std::string leftJustify (const int input, const int width)
////////////////////////////////////////////////////////////////////////////////
std::string leftJustify (const std::string& input, const int width)
{
return input + std::string (width - utf8_length (input), ' ');
return input + std::string (width - utf8_text_length (input), ' ');
}
////////////////////////////////////////////////////////////////////////////////
@ -744,7 +744,7 @@ std::string rightJustify (const int input, const int width)
////////////////////////////////////////////////////////////////////////////////
std::string rightJustify (const std::string& input, const int width)
{
return std::string (width - utf8_length (input), ' ') + input;
return std::string (width - utf8_text_length (input), ' ') + input;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -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;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -34,6 +34,7 @@ unsigned int utf8_next_char (const std::string&, std::string::size_type&);
std::string utf8_character (unsigned int);
int utf8_sequence (unsigned int);
int utf8_length (const std::string&);
int utf8_text_length (const std::string&);
#endif