@@ -5,6 +5,11 @@ import strings
55const cp_acp = 0
66const 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
0 commit comments