From 86861181cc5d452da3009d46348bae743938e1bd Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 10 Apr 2025 11:36:44 +0200 Subject: rewrite Block::clip doesn't change performance, but is nicer to read --- src/block.rs | 14 ++++---------- 1 file 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 -- cgit v1.2.3