hi
What I have is a simple string compressor that encodes/decodes byte size characters of strings into 2 bits. Example:
extern crate itertools;
use itertools::Itertools;
fn main() {
let myab = "aabbababbaBAs".to_string();
let bitstr = &myab
.chars()
.into_iter()
.map(|c| match c {
'A' | 'a' => "00",
'B' | 'b' => "01",
_ => "11",
})
.collect::<String>();
println!("{:?}", bitstr);
let bitvec = &bitstr
.chars()
.chunks(8)
.into_iter()
.map(|chunk| {
let mut c = chunk.collect::<String>();
if c.len() < 8 {
let l = format!("{:1<1$}", "", (8 - c.len()));
c.push_str(&l);
c
} else {
c
}
})
.collect::<Vec<_>>();
println!("{:?}", bitvec);
let cc: Vec<u8> = bitvec
.into_iter()
.map(|x| u8::from_str_radix(&x, 2).unwrap())
.collect();
println!("{:?}", cc);
let str = cc
.into_iter()
.map(|x| format!("{:08b}", x))
.collect::<String>();
println!("{:?}", str);
let back = str
.chars()
.chunks(2)
.into_iter()
.map(|chunk| {
let c = chunk.collect::<String>();
match &c[..] {
"00" => 'A',
"01" => 'B',
_ => 'X',
}
})
.collect::<String>();
println!("{:?}", back);
}
Short description:
- take a string
- break it into characters
- encode characters (00, 01, 11)
- collect it back to string
- split "01010101" string into 8 char substrings
- treat substrings as bits and convert it into u8
- print array
- do it in revers to get back to 1.
What I am looking for is a more efficient (in speed and memory) implementation of the above procedure .. Any advice ?