mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
arrays: add generic copy fn (#13677)
This commit is contained in:
parent
beb1b8ce1b
commit
17fcc788f2
@ -530,3 +530,15 @@ fn swap_nonoverlapping<T>(x_ &T, y_ &T, count int) {
|
||||
memswap(x, y, len)
|
||||
}
|
||||
}
|
||||
|
||||
// copy copies the `src` array elements to the `dst` array.
|
||||
// The number of the elements copied is the minimum of the length of both arrays.
|
||||
// Returns the number of elements copied.
|
||||
pub fn copy<T>(dst []T, src []T) int {
|
||||
min := if dst.len < src.len { dst.len } else { src.len }
|
||||
if min > 0 {
|
||||
blen := min * int(sizeof(T))
|
||||
unsafe { vmemmove(&T(dst.data), src.data, blen) }
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
@ -264,3 +264,20 @@ fn test_rotate_left_string() {
|
||||
rotate_left(mut x, 2)
|
||||
assert x == ['x3', 'x4', 'x5', 'x6', 'x1', 'x2']
|
||||
}
|
||||
|
||||
fn test_copy() {
|
||||
mut a := [1, 2, 3]
|
||||
mut b := [4, 5, 6]
|
||||
assert copy(b, a) == 3
|
||||
assert b == [1, 2, 3]
|
||||
// check independent copies
|
||||
b[0] = 99
|
||||
assert a[0] == 1
|
||||
// check longer src
|
||||
b << 7
|
||||
assert copy(a, b) == 3
|
||||
assert a == [99, 2, 3]
|
||||
// check longer dst
|
||||
assert copy(b, [8, 9]) == 2
|
||||
assert b == [8, 9, 3, 7]
|
||||
}
|
||||
|
@ -815,11 +815,11 @@ pub fn (b []byte) hex() string {
|
||||
// The number of the elements copied is the minimum of the length of both arrays.
|
||||
// Returns the number of elements copied.
|
||||
// NOTE: This is not an `array` method. It is a function that takes two arrays of bytes.
|
||||
// TODO: implement for all types
|
||||
// See also: `arrays.copy`.
|
||||
pub fn copy(dst []byte, src []byte) int {
|
||||
min := if dst.len < src.len { dst.len } else { src.len }
|
||||
if min > 0 {
|
||||
unsafe { vmemcpy(&byte(dst.data), src.data, min) }
|
||||
unsafe { vmemmove(&byte(dst.data), src.data, min) }
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user