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

@ -1290,8 +1290,8 @@ int wc_to_mb(int codepage, int flags, const wchar_t *wcstr, int wclen,
* the codepage is UTF-8, we can do the translation ourselves.
*/
if (codepage == CP_UTF8 && mblen > 0 && wclen > 0) {
size_t remaining = mblen;
char *p = mbstr;
buffer_sink bs[1];
buffer_sink_init(bs, mbstr, mblen);
while (wclen > 0) {
unsigned long wc = (wclen--, *wcstr++);
@ -1300,18 +1300,13 @@ int wc_to_mb(int codepage, int flags, const wchar_t *wcstr, int wclen,
wclen--, wcstr++;
}
char utfbuf[6];
size_t utflen = encode_utf8(utfbuf, wc);
if (utflen <= remaining) {
memcpy(p, utfbuf, utflen);
p += utflen;
remaining -= utflen;
} else {
return p - mbstr;
}
const char *prev_ptr = bs->out;
put_utf8_char(bs, wc);
if (bs->overflowed)
return prev_ptr - mbstr;
}
return p - mbstr;
return bs->out - mbstr;
}
#endif