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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Rust-BaseHangul: A binary-to-text encoding with readable Hangul syllables
// Written by Kang Seonghoon
//
// Any copyright is dedicated to the Public Domain.
// https://creativecommons.org/publicdomain/zero/1.0/

//! An implementation of [BaseHangul](https://BaseHangul.github.io) in Rust.

#![feature(into_cow)] // lib stability features as per RFC #507

extern crate encoding_index_korean;

use std::{char, iter};
use std::borrow::{Cow, IntoCow};

/// The enum that contains either `T` or a decoding error.
pub type DecodeResult<T> = Result<T, Cow<'static, str>>;

// --------------------&<--------------------
// code to index and vice versa

const NCHARS: u16 = 2350;
const NCOLS: u16 = 94;
const STRIDE: u16 = 190;
const LEAD_OFFSET: u16 = 0xb0 - 0x81;
const TRAIL_OFFSET: u16 = 0xa1 - 0x41;

fn index_to_char(i: u16) -> char {
    debug_assert!(i < NCHARS);
    let row = i / NCOLS + LEAD_OFFSET;
    let col = i % NCOLS + TRAIL_OFFSET;
    let code = row * STRIDE + col;
    char::from_u32(encoding_index_korean::euc_kr::forward(code)).unwrap()
}

fn char_to_index(c: char) -> Option<u16> {
    let code = encoding_index_korean::euc_kr::backward(c as u32);
    let row = code / STRIDE;
    let col = code % STRIDE;
    if row >= LEAD_OFFSET && col >= TRAIL_OFFSET {
        let index = (row - LEAD_OFFSET) * NCOLS + (col - TRAIL_OFFSET);
        if index < NCHARS { return Some(index); }
    }
    None
}

#[cfg(test)]
#[test]
fn test_index_to_char() {
    assert_eq!(index_to_char(0), '가');
    assert_eq!(index_to_char(1), '각');
    assert_eq!(index_to_char(2), '간');
    assert_eq!(index_to_char(2349), '힝');
}

#[cfg(test)]
#[test]
fn test_char_to_index() {
    assert_eq!(char_to_index('\0'), None);
    assert_eq!(char_to_index('a'), None);
    assert_eq!(char_to_index('가'), Some(0));
    assert_eq!(char_to_index('각'), Some(1));
    assert_eq!(char_to_index('갂'), None);
    assert_eq!(char_to_index('갃'), None);
    assert_eq!(char_to_index('간'), Some(2));
    assert_eq!(char_to_index('힝'), Some(2349));
    assert_eq!(char_to_index('힣'), None);
    assert_eq!(char_to_index('\u{ffff}'), None);
    assert_eq!(char_to_index('\u{10ffff}'), None);
}

#[cfg(test)]
#[test]
fn test_char_to_index_to_char() {
    let mut nextindex = 0;
    for ch in ('가' as u32)..('힣' as u32) {
        let ch = char::from_u32(ch).unwrap();
        if let Some(index) = char_to_index(ch) {
            assert_eq!(index, nextindex);
            nextindex += 1;
            assert_eq!(index_to_char(index), ch);
        }
    }
    assert_eq!(nextindex, 2350);
}

// --------------------&<--------------------
// encoder

/// An iterator adapter that packs the byte stream into a stream of unsigned integers < 2,350.
pub struct Packer<Iter> {
    iter: iter::Fuse<Iter>,
    bits: u32, // anything >= 18 bits would work
    nbits: usize,
}

impl<Iter: Iterator<Item=u8>> Packer<Iter> {
    /// Creates a packing adapter.
    pub fn new(iter: Iter) -> Packer<Iter> {
        Packer { iter: iter.fuse(), bits: 0, nbits: 0 }
    }
}

impl<Iter: Iterator<Item=u8>> Iterator for Packer<Iter> {
    type Item = u16;

