xref: /plan9/sys/src/ape/lib/utf/rune.c (revision e94a8e9b44de11e62dfd4566245554f8bf8e0295)
1 /*
2  * The authors of this software are Rob Pike and Ken Thompson.
3  *              Copyright (c) 2002 by Lucent Technologies.
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose without fee is hereby granted, provided that this entire notice
6  * is included in all copies of any software which is or includes a copy
7  * or modification of this software and in all copies of the supporting
8  * documentation for such software.
9  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
10  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
11  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
12  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
13  */
14 #include <stdarg.h>
15 #include <string.h>
16 #include "utf.h"
17 #include "utfdef.h"
18 
19 enum
20 {
21 	Bit1	= 7,
22 	Bitx	= 6,
23 	Bit2	= 5,
24 	Bit3	= 4,
25 	Bit4	= 3,
26 	Bit5	= 2,
27 
28 	T1	= ((1<<(Bit1+1))-1) ^ 0xFF,	/* 0000 0000 */
29 	Tx	= ((1<<(Bitx+1))-1) ^ 0xFF,	/* 1000 0000 */
30 	T2	= ((1<<(Bit2+1))-1) ^ 0xFF,	/* 1100 0000 */
31 	T3	= ((1<<(Bit3+1))-1) ^ 0xFF,	/* 1110 0000 */
32 	T4	= ((1<<(Bit4+1))-1) ^ 0xFF,	/* 1111 0000 */
33 	T5	= ((1<<(Bit5+1))-1) ^ 0xFF,	/* 1111 1000 */
34 
35 	Rune1	= (1<<(Bit1+0*Bitx))-1,		/* 0000 0000 0000 0000 0111 1111 */
36 	Rune2	= (1<<(Bit2+1*Bitx))-1,		/* 0000 0000 0000 0111 1111 1111 */
37 	Rune3	= (1<<(Bit3+2*Bitx))-1,		/* 0000 0000 1111 1111 1111 1111 */
38 	Rune4	= (1<<(Bit4+3*Bitx))-1,		/* 0001 1111 1111 1111 1111 1111 */
39 
40 	Maskx	= (1<<Bitx)-1,			/* 0011 1111 */
41 	Testx	= Maskx ^ 0xFF,			/* 1100 0000 */
42 
43 	SurrogateMin	= 0xD800,
44 	SurrogateMax	= 0xDFFF,
45 
46 	Bad	= Runeerror,
47 };
48 
49 int
chartorune(Rune * rune,char * str)50 chartorune(Rune *rune, char *str)
51 {
52 	int c, c1, c2, c3;
53 	long l;
54 
55 	/*
56 	 * one character sequence
57 	 *	00000-0007F => T1
58 	 */
59 	c = *(uchar*)str;
60 	if(c < Tx) {
61 		*rune = c;
62 		return 1;
63 	}
64 
65 	/*
66 	 * two character sequence
67 	 *	00080-007FF => T2 Tx
68 	 */
69 	c1 = *(uchar*)(str+1) ^ Tx;
70 	if(c1 & Testx)
71 		goto bad;
72 	if(c < T3) {
73 		if(c < T2)
74 			goto bad;
75 		l = ((c << Bitx) | c1) & Rune2;
76 		if(l <= Rune1)
77 			goto bad;
78 		*rune = l;
79 		return 2;
80 	}
81 
82 	/*
83 	 * three character sequence
84 	 *	00800-0FFFF => T3 Tx Tx
85 	 */
86 	c2 = *(uchar*)(str+2) ^ Tx;
87 
88 	if(c2 & Testx)
89 		goto bad;
90 	if(c < T4) {
91 		l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
92 		if(l <= Rune2)
93 			goto bad;
94 		if (SurrogateMin <= l && l <= SurrogateMax)
95 			goto bad;
96 		*rune = l;
97 		return 3;
98 	}
99 
100 	/*
101 	 * four character sequence
102 	 *	10000-10FFFF => T4 Tx Tx Tx
103 	 */
104 	if(UTFmax >= 4) {
105 		c3 = *(uchar*)(str+3) ^ Tx;
106 		if(c3 & Testx)
107 			goto bad;
108 		if(c < T5) {
109 			l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
110 			if(l <= Rune3)
111 				goto bad;
112 			if(l > Runemax)
113 				goto bad;
114 			*rune = l;
115 			return 4;
116 		}
117 	}
118 
119 	/*
120 	 * bad decoding
121 	 */
122 bad:
123 	*rune = Bad;
124 	return 1;
125 }
126 
127 int
runetochar(char * str,Rune * rune)128 runetochar(char *str, Rune *rune)
129 {
130 	long c;
131 
132 	/*
133 	 * one character sequence
134 	 *	00000-0007F => 00-7F
135 	 */
136 	c = *rune;
137 	if(c <= Rune1) {
138 		str[0] = c;
139 		return 1;
140 	}
141 
142 	/*
143 	 * two character sequence
144 	 *	0080-07FF => T2 Tx
145 	 */
146 	if(c <= Rune2) {
147 		str[0] = T2 | (c >> 1*Bitx);
148 		str[1] = Tx | (c & Maskx);
149 		return 2;
150 	}
151 	/*
152 	 * If the Rune is out of range or a surrogate half, convert it to the error rune.
153 	 * Do this test here because the error rune encodes to three bytes.
154 	 * Doing it earlier would duplicate work, since an out of range
155 	 * Rune wouldn't have fit in one or two bytes.
156 	 */
157 	if (c > Runemax)
158 		c = Runeerror;
159 	if (SurrogateMin <= c && c <= SurrogateMax)
160 		c = Runeerror;
161 
162 	/*
163 	 * three character sequence
164 	 *	0800-FFFF => T3 Tx Tx
165 	 */
166 	if (c <= Rune3) {
167 		str[0] = T3 |  (c >> 2*Bitx);
168 		str[1] = Tx | ((c >> 1*Bitx) & Maskx);
169 		str[2] = Tx |  (c & Maskx);
170 		return 3;
171 	}
172 
173 	/*
174 	 * four character sequence (21-bit value)
175 	 *     10000-1FFFFF => T4 Tx Tx Tx
176 	 */
177 	str[0] = T4 | (c >> 3*Bitx);
178 	str[1] = Tx | ((c >> 2*Bitx) & Maskx);
179 	str[2] = Tx | ((c >> 1*Bitx) & Maskx);
180 	str[3] = Tx | (c & Maskx);
181 	return 4;
182 }
183 
184 int
runelen(long c)185 runelen(long c)
186 {
187 	Rune rune;
188 	char str[10];
189 
190 	rune = c;
191 	return runetochar(str, &rune);
192 }
193 
194 int
runenlen(Rune * r,int nrune)195 runenlen(Rune *r, int nrune)
196 {
197 	int nb, c;
198 
199 	nb = 0;
200 	while(nrune--) {
201 		c = *r++;
202 		if(c <= Rune1)
203 			nb++;
204 		else if(c <= Rune2)
205 			nb += 2;
206 		else if(c <= Rune3)
207  			nb += 3;
208 		else
209 			nb += 4;
210 	}
211 	return nb;
212 }
213 
214 int
fullrune(char * str,int n)215 fullrune(char *str, int n)
216 {
217 	int c;
218 	if(n <= 0)
219 		return 0;
220 	c = *(uchar*)str;
221 	if(c < Tx)
222 		return 1;
223 	if(c < T3)
224 		return n >= 2;
225 	if(c < T4)
226 		return n >= 3;
227 	return n >= 4;
228 }
229