From 185e2a82917f8f22832937c16eb4afe41f8e50ff Mon Sep 17 00:00:00 2001 From: cubicle-jockey <110308650+cubicle-jockey@users.noreply.github.com> Date: Sun, 18 Jun 2023 22:37:57 -0500 Subject: [PATCH 1/2] Added new test cases --- src/_main.rs | 23 ++++- src/cj_binary/bitbuf.rs | 180 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+), 1 deletion(-) diff --git a/src/_main.rs b/src/_main.rs index 7421cfe..6b7daa9 100644 --- a/src/_main.rs +++ b/src/_main.rs @@ -4,32 +4,53 @@ use cj_common::prelude::*; // _main is only meant to help with testing/stepping through the code. fn main() { + println!("test1"); b64_test5(); + println!("test2"); b64_test6(); + println!("test3"); b64_test7(); + println!("test4"); b64_test8(); + println!("test5"); b64_test9(); + println!("test6"); b64_test10(); + println!("test7"); b64_test11(); + println!("test8"); b64_test12(); + println!("test9"); b64_test_iter(); + println!("test10"); b64_test_iter2(); + println!("test11"); b64_test_iter4(); + println!("test12"); b64_test_iter5(); + println!("test13"); perf_test_1(); + println!("test14"); perf_test_2(); + println!("test15"); hex_test1(); + println!("test16"); bit_iter_test1(); + println!("test17"); hex_iter_test1(); + println!("test18"); hex_iter_test2(); + println!("test19"); in_set_test(); + println!("test20"); perf_in_set_test_1(); + println!("test21"); perf_in_set_test_2(); //let x = ((0..5), (6..7), 8, 9); @@ -431,7 +452,7 @@ fn perf_in_set_test_1() { let now = Instant::now(); let mut total = 0usize; let list = [1_000, 10_000, 100_000_000]; - let iters = 1_000_000_000; + let iters = 1_000_000; for _ in 1..iters { for n in list { if n.in_set( diff --git a/src/cj_binary/bitbuf.rs b/src/cj_binary/bitbuf.rs index 92110f8..406de44 100644 --- a/src/cj_binary/bitbuf.rs +++ b/src/cj_binary/bitbuf.rs @@ -936,4 +936,184 @@ mod test { } } } + + #[test] + fn test_u8_vec_iter() { + // vec of 256 u8 values from 0 to 255 + let x = (0..256).map(|x| x as u8).collect::>(); + let mut v = Vec::with_capacity(256); + for i in x.iter_to_bit() { + v.push(i); + } + + assert_eq!( + v.as_slice(), + &[ + false, false, false, false, false, false, false, false, true, false, false, false, + false, false, false, false, false, true, false, false, false, false, false, false, + true, true, false, false, false, false, false, false, false, false, true, false, + false, false, false, false, true, false, true, false, false, false, false, false, + false, true, true, false, false, false, false, false, true, true, true, false, + false, false, false, false, false, false, false, true, false, false, false, false, + true, false, false, true, false, false, false, false, false, true, false, true, + false, false, false, false, true, true, false, true, false, false, false, false, + false, false, true, true, false, false, false, false, true, false, true, true, + false, false, false, false, false, true, true, true, false, false, false, false, + true, true, true, true, false, false, false, false, false, false, false, false, + true, false, false, false, true, false, false, false, true, false, false, false, + false, true, false, false, true, false, false, false, true, true, false, false, + true, false, false, false, false, false, true, false, true, false, false, false, + true, false, true, false, true, false, false, false, false, true, true, false, + true, false, false, false, true, true, true, false, true, false, false, false, + false, false, false, true, true, false, false, false, true, false, false, true, + true, false, false, false, false, true, false, true, true, false, false, false, + true, true, false, true, true, false, false, false, false, false, true, true, true, + false, false, false, true, false, true, true, true, false, false, false, false, + true, true, true, true, false, false, false, true, true, true, true, true, false, + false, false, false, false, false, false, false, true, false, false, true, false, + false, false, false, true, false, false, false, true, false, false, false, true, + false, false, true, true, false, false, false, true, false, false, false, false, + true, false, false, true, false, false, true, false, true, false, false, true, + false, false, false, true, true, false, false, true, false, false, true, true, + true, false, false, true, false, false, false, false, false, true, false, true, + false, false, true, false, false, true, false, true, false, false, false, true, + false, true, false, true, false, false, true, true, false, true, false, true, + false, false, false, false, true, true, false, true, false, false, true, false, + true, true, false, true, false, false, false, true, true, true, false, true, false, + false, true, true, true, true, false, true, false, false, false, false, false, + false, true, true, false, false, true, false, false, false, true, true, false, + false, false, true, false, false, true, true, false, false, true, true, false, + false, true, true, false, false, false, false, true, false, true, true, false, + false, true, false, true, false, true, true, false, false, false, true, true, + false, true, true, false, false, true, true, true, false, true, true, false, false, + false, false, false, true, true, true, false, false, true, false, false, true, + true, true, false, false, false, true, false, true, true, true, false, false, true, + true, false, true, true, true, false, false, false, false, true, true, true, true, + false, false, true, false, true, true, true, true, false, false, false, true, true, + true, true, true, false, false, true, true, true, true, true, true, false, false, + false, false, false, false, false, false, true, false, true, false, false, false, + false, false, true, false, false, true, false, false, false, false, true, false, + true, true, false, false, false, false, true, false, false, false, true, false, + false, false, true, false, true, false, true, false, false, false, true, false, + false, true, true, false, false, false, true, false, true, true, true, false, + false, false, true, false, false, false, false, true, false, false, true, false, + true, false, false, true, false, false, true, false, false, true, false, true, + false, false, true, false, true, true, false, true, false, false, true, false, + false, false, true, true, false, false, true, false, true, false, true, true, + false, false, true, false, false, true, true, true, false, false, true, false, + true, true, true, true, false, false, true, false, false, false, false, false, + true, false, true, false, true, false, false, false, true, false, true, false, + false, true, false, false, true, false, true, false, true, true, false, false, + true, false, true, false, false, false, true, false, true, false, true, false, + true, false, true, false, true, false, true, false, false, true, true, false, true, + false, true, false, true, true, true, false, true, false, true, false, false, + false, false, true, true, false, true, false, true, false, false, true, true, + false, true, false, false, true, false, true, true, false, true, false, true, true, + false, true, true, false, true, false, false, false, true, true, true, false, true, + false, true, false, true, true, true, false, true, false, false, true, true, true, + true, false, true, false, true, true, true, true, true, false, true, false, false, + false, false, false, false, true, true, false, true, false, false, false, false, + true, true, false, false, true, false, false, false, true, true, false, true, true, + false, false, false, true, true, false, false, false, true, false, false, true, + true, false, true, false, true, false, false, true, true, false, false, true, true, + false, false, true, true, false, true, true, true, false, false, true, true, false, + false, false, false, true, false, true, true, false, true, false, false, true, + false, true, true, false, false, true, false, true, false, true, true, false, true, + true, false, true, false, true, true, false, false, false, true, true, false, true, + true, false, true, false, true, true, false, true, true, false, false, true, true, + true, false, true, true, false, true, true, true, true, false, true, true, false, + false, false, false, false, true, true, true, false, true, false, false, false, + true, true, true, false, false, true, false, false, true, true, true, false, true, + true, false, false, true, true, true, false, false, false, true, false, true, true, + true, false, true, false, true, false, true, true, true, false, false, true, true, + false, true, true, true, false, true, true, true, false, true, true, true, false, + false, false, false, true, true, true, true, false, true, false, false, true, true, + true, true, false, false, true, false, true, true, true, true, false, true, true, + false, true, true, true, true, false, false, false, true, true, true, true, true, + false, true, false, true, true, true, true, true, false, false, true, true, true, + true, true, true, false, true, true, true, true, true, true, true, false, false, + false, false, false, false, false, false, true, true, false, false, false, false, + false, false, true, false, true, false, false, false, false, false, true, true, + true, false, false, false, false, false, true, false, false, true, false, false, + false, false, true, true, false, true, false, false, false, false, true, false, + true, true, false, false, false, false, true, true, true, true, false, false, + false, false, true, false, false, false, true, false, false, false, true, true, + false, false, true, false, false, false, true, false, true, false, true, false, + false, false, true, true, true, false, true, false, false, false, true, false, + false, true, true, false, false, false, true, true, false, true, true, false, + false, false, true, false, true, true, true, false, false, false, true, true, true, + true, true, false, false, false, true, false, false, false, false, true, false, + false, true, true, false, false, false, true, false, false, true, false, true, + false, false, true, false, false, true, true, true, false, false, true, false, + false, true, false, false, true, false, true, false, false, true, true, false, + true, false, true, false, false, true, false, true, true, false, true, false, + false, true, true, true, true, false, true, false, false, true, false, false, + false, true, true, false, false, true, true, false, false, true, true, false, + false, true, false, true, false, true, true, false, false, true, true, true, false, + true, true, false, false, true, false, false, true, true, true, false, false, true, + true, false, true, true, true, false, false, true, false, true, true, true, true, + false, false, true, true, true, true, true, true, false, false, true, false, false, + false, false, false, true, false, true, true, false, false, false, false, true, + false, true, false, true, false, false, false, true, false, true, true, true, + false, false, false, true, false, true, false, false, true, false, false, true, + false, true, true, false, true, false, false, true, false, true, false, true, true, + false, false, true, false, true, true, true, true, false, false, true, false, true, + false, false, false, true, false, true, false, true, true, false, false, true, + false, true, false, true, false, true, false, true, false, true, false, true, true, + true, false, true, false, true, false, true, false, false, true, true, false, true, + false, true, true, false, true, true, false, true, false, true, false, true, true, + true, false, true, false, true, true, true, true, true, false, true, false, true, + false, false, false, false, true, true, false, true, true, false, false, false, + true, true, false, true, false, true, false, false, true, true, false, true, true, + true, false, false, true, true, false, true, false, false, true, false, true, true, + false, true, true, false, true, false, true, true, false, true, false, true, true, + false, true, true, false, true, true, true, true, false, true, true, false, true, + false, false, false, true, true, true, false, true, true, false, false, true, true, + true, false, true, false, true, false, true, true, true, false, true, true, true, + false, true, true, true, false, true, false, false, true, true, true, true, false, + true, true, false, true, true, true, true, false, true, false, true, true, true, + true, true, false, true, true, true, true, true, true, true, false, true, false, + false, false, false, false, false, true, true, true, false, false, false, false, + false, true, true, false, true, false, false, false, false, true, true, true, true, + false, false, false, false, true, true, false, false, true, false, false, false, + true, true, true, false, true, false, false, false, true, true, false, true, true, + false, false, false, true, true, true, true, true, false, false, false, true, true, + false, false, false, true, false, false, true, true, true, false, false, true, + false, false, true, true, false, true, false, true, false, false, true, true, true, + true, false, true, false, false, true, true, false, false, true, true, false, + false, true, true, true, false, true, true, false, false, true, true, false, true, + true, true, false, false, true, true, true, true, true, true, false, false, true, + true, false, false, false, false, true, false, true, true, true, false, false, + false, true, false, true, true, false, true, false, false, true, false, true, true, + true, true, false, false, true, false, true, true, false, false, true, false, true, + false, true, true, true, false, true, false, true, false, true, true, false, true, + true, false, true, false, true, true, true, true, true, false, true, false, true, + true, false, false, false, true, true, false, true, true, true, false, false, true, + true, false, true, true, false, true, false, true, true, false, true, true, true, + true, false, true, true, false, true, true, false, false, true, true, true, false, + true, true, true, false, true, true, true, false, true, true, false, true, true, + true, true, false, true, true, true, true, true, true, true, false, true, true, + false, false, false, false, false, true, true, true, true, false, false, false, + false, true, true, true, false, true, false, false, false, true, true, true, true, + true, false, false, false, true, true, true, false, false, true, false, false, + true, true, true, true, false, true, false, false, true, true, true, false, true, + true, false, false, true, true, true, true, true, true, false, false, true, true, + true, false, false, false, true, false, true, true, true, true, false, false, true, + false, true, true, true, false, true, false, true, false, true, true, true, true, + true, false, true, false, true, true, true, false, false, true, true, false, true, + true, true, true, false, true, true, false, true, true, true, false, true, true, + true, false, true, true, true, true, true, true, true, false, true, true, true, + false, false, false, false, true, true, true, true, true, false, false, false, + true, true, true, true, false, true, false, false, true, true, true, true, true, + true, false, false, true, true, true, true, false, false, true, false, true, true, + true, true, true, false, true, false, true, true, true, true, false, true, true, + false, true, true, true, true, true, true, true, false, true, true, true, true, + false, false, false, true, true, true, true, true, true, false, false, true, true, + true, true, true, false, true, false, true, true, true, true, true, true, true, + false, true, true, true, true, true, false, false, true, true, true, true, true, + true, true, false, true, true, true, true, true, true, false, true, true, true, + true, true, true, true, true, true, true, true, true, true, true, true + ] + ) + } } From e8f54e807dd9a54041f5edd69dd997dc06fc3c59 Mon Sep 17 00:00:00 2001 From: cubicle-jockey <110308650+cubicle-jockey@users.noreply.github.com> Date: Sat, 10 Aug 2024 01:35:13 -0500 Subject: [PATCH 2/2] Changed static to const on a couple of lookup tables and variables incremented version to 1.0.1 --- Cargo.toml | 2 +- src/cj_binary/bitbuf.rs | 10 +++++----- src/cj_binary/hex.rs | 28 +++++++++++++--------------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b257713..effb270 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cj_common" -version = "1.0.0" +version = "1.0.1" edition = "2021" keywords = ["hex", "base64", "range", "bit", "inset"] categories = ["encoding", "parsing"] diff --git a/src/cj_binary/bitbuf.rs b/src/cj_binary/bitbuf.rs index 406de44..c09ebc1 100644 --- a/src/cj_binary/bitbuf.rs +++ b/src/cj_binary/bitbuf.rs @@ -359,11 +359,11 @@ impl Bitflag for u128 { /// ``` // TO.DO these need moved out of bitbuf and into a dedicated file -static DEF_U8: &u8 = &0; -static DEF_U16: &u16 = &0; -static DEF_U32: &u32 = &0; -static DEF_U64: &u64 = &0; -static DEF_U128: &u128 = &0; +const DEF_U8: &u8 = &0; +const DEF_U16: &u16 = &0; +const DEF_U32: &u32 = &0; +const DEF_U64: &u64 = &0; +const DEF_U128: &u128 = &0; /// Trait for returning references to default static values for a give types pub trait DefaultStatic { diff --git a/src/cj_binary/hex.rs b/src/cj_binary/hex.rs index dbbe165..b67ded5 100644 --- a/src/cj_binary/hex.rs +++ b/src/cj_binary/hex.rs @@ -22,7 +22,7 @@ use std::slice::Iter; use std::str::Chars; -static HEX_TABLE: [&str; 256] = [ +const HEX_TABLE: [&str; 256] = [ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", @@ -41,7 +41,7 @@ static HEX_TABLE: [&str; 256] = [ "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF", ]; -static HEX_TABLE_LOWER: [&str; 256] = [ +const HEX_TABLE_LOWER: [&str; 256] = [ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c", "1d", "1e", "1f", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2a", "2b", "2c", "2d", "2e", "2f", @@ -67,7 +67,7 @@ static HEX_TABLE_LOWER: [&str; 256] = [ /// assert_eq!(u8_to_hex_str(&0xD1), "D1"); /// ``` #[inline] -pub fn u8_to_hex_str(value: &u8) -> &'static str { +pub const fn u8_to_hex_str(value: &u8) -> &'static str { HEX_TABLE[*value as usize] } @@ -78,7 +78,7 @@ pub fn u8_to_hex_str(value: &u8) -> &'static str { /// assert_eq!(u8_to_hex_low_str(&0xD1), "d1"); /// ``` #[inline] -pub fn u8_to_hex_low_str(value: &u8) -> &'static str { +pub const fn u8_to_hex_low_str(value: &u8) -> &'static str { HEX_TABLE_LOWER[*value as usize] } @@ -114,10 +114,8 @@ pub fn u8_to_hex_low(value: &u8) -> String { #[inline] pub fn u8_array_to_hex(value: &[u8]) -> String { let mut rslt = String::with_capacity(value.len() * 2); - let _: () = value - .iter() - .map(|f| rslt.push_str(u8_to_hex_str(f))) - .collect(); + value.iter().for_each(|f| rslt.push_str(u8_to_hex_str(f))); + rslt } @@ -131,10 +129,10 @@ pub fn u8_array_to_hex(value: &[u8]) -> String { #[inline] pub fn u8_array_to_hex_low(value: &[u8]) -> String { let mut rslt = String::with_capacity(value.len() * 2); - let _: () = value + value .iter() - .map(|f| rslt.push_str(u8_to_hex_low_str(f))) - .collect(); + .for_each(|f| rslt.push_str(u8_to_hex_low_str(f))); + rslt } @@ -146,7 +144,7 @@ pub fn u8_array_to_hex_low(value: &[u8]) -> String { /// assert_eq!(hex_char_to_u8(&'G'),None); /// ``` #[inline] -pub fn hex_char_to_u8(hex1: &char) -> Option { +pub const fn hex_char_to_u8(hex1: &char) -> Option { let r = match hex1 { '0' => 0u8, '1' => 1, @@ -181,9 +179,9 @@ pub fn hex_char_to_u8(hex1: &char) -> Option { /// ``` #[inline] pub fn hex_str_to_u8(hex2: &str) -> Option { - if hex2.chars().count() > 0 { + if hex2.len() > 0 { let mut r: u8; - if let Some(x) = hex_char_to_u8(&hex2.chars().next().unwrap()) { + if let Some(x) = hex_char_to_u8(&hex2.chars().next()?) { r = x << 4; if let Some(y) = hex_char_to_u8(&hex2.chars().nth(1).unwrap_or('X')) { r += y; @@ -437,7 +435,7 @@ pub fn hex_str_to_u8_vec(hexstr: &str) -> Option> { let mut ca: HexArray = ['0', '0']; let mut ct = 0; - let mut v: Vec = Vec::with_capacity((hexstr.chars().count() / 2) + 1); + let mut v: Vec = Vec::with_capacity((hexstr.len() / 2) + 1); if hexstr.len() % 2 != 0 { ct = 1;