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.
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 */
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, /* 0011 1111 1111 1111 1111 1111 */
40 Maskx = (1<<Bitx)-1, /* 0011 1111 */
41 Testx = Maskx ^ 0xFF, /* 1100 0000 */
47 chartorune(Rune *rune, char *str)
53 * one character sequence
63 * two character sequence
66 c1 = *(uchar*)(str+1) ^ Tx;
72 l = ((c << Bitx) | c1) & Rune2;
80 * three character sequence
81 * 0800-FFFF => T3 Tx Tx
83 c2 = *(uchar*)(str+2) ^ Tx;
87 l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
95 * four character sequence
96 * 10000-10FFFF => T4 Tx Tx Tx
99 c3 = *(uchar*)(str+3) ^ Tx;
103 l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
122 runetochar(char *str, Rune *rune)
131 * one character sequence
132 * 00000-0007F => 00-7F
140 * two character sequence
144 str[0] = T2 | (c >> 1*Bitx);
145 str[1] = Tx | (c & Maskx);
150 * three character sequence
151 * 0800-FFFF => T3 Tx Tx
154 str[0] = T3 | (c >> 2*Bitx);
155 str[1] = Tx | ((c >> 1*Bitx) & Maskx);
156 str[2] = Tx | (c & Maskx);
161 * four character sequence
162 * 10000-1FFFFF => T4 Tx Tx Tx
164 str[0] = T4 | (c >> 3*Bitx);
165 str[1] = Tx | ((c >> 2*Bitx) & Maskx);
166 str[2] = Tx | ((c >> 1*Bitx) & Maskx);
167 str[3] = Tx | (c & Maskx);
178 return runetochar(str, &rune);
182 runenlen(Rune *r, int nrune)
195 if(c <= Rune3 || c > Runemax)
204 fullrune(char *str, int n)
215 if(UTFmax == 3 || c < T4)