1 /*
2 Copyright (c) 2003 Corinna Vinschen <corinna@vinschen.de>
3  */
4 /*
5 FUNCTION
6 	<<wcswidth>>---number of column positions of a wide-character string
7 
8 INDEX
9 	wcswidth
10 
11 SYNOPSIS
12 	#include <wchar.h>
13 	int wcswidth(const wchar_t *<[pwcs]>, size_t <[n]>);
14 
15 DESCRIPTION
16 	The <<wcswidth>> function shall determine the number of column
17 	positions required for <[n]> wide-character codes (or fewer than <[n]>
18 	wide-character codes if a null wide-character code is encountered
19 	before <[n]> wide-character codes are exhausted) in the string pointed
20 	to by <[pwcs]>.
21 
22 RETURNS
23 	The <<wcswidth>> function either shall return 0 (if <[pwcs]> points to a
24 	null wide-character code), or return the number of column positions
25 	to be occupied by the wide-character string pointed to by <[pwcs]>, or
26 	return -1 (if any of the first <[n]> wide-character codes in the
27 	wide-character string pointed to by <[pwcs]> is not a printable
28 	wide-character code).
29 
30 PORTABILITY
31 <<wcswidth>> has been introduced in the Single UNIX Specification Volume 2.
32 <<wcswidth>> has been marked as an extension in the Single UNIX Specification Volume 3.
33 */
34 
35 #include <_ansi.h>
36 #include <wchar.h>
37 #include <stdint.h>
38 #include "local.h"
39 
40 int
wcswidth(const wchar_t * pwcs,size_t n)41 wcswidth (const wchar_t *pwcs,
42 	size_t n)
43 
44 {
45   int w, len = 0;
46   if (!pwcs || n == 0)
47     return 0;
48   do {
49     uint32_t wi = (uint32_t) *pwcs;
50 
51 #ifdef _MB_CAPABLE
52   wi = _jp2uc (wi);
53   /* First half of a surrogate pair? */
54   if (sizeof (wchar_t) == 2 && wi >= (uint32_t) 0xd800 && wi <= (uint32_t) 0xdbff)
55     {
56       uint32_t wi2;
57 
58       /* Extract second half and check for validity. */
59       if (--n == 0 || (wi2 = _jp2uc (*++pwcs)) < (uint32_t) 0xdc00 || wi2 > (uint32_t) 0xdfff)
60 	return -1;
61       /* Compute actual unicode value to use in call to __wcwidth. */
62       wi = (((wi & 0x3ff) << 10) | (wi2 & 0x3ff)) + 0x10000;
63     }
64 #endif /* _MB_CAPABLE */
65     if ((w = __wcwidth (wi)) < 0)
66       return -1;
67     len += w;
68   } while (*pwcs++ && --n > 0);
69   return len;
70 }
71