slice: add Clean microfunction

To be used for various cleaning purposes, one line is better than three lines.
This commit is contained in:
Roman Khimov 2022-09-01 22:05:34 +03:00
parent 58dc8d0c9b
commit 74bf4a8e3f
2 changed files with 15 additions and 0 deletions

View file

@ -28,3 +28,10 @@ func Copy(b []byte) []byte {
copy(d, b)
return d
}
// Clean wipes the data in b by filling it with zeros.
func Clean(b []byte) {
for i := range b {
b[i] = 0
}
}

View file

@ -49,3 +49,11 @@ func TestCopyReverse(t *testing.T) {
}
}
}
func TestClean(t *testing.T) {
for _, tc := range testCases[1:] { // Empty one will be equal.
cp := Copy(tc.arr)
Clean(cp)
require.NotEqual(t, tc.arr, cp)
}
}