Better utf-8 normalization (bug 14172)

This commit is contained in:
rim
2018-07-29 09:22:48 +03:00
committed by Landry Breuil
parent d472fec051
commit b89684865d

View File

@@ -22,6 +22,7 @@
#include <glib-object.h> #include <glib-object.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gmodule.h>
#include "task-manager.h" #include "task-manager.h"
#ifdef HAVE_WNCK #ifdef HAVE_WNCK
@@ -125,12 +126,44 @@ setting_changed (GObject *object, GParamSpec *pspec __unused, XtmTaskManager *ma
static gchar * static gchar *
pretty_cmdline (gchar *cmdline, gchar *comm) pretty_cmdline (gchar *cmdline, gchar *comm)
{ {
/* Use the printable range of 0x20-0x7E */ gunichar c;
const gchar *valid_chars = " !\"#$%&'()*+,-./0123456789:;<=>?@" gchar *ch, *text_max, *text = g_strstrip (g_strdup (cmdline));
"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" gsize csize, text_size = (gsize)strlen (text);
"abcdefghijklmnopqrstuvwxyz{|}~";
gchar *text = g_strstrip (g_strcanon (g_strdup (cmdline), valid_chars, ' ')); /* UTF-8 normalize. */
gsize text_size = (gsize)strlen (text); 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) if (!full_cmdline && text_size > 3)
{ {
/* Shorten full path to commands and wine applications */ /* 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); gchar *p = g_strstr_len (text, (gssize)text_size, comm);
if (p != NULL) if (p != NULL)
{ {
g_strlcpy (text, p, text_size); memmove (text, p, text_size);
} }
} }
} }