From: Andrej Kacian Date: Wed, 30 Mar 2016 16:21:08 +0000 (+0200) Subject: Rewrite unfold_line() to handle UTF8 line breaks. X-Git-Tag: 3.14.0~134 X-Git-Url: http://git.claws-mail.org/?p=claws.git;a=commitdiff_plain;h=73e0a2e64074bab0b6abfde58fdf569b183dde5d Rewrite unfold_line() to handle UTF8 line breaks. This closes bug #3629 - Invalid subject can distort message list view --- diff --git a/src/common/utils.c b/src/common/utils.c index ab063cee3..5e24bc649 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -996,19 +996,37 @@ void remove_space(gchar *str) void unfold_line(gchar *str) { - register gchar *p = str; - register gint spc; + register gchar *ch; + register gunichar c; + register gint len; - while (*p) { - if (*p == '\n' || *p == '\r') { - *p++ = ' '; - spc = 0; - while (g_ascii_isspace(*(p + spc))) - spc++; - if (spc) - memmove(p, p + spc, strlen(p + spc) + 1); - } else - p++; + ch = str; /* iterator for source string */ + + while (*ch != 0) { + c = g_utf8_get_char_validated(ch, -1); + + if (c < 0) { + /* non-unicode byte, move past it */ + ch++; + continue; + } + + len = g_unichar_to_utf8(c, NULL); + + if (!g_unichar_isdefined(c) || !g_unichar_isprint(c) || + g_unichar_isspace(c)) { + /* replace anything bad or whitespacey with a single space */ + *ch = ' '; + ch++; + if (len > 1) { + /* move rest of the string forwards, since we just replaced + * a multi-byte sequence with one byte */ + memmove(ch, ch + len-1, strlen(ch + len-1) + 1); + } + } else { + /* A valid unicode character, copy it. */ + ch += len; + } } }