Hi, For the use of the 'wwchar_t' type, some modules such as 'fnmatch' require the usual wide string functions to be available in a variant for 'wwchar_t'. This reminded me that gnulib does not yet have the wide string functions (wmemchr, wstrcat, wcstok, wcswidth, etc.) available. They need to be supported first, before we can implement the 'wwchar_t' variants.
Let's start with the 'wmemchr' function. I take the source code from the libutf8 library that I wrote in 1999; it's simpler than the source code found in glibc (which emphasizes all kinds of tricks for speed, such as loop unrolling, but which makes the code less readable). 2011-02-05 Bruno Haible <[email protected]> New module 'wmemchr'. * modules/wmemchr: New file. * lib/wchar.in.h (wmemchr): New declaration. * lib/wmemchr.c: New file. * lib/wmemchr-impl.h: New file, from libutf8 with modifications. * m4/wmemchr.m4: New file. * m4/wchar_h.m4 (gl_WCHAR_H_DEFAULTS): Initialize GNULIB_WMEMCHR, HAVE_WMEMCHR. * modules/wchar (Makefile.am): Substitute GNULIB_WMEMCHR, HAVE_WMEMCHR. * tests/test-wchar-c++.cc: Test the declaration of wmemchr. * doc/posix-functions/wmemchr.texi: Mention the new module. =============================== modules/wmemchr =============================== Description: wmemchr() function: search wide character array for a wide character. Files: lib/wmemchr.c lib/wmemchr-impl.h m4/wmemchr.m4 Depends-on: wchar configure.ac: gl_FUNC_WMEMCHR gl_WCHAR_MODULE_INDICATOR([wmemchr]) Makefile.am: Include: <wchar.h> License: LGPL Maintainer: Bruno Haible ================================ lib/wmemchr.c ================================ /* Search wide character array for a wide character. Copyright (C) 2011 Free Software Foundation, Inc. Written by Bruno Haible <[email protected]>, 2011. 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. */ #include <config.h> /* Specification. */ #include <wchar.h> #include "wmemchr-impl.h" ============================= lib/wmemchr-impl.h ============================= /* Search wide character array for a wide character. Copyright (C) 1999, 2011 Free Software Foundation, Inc. Written by Bruno Haible <[email protected]>, 1999. 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. */ wchar_t * wmemchr (const wchar_t *s, wchar_t c, size_t n) { for (; n > 0; s++, n--) { if (*s == c) return (wchar_t *) s; } return NULL; } ================================ m4/wmemchr.m4 ================================ # wmemchr.m4 serial 1 dnl Copyright (C) 2011 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_WMEMCHR], [ AC_REPLACE_FUNCS([wmemchr]) ]) =============================================================================== --- doc/posix-functions/wmemchr.texi.orig Sat Feb 5 12:16:25 2011 +++ doc/posix-functions/wmemchr.texi Sat Feb 5 12:08:32 2011 @@ -4,18 +4,18 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/wmemchr.html} -Gnulib module: --- +Gnulib module: wmemchr Portability problems fixed by Gnulib: @itemize +@item +This function is missing on some platforms: +HP-UX 11.00, IRIX 6.5, Solaris 2.6, Interix 3.5. @end itemize Portability problems not fixed by Gnulib: @itemize @item -This function is missing on some platforms: -HP-UX 11.00, IRIX 6.5, Solaris 2.6, Interix 3.5. -@item On AIX and Windows platforms, @code{wchar_t} is a 16-bit type and therefore cannot accommodate all Unicode characters. @end itemize --- lib/wchar.in.h.orig Sat Feb 5 12:16:25 2011 +++ lib/wchar.in.h Sat Feb 5 11:58:36 2011 @@ -428,6 +428,22 @@ #endif +/* Search N wide characters of S for C. */ +#if @GNULIB_WMEMCHR@ +# if !@HAVE_WMEMCHR@ +_GL_FUNCDECL_SYS (wmemchr, wchar_t *, (const wchar_t *s, wchar_t c, size_t n)); +# endif +_GL_CXXALIAS_SYS (wmemchr, wchar_t *, (const wchar_t *s, wchar_t c, size_t n)); +_GL_CXXALIASWARN (wmemchr); +#elif defined GNULIB_POSIXCHECK +# undef wmemchr +# if HAVE_RAW_DECL_WMEMCHR +_GL_WARN_ON_USE (wmemchr, "wmemchr is unportable - " + "use gnulib module wmemchr for portability"); +# endif +#endif + + #endif /* _GL_WCHAR_H */ #endif /* _GL_WCHAR_H */ #endif --- m4/wchar_h.m4.orig Sat Feb 5 12:16:25 2011 +++ m4/wchar_h.m4 Sat Feb 5 11:59:48 2011 @@ -7,7 +7,7 @@ dnl Written by Eric Blake. -# wchar_h.m4 serial 37 +# wchar_h.m4 serial 38 AC_DEFUN([gl_WCHAR_H], [ @@ -143,6 +143,7 @@ GNULIB_WCSRTOMBS=0; AC_SUBST([GNULIB_WCSRTOMBS]) GNULIB_WCSNRTOMBS=0; AC_SUBST([GNULIB_WCSNRTOMBS]) GNULIB_WCWIDTH=0; AC_SUBST([GNULIB_WCWIDTH]) + GNULIB_WMEMCHR=0; AC_SUBST([GNULIB_WMEMCHR]) dnl Assume proper GNU behavior unless another module says otherwise. HAVE_BTOWC=1; AC_SUBST([HAVE_BTOWC]) HAVE_MBSINIT=1; AC_SUBST([HAVE_MBSINIT]) @@ -153,6 +154,7 @@ HAVE_WCRTOMB=1; AC_SUBST([HAVE_WCRTOMB]) HAVE_WCSRTOMBS=1; AC_SUBST([HAVE_WCSRTOMBS]) HAVE_WCSNRTOMBS=1; AC_SUBST([HAVE_WCSNRTOMBS]) + HAVE_WMEMCHR=1; AC_SUBST([HAVE_WMEMCHR]) HAVE_DECL_WCTOB=1; AC_SUBST([HAVE_DECL_WCTOB]) HAVE_DECL_WCWIDTH=1; AC_SUBST([HAVE_DECL_WCWIDTH]) REPLACE_MBSTATE_T=0; AC_SUBST([REPLACE_MBSTATE_T]) --- modules/wchar.orig Sat Feb 5 12:16:25 2011 +++ modules/wchar Sat Feb 5 12:00:26 2011 @@ -41,6 +41,7 @@ -e 's|@''GNULIB_WCSRTOMBS''@|$(GNULIB_WCSRTOMBS)|g' \ -e 's|@''GNULIB_WCSNRTOMBS''@|$(GNULIB_WCSNRTOMBS)|g' \ -e 's|@''GNULIB_WCWIDTH''@|$(GNULIB_WCWIDTH)|g' \ + -e 's|@''GNULIB_WMEMCHR''@|$(GNULIB_WMEMCHR)|g' \ -e 's|@''HAVE_WINT_T''@|$(HAVE_WINT_T)|g' \ -e 's|@''HAVE_BTOWC''@|$(HAVE_BTOWC)|g' \ -e 's|@''HAVE_MBSINIT''@|$(HAVE_MBSINIT)|g' \ @@ -51,6 +52,7 @@ -e 's|@''HAVE_WCRTOMB''@|$(HAVE_WCRTOMB)|g' \ -e 's|@''HAVE_WCSRTOMBS''@|$(HAVE_WCSRTOMBS)|g' \ -e 's|@''HAVE_WCSNRTOMBS''@|$(HAVE_WCSNRTOMBS)|g' \ + -e 's|@''HAVE_WMEMCHR''@|$(HAVE_WMEMCHR)|g' \ -e 's|@''HAVE_DECL_WCTOB''@|$(HAVE_DECL_WCTOB)|g' \ -e 's|@''HAVE_DECL_WCWIDTH''@|$(HAVE_DECL_WCWIDTH)|g' \ -e 's|@''REPLACE_MBSTATE_T''@|$(REPLACE_MBSTATE_T)|g' \ --- tests/test-wchar-c++.cc.orig Sat Feb 5 12:16:26 2011 +++ tests/test-wchar-c++.cc Sat Feb 5 12:15:20 2011 @@ -75,6 +75,11 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::wcwidth, int, (wchar_t)); #endif +#if GNULIB_TEST_WMEMCHR +SIGNATURE_CHECK (GNULIB_NAMESPACE::wmemchr, wchar_t *, + (const wchar_t *, wchar_t, size_t)); +#endif + int main () -- In memoriam Neil Aggett <http://en.wikipedia.org/wiki/Neil_Aggett>
