summaryrefslogtreecommitdiff
path: root/lib/std/utf.myr
blob: b5244c17d4cc8c5be559af285fbd58f0e6b51c3d (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use "extremum"
use "chartype"
use "die"
use "types"

pkg std =
	const Badchar	: char	= -1
	const Maxcharlen : size = 4
	const Maxcharval : char = 0x10FFFF


	const charlen	: (chr : char -> size)
	const encode	: (buf : byte[:], chr : char -> size)
	const decode	: (buf : byte[:] -> char)
	const charstep	: (str : byte[:] -> (char, byte[:]))
	const graphemestep : (str : byte[:] -> (byte[:], byte[:]))

	const strcellwidth : (str : byte[:] -> size)
;;

const charlen = {c
	if c < 0x80
		-> 1
	elif c < 0x800
		-> 2
	elif c < 0x10000
		-> 3
	elif c < 0x200000
		-> 4
	else
		-> 1 /* attempt to resync */
	;;
}

const encode = {buf, c
	var len
	var mark

	len = charlen(c)
	if len < 0 || buf.len < len
		-> -1
	;;

	if (len == 1)
		mark = 0
	else
		mark = ((1 << (8 - len) - 1) ^ 0xff : char)
	;;

	for var i = len - 1; i > 0; i--
		buf[i] = (c & 0x3f | 0x80 : byte)
		c >>= 6
	;;

	buf[0] = (c | mark : byte)
	-> len
}

const decode = {buf
	var c
	var b

	(c, b) = charstep(buf)
	-> c
}

const graphemestep = {str
	var len = 0
	var rest = str
	var c
	var cn = 0
	var width = 0

	while rest.len > 0
		(c, rest) = charstep(rest)
		cn = cellwidth(c)

		if (c == '\r' || c == '\n' || c == '\t')
			if len == 0
				-> (str[:1], str[1:])
			else
				-> (str[:len], str[len:])
			;;
		elif (cn > 0 || c == Badchar) && len > 0
			-> (str[:len], str[len:])
		elif c == Badchar
			-> (str[:1], str[1:])
		else
			len += charlen(c)
			width += cn
		;;
	;;

	-> (str[:len], str[len:])
}

const charstep = {str
	var len
	var mask
	var chr
	var c
	var tmp

	if str.len == 0
		/* empty string: no resync needed */
		-> (Badchar, str)
	;;
	c = str[0]
	len = 0
	if c & 0x80 == 0	/* 0b0xxx_xxxx */
		len = 1
	elif c & 0xe0 == 0xc0	/* 0b110x_xxxx */
		len = 2
	elif c & 0xf0 == 0xe0 	/* 0b1110_xxxx */
		len = 3
	elif c & 0xf8 == 0xf0 	/* 0b1111_0xxx */
		len = 4
	else
		/* skip one char forward so we can try
		   resyncing the character stream */
		-> (Badchar, str[1:])
	;;

	if len == 0 || len > str.len
		/* again, we want to try to resync */
		-> (Badchar, str[1:])
	;;

	mask = (1 << (8 - len)) - 1
	chr = (c : uint32) & mask
	for var i = 1; i < len; i++
		tmp = (str[i] : uint32)
		chr = (chr << 6) | (tmp & 0x3f)
	;;

	-> ((chr : char), str[len:])
}

const strcellwidth = {str
	var s : byte[:] = str
	var c : char = Badchar
	var n : size = 0

	while s.len > 0
		(c, s) = charstep(s)
		if s.len != 0 && c == Badchar
			/* Something will probably be printed as U+FFFD */
			n++
		elif c < 0x20
			/* Control characters take 0 cells */
		elif c < 0x7f
			/* Bog standard ASCII takes 1 cell */
			n++
		elif c == 0x7f
			/* DEL is like a control character */
		else
			/* It's not ASCII, so ask chartype what to do */
			n += (abs(cellwidth(c)) : size)
		;;
	;;

	-> n
}