diff options
-rw-r--r-- | src/fe-common/core/utf8.c | 64 | ||||
-rw-r--r-- | src/fe-common/core/utf8.h | 14 | ||||
-rw-r--r-- | src/fe-text/gui-entry.c | 33 |
3 files changed, 19 insertions, 92 deletions
diff --git a/src/fe-common/core/utf8.c b/src/fe-common/core/utf8.c index 6cd809f8..2d07ea8e 100644 --- a/src/fe-common/core/utf8.c +++ b/src/fe-common/core/utf8.c @@ -24,67 +24,3 @@ #include "module.h" -int strlen_utf8(const char *str) -{ - const unsigned char *p = (const unsigned char *) str; - int len; - unichar chr_r; - - len = 0; - while (*p != '\0') { - chr_r = g_utf8_get_char_validated(p, -1); - if (chr_r & 0x80000000) - break; - len++; - p = g_utf8_next_char(p); - } - return len; -} - -void utf8_to_utf16(const char *str, unichar *out) -{ - const unsigned char *p = (const unsigned char *) str; - unichar result; - - while (*p != '\0') { - result = g_utf8_get_char_validated(p, -1); - if (result & 0x80000000) - break; - - p = g_utf8_next_char(p); - *out++ = result; - } - - *out = '\0'; -} - -void utf16_to_utf8(const unichar *str, char *out) -{ - int len; - - while (*str != '\0') { - len = g_unichar_to_utf8(*str, out); - out += len; - - str++; - } - *out = '\0'; -} - -void utf16_to_utf8_with_pos(const unichar *str, int spos, char *out, int *opos) -{ - int len; - const unichar *sstart = str; - char *ostart = out; - - *opos = 0; - while (*str != '\0') { - len = g_unichar_to_utf8(*str, out); - out += len; - - str++; - if(str - sstart == spos) - *opos = out - ostart; - } - *out = '\0'; -} diff --git a/src/fe-common/core/utf8.h b/src/fe-common/core/utf8.h index 3bb22ff0..163f1717 100644 --- a/src/fe-common/core/utf8.h +++ b/src/fe-common/core/utf8.h @@ -1,20 +1,6 @@ #ifndef __UTF8_H #define __UTF8_H -/* 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. 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); - -/* unichar -> UTF-8 string with position transformed. The NUL is copied as well. - Make sure out is at least 6 x length of str. */ -void utf16_to_utf8_with_pos(const unichar *str, int spos, char *out, int *opos); - /* XXX I didn't check the encoding range of big5+. This is standard big5. */ #define is_big5_los(lo) (0x40 <= (lo) && (lo) <= 0x7E) /* standard */ #define is_big5_lox(lo) (0x80 <= (lo) && (lo) <= 0xFE) /* extended */ diff --git a/src/fe-text/gui-entry.c b/src/fe-text/gui-entry.c index 9ed21cb3..13cbfafd 100644 --- a/src/fe-text/gui-entry.c +++ b/src/fe-text/gui-entry.c @@ -400,10 +400,10 @@ char *gui_entry_get_text(GUI_ENTRY_REC *entry) g_return_val_if_fail(entry != NULL, NULL); - buf = g_malloc(entry->text_len*6 + 1); if (entry->utf8) - utf16_to_utf8(entry->text, buf); + buf = g_ucs4_to_utf8(entry->text, -1, NULL, NULL, NULL); else { + buf = g_malloc(entry->text_len*6 + 1); if (term_type == TERM_TYPE_BIG5) unichars_to_big5(entry->text, buf); else @@ -420,10 +420,11 @@ char *gui_entry_get_text_and_pos(GUI_ENTRY_REC *entry, int *pos) g_return_val_if_fail(entry != NULL, NULL); - buf = g_malloc(entry->text_len*6 + 1); - if (entry->utf8) - utf16_to_utf8_with_pos(entry->text, entry->pos, buf, pos); - else { + if (entry->utf8) { + buf = g_ucs4_to_utf8(entry->text, -1, NULL, NULL, NULL); + *pos = g_utf8_offset_to_pointer(buf, entry->pos) - buf; + } else { + buf = g_malloc(entry->text_len*6 + 1); if(term_type==TERM_TYPE_BIG5) unichars_to_big5_with_pos(entry->text, entry->pos, buf, pos); else @@ -440,15 +441,17 @@ void gui_entry_insert_text(GUI_ENTRY_REC *entry, const char *str) { unichar chr; int i, len; + const char *ptr; g_return_if_fail(entry != NULL); g_return_if_fail(str != NULL); gui_entry_redraw_from(entry, entry->pos); - if (entry->utf8) - len = strlen_utf8(str); - else if (term_type == TERM_TYPE_BIG5) + if (entry->utf8) { + g_utf8_validate(str, -1, &ptr); + len = g_utf8_pointer_to_offset(str, ptr); + } else if (term_type == TERM_TYPE_BIG5) len = strlen_big5(str); else len = strlen(str); @@ -468,9 +471,11 @@ void gui_entry_insert_text(GUI_ENTRY_REC *entry, const char *str) entry->text[entry->pos + i] = str[i]; } } else { - chr = entry->text[entry->pos+len]; - utf8_to_utf16(str, entry->text+entry->pos); - entry->text[entry->pos+len] = chr; + ptr = str; + for (i = 0; i < len; i++) { + entry->text[entry->pos + i] = g_utf8_get_char(ptr); + ptr = g_utf8_next_char(ptr); + } } entry->text_len += len; @@ -516,10 +521,10 @@ char *gui_entry_get_cutbuffer(GUI_ENTRY_REC *entry) if (entry->cutbuffer == NULL) return NULL; - buf = g_malloc(entry->cutbuffer_len*6 + 1); if (entry->utf8) - utf16_to_utf8(entry->cutbuffer, buf); + buf = g_ucs4_to_utf8(entry->cutbuffer, -1, NULL, NULL, NULL); else { + buf = g_malloc(entry->cutbuffer_len*6 + 1); if (term_type == TERM_TYPE_BIG5) unichars_to_big5(entry->cutbuffer, buf); else |