💾 Archived View for sdf.org › blippy › utf8.gmi captured on 2024-03-21 at 15:50:07. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
string utf8_unicode(int32_t uni) { //uint8_t bytes[5] = { 0b1110'0000, 0b1000'0000, 0b1, 0, 0}; //uint8_t bytes[5] = { 0, 0, 0, 0, 0}; //assert(uni >= 0x800 && uni < 0x10000); constexpr auto k6 = 0b11'1111; constexpr auto ext = 1<<7; if(uni < 0x80) { string str{"N"}; str[0] = uni; return str; } if(uni < 0x0800) { string str{"NN"}; str[0] = 0b1100'0000 + (uni>> 6); str[1] = ext + (uni & k6); return str; } if(uni < 0x10000) { string str{"NNN"}; str[0] = 0b1110'0000 + (uni>>12); str[1] = ext + ((uni>>6) & k6); str[2] = ext + (uni & k6); //return string(bytes[0] + bytes[1] + bytes[2]); return str; } //puts("deep into the woods"); assert(uni<= 0x10FFFF); string str{"NNNN"}; str[0] = 0b1111'0000 + (uni>>18); str[1] = ext + ((uni>>12) & k6); str[2] = ext + ((uni>>6) & k6); str[3] = ext + (uni & k6); return str; }