]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Add the mbswidth function to the lib.
authorBruno Haible <bruno@clisp.org>
Wed, 4 Apr 2001 23:53:21 +0000 (23:53 +0000)
committerBruno Haible <bruno@clisp.org>
Wed, 4 Apr 2001 23:53:21 +0000 (23:53 +0000)
lib/ChangeLog
lib/Makefile.am
lib/mbswidth.c [new file with mode: 0644]
lib/mbswidth.h [new file with mode: 0644]
m4/ChangeLog
m4/Makefile.am
m4/mbrtowc.m4 [new file with mode: 0644]
m4/mbstate_t.m4 [new file with mode: 0644]
m4/mbswidth.m4 [new file with mode: 0644]

index 842f3b2d87ac0ee8fb3b07dbe86b473892de151b..bd8687d08d979b0637e0796665657fc2ccb08670 100644 (file)
@@ -1,3 +1,10 @@
+2001-04-04  Bruno Haible  <haible@clisp.cons.org>
+
+       * mbswidth.h: New file, from textutils-2.0.13 with modifications.
+       * mbswidth.c: Likewise.
+       * Makefile.am (noinst_HEADERS): Add mbswidth.h.
+       (libnlsut_a_SOURCES): Add mbswidth.c.
+
 2001-04-04  Bruno Haible  <haible@clisp.cons.org>
 
        * linebreak.h (UC_BREAK_UNDEFINED): New enum value.
index 4df9dd6d4f954762f2308c7557f6a1459da1b639..3dee1f848b417279d5b281f92dd0008ffb1d7989 100644 (file)
@@ -27,13 +27,14 @@ strcspn.c strncasecmp.c strstr.c strtol.c strtoul.c vasprintf.c \
 gen-lbrkprop.c 3level.h
 
 libnlsut_a_SOURCES = basename.c c-ctype.c concatpath.c fstrcmp.c \
-getopt.c getopt1.c hash.c linebreak.c localcharset.c obstack.c xgetcwd.c \
-xmalloc.c xstrdup.c
+getopt.c getopt1.c hash.c linebreak.c localcharset.c mbswidth.c obstack.c \
+xgetcwd.c xmalloc.c xstrdup.c
 
 libnlsut_a_LIBADD = @ALLOCA@ @LIBOBJS@
 
 noinst_HEADERS = c-ctype.h error.h fstrcmp.h getline.h getopt.h hash.h \
-lbrkprop.h linebreak.h obstack.h printf-parse.h printf.h system.h pathmax.h
+lbrkprop.h linebreak.h mbswidth.h obstack.h printf-parse.h printf.h system.h \
+pathmax.h
 
 DEFS = -DLIBDIR=\"$(libdir)\" @DEFS@
 INCLUDES = -I. -I$(srcdir) -I.. -I../intl
