# HG changeset patch # User Sebastien Decugis # Date 1360853935 -3600 # Node ID 6a4d08e239bdfec8f27a7a1948c8a5bc5dcb7287 # Parent c7bf1a7a4e90b4d8fb8a1a332f14aa69c36d2d1a Fix UTF8Dump function for the case of truncating UTF8 multibyte sequence, thanks Thomas for the algorithm. diff -r c7bf1a7a4e90 -r 6a4d08e239bd libfdproto/dictionary_functions.c --- a/libfdproto/dictionary_functions.c Thu Feb 14 15:43:36 2013 +0100 +++ b/libfdproto/dictionary_functions.c Thu Feb 14 15:58:55 2013 +0100 @@ -227,7 +227,19 @@ char * end = strchr(ret, '\0'); - + while (end > ret) { + end--; + char b = *end; + /* after the position pointed by end, we have only \0s */ + if ((b & 0x80) == 0) { + break; /* this is a single byte char, no problem */ + } else { + /* this byte is start or cont. of multibyte sequence, as we do not know the next byte we need to delete it. */ + *end = '\0'; + if (b & 0x40) + break; /* This was a start byte, we can stop the loop */ + } + } } return ret; }