Skip to main content

bpack/unpack/
mod.rs

1mod node;
2// mod result_src;
3mod table;
4mod tree;
5
6pub(self) use node::Node;
7pub(self) use table::Table;
8pub(self) use tree::Tree;
9
10
11/// Decompresses byte vector
12///
13/// # Examples
14///
15/// ```
16/// use bpack::{pack, unpack};
17///
18/// let data = "some very long string".as_bytes();
19/// 
20/// if let Some(packed) = pack(data) {
21///     let unpacked = unpack(packed);
22/// 
23///     assert_eq!(data, unpacked.as_slice());
24/// }
25///
26/// # Panics
27/// if passed data is not a byte vector returned from `pack` 
28/// function 
29/// ```
30
31pub fn unpack(packed: Vec<u8>) -> Vec<u8> {
32    let (first, second) = (packed[0], packed[1]);
33    let original_size = (((first as u16) << 8) | second as u16) as usize;
34    let topo: Vec<u8> = packed[2..121].to_vec();
35
36    let tree = Tree::from_topo(topo);
37    let mut table = Table::new();
38    tree.dfs(&mut table);
39
40    let mut result = vec![0; original_size];
41
42    let mut idx = 0;
43
44    for i in 121..packed.len() {
45        table.handle(packed[i], &mut result, &mut idx, original_size);
46    }
47
48    result
49}
50
51// #[cfg(test)]
52// mod test {
53//     use result_src::{result_, src_};
54//     use super::*;
55//     use std::time::{Instant};
56//     #[test]
57//     #[ignore]
58//     fn unpack_test() {
59//         let packed = result_();
60//         let src = src_();
61//         println!("src -> {}", src.len());
62//         let before = Instant::now();
63//         let a = unpack(packed);
64//         let after = Instant::now();
65//         // println!("result -> {:?}", a);
66//         assert_eq!(a, src);
67//         for i in 0..a.len() {
68//             if a[i] != src[i] {
69//                 println!("idx -> {} a_val -> {}, src_val -> {}", i, a[i], src[i]);
70//             }
71//         }
72//         println!("time taken => {:?}", after - before);
73//         assert_eq!(1, 2);
74//     }
75// }