summaryrefslogtreecommitdiffstats
path: root/unicode/unicode_wcwidth.c
blob: bea01f5fa88976cb42ca0d0d4f4f6aaa398a2dc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "unicode_config.h"
#include "courier-unicode.h"

#include "eastasianwidth.h"
#include "linebreaktab_internal.h"

#include <stdlib.h>

int unicode_wcwidth(unicode_char c)
{
	size_t b=0;
	size_t e=sizeof(unicode_wcwidth_tab)/sizeof(unicode_wcwidth_tab[0]);

	while (b < e)
	{
		size_t n=b + (e-b)/2;

		if (c >= unicode_wcwidth_tab[n][0])
		{
			if (c <= unicode_wcwidth_tab[n][1])
				return 2;
			b=n+1;
		}
		else
		{
			e=n;
		}
	}

	switch (unicode_lb_lookup(c)) {
	case UNICODE_LB_BK:
	case UNICODE_LB_CR:
	case UNICODE_LB_LF:
	case UNICODE_LB_CM:
	case UNICODE_LB_NL:
	case UNICODE_LB_WJ:
	case UNICODE_LB_ZW:
		return 0;
	default:
		break;
	}
	return 1;
}

size_t unicode_wcwidth_str(const unicode_char *c)
{
	size_t w=0;

	while (*c)
		w += unicode_wcwidth(*c++);


	return w;
}