Skip to content

Commit 8094150

Browse files
authored
builtin,os: fix windows execute ANSI encoding result, make msvc error messages readable in a Chinese locale (fix #25727) (#25736)
1 parent f05ba1d commit 8094150

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

vlib/builtin/utf8.c.v

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import strings
55
const cp_acp = 0
66
const cp_utf8 = 65001
77

8+
@[params]
9+
pub struct ToWideConfig {
10+
from_ansi bool
11+
}
12+
813
// to_wide returns a pointer to an UTF-16 version of the string receiver.
914
// In V, strings are encoded using UTF-8 internally, but on windows most APIs,
1015
// that accept strings, need them to be in UTF-16 encoding.
@@ -13,14 +18,16 @@ const cp_utf8 = 65001
1318
// See also MultiByteToWideChar ( https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar )
1419
// See also builtin.wchar.from_string/1, for a version, that produces a
1520
// platform dependant L"" C style wchar_t* wide string.
16-
pub fn (_str string) to_wide() &u16 {
21+
pub fn (_str string) to_wide(param ToWideConfig) &u16 {
1722
$if windows {
1823
unsafe {
19-
num_chars := (C.MultiByteToWideChar(cp_utf8, 0, &char(_str.str), _str.len,
24+
src_encoding := if param.from_ansi { cp_acp } else { cp_utf8 }
25+
num_chars := (C.MultiByteToWideChar(src_encoding, 0, &char(_str.str), _str.len,
2026
0, 0))
2127
mut wstr := &u16(malloc_noscan((num_chars + 1) * 2)) // sizeof(wchar_t)
2228
if wstr != 0 {
23-
C.MultiByteToWideChar(cp_utf8, 0, &char(_str.str), _str.len, wstr, num_chars)
29+
C.MultiByteToWideChar(src_encoding, 0, &char(_str.str), _str.len, wstr,
30+
num_chars)
2431
C.memset(&u8(wstr) + num_chars * 2, 0, 2)
2532
}
2633
return wstr

vlib/os/os_windows.c.v

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module os
22

33
import strings
4+
import encoding.utf8.validate
45

56
#flag windows -l advapi32
67
#include <process.h>
@@ -387,7 +388,13 @@ pub fn raw_execute(cmd string) Result {
387388
break
388389
}
389390
}
390-
soutput := read_data.str()
391+
// encoding: from ANSI to UTF-8
392+
soutput_str := read_data.str()
393+
soutput := if validate.utf8_string(soutput_str) {
394+
soutput_str
395+
} else {
396+
string_from_wide(soutput_str.to_wide(from_ansi: true))
397+
}
391398
unsafe { read_data.free() }
392399
exit_code := u32(0)
393400
C.WaitForSingleObject(proc_info.h_process, C.INFINITE)

vlib/v/compiler_errors_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ fn (mut tasks Tasks) run() {
249249
m_skip_files << 'vlib/v/checker/tests/comptime_value_d_in_include_errors.vv'
250250
}
251251
$if msvc {
252-
m_skip_files << 'vlib/v/checker/tests/invalid_utf8_string.vv'
253252
m_skip_files << 'vlib/v/checker/tests/asm_alias_does_not_exist.vv'
254253
m_skip_files << 'vlib/v/checker/tests/asm_immutable_err.vv'
255254
// TODO: investigate why MSVC regressed
@@ -258,6 +257,7 @@ fn (mut tasks Tasks) run() {
258257
m_skip_files << 'vlib/v/checker/tests/comptime_value_d_in_include_errors.vv'
259258
}
260259
$if windows {
260+
m_skip_files << 'vlib/v/checker/tests/invalid_utf8_string.vv'
261261
m_skip_files << 'vlib/v/checker/tests/modules/deprecated_module'
262262
}
263263
for i in 0 .. tasks.all.len {

0 commit comments

Comments
 (0)