From: Bruno Haible Date: Sun, 11 Feb 2007 17:02:31 +0000 (+0000) Subject: New module 'mbslen'. X-Git-Tag: cvs-readonly~1092 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=ef66119f6b4d0211693d944c495d1e15c9331667;p=gnulib.git New module 'mbslen'. --- diff --git a/ChangeLog b/ChangeLog index b752d049b..e36c880c5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,17 @@ 2007-02-11 Bruno Haible + New module mbslen. + * modules/mbslen: New file. + * lib/mbslen.c: New file. + * lib/string_.h (mbslen): New declaration. + * m4/mbslen.m4: New file. + * m4/string_h.m4 (gl_STRING_MODULE_INDICATOR_DEFAULTS): Initialize + GNULIB_MBSLEN. + * modules/string (string.h): Also substitute GNULIB_MBSLEN. + * MODULES.html.sh (Internationalization functions): Add mbslen. + +2007-02-11 Bruno Haible + Ensure O(n) worst-case complexity of strcasestr substitute. * lib/strcasestr.c: Include stdbool.h. (knuth_morris_pratt): New function. diff --git a/MODULES.html.sh b/MODULES.html.sh index 114e3026d..4e42b80e6 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -2160,6 +2160,7 @@ func_all_modules () func_module iconvme func_module localcharset func_module hard-locale + func_module mbslen func_module mbschr func_module mbsrchr func_module mbsstr diff --git a/lib/mbslen.c b/lib/mbslen.c new file mode 100644 index 000000000..41c219f54 --- /dev/null +++ b/lib/mbslen.c @@ -0,0 +1,47 @@ +/* Counting the multibyte characters in a string. + Copyright (C) 2007 Free Software Foundation, Inc. + Written by Bruno Haible , 2007. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include + +/* Specification. */ +#include + +#if HAVE_MBRTOWC +# include "mbuiter.h" +#endif + +/* Return the number of multibyte characters in the character string STRING. */ +size_t +mbslen (const char *string) +{ +#if HAVE_MBRTOWC + if (MB_CUR_MAX > 1) + { + size_t count; + mbui_iterator_t iter; + + count = 0; + for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter)) + count++; + + return count; + } + else +#endif + return strlen (string); +} diff --git a/lib/string_.h b/lib/string_.h index 113bfb480..1ac0ea7df 100644 --- a/lib/string_.h +++ b/lib/string_.h @@ -349,6 +349,12 @@ extern char *strtok_r (char *restrict s, char const *restrict delim, /* The following functions are not specified by POSIX. They are gnulib extensions. */ +#if @GNULIB_MBSLEN@ +/* Return the number of multibyte characters in the character string STRING. + This considers multibyte characters, unlike strlen, which counts bytes. */ +extern size_t mbslen (const char *string); +#endif + #if @GNULIB_MBSCHR@ /* Locate the first single-byte character C in the character string STRING, and return a pointer to it. Return NULL if C is not found in STRING. diff --git a/m4/mbslen.m4 b/m4/mbslen.m4 new file mode 100644 index 000000000..dad8a8cf6 --- /dev/null +++ b/m4/mbslen.m4 @@ -0,0 +1,16 @@ +# mbslen.m4 serial 1 +dnl Copyright (C) 2007 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_MBSLEN], +[ + gl_PREREQ_MBSLEN +]) + +# Prerequisites of lib/mbslen.c. +AC_DEFUN([gl_PREREQ_MBSLEN], [ + AC_REQUIRE([gl_FUNC_MBRTOWC]) + : +]) diff --git a/m4/string_h.m4 b/m4/string_h.m4 index 80e95e1bb..9f5ba8752 100644 --- a/m4/string_h.m4 +++ b/m4/string_h.m4 @@ -67,6 +67,7 @@ AC_DEFUN([gl_STRING_MODULE_INDICATOR_DEFAULTS], GNULIB_STRSEP=0; AC_SUBST([GNULIB_STRSEP]) GNULIB_STRCASESTR=0; AC_SUBST([GNULIB_STRCASESTR]) GNULIB_STRTOK_R=0; AC_SUBST([GNULIB_STRTOK_R]) + GNULIB_MBSLEN=0; AC_SUBST([GNULIB_MBSLEN]) GNULIB_MBSCHR=0; AC_SUBST([GNULIB_MBSCHR]) GNULIB_MBSRCHR=0; AC_SUBST([GNULIB_MBSRCHR]) GNULIB_MBSSTR=0; AC_SUBST([GNULIB_MBSSTR]) diff --git a/modules/mbslen b/modules/mbslen new file mode 100644 index 000000000..47d7ab581 --- /dev/null +++ b/modules/mbslen @@ -0,0 +1,28 @@ +Description: +mbslen() function: Determine the number of multibyte characters in a string. + +Files: +lib/mbslen.c +m4/mbslen.m4 +m4/mbrtowc.m4 + +Depends-on: +mbuiter +string + +configure.ac: +gl_FUNC_MBSLEN +gl_STRING_MODULE_INDICATOR([mbslen]) + +Makefile.am: +lib_SOURCES += mbslen.c + +Include: + + +License: +LGPL + +Maintainer: +Bruno Haible + diff --git a/modules/string b/modules/string index abfc9ea18..480624032 100644 --- a/modules/string +++ b/modules/string @@ -21,6 +21,7 @@ string.h: string_.h rm -f $@-t $@ { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \ sed -e 's|@''ABSOLUTE_STRING_H''@|$(ABSOLUTE_STRING_H)|g' \ + -e 's|@''GNULIB_MBSLEN''@|$(GNULIB_MBSLEN)|g' \ -e 's|@''GNULIB_MBSCHR''@|$(GNULIB_MBSCHR)|g' \ -e 's|@''GNULIB_MBSRCHR''@|$(GNULIB_MBSRCHR)|g' \ -e 's|@''GNULIB_MBSSTR''@|$(GNULIB_MBSSTR)|g' \