Skip to content

Commit 44dddec

Browse files
authored
builtin: use a pure V version of string.to_wide() on != windows (#13314)
1 parent e43794a commit 44dddec

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

vlib/builtin/utf8.c.v

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ pub fn (_str string) to_wide() &u16 {
1717
return wstr
1818
}
1919
} $else {
20-
return 0
20+
srunes := _str.runes()
21+
unsafe {
22+
mut result := &u16(vcalloc_noscan((srunes.len + 1) * 2))
23+
for i, r in srunes {
24+
result[i] = u16(r)
25+
}
26+
return result
27+
}
2128
}
2229
}
2330

vlib/builtin/utf8_test.v

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,31 @@ fn test_utf8_wide_char() {
2626
assert val[2].hex() == '94'
2727
}
2828
}
29+
30+
fn test_to_wide_latin() {
31+
s := 'abc 123'
32+
w := s.to_wide()
33+
unsafe {
34+
assert w[0] == 97
35+
assert w[1] == 98
36+
assert w[2] == 99
37+
assert w[3] == 32
38+
assert w[4] == 49
39+
assert w[5] == 50
40+
assert w[6] == 51
41+
assert w[7] == 0
42+
}
43+
}
44+
45+
fn test_to_wide_cyrillic() {
46+
s := 'Проба'
47+
w := s.to_wide()
48+
unsafe {
49+
assert w[0] == 1055
50+
assert w[1] == 1088
51+
assert w[2] == 1086
52+
assert w[3] == 1073
53+
assert w[4] == 1072
54+
assert w[5] == 0
55+
}
56+
}

0 commit comments

Comments
 (0)