diff options
author | Timo Sirainen <cras@irssi.org> | 2004-01-19 19:13:15 +0000 |
---|---|---|
committer | cras <cras@dbcabf3a-b0e7-0310-adc4-f8d773084564> | 2004-01-19 19:13:15 +0000 |
commit | 713e45a977b6177ec3f93cf5ca72e7694081a2e8 (patch) | |
tree | d6efca21a1e3a4097dfd0d61086bcb80e143465c | |
parent | 25a38d83a35afd46d503df2fc32c507a44793dc8 (diff) | |
download | irssi-713e45a977b6177ec3f93cf5ca72e7694081a2e8.zip |
UTF-8 support to format_get_length() and format_real_length(). patch by
Jean-Yves Lefort
git-svn-id: http://svn.irssi.org/repos/irssi/trunk@3209 dbcabf3a-b0e7-0310-adc4-f8d773084564
-rw-r--r-- | src/fe-common/core/formats.c | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/src/fe-common/core/formats.c b/src/fe-common/core/formats.c index 22ed82be..235bc60c 100644 --- a/src/fe-common/core/formats.c +++ b/src/fe-common/core/formats.c @@ -297,14 +297,44 @@ void format_create_dest_tag(TEXT_DEST_REC *dest, void *server, window_find_closest(server, target, level); } +static gboolean term_is_utf8 (void) +{ + const char *charset; + + charset = settings_get_str("term_type"); + if (*charset) + return ! g_strcasecmp(charset, "utf-8"); + + return g_get_charset(&charset); +} + +static int advance (char const **str, gboolean utf8) +{ + if (utf8) { + gunichar c; + + c = g_utf8_get_char(*str); + *str = g_utf8_next_char(*str); + + return g_unichar_iswide(c) ? 2 : 1; + } else { + *str += 1; + + return 1; + } +} + /* Return length of text part in string (ie. without % codes) */ int format_get_length(const char *str) { GString *tmp; int len; + gboolean utf8; g_return_val_if_fail(str != NULL, 0); + utf8 = term_is_utf8() && g_utf8_validate(str, -1, NULL); + tmp = g_string_new(NULL); len = 0; while (*str != '\0') { @@ -321,8 +351,7 @@ int format_get_length(const char *str) len++; } - len++; - str++; + len += advance(&str, utf8); } g_string_free(tmp, TRUE); @@ -336,10 +365,13 @@ int format_real_length(const char *str, int len) { GString *tmp; const char *start; + gboolean utf8; g_return_val_if_fail(str != NULL, 0); g_return_val_if_fail(len >= 0, 0); + utf8 = term_is_utf8() && g_utf8_validate(str, -1, NULL); + start = str; tmp = g_string_new(NULL); while (*str != '\0' && len > 0) { @@ -358,8 +390,7 @@ int format_real_length(const char *str, int len) } } - len--; - str++; + len -= advance(&str, utf8); } g_string_free(tmp, TRUE); |