-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Windows: Use WriteFile to write to a UTF-8 console #134622
Conversation
rustbot has assigned @Mark-Simulacrum. Use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really have enough context to solidly say "yes" but the code changes/comments seem reasonable. r=me unless you feel we need someone more familiar with Windows etc.
/// Returns true if the attached console's code page is currently UTF-8. | ||
#[cfg(not(target_vendor = "win7"))] | ||
fn is_utf8_console() -> bool { | ||
unsafe { c::GetConsoleOutputCP() == c::CP_UTF8 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this cheap enough to call on every write? I guess we're already calling GetConsoleMode on every write...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I don't love it but it is cheap. Especially when compared to the previous code where if is_console
is true then it will be doing a full UTF-8 to UTF-16 conversion using a small buffer (necessitating multiple calls to write
).
fn write(handle_id: u32, data: &[u8], incomplete_utf8: &mut IncompleteUtf8) -> io::Result<usize> { | ||
if data.is_empty() { | ||
return Ok(0); | ||
} | ||
|
||
let handle = get_handle(handle_id)?; | ||
if !is_console(handle) { | ||
if !is_console(handle) || is_utf8_console() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be worried about race conditions at all with changes in the mode setting? My sense is probably no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not. It is possible that the code page could be changed after is_utf8_console
and before the write. But changing the console code page is like changing any global state (e.g. an environment variable or the current directory). That is, it's the sort of thing you maybe set once on startup and don't touch again (unless you're very careful).
@bors r=Mark-Simulacrum |
Rollup of 8 pull requests Successful merges: - rust-lang#134606 (ptr::copy: fix docs for the overlapping case) - rust-lang#134622 (Windows: Use WriteFile to write to a UTF-8 console) - rust-lang#134759 (compiletest: Remove the `-test` suffix from normalize directives) - rust-lang#134787 (Spruce up the docs of several queries related to the type/trait system and const eval) - rust-lang#134806 (rustdoc: use shorter paths as preferred canonical paths) - rust-lang#134815 (Sort triples by name in platform_support.md) - rust-lang#134816 (tools: fix build failure caused by PR rust-lang#134420) - rust-lang#134819 (Fix mistake in windows file open) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#134622 - ChrisDenton:write-file-utf8, r=Mark-Simulacrum Windows: Use WriteFile to write to a UTF-8 console If the console code page is UTF-8 then we can simply write to it without needing to convert to UTF-16 and calling `WriteConsole`.
…k-Simulacrum Windows: Use WriteFile to write to a UTF-8 console If the console code page is UTF-8 then we can simply write to it without needing to convert to UTF-16 and calling `WriteConsole`.
Rollup of 8 pull requests Successful merges: - rust-lang#134606 (ptr::copy: fix docs for the overlapping case) - rust-lang#134622 (Windows: Use WriteFile to write to a UTF-8 console) - rust-lang#134759 (compiletest: Remove the `-test` suffix from normalize directives) - rust-lang#134787 (Spruce up the docs of several queries related to the type/trait system and const eval) - rust-lang#134806 (rustdoc: use shorter paths as preferred canonical paths) - rust-lang#134815 (Sort triples by name in platform_support.md) - rust-lang#134816 (tools: fix build failure caused by PR rust-lang#134420) - rust-lang#134819 (Fix mistake in windows file open) r? `@ghost` `@rustbot` modify labels: rollup
If the console code page is UTF-8 then we can simply write to it without needing to convert to UTF-16 and calling
WriteConsole
.