    fn next(&mut self) -> Option<u16> {
        loop {
            debug_assert!(self.nbits < 10);
            match self.iter.next() {
                Some(b) => {
                    self.bits = (self.bits << 8) | b as u32;
                    self.nbits += 8;
                    if self.nbits >= 10 {
                        self.nbits -= 10;
                        let code = (self.bits >> self.nbits) & 0x3ff;
                        return Some(code as u16);
                    }
                }
                None if self.nbits > 0 => {
                    // make an 11-bit pattern of 11..1100..00 with nbits+1 zeroes
                    let base = 0x7ff & !((2 << self.nbits) - 1);
                    let code = base | (self.bits & ((1 << self.nbits) - 1));
                    self.bits = 0;
                    self.nbits = 0;
                    return Some(code as u16);
                }
                None => {
                    return None;
                }
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        // nchars = ceil(nbits / 10)
        let (lo, hi) = self.iter.size_hint();
        ((lo * 8 + 9) / 10, hi.map(|hi| (hi * 8 + 9) / 10))
    }
}

#[cfg(test)]
fn pack(input: &[u8]) -> Vec<u16> {
    Packer::new(input.iter().map(|&v| v)).collect()
}

#[cfg(test)]
#[test]
fn test_pack() {
    assert_eq!(pack(&[]), vec![]);
    assert_eq!(pack(&[0b_____00000000]),
               vec!  [0b1_10_00000000]);
    assert_eq!(pack(&[0b_____11111111]),
               vec!  [0b1_10_11111111]);
    assert_eq!(pack(&[0b00000000, 0b11___________111111]),
               vec!  [0b00000000____11, 0b1_1110_111111]);
    assert_eq!(pack(&[0b11111111, 0b00___________000000]),
               vec!  [0b11111111____00, 0b1_1110_000000]);
    assert_eq!(pack(&[0b00000000, 0b11____111111, 0b0000_____________0000]),
               vec!  [0b00000000____11, 0b111111____0000, 0b1_111110_0000]);
    assert_eq!(pack(&[0b11111111, 0b00____000000, 0b1111_____________1111]),
               vec!  [0b11111111____00, 0b000000____1111, 0b1_111110_1111]);
    assert_eq!(pack(&[0b00000000, 0b11____111111, 0b0000____0000, 0b111111_______________11]),
               vec!  [0b00000000____11, 0b111111____0000, 0b0000____111111, 0b1_11111110_11]);
    assert_eq!(pack(&[0b11111111, 0b00____000000, 0b1111____1111, 0b000000_______________00]),
               vec!  [0b11111111____00, 0b000000____1111, 0b1111____000000, 0b1_11111110_00]);
    assert_eq!(pack(&[0b00000000, 0b11____111111, 0b0000____0000, 0b111111____11, 0b00000000]),
               vec!  [0b00000000____11, 0b111111____0000, 0b0000____111111, 0b11____00000000]);

    // spec examples&
    assert_eq!(pack(&[49, 50, 51, 97, 98]), vec![196, 803, 216, 354]);
    assert_eq!(pack(&[49, 50, 51, 100]), vec![196, 803, 217, 2040]);
    assert_eq!(pack(&[49]), vec![1585]);
}

/// An iterator adapter that encodes the byte stream into the BaseHangul stream.
pub struct Encoder<Iter> {
    packer: Packer<Iter>,
}

impl<Iter: Iterator<Item=u8>> Encoder<Iter> {
    /// Creates an encoding adapter.
    pub fn new(iter: Iter) -> Encoder<Iter> {
        Encoder { packer: Packer::new(iter) }
    }
}

impl<Iter: Iterator<Item=u8>> Iterator for Encoder<Iter> {
    type Item = char;
    fn next(&mut self) -> Option<char> { self.packer.next().map(index_to_char) }
    fn size_hint(&self) -> (usize, Option<usize>) { self.packer.size_hint() }
}

/// Converts a byte sequence into the BaseHangul string.
pub fn encode(input: &[u8]) -> String {
    Encoder::new(input.iter().map(|&v| v)).collect()
}

#[cfg(test)]
#[test]
fn test_encode() {
    // spec examples
    assert_eq!(encode(&[49, 50, 51, 97, 98]), "꺽먹꼍녜".to_string());
    assert_eq!(encode(&[49, 50, 51, 100]), "꺽먹꼐톈".to_string());
    assert_eq!(encode(&[49]), "쟌".to_string());
    assert_eq!(encode(&[]), "".to_string());
}

// --------------------&<--------------------
// decoder

/// An iterator adapter that unpacks the byte stream from a stream of unsigned integers < 2,350.
pub struct Unpacker<Iter> {
    iter: Iter,
    bits: u32,
    nbits: usize,
    last: bool,
}

impl<Iter: Iterator<Item=u16>> Unpacker<Iter> {
    /// Creates an unpacking adapter.
    pub fn new(iter: Iter) -> Unpacker<Iter> {
        Unpacker { iter: iter, bits: 0, nbits: 0, last: false }
    }
}

impl<Iter: Iterator<Item=u16>> Iterator for Unpacker<Iter> {
    type Item = DecodeResult<u8>;

    fn next(&mut self) -> Option<DecodeResult<u8>> {
        loop {
            // emit the buffered byte
            if self.nbits >= 8 {
                self.nbits -= 8;
                let code = (self.bits >> self.nbits) & 0xff;
                return Some(Ok(code as u8));
            }

            // if we have no more than 8 bits, we are almost done
            if self.last {
                if self.nbits > 0 {
                    self.nbits = 0;
                    return Some(Err("non-integral number of bytes".into_cow()));
                } else {
                    return None;
                }
            }

            match self.iter.next() {
                Some(b @ 0...1023) => {
                    self.bits = (self.bits << 10) | b as u32;
                    self.nbits += 10;
                }
                Some(b @ 1024...2045) => {
                    static CL1: [u8; 32] = [0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
                                            1,1,1,1,1,1,1,1, 2,2,2,2,3,3,4,5];
                    let mut nones = CL1[((b >> 5) & 0x1f) as usize] as usize;
                    if nones == 5 { // we may have more ones in the lower half
                        nones += CL1[(b & 0x1f) as usize] as usize;
                    }
                    debug_assert!(nones < 10);
                    let nbits = 9 - nones;

                    self.last = true;
                    self.bits = (self.bits << nbits) | (b as u32 & ((1 << nbits) - 1));
                    self.nbits += nbits;
                }
                Some(_) => {
                    self.last = true;
                    self.nbits = 0;
                    return Some(Err("invalid code word".into_cow()));
                }
                None => {
                    self.last = true;
                }
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        // nchars * 10 - 9 <= nbits <= nchars * 10
        let (lo, hi) = self.iter.size_hint();
        ((lo * 10 - 9 + 7) / 8, hi.map(|hi| (hi * 10 + 7) / 8))
    }
}

#[cfg(test)]
fn unpack(input: &[u16]) -> DecodeResult<Vec<u8>> {
    Unpacker::new(input.iter().map(|&v| v)).collect()
}

#[cfg(test)]
#[test]
fn test_unpack() {
    assert_eq!(unpack(&[]), Ok(vec![]));
    assert_eq!(unpack(&[0b1_10_00000000]),
               Ok(vec! [0b_____00000000]));
    assert_eq!(unpack(&[0b1_10_11111111]),
               Ok(vec! [0b_____11111111]));
    assert_eq!(unpack(&[0b00000000____11, 0b1_1110_111111]),
               Ok(vec! [0b00000000, 0b11___________111111]));
    assert_eq!(unpack(&[0b11111111____00, 0b1_1110_000000]),
               Ok(vec! [0b11111111, 0b00___________000000]));
    assert_eq!(unpack(&[0b00000000____11, 0b111111____0000, 0b1_111110_0000]),
               Ok(vec! [0b00000000, 0b11____111111, 0b0000_____________0000]));
    assert_eq!(unpack(&[0b11111111____00, 0b000000____1111, 0b1_111110_1111]),
               Ok(vec! [0b11111111, 0b00____000000, 0b1111_____________1111]));
    assert_eq!(unpack(&[0b00000000____11, 0b111111____0000, 0b0000____111111, 0b1_11111110_11]),
               Ok(vec! [0b00000000, 0b11____111111, 0b0000____0000, 0b111111_______________11]));
    assert_eq!(unpack(&[0b11111111____00, 0b000000____1111, 0b1111____000000, 0b1_11111110_00]),
               Ok(vec! [0b11111111, 0b00____000000, 0b1111____1111, 0b000000_______________00]));
    assert_eq!(unpack(&[0b00000000____11, 0b111111____0000, 0b0000____111111, 0b11____00000000]),
               Ok(vec! [0b00000000, 0b11____111111, 0b0000____0000, 0b111111____11, 0b00000000]));

    // spec examples
    assert_eq!(unpack(&[196, 803, 216, 354]), Ok(vec![49, 50, 51, 97, 98]));
    assert_eq!(unpack(&[196, 803, 217, 2040]), Ok(vec![49, 50, 51, 100]));
    assert_eq!(unpack(&[1585]), Ok(vec![49]));

    // error: non-integral number of bits
    assert!(unpack(&[0b1_111111110_0]).is_err());
    assert!(unpack(&[0b1_11111110_00]).is_err());
    assert!(unpack(&[0b1_1111110_000]).is_err());
    assert!(unpack(&[0b1_111110_0000]).is_err());
    assert!(unpack(&[0b1_11110_00000]).is_err());
    assert!(unpack(&[0b1_1110_000000]).is_err());
    assert!(unpack(&[0b1_110_0000000]).is_err());
    assert!(unpack(&[0b1_0_000000000]).is_err());
    assert!(unpack(&[1]).is_err());
    assert!(unpack(&[1, 0b1_111111110_0]).is_err());
    assert!(unpack(&[1, 0b1_11111110_00]).is_err());
    assert!(unpack(&[1, 0b1_1111110_000]).is_err());
    assert!(unpack(&[1, 0b1_111110_0000]).is_err());
    assert!(unpack(&[1, 0b1_11110_00000]).is_err());
    assert!(unpack(&[1, 0b1_110_0000000]).is_err());
    assert!(unpack(&[1, 0b1_10_00000000]).is_err());
    assert!(unpack(&[1, 0b1_0_000000000]).is_err());
    assert!(unpack(&[1, 2]).is_err());
    assert!(unpack(&[1, 2, 0b1_111111110_0]).is_err());
    assert!(unpack(&[1, 2, 0b1_11111110_00]).is_err());
    assert!(unpack(&[1, 2, 0b1_1111110_000]).is_err());
    assert!(unpack(&[1, 2, 0b1_11110_00000]).is_err());
    assert!(unpack(&[1, 2, 0b1_1110_000000]).is_err());
    assert!(unpack(&[1, 2, 0b1_110_0000000]).is_err());
    assert!(unpack(&[1, 2, 0b1_10_00000000]).is_err());
    assert!(unpack(&[1, 2, 0b1_0_000000000]).is_err());
    assert!(unpack(&[1, 2, 3]).is_err());
    assert!(unpack(&[1, 2, 3, 0b1_111111110_0]).is_err());
    assert!(unpack(&[1, 2, 3, 0b1_1111110_000]).is_err());
    assert!(unpack(&[1, 2, 3, 0b1_111110_0000]).is_err());
    assert!(unpack(&[1, 2, 3, 0b1_11110_00000]).is_err());
    assert!(unpack(&[1, 2, 3, 0b1_1110_000000]).is_err());
    assert!(unpack(&[1, 2, 3, 0b1_110_0000000]).is_err());
    assert!(unpack(&[1, 2, 3, 0b1_10_00000000]).is_err());
    assert!(unpack(&[1, 2, 3, 0b1_0_000000000]).is_err());

    // error: invalid code word
    assert!(unpack(&[0b1_1111111110]).is_err());
    assert!(unpack(&[0b1_1111111111]).is_err());
    assert!(unpack(&[2048]).is_err());
    assert!(unpack(&[2349]).is_err());
}

/// An iterator adapter that decodes the byte stream from the BaseHangul stream.
pub struct Decoder<Iter: Iterator<Item=char>> {
    unpacker: Unpacker<iter::Map<Iter, fn(char) -> u16>>,
}

impl<Iter: Iterator<Item=char>> Decoder<Iter> {
    /// Creates a decoding adapter.
    pub fn new(iter: Iter) -> Decoder<Iter> {
        fn char_to_index_or_so(c: char) -> u16 {
            char_to_index(c).unwrap_or(NCHARS /*out-of-bounds*/)
        }
        Decoder { unpacker: Unpacker::new(iter.map(char_to_index_or_so)) }
    }
}

impl<Iter: Iterator<Item=char>> Iterator for Decoder<Iter> {
    type Item = DecodeResult<u8>;
    fn next(&mut self) -> Option<DecodeResult<u8>> {
        self.unpacker.next()
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        let (lo, hi) = self.unpacker.size_hint();
        (if lo > 0 {1} else {0}, hi) // error can occur anywhere
    }
}

/// Converts a BaseHangul string into the byte sequence if possible.
pub fn decode(input: &str) -> DecodeResult<Vec<u8>> {
    Decoder::new(input.chars().filter(|c| !c.is_whitespace())).collect()
}

#[cfg(test)]
#[test]
fn test_decode() {
    // spec examples
    assert_eq!(decode("꺽먹꼍녜"), Ok(vec![49, 50, 51, 97, 98]));
    assert_eq!(decode("꺽먹꼐톈"), Ok(vec![49, 50, 51, 100]));
    assert_eq!(decode("쟌"), Ok(vec![49]));
    assert_eq!(decode(""), Ok(vec![]));

    // ignores whitespace
    assert_eq!(decode("   쟌    "), Ok(vec![49]));
    assert_eq!(decode("\t가\n가\u{3000}가가"), Ok(vec![0, 0, 0, 0, 0]));

    // error: invalid
    assert!(decode("X").is_err());
    assert!(decode("뷁").is_err()); // not in KS X 1001
    assert!(decode("힝").is_err()); // out of bounds
}