MCPcopy Create free account
hub / github.com/defold/defold / codepoint_to_utf8

Function codepoint_to_utf8

engine/script/src/luacjson/lua_cjson.c:1056–1089  ·  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

1054/* Converts a Unicode codepoint to UTF-8.
1055 * Returns UTF-8 string length, and up to 4 bytes in *utf8 */
1056static int codepoint_to_utf8(char *utf8, int codepoint)
1057{
1058 /* 0xxxxxxx */
1059 if (codepoint <= 0x7F) {
1060 utf8[0] = codepoint;
1061 return 1;
1062 }
1063
1064 /* 110xxxxx 10xxxxxx */
1065 if (codepoint <= 0x7FF) {
1066 utf8[0] = (codepoint >> 6) | 0xC0;
1067 utf8[1] = (codepoint & 0x3F) | 0x80;
1068 return 2;
1069 }
1070
1071 /* 1110xxxx 10xxxxxx 10xxxxxx */
1072 if (codepoint <= 0xFFFF) {
1073 utf8[0] = (codepoint >> 12) | 0xE0;
1074 utf8[1] = ((codepoint >> 6) & 0x3F) | 0x80;
1075 utf8[2] = (codepoint & 0x3F) | 0x80;
1076 return 3;
1077 }
1078
1079 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
1080 if (codepoint <= 0x1FFFFF) {
1081 utf8[0] = (codepoint >> 18) | 0xF0;
1082 utf8[1] = ((codepoint >> 12) & 0x3F) | 0x80;
1083 utf8[2] = ((codepoint >> 6) & 0x3F) | 0x80;
1084 utf8[3] = (codepoint & 0x3F) | 0x80;
1085 return 4;
1086 }
1087
1088 return 0;
1089}
1090
1091
1092/* Called when index pointing to beginning of UTF-16 code escape: \uXXXX

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected