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

vlib: add a new module builtin.wchar, to ease dealing with C APIs that accept wchar_t* (#18794)

This commit is contained in:
Delyan Angelov
2023-07-07 02:40:11 +03:00
committed by GitHub
parent de392003be
commit ded6c38061
5 changed files with 167 additions and 4 deletions

View File

@@ -0,0 +1,36 @@
import builtin.wchar
const wide_serial_number_unix = [u16(67), 0, 76, 0, 52, 0, 54, 0, 73, 0, 49, 0, 65, 0, 48, 0, 48,
0, 54, 0, 52, 0, 57, 0, 0, 0, 0]
const wide_serial_number_windows = wide_serial_number_unix.map(u8(it))
const swide_serial_number = 'CL46I1A00649'
fn test_from_to_rune() {
for r in swide_serial_number.runes() {
c := wchar.from_rune(r)
assert c.to_rune() == r
}
assert wchar.from_rune(0).to_rune() == 0
}
fn test_to_string() {
mut p := voidptr(wide_serial_number_unix.data)
$if windows {
p = wide_serial_number_windows.data
}
assert unsafe { wchar.length_in_characters(p) } == swide_serial_number.len
s := unsafe { wchar.to_string(p) }
dump(s)
assert s == swide_serial_number
}
fn test_from_string() {
x := wchar.from_string(swide_serial_number)
assert unsafe { x[0] } == wchar.from_rune(`C`)
assert unsafe { x[1] } == wchar.from_rune(`L`)
assert unsafe { x[2] } == wchar.from_rune(`4`)
assert unsafe { x[11] } == wchar.from_rune(`9`)
assert unsafe { x[12] } == wchar.zero
}