From e349079c87a458d0d7c0d6a4b31c4b282e4677e4 Mon Sep 17 00:00:00 2001 From: Stefan Furne Date: Tue, 16 Sep 2025 22:03:11 +0200 Subject: [PATCH] Stay within the set number of tags --- src/output.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/output.rs b/src/output.rs index f121480..2330b3c 100644 --- a/src/output.rs +++ b/src/output.rs @@ -31,7 +31,7 @@ impl Output { let tags: u32 = self.focused_tags.unwrap_or_default(); let mut new_tags: u32 = tags; - let occupied_tags = self.find_set_bits_positions(); + let occupied_tags = self.find_set_bits_positions(*n_tags); match direction { // Only skip unoccupied on user flag and if there are more than one occupied tag @@ -135,7 +135,7 @@ impl Output { } /// Find the indices of set bits - fn find_set_bits_positions(&self) -> Vec { + fn find_set_bits_positions(&self, n_tags: u8) -> Vec { let mut result = Vec::new(); // Iterate over 4-byte chunks of the occupied tags @@ -146,8 +146,12 @@ impl Output { for bit_index in 0..8 { // Check if the bit at bit_index is set = occupied tag if (byte & (1 << bit_index)) != 0 { - // If set, calculate the overall bit index and push it to the result vector - result.push((byte_index * 8 + bit_index) as u8); + // If set, calculate the overall bit index + let index = (byte_index * 8 + bit_index) as u8; + // Only keep it if it's within the number of tags + if index < n_tags { + result.push(index); + } } } }