Skip to content

Cannot read unicode data from STDIN on Windows #5148

Description

@Tetralux

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    os-windowsMicrosoft Windowsstandard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions