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

bitfield: add from_bytes() function

This commit is contained in:
Silvan Büdenbender
2019-11-13 17:48:00 +01:00
committed by Alexander Medvednikov
parent ffa6bcfff5
commit f30b0f1017
2 changed files with 40 additions and 0 deletions

View File

@ -77,6 +77,32 @@ fn cleartail(instance mut BitField) {
// public functions
// from_bytes() converts a byte arry into a bitfield.
// Be aware of possible trailing zeroes being added
// due to the underlying 32bit int containers.
pub fn from_bytes(input []byte) BitField {
mut output := new(input.len * 8)
for i, b in input {
pos := i / 4
match i % 4 {
0 {
output.field[pos] = output.field[pos] | (u32(b) << 24)
}
1 {
output.field[pos] = output.field[pos] | (u32(b) << 16)
}
2 {
output.field[pos] = output.field[pos] | (u32(b) << 8)
}
3 {
output.field[pos] = output.field[pos] | u32(b)
}
}
}
return output
}
// str2bf() converts a string of characters ('0' and '1') to a bit
// array. Any character different from '0' is treated as '1'.