mirror of
https://github.com/vlang/v.git
synced 2025-09-16 16:02:29 +03:00
builtin: add an unsafe { a.reset() }
method, for quickly setting all bytes in an array to 0
This commit is contained in:
parent
413da8be62
commit
ec9ac7715a
2 changed files with 30 additions and 0 deletions
|
@ -389,6 +389,16 @@ pub fn (mut a array) clear() {
|
|||
a.len = 0
|
||||
}
|
||||
|
||||
// reset quickly sets the bytes of all elements of the array to 0.
|
||||
// Useful mainly for numeric arrays. Note, that calling reset()
|
||||
// is not safe, when your array contains more complex elements,
|
||||
// like structs, maps, pointers etc, since setting them to 0,
|
||||
// can later lead to hard to find bugs.
|
||||
[unsafe]
|
||||
pub fn (mut a array) reset() {
|
||||
unsafe { vmemset(a.data, 0, a.len * a.element_size) }
|
||||
}
|
||||
|
||||
// trim trims the array length to `index` without modifying the allocated data.
|
||||
// If `index` is greater than `len` nothing will be changed.
|
||||
// Example: a.trim(3) // `a.len` is now <= 3
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue