diff --git a/include/ftxui/screen/string.hpp b/include/ftxui/screen/string.hpp index 15849a4e..ad312b5b 100644 --- a/include/ftxui/screen/string.hpp +++ b/include/ftxui/screen/string.hpp @@ -12,7 +12,9 @@ std::wstring to_wstring(T s) { return to_wstring(std::to_string(s)); } +int wchar_width(char32_t); int wchar_width(wchar_t); +int wchar_width_cjk(char32_t); int wchar_width_cjk(wchar_t); int wstring_width(const std::wstring&); int wstring_width_cjk(const std::wstring&); diff --git a/src/ftxui/screen/wcwidth.cpp b/src/ftxui/screen/wcwidth.cpp index 7791a3e4..a23ce3bf 100644 --- a/src/ftxui/screen/wcwidth.cpp +++ b/src/ftxui/screen/wcwidth.cpp @@ -67,12 +67,12 @@ namespace ftxui { namespace { struct interval { - int first; - int last; + char32_t first; + char32_t last; }; /* auxiliary function for binary search in interval table */ -int bisearch(wchar_t ucs, const struct interval* table, int max) { +int bisearch(char32_t ucs, const struct interval* table, int max) { int min = 0; int mid; @@ -120,11 +120,11 @@ int bisearch(wchar_t ucs, const struct interval* table, int max) { * ISO 8859-1 and WGL4 characters, Unicode control characters, * etc.) have a column width of 1. * - * This implementation assumes that wchar_t characters are encoded + * This implementation assumes that char32_t characters are encoded * in ISO 10646. */ -int wchar_width(wchar_t ucs) { +int wchar_width(char32_t ucs) { /* sorted list of non-overlapping intervals of non-spacing characters */ /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */ static const struct interval combining[] = { @@ -214,7 +214,7 @@ int wchar_width(wchar_t ucs) { * the traditional terminal character-width behaviour. It is not * otherwise recommended for general use. */ -int wchar_width_cjk(wchar_t ucs) { +int wchar_width_cjk(char32_t ucs) { /* sorted list of non-overlapping intervals of East Asian Ambiguous * characters, generated by "uniset +WIDTH-A -cat=Me -cat=Mn -cat=Cf c" */ static const struct interval ambiguous[] = { @@ -278,6 +278,14 @@ int wchar_width_cjk(wchar_t ucs) { return wchar_width(ucs); } +int wchar_width(wchar_t ucs) { + return wchar_width(char32_t(ucs)); +} + +int wchar_width_cjk(wchar_t ucs) { + return wchar_width_cjk(char32_t(ucs)); +} + int wstring_width(const std::wstring& text) { int width = 0;