From b89684865d88bbb8399f70387cae9e8ae17d64d9 Mon Sep 17 00:00:00 2001 From: rim Date: Sun, 29 Jul 2018 09:22:48 +0300 Subject: [PATCH] Better utf-8 normalization (bug 14172) --- src/task-manager.c | 47 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/src/task-manager.c b/src/task-manager.c index 93f9122..8188de6 100644 --- a/src/task-manager.c +++ b/src/task-manager.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "task-manager.h" #ifdef HAVE_WNCK @@ -125,12 +126,44 @@ setting_changed (GObject *object, GParamSpec *pspec __unused, XtmTaskManager *ma static gchar * pretty_cmdline (gchar *cmdline, gchar *comm) { - /* Use the printable range of 0x20-0x7E */ - const gchar *valid_chars = " !\"#$%&'()*+,-./0123456789:;<=>?@" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" - "abcdefghijklmnopqrstuvwxyz{|}~"; - gchar *text = g_strstrip (g_strcanon (g_strdup (cmdline), valid_chars, ' ')); - gsize text_size = (gsize)strlen (text); + gunichar c; + gchar *ch, *text_max, *text = g_strstrip (g_strdup (cmdline)); + gsize csize, text_size = (gsize)strlen (text); + + /* UTF-8 normalize. */ + do { + for (ch = text, text_max = (text + text_size); + text_max > ch; + text_max = (text + text_size), ch = g_utf8_next_char(ch)) { + c = g_utf8_get_char_validated(ch, -1); /* If use (text_max - ch) - result is worse. */ + if ((gunichar)-2 == c) { + text_size = (gsize)(ch - text); + (*ch) = 0; + break; + } + if ((gunichar)-1 == c) { + (*ch) = ' '; + continue; + } + csize = (gsize)g_unichar_to_utf8(c, NULL); + + if (!g_unichar_isdefined(c) || + !g_unichar_isprint(c) || + (g_unichar_isspace(c) && (1 != csize || (' ' != (*ch) && ' ' != (*ch)))) || + g_unichar_ismark(c) || + g_unichar_istitle(c) || + g_unichar_iswide(c) || + g_unichar_iszerowidth(c) || + g_unichar_iscntrl(c)) { + if (text_max < (ch + csize)) + break; + memmove(ch, (ch + csize), (gsize)(text_max - (ch + csize))); + text_size -= csize; + } + } + text[text_size] = 0; + } while (!g_utf8_validate(text, (gssize)text_size, NULL)); + if (!full_cmdline && text_size > 3) { /* Shorten full path to commands and wine applications */ @@ -139,7 +172,7 @@ pretty_cmdline (gchar *cmdline, gchar *comm) gchar *p = g_strstr_len (text, (gssize)text_size, comm); if (p != NULL) { - g_strlcpy (text, p, text_size); + memmove (text, p, text_size); } } }