mcabber/src/utf8.c
changeset 48 f937475e9baa
parent 34 f78ffe7ce43d
child 49 18a03a69f5e4
equal deleted inserted replaced
47:7259a61e1a4b 48:f937475e9baa
    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   unsigned char *aux = ret;
    44   unsigned char *aux = (unsigned char*)ret;
    45 
    45 
    46   while (*src) {
    46   while (*src) {
    47     unsigned char ch = *src++;
    47     unsigned char ch = *src++;
    48     if (ch < 0x80U) {
    48     if (ch < 0x80U) {
    49       *aux++ = ch;
    49       *aux++ = ch;
    50     } else if (ch < 0x800U) {			/* if (ch < 0x800) { */
    50     } else {  /* if (ch < 0x800U) { */
    51       *aux++ = 0xc0 | (ch >> 6);
    51       *aux++ = 0xc0 | (ch >> 6);
    52       *aux++ = 0x80 | (ch & 0x3f);
       
    53     } else {
       
    54       *aux++ = 0xe0 | (ch >> 12);
       
    55       *aux++ = 0x80 | ((ch >> 6) & 0x3f);
       
    56       *aux++ = 0x80 | (ch & 0x3f);
    52       *aux++ = 0x80 | (ch & 0x3f);
    57     }
    53     }
    58   }
    54   }
    59 
    55 
    60   return ret;
    56   return ret;