| 1 | // This implementation should correspond to the algorithms described in |
| 2 | // Unicode 3.12. |
| 3 | |
| 4 | /// A set of ranges that corresponds to the set of all Hangul syllable |
| 5 | /// codepoints. |
| 6 | /// |
| 7 | /// These ranges are defined in Unicode 4.8 Table 4-13. |
| 8 | pub const RANGE_HANGUL_SYLLABLErust-analyzer cargo ucd-util 0.2.2 hangul/RANGE_HANGUL_SYLLABLE.: &'static [(u32, u32)] = &[(0xAC00, 0xD7A3)]; |
| 9 | |
| 10 | const S_BASErust-analyzer cargo ucd-util 0.2.2 hangul/S_BASE.: u32 = 0xAC00; |
| 11 | const L_BASErust-analyzer cargo ucd-util 0.2.2 hangul/L_BASE.: u32 = 0x1100; |
| 12 | const V_BASErust-analyzer cargo ucd-util 0.2.2 hangul/V_BASE.: u32 = 0x1161; |
| 13 | const T_BASErust-analyzer cargo ucd-util 0.2.2 hangul/T_BASE.: u32 = 0x11A7; |
| 14 | const T_COUNTrust-analyzer cargo ucd-util 0.2.2 hangul/T_COUNT.: u32 = 28; |
| 15 | const N_COUNTrust-analyzer cargo ucd-util 0.2.2 hangul/N_COUNT.: u32 = 588; |
| 16 | |
| 17 | /// Return the character name of the given precomposed Hangul codepoint. |
| 18 | /// |
| 19 | /// If the given codepoint does not correspond to a precomposed Hangul |
| 20 | /// codepoint in the inclusive range `AC00..D7A3`, then this returns `None`. |
| 21 | /// |
| 22 | /// This implements the algorithms described in Unicode 3.12 and Unicode 4.8. |
| 23 | /// |
| 24 | /// The `table` given should be a map from codepoint to the corresponding |
| 25 | /// Jamo short name for that codepoint. If you're using `ucd-generate`, then |
| 26 | /// the table can be generated via the `jamo-short-name` sub-command. |
| 27 | pub fn hangul_namerust-analyzer cargo ucd-util 0.2.2 hangul/hangul_name().<'a>( |
| 28 | table: &'a [(u32, &'a str)], |
| 29 | cp: u32, |
| 30 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 31 | let mut name = "HANGUL SYLLABLE ".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 32 | let (lpart, vpart, tpart) = match hangul_full_canonical_decompositionrust-analyzer cargo ucd-util 0.2.2 hangul/hangul_full_canonical_decomposition().(cp) { |
| 33 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 34 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(triple) => triple, |
| 35 | }; |
| 36 | |
| 37 | name.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(jamo_short_namerust-analyzer cargo ucd-util 0.2.2 hangul/jamo_short_name().(table, lpart)); |
| 38 | name.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(jamo_short_namerust-analyzer cargo ucd-util 0.2.2 hangul/jamo_short_name().(table, vpart)); |
| 39 | name.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(tpart.map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().("", |cp| jamo_short_namerust-analyzer cargo ucd-util 0.2.2 hangul/jamo_short_name().(table, cp))); |
| 40 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name) |
| 41 | } |
| 42 | |
| 43 | /// Return the full canonical decomposition of the given precomposed Hangul |
| 44 | /// codepoint. |
| 45 | /// |
| 46 | /// If the decomposition does not have any trailing consonant, then the third |
| 47 | /// part of the tuple returned is `None`. |
| 48 | /// |
| 49 | /// If the given codepoint does not correspond to a precomposed Hangul |
| 50 | /// codepoint in the inclusive range `AC00..D7A3`, then this returns `None`. |
| 51 | /// |
| 52 | /// This implements the algorithms described in Unicode 3.12 and Unicode 4.8. |
| 53 | pub fn hangul_full_canonical_decompositionrust-analyzer cargo ucd-util 0.2.2 hangul/hangul_full_canonical_decomposition().( |
| 54 | cp: u32, |
| 55 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(u32, u32, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u32>)> { |
| 56 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().(0xAC00 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]le().cp && cp rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]le().0xD7A3) { |
| 57 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 58 | } |
| 59 | |
| 60 | let s_index = cp rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Sub<Self>`]sub().S_BASErust-analyzer cargo ucd-util 0.2.2 hangul/S_BASE.; |
| 61 | let l_index = s_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Div<Self>`]div().N_COUNTrust-analyzer cargo ucd-util 0.2.2 hangul/N_COUNT.; |
| 62 | let v_index = (s_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Rem<Self>`]rem().N_COUNTrust-analyzer cargo ucd-util 0.2.2 hangul/N_COUNT.) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Div<Self>`]div().T_COUNTrust-analyzer cargo ucd-util 0.2.2 hangul/T_COUNT.; |
| 63 | let t_index = s_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Rem<Self>`]rem().T_COUNTrust-analyzer cargo ucd-util 0.2.2 hangul/T_COUNT.; |
| 64 | |
| 65 | let l_part = L_BASErust-analyzer cargo ucd-util 0.2.2 hangul/L_BASE. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().l_index; |
| 66 | let v_part = V_BASErust-analyzer cargo ucd-util 0.2.2 hangul/V_BASE. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().v_index; |
| 67 | let t_part = if t_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().0 { Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# } else { Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(T_BASErust-analyzer cargo ucd-util 0.2.2 hangul/T_BASE. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().t_index) }; |
| 68 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((l_part, v_part, t_part)) |
| 69 | } |
| 70 | |
| 71 | type JamoShortNamerust-analyzer cargo ucd-util 0.2.2 hangul/JamoShortName#<'a> = &'a [(u32, &'a str)]; |
| 72 | |
| 73 | fn jamo_short_namerust-analyzer cargo ucd-util 0.2.2 hangul/jamo_short_name().<'a>(table: JamoShortNamerust-analyzer cargo ucd-util 0.2.2 hangul/JamoShortName#<'a>, cp: u32) -> &'a str { |
| 74 | let i = table.binary_search_by_keyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]binary_search_by_key().(&cp, |p| p.0).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 75 | table[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..1 |
| 76 | } |
| 77 | |
| 78 | #[cfg(test)] |
| 79 | mod testsrust-analyzer cargo ucd-util 0.2.2 hangul/tests/ { |
| 80 | use craterust-analyzer cargo ucd-util 0.2.2 crate/::unicode_tablesrust-analyzer cargo ucd-util 0.2.2 unicode_tables/::jamo_short_namerust-analyzer cargo ucd-util 0.2.2 unicode_tables/jamo_short_name/::JAMO_SHORT_NAMErust-analyzer cargo ucd-util 0.2.2 unicode_tables/jamo_short_name/JAMO_SHORT_NAME. as TABLErust-analyzer cargo ucd-util 0.2.2 unicode_tables/jamo_short_name/JAMO_SHORT_NAME.; |
| 81 | |
| 82 | use superrust-analyzer cargo ucd-util 0.2.2 hangul/::{hangul_full_canonical_decompositionrust-analyzer cargo ucd-util 0.2.2 hangul/hangul_full_canonical_decomposition()., hangul_namerust-analyzer cargo ucd-util 0.2.2 hangul/hangul_name().}; |
| 83 | |
| 84 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 85 | fn canon_decomprust-analyzer cargo ucd-util 0.2.2 hangul/tests/canon_decomp().() { |
| 86 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 87 | hangul_full_canonical_decompositionrust-analyzer cargo ucd-util 0.2.2 hangul/hangul_full_canonical_decomposition().(0xD4DB), |
| 88 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((0x1111, 0x1171, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0x11B6))) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 93 | fn namerust-analyzer cargo ucd-util 0.2.2 hangul/tests/name().() { |
| 94 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 95 | hangul_namerust-analyzer cargo ucd-util 0.2.2 hangul/hangul_name().(TABLErust-analyzer cargo ucd-util 0.2.2 unicode_tables/jamo_short_name/JAMO_SHORT_NAME., 0xD4DB).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), |
| 96 | "HANGUL SYLLABLE PWILH" |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 101 | fn allrust-analyzer cargo ucd-util 0.2.2 hangul/tests/all().() { |
| 102 | for cp in 0xAC00..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#(0xD7A3 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().1) { |
| 103 | hangul_namerust-analyzer cargo ucd-util 0.2.2 hangul/hangul_name().(TABLErust-analyzer cargo ucd-util 0.2.2 unicode_tables/jamo_short_name/JAMO_SHORT_NAME., cp).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 108 | fn invalidrust-analyzer cargo ucd-util 0.2.2 hangul/tests/invalid().() { |
| 109 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(hangul_namerust-analyzer cargo ucd-util 0.2.2 hangul/hangul_name().(TABLErust-analyzer cargo ucd-util 0.2.2 unicode_tables/jamo_short_name/JAMO_SHORT_NAME., 0).is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().()); |
| 110 | } |
| 111 | } |