Code

hscroll: work with UTF-8 strings
authorMax Kellermann <max@duempel.org>
Tue, 20 Oct 2009 05:59:14 +0000 (07:59 +0200)
committerMax Kellermann <max@duempel.org>
Tue, 20 Oct 2009 05:59:14 +0000 (07:59 +0200)
Convert the input strings to UTF-8 before applying UTF-8 functions on
them.  Convert back to the locale before returning.

src/hscroll.c

index 481683c847a8f490820a8b6b7991a3e045c45d21..109293d3364254666aae645fb1c1c756fa739bf3 100644 (file)
@@ -28,7 +28,7 @@ strscroll(struct hscroll *hscroll, const char *str, const char *separator,
          unsigned width)
 {
        gchar *tmp, *buf;
-       gsize len, size;
+       gsize len, size, ulen;
 
        assert(hscroll != NULL);
        assert(str != NULL);
@@ -38,7 +38,7 @@ strscroll(struct hscroll *hscroll, const char *str, const char *separator,
                return g_strdup(str);
 
        /* create a buffer containing the string and the separator */
-       tmp = g_strconcat(str, separator, NULL);
+       tmp = replace_locale_to_utf8(g_strconcat(str, separator, NULL));
        len = utf8_width(tmp);
 
        if (hscroll->offset >= len)
@@ -46,19 +46,14 @@ strscroll(struct hscroll *hscroll, const char *str, const char *separator,
 
        /* create the new scrolled string */
        size = width+1;
-       if (g_utf8_validate(tmp, -1, NULL) ) {
-               size_t ulen;
-               buf = g_malloc(size*6);// max length of utf8 char is 6
-               g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp, hscroll->offset), size);
-               if( (ulen = g_utf8_strlen(buf, -1)) < width )
-                       g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1);
-       } else {
-               buf = g_malloc(size);
-               g_strlcpy(buf, tmp + hscroll->offset, size);
-               if (strlen(buf) < (size_t)width)
-                       g_strlcat(buf, tmp, size);
-       }
+       buf = g_malloc(size * 6);// max length of utf8 char is 6
+       g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,
+                                                    hscroll->offset), size);
+       if ((ulen = g_utf8_strlen(buf, -1)) < width)
+               g_utf8_strncpy(buf + strlen(buf), tmp, size - ulen - 1);
 
        g_free(tmp);
-       return buf;
+       tmp = utf8_to_locale(buf);
+       g_free(buf);
+       return tmp;
 }