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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#![feature(non_ascii_idents, static_in_const, more_struct_aliases)]

#[macro_use] extern crate lazy_static;

use std::io::{ self, Write, Read };
use std::cmp::min;
use std::collections::HashMap;

include!(concat!(env!("OUT_DIR"), "/table.rs"));

lazy_static! {
    static ref INV_CHINESE_WORD_MAP: HashMap<char, u16> = {
        CHINESE_CHAR_TABLE // XXX: maybe const ?
            .iter()
            .enumerate()
            .map(|(i, &c)| (c, i as u16))
            .collect()
    };

    static ref INV_END_CHINESE_CHAR_TABLE: HashMap<char, u16> = {
        END_CHINESE_CHAR_TABLE
            .iter()
            .enumerate()
            .map(|(i, &c)| (c, i as u16))
            .collect()
    };
}

pub const CHAR_BITS: usize = 12;
const BYTE_BITS: usize = 8;


/// ```
/// use std::io::Write;
/// use hanzi4096::ZiWrite;
///
/// let mut w = ZiWrite::new();
/// write!(w, "Hello 汉字!").unwrap();
/// assert_eq!(w.into_string(), "贰娃迤交杀萝尻淳");
/// ```
#[derive(Debug, Clone)]
pub struct 字写 {
    buff: String,
    char_buf: u16,
    bits: usize
}

pub type ZiWrite = 字写;

impl Default for 字写 {
    fn default() -> Self {
        Self {
            buff: String::new(),
            char_buf: 0,
            bits: 0
        }
    }
}

impl 字写 {
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            buff: String::with_capacity(capacity),
            char_buf: 0,
            bits: 0
        }
    }

    #[inline]
    pub fn as_str(&self) -> &str {
        &self.buff
    }

    #[inline]
    pub fn into_string(self) -> String {
        self.buff
    }
}

impl Write for 字写 {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        for mut b in buf.iter().map(|&b| b as u16) {
            let bits_left = CHAR_BITS - self.bits;

            let bits = if let Some(bits) = BYTE_BITS.checked_sub(bits_left) {
                let bb = b & ((1 << bits_left) - 1);
                self.char_buf |= bb << self.bits;
                b >>= bits_left;
                self.bits += bits_left;

                self.flush()?;
                bits
            } else {
                BYTE_BITS
            };

            self.char_buf |= b << self.bits;
            self.bits += bits;

            if self.bits >= CHAR_BITS { self.flush()? };
        }

        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        if self.bits > 0 {
            self.buff.push(if self.bits < 12 {
                END_CHINESE_CHAR_TABLE[self.char_buf as usize]
            } else {
                CHINESE_CHAR_TABLE[self.char_buf as usize]
            });
            self.char_buf = 0;
            self.bits = 0;
        }

        Ok(())
    }
}


/// ```
/// use std::io::Read;
/// use hanzi4096::ZiRead;
///
/// let mut r = ZiRead::from(
///     "桃之夭夭灼灼其华之子于归宜其室家"
/// );
/// let mut output = [0; 24];
/// r.read(&mut output).unwrap();
/// assert_eq!(
///     output,
///     [51, 151, 3, 125, 208, 7, 84, 67, 53, 227, 115, 29, 57, 240, 3, 23, 144, 14, 253, 52, 62, 160, 38, 131]
/// );
/// ```
#[derive(Debug, Clone)]
pub struct 字读 {
    buff: String,
    cursor: usize,
    bits: usize,
    ignore_flag: bool
}

pub type ZiRead = 字读;

impl<'a> From<&'a str> for 字读 {
    fn from(s: &str) -> Self {
        Self::from(s.to_string())
    }
}

impl From<String> for 字读 {
    fn from(s: String) -> Self {
        Self {
            buff: s,
            cursor: 0,
            bits: 0,
            ignore_flag: false
        }
    }
}

impl 字读 {
    /// Ignore invalid char.
    ///
    /// ```
    /// use std::io::Read;
    /// use hanzi4096::{ self, ZiRead };
    ///
    /// let text = "
    ///     南有乔木 不可休息
    ///     汉有游女 不可求思
    ///     汉之广矣 不可泳思
    ///     江之永矣 不可方思
    /// ";
    ///
    /// let mut r = ZiRead::from(text);
    /// let mut output = Vec::new();
    /// r.with_ignore(true);
    /// r.read_to_end(&mut output).unwrap();
    ///
    /// assert_eq!(
    ///     hanzi4096::encode(&output),
    ///     text.lines()
    ///         .flat_map(|line| line.split_whitespace())
    ///         .collect::<String>()
    /// );
    /// ```
    pub fn with_ignore(&mut self, flag: bool) -> &mut Self {
        self.ignore_flag = flag;
        self
    }
}

impl Read for 字读 {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let mut count = 0;
        let mut byte_bits = 0;
        let mut end = false;

        for c in self.buff.chars().skip(self.cursor) {
            let mut b = match INV_CHINESE_WORD_MAP.get(&c) {
                Some(&b) => b,
                None => match INV_END_CHINESE_CHAR_TABLE.get(&c) {
                    Some(&b) => {
                        end = true;
                        b
                    },
                    None => {
                        self.cursor += 1;
                        if self.ignore_flag {
                            continue;
                        } else {
                            return Err(io::Error::new(io::ErrorKind::InvalidData, c.to_string()));
                        }
                    }
                }
            };
            b >>= self.bits;

            loop {
                if count >= buf.len() {
                    return Ok(count);
                }

                let min_left = min(CHAR_BITS - self.bits, BYTE_BITS - byte_bits);

                let bb = b & ((1 << min_left) - 1);
                buf[count] |= (bb as u8) << byte_bits;
                b >>= min_left;
                self.bits += min_left;
                byte_bits += min_left;

                if end {
                    self.bits = CHAR_BITS;
                    byte_bits = BYTE_BITS;
                }
                if byte_bits >= BYTE_BITS {
                    count += 1;
                    byte_bits -= BYTE_BITS;
                }
                if self.bits >= CHAR_BITS {
                    self.bits -= CHAR_BITS;
                    break;
                }
            }
            self.cursor += 1;
        }

        Ok(count)
    }
}

#[inline]
pub fn encode(input: &[u8]) -> String {
    let mut w = 字写::with_capacity(
        (input.len() as f64 * BYTE_BITS as f64 / CHAR_BITS as f64).ceil() as usize
        * 3
    );
    w.write(input).expect("unreachable");
    w.flush().expect("unreachable");
    w.into_string()
}

#[inline]
pub fn decode(input: &str) -> io::Result<Vec<u8>> {
    let mut r = 字读::from(input);
    let mut output = Vec::with_capacity(input.chars().count() * CHAR_BITS / 8);
    r.read_to_end(&mut output)?;
    Ok(output)
}