Permalink
Browse files

Fixed work with utf8 strings

  • Loading branch information...
ZEN ZEN
ZEN authored and ZEN committed Jun 9, 2014
1 parent 31a12b8 commit 2c7c9d604b5a317ec7794e441c2c936055513a3c
Showing with 46 additions and 19 deletions.
  1. +46 −19 src/utils.c
@@ -68,25 +68,52 @@ gboolean is_hyperlink(gchar *text)
/* 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 (string->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 */

0 comments on commit 2c7c9d6

Please sign in to comment.