1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 01:18:00 +00:00
putty-source/utils/encode_utf8.c
Simon Tatham 834b58e39b Make encode_utf8() output to a BinarySink.
Previously it output to an ordinary char buffer, and returned the
number of bytes it had written. But three out of the four call sites
immediately chucked the resulting bytes into a BinarySink anyway. The
fourth, in windows/unicode.c, really is writing into successive
locations of a fixed-size buffer - but we can make that into a
BinarySink too, using the buffer_sink added in the previous commit.

So now encode_utf8() is renamed put_utf8_char, and the call sites all
look simpler than they started out.
2022-11-09 19:02:32 +00:00

27 lines
746 B
C

/*
* Encode a single code point as UTF-8.
*/
#include "defs.h"
#include "misc.h"
void BinarySink_put_utf8_char(BinarySink *output, unsigned ch)
{
if (ch < 0x80) {
put_byte(output, ch);
} else if (ch < 0x800) {
put_byte(output, 0xC0 | (ch >> 6));
put_byte(output, 0x80 | (ch & 0x3F));
} else if (ch < 0x10000) {
put_byte(output, 0xE0 | (ch >> 12));
put_byte(output, 0x80 | ((ch >> 6) & 0x3F));
put_byte(output, 0x80 | (ch & 0x3F));
} else {
assert(ch <= 0x10FFFF);
put_byte(output, 0xF0 | (ch >> 18));
put_byte(output, 0x80 | ((ch >> 12) & 0x3F));
put_byte(output, 0x80 | ((ch >> 6) & 0x3F));
put_byte(output, 0x80 | (ch & 0x3F));
}
}