X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/4ecbdbd99dea3236c3c6d5ea5401a08c56de5d3c..49e57291d78de2b583d6f2197ae6f9d9ff05e456:/lib/charset.c diff --git a/lib/charset.c b/lib/charset.c index fbcf275..3c5f88b 100644 --- a/lib/charset.c +++ b/lib/charset.c @@ -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 @@ -19,11 +19,9 @@ */ /** @file lib/charset.c @brief Character set conversion */ -#include -#include "types.h" +#include "common.h" #include -#include #include #include @@ -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);