diff --git a/lib/mbswidth.c b/lib/mbswidth.c
new file mode 100644 (file)
index 0000000..b6b0113
--- /dev/null
@@ -0,0 +1,210 @@
+/* Determine the number of screen columns needed for a string.
+   Copyright (C) 2000-2001 Free Software Foundation, Inc.
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+/* Written by Bruno Haible <haible@clisp.cons.org>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Get MB_CUR_MAX.  */
+#if HAVE_STDLIB_H
+# include <stdlib.h>
+#endif
+
+#if HAVE_STRING_H
+# include <string.h>
+#endif
+
+/* Get isprint().  */
+#include <ctype.h>
+
+/* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth().  */
+#if HAVE_WCHAR_H
+# include <wchar.h>
+#endif
+
+/* Get iswprint().  */
+#if HAVE_WCTYPE_H
+# include <wctype.h>
+#endif
+#if !defined iswprint && !HAVE_ISWPRINT
+# define iswprint(wc) 1
+#endif
+#if !defined iswcntrl && !HAVE_ISWCNTRL
+# define iswcntrl(wc) 0
+#endif
+
+#ifndef HAVE_DECL_WCWIDTH
+"this configure-time declaration test was not run"
+#endif
+#if !HAVE_DECL_WCWIDTH
+int wcwidth ();
+#endif
+
+#ifndef wcwidth
+# if !HAVE_WCWIDTH
+/* wcwidth doesn't exist, so assume all printable characters have
+   width 1.  */
+#  define wcwidth(wc) ((wc) == 0 ? 0 : iswprint (wc) ? 1 : -1)
+# endif
+#endif
+
+/* Get ISPRINT.  */
+#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
+# define IN_CTYPE_DOMAIN(c) 1
+#else
+# define IN_CTYPE_DOMAIN(c) isascii(c)
+#endif
+/* Undefine to protect against the definition in wctype.h of solaris2.6.   */
+#undef ISPRINT
+#define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
+#undef ISCNTRL
+#define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
+
+#include "mbswidth.h"
+
+/* Returns the number of columns needed to represent the multibyte
+   character string pointed to by STRING.  If a non-printable character
+   occurs, and MBSW_REJECT_UNPRINTABLE is specified, -1 is returned.
+   With flags = MBSW_REJECT_INVALID | MBSW_REJECT_UNPRINTABLE, this is
+   the multibyte analogon of the wcswidth function.  */
+int
+mbswidth (const char *string, int flags)
+{
+  return mbsnwidth (string, strlen (string), flags);
+}
+
+/* Returns the number of columns needed to represent the multibyte
+   character string pointed to by STRING of length NBYTES.  If a
+   non-printable character occurs, and MBSW_REJECT_UNPRINTABLE is
+   specified, -1 is returned.  */
+int
+mbsnwidth (const char *string, size_t nbytes, int flags)
+{
+  const char *p = string;
+  const char *plimit = p + nbytes;
+  int width;
+
+  width = 0;
+#if HAVE_MBRTOWC
+  if (MB_CUR_MAX > 1)
+    {
+      while (p < plimit)
+       switch (*p)
+         {
+           case ' ': case '!': case '"': case '#': case '%':
+           case '&': case '\'': case '(': case ')': case '*':
+           case '+': case ',': case '-': case '.': case '/':
+           case '0': case '1': case '2': case '3': case '4':
+           case '5': case '6': case '7': case '8': case '9':
+           case ':': case ';': case '<': case '=': case '>':
+           case '?':
+           case 'A': case 'B': case 'C': case 'D': case 'E':
+           case 'F': case 'G': case 'H': case 'I': case 'J':
+           case 'K': case 'L': case 'M': case 'N': case 'O':
+           case 'P': case 'Q': case 'R': case 'S': case 'T':
+           case 'U': case 'V': case 'W': case 'X': case 'Y':
+           case 'Z':
+           case '[': case '\\': case ']': case '^': case '_':
+           case 'a': case 'b': case 'c': case 'd': case 'e':
+           case 'f': case 'g': case 'h': case 'i': case 'j':
+           case 'k': case 'l': case 'm': case 'n': case 'o':
+           case 'p': case 'q': case 'r': case 's': case 't':
+           case 'u': case 'v': case 'w': case 'x': case 'y':
+           case 'z': case '{': case '|': case '}': case '~':
+             /* These characters are printable ASCII characters.  */
+             p++;
+             width++;
+             break;
+           default:
+             /* If we have a multibyte sequence, scan it up to its end.  */
+             {
+               mbstate_t mbstate;
+               memset (&mbstate, 0, sizeof mbstate);
+               do
+                 {
+                   wchar_t wc;
+                   size_t bytes;
+                   int w;
+
+                   bytes = mbrtowc (&wc, p, plimit - p, &mbstate);
+
+                   if (bytes == (size_t) -1)
+                     /* An invalid multibyte sequence was encountered.  */
+                     {
+                       if (!(flags & MBSW_REJECT_INVALID))
+                         {
+                           p++;
+                           width++;
+                           break;
+                         }
+                       else
+                         return -1;
+                     }
+
+                   if (bytes == (size_t) -2)
+                     /* An incomplete multibyte character at the end.  */
+                     {
+                       if (!(flags & MBSW_REJECT_INVALID))
+                         {
+                           p = plimit;
+                           width++;
+                           break;
+                         }
+                       else
+                         return -1;
+                     }
+
+                   if (bytes == 0)
+                     /* A null wide character was encountered.  */
+                     bytes = 1;
+
+                   w = wcwidth (wc);
+                   if (w >= 0)
+                     /* A printable multibyte character.  */
+                     width += w;
+                   else
+                     /* An unprintable multibyte character.  */
+                     if (!(flags & MBSW_REJECT_UNPRINTABLE))
+                       width += (iswcntrl (wc) ? 0 : 1);
+                     else
+                       return -1;
+
+                   p += bytes;
+                 }
+               while (! mbsinit (&mbstate));
+             }
+             break;
+         }
+      return width;
+    }
+#endif
+
+  while (p < plimit)
+    {
+      unsigned char c = (unsigned char) *p++;
+
+      if (ISPRINT (c))
+       width++;
+      else if (!(flags & MBSW_REJECT_UNPRINTABLE))
+       width += (ISCNTRL (c) ? 0 : 1);
+      else
+       return -1;
+    }
+  return width;
+}
diff --git a/lib/mbswidth.h b/lib/mbswidth.h
new file mode 100644 (file)
index 0000000..38911da
--- /dev/null
@@ -0,0 +1,43 @@
+/* Determine the number of screen columns needed for a string.
+   Copyright (C) 2000-2001 Free Software Foundation, Inc.
+
+   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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#ifndef PARAMS
+# if defined (__GNUC__) || __STDC__
+#  define PARAMS(Args) Args
+# else
+#  define PARAMS(Args) ()
+# endif
+#endif
+
+/* Optional flags to influence mbswidth/mbsnwidth behavior.  */
+
+/* If this bit is set, return -1 upon finding an invalid or incomplete
+   character.  Otherwise, assume invalid characters have width 1.  */
+#define MBSW_REJECT_INVALID 1
+
+/* If this bit is set, return -1 upon finding a non-printable character.
+   Otherwise, assume unprintable characters have width 0 if they are
+   control characters and 1 otherwise.  */
+#define MBSW_REJECT_UNPRINTABLE        2
+
+/* Returns the number of screen columns needed for STRING.  */
+#define mbswidth gnu_mbswidth  /* avoid clash with UnixWare 7.1.1 function */
+extern int mbswidth PARAMS ((const char *string, int flags));
+
+/* Returns the number of screen columns needed for the NBYTES bytes
+   starting at BUF.  */
+extern int mbsnwidth PARAMS ((const char *buf, size_t nbytes, int flags));
index a51825ee50cd46e5883d22524576ead8a658e5d5..ef98e4540d1263ea403d390e9fa61bc90aafd9ac 100644 (file)
@@ -1,3 +1,10 @@
+2001-04-04  Bruno Haible  <haible@clisp.cons.org>
+
+       * mbswidth.m4: New file, from textutils-2.0.13 with modifications.
+       * mbrtowc.m4: Likewise.
+       * mbstate_t.m4: Likewise.
+       * Makefile.am (EXTRA_DIST): Add them.
+
 2001-03-29  Bruno Haible  <haible@clisp.cons.org>
 
        * gettext-0.10.36 released.
