3 * - By John Hodge (thePowersGang)
6 * - UTF-16 Translation/Manipulation
12 int ReadUTF16(const Uint16 *Str16, Uint32 *Codepoint)
14 if( 0xD800 < *Str16 && *Str16 <= 0xDFFF )
16 // UTF-16 surrogate pair
17 // > 0xDC00 is the second word
18 if( Str16[0] > 0xDC00 ) {
22 if( Str16[1] < 0xD800 || Str16[1] >= 0xDC00 ) {
27 *Codepoint = 0x10000 + (((Str16[0] & 0x3FF) << 10) | (Str16[1] & 0x3FF));
36 size_t UTF16_ConvertToUTF8(size_t DestLen, char *Dest, size_t SrcLen, const Uint16 *Source)
39 for( ; *Source && SrcLen --; Source ++ )
41 // TODO: Decode/Reencode
42 if( Dest && len < DestLen )
46 if( Dest && len < DestLen )
51 int UTF16_CompareWithUTF8Ex(size_t Str16Len, const Uint16 *Str16, const char *Str8, int bCaseInsensitive)
53 int pos16 = 0, pos8 = 0;
54 const Uint8 *str8 = (const Uint8 *)Str8;
56 while( pos16 < Str16Len && Str16[pos16] && str8[pos8] )
59 pos16 += ReadUTF16(Str16+pos16, &cp16);
60 pos8 += ReadUTF8(str8 + pos8, &cp8);
61 if( bCaseInsensitive ) {
66 LOG("cp16 = %x, cp8 = %x", cp16, cp8);
67 if(cp16 == cp8) continue ;
74 if( pos16 == Str16Len )
76 if( Str16[pos16] && str8[pos8] )
84 int UTF16_CompareWithUTF8(size_t Str16Len, const Uint16 *Str16, const char *Str8)
86 return UTF16_CompareWithUTF8Ex(Str16Len, Str16, Str8, 0);
89 int UTF16_CompareWithUTF8CI(size_t Str16Len, const Uint16 *Str16, const char *Str8)
91 return UTF16_CompareWithUTF8Ex(Str16Len, Str16, Str8, 1);