diff --git a/src/utf8.cpp b/src/utf8.cpp index 8c6b8d7f4..6620f9e5e 100644 --- a/src/utf8.cpp +++ b/src/utf8.cpp @@ -28,6 +28,7 @@ #define L10N // Localization complete. #include +#include #include #include @@ -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) { diff --git a/src/utf8.h b/src/utf8.h index 3b67f2ab8..a09349274 100644 --- a/src/utf8.h +++ b/src/utf8.h @@ -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); diff --git a/test/utf8.t.cpp b/test/utf8.t.cpp index 487933548..3885408e8 100644 --- a/test/utf8.t.cpp +++ b/test/utf8.t.cpp @@ -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; }