UTF8
- Added UTF8 string width calculation.
This commit is contained in:
14
src/utf8.cpp
14
src/utf8.cpp
@@ -28,6 +28,7 @@
|
||||
#define L10N // Localization complete.
|
||||
|
||||
#include <string>
|
||||
#include <text.h>
|
||||
#include <utf8.h>
|
||||
#include <i18n.h>
|
||||
|
||||
@@ -192,6 +193,19 @@ unsigned int utf8_length (const std::string& str)
|
||||
return charLength;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Width of a string in character cells.
|
||||
unsigned int utf8_width (const std::string& str)
|
||||
{
|
||||
unsigned int length = 0;
|
||||
std::string::size_type i = 0;
|
||||
unsigned int c;
|
||||
while ((c = utf8_next_char (str, i)))
|
||||
length += mk_wcwidth (c);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
unsigned int utf8_text_length (const std::string& str)
|
||||
{
|
||||
|
||||
@@ -37,6 +37,7 @@ std::string utf8_character (unsigned int);
|
||||
int utf8_sequence (unsigned int);
|
||||
unsigned int utf8_length (const std::string&);
|
||||
unsigned int utf8_text_length (const std::string&);
|
||||
unsigned int utf8_width (const std::string& str);
|
||||
const std::string utf8_substr (const std::string&, unsigned int, unsigned int length = 0);
|
||||
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main (int argc, char** argv)
|
||||
{
|
||||
UnitTest t (6);
|
||||
UnitTest t (10);
|
||||
|
||||
std::string ascii_text = "This is a test";
|
||||
std::string utf8_text = "más sábado miércoles";
|
||||
@@ -57,6 +57,12 @@ int main (int argc, char** argv)
|
||||
t.is (utf8_substr (ascii_text, 0, 2), "Th", "ASCII utf8_substr");
|
||||
t.is (utf8_substr (utf8_text, 0, 2), "má", "UTF8 utf8_substr");
|
||||
|
||||
// unsigned int utf8_width (const std::string&);
|
||||
t.is (utf8_width ("one"), 3, "utf8_width 'one' --> 3");
|
||||
t.is (utf8_width ("más sábado miércoles"), 20, "utf8_width 'más sábado miércoles' --> 20");
|
||||
t.is (utf8_width ("改变各种颜色"), 12, "utf8_width '改变各种颜色' --> 12");
|
||||
t.is (utf8_width ("改"), 2, "utf8_width '改' --> 2");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user