1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! ```rust
//! use compressed_string::ComprString;
//!
//! let raw = "It uses the deflate algorithm, which has a small header overhead, \
//! so it's suitable even for short-ish strings";
//!
//! let compr = ComprString::new(raw);
//!
//! assert_eq!(109, raw.len());
//! assert_eq!(84, compr.compressed_len());
//!
//! println!("{}", compr);
//! let string = compr.to_string();
//! ```

use std::io::Read;
use std::io::Write;
use std::fmt;
use flate2::Compression;
use flate2::write::DeflateEncoder;
use flate2::read::DeflateDecoder;

/// DEFLATE-compressed `String`
///
/// Note that conversion from and to the compressed string has a CPU cost, so plan accordingly.
#[derive(Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "with_serde", derive(serde_derive::Serialize, serde_derive::Deserialize))]
pub struct ComprString {
    gz: Box<[u8]>
}

impl ComprString {
    /// Compress the given string.
    ///
    /// You can also use `.into()`
    pub fn new(s: &str) -> Self {
        let mut e = DeflateEncoder::new(Vec::with_capacity(s.len()/2), Compression::best());
        e.write_all(s.as_bytes()).unwrap();
        ComprString{ gz: e.finish().unwrap().into_boxed_slice() }
    }

    /// Decompress the string
    ///
    /// You can also use `.into()`
    pub fn to_string(&self) -> String {
        let mut deflater = DeflateDecoder::new(&self.gz[..]);
        let mut s = String::with_capacity(self.gz.len()*2);
        deflater.read_to_string(&mut s).unwrap();
        s
    }

    /// Length of just the DEFLATE-compressed data
    /// (there's a bit of extra RAM overhead on top of that).
    ///
    /// There's no way to get the original length without decompressing the string first.
    pub fn compressed_len(&self) -> usize {
        self.gz.len()
    }
}

impl fmt::Display for ComprString {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&self.to_string())
    }
}

impl fmt::Debug for ComprString {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.to_string().fmt(f)?;
        write!(f, "@{}B", self.gz.len())
    }
}

impl From<String> for ComprString {
    fn from(o: String) -> Self {
        Self::new(&o)
    }
}

impl Into<String> for ComprString {
    fn into(self) -> String {
        self.to_string()
    }
}

impl From<Box<str>> for ComprString {
    fn from(o: Box<str>) -> Self {
        Self::new(&o)
    }
}

impl Into<Box<str>> for ComprString {
    fn into(self) -> Box<str> {
        self.to_string().into_boxed_str()
    }
}

impl<'a> From<&'a str> for ComprString {
    fn from(o: &'a str) -> Self {
        Self::new(o)
    }
}

#[test]
fn test() {
    let s = ComprString::new("hęllo world");
    assert_eq!("hęllo world", &s.to_string());
    assert_eq!("hęllo world", &format!("{}", s));
    assert_eq!("\"hęllo world\"@17B", &format!("{:?}", s));

    let s2 = ComprString::new("hęllo world");
    assert_eq!(s, s2);

    let s = ComprString::new("");
    assert_eq!("", &s.to_string());
    assert_eq!(2, s.compressed_len());

    let l = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    let s = ComprString::new(l);
    assert_eq!(l, &s.to_string());
    assert!(s.compressed_len() < l.len());
}