icu_provider_source/properties/
bin_cp_set.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::SourceDataProvider;
use icu::collections::codepointinvlist::CodePointInversionListBuilder;
use icu::properties::provider::*;
use icu_provider::prelude::*;
use std::collections::HashSet;

impl SourceDataProvider {
    // get the source data for a Unicode binary property that only defines values for code points
    pub(super) fn get_binary_prop_for_code_point_set<'a>(
        &'a self,
        key: &str,
    ) -> Result<&'a super::uprops_serde::binary::BinaryProperty, DataError> {
        self.icuexport()?
            .read_and_parse_toml::<super::uprops_serde::binary::Main>(&format!(
                "uprops/{}/{}.toml",
                self.trie_type(),
                key
            ))?
            .binary_property
            .first()
            .ok_or_else(|| DataErrorKind::MarkerNotFound.into_error())
    }
}

macro_rules! expand {
    ($(($marker:ident, $prop_name:literal)),+) => {
        $(
            impl DataProvider<$marker> for SourceDataProvider {
                fn load(
                    &self,
                    req: DataRequest,
                ) -> Result<DataResponse<$marker>, DataError> {
                    self.check_req::<$marker>(req)?;
                    let data = self.get_binary_prop_for_code_point_set($prop_name)?;

                    let mut builder = CodePointInversionListBuilder::new();
                    for (start, end) in &data.ranges {
                        builder.add_range32(start..=end);
                    }
                    let inv_list = builder.build();

                    Ok(DataResponse {
                        metadata: Default::default(),
                        payload: DataPayload::from_owned(
                            PropertyCodePointSetV1::InversionList(inv_list),
                        ),
                    })
                }
            }

            impl crate::IterableDataProviderCached<$marker> for SourceDataProvider {
                fn iter_ids_cached(&self) -> Result<HashSet<DataIdentifierCow<'static>>, DataError> {
                    self.get_binary_prop_for_code_point_set($prop_name)?;

                    Ok(HashSet::from_iter([Default::default()]))
                }
            }
        )+
    };
}

expand!(
    (AsciiHexDigitV1Marker, "AHex"),
    (AlnumV1Marker, "alnum"),
    (AlphabeticV1Marker, "Alpha"),
    (BidiControlV1Marker, "Bidi_C"),
    (BidiMirroredV1Marker, "Bidi_M"),
    (BlankV1Marker, "blank"),
    (CasedV1Marker, "Cased"),
    (CaseIgnorableV1Marker, "CI"),
    (FullCompositionExclusionV1Marker, "Comp_Ex"),
    (ChangesWhenCasefoldedV1Marker, "CWCF"),
    (ChangesWhenCasemappedV1Marker, "CWCM"),
    (ChangesWhenNfkcCasefoldedV1Marker, "CWKCF"),
    (ChangesWhenLowercasedV1Marker, "CWL"),
    (ChangesWhenTitlecasedV1Marker, "CWT"),
    (ChangesWhenUppercasedV1Marker, "CWU"),
    (DashV1Marker, "Dash"),
    (DeprecatedV1Marker, "Dep"),
    (DefaultIgnorableCodePointV1Marker, "DI"),
    (DiacriticV1Marker, "Dia"),
    (EmojiModifierBaseV1Marker, "EBase"),
    (EmojiComponentV1Marker, "EComp"),
    (EmojiModifierV1Marker, "EMod"),
    (EmojiV1Marker, "Emoji"),
    (EmojiPresentationV1Marker, "EPres"),
    (ExtenderV1Marker, "Ext"),
    (ExtendedPictographicV1Marker, "ExtPict"),
    (GraphV1Marker, "graph"),
    (GraphemeBaseV1Marker, "Gr_Base"),
    (GraphemeExtendV1Marker, "Gr_Ext"),
    (GraphemeLinkV1Marker, "Gr_Link"),
    (HexDigitV1Marker, "Hex"),
    (HyphenV1Marker, "Hyphen"),
    (IdContinueV1Marker, "IDC"),
    (IdeographicV1Marker, "Ideo"),
    (IdStartV1Marker, "IDS"),
    (IdsBinaryOperatorV1Marker, "IDSB"),
    (IdsTrinaryOperatorV1Marker, "IDST"),
    (JoinControlV1Marker, "Join_C"),
    (LogicalOrderExceptionV1Marker, "LOE"),
    (LowercaseV1Marker, "Lower"),
    (MathV1Marker, "Math"),
    (NoncharacterCodePointV1Marker, "NChar"),
    (NfcInertV1Marker, "nfcinert"),
    (NfdInertV1Marker, "nfdinert"),
    (NfkcInertV1Marker, "nfkcinert"),
    (NfkdInertV1Marker, "nfkdinert"),
    (PatternSyntaxV1Marker, "Pat_Syn"),
    (PatternWhiteSpaceV1Marker, "Pat_WS"),
    (PrependedConcatenationMarkV1Marker, "PCM"),
    (PrintV1Marker, "print"),
    (QuotationMarkV1Marker, "QMark"),
    (RadicalV1Marker, "Radical"),
    (RegionalIndicatorV1Marker, "RI"),
    (SoftDottedV1Marker, "SD"),
    (SegmentStarterV1Marker, "segstart"),
    (CaseSensitiveV1Marker, "Sensitive"),
    (SentenceTerminalV1Marker, "STerm"),
    (TerminalPunctuationV1Marker, "Term"),
    (UnifiedIdeographV1Marker, "UIdeo"),
    (UppercaseV1Marker, "Upper"),
    (VariationSelectorV1Marker, "VS"),
    (WhiteSpaceV1Marker, "WSpace"),
    (XdigitV1Marker, "xdigit"),
    (XidContinueV1Marker, "XIDC"),
    (XidStartV1Marker, "XIDS")
);

#[test]
fn test_basic() {
    use icu::properties::{props::WhiteSpace, CodePointSetData};

    let provider = SourceDataProvider::new_testing();

    let whitespace = CodePointSetData::try_new_unstable::<WhiteSpace>(&provider).unwrap();
    let whitespace = whitespace.as_code_point_inversion_list().unwrap();

    assert!(whitespace.contains(' '));
    assert!(whitespace.contains('\n'));
    assert!(whitespace.contains('\u{3000}')); // U+3000 IDEOGRAPHIC SPACE

    assert!(!whitespace.contains('A'));
}