mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 17:38:00 +00:00
834b58e39b
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.
24 lines
571 B
C
24 lines
571 B
C
/*
|
|
* Encode a string of wchar_t as UTF-8.
|
|
*/
|
|
|
|
#include "putty.h"
|
|
#include "misc.h"
|
|
|
|
char *encode_wide_string_as_utf8(const wchar_t *ws)
|
|
{
|
|
strbuf *sb = strbuf_new();
|
|
while (*ws) {
|
|
unsigned long ch = *ws++;
|
|
if (sizeof(wchar_t) == 2 && IS_HIGH_SURROGATE(ch) &&
|
|
IS_LOW_SURROGATE(*ws)) {
|
|
ch = FROM_SURROGATES(ch, *ws);
|
|
ws++;
|
|
} else if (IS_SURROGATE(ch)) {
|
|
ch = 0xfffd; /* illegal UTF-16 -> REPLACEMENT CHARACTER */
|
|
}
|
|
put_utf8_char(sb, ch);
|
|
}
|
|
return strbuf_to_str(sb);
|
|
}
|