From 4d65a731c28d53a536a044a85e82223a7aa46bd5 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 30 Mar 2016 13:09:36 +0300 Subject: [PATCH] mcview: refactoring of mcview_get_utf(). Signed-off-by: Andrew Borodin --- src/viewer/ascii.c | 6 ++---- src/viewer/datasource.c | 30 +++++++++++++++--------------- src/viewer/internal.h | 2 +- src/viewer/nroff.c | 7 +++---- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/viewer/ascii.c b/src/viewer/ascii.c index 83fed05da..567cec432 100644 --- a/src/viewer/ascii.c +++ b/src/viewer/ascii.c @@ -354,11 +354,9 @@ mcview_get_next_char (WView * view, mcview_state_machine_t * state, int *c) #ifdef HAVE_CHARSET if (view->utf8) { - gboolean result; - int char_length; + int char_length = 0; - *c = mcview_get_utf (view, state->offset, &char_length, &result); - if (!result) + if (!mcview_get_utf (view, state->offset, c, &char_length)) return FALSE; /* Pretend EOF if we crossed force_max */ if (view->force_max >= 0 && state->offset + char_length > view->force_max) diff --git a/src/viewer/datasource.c b/src/viewer/datasource.c index f98e501ce..0379be413 100644 --- a/src/viewer/datasource.c +++ b/src/viewer/datasource.c @@ -151,18 +151,13 @@ mcview_get_ptr_file (WView * view, off_t byte_index) /* --------------------------------------------------------------------------------------------- */ -int -mcview_get_utf (WView * view, off_t byte_index, int *char_length, gboolean * result) +gboolean +mcview_get_utf (WView * view, off_t byte_index, int *ch, int *ch_len) { gchar *str = NULL; - int res = -1; - gunichar ch; - gchar *next_ch = NULL; + int res; gchar utf8buf[UTF8_CHAR_LEN + 1]; - *char_length = 0; - *result = FALSE; - switch (view->datasource) { case DS_STDIO_PIPE: @@ -180,8 +175,10 @@ mcview_get_utf (WView * view, off_t byte_index, int *char_length, gboolean * res break; } + *ch = 0; + if (str == NULL) - return 0; + return FALSE; res = g_utf8_get_char_validated (str, -1); @@ -189,6 +186,7 @@ mcview_get_utf (WView * view, off_t byte_index, int *char_length, gboolean * res { /* Retry with explicit bytes to make sure it's not a buffer boundary */ int i; + for (i = 0; i < UTF8_CHAR_LEN; i++) { if (mcview_get_byte (view, byte_index + i, &res)) @@ -206,18 +204,20 @@ mcview_get_utf (WView * view, off_t byte_index, int *char_length, gboolean * res if (res < 0) { - ch = *str; - *char_length = 1; + *ch = (unsigned char) (*str); + *ch_len = 1; } else { - ch = res; + gchar *next_ch = NULL; + + *ch = res; /* Calculate UTF-8 char length */ next_ch = g_utf8_next_char (str); - *char_length = next_ch - str; + *ch_len = next_ch - str; } - *result = TRUE; - return ch; + + return TRUE; } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/viewer/internal.h b/src/viewer/internal.h index a5a7350f6..9776ab42c 100644 --- a/src/viewer/internal.h +++ b/src/viewer/internal.h @@ -264,7 +264,7 @@ off_t mcview_get_filesize (WView *); void mcview_update_filesize (WView * view); char *mcview_get_ptr_file (WView *, off_t); char *mcview_get_ptr_string (WView *, off_t); -int mcview_get_utf (WView *, off_t, int *, gboolean *); +gboolean mcview_get_utf (WView * view, off_t byte_index, int *ch, int *ch_len); gboolean mcview_get_byte_string (WView *, off_t, int *); gboolean mcview_get_byte_none (WView *, off_t, int *); void mcview_set_byte (WView *, off_t, byte); diff --git a/src/viewer/nroff.c b/src/viewer/nroff.c index f698f3838..903906968 100644 --- a/src/viewer/nroff.c +++ b/src/viewer/nroff.c @@ -60,13 +60,12 @@ static gboolean mcview_nroff_get_char (mcview_nroff_t * nroff, int *ret_val, off_t nroff_index) { - int c; + int c = 0; + #ifdef HAVE_CHARSET if (nroff->view->utf8) { - gboolean utf_result; - c = mcview_get_utf (nroff->view, nroff_index, &nroff->char_length, &utf_result); - if (!utf_result) + if (!mcview_get_utf (nroff->view, nroff_index, &c, &nroff->char_length)) { /* we need got symbol in any case */ nroff->char_length = 1; -- 2.11.4.GIT