New module 'wcswidth'.
authorBruno Haible <bruno@clisp.org>
Sun, 6 Feb 2011 14:51:55 +0000 (15:51 +0100)
committerBruno Haible <bruno@clisp.org>
Mon, 7 Feb 2011 22:36:08 +0000 (23:36 +0100)
* 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
doc/posix-functions/wcswidth.texi
lib/wchar.in.h
lib/wcswidth-impl.h [new file with mode: 0644]
lib/wcswidth.c [new file with mode: 0644]
m4/wchar_h.m4
m4/wcswidth.m4 [new file with mode: 0644]
modules/wchar
modules/wcswidth [new file with mode: 0644]
tests/test-wchar-c++.cc

index c6b8a83..e227879 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,21 @@
 2011-02-06  Bruno Haible  <bruno@clisp.org>
 
+       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  <bruno@clisp.org>
+
        New module 'wcstok'.
        * modules/wcstok: New file.
        * lib/wchar.in.h (wcstok): New declaration.
index 27d0e8d..cb61a9a 100644 (file)
@@ -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
index 13e4d7f..e2010a8 100644 (file)
@@ -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 (file)
index 0000000..dc1a60c
--- /dev/null
@@ -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 <bruno@clisp.org>, 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/>.  */
+
+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 (file)
index 0000000..3133c84
--- /dev/null
@@ -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 <bruno@clisp.org>, 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 "wcswidth-impl.h"
index f320dd7..6255ff3 100644 (file)
@@ -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 (file)
index 0000000..80ccf3f
--- /dev/null
@@ -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
+])
index bd9719f..2e2d065 100644 (file)
@@ -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 (file)
index 0000000..2b748eb
--- /dev/null
@@ -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:
+<wchar.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
index e8fd2ac..9b28e31 100644 (file)
@@ -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 ()