X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/299a14ee0d61b7994dc4ce5246b55b4d75fdcb2e..4942ee7d61bf22ba38bf026c7d05028cb7db0d54:/lib/charset.c diff --git a/lib/charset.c b/lib/charset.c index fbcf275..e9c503e 100644 --- a/lib/charset.c +++ b/lib/charset.c @@ -1,29 +1,25 @@ /* * 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 + * 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 - * the Free Software Foundation; either version 2 of the License, or + * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * along with this program. If not, see . */ /** @file lib/charset.c @brief Character set conversion */ -#include -#include "types.h" +#include "common.h" #include -#include #include #include @@ -115,20 +111,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);