From 098a75dc51caa98f369d98a9809d773bc45329aa Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Fri, 15 May 2009 11:40:28 +0000 Subject: * libc/string/local.h: New file. * libc/string/wcswidth.c (wcswidth): Convert japanese wide characters to Unicode here. Handle surrogate pairs for UTF-16 systems. Call __wcwidth rather than wcwidth. * libc/string/wcwidth.c: New implementation using Markus Kuhn's wcwidth implementation for Unicode. (bisearch): New static function. (__wcwidth): New function. Take wint_t rather than wchar_t as parameter to allow full Unicode handling on UTF-16 systems. Move old wcwidth implementation here for non-multibyte aware systems. (wcwidth): Convert japanese wide characters to Unicode here. Call __wcwidth rather than using iswprint/iswcntrl. --- newlib/libc/string/wcswidth.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'newlib/libc/string/wcswidth.c') diff --git a/newlib/libc/string/wcswidth.c b/newlib/libc/string/wcswidth.c index 8a9670eb1..6c0efe63f 100644 --- a/newlib/libc/string/wcswidth.c +++ b/newlib/libc/string/wcswidth.c @@ -37,6 +37,7 @@ PORTABILITY #include <_ansi.h> #include +#include "local.h" int _DEFUN (wcswidth, (pwcs, n), @@ -48,7 +49,23 @@ _DEFUN (wcswidth, (pwcs, n), if (!pwcs || n == 0) return 0; do { - if ((w = wcwidth (*pwcs)) < 0) + wint_t wi = *pwcs; + +#ifdef _MB_CAPABLE + wi = _jp2uc (wi); + /* First half of a surrogate pair? */ + if (sizeof (wchar_t) == 2 && wi >= 0xd800 && wi <= 0xdbff) + { + wint_t wi2; + + /* Extract second half and check for validity. */ + if (--n == 0 || (wi2 = _jp2uc (*++pwcs)) < 0xdc00 || wi2 > 0xdfff) + return -1; + /* Compute actual unicode value to use in call to __wcwidth. */ + wi = (((wi & 0x3ff) << 10) | (wi2 & 0x3ff)) + 0x10000; + } +#endif /* _MB_CAPABLE */ + if ((w = __wcwidth (wi)) < 0) return -1; len += w; } while (*pwcs++ && --n > 0); -- cgit v1.2.3