Remaining builtin

This commit is contained in:
microproofs 2024-12-06 12:17:48 +07:00
parent b507992172
commit a1804863dc
No known key found for this signature in database
GPG Key ID: 14F93C84DE6AFD17
1 changed files with 36 additions and 1 deletions

View File

@ -1702,7 +1702,42 @@ impl DefaultFunction {
Ok(Value::integer(hamming::weight(bytes).into()))
}
DefaultFunction::FindFirstSetBit => todo!(),
DefaultFunction::FindFirstSetBit => {
let bytes = args[0].unwrap_byte_string()?;
let first_bit =
bytes
.into_iter()
.rev()
.enumerate()
.find_map(|(byte_index, value)| {
let value = value.reverse_bits();
let first_bit: Option<usize> = if value > 128 {
Some(0)
} else if value > 64 {
Some(1)
} else if value > 32 {
Some(2)
} else if value > 16 {
Some(3)
} else if value > 8 {
Some(4)
} else if value > 4 {
Some(5)
} else if value > 2 {
Some(6)
} else if value > 1 {
Some(7)
} else {
None
};
first_bit.map(|bit| isize::try_from(bit + byte_index * 8).unwrap())
});
Ok(Value::integer(first_bit.unwrap_or(-1).into()))
}
DefaultFunction::Ripemd_160 => {
use cryptoxide::{digest::Digest, ripemd160::Ripemd160};