1
0
mirror of https://github.com/irssi/irssi.git synced 2024-12-04 14:46:39 -05:00

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
This commit is contained in:
Timo Sirainen 2004-01-19 19:13:15 +00:00 committed by cras
parent 25a38d83a3
commit 713e45a977

View File

@ -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);