Converts a Unicode codepoint to UTF-8. * Returns UTF-8 string length, and up to 4 bytes in *utf8 */
| 801 | /* Converts a Unicode codepoint to UTF-8. |
| 802 | * Returns UTF-8 string length, and up to 4 bytes in *utf8 */ |
| 803 | static int codepoint_to_utf8(char *utf8, int codepoint) |
| 804 | { |
| 805 | /* 0xxxxxxx */ |
| 806 | if (codepoint <= 0x7F) { |
| 807 | utf8[0] = codepoint; |
| 808 | return 1; |
| 809 | } |
| 810 | |
| 811 | /* 110xxxxx 10xxxxxx */ |
| 812 | if (codepoint <= 0x7FF) { |
| 813 | utf8[0] = (codepoint >> 6) | 0xC0; |
| 814 | utf8[1] = (codepoint & 0x3F) | 0x80; |
| 815 | return 2; |
| 816 | } |
| 817 | |
| 818 | /* 1110xxxx 10xxxxxx 10xxxxxx */ |
| 819 | if (codepoint <= 0xFFFF) { |
| 820 | utf8[0] = (codepoint >> 12) | 0xE0; |
| 821 | utf8[1] = ((codepoint >> 6) & 0x3F) | 0x80; |
| 822 | utf8[2] = (codepoint & 0x3F) | 0x80; |
| 823 | return 3; |
| 824 | } |
| 825 | |
| 826 | /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ |
| 827 | if (codepoint <= 0x1FFFFF) { |
| 828 | utf8[0] = (codepoint >> 18) | 0xF0; |
| 829 | utf8[1] = ((codepoint >> 12) & 0x3F) | 0x80; |
| 830 | utf8[2] = ((codepoint >> 6) & 0x3F) | 0x80; |
| 831 | utf8[3] = (codepoint & 0x3F) | 0x80; |
| 832 | return 4; |
| 833 | } |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | |
| 839 | /* Called when index pointing to beginning of UTF-16 code escape: \uXXXX |
no outgoing calls
no test coverage detected