add Encoding::decode()

This commit is contained in:
josh committed 2016-08-15 22:14:47 -04:00
1 parent b2bed71ee4
commit f659c14242
4 files changed
+66 -2

No files matched your search

+60
View File
@@ -96,3 +96,63 @@ const uint8_t * Encoding::beginning_of_code_point(Type type, const uint8_t * enc
return nullptr;
}
uint32_t Encoding::decode(Type type, const uint8_t * encoded)
{
switch (type)
{
case UTF_8:
{
const uint8_t c = *encoded;
uint8_t following_bytes = 0u;
uint32_t v;
if ((c & 0x80u) == 0u)
{
return c;
}
else if ((c & 0xE0u) == 0xC0u)
{
v = c & 0x1Fu;
following_bytes = 1u;
}
else if ((c & 0xF0u) == 0xE0u)
{
v = c & 0x0Fu;
following_bytes = 2u;
}
else if ((c & 0xF8u) == 0xF0u)
{
v = c & 0x07u;
following_bytes = 3u;
}
else if ((c & 0xFCu) == 0xF8u)
{
v = c & 0x03u;
following_bytes = 4u;
}
else if ((c & 0xFEu) == 0xFCu)
{
v = c & 0x01u;
following_bytes = 5u;
}
else
{
return 0u;
}
while (following_bytes-- > 0u)
{
encoded++;
v <<= 6u;
v |= *encoded & 0x3Fu;
}
return v;
}
break;
case CP_1252:
/* TODO: map byte to code point */
return *encoded;
break;
}
return 0u;
}