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

builtin: unsafe vlib pointer indexing (#5836)

This commit is contained in:
Nick Treleaven
2020-07-15 20:56:50 +01:00
committed by GitHub
parent cc7c8009a1
commit f5e6a83a05
7 changed files with 326 additions and 189 deletions

View File

@ -5,10 +5,11 @@ pub fn repeat(c byte, n int) string {
if n <= 0 {
return ''
}
mut bytes := &byte(0)
unsafe { bytes = malloc(n + 1) }
C.memset( bytes, c, n )
bytes[n] = `0`
mut bytes := unsafe {malloc(n + 1)}
unsafe {
C.memset( bytes, c, n )
bytes[n] = `0`
}
return string( bytes, n )
}
@ -21,14 +22,17 @@ pub fn repeat_string(s string, n int) string {
}
slen := s.len
blen := slen*n
mut bytes := &byte(0)
unsafe { bytes = malloc(blen + 1) }
mut bytes := unsafe {malloc(blen + 1)}
for bi in 0..n {
bislen := bi*slen
for si in 0..slen {
bytes[bislen+si] = s[si]
unsafe {
bytes[bislen+si] = s[si]
}
}
}
bytes[blen] = `0`
unsafe {
bytes[blen] = `0`
}
return string( bytes, blen )
}