BAN: Add function to decode utf-8 encoded strings

works for 3 byte unicode characters
This commit is contained in:
Bananymous 2023-02-22 21:45:26 +02:00
parent d9c05b7378
commit a845f8696c
1 changed files with 28 additions and 0 deletions

28
BAN/include/BAN/UTF8.h Normal file
View File

@ -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;
}
}