mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-03 04:22:47 -05:00
Add a manual single-char UTF-8 decoder.
This parallels encode_utf8 which we already had. Decoding is more fraught with perils than encoding, so I've also included a small test program.
This commit is contained in:
20
utils/decode_utf8_to_wchar.c
Normal file
20
utils/decode_utf8_to_wchar.c
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Decode a single UTF-8 character to the platform's local wchar_t.
|
||||
*/
|
||||
|
||||
#include "putty.h"
|
||||
#include "misc.h"
|
||||
|
||||
size_t decode_utf8_to_wchar(const char **utf8, wchar_t *out)
|
||||
{
|
||||
size_t outlen = 0;
|
||||
unsigned wc = decode_utf8(utf8);
|
||||
if (sizeof(wchar_t) > 2 || wc < 0x10000) {
|
||||
out[outlen++] = wc;
|
||||
} else {
|
||||
unsigned wcoff = wc - 0x10000;
|
||||
out[outlen++] = 0xD800 | (0x3FF & (wcoff >> 10));
|
||||
out[outlen++] = 0xDC00 | (0x3FF & wcoff);
|
||||
}
|
||||
return outlen;
|
||||
}
|
Reference in New Issue
Block a user