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

builtin: add 0 terminators for strings returned by .to_lower, .to_upper, utf32_to_str_no_malloc/2

This commit is contained in:
Delyan Angelov
2021-03-14 18:21:45 +02:00
parent b6d089b605
commit d65ad68e77
4 changed files with 17 additions and 14 deletions

View File

@ -35,8 +35,8 @@ pub fn (c byte) is_capital() bool {
pub fn (b []byte) clone() []byte {
mut res := []byte{len: b.len}
//mut res := make([]byte, {repeat:b.len})
for i in 0..b.len {
// mut res := make([]byte, {repeat:b.len})
for i in 0 .. b.len {
res[i] = b[i]
}
return res
@ -49,8 +49,10 @@ pub fn (b []byte) bytestr() string {
// TODO copy pasted from builder.v
fn bytes2string(b []byte) string {
mut copy := b.clone()
copy << `\0`
res := unsafe { tos(copy.data, copy.len-1) }
return res
unsafe {
buf := malloc(b.len + 1)
C.memcpy(buf, b.data, b.len)
buf[b.len] = 0
return tos(buf, b.len)
}
}