mirror of git://git.sv.gnu.org/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
This commit is contained in:
parent
14ace1711d
commit
3f9c63589e
33
ChangeLog
33
ChangeLog
|
@ -97,22 +97,23 @@ CVS code -
|
||||||
is_alnum_mbchar(), is_alnum_wchar(), is_blank_mbchar(),
|
is_alnum_mbchar(), is_alnum_wchar(), is_blank_mbchar(),
|
||||||
is_blank_wchar(), is_cntrl_mbchar(), is_cntrl_wchar(),
|
is_blank_wchar(), is_cntrl_mbchar(), is_cntrl_wchar(),
|
||||||
control_mbrep(), control_wrep(), mbwidth(), mb_cur_max(),
|
control_mbrep(), control_wrep(), mbwidth(), mb_cur_max(),
|
||||||
make_mbchar(), mbstrnlen(), mbstrcasecmp(), mbstrncasecmp(),
|
make_mbchar(), mbstrlen(), mbstrnlen(), mbstrcasecmp(),
|
||||||
mbstrcasestr(), and mbrevstrcasestr(); changes to is_byte()
|
mbstrncasecmp(), mbstrcasestr(), and mbrevstrcasestr();
|
||||||
(moved to chars.c), is_blank_char() (moved to chars.c),
|
changes to is_byte() (moved to chars.c), is_blank_char()
|
||||||
is_cntrl_char() (moved to chars.c), nstricmp() (renamed
|
(moved to chars.c), is_cntrl_char() (moved to chars.c),
|
||||||
nstrcasecmp() and moved to chars.c), nstrnicmp() (renamed
|
nstricmp() (renamed nstrcasecmp() and moved to chars.c),
|
||||||
nstrncasecmp() and moved to chars.c), nstristr() (renamed
|
nstrnicmp() (renamed nstrncasecmp() and moved to chars.c),
|
||||||
nstrcasestr() and moved to chars.c), revstrstr() (moved to
|
nstristr() (renamed nstrcasestr() and moved to chars.c),
|
||||||
chars.c), revstristr() (renamed revstrcasestr() and moved to
|
revstrstr() (moved to chars.c), revstristr() (renamed
|
||||||
chars.c), nstrnlen() (moved to chars.c), parse_char()
|
revstrcasestr() and moved to chars.c), nstrnlen() (moved to
|
||||||
(renamed parse_mbchar() and moved to chars.c), move_left()
|
chars.c), parse_char() (renamed parse_mbchar() and moved to
|
||||||
(renamed move_mbleft() and moved to chars.c), move_right()
|
chars.c), move_left() (renamed move_mbleft() and moved to
|
||||||
(renamed move_mbright() and moved to chars.c), do_home(),
|
chars.c), move_right() (renamed move_mbright() and moved to
|
||||||
do_verbatim_input(), do_delete(), do_tab(), do_next_word(),
|
chars.c), do_home(), do_verbatim_input(), do_delete(),
|
||||||
do_prev_word(), do_input(), do_output(), strstrwrapper(),
|
do_tab(), do_next_word(), do_prev_word(), do_input(),
|
||||||
get_buffer(), unget_input(), unget_kbinput(), get_input(),
|
do_output(), strstrwrapper(), get_buffer(), unget_input(),
|
||||||
parse_kbinput(), unparse_kbinput(), parse_verbatim_kbinput(),
|
unget_kbinput(), get_input(), parse_kbinput(),
|
||||||
|
unparse_kbinput(), parse_verbatim_kbinput(),
|
||||||
do_statusbar_input(), do_statusbar_home(),
|
do_statusbar_input(), do_statusbar_home(),
|
||||||
do_statusbar_verbatim_kbinput(), do_statusbar_output(), and
|
do_statusbar_verbatim_kbinput(), do_statusbar_output(), and
|
||||||
display_string(); removal of buffer_to_keys() and
|
display_string(); removal of buffer_to_keys() and
|
||||||
|
|
74
src/chars.c
74
src/chars.c
|
@ -436,66 +436,19 @@ size_t move_mbright(const char *buf, size_t pos)
|
||||||
/* This function is equivalent to strcasecmp(). */
|
/* This function is equivalent to strcasecmp(). */
|
||||||
int nstrcasecmp(const char *s1, const char *s2)
|
int nstrcasecmp(const char *s1, const char *s2)
|
||||||
{
|
{
|
||||||
assert(s1 != NULL && s2 != NULL);
|
return
|
||||||
|
#ifdef HAVE_STRNCASECMP
|
||||||
for (; *s1 != '\0' && *s2 != '\0'; s1++, s2++) {
|
strncasecmp(s1, s2, (size_t)-1);
|
||||||
if (tolower(*s1) != tolower(*s2))
|
#else
|
||||||
break;
|
nstrncasecmp(s1, s2, (size_t)-1);
|
||||||
}
|
#endif
|
||||||
|
|
||||||
return (tolower(*s1) - tolower(*s2));
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* This function is equivalent to strcasecmp() for multibyte strings. */
|
/* This function is equivalent to strcasecmp() for multibyte strings. */
|
||||||
int mbstrcasecmp(const char *s1, const char *s2)
|
int mbstrcasecmp(const char *s1, const char *s2)
|
||||||
{
|
{
|
||||||
#ifdef NANO_WIDE
|
return mbstrncasecmp(s1, s2, (size_t)-1);
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef HAVE_STRNCASECMP
|
#ifndef HAVE_STRNCASECMP
|
||||||
|
@ -752,6 +705,12 @@ const char *mbrevstrcasestr(const char *haystack, const char *needle,
|
||||||
}
|
}
|
||||||
#endif
|
#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
|
#ifndef HAVE_STRNLEN
|
||||||
/* This function is equivalent to strnlen(). */
|
/* This function is equivalent to strnlen(). */
|
||||||
size_t nstrnlen(const char *s, size_t maxlen)
|
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;
|
int s_mb_len;
|
||||||
|
|
||||||
while (*s != '\0') {
|
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)
|
if (maxlen == 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
maxlen--;
|
maxlen--;
|
||||||
n += s_mb_len;
|
s += s_mb_len;
|
||||||
|
n++;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(s_mb);
|
free(s_mb);
|
||||||
|
|
||||||
return strnlenpt(s, n);
|
return n;
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
return
|
return
|
||||||
|
|
|
@ -202,6 +202,7 @@ const char *revstrcasestr(const char *haystack, const char *needle,
|
||||||
const char *mbrevstrcasestr(const char *haystack, const char *needle,
|
const char *mbrevstrcasestr(const char *haystack, const char *needle,
|
||||||
const char *rev_start);
|
const char *rev_start);
|
||||||
#endif
|
#endif
|
||||||
|
size_t mbstrlen(const char *s);
|
||||||
#ifndef HAVE_STRNLEN
|
#ifndef HAVE_STRNLEN
|
||||||
size_t nstrnlen(const char *s, size_t maxlen);
|
size_t nstrnlen(const char *s, size_t maxlen);
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue