3 * - By John Hodge (thePowersGang)
6 * - UTF-16 Translation/Manipulation
13 int ReadUTF16(const Uint16 *Str16, Uint32 *Codepoint)
15 if( 0xD800 < *Str16 && *Str16 <= 0xDFFF )
17 // UTF-16 surrogate pair
18 // > 0xDC00 is the second word
19 if( Str16[0] > 0xDC00 ) {
23 if( Str16[1] < 0xD800 || Str16[1] >= 0xDC00 ) {
28 *Codepoint = 0x10000 + (((Str16[0] & 0x3FF) << 10) | (Str16[1] & 0x3FF));
37 size_t UTF16_ConvertToUTF8(size_t DestLen, char *Dest, size_t SrcLen, const Uint16 *Source)
40 for( ; *Source && SrcLen --; Source ++ )
42 // TODO: Decode/Reencode
43 if( Dest && len < DestLen )
47 if( Dest && len < DestLen )
52 int UTF16_CompareWithUTF8Ex(size_t Str16Len, const Uint16 *Str16, const char *Str8, int bCaseInsensitive)
54 int pos16 = 0, pos8 = 0;
55 const Uint8 *str8 = (const Uint8 *)Str8;
57 while( pos16 < Str16Len && Str16[pos16] && str8[pos8] )
60 pos16 += ReadUTF16(Str16+pos16, &cp16);
61 pos8 += ReadUTF8(str8 + pos8, &cp8);
62 if( bCaseInsensitive ) {
67 LOG("cp16 = %x, cp8 = %x", cp16, cp8);
68 if(cp16 == cp8) continue ;
75 if( pos16 == Str16Len )
77 if( Str16[pos16] && str8[pos8] )
85 int UTF16_CompareWithUTF8(size_t Str16Len, const Uint16 *Str16, const char *Str8)
87 return UTF16_CompareWithUTF8Ex(Str16Len, Str16, Str8, 0);
90 int UTF16_CompareWithUTF8CI(size_t Str16Len, const Uint16 *Str16, const char *Str8)
92 return UTF16_CompareWithUTF8Ex(Str16Len, Str16, Str8, 1);