Patch for ClipIt to fix work with utf8 strings

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
--- clipit-1.4.1.orig/src/utils.c 2011-05-27 10:10:07.000000000 +0300
+++ clipit-1.4.1/src/utils.c 2014-06-09 12:00:23.402616765 +0300
@@ -68,25 +68,52 @@
/* Ellipsize a string according to the settings */
GString *ellipsize_string(GString *string)
{
- if (string->len > prefs.item_length)
- {
- switch (prefs.ellipsize)
- {
- case PANGO_ELLIPSIZE_START:
- string = g_string_erase(string, 0, string->len-(prefs.item_length));
- string = g_string_prepend(string, "...");
- break;
- case PANGO_ELLIPSIZE_MIDDLE:
- string = g_string_erase(string, (prefs.item_length/2), string->len-(prefs.item_length));
- string = g_string_insert(string, (string->len/2), "...");
- break;
- case PANGO_ELLIPSIZE_END:
- string = g_string_truncate(string, prefs.item_length);
- string = g_string_append(string, "...");
- break;
- }
- }
- return string;
+ gboolean is_utf8 = g_utf8_validate(string->str, -1, NULL);
+ gint str_len = is_utf8 ? g_utf8_strlen(string->str, -1) : string->len;
+ if (str_len > prefs.item_length)
+ {
+ switch (prefs.ellipsize)
+ {
+ case PANGO_ELLIPSIZE_START:
+ if (is_utf8)
+ {
+ gchar *end = g_utf8_substring(string->str, str_len - prefs.item_length, str_len);
+ g_string_printf(string, "...%s", end);
+ }
+ else
+ {
+ string = g_string_erase(string, 0, str_len - (prefs.item_length));
+ string = g_string_prepend(string, "...");
+ }
+ break;
+ case PANGO_ELLIPSIZE_MIDDLE:
+ if (is_utf8)
+ {
+ gchar *start = g_utf8_substring(string->str, 0, prefs.item_length/2);
+ gchar *end = g_utf8_substring(string->str, str_len - prefs.item_length/2, str_len);
+ g_string_printf(string, "%s...%s", start, end);
+ }
+ else
+ {
+ string = g_string_erase(string, (prefs.item_length/2), str_len - (prefs.item_length));
+ string = g_string_insert(string, (string->len/2), "...");
+ }
+ break;
+ case PANGO_ELLIPSIZE_END:
+ if (is_utf8)
+ {
+ gchar *buff = g_utf8_substring(string->str, 0, prefs.item_length);
+ g_string_assign(string, buff);
+ }
+ else
+ {
+ string = g_string_truncate(string, prefs.item_length);
+ }
+ string = g_string_append(string, "...");
+ break;
+ }
+ }
+ return string;
}
/* Remove newlines from string */