Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

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