Created
February 17, 2020 19:45
-
-
Save WebFreak001/65976f5d8916efc4bc8f28f93bfb827c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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