Imported Upstream version 0.66.1
[hcoop/debian/courier-authlib.git] / libs / unicode / unicode_wcwidth.c
1 #include "unicode_config.h"
2 #include "unicode.h"
3
4 #include "eastasianwidth.h"
5 #include "linebreaktab_internal.h"
6
7 #include <stdlib.h>
8
9 int unicode_wcwidth(unicode_char c)
10 {
11 size_t b=0;
12 size_t e=sizeof(unicode_wcwidth_tab)/sizeof(unicode_wcwidth_tab[0]);
13
14 while (b < e)
15 {
16 size_t n=b + (e-b)/2;
17
18 if (c >= unicode_wcwidth_tab[n][0])
19 {
20 if (c <= unicode_wcwidth_tab[n][1])
21 return 2;
22 b=n+1;
23 }
24 else
25 {
26 e=n;
27 }
28 }
29
30 switch (unicode_lb_lookup(c)) {
31 case UNICODE_LB_BK:
32 case UNICODE_LB_CR:
33 case UNICODE_LB_LF:
34 case UNICODE_LB_CM:
35 case UNICODE_LB_NL:
36 case UNICODE_LB_WJ:
37 case UNICODE_LB_ZW:
38 return 0;
39 default:
40 break;
41 }
42 return 1;
43 }
44
45 int unicode_isspace(unicode_char ch)
46 {
47 if (ch == 9)
48 return 1;
49
50 switch (unicode_lb_lookup(ch)) {
51 case UNICODE_LB_BK:
52 case UNICODE_LB_CR:
53 case UNICODE_LB_LF:
54 case UNICODE_LB_NL:
55 case UNICODE_LB_SP:
56 return 1;
57 }
58
59 return 0;
60 }
61
62 size_t unicode_wcwidth_str(const unicode_char *c)
63 {
64 size_t w=0;
65
66 while (*c)
67 w += unicode_wcwidth(*c++);
68
69
70 return w;
71 }