2020-12-15 11:17:46 +03:00
|
|
|
module builtin
|
|
|
|
|
2022-11-28 10:46:04 +03:00
|
|
|
const cp_utf8 = 65001
|
2020-12-15 12:01:51 +03:00
|
|
|
|
2022-11-28 10:46:04 +03:00
|
|
|
// to_wide returns a pointer to an UTF-16 version of the string receiver.
|
|
|
|
// In V, strings are encoded using UTF-8 internally, but on windows most APIs,
|
|
|
|
// that accept strings, need them to be in UTF-16 encoding.
|
|
|
|
// The returned pointer of .to_wide(), has a type of &u16, and is suitable
|
|
|
|
// for passing to Windows APIs that expect LPWSTR or wchar_t* parameters.
|
|
|
|
// See also MultiByteToWideChar ( https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar )
|
2020-12-15 11:17:46 +03:00
|
|
|
pub fn (_str string) to_wide() &u16 {
|
|
|
|
$if windows {
|
2021-02-14 21:31:42 +03:00
|
|
|
unsafe {
|
2021-04-04 17:43:32 +03:00
|
|
|
num_chars := (C.MultiByteToWideChar(cp_utf8, 0, &char(_str.str), _str.len,
|
2021-03-24 21:39:59 +03:00
|
|
|
0, 0))
|
2021-06-15 14:47:11 +03:00
|
|
|
mut wstr := &u16(malloc_noscan((num_chars + 1) * 2)) // sizeof(wchar_t)
|
2021-02-14 21:31:42 +03:00
|
|
|
if wstr != 0 {
|
2021-04-04 17:43:32 +03:00
|
|
|
C.MultiByteToWideChar(cp_utf8, 0, &char(_str.str), _str.len, wstr, num_chars)
|
2022-04-15 14:58:56 +03:00
|
|
|
C.memset(&u8(wstr) + num_chars * 2, 0, 2)
|
2021-02-14 21:31:42 +03:00
|
|
|
}
|
|
|
|
return wstr
|
2020-12-15 11:17:46 +03:00
|
|
|
}
|
|
|
|
} $else {
|
2022-01-28 21:41:50 +03:00
|
|
|
srunes := _str.runes()
|
|
|
|
unsafe {
|
|
|
|
mut result := &u16(vcalloc_noscan((srunes.len + 1) * 2))
|
|
|
|
for i, r in srunes {
|
|
|
|
result[i] = u16(r)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2020-12-15 11:17:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 10:46:04 +03:00
|
|
|
// string_from_wide creates a V string, encoded in UTF-8, given a windows
|
|
|
|
// style string encoded in UTF-16.
|
2021-02-15 18:15:52 +03:00
|
|
|
[unsafe]
|
2020-12-15 11:17:46 +03:00
|
|
|
pub fn string_from_wide(_wstr &u16) string {
|
|
|
|
$if windows {
|
2021-02-14 21:31:42 +03:00
|
|
|
unsafe {
|
|
|
|
wstr_len := C.wcslen(_wstr)
|
|
|
|
return string_from_wide2(_wstr, wstr_len)
|
|
|
|
}
|
2020-12-15 11:17:46 +03:00
|
|
|
} $else {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 10:46:04 +03:00
|
|
|
// string_from_wide2 creates a V string, encoded in UTF-8, given a windows
|
|
|
|
// style string, encoded in UTF-16. It is more efficient, compared to
|
|
|
|
// string_from_wide, but it requires you to know the input string length,
|
|
|
|
// and to pass it as the second argument.
|
2021-02-15 18:15:52 +03:00
|
|
|
[unsafe]
|
2020-12-15 11:17:46 +03:00
|
|
|
pub fn string_from_wide2(_wstr &u16, len int) string {
|
|
|
|
$if windows {
|
2021-02-14 21:31:42 +03:00
|
|
|
unsafe {
|
|
|
|
num_chars := C.WideCharToMultiByte(cp_utf8, 0, _wstr, len, 0, 0, 0, 0)
|
2021-06-15 14:47:11 +03:00
|
|
|
mut str_to := malloc_noscan(num_chars + 1)
|
2021-02-14 21:31:42 +03:00
|
|
|
if str_to != 0 {
|
2021-04-04 17:43:32 +03:00
|
|
|
C.WideCharToMultiByte(cp_utf8, 0, _wstr, len, &char(str_to), num_chars,
|
2021-02-14 21:31:42 +03:00
|
|
|
0, 0)
|
|
|
|
C.memset(str_to + num_chars, 0, 1)
|
|
|
|
}
|
|
|
|
return tos2(str_to)
|
2020-12-15 11:17:46 +03:00
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|