From b02dccc51fbefb44228f94ee859892c1d88c62d8 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Thu, 3 Oct 2019 10:25:58 +0200 Subject: [PATCH] tweaks: elide a function from a non-UTF8 build In a non-UTF8 build, mbwidth() returns always 1, so it is pointless to call that function and compare its result to zero then. Also, don't bother special-casing the function for a non-UTF8 locale. --- src/chars.c | 9 +++------ src/proto.h | 2 ++ src/winio.c | 8 ++++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/chars.c b/src/chars.c index f759fed0..ca824a89 100644 --- a/src/chars.c +++ b/src/chars.c @@ -200,11 +200,10 @@ char control_mbrep(const char *c, bool isdata) return control_rep(*c); } +#ifdef ENABLE_UTF8 /* This function is equivalent to wcwidth() for multibyte characters. */ int mbwidth(const char *c) { -#ifdef ENABLE_UTF8 - if (use_utf8) { wchar_t wc; int width; @@ -213,14 +212,12 @@ int mbwidth(const char *c) width = wcwidth(wc); - if (width == -1) + if (width < 0) return 1; return width; - } else -#endif - return 1; } +#endif /* Convert the Unicode value in code to a multibyte character, if possible. * If the conversion succeeds, return the (dynamically allocated) multibyte diff --git a/src/proto.h b/src/proto.h index dee78991..ba32e9d3 100644 --- a/src/proto.h +++ b/src/proto.h @@ -213,7 +213,9 @@ bool is_ascii_cntrl_char(int c); bool is_cntrl_mbchar(const char *c); bool is_word_mbchar(const char *c, bool allow_punct); char control_mbrep(const char *c, bool isdata); +#ifdef ENABLE_UTF8 int mbwidth(const char *c); +#endif char *make_mbchar(long chr, int *chr_mb_len); int char_length(const char *pointer); int parse_mbchar(const char *buf, char *chr, size_t *col); diff --git a/src/winio.c b/src/winio.c index 444f5318..178e593b 100644 --- a/src/winio.c +++ b/src/winio.c @@ -1890,11 +1890,13 @@ char *display_string(const char *buf, size_t column, size_t span, #ifdef ENABLE_UTF8 #define ISO8859_CHAR FALSE +#define ZEROWIDTH_CHAR (mbwidth(buf) == 0) #else #define ISO8859_CHAR ((unsigned char)*buf > 0x9F) +#define ZEROWIDTH_CHAR FALSE #endif - while (*buf != '\0' && (column < beyond || mbwidth(buf) == 0)) { + while (*buf != '\0' && (column < beyond || ZEROWIDTH_CHAR)) { /* A plain printable ASCII character is one byte, one column. */ if (((signed char)*buf > 0x20 && *buf != DEL_CODE) || ISO8859_CHAR) { converted[index++] = *(buf++); @@ -1983,14 +1985,16 @@ char *display_string(const char *buf, size_t column, size_t span, /* If there is more text than can be shown, make room for the ">". */ if (column > beyond || (*buf != '\0' && (isprompt || (isdata && !ISSET(SOFTWRAP))))) { +#ifdef ENABLE_UTF8 do { index = step_left(converted, index); } while (mbwidth(converted + index) == 0); -#ifdef ENABLE_UTF8 /* Display the left half of a two-column character as '['. */ if (mbwidth(converted + index) == 2) converted[index++] = '['; +#else + index--; #endif }