Tracking issue for how, on Windows, just using ReadFile will completely corrupt UTF-16 data.
Instead, you have to use ReadConsoleW.
For example:
extern"kernel32"fnReadConsoleW(handle: os.fd_t, buffer: [*]u16, len: os.windows.DWORD, read: *os.windows.DWORD, input_ctrl: ?*c_void) i32;
extern"kernel32"fnSetConsoleOutputCP(cp: os.windows.UINT) i32;
// ...assert(SetConsoleOutputCP(65001) !=0); // display outputted utf8 correctly in the consolevarstdin=io.getStdIn().handle;
vardata: [256]u16=undefined;
varread: u32=undefined;
constok=ReadConsoleW(stdin, &data, data.len, &read, null); // type in '안녕'assert(ok!=0);
varutf8: [1024]u8=undefined;
constutf8_len=tryunicode.utf16leToUtf8(&utf8, data[0..read]);
consts=utf8[0..utf8_len];
print("'{}'\n", .{s}); // prints out '안녕// 'As noted by @fengb, this may mean Zig needs to have a special InStream for the console.
Tracking issue for how, on Windows, just using
ReadFilewill completely corrupt UTF-16 data.Instead, you have to use
ReadConsoleW.For example:
As noted by @fengb, this may mean Zig needs to have a special
InStreamfor the console.