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,
24 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
26 provider: &ICU4XDataProvider,
27 ) -> Result<Box<ICU4XCanonicalCombiningClassMap>, ICU4XError> {
28 Ok(Box::new(ICU4XCanonicalCombiningClassMap(
30 CanonicalCombiningClassMap::new [r => Ok(r)],
31 CanonicalCombiningClassMap::try_new_with_any_provider,
32 CanonicalCombiningClassMap::try_new_with_buffer_provider,
38 #[diplomat::rust_link(
39 icu::normalizer::properties::CanonicalCombiningClassMap::get,
42 #[diplomat::rust_link(
43 icu::properties::properties::CanonicalCombiningClass,
47 #[diplomat::attr(supports = indexing, indexer)]
48 pub fn get(&self, ch: DiplomatChar) -> u8 {
51 #[diplomat::rust_link(
52 icu::normalizer::properties::CanonicalCombiningClassMap::get32,
55 #[diplomat::rust_link(
56 icu::properties::properties::CanonicalCombiningClass,
60 #[diplomat::attr(dart, disable)]
61 pub fn get32(&self, ch: u32) -> u8 {
66 /// The raw canonical composition operation.
68 /// Callers should generally use ICU4XComposingNormalizer unless they specifically need raw composition operations
70 #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition, Struct)]
71 pub struct ICU4XCanonicalComposition(pub CanonicalComposition);
73 impl ICU4XCanonicalComposition {
74 /// Construct a new ICU4XCanonicalComposition instance for NFC
75 #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition::new, FnInStruct)]
76 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
78 provider: &ICU4XDataProvider,
79 ) -> Result<Box<ICU4XCanonicalComposition>, ICU4XError> {
80 Ok(Box::new(ICU4XCanonicalComposition(call_constructor!(
81 CanonicalComposition::new [r => Ok(r)],
82 CanonicalComposition::try_new_with_any_provider,
83 CanonicalComposition::try_new_with_buffer_provider,
88 /// Performs canonical composition (including Hangul) on a pair of characters
89 /// or returns NUL if these characters don’t compose. Composition exclusions are taken into account.
90 #[diplomat::rust_link(
91 icu::normalizer::properties::CanonicalComposition::compose,
94 pub fn compose(&self, starter: DiplomatChar, second: DiplomatChar) -> DiplomatChar {
95 match (char::from_u32(starter), char::from_u32(second)) {
96 (Some(starter), Some(second)) => self.0.compose(starter, second),
99 .unwrap_or('\0') as DiplomatChar
103 /// The outcome of non-recursive canonical decomposition of a character.
104 /// `second` will be NUL when the decomposition expands to a single character
105 /// (which may or may not be the original one)
106 #[diplomat::rust_link(icu::normalizer::properties::Decomposed, Enum)]
108 pub struct ICU4XDecomposed {
110 second: DiplomatChar,
113 /// The raw (non-recursive) canonical decomposition operation.
115 /// Callers should generally use ICU4XDecomposingNormalizer unless they specifically need raw composition operations
117 #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition, Struct)]
118 pub struct ICU4XCanonicalDecomposition(pub CanonicalDecomposition);
120 impl ICU4XCanonicalDecomposition {
121 /// Construct a new ICU4XCanonicalDecomposition instance for NFC
122 #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition::new, FnInStruct)]
123 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
125 provider: &ICU4XDataProvider,
126 ) -> Result<Box<ICU4XCanonicalDecomposition>, ICU4XError> {
127 Ok(Box::new(ICU4XCanonicalDecomposition(call_constructor!(
128 CanonicalDecomposition::new [r => Ok(r)],
129 CanonicalDecomposition::try_new_with_any_provider,
130 CanonicalDecomposition::try_new_with_buffer_provider,
135 /// Performs non-recursive canonical decomposition (including for Hangul).
136 #[diplomat::rust_link(
137 icu::normalizer::properties::CanonicalDecomposition::decompose,
140 pub fn decompose(&self, c: DiplomatChar) -> ICU4XDecomposed {
141 match char::from_u32(c) {
142 Some(c) => match self.0.decompose(c) {
143 Decomposed::Default => ICU4XDecomposed {
144 first: c as DiplomatChar,
145 second: '\0' as DiplomatChar,
147 Decomposed::Singleton(s) => ICU4XDecomposed {
148 first: s as DiplomatChar,
149 second: '\0' as DiplomatChar,
151 Decomposed::Expansion(first, second) => ICU4XDecomposed {
152 first: first as DiplomatChar,
153 second: second as DiplomatChar,
156 _ => ICU4XDecomposed {
158 second: '\0' as DiplomatChar,