move nstrnlen() to chars.c, and add a multibyte equivalent of strnlen()

to it


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2259 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
This commit is contained in:
David Lawrence Ramsey 2005-01-14 04:22:14 +00:00
parent 49ff08b251
commit 5508cc5b0a
6 changed files with 65 additions and 25 deletions

View File

@ -94,9 +94,10 @@ CVS code -
character-specific functions to their own source file. New character-specific functions to their own source file. New
file chars.c; new functions is_blank_mbchar(), file chars.c; new functions 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(), and mbstrnlen(), control_mbrep(), control_wrep(), mbwidth(),
make_mbchar(); changes to is_blank_char() (moved to chars.c), mb_cur_max(), and make_mbchar(); changes to is_blank_char()
is_cntrl_char() (moved to chars.c), parse_char() (renamed (moved to chars.c), is_cntrl_char() (moved to chars.c),
nstrnlen() (moved to chars.c), parse_char() (renamed
parse_mbchar() and moved to chars.c), do_home(), parse_mbchar() and moved to chars.c), do_home(),
do_verbatim_input(), do_delete(), do_tab(), do_input(), do_verbatim_input(), do_delete(), do_tab(), do_input(),
do_output(), get_buffer(), unget_input(), unget_kbinput(), do_output(), get_buffer(), unget_input(), unget_kbinput(),

View File

@ -24,6 +24,7 @@
#endif #endif
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <ctype.h> #include <ctype.h>
#include <assert.h> #include <assert.h>
#include "proto.h" #include "proto.h"
@ -124,6 +125,59 @@ bool is_cntrl_wchar(wchar_t wc)
} }
#endif #endif
#ifndef HAVE_STRNLEN
/* This function is equivalent to strnlen(). */
size_t nstrnlen(const char *s, size_t maxlen)
{
size_t n = 0;
assert(s != NULL);
for (; maxlen > 0 && *s != '\0'; maxlen--, n++, s++)
;
return n;
}
#endif
/* This function is equivalent to strnlen() for multibyte strings. */
size_t mbstrnlen(const char *s, size_t maxlen)
{
#ifdef NANO_WIDE
if (ISSET(NO_UTF8)) {
size_t n = 0;
char *s_mb = charalloc(mb_cur_max());
int s_mb_len;
assert(s != NULL);
while (*s != '\0') {
s_mb_len = parse_mbchar(s + n, s_mb
#ifdef NANO_WIDE
, NULL
#endif
, NULL);
maxlen -= s_mb_len;
n += s_mb_len;
if (maxlen == 0)
break;
}
free(s_mb);
return n;
} else
#endif
return
#ifdef HAVE_STRNLEN
strnlen(s, maxlen);
#else
nstrnlen(s, maxlen);
#endif
}
/* c is a control character. It displays as ^@, ^?, or ^[ch] where ch /* c is a control character. It displays as ^@, ^?, or ^[ch] where ch
* is c + 64. We return that character. */ * is c + 64. We return that character. */
unsigned char control_rep(unsigned char c) unsigned char control_rep(unsigned char c)

View File

@ -100,8 +100,8 @@
# endif # endif
#endif #endif
/* If no strcasecmp(), strncasecmp(), strcasestr(), strnlen(), /* If no strcasecmp(), strncasecmp(), strcasestr(), getdelim(), or
* getdelim(), or getline(), use the versions we have. */ * getline(), use the versions we have. */
#ifndef HAVE_STRCASECMP #ifndef HAVE_STRCASECMP
#define strcasecmp nstricmp #define strcasecmp nstricmp
#endif #endif
@ -114,10 +114,6 @@
#define strcasestr nstristr #define strcasestr nstristr
#endif #endif
#ifndef HAVE_STRNLEN
#define strnlen nstrnlen
#endif
#ifndef HAVE_GETDELIM #ifndef HAVE_GETDELIM
#define getdelim ngetdelim #define getdelim ngetdelim
#endif #endif

View File

@ -161,6 +161,10 @@ bool is_cntrl_mbchar(const char *c);
#ifdef NANO_WIDE #ifdef NANO_WIDE
bool is_cntrl_wchar(wchar_t wc); bool is_cntrl_wchar(wchar_t wc);
#endif #endif
#ifndef HAVE_STRNLEN
size_t nstrnlen(const char *s, size_t maxlen);
#endif
size_t mbstrnlen(const char *s, size_t maxlen);
unsigned char control_rep(unsigned char c); unsigned char control_rep(unsigned char c);
char *control_mbrep(const char *c, char *crep, int *crep_len); char *control_mbrep(const char *c, char *crep, int *crep_len);
#ifdef NANO_WIDE #ifdef NANO_WIDE

View File

@ -84,7 +84,7 @@ void not_found_msg(const char *str)
assert(str != NULL); assert(str != NULL);
disp = display_string(str, 0, (COLS / 2) + 1, FALSE); disp = display_string(str, 0, (COLS / 2) + 1, FALSE);
numchars = strnlen(disp, COLS / 2); numchars = mbstrnlen(disp, COLS / 2);
statusbar(_("\"%.*s%s\" not found"), numchars, disp, statusbar(_("\"%.*s%s\" not found"), numchars, disp,
(disp[numchars] == '\0') ? "" : "..."); (disp[numchars] == '\0') ? "" : "...");

View File

@ -261,21 +261,6 @@ const char *revstristr(const char *haystack, const char *needle, const
} }
#endif /* !NANO_SMALL */ #endif /* !NANO_SMALL */
#ifndef HAVE_STRNLEN
/* This function is equivalent to strnlen(). */
size_t nstrnlen(const char *s, size_t maxlen)
{
size_t n = 0;
assert(s != NULL);
for (; maxlen > 0 && *s != '\0'; maxlen--, n++, s++)
;
return n;
}
#endif
#if !defined(NANO_SMALL) && defined(ENABLE_NANORC) #if !defined(NANO_SMALL) && defined(ENABLE_NANORC)
#ifndef HAVE_GETLINE #ifndef HAVE_GETLINE
/* This function is equivalent to getline(). It was adapted from /* This function is equivalent to getline(). It was adapted from