Skip to content

Instantly share code, notes, and snippets.

@WebFreak001
Created February 17, 2020 19:45
Show Gist options
  • Save WebFreak001/65976f5d8916efc4bc8f28f93bfb827c to your computer and use it in GitHub Desktop.
Save WebFreak001/65976f5d8916efc4bc8f28f93bfb827c to your computer and use it in GitHub Desktop.
size_t bytesToUTF16Count(size_t bytes)
{
if (bytes > text.length)
bytes = text.length;
size_t offset;
size_t index;
while (index < bytes)
{
switch (text.ptr[index] & 0b1111_0000)
{
case 0b1110_0000:
// assume valid encoding (no wrong surrogates)
offset++;
index += 3;
break;
case 0b1111_0000:
offset += 2;
index += 4;
break;
case 0b1100_0000:
case 0b1101_0000:
offset++;
index += 2;
break;
default:
offset++;
index++;
break;
}
}
return offset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment