forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libutf.c
53 lines (50 loc) · 1.22 KB
/
libutf.c
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
/* libutf8 © 2012-2015 Connor Lane Smith <cls@lubutu.com> */
#include "libutf.h"
#include "util.h"
int
runelen(Rune r)
{
if(r <= 0x7F)
return 1;
else if(r <= 0x07FF)
return 2;
else if(r <= 0xD7FF)
return 3;
else if(r <= 0xDFFF)
return 0; /* surrogate character */
else if(r <= 0xFFFD)
return 3;
else if(r <= 0xFFFF)
return 0; /* illegal character */
else if(r <= Runemax)
return 4;
else
return 0; /* rune too large */
}
int
runetochar(char *s, const Rune *p)
{
Rune r = *p;
switch(runelen(r)) {
case 1: /* 0aaaaaaa */
s[0] = r;
return 1;
case 2: /* 00000aaa aabbbbbb */
s[0] = 0xC0 | ((r & 0x0007C0) >> 6); /* 110aaaaa */
s[1] = 0x80 | (r & 0x00003F); /* 10bbbbbb */
return 2;
case 3: /* aaaabbbb bbcccccc */
s[0] = 0xE0 | ((r & 0x00F000) >> 12); /* 1110aaaa */
s[1] = 0x80 | ((r & 0x000FC0) >> 6); /* 10bbbbbb */
s[2] = 0x80 | (r & 0x00003F); /* 10cccccc */
return 3;
case 4: /* 000aaabb bbbbcccc ccdddddd */
s[0] = 0xF0 | ((r & 0x1C0000) >> 18); /* 11110aaa */
s[1] = 0x80 | ((r & 0x03F000) >> 12); /* 10bbbbbb */
s[2] = 0x80 | ((r & 0x000FC0) >> 6); /* 10cccccc */
s[3] = 0x80 | (r & 0x00003F); /* 10dddddd */
return 4;
default:
return 0; /* error */
}
}