MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / codepoint_to_utf8

Function codepoint_to_utf8

deps/lua/src/lua_cjson.c:775–808  ·  view source on GitHub ↗

Converts a Unicode codepoint to UTF-8. * Returns UTF-8 string length, and up to 4 bytes in *utf8 */

Source from the content-addressed store, hash-verified

773/* Converts a Unicode codepoint to UTF-8.
774 * Returns UTF-8 string length, and up to 4 bytes in *utf8 */
775static int codepoint_to_utf8(char *utf8, int codepoint)
776{
777 /* 0xxxxxxx */
778 if (codepoint <= 0x7F) {
779 utf8[0] = codepoint;
780 return 1;
781 }
782
783 /* 110xxxxx 10xxxxxx */
784 if (codepoint <= 0x7FF) {
785 utf8[0] = (codepoint >> 6) | 0xC0;
786 utf8[1] = (codepoint & 0x3F) | 0x80;
787 return 2;
788 }
789
790 /* 1110xxxx 10xxxxxx 10xxxxxx */
791 if (codepoint <= 0xFFFF) {
792 utf8[0] = (codepoint >> 12) | 0xE0;
793 utf8[1] = ((codepoint >> 6) & 0x3F) | 0x80;
794 utf8[2] = (codepoint & 0x3F) | 0x80;
795 return 3;
796 }
797
798 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
799 if (codepoint <= 0x1FFFFF) {
800 utf8[0] = (codepoint >> 18) | 0xF0;
801 utf8[1] = ((codepoint >> 12) & 0x3F) | 0x80;
802 utf8[2] = ((codepoint >> 6) & 0x3F) | 0x80;
803 utf8[3] = (codepoint & 0x3F) | 0x80;
804 return 4;
805 }
806
807 return 0;
808}
809
810
811/* Called when index pointing to beginning of UTF-16 code escape: \uXXXX

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected