diff options
author | Bruno Haible <bruno@clisp.org> | 2001-04-04 23:53:21 +0000 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2001-04-04 23:53:21 +0000 |
commit | 32cb998e0258a29dedca89f67276931992676924 (patch) | |
tree | f21d5973db7f07c0aadc8eebb097f0f00d4ce757 | |
parent | b2b841f8bda96fee7fa80a1f678fc12f37e3d605 (diff) | |
download | external_gettext-32cb998e0258a29dedca89f67276931992676924.zip external_gettext-32cb998e0258a29dedca89f67276931992676924.tar.gz external_gettext-32cb998e0258a29dedca89f67276931992676924.tar.bz2 |
Add the mbswidth function to the lib.
-rw-r--r-- | lib/ChangeLog | 7 | ||||
-rw-r--r-- | lib/Makefile.am | 7 | ||||
-rw-r--r-- | lib/mbswidth.c | 210 | ||||
-rw-r--r-- | lib/mbswidth.h | 43 | ||||
-rw-r--r-- | m4/ChangeLog | 7 | ||||
-rw-r--r-- | m4/Makefile.am | 4 | ||||
-rw-r--r-- | m4/mbrtowc.m4 | 18 | ||||
-rw-r--r-- | m4/mbstate_t.m4 | 29 | ||||
-rw-r--r-- | m4/mbswidth.m4 | 29 |
9 files changed, 349 insertions, 5 deletions
diff --git a/lib/ChangeLog b/lib/ChangeLog index 842f3b2..bd8687d 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,5 +1,12 @@ 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. (u8_width_linebreaks, u16_width_linebreaks, u32_width_linebreaks, mbs_width_linebreaks): Add overrides argument. diff --git a/lib/Makefile.am b/lib/Makefile.am index 4df9dd6..3dee1f8 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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 index 0000000..b6b0113 --- /dev/null +++ b/lib/mbswidth.c @@ -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 index 0000000..38911da --- /dev/null +++ b/lib/mbswidth.h @@ -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)); diff --git a/m4/ChangeLog b/m4/ChangeLog index a51825e..ef98e45 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -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. diff --git a/m4/Makefile.am b/m4/Makefile.am index e930fd7..cad7a00 100644 --- a/m4/Makefile.am +++ b/m4/Makefile.am @@ -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 index 0000000..371febf --- /dev/null +++ b/m4/mbrtowc.m4 @@ -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 index 0000000..a185a7d --- /dev/null +++ b/m4/mbstate_t.m4 @@ -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 index 0000000..74fce1b --- /dev/null +++ b/m4/mbswidth.m4 @@ -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 +]) |