This source file includes following definitions.
- charntorune
- chartorune
- isvalidcharntorune
- runetochar
- runelen
- runenlen
- fullrune
#include "utf.h"
#include "utfdef.h"
enum
{
Bit1 = 7,
Bitx = 6,
Bit2 = 5,
Bit3 = 4,
Bit4 = 3,
Bit5 = 2,
T1 = ((1<<(Bit1+1))-1) ^ 0xFF,
Tx = ((1<<(Bitx+1))-1) ^ 0xFF,
T2 = ((1<<(Bit2+1))-1) ^ 0xFF,
T3 = ((1<<(Bit3+1))-1) ^ 0xFF,
T4 = ((1<<(Bit4+1))-1) ^ 0xFF,
T5 = ((1<<(Bit5+1))-1) ^ 0xFF,
Rune1 = (1<<(Bit1+0*Bitx))-1,
Rune2 = (1<<(Bit2+1*Bitx))-1,
Rune3 = (1<<(Bit3+2*Bitx))-1,
Rune4 = (1<<(Bit4+3*Bitx))-1,
Maskx = (1<<Bitx)-1,
Testx = Maskx ^ 0xFF,
SurrogateMin = 0xD800,
SurrogateMax = 0xDFFF,
Bad = Runeerror,
};
int
charntorune(Rune *rune, const char *str, int length)
{
int c, c1, c2, c3;
long l;
if(length <= 0) {
goto badlen;
}
c = *(uchar*)str;
if(c < Tx) {
*rune = (Rune)c;
return 1;
}
if(length <= 1) {
goto badlen;
}
c1 = *(uchar*)(str+1) ^ Tx;
if(c1 & Testx)
goto bad;
if(c < T3) {
if(c < T2)
goto bad;
l = ((c << Bitx) | c1) & Rune2;
if(l <= Rune1)
goto bad;
*rune = (Rune)l;
return 2;
}
if(length <= 2) {
goto badlen;
}
c2 = *(uchar*)(str+2) ^ Tx;
if(c2 & Testx)
goto bad;
if(c < T4) {
l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
if(l <= Rune2)
goto bad;
if (SurrogateMin <= l && l <= SurrogateMax)
goto bad;
*rune = (Rune)l;
return 3;
}
if (length <= 3)
goto badlen;
c3 = *(uchar*)(str+3) ^ Tx;
if (c3 & Testx)
goto bad;
if (c < T5) {
l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
if (l <= Rune3 || l > Runemax)
goto bad;
*rune = (Rune)l;
return 4;
}
bad:
*rune = Bad;
return 1;
badlen:
*rune = Bad;
return 0;
}
int
chartorune(Rune *rune, const char *str)
{
int c, c1, c2, c3;
long l;
c = *(uchar*)str;
if(c < Tx) {
*rune = (Rune)c;
return 1;
}
c1 = *(uchar*)(str+1) ^ Tx;
if(c1 & Testx)
goto bad;
if(c < T3) {
if(c < T2)
goto bad;
l = ((c << Bitx) | c1) & Rune2;
if(l <= Rune1)
goto bad;
*rune = (Rune)l;
return 2;
}
c2 = *(uchar*)(str+2) ^ Tx;
if(c2 & Testx)
goto bad;
if(c < T4) {
l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
if(l <= Rune2)
goto bad;
if (SurrogateMin <= l && l <= SurrogateMax)
goto bad;
*rune = (Rune)l;
return 3;
}
c3 = *(uchar*)(str+3) ^ Tx;
if (c3 & Testx)
goto bad;
if (c < T5) {
l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
if (l <= Rune3 || l > Runemax)
goto bad;
*rune = (Rune)l;
return 4;
}
bad:
*rune = Bad;
return 1;
}
int
isvalidcharntorune(const char* str, int length, Rune* rune, int* consumed)
{
*consumed = charntorune(rune, str, length);
return *rune != Runeerror || *consumed == 3;
}
int
runetochar(char *str, const Rune *rune)
{
unsigned long c;
c = *rune;
if(c <= Rune1) {
str[0] = (char)c;
return 1;
}
if(c <= Rune2) {
str[0] = (char)(T2 | (c >> 1*Bitx));
str[1] = (char)(Tx | (c & Maskx));
return 2;
}
if (c > Runemax)
c = Runeerror;
if (SurrogateMin <= c && c <= SurrogateMax)
c = Runeerror;
if (c <= Rune3) {
str[0] = (char)(T3 | (c >> 2*Bitx));
str[1] = (char)(Tx | ((c >> 1*Bitx) & Maskx));
str[2] = (char)(Tx | (c & Maskx));
return 3;
}
str[0] = (char)(T4 | (c >> 3*Bitx));
str[1] = (char)(Tx | ((c >> 2*Bitx) & Maskx));
str[2] = (char)(Tx | ((c >> 1*Bitx) & Maskx));
str[3] = (char)(Tx | (c & Maskx));
return 4;
}
int
runelen(Rune rune)
{
char str[10];
return runetochar(str, &rune);
}
int
runenlen(const Rune *r, int nrune)
{
int nb, c;
nb = 0;
while(nrune--) {
c = (int)*r++;
if (c <= Rune1)
nb++;
else if (c <= Rune2)
nb += 2;
else if (c <= Rune3)
nb += 3;
else
nb += 4;
}
return nb;
}
int
fullrune(const char *str, int n)
{
if (n > 0) {
int c = *(uchar*)str;
if (c < Tx)
return 1;
if (n > 1) {
if (c < T3)
return 1;
if (n > 2) {
if (c < T4 || n > 3)
return 1;
}
}
}
return 0;
}