chiark / gitweb /
Fiddle with CSS+HTML in effort to get more consistent buttons
[disorder] / lib / charset.c
index bacaea9fa01d372a14f5ae47becc30191956ac8e..3c5f88b79cae97c747af797d65df9c384c7d10e2 100644 (file)
  */
 /** @file lib/charset.c @brief Character set conversion */
 
-#include <config.h>
-#include "types.h"
+#include "common.h"
 
 #include <iconv.h>
-#include <string.h>
 #include <errno.h>
 #include <langinfo.h>
 
@@ -115,20 +113,22 @@ const char *truncate_for_display(const char *s, long max) {
   uint32_t *s32;
   size_t l32, cut;
   utf32_iterator it;
+  long graphemes;
 
   /* Convert to UTF-32 for processing */
   if(!(s32 = utf8_to_utf32(s, strlen(s), &l32)))
     return 0;
   it = utf32_iterator_new(s32, l32);
   cut = l32;
-  while(max && utf32_iterator_where(it) < l32) {
+  graphemes = 0;                        /* # of graphemes left of it */
+  while(graphemes <= max && utf32_iterator_where(it) < l32) {
+    if(graphemes == max - 1)
+      cut = utf32_iterator_where(it);
     utf32_iterator_advance(it, 1);
     if(utf32_iterator_grapheme_boundary(it))
-      --max;
-    if(max == 1)
-      cut = utf32_iterator_where(it);
+      ++graphemes;
   }
-  if(max == 0) {                        /* we need to cut */
+  if(graphemes > max) {                 /* we need to cut */
     s32[cut] = 0x2026;                  /* HORIZONTAL ELLIPSIS */
     l32 = cut + 1;
     s = utf32_to_utf8(s32, l32, 0);