comparison mcabber/src/utf8.c @ 327:53dfe6aa6a23

Fix previous fix... :-(
author Mikael Berthe <mikael@lilotux.net>
date Sun, 17 Jul 2005 15:40:07 +0100
parents 1bc374915787
children 33b8e801ffa6
comparison
equal deleted inserted replaced
326:1bc374915787 327:53dfe6aa6a23
11 * 11 *
12 * Note: it is up to the caller to free the returned string 12 * Note: it is up to the caller to free the returned string
13 */ 13 */
14 char *utf8_decode(const char *src) 14 char *utf8_decode(const char *src)
15 { 15 {
16 unsigned char *ret; 16 unsigned char *ret, *aux;
17 17
18 if (!src) return NULL; 18 if (!src) return NULL;
19 19
20 ret = calloc(1, strlen(src) + 1); 20 aux = ret = calloc(1, strlen(src) + 1);
21 21
22 while (*src) { 22 while (*src) {
23 unsigned char lead = *src++; 23 unsigned char lead = *src++;
24 if ((lead & 0xe0) == 0xc0) { 24 if ((lead & 0xe0) == 0xc0) {
25 unsigned char ch2 = *src++; 25 unsigned char ch2 = *src++;
26 *ret = ((lead & 0x1f) << 6) | (ch2 & 0x3f); 26 *aux = ((lead & 0x1f) << 6) | (ch2 & 0x3f);
27 } else { 27 } else {
28 *ret = lead; 28 *aux = lead;
29 } 29 }
30 ret++; 30 aux++;
31 } 31 }
32 32
33 return (char*)ret; 33 return (char*)ret;
34 } 34 }
35 35
41 * 41 *
42 * Note: it is up to the caller to free the returned string 42 * Note: it is up to the caller to free the returned string
43 */ 43 */
44 char *utf8_encode(const char *src) 44 char *utf8_encode(const char *src)
45 { 45 {
46 unsigned char *ret; 46 unsigned char *ret, *aux;
47 47
48 if (!src) return NULL; 48 if (!src) return NULL;
49 49
50 ret = calloc(1, (strlen(src) * 2) + 1); 50 aux = ret = calloc(1, (strlen(src) * 2) + 1);
51 51
52 while (*src) { 52 while (*src) {
53 unsigned char ch = *src++; 53 unsigned char ch = *src++;
54 if (ch < 0x80U) { 54 if (ch < 0x80U) {
55 *ret++ = ch; 55 *aux++ = ch;
56 } else { /* if (ch < 0x800U) { */ 56 } else { /* if (ch < 0x800U) { */
57 *ret++ = 0xc0 | (ch >> 6); 57 *aux++ = 0xc0 | (ch >> 6);
58 *ret++ = 0x80 | (ch & 0x3f); 58 *aux++ = 0x80 | (ch & 0x3f);
59 } 59 }
60 } 60 }
61 61
62 return (char*)ret; 62 return (char*)ret;
63 } 63 }