From: David Lawrence Ramsey Date: Mon, 9 Jul 2007 22:41:55 +0000 (+0000) Subject: in mbstrncasecmp(), mbstrnlen(), mbstrpbrk(), and has_blank_mbchars(), X-Git-Tag: v2.0.7~76 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=f9755eb4770432298a67bb45d7a628ec763cbc20;p=nano.git in mbstrncasecmp(), mbstrnlen(), mbstrpbrk(), and has_blank_mbchars(), simplify by using for loops instead of while loops where possible, to match the single-byte versions of these functions git-svn-id: svn://svn.savannah.gnu.org/nano/branches/nano_2_0_branch/nano@4128 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/ChangeLog b/ChangeLog index 734ef8f5..9e08a0d1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,11 @@ CVS code - wmouse_trafo(), which does both. Changes to do_browser(), do_mouse(), do_statusbar_mouse(), do_yesno_prompt(), and do_mouseinput(). (DLR) + - Simplify various multibyte string functions by using for loops + instead of while loops where possible, to match the + single-byte versions of these functions. Changes to + mbstrncasecmp(), mbstrnlen(), mbstrpbrk(), and + has_blank_mbchars(). (DLR) - chars.c: nstrncasecmp(), mbstrncasecmp() - For efficiency, return zero immediately if s1 and s2 point to diff --git a/src/chars.c b/src/chars.c index 6c07380e..4f393a31 100644 --- a/src/chars.c +++ b/src/chars.c @@ -537,7 +537,8 @@ int mbstrncasecmp(const char *s1, const char *s2, size_t n) s1_mb = charalloc(MB_CUR_MAX); s2_mb = charalloc(MB_CUR_MAX); - while (*s1 != '\0' && *s2 != '\0' && n > 0) { + for (; *s1 != '\0' && *s2 != '\0' && n > 0; s1 += + move_mbright(s1, 0), s2 += move_mbright(s2, 0), n--) { bool bad_s1_mb = FALSE, bad_s2_mb = FALSE; int s1_mb_len, s2_mb_len; @@ -557,19 +558,15 @@ int mbstrncasecmp(const char *s1, const char *s2, size_t n) bad_s2_mb = TRUE; } - if (n == 0 || bad_s1_mb != bad_s2_mb || - towlower(ws1) != towlower(ws2)) + if (bad_s1_mb != bad_s2_mb || towlower(ws1) != + towlower(ws2)) break; - - s1 += s1_mb_len; - s2 += s2_mb_len; - n--; } free(s1_mb); free(s2_mb); - return towlower(ws1) - towlower(ws2); + return (n > 0) ? towlower(ws1) - towlower(ws2) : 0; } else #endif return strncasecmp(s1, s2, n); @@ -839,18 +836,10 @@ size_t mbstrnlen(const char *s, size_t maxlen) #ifdef ENABLE_UTF8 if (use_utf8) { size_t n = 0; - int s_mb_len; - - while (*s != '\0') { - s_mb_len = parse_mbchar(s, NULL, NULL); - if (maxlen == 0) - break; - - s += s_mb_len; - maxlen--; - n++; - } + for (; *s != '\0' && maxlen > 0; s += move_mbright(s, 0), + maxlen--, n++) + ; return n; } else @@ -914,11 +903,9 @@ char *mbstrpbrk(const char *s, const char *accept) #ifdef ENABLE_UTF8 if (use_utf8) { - while (*s != '\0') { + for (; *s != '\0'; s += move_mbright(s, 0)) { if (mbstrchr(accept, s) != NULL) return (char *)s; - - s += move_mbright(s, 0); } return NULL; @@ -1000,18 +987,16 @@ bool has_blank_mbchars(const char *s) #ifdef ENABLE_UTF8 if (use_utf8) { - char *chr_mb = charalloc(MB_CUR_MAX); bool retval = FALSE; + char *chr_mb = charalloc(MB_CUR_MAX); - while (*s != '\0') { - int chr_mb_len = parse_mbchar(s, chr_mb, NULL); + for (; *s != '\0'; s += move_mbright(s, 0)) { + parse_mbchar(s, chr_mb, NULL); if (is_blank_mbchar(chr_mb)) { retval = TRUE; break; } - - s += chr_mb_len; } free(chr_mb);