tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

normalizer_properties.rs (10101B)


      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 ).
      4 
      5 #[diplomat::bridge]
      6 #[diplomat::abi_rename = "icu4x_{0}_mv1"]
      7 #[diplomat::attr(auto, namespace = "icu4x")]
      8 pub mod ffi {
      9    use alloc::boxed::Box;
     10 
     11    #[cfg(feature = "buffer_provider")]
     12    use crate::unstable::{errors::ffi::DataError, provider::ffi::DataProvider};
     13 
     14    /// Lookup of the Canonical_Combining_Class Unicode property
     15    #[diplomat::opaque]
     16    #[diplomat::rust_link(icu::normalizer::properties::CanonicalCombiningClassMap, Struct)]
     17    #[diplomat::rust_link(
     18        icu::normalizer::properties::CanonicalCombiningClassMapBorrowed,
     19        Struct,
     20        hidden
     21    )]
     22    pub struct CanonicalCombiningClassMap(
     23        pub icu_normalizer::properties::CanonicalCombiningClassMap,
     24    );
     25 
     26    impl CanonicalCombiningClassMap {
     27        /// Construct a new CanonicalCombiningClassMap instance for NFC using compiled data.
     28        #[diplomat::rust_link(
     29            icu::normalizer::properties::CanonicalCombiningClassMap::new,
     30            FnInStruct
     31        )]
     32        #[diplomat::rust_link(
     33            icu::normalizer::properties::CanonicalCombiningClassMapBorrowed::new,
     34            FnInStruct,
     35            hidden
     36        )]
     37        #[diplomat::attr(auto, constructor)]
     38        #[cfg(feature = "compiled_data")]
     39        pub fn create() -> Box<CanonicalCombiningClassMap> {
     40            Box::new(CanonicalCombiningClassMap(
     41                icu_normalizer::properties::CanonicalCombiningClassMap::new().static_to_owned(),
     42            ))
     43        }
     44 
     45        /// Construct a new CanonicalCombiningClassMap instance for NFC using a particular data source.
     46        #[diplomat::rust_link(
     47            icu::normalizer::properties::CanonicalCombiningClassMap::new,
     48            FnInStruct
     49        )]
     50        #[diplomat::rust_link(
     51            icu::normalizer::properties::CanonicalCombiningClassMapBorrowed::new,
     52            FnInStruct,
     53            hidden
     54        )]
     55        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")]
     56        #[cfg(feature = "buffer_provider")]
     57        pub fn create_with_provider(
     58            provider: &DataProvider,
     59        ) -> Result<Box<CanonicalCombiningClassMap>, DataError> {
     60            Ok(Box::new(CanonicalCombiningClassMap(icu_normalizer::properties::CanonicalCombiningClassMap::try_new_with_buffer_provider(provider.get()?)?)))
     61        }
     62 
     63        #[diplomat::rust_link(
     64            icu::normalizer::properties::CanonicalCombiningClassMapBorrowed::get,
     65            FnInStruct
     66        )]
     67        #[diplomat::rust_link(
     68            icu::normalizer::properties::CanonicalCombiningClassMapBorrowed::get32,
     69            FnInStruct,
     70            hidden
     71        )]
     72        #[diplomat::rust_link(
     73            icu::normalizer::properties::CanonicalCombiningClassMapBorrowed::get32_u8,
     74            FnInStruct,
     75            hidden
     76        )]
     77        #[diplomat::rust_link(
     78            icu::normalizer::properties::CanonicalCombiningClassMapBorrowed::get_u8,
     79            FnInStruct,
     80            hidden
     81        )]
     82        #[diplomat::rust_link(icu::properties::props::CanonicalCombiningClass, Struct, compact)]
     83        #[diplomat::attr(auto, indexer)]
     84        pub fn get(&self, ch: DiplomatChar) -> u8 {
     85            self.0.as_borrowed().get32_u8(ch)
     86        }
     87    }
     88 
     89    /// The raw canonical composition operation.
     90    ///
     91    /// Callers should generally use ComposingNormalizer unless they specifically need raw composition operations
     92    #[diplomat::opaque]
     93    #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition, Struct)]
     94    #[diplomat::rust_link(
     95        icu::normalizer::properties::CanonicalCompositionBorrowed,
     96        Struct,
     97        hidden
     98    )]
     99    pub struct CanonicalComposition(pub icu_normalizer::properties::CanonicalComposition);
    100 
    101    impl CanonicalComposition {
    102        /// Construct a new CanonicalComposition instance for NFC using compiled data.
    103        #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition::new, FnInStruct)]
    104        #[diplomat::rust_link(
    105            icu::normalizer::properties::CanonicalCompositionBorrowed::new,
    106            FnInStruct,
    107            hidden
    108        )]
    109        #[diplomat::attr(auto, constructor)]
    110        #[cfg(feature = "compiled_data")]
    111        pub fn create() -> Box<CanonicalComposition> {
    112            Box::new(CanonicalComposition(
    113                icu_normalizer::properties::CanonicalComposition::new().static_to_owned(),
    114            ))
    115        }
    116 
    117        /// Construct a new CanonicalComposition instance for NFC using a particular data source.
    118        #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition::new, FnInStruct)]
    119        #[diplomat::rust_link(
    120            icu::normalizer::properties::CanonicalCompositionBorrowed::new,
    121            FnInStruct,
    122            hidden
    123        )]
    124        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")]
    125        #[cfg(feature = "buffer_provider")]
    126        pub fn create_with_provider(
    127            provider: &DataProvider,
    128        ) -> Result<Box<CanonicalComposition>, DataError> {
    129            Ok(Box::new(CanonicalComposition(
    130                icu_normalizer::properties::CanonicalComposition::try_new_with_buffer_provider(
    131                    provider.get()?,
    132                )?,
    133            )))
    134        }
    135 
    136        /// Performs canonical composition (including Hangul) on a pair of characters
    137        /// or returns NUL if these characters don’t compose. Composition exclusions are taken into account.
    138        #[diplomat::rust_link(
    139            icu::normalizer::properties::CanonicalCompositionBorrowed::compose,
    140            FnInStruct
    141        )]
    142        pub fn compose(&self, starter: DiplomatChar, second: DiplomatChar) -> DiplomatChar {
    143            match (char::from_u32(starter), char::from_u32(second)) {
    144                (Some(starter), Some(second)) => self.0.as_borrowed().compose(starter, second),
    145                _ => None,
    146            }
    147            .unwrap_or('\0') as DiplomatChar
    148        }
    149    }
    150 
    151    /// The outcome of non-recursive canonical decomposition of a character.
    152    /// `second` will be NUL when the decomposition expands to a single character
    153    /// (which may or may not be the original one)
    154    #[diplomat::rust_link(icu::normalizer::properties::Decomposed, Enum)]
    155    #[diplomat::out]
    156    pub struct Decomposed {
    157        first: DiplomatChar,
    158        second: DiplomatChar,
    159    }
    160 
    161    /// The raw (non-recursive) canonical decomposition operation.
    162    ///
    163    /// Callers should generally use DecomposingNormalizer unless they specifically need raw composition operations
    164    #[diplomat::opaque]
    165    #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition, Struct)]
    166    #[diplomat::rust_link(
    167        icu::normalizer::properties::CanonicalDecompositionBorrowed,
    168        Struct,
    169        hidden
    170    )]
    171    pub struct CanonicalDecomposition(pub icu_normalizer::properties::CanonicalDecomposition);
    172 
    173    impl CanonicalDecomposition {
    174        /// Construct a new CanonicalDecomposition instance for NFC using compiled data.
    175        #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition::new, FnInStruct)]
    176        #[diplomat::rust_link(
    177            icu::normalizer::properties::CanonicalDecompositionBorrowed::new,
    178            FnInStruct,
    179            hidden
    180        )]
    181        #[diplomat::attr(auto, constructor)]
    182        #[cfg(feature = "compiled_data")]
    183        pub fn create() -> Box<CanonicalDecomposition> {
    184            Box::new(CanonicalDecomposition(
    185                icu_normalizer::properties::CanonicalDecomposition::new().static_to_owned(),
    186            ))
    187        }
    188 
    189        /// Construct a new CanonicalDecomposition instance for NFC using a particular data source.
    190        #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition::new, FnInStruct)]
    191        #[diplomat::rust_link(
    192            icu::normalizer::properties::CanonicalDecompositionBorrowed::new,
    193            FnInStruct,
    194            hidden
    195        )]
    196        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")]
    197        #[cfg(feature = "buffer_provider")]
    198        pub fn create_with_provider(
    199            provider: &DataProvider,
    200        ) -> Result<Box<CanonicalDecomposition>, DataError> {
    201            Ok(Box::new(CanonicalDecomposition(
    202                icu_normalizer::properties::CanonicalDecomposition::try_new_with_buffer_provider(
    203                    provider.get()?,
    204                )?,
    205            )))
    206        }
    207 
    208        /// Performs non-recursive canonical decomposition (including for Hangul).
    209        #[diplomat::rust_link(
    210            icu::normalizer::properties::CanonicalDecompositionBorrowed::decompose,
    211            FnInStruct
    212        )]
    213        pub fn decompose(&self, c: DiplomatChar) -> Decomposed {
    214            match char::from_u32(c) {
    215                Some(c) => match self.0.as_borrowed().decompose(c) {
    216                    icu_normalizer::properties::Decomposed::Default => Decomposed {
    217                        first: c as DiplomatChar,
    218                        second: '\0' as DiplomatChar,
    219                    },
    220                    icu_normalizer::properties::Decomposed::Singleton(s) => Decomposed {
    221                        first: s as DiplomatChar,
    222                        second: '\0' as DiplomatChar,
    223                    },
    224                    icu_normalizer::properties::Decomposed::Expansion(first, second) => {
    225                        Decomposed {
    226                            first: first as DiplomatChar,
    227                            second: second as DiplomatChar,
    228                        }
    229                    }
    230                },
    231                _ => Decomposed {
    232                    first: c,
    233                    second: '\0' as DiplomatChar,
    234                },
    235            }
    236        }
    237    }
    238 }