From: David Lawrence Ramsey Date: Tue, 25 Jan 2005 19:21:11 +0000 (+0000) Subject: turn string functions that don't take length arguments into wrappers for X-Git-Tag: v1.3.6~103 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=3f9c63589ea9e08a93d6f65f8a34e040cb13320e;p=nano.git turn string functions that don't take length arguments into wrappers for the versions that take length arguments, add multibyte equivalent of strlen(), and fix potential segfault in mbstrnlen() git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2300 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/ChangeLog b/ChangeLog index d3fd88dd..46cdd321 100644 --- a/ChangeLog +++ b/ChangeLog @@ -97,22 +97,23 @@ CVS code - is_alnum_mbchar(), is_alnum_wchar(), is_blank_mbchar(), is_blank_wchar(), is_cntrl_mbchar(), is_cntrl_wchar(), control_mbrep(), control_wrep(), mbwidth(), mb_cur_max(), - make_mbchar(), mbstrnlen(), mbstrcasecmp(), mbstrncasecmp(), - mbstrcasestr(), and mbrevstrcasestr(); changes to is_byte() - (moved to chars.c), is_blank_char() (moved to chars.c), - is_cntrl_char() (moved to chars.c), nstricmp() (renamed - nstrcasecmp() and moved to chars.c), nstrnicmp() (renamed - nstrncasecmp() and moved to chars.c), nstristr() (renamed - nstrcasestr() and moved to chars.c), revstrstr() (moved to - chars.c), revstristr() (renamed revstrcasestr() and moved to - chars.c), nstrnlen() (moved to chars.c), parse_char() - (renamed parse_mbchar() and moved to chars.c), move_left() - (renamed move_mbleft() and moved to chars.c), move_right() - (renamed move_mbright() and moved to chars.c), do_home(), - do_verbatim_input(), do_delete(), do_tab(), do_next_word(), - do_prev_word(), do_input(), do_output(), strstrwrapper(), - get_buffer(), unget_input(), unget_kbinput(), get_input(), - parse_kbinput(), unparse_kbinput(), parse_verbatim_kbinput(), + make_mbchar(), mbstrlen(), mbstrnlen(), mbstrcasecmp(), + mbstrncasecmp(), mbstrcasestr(), and mbrevstrcasestr(); + changes to is_byte() (moved to chars.c), is_blank_char() + (moved to chars.c), is_cntrl_char() (moved to chars.c), + nstricmp() (renamed nstrcasecmp() and moved to chars.c), + nstrnicmp() (renamed nstrncasecmp() and moved to chars.c), + nstristr() (renamed nstrcasestr() and moved to chars.c), + revstrstr() (moved to chars.c), revstristr() (renamed + revstrcasestr() and moved to chars.c), nstrnlen() (moved to + chars.c), parse_char() (renamed parse_mbchar() and moved to + chars.c), move_left() (renamed move_mbleft() and moved to + chars.c), move_right() (renamed move_mbright() and moved to + chars.c), do_home(), do_verbatim_input(), do_delete(), + do_tab(), do_next_word(), do_prev_word(), do_input(), + do_output(), strstrwrapper(), get_buffer(), unget_input(), + unget_kbinput(), get_input(), parse_kbinput(), + unparse_kbinput(), parse_verbatim_kbinput(), do_statusbar_input(), do_statusbar_home(), do_statusbar_verbatim_kbinput(), do_statusbar_output(), and display_string(); removal of buffer_to_keys() and diff --git a/src/chars.c b/src/chars.c index 8ba6d95d..356ecc50 100644 --- a/src/chars.c +++ b/src/chars.c @@ -436,66 +436,19 @@ size_t move_mbright(const char *buf, size_t pos) /* This function is equivalent to strcasecmp(). */ int nstrcasecmp(const char *s1, const char *s2) { - assert(s1 != NULL && s2 != NULL); - - for (; *s1 != '\0' && *s2 != '\0'; s1++, s2++) { - if (tolower(*s1) != tolower(*s2)) - break; - } - - return (tolower(*s1) - tolower(*s2)); + return +#ifdef HAVE_STRNCASECMP + strncasecmp(s1, s2, (size_t)-1); +#else + nstrncasecmp(s1, s2, (size_t)-1); +#endif } #endif /* This function is equivalent to strcasecmp() for multibyte strings. */ int mbstrcasecmp(const char *s1, const char *s2) { -#ifdef NANO_WIDE - if (!ISSET(NO_UTF8)) { - char *s1_mb = charalloc(MB_CUR_MAX); - char *s2_mb = charalloc(MB_CUR_MAX); - wchar_t ws1, ws2; - - assert(s1 != NULL && s2 != NULL); - - while (*s1 != '\0' && *s2 != '\0') { - int s1_mb_len, s2_mb_len; - - s1_mb_len = parse_mbchar(s1, s1_mb, NULL, NULL); - - if (mbtowc(&ws1, s1_mb, s1_mb_len) <= 0) { - mbtowc(NULL, NULL, 0); - ws1 = (unsigned char)*s1_mb; - } - - - s2_mb_len = parse_mbchar(s2, s2_mb, NULL, NULL); - - if (mbtowc(&ws2, s2_mb, s2_mb_len) <= 0) { - mbtowc(NULL, NULL, 0); - ws2 = (unsigned char)*s2_mb; - } - - - if (towlower(ws1) != towlower(ws2)) - break; - - s1 += s1_mb_len; - s2 += s2_mb_len; - } - - free(s1_mb); - free(s2_mb); - - return (towlower(ws1) - towlower(ws2)); - } else -#endif - return -#ifdef HAVE_STRCASECMP - strcasecmp(s1, s2); -#else - nstrcasecmp(s1, s2); -#endif + return mbstrncasecmp(s1, s2, (size_t)-1); } #ifndef HAVE_STRNCASECMP @@ -752,6 +705,12 @@ const char *mbrevstrcasestr(const char *haystack, const char *needle, } #endif +/* This function is equivalent to strlen() for multibyte strings. */ +size_t mbstrlen(const char *s) +{ + return mbstrnlen(s, (size_t)-1); +} + #ifndef HAVE_STRNLEN /* This function is equivalent to strnlen(). */ size_t nstrnlen(const char *s, size_t maxlen) @@ -779,18 +738,19 @@ size_t mbstrnlen(const char *s, size_t maxlen) int s_mb_len; while (*s != '\0') { - s_mb_len = parse_mbchar(s + n, s_mb, NULL, NULL); + s_mb_len = parse_mbchar(s, s_mb, NULL, NULL); if (maxlen == 0) break; maxlen--; - n += s_mb_len; + s += s_mb_len; + n++; } free(s_mb); - return strnlenpt(s, n); + return n; } else #endif return diff --git a/src/proto.h b/src/proto.h index 668d1002..605396ec 100644 --- a/src/proto.h +++ b/src/proto.h @@ -202,6 +202,7 @@ const char *revstrcasestr(const char *haystack, const char *needle, const char *mbrevstrcasestr(const char *haystack, const char *needle, const char *rev_start); #endif +size_t mbstrlen(const char *s); #ifndef HAVE_STRNLEN size_t nstrnlen(const char *s, size_t maxlen); #endif