[utils] Get utf-8 writing working for up to 11 bits

I need to write some automated tests for this, and reading of course,
but 1 and two byte outputs look correct. Kind of sad it took sixteen
years to get around to attempting to use the code :(
This commit is contained in:
Bill Currie 2021-07-27 21:41:35 +09:00
parent b9d2882e02
commit 5b4428420e

View file

@ -215,27 +215,27 @@ MSG_WriteUTF8 (sizebuf_t *sb, unsigned utf8)
return; // invalid (FIXME die?) return; // invalid (FIXME die?)
} else if (utf8 & 0x7c000000) { } else if (utf8 & 0x7c000000) {
buf = SZ_GetSpace (sb, count = 6); buf = SZ_GetSpace (sb, count = 6);
*buf = 0xfc | ((utf8 & 0x40000000) >> 30); // 1 bit *buf++ = 0xfc | ((utf8 & 0x40000000) >> 30); // 1 bit
utf8 <<= 2; utf8 <<= 2;
} else if (utf8 & 0x03e00000) { } else if (utf8 & 0x03e00000) {
buf = SZ_GetSpace (sb, count = 5); buf = SZ_GetSpace (sb, count = 5);
*buf = 0xf8 | ((utf8 & 0x30000000) >> 28); // 2 bits *buf++ = 0xf8 | ((utf8 & 0x30000000) >> 28); // 2 bits
utf8 <<= 4; utf8 <<= 4;
} else if (utf8 & 0x001f0000) { } else if (utf8 & 0x001f0000) {
buf = SZ_GetSpace (sb, count = 4); buf = SZ_GetSpace (sb, count = 4);
*buf = 0xf0 | ((utf8 & 0x001c0000) >> 18); // 3 bits *buf++ = 0xf0 | ((utf8 & 0x001c0000) >> 18); // 3 bits
utf8 <<= 14; utf8 <<= 14;
} else if (utf8 & 0x0000f800) { } else if (utf8 & 0x0000f800) {
buf = SZ_GetSpace (sb, count = 3); buf = SZ_GetSpace (sb, count = 3);
*buf = 0xe0 | ((utf8 & 0x0000f000) >> 12); // 4 bits *buf++ = 0xe0 | ((utf8 & 0x0000f000) >> 12); // 4 bits
utf8 <<= 20; utf8 <<= 20;
} else if (utf8 & 0x00000780) { } else if (utf8 & 0x00000780) {
buf = SZ_GetSpace (sb, count = 2); buf = SZ_GetSpace (sb, count = 2);
*buf = 0xc0 | ((utf8 & 0x00000780) >> 7); // 5 bits *buf++ = 0xc0 | ((utf8 & 0x000007c0) >> 6); // 5 bits
utf8 <<= 25; utf8 <<= 26;
} else { } else {
buf = SZ_GetSpace (sb, count = 1); buf = SZ_GetSpace (sb, count = 1);
*buf = utf8; *buf++ = utf8;
return; return;
} }
while (--count) { while (--count) {