test: update
[vis.git] / libutf.c
blob2a82dc76c444d86532c46efb77ecfbc554feb48d
1 /* libutf8 © 2012-2015 Connor Lane Smith <cls@lubutu.com> */
2 #include "libutf.h"
3 #include "util.h"
5 int
6 runelen(Rune r)
8 if(r <= 0x7F)
9 return 1;
10 else if(r <= 0x07FF)
11 return 2;
12 else if(r <= 0xD7FF)
13 return 3;
14 else if(r <= 0xDFFF)
15 return 0; /* surrogate character */
16 else if(r <= 0xFFFD)
17 return 3;
18 else if(r <= 0xFFFF)
19 return 0; /* illegal character */
20 else if(r <= Runemax)
21 return 4;
22 else
23 return 0; /* rune too large */
26 int
27 runetochar(char *s, const Rune *p)
29 Rune r = *p;
31 switch(runelen(r)) {
32 case 1: /* 0aaaaaaa */
33 s[0] = r;
34 return 1;
35 case 2: /* 00000aaa aabbbbbb */
36 s[0] = 0xC0 | ((r & 0x0007C0) >> 6); /* 110aaaaa */
37 s[1] = 0x80 | (r & 0x00003F); /* 10bbbbbb */
38 return 2;
39 case 3: /* aaaabbbb bbcccccc */
40 s[0] = 0xE0 | ((r & 0x00F000) >> 12); /* 1110aaaa */
41 s[1] = 0x80 | ((r & 0x000FC0) >> 6); /* 10bbbbbb */
42 s[2] = 0x80 | (r & 0x00003F); /* 10cccccc */
43 return 3;
44 case 4: /* 000aaabb bbbbcccc ccdddddd */
45 s[0] = 0xF0 | ((r & 0x1C0000) >> 18); /* 11110aaa */
46 s[1] = 0x80 | ((r & 0x03F000) >> 12); /* 10bbbbbb */
47 s[2] = 0x80 | ((r & 0x000FC0) >> 6); /* 10cccccc */
48 s[3] = 0x80 | (r & 0x00003F); /* 10dddddd */
49 return 4;
50 default:
51 return 0; /* error */