1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 03:22:48 -05:00

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.
This commit is contained in:
Simon Tatham
2022-11-09 18:56:51 +00:00
parent 991e22c9bb
commit 834b58e39b
7 changed files with 27 additions and 45 deletions

View File

@ -5,25 +5,22 @@
#include "defs.h"
#include "misc.h"
size_t encode_utf8(void *output, unsigned long ch)
void BinarySink_put_utf8_char(BinarySink *output, unsigned ch)
{
unsigned char *start = (unsigned char *)output, *p = start;
if (ch < 0x80) {
*p++ = ch;
put_byte(output, ch);
} else if (ch < 0x800) {
*p++ = 0xC0 | (ch >> 6);
*p++ = 0x80 | (ch & 0x3F);
put_byte(output, 0xC0 | (ch >> 6));
put_byte(output, 0x80 | (ch & 0x3F));
} else if (ch < 0x10000) {
*p++ = 0xE0 | (ch >> 12);
*p++ = 0x80 | ((ch >> 6) & 0x3F);
*p++ = 0x80 | (ch & 0x3F);
put_byte(output, 0xE0 | (ch >> 12));
put_byte(output, 0x80 | ((ch >> 6) & 0x3F));
put_byte(output, 0x80 | (ch & 0x3F));
} else {
assert(ch <= 0x10FFFF);
*p++ = 0xF0 | (ch >> 18);
*p++ = 0x80 | ((ch >> 12) & 0x3F);
*p++ = 0x80 | ((ch >> 6) & 0x3F);
*p++ = 0x80 | (ch & 0x3F);
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));
}
return p - start;
}

View File

@ -17,9 +17,7 @@ char *encode_wide_string_as_utf8(const wchar_t *ws)
} else if (IS_SURROGATE(ch)) {
ch = 0xfffd; /* illegal UTF-16 -> REPLACEMENT CHARACTER */
}
char utf8[6];
size_t size = encode_utf8(utf8, ch);
put_data(sb, utf8, size);
put_utf8_char(sb, ch);
}
return strbuf_to_str(sb);
}

View File

@ -217,9 +217,6 @@ static inline void stripctrl_term_put_wc(
if (prefix.len)
put_datapl(scc->bs_out, prefix);
char outbuf[6];
size_t produced;
/*
* The Terminal implementation encodes 7-bit ASCII characters in
* UTF-8 mode, and all printing characters in non-UTF-8 (i.e.
@ -232,14 +229,10 @@ static inline void stripctrl_term_put_wc(
wc &= 0xFF;
if (in_utf(scc->term)) {
produced = encode_utf8(outbuf, wc);
put_utf8_char(scc->bs_out, wc);
} else {
outbuf[0] = wc;
produced = 1;
put_byte(scc->bs_out, wc);
}
if (produced > 0)
put_data(scc->bs_out, outbuf, produced);
}
static inline size_t stripctrl_locale_try_consume(