From cbe8a0a3fe09047b99e5c0cfb67f45634d036487 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 6 Feb 2011 15:51:55 +0100 Subject: [PATCH] New module 'wcswidth'. * modules/wcswidth: New file. * lib/wchar.in.h (wcswidth): New declaration. * lib/wcswidth.c: New file. * lib/wcswidth-impl.h: New file, from libutf8 with modifications. * m4/wcswidth.m4: New file. * m4/wchar_h.m4 (gl_WCHAR_H): Test whether wcswidth is declared. (gl_WCHAR_H_DEFAULTS): Initialize GNULIB_WCSWIDTH, HAVE_WCSWIDTH, REPLACE_WCSWIDTH. * modules/wchar (Makefile.am): Substitute GNULIB_WCSWIDTH, HAVE_WCSWIDTH, REPLACE_WCSWIDTH. * tests/test-wchar-c++.cc: Test the declaration of wcswidth. * doc/posix-functions/wcswidth.texi: Mention the new module. --- ChangeLog | 16 ++++++++++++++++ doc/posix-functions/wcswidth.texi | 12 ++++++++---- lib/wchar.in.h | 26 ++++++++++++++++++++++++++ lib/wcswidth-impl.h | 38 ++++++++++++++++++++++++++++++++++++++ lib/wcswidth.c | 23 +++++++++++++++++++++++ m4/wchar_h.m4 | 5 ++++- m4/wcswidth.m4 | 24 ++++++++++++++++++++++++ modules/wchar | 3 +++ modules/wcswidth | 27 +++++++++++++++++++++++++++ tests/test-wchar-c++.cc | 4 ++++ 10 files changed, 173 insertions(+), 5 deletions(-) create mode 100644 lib/wcswidth-impl.h create mode 100644 lib/wcswidth.c create mode 100644 m4/wcswidth.m4 create mode 100644 modules/wcswidth diff --git a/ChangeLog b/ChangeLog index c6b8a83cd..e22787963 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,21 @@ 2011-02-06 Bruno Haible + New module 'wcswidth'. + * modules/wcswidth: New file. + * lib/wchar.in.h (wcswidth): New declaration. + * lib/wcswidth.c: New file. + * lib/wcswidth-impl.h: New file, from libutf8 with modifications. + * m4/wcswidth.m4: New file. + * m4/wchar_h.m4 (gl_WCHAR_H): Test whether wcswidth is declared. + (gl_WCHAR_H_DEFAULTS): Initialize GNULIB_WCSWIDTH, HAVE_WCSWIDTH, + REPLACE_WCSWIDTH. + * modules/wchar (Makefile.am): Substitute GNULIB_WCSWIDTH, + HAVE_WCSWIDTH, REPLACE_WCSWIDTH. + * tests/test-wchar-c++.cc: Test the declaration of wcswidth. + * doc/posix-functions/wcswidth.texi: Mention the new module. + +2011-02-06 Bruno Haible + New module 'wcstok'. * modules/wcstok: New file. * lib/wchar.in.h (wcstok): New declaration. diff --git a/doc/posix-functions/wcswidth.texi b/doc/posix-functions/wcswidth.texi index 27d0e8dd0..cb61a9a68 100644 --- a/doc/posix-functions/wcswidth.texi +++ b/doc/posix-functions/wcswidth.texi @@ -4,18 +4,22 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/wcswidth.html} -Gnulib module: --- +Gnulib module: wcswidth Portability problems fixed by Gnulib: @itemize +@item +This function is missing on some platforms: +OpenBSD 3.8, IRIX 5.3, Solaris 2.5.1, mingw, BeOS. +@item +This function handles combining characters in UTF-8 locales incorrectly on some +platforms: +MacOS X 10.3. @end itemize Portability problems not fixed by Gnulib: @itemize @item -This function is missing on some platforms: -OpenBSD 3.8, IRIX 5.3, Solaris 2.5.1, mingw, BeOS. -@item On AIX and Windows platforms, @code{wchar_t} is a 16-bit type and therefore cannot accommodate all Unicode characters. @end itemize diff --git a/lib/wchar.in.h b/lib/wchar.in.h index 13e4d7fed..e2010a8c0 100644 --- a/lib/wchar.in.h +++ b/lib/wchar.in.h @@ -891,6 +891,32 @@ _GL_WARN_ON_USE (wcstok, "wcstok is unportable - " #endif +/* Determine number of column positions required for first N wide + characters (or fewer if S ends before this) in S. */ +#if @GNULIB_WCSWIDTH@ +# if @REPLACE_WCSWIDTH@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef wcswidth +# define wcswidth rpl_wcswidth +# endif +_GL_FUNCDECL_RPL (wcswidth, int, (const wchar_t *s, size_t n)); +_GL_CXXALIAS_RPL (wcswidth, int, (const wchar_t *s, size_t n)); +# else +# if !@HAVE_WCSWIDTH@ +_GL_FUNCDECL_SYS (wcswidth, int, (const wchar_t *s, size_t n)); +# endif +_GL_CXXALIAS_SYS (wcswidth, int, (const wchar_t *s, size_t n)); +# endif +_GL_CXXALIASWARN (wcswidth); +#elif defined GNULIB_POSIXCHECK +# undef wcswidth +# if HAVE_RAW_DECL_WCSWIDTH +_GL_WARN_ON_USE (wcswidth, "wcswidth is unportable - " + "use gnulib module wcswidth for portability"); +# endif +#endif + + #endif /* _GL_WCHAR_H */ #endif /* _GL_WCHAR_H */ #endif diff --git a/lib/wcswidth-impl.h b/lib/wcswidth-impl.h new file mode 100644 index 000000000..dc1a60c53 --- /dev/null +++ b/lib/wcswidth-impl.h @@ -0,0 +1,38 @@ +/* Determine number of screen columns needed for a size-bounded wide string. + Copyright (C) 1999, 2011 Free Software Foundation, Inc. + Written by Bruno Haible , 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 . */ + +int +wcswidth (const wchar_t *s, size_t n) +{ + int count = 0; + for (; n > 0; s++, n--) + { + wchar_t c = *s; + if (c == (wchar_t)'\0') + break; + { + int width = wcwidth (c); + if (width < 0) + goto found_nonprinting; + count += width; + } + } + return count; + + found_nonprinting: + return -1; +} diff --git a/lib/wcswidth.c b/lib/wcswidth.c new file mode 100644 index 000000000..3133c8445 --- /dev/null +++ b/lib/wcswidth.c @@ -0,0 +1,23 @@ +/* Determine number of screen columns needed for a size-bounded wide string. + Copyright (C) 2011 Free Software Foundation, Inc. + Written by Bruno Haible , 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 . */ + +#include + +/* Specification. */ +#include + +#include "wcswidth-impl.h" diff --git a/m4/wchar_h.m4 b/m4/wchar_h.m4 index f320dd793..6255ff352 100644 --- a/m4/wchar_h.m4 +++ b/m4/wchar_h.m4 @@ -53,7 +53,7 @@ AC_DEFUN([gl_WCHAR_H], wcsrtombs wcsnrtombs wcwidth wmemchr wmemcmp wmemcpy wmemmove wmemset wcslen wcsnlen wcscpy wcpcpy wcsncpy wcpncpy wcscat wcsncat wcscmp wcsncmp wcscasecmp wcsncasecmp wcscoll wcsxfrm wcsdup wcschr wcsrchr - wcscspn wcsspn wcspbrk wcsstr wcstok + wcscspn wcsspn wcspbrk wcsstr wcstok wcswidth ]) ]) @@ -175,6 +175,7 @@ AC_DEFUN([gl_WCHAR_H_DEFAULTS], GNULIB_WCSPBRK=0; AC_SUBST([GNULIB_WCSPBRK]) GNULIB_WCSSTR=0; AC_SUBST([GNULIB_WCSSTR]) GNULIB_WCSTOK=0; AC_SUBST([GNULIB_WCSTOK]) + GNULIB_WCSWIDTH=0; AC_SUBST([GNULIB_WCSWIDTH]) dnl Assume proper GNU behavior unless another module says otherwise. HAVE_BTOWC=1; AC_SUBST([HAVE_BTOWC]) HAVE_MBSINIT=1; AC_SUBST([HAVE_MBSINIT]) @@ -212,6 +213,7 @@ AC_DEFUN([gl_WCHAR_H_DEFAULTS], HAVE_WCSPBRK=1; AC_SUBST([HAVE_WCSPBRK]) HAVE_WCSSTR=1; AC_SUBST([HAVE_WCSSTR]) HAVE_WCSTOK=1; AC_SUBST([HAVE_WCSTOK]) + HAVE_WCSWIDTH=1; AC_SUBST([HAVE_WCSWIDTH]) 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]) @@ -226,4 +228,5 @@ AC_DEFUN([gl_WCHAR_H_DEFAULTS], REPLACE_WCSRTOMBS=0; AC_SUBST([REPLACE_WCSRTOMBS]) REPLACE_WCSNRTOMBS=0; AC_SUBST([REPLACE_WCSNRTOMBS]) REPLACE_WCWIDTH=0; AC_SUBST([REPLACE_WCWIDTH]) + REPLACE_WCSWIDTH=0; AC_SUBST([REPLACE_WCSWIDTH]) ]) diff --git a/m4/wcswidth.m4 b/m4/wcswidth.m4 new file mode 100644 index 000000000..80ccf3f86 --- /dev/null +++ b/m4/wcswidth.m4 @@ -0,0 +1,24 @@ +# wcswidth.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_WCSWIDTH], +[ + AC_REQUIRE([gl_WCHAR_H_DEFAULTS]) + AC_REQUIRE([gl_FUNC_WCWIDTH]) + AC_CHECK_FUNCS_ONCE([wcswidth]) + if test $ac_cv_func_wcswidth = no; then + HAVE_WCSWIDTH=0 + else + if test $REPLACE_WCWIDTH = 1; then + dnl If wcwidth needed to be replaced, wcswidth needs to be replaced + dnl as well. + REPLACE_WCSWIDTH=1 + fi + fi + if test $HAVE_WCSWIDTH = 0 || test $REPLACE_WCSWIDTH = 1; then + AC_LIBOBJ([wcswidth]) + fi +]) diff --git a/modules/wchar b/modules/wchar index bd9719fc7..2e2d06507 100644 --- a/modules/wchar +++ b/modules/wchar @@ -68,6 +68,7 @@ wchar.h: wchar.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H) -e 's|@''GNULIB_WCSPBRK''@|$(GNULIB_WCSPBRK)|g' \ -e 's|@''GNULIB_WCSSTR''@|$(GNULIB_WCSSTR)|g' \ -e 's|@''GNULIB_WCSTOK''@|$(GNULIB_WCSTOK)|g' \ + -e 's|@''GNULIB_WCSWIDTH''@|$(GNULIB_WCSWIDTH)|g' \ -e 's|@''HAVE_WINT_T''@|$(HAVE_WINT_T)|g' \ -e 's|@''HAVE_BTOWC''@|$(HAVE_BTOWC)|g' \ -e 's|@''HAVE_MBSINIT''@|$(HAVE_MBSINIT)|g' \ @@ -105,6 +106,7 @@ wchar.h: wchar.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H) -e 's|@''HAVE_WCSPBRK''@|$(HAVE_WCSPBRK)|g' \ -e 's|@''HAVE_WCSSTR''@|$(HAVE_WCSSTR)|g' \ -e 's|@''HAVE_WCSTOK''@|$(HAVE_WCSTOK)|g' \ + -e 's|@''HAVE_WCSWIDTH''@|$(HAVE_WCSWIDTH)|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' \ @@ -119,6 +121,7 @@ wchar.h: wchar.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H) -e 's|@''REPLACE_WCSRTOMBS''@|$(REPLACE_WCSRTOMBS)|g' \ -e 's|@''REPLACE_WCSNRTOMBS''@|$(REPLACE_WCSNRTOMBS)|g' \ -e 's|@''REPLACE_WCWIDTH''@|$(REPLACE_WCWIDTH)|g' \ + -e 's|@''REPLACE_WCSWIDTH''@|$(REPLACE_WCSWIDTH)|g' \ -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \ -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \ -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \ diff --git a/modules/wcswidth b/modules/wcswidth new file mode 100644 index 000000000..2b748eb7a --- /dev/null +++ b/modules/wcswidth @@ -0,0 +1,27 @@ +Description: +wcswidth() function: determine number of screen columns needed for a +size-bounded wide string. + +Files: +lib/wcswidth.c +lib/wcswidth-impl.h +m4/wcswidth.m4 + +Depends-on: +wchar +wcwidth + +configure.ac: +gl_FUNC_WCSWIDTH +gl_WCHAR_MODULE_INDICATOR([wcswidth]) + +Makefile.am: + +Include: + + +License: +LGPL + +Maintainer: +Bruno Haible diff --git a/tests/test-wchar-c++.cc b/tests/test-wchar-c++.cc index e8fd2ac95..9b28e3194 100644 --- a/tests/test-wchar-c++.cc +++ b/tests/test-wchar-c++.cc @@ -207,6 +207,10 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::wcstok, wchar_t *, (wchar_t *, const wchar_t *, wchar_t **)); #endif +#if GNULIB_TEST_WCSWIDTH +SIGNATURE_CHECK (GNULIB_NAMESPACE::wcswidth, int, (const wchar_t *, size_t)); +#endif + int main () -- 2.11.0