Game Development Reference
In-Depth Information
15. The decoder uses the following function to read individual character codes:
int DecodeNextUTF8Char()
{
// the first byte of the character and the result
int c, r;
if ( FIndex >= FLength )
return FIndex == FLength ?
UTF8_LINE_END : UTF8_DECODE_ERROR;
c = NextUTF8();
if ( ( c & 0x80 ) == 0 ) { return c; }
if ( ( c & 0xE0 ) == 0xC0 )
{
int c1 = ContUTF8();
if ( c1 < 0 ) { return UTF8_DECODE_ERROR; }
r = ( ( c & 0x1F ) << 6 ) | c1;
return r >= 128 ? r : UTF8_DECODE_ERROR;
}
if ( ( c & 0xF0 ) == 0xE0 )
{
int c1 = ContUTF8(), c2 = ContUTF8();
if ( c1 < 0 || c2 < 0 ) { return UTF8_DECODE_ERROR; }
r = ( ( c & 0x0F ) << 12 ) | ( c1 << 6 ) | c2;
return r>=2048&&(r<55296||r>57343)?
r:UTF8_DECODE_ERROR;
}
if ( ( c & 0xF8 ) == 0xF0 )
{
int c1 = ContUTF8(), c2 = ContUTF8(), c3 = ContUTF8();
if (c1 < 0||c2 < 0||c3< 0) { return UTF8_DECODE_ERROR; }
r = (( c & 0x0F ) << 18) | (c1 << 12) | (c2 << 6) | c3;
return r>=65536 && r<=1114111 ? r: UTF8_DECODE_ERROR;
}
return UTF8_DECODE_ERROR;
}
The source code of DecodeNextUTF8Char() was taken from the
Linderdaum Engine at http://www.linderdaum.com .
 
Search WWH ::




Custom Search