comparison mcabber/src/utf8.c @ 34:f78ffe7ce43d

[/trunk] Changeset 50 by mikael * Fix a bug in the UTF-8 encoder.
author mikael
date Mon, 04 Apr 2005 18:00:51 +0000
parents e88b15cbf2de
children f937475e9baa
comparison
equal deleted inserted replaced
33:bb9172f2cbf1 34:f78ffe7ce43d
39 * Note: it is up to the caller to free the returned string 39 * Note: it is up to the caller to free the returned string
40 */ 40 */
41 char *utf8_encode(const char *src) 41 char *utf8_encode(const char *src)
42 { 42 {
43 char *ret = calloc(1, (strlen(src) * 2) + 1); 43 char *ret = calloc(1, (strlen(src) * 2) + 1);
44 char *aux = ret; 44 unsigned char *aux = ret;
45 45
46 while (*src) { 46 while (*src) {
47 unsigned char ch = *src++; 47 unsigned char ch = *src++;
48 if (ch < 0x80) { 48 if (ch < 0x80U) {
49 *aux = ch; 49 *aux++ = ch;
50 } else { /* if (ch < 0x800) { */ 50 } else if (ch < 0x800U) { /* if (ch < 0x800) { */
51 *aux++ = 0xc0 | (ch >> 6 & 0x1f); 51 *aux++ = 0xc0 | (ch >> 6);
52 *aux = 0xc0 | (0x80 | (ch & 0x3f)); 52 *aux++ = 0x80 | (ch & 0x3f);
53 } else {
54 *aux++ = 0xe0 | (ch >> 12);
55 *aux++ = 0x80 | ((ch >> 6) & 0x3f);
56 *aux++ = 0x80 | (ch & 0x3f);
53 } 57 }
54 aux++;
55 } 58 }
56 59
57 return ret; 60 return ret;
58 } 61 }