From 0eb64908540d6b62323823616b1d42fda14a8417 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 02:24:23 +0200 Subject: [PATCH 1/7] feat: split unsigned numbers into bytes #76 --- members/libpt-bintols/src/lib.rs | 1 + members/libpt-bintols/src/split_numbers.rs | 28 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 members/libpt-bintols/src/split_numbers.rs diff --git a/members/libpt-bintols/src/lib.rs b/members/libpt-bintols/src/lib.rs index ca13f78..54a907f 100644 --- a/members/libpt-bintols/src/lib.rs +++ b/members/libpt-bintols/src/lib.rs @@ -25,3 +25,4 @@ pub const YOBI: u128 = 2u128.pow(80); // use libpt_core; pub mod datalayout; pub mod display; +pub mod split_numbers; diff --git a/members/libpt-bintols/src/split_numbers.rs b/members/libpt-bintols/src/split_numbers.rs new file mode 100644 index 0000000..84a29cb --- /dev/null +++ b/members/libpt-bintols/src/split_numbers.rs @@ -0,0 +1,28 @@ +//! # Split numbers into bits and bytes +//! +//! Sometimes, you need a large integer in the form of many bytes, so split into [u8]. +//! Rust provides + +use num_traits::Unsigned; + +/// split an integer into it's bytes, ignoring those bytes that would be all zero. +/// +/// If the integer is zero, the Vec contains a single null byte. +pub fn unsigned_to_vec(mut num: T) -> Vec +where + T: Unsigned, + T: std::ops::ShrAssign, + T: std::cmp::PartialOrd, + u8: std::convert::From, + T: Copy, +{ + if num == 0 { + return vec![0]; + } + let mut buf: Vec = Vec::new(); + while num > 0 { + buf.push(num.into()); + num >>= 8; + } + buf +} From dc506de8aa22d06ea455e6d3747d944e1f3cc03b Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 02:47:12 +0200 Subject: [PATCH 2/7] test: split-numbers #76 test if splitting of numbers into Vec works correctly --- members/libpt-bintols/tests/split_numbers.rs | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 members/libpt-bintols/tests/split_numbers.rs diff --git a/members/libpt-bintols/tests/split_numbers.rs b/members/libpt-bintols/tests/split_numbers.rs new file mode 100644 index 0000000..52b5ac5 --- /dev/null +++ b/members/libpt-bintols/tests/split_numbers.rs @@ -0,0 +1,35 @@ +use libpt_bintols::split_numbers::*; + +#[test] +fn split_u128() { + let source = [16, 255, 256, 0, u128::MAX, u64::MAX as u128]; + let correct = [ + vec![16], + vec![255], + vec![255, 1], + vec![0], + vec![ + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + ], + vec![255, 255, 255, 255, 255, 255, 255, 255], + ]; + for (i, n) in source.iter().enumerate() { + assert_eq!(unsigned_to_vec(*n), correct[i]); + } +} + +#[test] +fn split_u64() { + let source = [16, 255, 256, 0, u64::MAX, u32::MAX as u64]; + let correct = [ + vec![16], + vec![255], + vec![255, 1], + vec![0], + vec![255, 255, 255, 255, 255, 255, 255, 255], + vec![255, 255, 255, 255], + ]; + for (i, n) in source.iter().enumerate() { + assert_eq!(unsigned_to_vec(*n), correct[i]); + } +} From 77d473e22c0a9587e98016093b375d4843b0786f Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 02:47:40 +0200 Subject: [PATCH 3/7] refactor: split-numbers generic by converting to u128 #76 --- members/libpt-bintols/src/split_numbers.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/members/libpt-bintols/src/split_numbers.rs b/members/libpt-bintols/src/split_numbers.rs index 84a29cb..20cefa6 100644 --- a/members/libpt-bintols/src/split_numbers.rs +++ b/members/libpt-bintols/src/split_numbers.rs @@ -3,25 +3,20 @@ //! Sometimes, you need a large integer in the form of many bytes, so split into [u8]. //! Rust provides -use num_traits::Unsigned; - /// split an integer into it's bytes, ignoring those bytes that would be all zero. /// /// If the integer is zero, the Vec contains a single null byte. -pub fn unsigned_to_vec(mut num: T) -> Vec +pub fn unsigned_to_vec(num: T) -> Vec where - T: Unsigned, - T: std::ops::ShrAssign, - T: std::cmp::PartialOrd, - u8: std::convert::From, - T: Copy, + u128: std::convert::From { + let mut num: u128 = num.into(); if num == 0 { return vec![0]; } let mut buf: Vec = Vec::new(); while num > 0 { - buf.push(num.into()); + buf.push(num as u8); num >>= 8; } buf From 9da3c584cdad09d41a80e4e5457af473c6bbfc33 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 17:13:18 +0200 Subject: [PATCH 4/7] refactor: add doc comment for split numbers and change order to msb first #76 --- members/libpt-bintols/src/split_numbers.rs | 20 ++++++++++++++++++-- members/libpt-bintols/tests/split_numbers.rs | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/members/libpt-bintols/src/split_numbers.rs b/members/libpt-bintols/src/split_numbers.rs index 20cefa6..1405cbf 100644 --- a/members/libpt-bintols/src/split_numbers.rs +++ b/members/libpt-bintols/src/split_numbers.rs @@ -3,9 +3,24 @@ //! Sometimes, you need a large integer in the form of many bytes, so split into [u8]. //! Rust provides -/// split an integer into it's bytes, ignoring those bytes that would be all zero. + +/// Split unsigned integers into a [Vec] of [u8]s /// -/// If the integer is zero, the Vec contains a single null byte. +/// Say you have the [u32] 1717 (binary: `00000000 00000000 00000110 10110101 `). This number would +/// be splitted to `vec![0b00000110, 0b10110101]`. +/// +/// The 0 bytes of the numbers will be discarded (unless the number is 0, then the Vec contains a +/// single Null byte.) and the remaining parts of the numbers are inserted into a Vec as [u8]. +/// +/// # Examples +/// +/// ``` +/// # use libpt_bintols::split_numbers::*; +/// +/// let x: u32 = 1717; +/// +/// assert_eq!(unsigned_to_vec(x), vec![0b00000110, 0b10110101]); +/// ``` pub fn unsigned_to_vec(num: T) -> Vec where u128: std::convert::From @@ -19,5 +34,6 @@ where buf.push(num as u8); num >>= 8; } + buf.reverse(); buf } diff --git a/members/libpt-bintols/tests/split_numbers.rs b/members/libpt-bintols/tests/split_numbers.rs index 52b5ac5..2ee6a13 100644 --- a/members/libpt-bintols/tests/split_numbers.rs +++ b/members/libpt-bintols/tests/split_numbers.rs @@ -24,7 +24,7 @@ fn split_u64() { let correct = [ vec![16], vec![255], - vec![255, 1], + vec![1, 255], vec![0], vec![255, 255, 255, 255, 255, 255, 255, 255], vec![255, 255, 255, 255], From 73d291a47723aef04b2a2daee33e77619ef812ce Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 17:19:42 +0200 Subject: [PATCH 5/7] fix: split-numbers test solutions were wrong #76 --- members/libpt-bintols/tests/split_numbers.rs | 28 +++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/members/libpt-bintols/tests/split_numbers.rs b/members/libpt-bintols/tests/split_numbers.rs index 2ee6a13..3f461a3 100644 --- a/members/libpt-bintols/tests/split_numbers.rs +++ b/members/libpt-bintols/tests/split_numbers.rs @@ -2,16 +2,25 @@ use libpt_bintols::split_numbers::*; #[test] fn split_u128() { - let source = [16, 255, 256, 0, u128::MAX, u64::MAX as u128]; + let source = [ + 16, + 255, + 256, + 0, + u128::MAX, + u64::MAX as u128, + u64::MAX as u128 + 1, + ]; let correct = [ vec![16], vec![255], - vec![255, 1], + vec![1, 0], vec![0], vec![ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, ], vec![255, 255, 255, 255, 255, 255, 255, 255], + vec![1, 0, 0, 0, 0, 0, 0, 0, 0], ]; for (i, n) in source.iter().enumerate() { assert_eq!(unsigned_to_vec(*n), correct[i]); @@ -20,14 +29,25 @@ fn split_u128() { #[test] fn split_u64() { - let source = [16, 255, 256, 0, u64::MAX, u32::MAX as u64]; + let source = [ + 16, + 255, + 256, + 0, + u64::MAX, + u32::MAX as u64, + 0b1_00000001, + 0b10011011_10110101_11110000_00110011, + ]; let correct = [ vec![16], vec![255], - vec![1, 255], + vec![1, 0], vec![0], vec![255, 255, 255, 255, 255, 255, 255, 255], vec![255, 255, 255, 255], + vec![1, 1], + vec![0b10011011, 0b10110101, 0b11110000, 0b00110011], ]; for (i, n) in source.iter().enumerate() { assert_eq!(unsigned_to_vec(*n), correct[i]); From 75feb7b2b022baf03fa47bd51a343034f7c9e68a Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 15:22:49 +0000 Subject: [PATCH 6/7] automatic cargo CI changes --- members/libpt-bintols/src/split_numbers.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/members/libpt-bintols/src/split_numbers.rs b/members/libpt-bintols/src/split_numbers.rs index 1405cbf..7b3bda2 100644 --- a/members/libpt-bintols/src/split_numbers.rs +++ b/members/libpt-bintols/src/split_numbers.rs @@ -3,7 +3,6 @@ //! Sometimes, you need a large integer in the form of many bytes, so split into [u8]. //! Rust provides - /// Split unsigned integers into a [Vec] of [u8]s /// /// Say you have the [u32] 1717 (binary: `00000000 00000000 00000110 10110101 `). This number would @@ -23,7 +22,7 @@ /// ``` pub fn unsigned_to_vec(num: T) -> Vec where - u128: std::convert::From + u128: std::convert::From, { let mut num: u128 = num.into(); if num == 0 { From 401faca4db128a00fd3cadd1510a540760ed8b7c Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 17:36:26 +0200 Subject: [PATCH 7/7] ci install python --- .github/workflows/cargo.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/cargo.yaml b/.github/workflows/cargo.yaml index d2c9936..7d29156 100644 --- a/.github/workflows/cargo.yaml +++ b/.github/workflows/cargo.yaml @@ -22,6 +22,10 @@ jobs: run: | rustup component add rustfmt rustup component add clippy + - name: install python + uses: actions/setup-python@v5 + with: + python-version: '3.11' - name: config custom registry run: | mkdir -p ~/.cargo/