mirror of
https://github.com/vlang/v.git
synced 2025-09-15 07:22:27 +03:00
vlib/arrays: fix copy
to not use memcpy for array, map, string (#13703)
This commit is contained in:
parent
de2fc87995
commit
4bea35b028
2 changed files with 31 additions and 1 deletions
|
@ -546,9 +546,27 @@ fn swap_nonoverlapping<T>(x_ &T, y_ &T, count int) {
|
|||
// Returns the number of elements copied.
|
||||
pub fn copy<T>(mut dst []T, src []T) int {
|
||||
min := if dst.len < src.len { dst.len } else { src.len }
|
||||
if min > 0 {
|
||||
if min <= 0 {
|
||||
return 0
|
||||
}
|
||||
if can_copy_bits<T>() {
|
||||
blen := min * int(sizeof(T))
|
||||
unsafe { vmemmove(&T(dst.data), src.data, blen) }
|
||||
} else {
|
||||
for i in 0 .. min {
|
||||
dst[i] = src[i]
|
||||
}
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
||||
// determines if T can be copied using `memcpy`
|
||||
// false if autofree needs to intervene
|
||||
// false if type is not copyable e.g. map
|
||||
fn can_copy_bits<T>() bool {
|
||||
// references, C pointers, integers, floats, runes
|
||||
if T.name[0] in [`&`, `b`, `c`, `f`, `i`, `r`, `u`, `v`] {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue