1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
7 use crate::{errors::ffi::ICU4XError, provider::ffi::ICU4XDataProvider};
9 use icu_normalizer::properties::{
10 CanonicalCombiningClassMap, CanonicalComposition, CanonicalDecomposition, Decomposed,
13 /// Lookup of the Canonical_Combining_Class Unicode property
15 #[diplomat::rust_link(icu::normalizer::properties::CanonicalCombiningClassMap, Struct)]
16 pub struct ICU4XCanonicalCombiningClassMap(pub CanonicalCombiningClassMap);
18 impl ICU4XCanonicalCombiningClassMap {
19 /// Construct a new ICU4XCanonicalCombiningClassMap instance for NFC
20 #[diplomat::rust_link(
21 icu::normalizer::properties::CanonicalCombiningClassMap::new,
25 provider: &ICU4XDataProvider,
26 ) -> Result<Box<ICU4XCanonicalCombiningClassMap>, ICU4XError> {
27 Ok(Box::new(ICU4XCanonicalCombiningClassMap(
29 CanonicalCombiningClassMap::new [r => Ok(r)],
30 CanonicalCombiningClassMap::try_new_with_any_provider,
31 CanonicalCombiningClassMap::try_new_with_buffer_provider,
37 #[diplomat::rust_link(
38 icu::normalizer::properties::CanonicalCombiningClassMap::get,
41 #[diplomat::rust_link(
42 icu::properties::properties::CanonicalCombiningClass,
46 pub fn get(&self, ch: char) -> u8 {
49 #[diplomat::rust_link(
50 icu::normalizer::properties::CanonicalCombiningClassMap::get32,
53 #[diplomat::rust_link(
54 icu::properties::properties::CanonicalCombiningClass,
58 pub fn get32(&self, ch: u32) -> u8 {
63 /// The raw canonical composition operation.
65 /// Callers should generally use ICU4XComposingNormalizer unless they specifically need raw composition operations
67 #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition, Struct)]
68 pub struct ICU4XCanonicalComposition(pub CanonicalComposition);
70 impl ICU4XCanonicalComposition {
71 /// Construct a new ICU4XCanonicalComposition instance for NFC
72 #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition::new, FnInStruct)]
74 provider: &ICU4XDataProvider,
75 ) -> Result<Box<ICU4XCanonicalComposition>, ICU4XError> {
76 Ok(Box::new(ICU4XCanonicalComposition(call_constructor!(
77 CanonicalComposition::new [r => Ok(r)],
78 CanonicalComposition::try_new_with_any_provider,
79 CanonicalComposition::try_new_with_buffer_provider,
84 /// Performs canonical composition (including Hangul) on a pair of characters
85 /// or returns NUL if these characters don’t compose. Composition exclusions are taken into account.
86 #[diplomat::rust_link(
87 icu::normalizer::properties::CanonicalComposition::compose,
90 pub fn compose(&self, starter: char, second: char) -> char {
91 self.0.compose(starter, second).unwrap_or('\0')
95 /// The outcome of non-recursive canonical decomposition of a character.
96 /// `second` will be NUL when the decomposition expands to a single character
97 /// (which may or may not be the original one)
98 #[diplomat::rust_link(icu::normalizer::properties::Decomposed, Enum)]
99 pub struct ICU4XDecomposed {
104 /// The raw (non-recursive) canonical decomposition operation.
106 /// Callers should generally use ICU4XDecomposingNormalizer unless they specifically need raw composition operations
108 #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition, Struct)]
109 pub struct ICU4XCanonicalDecomposition(pub CanonicalDecomposition);
111 impl ICU4XCanonicalDecomposition {
112 /// Construct a new ICU4XCanonicalDecomposition instance for NFC
113 #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition::new, FnInStruct)]
115 provider: &ICU4XDataProvider,
116 ) -> Result<Box<ICU4XCanonicalDecomposition>, ICU4XError> {
117 Ok(Box::new(ICU4XCanonicalDecomposition(call_constructor!(
118 CanonicalDecomposition::new [r => Ok(r)],
119 CanonicalDecomposition::try_new_with_any_provider,
120 CanonicalDecomposition::try_new_with_buffer_provider,
125 /// Performs non-recursive canonical decomposition (including for Hangul).
126 #[diplomat::rust_link(
127 icu::normalizer::properties::CanonicalDecomposition::decompose,
130 pub fn decompose(&self, c: char) -> ICU4XDecomposed {
131 match self.0.decompose(c) {
132 Decomposed::Default => ICU4XDecomposed {
136 Decomposed::Singleton(s) => ICU4XDecomposed {
140 Decomposed::Expansion(first, second) => ICU4XDecomposed { first, second },