patches/eel-01-multibyte-bookmark-menu.diff
author laca
Tue, 16 Aug 2005 02:39:40 +0000
changeset 5542 175810cfb178
parent 5518 235b0b4aa324
child 11349 fcc038811614
permissions -rw-r--r--
delete context of ChangeLog entry

--- eel-2.10.1/ChangeLog	2005-04-11 13:06:51.000000000 +0530
+++ eel-2.10.1-new/ChangeLog	2005-05-13 15:10:25.055781000 +0530
@@ -0,0 +0,8 @@
+2005-05-13  Suresh Chandrasekharan <[email protected]>
+
+	Fix for bugster #6184582 [cinnabar]: nautilus truncates certain
+	i18nalized bookmark entries
+
+	* eel/eel-string.c (eel_str_middle_truncate): Logic for truncating
+	utf8 strings right.
+
--- eel-2.10.1/eel/eel-string.c	2002-03-19 03:05:08.000000000 +0530
+++ eel-2.10.1-new/eel/eel-string.c	2005-05-13 15:09:48.219632000 +0530
@@ -28,6 +28,8 @@
 #include <errno.h>
 #include <locale.h>
 #include <stdlib.h>
+#include <ctype.h>
+#include <glib.h>
 
 #if !defined (EEL_OMIT_SELF_CHECK)
 #include "eel-lib-self-check-functions.h"
@@ -531,9 +533,10 @@ eel_str_middle_truncate (const char *str
 			      guint truncate_length)
 {
 	char *truncated;
-	guint length;
+	guint length, i;
 	guint num_left_chars;
 	guint num_right_chars;
+	gboolean is_ascii = TRUE, valid_utf8 =  TRUE;
 
 	const char delimter[] = "...";
 	const guint delimter_length = strlen (delimter);
@@ -557,11 +560,41 @@ eel_str_middle_truncate (const char *str
 	if (length <= truncate_length) {
 		return g_strdup (string);
 	}
+	
+	for (i=0; i<length; i++) {
+		if (!isascii (string[i])) {
+			is_ascii = FALSE;
+			break;
+		}
+	}
+
+	if (!is_ascii && g_utf8_validate (string, -1, NULL)) {
+		valid_utf8 = TRUE;
+	}
 
 	/* Find the 'middle' where the truncation will occur. */
 	num_left_chars = (truncate_length - delimter_length) / 2;
+
+	if (valid_utf8 && !g_utf8_validate (string + num_left_chars, -1, NULL)) {
+		gchar *tc;
+		tc = g_utf8_find_next_char (string + num_left_chars, NULL);
+		if (tc) {
+			num_left_chars = (gint) (tc - string);
+		}
+	}
 	num_right_chars = truncate_length - num_left_chars - delimter_length + 1;
 
+	if (valid_utf8 && !g_utf8_validate (string + length - num_right_chars + 1, -1, NULL)) {
+		gchar *tc;
+		tc = g_utf8_find_prev_char (string, string + length - num_right_chars + 1);
+		if (tc) {
+			num_right_chars = strlen (tc) + 1;
+		}
+	}
+
+	if (valid_utf8)
+		truncate_length = num_left_chars + num_right_chars + delimter_length;
+
 	truncated = g_new (char, truncate_length + 1);
 
 	strncpy (truncated, string, num_left_chars);