BAN: Add function to decode utf-8 encoded strings
works for 3 byte unicode characters
This commit is contained in:
parent
d9c05b7378
commit
a845f8696c
|
@ -0,0 +1,28 @@
|
|||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace BAN
|
||||
{
|
||||
|
||||
static constexpr uint16_t utf8_to_codepoint(uint8_t* bytes, size_t count)
|
||||
{
|
||||
if (count > 3)
|
||||
return 0xFFFF;
|
||||
|
||||
for (size_t i = 1; i < count; i++)
|
||||
if ((bytes[i] & 0xC0) != 0x80)
|
||||
return 0xFFFF;
|
||||
|
||||
switch (count)
|
||||
{
|
||||
case 1: return bytes[0];
|
||||
case 2: return ((bytes[0] & 0x1F) << 6) | (bytes[1] & 0x3F);
|
||||
case 3: return ((bytes[0] & 0x1F) << 12) | ((bytes[1] & 0x3F) << 6) | (bytes[2] & 0x3F);
|
||||
}
|
||||
|
||||
return 0xFFFF;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue