1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

Windows Unicode I/O

This commit is contained in:
vitalyster
2019-07-24 13:16:45 +03:00
committed by Alexander Medvednikov
parent c28a490c17
commit fcb1f211e3
7 changed files with 198 additions and 64 deletions

View File

@ -91,3 +91,43 @@ pub fn (_rune string) utf32_code() int {
return res
}
const (
CP_UTF8 = 65001
)
pub fn (_str string) to_wide() &u16 {
$if windows {
num_chars := int(C.MultiByteToWideChar(CP_UTF8, 0, _str.str, _str.len, 0, 0))
mut wstr := &u16(malloc((num_chars + 1) * 2)) // sizeof(wchar_t)
if wstr > 0 {
C.MultiByteToWideChar(CP_UTF8, 0, _str.str, _str.len, wstr, num_chars)
C.memset(&byte(wstr) + num_chars * 2, 0, 2)
}
return wstr
} $else {
return 0
}
}
pub fn string_from_wide(_wstr &u16) string {
$if windows {
wstr_len := int(C.wcslen(_wstr))
return string_from_wide2(_wstr, wstr_len)
} $else {
return ''
}
}
pub fn string_from_wide2(_wstr &u16, len int) string {
$if windows {
num_chars := int(C.WideCharToMultiByte(CP_UTF8, 0, _wstr, len, 0, 0, 0, 0))
mut str_to := &byte(malloc(num_chars + 1))
if str_to > 0 {
C.WideCharToMultiByte(CP_UTF8, 0, _wstr, len, str_to, num_chars, 0, 0)
C.memset(&byte(str_to) + num_chars, 0, 1)
}
return tos2(str_to)
} $else {
return ''
}
}