diff options
author | Daniel Schadt <kingdread@gmx.de> | 2025-04-10 11:36:44 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2025-04-10 18:22:46 +0200 |
commit | 86861181cc5d452da3009d46348bae743938e1bd (patch) | |
tree | eaf3073c750ae543ea3dc29c2e36079543aa8ea3 /src | |
parent | 9b37e6e224bc42cf13362273724f761ec45d420a (diff) | |
download | zears-86861181cc5d452da3009d46348bae743938e1bd.tar.gz zears-86861181cc5d452da3009d46348bae743938e1bd.tar.bz2 zears-86861181cc5d452da3009d46348bae743938e1bd.zip |
rewrite Block::clip
doesn't change performance, but is nicer to read
Diffstat (limited to 'src')
-rw-r--r-- | src/block.rs | 14 |
1 files changed, 4 insertions, 10 deletions
diff --git a/src/block.rs b/src/block.rs index e9ba527..3a9eea2 100644 --- a/src/block.rs +++ b/src/block.rs @@ -41,17 +41,11 @@ impl Block { } /// Clip the block by setting all bits beyond the given length to 0. - pub fn clip(&self, mut length: usize) -> Block { - let mut block = self.0; - for byte in &mut block { - if length == 0 { - *byte = 0; - } else if length < 8 { - *byte &= 0xff << (8 - length); - } - length = length.saturating_sub(8); + pub fn clip(&self, length: usize) -> Block { + match length { + 0 => Block::default(), + _ => Block::from_int(self.to_int() & (u128::MAX << (128 - length))), } - Block(block) } /// Computes self * 2^exponent |