chiark / gitweb /
Fiddle with CSS+HTML in effort to get more consistent buttons
[disorder] / lib / charset.c
index fbcf2753b011747e9cf1bf6554c7de1c4ffed227..3c5f88b79cae97c747af797d65df9c384c7d10e2 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004, 2005 Richard Kettlewell
+ * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  */
 /** @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);