From: Bruno Haible Date: Mon, 5 Feb 2007 01:01:37 +0000 (+0000) Subject: New module 'mbschr'. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=22a636592e5562bef231b878757c5a6efdadabea;hp=5b5d0e2fdd405118ae205d0764b474991ae2518c;p=pspp New module 'mbschr'. --- diff --git a/ChangeLog b/ChangeLog index 6cb9db336f..1a4c56a8ef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2007-02-04 Bruno Haible + + 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 * lib/stdbool_.h: Mention that bool bit-fields aren't supported. diff --git a/MODULES.html.sh b/MODULES.html.sh index d2ed19bcee..c22c4f8f4f 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -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 index 0000000000..084694d7a6 --- /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 , 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 + +/* 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); +} diff --git a/lib/string_.h b/lib/string_.h index 3947241815..e365810c8b 100644 --- a/lib/string_.h +++ b/lib/string_.h @@ -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 index 0000000000..92fb618478 --- /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]) + : +]) diff --git a/m4/string_h.m4 b/m4/string_h.m4 index 8e0be44f85..ab2efd4c92 100644 --- a/m4/string_h.m4 +++ b/m4/string_h.m4 @@ -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 index 0000000000..1407eeae1e --- /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: + + +License: +LGPL + +Maintainer: +Bruno Haible + diff --git a/modules/string b/modules/string index d0df673172..5e9bd11b16 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_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' \