5 #include "UTFConvert.h"
8 static Byte kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
10 // These functions are for UTF8 <-> UTF16 conversion.
12 bool ConvertUTF8ToUnicode(const AString &src, UString &dest)
15 for(int i = 0; i < src.Length();)
17 Byte c = (Byte)src[i++];
26 for (numAdds = 1; numAdds < 5; numAdds++)
27 if (c < kUtf8Limits[numAdds])
29 UInt32 value = (c - kUtf8Limits[numAdds - 1]);
32 if (i >= src.Length())
34 Byte c2 = (Byte)src[i++];
35 if (c2 < 0x80 || c2 >= 0xC0)
43 dest += (wchar_t)(value);
47 if (value >= 0x100000)
49 dest += (wchar_t)(0xD800 + (value >> 10));
50 dest += (wchar_t)(0xDC00 + (value & 0x3FF));
56 bool ConvertUnicodeToUTF8(const UString &src, AString &dest)
59 for(int i = 0; i < src.Length();)
61 UInt32 value = (UInt32)src[i++];
67 if (value >= 0xD800 && value < 0xE000)
71 if (i >= src.Length())
73 UInt32 c2 = (UInt32)src[i++];
74 if (c2 < 0xDC00 || c2 >= 0xE000)
76 value = ((value - 0xD800) << 10) | (c2 - 0xDC00);
79 for (numAdds = 1; numAdds < 5; numAdds++)
80 if (value < (((UInt32)1) << (numAdds * 5 + 6)))
82 dest += (char)(kUtf8Limits[numAdds - 1] + (value >> (6 * numAdds)));
86 dest += (char)(0x80 + ((value >> (6 * numAdds)) & 0x3F));