diff options
-rw-r--r-- | src/fe-common/core/utf8.c | 130 | ||||
-rw-r--r-- | src/fe-common/core/utf8.h | 7 | ||||
-rw-r--r-- | src/fe-text/gui-readline.c | 4 | ||||
-rw-r--r-- | src/fe-text/term-terminfo.c | 2 |
4 files changed, 15 insertions, 128 deletions
diff --git a/src/fe-common/core/utf8.c b/src/fe-common/core/utf8.c index 564eb98e..6cd809f8 100644 --- a/src/fe-common/core/utf8.c +++ b/src/fe-common/core/utf8.c @@ -24,74 +24,6 @@ #include "module.h" -#define UTF8_COMPUTE(Char, Mask, Len) \ - if (Char < 128) \ - { \ - Len = 1; \ - Mask = 0x7f; \ - } \ - else if ((Char & 0xe0) == 0xc0) \ - { \ - Len = 2; \ - Mask = 0x1f; \ - } \ - else if ((Char & 0xf0) == 0xe0) \ - { \ - Len = 3; \ - Mask = 0x0f; \ - } \ - else if ((Char & 0xf8) == 0xf0) \ - { \ - Len = 4; \ - Mask = 0x07; \ - } \ - else if ((Char & 0xfc) == 0xf8) \ - { \ - Len = 5; \ - Mask = 0x03; \ - } \ - else if ((Char & 0xfe) == 0xfc) \ - { \ - Len = 6; \ - Mask = 0x01; \ - } \ - else \ - Len = -1; - -#define UTF8_GET(Result, Chars, Count, Mask, Len) \ - (Result) = (Chars)[0] & (Mask); \ - for ((Count) = 1; (Count) < (Len); ++(Count)) \ - { \ - if (((Chars)[(Count)] & 0xc0) != 0x80) \ - { \ - (Result) = -1; \ - break; \ - } \ - (Result) <<= 6; \ - (Result) |= ((Chars)[(Count)] & 0x3f); \ - } - -int get_utf8_char(const unsigned char **ptr, int len, unichar *chr_r) -{ - int i, result, mask, chrlen; - - mask = 0; - UTF8_COMPUTE(**ptr, mask, chrlen); - if (chrlen == -1) - return -2; - - if (chrlen > len) - return -1; - - UTF8_GET(result, *ptr, i, mask, chrlen); - if (result == -1) - return -2; - - *chr_r = (unichar) result; - *ptr += chrlen-1; - return result; -} - int strlen_utf8(const char *str) { const unsigned char *p = (const unsigned char *) str; @@ -99,65 +31,27 @@ int strlen_utf8(const char *str) unichar chr_r; len = 0; - while (*p != '\0' && get_utf8_char(&p, 6, &chr_r) > 0) { + while (*p != '\0') { + chr_r = g_utf8_get_char_validated(p, -1); + if (chr_r & 0x80000000) + break; len++; - p++; + p = g_utf8_next_char(p); } return len; } -int utf16_char_to_utf8(unichar c, char *outbuf) -{ - int len, i, first; - - len = 0; - if (c < 0x80) { - first = 0; - len = 1; - } else if (c < 0x800) { - first = 0xc0; - len = 2; - } else if (c < 0x10000) { - first = 0xe0; - len = 3; - } else if (c < 0x200000) { - first = 0xf0; - len = 4; - } else if (c < 0x4000000) { - first = 0xf8; - len = 5; - } else { - first = 0xfc; - len = 6; - } - - if (outbuf) { - for (i = len - 1; i > 0; --i) { - outbuf[i] = (c & 0x3f) | 0x80; - c >>= 6; - } - outbuf[0] = c | first; - } - - return len; -} - void utf8_to_utf16(const char *str, unichar *out) { const unsigned char *p = (const unsigned char *) str; - int i, result, mask, len; + unichar result; while (*p != '\0') { - mask = 0; - UTF8_COMPUTE(*p, mask, len); - if (len == -1) - break; - - UTF8_GET(result, p, i, mask, len); - if (result == -1) - break; + result = g_utf8_get_char_validated(p, -1); + if (result & 0x80000000) + break; - p += len; + p = g_utf8_next_char(p); *out++ = result; } @@ -169,7 +63,7 @@ void utf16_to_utf8(const unichar *str, char *out) int len; while (*str != '\0') { - len = utf16_char_to_utf8(*str, out); + len = g_unichar_to_utf8(*str, out); out += len; str++; @@ -185,7 +79,7 @@ void utf16_to_utf8_with_pos(const unichar *str, int spos, char *out, int *opos) *opos = 0; while (*str != '\0') { - len = utf16_char_to_utf8(*str, out); + len = g_unichar_to_utf8(*str, out); out += len; str++; diff --git a/src/fe-common/core/utf8.h b/src/fe-common/core/utf8.h index 067a9df7..3bb22ff0 100644 --- a/src/fe-common/core/utf8.h +++ b/src/fe-common/core/utf8.h @@ -1,19 +1,12 @@ #ifndef __UTF8_H #define __UTF8_H -/* Returns -2 = invalid, -1 = need more data, otherwise unichar. */ -int get_utf8_char(const unsigned char **ptr, int len, unichar *chr_r); - /* Returns length of UTF8 string */ int strlen_utf8(const char *str); /* UTF-8 -> unichar string. The NUL is copied as well. */ void utf8_to_utf16(const char *str, unichar *out); -/* unichar -> UTF-8 string. outbuf must be at least 6 chars long. - Returns outbuf string length. */ -int utf16_char_to_utf8(unichar c, char *outbuf); - /* unichar -> UTF-8 string. The NUL is copied as well. Make sure out is at least 6 x length of str. */ void utf16_to_utf8(const unichar *str, char *out); diff --git a/src/fe-text/gui-readline.c b/src/fe-text/gui-readline.c index 65e35268..30e92fb7 100644 --- a/src/fe-text/gui-readline.c +++ b/src/fe-text/gui-readline.c @@ -295,7 +295,7 @@ static void paste_send(void) active_win->active); g_string_truncate(str, 0); } else if (active_entry->utf8) { - out[utf16_char_to_utf8(arr[i], out)] = '\0'; + out[g_unichar_to_utf8(arr[i], out)] = '\0'; g_string_append(str, out); } else if (term_type == TERM_TYPE_BIG5) { if (arr[i] > 0xff) @@ -490,7 +490,7 @@ static void sig_gui_key_pressed(gpointer keyp) } } else { /* need to convert to utf8 */ - str[utf16_char_to_utf8(key, str)] = '\0'; + str[g_unichar_to_utf8(key, str)] = '\0'; } if (strcmp(str, "^") == 0) { diff --git a/src/fe-text/term-terminfo.c b/src/fe-text/term-terminfo.c index 03087949..7cf61e20 100644 --- a/src/fe-text/term-terminfo.c +++ b/src/fe-text/term-terminfo.c @@ -389,7 +389,7 @@ static void term_addch_utf8(TERM_WINDOW *window, unichar chr) char buf[10]; int i, len; - len = utf16_char_to_utf8(chr, buf); + len = g_unichar_to_utf8(chr, buf); for (i = 0; i < len; i++) putc(buf[i], window->term->out); } |