talons

Fork of Claws Mail https://www.claws-mail
Log | Files | Refs | README | LICENSE

commit 73e0a2e64074bab0b6abfde58fdf569b183dde5d
parent 57e0b0d5d8b901d82155af0a313ba7ab94b52173
Author: Andrej Kacian <ticho@claws-mail.org>
Date:   Wed, 30 Mar 2016 18:21:08 +0200

Rewrite unfold_line() to handle UTF8 line breaks.

This closes bug #3629 - Invalid subject can distort
message list view

Diffstat:
Msrc/common/utils.c | 42++++++++++++++++++++++++++++++------------
1 file changed, 30 insertions(+), 12 deletions(-)

diff --git 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; + } } }