@@ -84,21 +84,43 @@ fn is_console(handle: c::HANDLE) -> bool {
84
84
unsafe { c:: GetConsoleMode ( handle, & mut mode) != 0 }
85
85
}
86
86
87
+ /// Returns true if the attached console's code page is currently UTF-8.
88
+ #[ cfg( not( target_vendor = "win7" ) ) ]
89
+ fn is_utf8_console ( ) -> bool {
90
+ unsafe { c:: GetConsoleOutputCP ( ) == c:: CP_UTF8 }
91
+ }
92
+
93
+ #[ cfg( target_vendor = "win7" ) ]
94
+ fn is_utf8_console ( ) -> bool {
95
+ // Windows 7 has a fun "feature" where WriteFile on a console handle will return
96
+ // the number of UTF-16 code units written and not the number of bytes from the input string.
97
+ // So we always claim the console isn't UTF-8 to trigger the WriteConsole fallback code.
98
+ false
99
+ }
100
+
87
101
fn write ( handle_id : u32 , data : & [ u8 ] , incomplete_utf8 : & mut IncompleteUtf8 ) -> io:: Result < usize > {
88
102
if data. is_empty ( ) {
89
103
return Ok ( 0 ) ;
90
104
}
91
105
92
106
let handle = get_handle ( handle_id) ?;
93
- if !is_console ( handle) {
107
+ if !is_console ( handle) || is_utf8_console ( ) {
94
108
unsafe {
95
109
let handle = Handle :: from_raw_handle ( handle) ;
96
110
let ret = handle. write ( data) ;
97
111
let _ = handle. into_raw_handle ( ) ; // Don't close the handle
98
112
return ret;
99
113
}
114
+ } else {
115
+ write_console_utf16 ( data, incomplete_utf8, handle)
100
116
}
117
+ }
101
118
119
+ fn write_console_utf16 (
120
+ data : & [ u8 ] ,
121
+ incomplete_utf8 : & mut IncompleteUtf8 ,
122
+ handle : c:: HANDLE ,
123
+ ) -> io:: Result < usize > {
102
124
if incomplete_utf8. len > 0 {
103
125
assert ! (
104
126
incomplete_utf8. len < 4 ,
0 commit comments