Simplify read_scriptbool

Simplify `read_scriptbool` by doing:

- Use `split_last` to get at the last element
- Mask the last byte against ^0x80 instead of using two equality
  statements
This commit is contained in:
Tobin Harding 2022-01-26 16:52:41 +11:00
parent 4b6e86658d
commit df7bb03a67
1 changed files with 4 additions and 6 deletions

View File

@ -239,12 +239,10 @@ pub fn read_scriptint(v: &[u8]) -> Result<i64, Error> {
/// else as true", except that the overflow rules don't apply.
#[inline]
pub fn read_scriptbool(v: &[u8]) -> bool {
let last = match v.last() {
Some(last) => *last,
None => return false,
};
!((last == 0x00 || last == 0x80) && v.iter().rev().skip(1).all(|&b| b == 0))
match v.split_last() {
Some((last, rest)) => !((last & !0x80 == 0x00) && rest.iter().all(|&b| b == 0)),
None => false,
}
}
/// Read a script-encoded unsigned integer