conterm/libc/rune.c

167 lines
2.8 KiB
C
Raw Permalink Normal View History

2005-08-08 16:50:13 +04:00
#include <u.h>
#include <libc.h>
#define Bit(i) (7-(i))
/* N 0's preceded by i 1's, T(Bit(2)) is 1100 0000 */
#define T(i) (((1 << (Bit(i)+1))-1) ^ 0xFF)
/* 0000 0000 0000 0111 1111 1111 */
#define RuneX(i) ((1 << (Bit(i) + ((i)-1)*Bitx))-1)
2005-08-08 16:50:13 +04:00
enum
{
Bitx = Bit(1),
2005-08-08 16:50:13 +04:00
Tx = T(1), /* 1000 0000 */
Rune1 = (1<<(Bit(0)+0*Bitx))-1, /* 0000 0000 0000 0000 0111 1111 */
2005-08-08 16:50:13 +04:00
Maskx = (1<<Bitx)-1, /* 0011 1111 */
Testx = Maskx ^ 0xFF, /* 1100 0000 */
2005-08-08 16:50:13 +04:00
SurrogateMin = 0xD800,
SurrogateMax = 0xDFFF,
2005-08-08 16:50:13 +04:00
Bad = Runeerror,
};
int
chartorune(Rune *rune, char *str)
{
int c[UTFmax], i;
Rune l;
2005-08-08 16:50:13 +04:00
/*
* N character sequence
2005-08-08 16:50:13 +04:00
* 00000-0007F => T1
* 00080-007FF => T2 Tx
* 00800-0FFFF => T3 Tx Tx
* 10000-10FFFF => T4 Tx Tx Tx
2005-08-08 16:50:13 +04:00
*/
c[0] = *(uchar*)(str);
if(c[0] < Tx){
*rune = c[0];
return 1;
2005-08-08 16:50:13 +04:00
}
l = c[0];
2005-08-08 16:50:13 +04:00
for(i = 1; i < UTFmax; i++) {
c[i] = *(uchar*)(str+i);
c[i] ^= Tx;
if(c[i] & Testx)
2005-08-08 16:50:13 +04:00
goto bad;
l = (l << Bitx) | c[i];
if(c[0] < T(i + 2)) {
l &= RuneX(i + 1);
if(i == 1) {
if(c[0] < T(2) || l <= Rune1)
goto bad;
} else if(l <= RuneX(i) || l > Runemax)
goto bad;
if (i == 2 && SurrogateMin <= l && l <= SurrogateMax)
goto bad;
*rune = l;
return i + 1;
}
2005-08-08 16:50:13 +04:00
}
/*
* bad decoding
*/
bad:
*rune = Bad;
return 1;
}
int
runetochar(char *str, Rune *rune)
{
int i, j;
Rune c;
2005-08-08 16:50:13 +04:00
c = *rune;
if(c <= Rune1) {
str[0] = c;
return 1;
}
/*
* one character sequence
* 00000-0007F => 00-7F
2005-08-08 16:50:13 +04:00
* two character sequence
* 0080-07FF => T2 Tx
* three character sequence
* 0800-FFFF => T3 Tx Tx
* four character sequence (21-bit value)
* 10000-1FFFFF => T4 Tx Tx Tx
* If the Rune is out of range or a surrogate half,
* convert it to the error rune.
* Do this test when i==3 because the error rune encodes to three bytes.
* Doing it earlier would duplicate work, since an out of range
* Rune wouldn't have fit in one or two bytes.
2005-08-08 16:50:13 +04:00
*/
for(i = 2; i < UTFmax + 1; i++){
if(i == 3){
if(c > Runemax)
c = Runeerror;
if(SurrogateMin <= c && c <= SurrogateMax)
c = Runeerror;
}
if (c <= RuneX(i) || i == UTFmax ) {
str[0] = T(i) | (c >> (i - 1)*Bitx);
for(j = 1; j < i; j++)
str[j] = Tx | ((c >> (i - j - 1)*Bitx) & Maskx);
return i;
}
}
return UTFmax;
2005-08-08 16:50:13 +04:00
}
int
runelen(long c)
{
Rune rune;
char str[10];
rune = c;
return runetochar(str, &rune);
}
int
runenlen(Rune *r, int nrune)
{
int nb, i;
Rune c;
2005-08-08 16:50:13 +04:00
nb = 0;
while(nrune--) {
c = *r++;
if(c <= Rune1){
2005-08-08 16:50:13 +04:00
nb++;
} else {
for(i = 2; i < UTFmax + 1; i++)
if(c <= RuneX(i) || i == UTFmax){
nb += i;
break;
}
}
2005-08-08 16:50:13 +04:00
}
return nb;
}
int
fullrune(char *str, int n)
{
int i;
Rune c;
if(n <= 0)
return 0;
c = *(uchar*)str;
if(c < Tx)
return 1;
for(i = 3; i < UTFmax + 1; i++)
if(c < T(i))
return n >= i - 1;
return n >= UTFmax;
2005-08-08 16:50:13 +04:00
}