index e930fd74eb0405c86bb6adde23e88d76bfffc64e..cad7a0017ad1a382b7beb728db742f9d7ecf78b9 100644 (file)
@@ -8,5 +8,5 @@ aclocal_DATA = codeset.m4 gettext.m4 iconv.m4 isc-posix.m4 lcmessage.m4 progtest
 #   |sed 's/@$/%/;s/@/ \\@/g' |tr @% '\012\012'
 EXTRA_DIST = README \
 c-bs-a.m4 codeset.m4 getline.m4 gettext.m4 iconv.m4 inttypes_h.m4 \
-isc-posix.m4 lcmessage.m4 libtool.m4 progtest.m4 setlocale.m4 signed.m4 \
-uintmax_t.m4 ulonglong.m4
+isc-posix.m4 lcmessage.m4 libtool.m4 mbrtowc.m4 mbstate_t.m4 mbswidth.m4 \
+progtest.m4 setlocale.m4 signed.m4 uintmax_t.m4 ulonglong.m4
diff --git a/m4/mbrtowc.m4 b/m4/mbrtowc.m4
new file mode 100644 (file)
index 0000000..371febf
--- /dev/null
@@ -0,0 +1,18 @@
+#serial 2
+
+dnl From Paul Eggert
+
+AC_DEFUN(jm_FUNC_MBRTOWC,
+[
+  AC_CACHE_CHECK([whether mbrtowc and mbstate_t are properly declared],
+    jm_cv_func_mbrtowc,
+    [AC_TRY_LINK(
+       [#include <wchar.h>],
+       [mbstate_t state; return ! (sizeof state && mbrtowc);],
+       jm_cv_func_mbrtowc=yes,
+       jm_cv_func_mbrtowc=no)])
+  if test $jm_cv_func_mbrtowc = yes; then
+    AC_DEFINE(HAVE_MBRTOWC, 1,
+      [Define to 1 if mbrtowc and mbstate_t are properly declared.])
+  fi
+])
diff --git a/m4/mbstate_t.m4 b/m4/mbstate_t.m4
new file mode 100644 (file)
index 0000000..a185a7d
--- /dev/null
@@ -0,0 +1,29 @@
+# serial 8
+
+# From Paul Eggert.
+
+# BeOS 5 has <wchar.h> but does not define mbstate_t,
+# so you can't declare an object of that type.
+# Check for this incompatibility with Standard C.
+
+# Include stdlib.h first, because otherwise this test would fail on Linux
+# (at least glibc-2.1.3) because the "_XOPEN_SOURCE 500" definition elicits
+# a syntax error in wchar.h due to the use of undefined __int32_t.
+
+AC_DEFUN(AC_MBSTATE_T,
+  [
+   AC_CHECK_HEADERS(stdlib.h)
+
+   AC_CACHE_CHECK([for mbstate_t], ac_cv_type_mbstate_t,
+    [AC_TRY_COMPILE([
+#if HAVE_STDLIB_H
+# include <stdlib.h>
+#endif
+#include <wchar.h>],
+      [mbstate_t x; return sizeof x;],
+      ac_cv_type_mbstate_t=yes,
+      ac_cv_type_mbstate_t=no)])
+   if test $ac_cv_type_mbstate_t = no; then
+     AC_DEFINE(mbstate_t, int,
+              [Define to a type if <wchar.h> does not define.])
+   fi])
diff --git a/m4/mbswidth.m4 b/m4/mbswidth.m4
new file mode 100644 (file)
index 0000000..74fce1b
--- /dev/null
@@ -0,0 +1,29 @@
+#serial 4
+
+dnl autoconf tests required for use of mbswidth.c
+dnl From Bruno Haible.
+
+AC_DEFUN(jm_PREREQ_MBSWIDTH,
+[
+  AC_REQUIRE([AC_HEADER_STDC])
+  AC_CHECK_HEADERS(limits.h stdlib.h string.h wchar.h wctype.h)
+  AC_CHECK_FUNCS(isascii iswcntrl iswprint wcwidth)
+  jm_FUNC_MBRTOWC
+
+  AC_CACHE_CHECK([whether wcwidth is declared], ac_cv_have_decl_wcwidth,
+    [AC_TRY_COMPILE([
+#if HAVE_WCHAR_H
+# include <wchar.h>
+#endif
+], [
+#ifndef wcwidth
+  char *p = (char *) wcwidth;
+#endif
+], ac_cv_have_decl_wcwidth=yes, ac_cv_have_decl_wcwidth=no)])
+  if test $ac_cv_have_decl_wcwidth = yes; then
+    AC_DEFINE(HAVE_DECL_WCWIDTH, 1,
+      [Define to 1 if you have the declaration of wcwidth().])
+  fi
+
+  AC_MBSTATE_T
+])