changeset 8968:236e8277b9b1

New module 'mbsnlen'.
author Bruno Haible <bruno@clisp.org>
date Mon, 11 Jun 2007 00:29:30 +0000
parents 5963f8c7f555
children c6eeca55f502
files ChangeLog MODULES.html.sh lib/mbsnlen.c lib/string_.h m4/mbsnlen.m4 m4/string_h.m4 modules/mbsnlen modules/string
diffstat 8 files changed, 113 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2007-06-10  Bruno Haible  <bruno@clisp.org>
+
+	* lib/string_.h (mbsnlen): New declaration.
+	* lib/mbsnlen.c: New file.
+	* m4/mbsnlen.m4: New file.
+	* modules/mbsnlen: New file.
+	* m4/string_h.m4 (gl_HEADER_STRING_H_DEFAULTS): Set GNULIB_MBSNLEN.
+	* modules/string (string.h): Substitute GNULIB_MBSNLEN.
+	* MODULES.html.sh (Internationalization functions): Add mbsnlen.
+
 2007-06-10  Bruno Haible  <bruno@clisp.org>
 
 	* lib/mbslen.c: Include <stdlib.h>, needed for MB_CUR_MAX.
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -2267,6 +2267,7 @@
   func_module localcharset
   func_module hard-locale
   func_module mbslen
+  func_module mbsnlen
   func_module mbschr
   func_module mbsrchr
   func_module mbsstr
new file mode 100644
--- /dev/null
+++ b/lib/mbsnlen.c
@@ -0,0 +1,50 @@
+/* Counting the multibyte characters in a string.
+   Copyright (C) 2007 Free Software Foundation, Inc.
+   Written by Bruno Haible <bruno@clisp.org>, 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 <config.h>
+
+/* Specification.  */
+#include <string.h>
+
+#include <stdlib.h>
+
+#if HAVE_MBRTOWC
+# include "mbiter.h"
+#endif
+
+/* Return the number of multibyte characters in the character string starting
+   at STRING and ending at STRING + LEN.  */
+size_t
+mbsnlen (const char *string, size_t len)
+{
+#if HAVE_MBRTOWC
+  if (MB_CUR_MAX > 1)
+    {
+      size_t count;
+      mbi_iterator_t iter;
+
+      count = 0;
+      for (mbi_init (iter, string, len); mbi_avail (iter); mbi_advance (iter))
+	count++;
+
+      return count;
+    }
+  else
+#endif
+    return len;
+}
--- a/lib/string_.h
+++ b/lib/string_.h
@@ -401,6 +401,12 @@
 extern size_t mbslen (const char *string);
 #endif
 
+#if @GNULIB_MBSNLEN@
+/* Return the number of multibyte characters in the character string starting
+   at STRING and ending at STRING + LEN.  */
+extern size_t mbsnlen (const char *string, size_t len);
+#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.
new file mode 100644
--- /dev/null
+++ b/m4/mbsnlen.m4
@@ -0,0 +1,16 @@
+# mbsnlen.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_MBSNLEN],
+[
+  gl_PREREQ_MBSNLEN
+])
+
+# Prerequisites of lib/mbsnlen.c.
+AC_DEFUN([gl_PREREQ_MBSNLEN], [
+  AC_REQUIRE([gl_FUNC_MBRTOWC])
+  :
+])
--- a/m4/string_h.m4
+++ b/m4/string_h.m4
@@ -46,6 +46,7 @@
   GNULIB_STRCASESTR=0;  AC_SUBST([GNULIB_STRCASESTR])
   GNULIB_STRTOK_R=0;    AC_SUBST([GNULIB_STRTOK_R])
   GNULIB_MBSLEN=0;      AC_SUBST([GNULIB_MBSLEN])
+  GNULIB_MBSNLEN=0;     AC_SUBST([GNULIB_MBSNLEN])
   GNULIB_MBSCHR=0;      AC_SUBST([GNULIB_MBSCHR])
   GNULIB_MBSRCHR=0;     AC_SUBST([GNULIB_MBSRCHR])
   GNULIB_MBSSTR=0;      AC_SUBST([GNULIB_MBSSTR])
new file mode 100644
--- /dev/null
+++ b/modules/mbsnlen
@@ -0,0 +1,28 @@
+Description:
+mbsnlen() function: Determine the number of multibyte characters in a string.
+
+Files:
+lib/mbsnlen.c
+m4/mbsnlen.m4
+m4/mbrtowc.m4
+
+Depends-on:
+mbiter
+string
+
+configure.ac:
+gl_FUNC_MBSNLEN
+gl_STRING_MODULE_INDICATOR([mbsnlen])
+
+Makefile.am:
+lib_SOURCES += mbsnlen.c
+
+Include:
+<string.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+
--- a/modules/string
+++ b/modules/string
@@ -24,6 +24,7 @@
 	  sed -e 's|@''ABSOLUTE_STRING_H''@|$(ABSOLUTE_STRING_H)|g' \
 	      -e 's/@''HAVE_INCLUDE_NEXT''@/$(HAVE_INCLUDE_NEXT)/g' \
 	      -e 's|@''GNULIB_MBSLEN''@|$(GNULIB_MBSLEN)|g' \
+	      -e 's|@''GNULIB_MBSNLEN''@|$(GNULIB_MBSNLEN)|g' \
 	      -e 's|@''GNULIB_MBSCHR''@|$(GNULIB_MBSCHR)|g' \
 	      -e 's|@''GNULIB_MBSRCHR''@|$(GNULIB_MBSRCHR)|g' \
 	      -e 's|@''GNULIB_MBSSTR''@|$(GNULIB_MBSSTR)|g' \