Manish Goregaokar | 361854c | 2025-03-05 15:02:28 -0800 | [diff] [blame] | 1 | // Copyright (C) 2025 The Android Open Source Project |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | #![allow(missing_docs)] // Not particularly useful to document these thin wrappers |
| 5 | |
| 6 | //! This is a thin wrapper around ICU4X for use in Bionic |
| 7 | |
| 8 | use icu_casemap::CaseMapper; |
| 9 | use icu_collections::codepointtrie::TrieValue; |
| 10 | use icu_properties::props::*; |
| 11 | use icu_properties::{CodePointMapData, CodePointSetData}; |
| 12 | |
| 13 | #[no_mangle] |
| 14 | pub extern "C" fn __icu4x_bionic_general_category(ch: u32) -> u8 { |
| 15 | CodePointMapData::<GeneralCategory>::new().get32(ch) as u8 |
| 16 | } |
| 17 | |
| 18 | #[no_mangle] |
| 19 | pub extern "C" fn __icu4x_bionic_east_asian_width(ch: u32) -> u8 { |
| 20 | CodePointMapData::<EastAsianWidth>::new().get32(ch).to_u32() as u8 |
| 21 | } |
| 22 | |
| 23 | #[no_mangle] |
| 24 | pub extern "C" fn __icu4x_bionic_hangul_syllable_type(ch: u32) -> u8 { |
| 25 | CodePointMapData::<HangulSyllableType>::new().get32(ch).to_u32() as u8 |
| 26 | } |
| 27 | |
| 28 | #[no_mangle] |
| 29 | pub extern "C" fn __icu4x_bionic_is_alphabetic(ch: u32) -> bool { |
| 30 | CodePointSetData::new::<Alphabetic>().contains32(ch) |
| 31 | } |
| 32 | |
| 33 | #[no_mangle] |
| 34 | pub extern "C" fn __icu4x_bionic_is_default_ignorable_code_point(ch: u32) -> bool { |
| 35 | CodePointSetData::new::<DefaultIgnorableCodePoint>().contains32(ch) |
| 36 | } |
| 37 | |
| 38 | #[no_mangle] |
| 39 | pub extern "C" fn __icu4x_bionic_is_lowercase(ch: u32) -> bool { |
| 40 | CodePointSetData::new::<Lowercase>().contains32(ch) |
| 41 | } |
| 42 | |
| 43 | #[no_mangle] |
| 44 | pub extern "C" fn __icu4x_bionic_is_alnum(ch: u32) -> bool { |
| 45 | CodePointSetData::new::<Alnum>().contains32(ch) |
| 46 | } |
| 47 | |
| 48 | #[no_mangle] |
| 49 | pub extern "C" fn __icu4x_bionic_is_blank(ch: u32) -> bool { |
| 50 | CodePointSetData::new::<Blank>().contains32(ch) |
| 51 | } |
| 52 | |
| 53 | #[no_mangle] |
| 54 | pub extern "C" fn __icu4x_bionic_is_graph(ch: u32) -> bool { |
| 55 | CodePointSetData::new::<Graph>().contains32(ch) |
| 56 | } |
| 57 | |
| 58 | #[no_mangle] |
| 59 | pub extern "C" fn __icu4x_bionic_is_print(ch: u32) -> bool { |
| 60 | CodePointSetData::new::<Print>().contains32(ch) |
| 61 | } |
| 62 | |
| 63 | #[no_mangle] |
| 64 | pub extern "C" fn __icu4x_bionic_is_xdigit(ch: u32) -> bool { |
| 65 | CodePointSetData::new::<Xdigit>().contains32(ch) |
| 66 | } |
| 67 | |
| 68 | #[no_mangle] |
| 69 | pub extern "C" fn __icu4x_bionic_is_white_space(ch: u32) -> bool { |
| 70 | CodePointSetData::new::<WhiteSpace>().contains32(ch) |
| 71 | } |
| 72 | |
| 73 | #[no_mangle] |
| 74 | pub extern "C" fn __icu4x_bionic_is_uppercase(ch: u32) -> bool { |
| 75 | CodePointSetData::new::<Uppercase>().contains32(ch) |
| 76 | } |
| 77 | |
| 78 | /// Convert a code point to uppercase |
| 79 | #[no_mangle] |
| 80 | pub extern "C" fn __icu4x_bionic_to_upper(ch: u32) -> u32 { |
| 81 | let Ok(ch) = char::try_from(ch) else { |
| 82 | return ch; |
| 83 | }; |
| 84 | let cm = CaseMapper::new(); |
| 85 | |
| 86 | cm.simple_uppercase(ch) as u32 |
| 87 | } |
| 88 | |
| 89 | /// Convert a code point to lowercase |
| 90 | #[no_mangle] |
| 91 | pub extern "C" fn __icu4x_bionic_to_lower(ch: u32) -> u32 { |
| 92 | let Ok(ch) = char::try_from(ch) else { |
| 93 | return ch; |
| 94 | }; |
| 95 | let cm = CaseMapper::new(); |
| 96 | |
| 97 | cm.simple_lowercase(ch) as u32 |
| 98 | } |