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
//! Tables for conversion
use std::collections::HashMap;
use std::vec::Vec;

/// ASCII(full-width)
pub(crate) const FULL_ASCII: [&str; 85] =
    ["!", """, "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-",
     ".", "/", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D",
     "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
     "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\", "]", "^",
     "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
     "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
     "y", "z", "{", "|", "}", "~", " "];
/// ASCII(half-width)
pub(crate) const HALF_ASCII: [&str; 85] =
    ["!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<",
     "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
     "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`",
     "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
     "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", " "];

/// Digits(full-width)
pub(crate) const FULL_DIGIT: [&str; 10] = ["0", "1", "2", "3", "4", "5", "6", "7",
                                           "8", "9"];
/// Digits(half-width)
pub(crate) const HALF_DIGIT: [&str; 10] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];

/// Hiragana(only full-width)
pub(crate) const HIRAGANA: [&str; 94] =
    ["ぁ", "あ", "ぃ", "い", "ぅ", "う", "ぇ", "え", "ぉ", "お", "か", "が", "き",
     "ぎ", "く", "ぐ", "け", "げ", "こ", "ご", "さ", "ざ", "し", "じ", "す", "ず",
     "せ", "ぜ", "そ", "ぞ", "た", "だ", "ち", "ぢ", "っ", "つ", "づ", "て", "で",
     "と", "ど", "な", "に", "ぬ", "ね", "の", "は", "ば", "ぱ", "ひ", "び", "ぴ",
     "ふ", "ぶ", "ぷ", "へ", "べ", "ぺ", "ほ", "ぼ", "ぽ", "ま", "み", "む", "め",
     "も", "ゃ", "や", "ゅ", "ゆ", "ょ", "よ", "ら", "り", "る", "れ", "ろ", "わ",
     "を", "ん", "ー", "ゎ", "ゐ", "ゑ", "ゕ", "ゖ", "ゔ", "ゝ", "ゞ", "・", "「",
     "」", "。", "、"];

/// Katakana(full-width)
pub(crate) const FULL_KANA: [&str; 94] =
    ["ァ", "ア", "ィ", "イ", "ゥ", "ウ", "ェ", "エ", "ォ", "オ", "カ", "ガ", "キ",
     "ギ", "ク", "グ", "ケ", "ゲ", "コ", "ゴ", "サ", "ザ", "シ", "ジ", "ス", "ズ",
     "セ", "ゼ", "ソ", "ゾ", "タ", "ダ", "チ", "ヂ", "ッ", "ツ", "ヅ", "テ", "デ",
     "ト", "ド", "ナ", "ニ", "ヌ", "ネ", "ノ", "ハ", "バ", "パ", "ヒ", "ビ", "ピ",
     "フ", "ブ", "プ", "ヘ", "ベ", "ペ", "ホ", "ボ", "ポ", "マ", "ミ", "ム", "メ",
     "モ", "ャ", "ヤ", "ュ", "ユ", "ョ", "ヨ", "ラ", "リ", "ル", "レ", "ロ", "ワ",
     "ヲ", "ン", "ー", "ヮ", "ヰ", "ヱ", "ヵ", "ヶ", "ヴ", "ヽ", "ヾ", "・", "「",
     "」", "。", "、"];
/// Katakana(half-width)
pub(crate) const HALF_KANA: [&str; 94] =
    ["ァ", "ア", "ィ", "イ", "ゥ", "ウ", "ェ", "エ", "ォ", "オ", "カ", "ガ", "キ",
     "ギ", "ク", "グ", "ケ", "ゲ", "コ", "ゴ", "サ", "ザ", "シ",
     "ジ", "ス", "ズ", "セ", "ゼ", "ソ", "ゾ", "タ", "ダ", "チ",
     "ヂ", "ッ", "ツ", "ヅ", "テ", "デ", "ト", "ド", "ナ", "ニ", "ヌ",
     "ネ", "ノ", "ハ", "バ", "パ", "ヒ", "ビ", "ピ", "フ", "ブ",
     "プ", "ヘ", "ベ", "ペ", "ホ", "ボ", "ポ", "マ", "ミ", "ム", "メ",
     "モ", "ャ", "ヤ", "ュ", "ユ", "ョ", "ヨ", "ラ", "リ", "ル", "レ", "ロ", "ワ",
     "ヲ", "ン", "ー", "ヮ", "ヰ", "ヱ", "ヵ", "ヶ", "ヴ", "ヽ", "ヾ", "・", "「",
     "」", "。", "、"];

/// Katakana(full-width, no voiced consonant marks)
pub(crate) const FULL_KANA_SEION: [&str; 68] =
    ["ァ", "ア", "ィ", "イ", "ゥ", "ウ", "ェ", "エ", "ォ", "オ", "カ", "キ", "ク",
     "ケ", "コ", "サ", "シ", "ス", "セ", "ソ", "タ", "チ", "ッ", "ツ", "テ", "ト",
     "ナ", "ニ", "ヌ", "ネ", "ノ", "ハ", "ヒ", "フ", "ヘ", "ホ", "マ", "ミ", "ム",
     "メ", "モ", "ャ", "ヤ", "ュ", "ユ", "ョ", "ヨ", "ラ", "リ", "ル", "レ", "ロ",
     "ワ", "ヲ", "ン", "ー", "ヮ", "ヰ", "ヱ", "ヵ", "ヶ", "ヽ", "ヾ", "・", "「",
     "」", "。", "、"];
/// Katakana(half-width, no voiced consonant marks)
pub(crate) const HALF_KANA_SEION: [&str; 68] =
    ["ァ", "ア", "ィ", "イ", "ゥ", "ウ", "ェ", "エ", "ォ", "オ", "カ", "キ", "ク",
     "ケ", "コ", "サ", "シ", "ス", "セ", "ソ", "タ", "チ", "ッ", "ツ", "テ", "ト",
     "ナ", "ニ", "ヌ", "ネ", "ノ", "ハ", "ヒ", "フ", "ヘ", "ホ", "マ", "ミ", "ム",
     "メ", "モ", "ャ", "ヤ", "ュ", "ユ", "ョ", "ヨ", "ラ", "リ", "ル", "レ", "ロ",
     "ワ", "ヲ", "ン", "ー", "ヮ", "ヰ", "ヱ", "ヵ", "ヶ", "ヽ", "ヾ", "・", "「",
     "」", "。", "、"];

/// A table that convert from katakana(half-width) to katakana(full-width)
/// with voiced consonant marks
pub(crate) const MAP_KANA: [(&str, &str); 26] = [("ガ", "ガ"),
                                                 ("ギ", "ギ"),
                                                 ("グ", "グ"),
                                                 ("ゲ", "ゲ"),
                                                 ("ゴ", "ゴ"),
                                                 ("ザ", "ザ"),
                                                 ("ジ", "ジ"),
                                                 ("ズ", "ズ"),
                                                 ("ゼ", "ゼ"),
                                                 ("ゾ", "ゾ"),
                                                 ("ダ", "ダ"),
                                                 ("ヂ", "ヂ"),
                                                 ("ヅ", "ヅ"),
                                                 ("デ", "デ"),
                                                 ("ド", "ド"),
                                                 ("バ", "バ"),
                                                 ("ビ", "ビ"),
                                                 ("ブ", "ブ"),
                                                 ("ベ", "ベ"),
                                                 ("ボ", "ボ"),
                                                 ("パ", "パ"),
                                                 ("ピ", "ピ"),
                                                 ("プ", "プ"),
                                                 ("ペ", "ペ"),
                                                 ("ポ", "ポ"),
                                                 ("ヴ", "ヴ")];

fn to_table(key: Vec<&str>, value: Vec<&str>) -> HashMap<u32, String> {
    assert!(key.len() == value.len());

    let keys = key.join("").chars().map(|c| c as u32).collect::<Vec<u32>>();
    let mut table = HashMap::new();

    for (k, v) in keys.into_iter().zip(value.into_iter()) {
        table.insert(k, v.to_string());
    }

    table
}

/// Convert from full-width to half-width
pub enum FullToHalf {
    /// Ascii, digits and katakana
    All,
    /// Only ascii
    Ascii,
    /// Ascii and digits
    AsciiAndDigits,
    /// Ascii and katakana
    AsciiAndKana,
    /// Only digits
    Digits,
    /// Digits and katakana
    DigitsAndKana,
    /// Only katakana
    Kana,
}

impl FullToHalf {
    /// Make a conversion table
    pub fn to_table(&self) -> HashMap<u32, String> {
        match *self {
            FullToHalf::All => {
                to_table([&FULL_ASCII[..], &FULL_DIGIT[..], &FULL_KANA[..]].concat(),
                         [&HALF_ASCII[..], &HALF_DIGIT[..], &HALF_KANA[..]].concat())
            }
            FullToHalf::Ascii => to_table([&FULL_ASCII[..]].concat(), [&HALF_ASCII[..]].concat()),
            FullToHalf::AsciiAndDigits => {
                to_table([&FULL_ASCII[..], &FULL_DIGIT[..]].concat(),
                         [&HALF_ASCII[..], &HALF_DIGIT[..]].concat())
            }
            FullToHalf::AsciiAndKana => {
                to_table([&FULL_ASCII[..], &FULL_KANA[..]].concat(),
                         [&HALF_ASCII[..], &HALF_KANA[..]].concat())
            }
            FullToHalf::Digits => to_table([&FULL_DIGIT[..]].concat(), [&HALF_DIGIT[..]].concat()),
            FullToHalf::DigitsAndKana => {
                to_table([&FULL_DIGIT[..], &FULL_KANA[..]].concat(),
                         [&HALF_DIGIT[..], &HALF_KANA[..]].concat())
            }
            FullToHalf::Kana => to_table([&FULL_KANA[..]].concat(), [&HALF_KANA[..]].concat()),
        }
    }
}

/// Convert from half-width to full-width
pub enum HalfToFull {
    /// Ascii, digits and katakana
    All,
    /// Only ascii
    Ascii,
    /// Ascii and digits
    AsciiAndDigits,
    /// Ascii and katakana
    AsciiAndKana,
    /// Only digits
    Digits,
    /// Digits and katakana
    DigitsAndKana,
    /// Only katakana
    Kana,
}

impl HalfToFull {
    /// Make a conversion table
    pub fn to_table(&self) -> HashMap<u32, String> {
        match *self {
            HalfToFull::All => {
                to_table([&HALF_ASCII[..], &HALF_DIGIT[..], &HALF_KANA_SEION[..]].concat(),
                         [&FULL_ASCII[..], &FULL_DIGIT[..], &FULL_KANA_SEION[..]].concat())
            }
            HalfToFull::Ascii => to_table([&HALF_ASCII[..]].concat(), [&FULL_ASCII[..]].concat()),
            HalfToFull::AsciiAndDigits => {
                to_table([&HALF_ASCII[..], &HALF_DIGIT[..]].concat(),
                         [&FULL_ASCII[..], &FULL_DIGIT[..]].concat())
            }
            HalfToFull::AsciiAndKana => {
                to_table([&HALF_ASCII[..], &HALF_KANA_SEION[..]].concat(),
                         [&FULL_ASCII[..], &FULL_KANA_SEION[..]].concat())
            }
            HalfToFull::Digits => to_table([&HALF_DIGIT[..]].concat(), [&FULL_DIGIT[..]].concat()),
            HalfToFull::DigitsAndKana => {
                to_table([&HALF_DIGIT[..], &HALF_KANA_SEION[..]].concat(),
                         [&FULL_DIGIT[..], &FULL_KANA_SEION[..]].concat())
            }
            HalfToFull::Kana => {
                to_table([&HALF_KANA_SEION[..]].concat(),
                         [&FULL_KANA_SEION[..]].concat())
            }
        }
    }
}

/// Convert between hiragana and katakana
pub enum HiraKana {
    /// From hiragana to katakana(half-width)
    HiraToHalfKana,
    /// From hiragana to katakana(full-width)
    HiraToKana,
    /// From katakana(full-width) to hiragana
    KanaToHira,
}

impl HiraKana {
    /// Make a conversion table
    pub fn to_table(&self) -> HashMap<u32, String> {
        match *self {
            HiraKana::HiraToHalfKana => {
                to_table([&HIRAGANA[..]].concat(), [&HALF_KANA[..]].concat())
            }
            HiraKana::HiraToKana => to_table([&HIRAGANA[..]].concat(), [&FULL_KANA[..]].concat()),
            HiraKana::KanaToHira => to_table([&FULL_KANA[..]].concat(), [&HIRAGANA[..]].concat()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_full_to_half_all() {
        let table = FullToHalf::All.to_table();
        assert_eq!(189, table.len());
        assert_eq!(table.get(&65313).unwrap(), "A");
        assert_eq!(table.get(&65296).unwrap(), "0");
        assert_eq!(table.get(&12460).unwrap(), "ガ");
    }

    #[test]
    fn test_full_to_half_ascii() {
        let table = FullToHalf::Ascii.to_table();
        assert_eq!(85, table.len());
        assert_eq!(table.get(&65314).unwrap(), "B");
        assert_eq!(table.get(&65297), None);
        assert_eq!(table.get(&12461), None);
    }

    #[test]
    fn test_full_to_half_ascii_and_digits() {
        let table = FullToHalf::AsciiAndDigits.to_table();
        assert_eq!(95, table.len());
        assert_eq!(table.get(&65315).unwrap(), "C");
        assert_eq!(table.get(&65298).unwrap(), "2");
        assert_eq!(table.get(&12462), None);
    }

    #[test]
    fn test_full_to_half_ascii_and_kana() {
        let table = FullToHalf::AsciiAndKana.to_table();
        assert_eq!(179, table.len());
        assert_eq!(table.get(&65316).unwrap(), "D");
        assert_eq!(table.get(&65299), None);
        assert_eq!(table.get(&12463).unwrap(), "ク");
    }

    #[test]
    fn test_full_to_half_digits() {
        let table = FullToHalf::Digits.to_table();
        assert_eq!(10, table.len());
        assert_eq!(table.get(&65317), None);
        assert_eq!(table.get(&65300).unwrap(), "4");
        assert_eq!(table.get(&12464), None)
    }

    #[test]
    fn test_full_to_half_digits_and_kana() {
        let table = FullToHalf::DigitsAndKana.to_table();
        assert_eq!(104, table.len());
        assert_eq!(table.get(&65318), None);
        assert_eq!(table.get(&65301).unwrap(), "5");
        assert_eq!(table.get(&12465).unwrap(), "ケ");
    }

    #[test]
    fn test_full_to_half_kana() {
        let table = FullToHalf::Kana.to_table();
        assert_eq!(94, table.len());
        assert_eq!(table.get(&65319), None);
        assert_eq!(table.get(&65302), None);
        assert_eq!(table.get(&12466).unwrap(), "ゲ");
    }

    #[test]
    fn test_half_to_full_all() {
        let table = HalfToFull::All.to_table();
        assert_eq!(163, table.len());
        assert_eq!(table.get(&97).unwrap(), "a");
        assert_eq!(table.get(&48).unwrap(), "0");
        assert_eq!(table.get(&65393).unwrap(), "ア");
    }

    #[test]
    fn test_half_to_full_ascii() {
        let table = HalfToFull::Ascii.to_table();
        assert_eq!(85, table.len());
        assert_eq!(table.get(&98).unwrap(), "b");
        assert_eq!(table.get(&49), None);
        assert_eq!(table.get(&65394), None);
    }

    #[test]
    fn test_half_to_full_ascii_and_digits() {
        let table = HalfToFull::AsciiAndDigits.to_table();
        assert_eq!(95, table.len());
        assert_eq!(table.get(&99).unwrap(), "c");
        assert_eq!(table.get(&50).unwrap(), "2");
        assert_eq!(table.get(&65395), None);
    }

    #[test]
    fn test_half_to_full_ascii_and_kana() {
        let table = HalfToFull::AsciiAndKana.to_table();
        assert_eq!(153, table.len());
        assert_eq!(table.get(&100).unwrap(), "d");
        assert_eq!(table.get(&51), None);
        assert_eq!(table.get(&65396).unwrap(), "エ");
    }

    #[test]
    fn test_half_to_full_digits() {
        let table = HalfToFull::Digits.to_table();
        assert_eq!(10, table.len());
        assert_eq!(table.get(&101), None);
        assert_eq!(table.get(&52).unwrap(), "4");
        assert_eq!(table.get(&65397), None);
    }

    #[test]
    fn test_half_to_full_digits_and_kana() {
        let table = HalfToFull::DigitsAndKana.to_table();
        assert_eq!(78, table.len());
        assert_eq!(table.get(&102), None);
        assert_eq!(table.get(&53).unwrap(), "5");
        assert_eq!(table.get(&65398).unwrap(), "カ");
    }

    #[test]
    fn test_half_to_full_kana() {
        let table = HalfToFull::Kana.to_table();
        assert_eq!(68, table.len());
        assert_eq!(table.get(&103), None);
        assert_eq!(table.get(&54), None);
        assert_eq!(table.get(&65399).unwrap(), "キ");
    }

    #[test]
    fn test_hiara_kana_hira_to_half_kana() {
        let table = HiraKana::HiraToHalfKana.to_table();
        assert_eq!(94, table.len());
        assert_eq!(table.get(&12354).unwrap(), "ア");
    }

    #[test]
    fn test_hira_kana_hira_to_kana() {
        let table = HiraKana::HiraToKana.to_table();
        assert_eq!(94, table.len());
        assert_eq!(table.get(&12355).unwrap(), "ィ");
    }

    #[test]
    fn test_hira_kana_kana_to_hira() {
        let table = HiraKana::KanaToHira.to_table();
        assert_eq!(94, table.len());
        assert_eq!(table.get(&12531).unwrap(), "ん");
    }
}