Skip to content

Instantly share code, notes, and snippets.

@vinniefalco
Created October 15, 2019 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinniefalco/d416eefae1bc2d317c1dc712d75db2eb to your computer and use it in GitHub Desktop.
Save vinniefalco/d416eefae1bc2d317c1dc712d75db2eb to your computer and use it in GitHub Desktop.
static
int
utf8_encode(
char* dest,
unsigned long cp)
{
if(cp < 0x80)
{
dest[0] = static_cast<char>(cp);
return 1;
}
if(cp < 0x800)
{
dest[0] = static_cast<char>((cp >> 6) | 0xc0);
dest[1] = static_cast<char>((cp & 0x3f) | 0x80);
return 2;
}
if(cp < 0x10000)
{
dest[0] = static_cast<char>((cp >> 12) | 0xe0);
dest[1] = static_cast<char>(((cp >> 6) & 0x3f) | 0x80);
dest[2] = static_cast<char>((cp & 0x3f) | 0x80);
return 3;
}
{
dest[0] = static_cast<char>((cp >> 18) | 0xf0);
dest[1] = static_cast<char>(((cp >> 12) & 0x3f) | 0x80);
dest[2] = static_cast<char>(((cp >> 6) & 0x3f) | 0x80);
dest[3] = static_cast<char>((cp & 0x3f) | 0x80);
return 4;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment