changeset 8083:c8815c37b48c

New module 'mbschr'.
author Bruno Haible <bruno@clisp.org>
date Mon, 05 Feb 2007 01:01:37 +0000
parents b3f03e45d0c8
children 26bd958c1cd1
files ChangeLog MODULES.html.sh lib/mbschr.c lib/string_.h m4/mbschr.m4 m4/string_h.m4 modules/mbschr modules/string
diffstat 8 files changed, 140 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2007-02-04  Bruno Haible  <bruno@clisp.org>
+
+	New module mbschr.
+	* modules/mbschr: New file.
+	* lib/mbschr.c: New file.
+	* lib/string_.h (strchr): Add a conditional link warning.
+	(mbschr): New declaration.
+	* m4/mbschr.m4: New file.
+	* m4/string_h.m4 (gl_STRING_MODULE_INDICATOR_DEFAULTS): Initialize
+	GNULIB_MBSCHR.
+	* modules/string (string.h): Also substitute GNULIB_MBSCHR.
+	* MODULES.html.sh (Internationalization functions): Add mbschr.
+
 2007-02-04  Paul Eggert  <eggert@cs.ucla.edu>
 
 	* lib/stdbool_.h: Mention that bool bit-fields aren't supported.
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -2161,6 +2161,7 @@
   func_module iconvme
   func_module localcharset
   func_module hard-locale
+  func_module mbschr
   func_module mbswidth
   func_module memcasecmp
   func_module memcoll
new file mode 100644
--- /dev/null
+++ b/lib/mbschr.c
@@ -0,0 +1,57 @@
+/* Searching a string for a character.
+   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>
+
+#if HAVE_MBRTOWC
+# include "mbuiter.h"
+#endif
+
+/* 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.  */
+char *
+mbschr (const char *string, int c)
+{
+#if HAVE_MBRTOWC
+  if (MB_CUR_MAX > 1
+      /* Optimization: We know that ASCII characters < 0x30 don't occur as
+	 part of multibyte characters longer than 1 byte.  Hence, if c < 0x30,
+	 the faster unibyte loop can be used.  */
+      && (unsigned char) c >= 0x30)
+    {
+      mbui_iterator_t iter;
+
+      for (mbui_init (iter, string);; mbui_advance (iter))
+	{
+	  if (mb_len (mbui_cur (iter)) == 1
+	      && (unsigned char) * mbui_cur_ptr (iter) == (unsigned char) c)
+	    break;
+	  if (!mbui_avail (iter))
+	    goto notfound;
+	}
+      return (char *) mbui_cur_ptr (iter);
+     notfound:
+      return NULL;
+    }
+  else
+#endif
+    return strchr (string, c);
+}
--- a/lib/string_.h
+++ b/lib/string_.h
@@ -148,6 +148,15 @@
      strncasecmp (a, b))
 #endif
 
+#if defined GNULIB_POSIXCHECK
+/* strchr() does not work with multibyte strings if the locale encoding is
+   GB18030 and the character to be searched is a digit.  */
+# undef strchr
+# define strchr(s,c) \
+    (GL_LINK_WARNING ("strchr cannot work correctly on character strings in some multibyte locales - use mbschr if you care about internationalization"), \
+     strchr (s, c))
+#endif
+
 /* Find the first occurrence of C in S or the final NUL byte.  */
 #if @GNULIB_STRCHRNUL@
 # if ! @HAVE_STRCHRNUL@
@@ -295,6 +304,20 @@
 # define strtok_r strtok_r_is_unportable__use_gnulib_module_strtok_r_for_portability
 #endif
 
+
+/* The following functions are not specified by POSIX.  They are gnulib
+   extensions.  */
+
+#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.
+   Unlike strchr(), this function works correctly in multibyte locales with
+   encodings such as GB18030.  */
+# define mbschr rpl_mbschr /* avoid collision with HP-UX function */
+extern char * mbschr (const char *string, int c);
+#endif
+
+
 #ifdef __cplusplus
 }
 #endif
new file mode 100644
--- /dev/null
+++ b/m4/mbschr.m4
@@ -0,0 +1,16 @@
+# mbschr.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_MBSCHR],
+[
+  gl_PREREQ_MBSCHR
+])
+
+# Prerequisites of lib/mbschr.c.
+AC_DEFUN([gl_PREREQ_MBSCHR], [
+  AC_REQUIRE([gl_FUNC_MBRTOWC])
+  :
+])
--- a/m4/string_h.m4
+++ b/m4/string_h.m4
@@ -70,4 +70,5 @@
   GNULIB_STRSTR=0;      AC_SUBST([GNULIB_STRSTR])
   GNULIB_STRCASESTR=0;  AC_SUBST([GNULIB_STRCASESTR])
   GNULIB_STRTOK_R=0;    AC_SUBST([GNULIB_STRTOK_R])
+  GNULIB_MBSCHR=0;      AC_SUBST([GNULIB_MBSCHR])
 ])
new file mode 100644
--- /dev/null
+++ b/modules/mbschr
@@ -0,0 +1,28 @@
+Description:
+mbschr() function: search a string for a character.
+
+Files:
+lib/mbschr.c
+m4/mbschr.m4
+m4/mbrtowc.m4
+
+Depends-on:
+mbuiter
+string
+
+configure.ac:
+gl_FUNC_MBSCHR
+gl_STRING_MODULE_INDICATOR([mbschr])
+
+Makefile.am:
+lib_SOURCES += mbschr.c
+
+Include:
+<string.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+
--- a/modules/string
+++ b/modules/string
@@ -21,6 +21,7 @@
 	rm -f $@-t $@
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''ABSOLUTE_STRING_H''@|$(ABSOLUTE_STRING_H)|g' \
+	      -e 's|@''GNULIB_MBSCHR''@|$(GNULIB_MBSCHR)|g' \
 	      -e 's|@''GNULIB_MEMMEM''@|$(GNULIB_MEMMEM)|g' \
 	      -e 's|@''GNULIB_MEMPCPY''@|$(GNULIB_MEMPCPY)|g' \
 	      -e 's|@''GNULIB_MEMRCHR''@|$(GNULIB_MEMRCHR)|g' \