New module 'mbschr'.
authorBruno Haible <bruno@clisp.org>
Mon, 5 Feb 2007 01:01:37 +0000 (01:01 +0000)
committerBruno Haible <bruno@clisp.org>
Mon, 5 Feb 2007 01:01:37 +0000 (01:01 +0000)
ChangeLog
MODULES.html.sh
lib/mbschr.c [new file with mode: 0644]
lib/string_.h
m4/mbschr.m4 [new file with mode: 0644]
m4/string_h.m4
modules/mbschr [new file with mode: 0644]
modules/string

index 6cb9db336ff64db9d98fb837ebf25f93d76012d6..1a4c56a8ef3683e7b0db4433bfa874dfa66c8842 100644 (file)
--- 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.
index d2ed19bcee4e80fad60a6984c3169aa6c877e99b..c22c4f8f4f23123519bae5025e27d237bb0c0d87 100755 (executable)
@@ -2161,6 +2161,7 @@ func_all_modules ()
   func_module iconvme
   func_module localcharset
   func_module hard-locale
+  func_module mbschr
   func_module mbswidth
   func_module memcasecmp
   func_module memcoll
diff --git a/lib/mbschr.c b/lib/mbschr.c
new file mode 100644 (file)
index 0000000..084694d
--- /dev/null
@@ -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);
+}
index 39472418155cba74747402f28056a20277cb4d9c..e365810c8b28ce391fce348bf3ec379c79d31f5a 100644 (file)
@@ -148,6 +148,15 @@ extern int strncasecmp (char const *__s1, char const *__s2, size_t __n);
      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 @@ extern char *strtok_r (char *restrict __s, char const *restrict __sep,
 # 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
diff --git a/m4/mbschr.m4 b/m4/mbschr.m4
new file mode 100644 (file)
index 0000000..92fb618
--- /dev/null
@@ -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])
+  :
+])
index 8e0be44f8586c753704fef68ed83ac0f97129290..ab2efd4c928c70f80ad29abbf07491f4504a3591 100644 (file)
@@ -70,4 +70,5 @@ AC_DEFUN([gl_STRING_MODULE_INDICATOR_DEFAULTS],
   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])
 ])
diff --git a/modules/mbschr b/modules/mbschr
new file mode 100644 (file)
index 0000000..1407eea
--- /dev/null
@@ -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
+
index d0df67317269a04d815f2d34a3cd9e9a7cf29ae2..5e9bd11b16f43055b98b9d96cd720940a8677e56 100644 (file)
@@ -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_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' \