From 75139a565adf4c55fe60772c3de10ca424e2c71f Mon Sep 17 00:00:00 2001 From: Ilia Maslakov Date: Tue, 6 Dec 2011 18:00:46 +0400 Subject: [PATCH] fixed function 'edit_get_prev_utf ' to obtain the correct previous utf8 character. Signed-off-by: Ilia Maslakov --- src/editor/edit-impl.h | 3 - src/editor/edit.c | 186 ++++++++++++++++++------------------------------- 2 files changed, 67 insertions(+), 122 deletions(-) diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h index 8a3ed5df9..c08c6111a 100644 --- a/src/editor/edit-impl.h +++ b/src/editor/edit-impl.h @@ -205,10 +205,7 @@ void edit_init_menu (struct WMenuBar *menubar); void menu_save_mode_cmd (void); int edit_translate_key (WEdit * edit, long x_key, int *cmd, int *ch); int edit_get_byte (WEdit * edit, long byte_index); -char *edit_get_byte_ptr (WEdit * edit, long byte_index); -char *edit_get_buf_ptr (WEdit * edit, long byte_index); int edit_get_utf (WEdit * edit, long byte_index, int *char_width); -int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width); long edit_count_lines (WEdit * edit, long current, long upto); long edit_move_forward (WEdit * edit, long current, long lines, long upto); long edit_move_forward3 (WEdit * edit, long current, int cols, long upto); diff --git a/src/editor/edit.c b/src/editor/edit.c index 9f0f63897..2055641fc 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -629,6 +629,72 @@ edit_modification (WEdit * edit) /* --------------------------------------------------------------------------------------------- */ +static char * +edit_get_byte_ptr (WEdit * edit, long byte_index) +{ + if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) + return NULL; + + if (byte_index >= edit->curs1) + { + unsigned long p; + + p = edit->curs1 + edit->curs2 - byte_index - 1; + return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1)); + } + + return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + + (byte_index & M_EDIT_BUF_SIZE)); +} + +/* --------------------------------------------------------------------------------------------- */ + +static int +edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width) +{ + int i, res; + gchar utf8_buf[3 * UTF8_CHAR_LEN + 1]; + gchar *str; + gchar *cursor_buf_ptr; + + if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0) + { + *char_width = 0; + return 0; + } + + for (i = 0; i < (3 * UTF8_CHAR_LEN); i++) + utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN)); + utf8_buf[3 * UTF8_CHAR_LEN] = '\0'; + + cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN); + str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr); + + if (str == NULL || g_utf8_next_char(str) != cursor_buf_ptr) + { + *char_width = 1; + return *(cursor_buf_ptr-1); + } + else + { + res = g_utf8_get_char_validated (str, -1); + + if (res < 0) + { + *char_width = 1; + return *(cursor_buf_ptr-1); + } + else + { + *char_width = cursor_buf_ptr - str; + return res; + } + } +} + +/* --------------------------------------------------------------------------------------------- */ + static void edit_insert_over (WEdit * edit) { @@ -1629,53 +1695,6 @@ edit_get_byte (WEdit * edit, long byte_index) /* --------------------------------------------------------------------------------------------- */ -char * -edit_get_byte_ptr (WEdit * edit, long byte_index) -{ - unsigned long p; - if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) - return NULL; - - if (byte_index >= edit->curs1) - { - p = edit->curs1 + edit->curs2 - byte_index - 1; - return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + - (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1)); - } - else - { - return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + - (byte_index & M_EDIT_BUF_SIZE)); - } -} - -/* --------------------------------------------------------------------------------------------- */ - -char * -edit_get_buf_ptr (WEdit * edit, long byte_index) -{ - unsigned long p; - - if (byte_index >= (edit->curs1 + edit->curs2)) - byte_index--; - - if (byte_index < 0) - return NULL; - - if (byte_index >= edit->curs1) - { - p = edit->curs1 + edit->curs2 - 1; - return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + - (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1)); - } - else - { - return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE)); - } -} - -/* --------------------------------------------------------------------------------------------- */ - int edit_get_utf (WEdit * edit, long byte_index, int *char_width) { @@ -1684,6 +1703,7 @@ edit_get_utf (WEdit * edit, long byte_index, int *char_width) gunichar ch; gchar *next_ch = NULL; int width = 0; + gchar utf8_buf[UTF8_CHAR_LEN + 1]; if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) { @@ -1703,78 +1723,6 @@ edit_get_utf (WEdit * edit, long byte_index, int *char_width) if (res < 0) { - ch = *str; - width = 0; - } - else - { - ch = res; - /* Calculate UTF-8 char width */ - next_ch = g_utf8_next_char (str); - if (next_ch) - { - width = next_ch - str; - } - else - { - ch = 0; - width = 0; - } - } - *char_width = width; - return ch; -} - -/* --------------------------------------------------------------------------------------------- */ - -int -edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width) -{ - gchar *str, *buf = NULL; - int res = -1; - gunichar ch; - gchar *next_ch = NULL; - int width = 0; - gchar utf8_buf[UTF8_CHAR_LEN + 1]; - - if (byte_index > 0) - byte_index--; - - if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) - { - *char_width = 0; - return 0; - } - - ch = edit_get_utf (edit, byte_index, &width); - - if (width == 1) - { - *char_width = width; - return ch; - } - - str = edit_get_byte_ptr (edit, byte_index); - buf = edit_get_buf_ptr (edit, byte_index); - if (str == NULL || buf == NULL) - { - *char_width = 0; - return 0; - } - /* get prev utf8 char */ - if (str != buf) - str = g_utf8_find_prev_char (buf, str); - - if (str == NULL) - { - *char_width = 0; - return 0; - } - else - res = g_utf8_get_char_validated (str, -1); - - if (res < 0) - { /* Retry with explicit bytes to make sure it's not a buffer boundary */ int i; for (i = 0; i < UTF8_CHAR_LEN; i++) -- 2.11.4.GIT