diff options
author | Max Kellermann <max@duempel.org> | 2008-09-18 13:18:16 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2008-09-18 13:18:16 +0200 |
commit | cfa2f7b625ed645257897f94f258832f076ee6b0 (patch) | |
tree | e3b87ffbef631722fd2aec81742707aab94dcb75 /src | |
parent | a67a7dd81bf2f14afbf6b8cd0ae50d7ecdf9fec5 (diff) | |
download | mpd-cfa2f7b625ed645257897f94f258832f076ee6b0.tar.gz mpd-cfa2f7b625ed645257897f94f258832f076ee6b0.tar.xz mpd-cfa2f7b625ed645257897f94f258832f076ee6b0.zip |
added support for wide characters
This patch was taken from bug 476, author unknown
(http://www.musicpd.org/mantis/view.php?id=476). It changes the
definition of my_strlen(): instead of returning the number of UTF-8
characters, it counts the number of terminal cells occupied by a
string.
Diffstat (limited to 'src')
-rw-r--r-- | src/support.c | 16 | ||||
-rw-r--r-- | src/support.h | 5 |
2 files changed, 17 insertions, 4 deletions
diff --git a/src/support.c b/src/support.c index 949c8dc52..ad0f93bd2 100644 --- a/src/support.c +++ b/src/support.c @@ -36,9 +36,19 @@ static gboolean noconvert = TRUE; size_t my_strlen(const char *str) { - if (g_utf8_validate(str, -1, NULL)) - return g_utf8_strlen(str, -1); - else + if (g_utf8_validate(str, -1, NULL)) { + size_t len = g_utf8_strlen(str, -1); + size_t width = 0; + gunichar c; + + while (len--) { + c = g_utf8_get_char(str); + width += g_unichar_iswide(c) ? 2 : 1; + str += g_unichar_to_utf8(c, NULL); + } + + return width; + } else return strlen(str); } diff --git a/src/support.h b/src/support.h index b80b9c8a2..3377f9a80 100644 --- a/src/support.h +++ b/src/support.h @@ -28,8 +28,11 @@ void charset_init(gboolean disable); char *utf8_to_locale(const char *str); char *locale_to_utf8(const char *str); -/* number of characters in str */ +/** + * Returns the number of terminal cells occupied by this string. + */ size_t my_strlen(const char *str); + /* number of bytes in str */ size_t my_strsize(char *str); |