From: Max Kellermann Date: Tue, 20 Oct 2009 06:00:12 +0000 (+0200) Subject: hscroll: simplify the code with utf8_cut_width() X-Git-Tag: release-0.16~67 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=d5ad6e7e070c54b213c1f14aa6e89ab0f371dd97;p=ncmpc.git hscroll: simplify the code with utf8_cut_width() Concatenate the input string twice, and select a substring from that large string. --- diff --git a/src/hscroll.c b/src/hscroll.c index 109293d..ace5bde 100644 --- a/src/hscroll.c +++ b/src/hscroll.c @@ -28,32 +28,24 @@ strscroll(struct hscroll *hscroll, const char *str, const char *separator, unsigned width) { gchar *tmp, *buf; - gsize len, size, ulen; assert(hscroll != NULL); assert(str != NULL); assert(separator != NULL); - if (hscroll->offset == 0) - return g_strdup(str); - /* create a buffer containing the string and the separator */ - tmp = replace_locale_to_utf8(g_strconcat(str, separator, NULL)); - len = utf8_width(tmp); + tmp = replace_locale_to_utf8(g_strconcat(str, separator, + str, separator, NULL)); - if (hscroll->offset >= len) + if (hscroll->offset >= (unsigned)g_utf8_strlen(tmp, -1) / 2) hscroll->offset = 0; /* create the new scrolled string */ - size = width+1; - 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); + buf = g_utf8_offset_to_pointer(tmp, hscroll->offset); + utf8_cut_width(buf, width); + /* convert back to locale */ + buf = utf8_to_locale(buf); g_free(tmp); - tmp = utf8_to_locale(buf); - g_free(buf); - return tmp; + return buf; }