• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zbraniecki / icu4x / 6815798908

09 Nov 2023 05:17PM UTC coverage: 72.607% (-2.4%) from 75.01%
6815798908

push

github

web-flow
Implement `Any/BufferProvider` for some smart pointers (#4255)

Allows storing them as a `Box<dyn Any/BufferProvider>` without using a
wrapper type that implements the trait.

44281 of 60987 relevant lines covered (72.61%)

201375.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.68
/components/properties/src/script.rs
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
//! Data and APIs for supporting both Script and Script_Extensions property
6
//! values in an efficient structure.
7

8
use crate::error::PropertiesError;
9
use crate::props::Script;
10
use crate::props::ScriptULE;
11
use crate::provider::*;
12

13
use core::iter::FromIterator;
14
use core::ops::RangeInclusive;
15
use icu_collections::codepointinvlist::CodePointInversionList;
16
use icu_provider::prelude::*;
17
use zerovec::{ule::AsULE, ZeroSlice};
18

19
/// The number of bits at the low-end of a `ScriptWithExt` value used for
20
/// storing the `Script` value (or `extensions` index).
21
const SCRIPT_VAL_LENGTH: u16 = 10;
22

23
/// The bit mask necessary to retrieve the `Script` value (or `extensions` index)
24
/// from a `ScriptWithExt` value.
25
const SCRIPT_X_SCRIPT_VAL: u16 = (1 << SCRIPT_VAL_LENGTH) - 1;
26

27
/// An internal-use only pseudo-property that represents the values stored in
28
/// the trie of the special data structure [`ScriptWithExtensionsPropertyV1`].
29
///
30
/// Note: The will assume a 12-bit layout. The 2 higher order bits in positions
31
/// 11..10 will indicate how to deduce the Script value and Script_Extensions,
32
/// and the lower 10 bits 9..0 indicate either the Script value or the index
33
/// into the `extensions` structure.
34
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
682,050✔
35
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
×
36
#[cfg_attr(feature = "datagen", derive(databake::Bake))]
×
37
#[cfg_attr(feature = "datagen", databake(path = icu_properties::script))]
38
#[repr(transparent)]
39
#[doc(hidden)]
40
// `ScriptWithExt` not intended as public-facing but for `ScriptWithExtensionsPropertyV1` constructor
41
#[allow(clippy::exhaustive_structs)] // this type is stable
42
pub struct ScriptWithExt(pub u16);
445,393✔
43

44
#[allow(missing_docs)] // These constants don't need individual documentation.
45
#[allow(non_upper_case_globals)]
46
#[doc(hidden)] // `ScriptWithExt` not intended as public-facing but for `ScriptWithExtensionsPropertyV1` constructor
47
impl ScriptWithExt {
48
    pub const Unknown: ScriptWithExt = ScriptWithExt(0);
49
}
50

51
impl AsULE for ScriptWithExt {
52
    type ULE = ScriptULE;
53

54
    #[inline]
55
    fn to_unaligned(self) -> Self::ULE {
40,498✔
56
        Script(self.0).to_unaligned()
40,498✔
57
    }
40,498✔
58

59
    #[inline]
60
    fn from_unaligned(unaligned: Self::ULE) -> Self {
276,127✔
61
        ScriptWithExt(Script::from_unaligned(unaligned).0)
276,127✔
62
    }
276,127✔
63
}
64

65
#[doc(hidden)] // `ScriptWithExt` not intended as public-facing but for `ScriptWithExtensionsPropertyV1` constructor
66
impl ScriptWithExt {
67
    /// Returns whether the [`ScriptWithExt`] value has Script_Extensions and
68
    /// also indicates a Script value of [`Script::Common`].
69
    ///
70
    /// # Examples
71
    ///
72
    /// ```
73
    /// use icu::properties::script::ScriptWithExt;
74
    ///
75
    /// assert!(ScriptWithExt(0x04FF).is_common());
76
    /// assert!(ScriptWithExt(0x0400).is_common());
77
    ///
78
    /// assert!(!ScriptWithExt(0x08FF).is_common());
79
    /// assert!(!ScriptWithExt(0x0800).is_common());
80
    ///
81
    /// assert!(!ScriptWithExt(0x0CFF).is_common());
82
    /// assert!(!ScriptWithExt(0x0C00).is_common());
83
    ///
84
    /// assert!(!ScriptWithExt(0xFF).is_common());
85
    /// assert!(!ScriptWithExt(0x0).is_common());
86
    /// ```
87
    pub fn is_common(&self) -> bool {
1,588✔
88
        self.0 >> SCRIPT_VAL_LENGTH == 1
1,588✔
89
    }
1,588✔
90

91
    /// Returns whether the [`ScriptWithExt`] value has Script_Extensions and
92
    /// also indicates a Script value of [`Script::Inherited`].
93
    ///
94
    /// # Examples
95
    ///
96
    /// ```
97
    /// use icu::properties::script::ScriptWithExt;
98
    ///
99
    /// assert!(!ScriptWithExt(0x04FF).is_inherited());
100
    /// assert!(!ScriptWithExt(0x0400).is_inherited());
101
    ///
102
    /// assert!(ScriptWithExt(0x08FF).is_inherited());
103
    /// assert!(ScriptWithExt(0x0800).is_inherited());
104
    ///
105
    /// assert!(!ScriptWithExt(0x0CFF).is_inherited());
106
    /// assert!(!ScriptWithExt(0x0C00).is_inherited());
107
    ///
108
    /// assert!(!ScriptWithExt(0xFF).is_inherited());
109
    /// assert!(!ScriptWithExt(0x0).is_inherited());
110
    /// ```
111
    pub fn is_inherited(&self) -> bool {
533✔
112
        self.0 >> SCRIPT_VAL_LENGTH == 2
533✔
113
    }
533✔
114

115
    /// Returns whether the [`ScriptWithExt`] value has Script_Extensions and
116
    /// also indicates that the Script value is neither [`Script::Common`] nor
117
    /// [`Script::Inherited`].
118
    ///
119
    /// # Examples
120
    ///
121
    /// ```
122
    /// use icu::properties::script::ScriptWithExt;
123
    ///
124
    /// assert!(!ScriptWithExt(0x04FF).is_other());
125
    /// assert!(!ScriptWithExt(0x0400).is_other());
126
    ///
127
    /// assert!(!ScriptWithExt(0x08FF).is_other());
128
    /// assert!(!ScriptWithExt(0x0800).is_other());
129
    ///
130
    /// assert!(ScriptWithExt(0x0CFF).is_other());
131
    /// assert!(ScriptWithExt(0x0C00).is_other());
132
    ///
133
    /// assert!(!ScriptWithExt(0xFF).is_other());
134
    /// assert!(!ScriptWithExt(0x0).is_other());
135
    /// ```
136
    pub fn is_other(&self) -> bool {
1,992✔
137
        self.0 >> SCRIPT_VAL_LENGTH == 3
1,992✔
138
    }
1,992✔
139

140
    /// Returns whether the [`ScriptWithExt`] value has Script_Extensions.
141
    ///
142
    /// # Examples
143
    ///
144
    /// ```
145
    /// use icu::properties::script::ScriptWithExt;
146
    ///
147
    /// assert!(ScriptWithExt(0x04FF).has_extensions());
148
    /// assert!(ScriptWithExt(0x0400).has_extensions());
149
    ///
150
    /// assert!(ScriptWithExt(0x08FF).has_extensions());
151
    /// assert!(ScriptWithExt(0x0800).has_extensions());
152
    ///
153
    /// assert!(ScriptWithExt(0x0CFF).has_extensions());
154
    /// assert!(ScriptWithExt(0x0C00).has_extensions());
155
    ///
156
    /// assert!(!ScriptWithExt(0xFF).has_extensions());
157
    /// assert!(!ScriptWithExt(0x0).has_extensions());
158
    /// ```
159
    pub fn has_extensions(&self) -> bool {
26,467✔
160
        let high_order_bits = self.0 >> SCRIPT_VAL_LENGTH;
26,467✔
161
        high_order_bits > 0
26,467✔
162
    }
26,467✔
163
}
164

165
impl From<ScriptWithExt> for u32 {
166
    fn from(swe: ScriptWithExt) -> Self {
×
167
        swe.0 as u32
×
168
    }
×
169
}
170

171
impl From<ScriptWithExt> for Script {
172
    fn from(swe: ScriptWithExt) -> Self {
24,510✔
173
        Script(swe.0)
24,510✔
174
    }
24,510✔
175
}
176

177
/// A struct that wraps a [`Script`] array, such as in the return value for
178
/// [`get_script_extensions_val()`](ScriptWithExtensionsBorrowed::get_script_extensions_val).
179
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
×
180
pub struct ScriptExtensionsSet<'a> {
181
    values: &'a ZeroSlice<Script>,
×
182
}
183

184
impl ScriptExtensionsSet<'_> {
185
    /// Returns whether this set contains the given script.
186
    ///
187
    /// # Example
188
    ///
189
    /// ```
190
    /// use icu::properties::{script, Script};
191
    /// let swe = script::script_with_extensions();
192
    ///
193
    /// assert!(swe
194
    ///     .get_script_extensions_val(0x11303) // GRANTHA SIGN VISARGA
195
    ///     .contains(&Script::Grantha));
196
    /// ```
197
    pub fn contains(&self, x: &Script) -> bool {
3✔
198
        ZeroSlice::binary_search(self.values, x).is_ok()
3✔
199
    }
3✔
200

201
    /// Gets an iterator over the elements.
202
    ///
203
    /// # Example
204
    ///
205
    /// ```
206
    /// use icu::properties::{script, Script};
207
    /// let swe = script::script_with_extensions();
208
    ///
209
    /// assert_eq!(
210
    ///     swe.get_script_extensions_val('௫' as u32) // U+0BEB TAMIL DIGIT FIVE
211
    ///         .iter()
212
    ///         .collect::<Vec<Script>>(),
213
    ///     vec![Script::Tamil, Script::Grantha]
214
    /// );
215
    /// ```
216
    pub fn iter(&self) -> impl DoubleEndedIterator<Item = Script> + '_ {
17✔
217
        ZeroSlice::iter(self.values)
17✔
218
    }
17✔
219

220
    /// For accessing this set as an array instead of an iterator
221
    /// only needed for the FFI bindings; shouldn't be used directly from Rust
222
    #[doc(hidden)]
223
    pub fn array_len(&self) -> usize {
×
224
        self.values.len()
×
225
    }
×
226
    /// For accessing this set as an array instead of an iterator
227
    /// only needed for the FFI bindings; shouldn't be used directly from Rust
228
    #[doc(hidden)]
229
    pub fn array_get(&self, index: usize) -> Option<Script> {
×
230
        self.values.get(index)
×
231
    }
×
232
}
233

234
/// A wrapper around script extensions data. Can be obtained via [`load_script_with_extensions_unstable()`] and
235
/// related getters.
236
///
237
/// Most useful methods are on [`ScriptWithExtensionsBorrowed`] obtained by calling [`ScriptWithExtensions::as_borrowed()`]
238
#[derive(Debug)]
×
239
pub struct ScriptWithExtensions {
240
    data: DataPayload<ScriptWithExtensionsPropertyV1Marker>,
×
241
}
242

243
/// A borrowed wrapper around script extension data, returned by
244
/// [`ScriptWithExtensions::as_borrowed()`]. More efficient to query.
245
#[derive(Clone, Copy, Debug)]
×
246
pub struct ScriptWithExtensionsBorrowed<'a> {
247
    data: &'a ScriptWithExtensionsPropertyV1<'a>,
×
248
}
249

250
impl ScriptWithExtensions {
251
    /// Construct a borrowed version of this type that can be queried.
252
    ///
253
    /// This avoids a potential small underlying cost per API call (ex: `contains()`) by consolidating it
254
    /// up front.
255
    #[inline]
256
    pub fn as_borrowed(&self) -> ScriptWithExtensionsBorrowed<'_> {
8✔
257
        ScriptWithExtensionsBorrowed {
8✔
258
            data: self.data.get(),
8✔
259
        }
260
    }
8✔
261

262
    /// Construct a new one from loaded data
263
    ///
264
    /// Typically it is preferable to use getters like [`load_script_with_extensions_unstable()`] instead
265
    pub fn from_data(data: DataPayload<ScriptWithExtensionsPropertyV1Marker>) -> Self {
8✔
266
        Self { data }
8✔
267
    }
8✔
268
}
269

270
impl<'a> ScriptWithExtensionsBorrowed<'a> {
271
    /// Returns the `Script` property value for this code point.
272
    ///
273
    /// # Examples
274
    ///
275
    /// ```
276
    /// use icu::properties::{script, Script};
277
    ///
278
    /// let swe = script::script_with_extensions();
279
    ///
280
    /// // U+0640 ARABIC TATWEEL
281
    /// assert_eq!(swe.get_script_val(0x0640), Script::Common); // main Script value
282
    /// assert_ne!(swe.get_script_val(0x0640), Script::Arabic);
283
    /// assert_ne!(swe.get_script_val(0x0640), Script::Syriac);
284
    /// assert_ne!(swe.get_script_val(0x0640), Script::Thaana);
285
    ///
286
    /// // U+0650 ARABIC KASRA
287
    /// assert_eq!(swe.get_script_val(0x0650), Script::Inherited); // main Script value
288
    /// assert_ne!(swe.get_script_val(0x0650), Script::Arabic);
289
    /// assert_ne!(swe.get_script_val(0x0650), Script::Syriac);
290
    /// assert_ne!(swe.get_script_val(0x0650), Script::Thaana);
291
    ///
292
    /// // U+0660 ARABIC-INDIC DIGIT ZERO
293
    /// assert_ne!(swe.get_script_val(0x0660), Script::Common);
294
    /// assert_eq!(swe.get_script_val(0x0660), Script::Arabic); // main Script value
295
    /// assert_ne!(swe.get_script_val(0x0660), Script::Syriac);
296
    /// assert_ne!(swe.get_script_val(0x0660), Script::Thaana);
297
    ///
298
    /// // U+FDF2 ARABIC LIGATURE ALLAH ISOLATED FORM
299
    /// assert_ne!(swe.get_script_val(0xFDF2), Script::Common);
300
    /// assert_eq!(swe.get_script_val(0xFDF2), Script::Arabic); // main Script value
301
    /// assert_ne!(swe.get_script_val(0xFDF2), Script::Syriac);
302
    /// assert_ne!(swe.get_script_val(0xFDF2), Script::Thaana);
303
    /// ```
304
    pub fn get_script_val(self, code_point: u32) -> Script {
26✔
305
        let sc_with_ext = self.data.trie.get32(code_point);
26✔
306

307
        if sc_with_ext.is_other() {
26✔
308
            let ext_idx = sc_with_ext.0 & SCRIPT_X_SCRIPT_VAL;
12✔
309
            let scx_val = self.data.extensions.get(ext_idx as usize);
12✔
310
            let scx_first_sc = scx_val.and_then(|scx| scx.get(0));
24✔
311

312
            let default_sc_val = Script::Unknown;
12✔
313

314
            scx_first_sc.unwrap_or(default_sc_val)
12✔
315
        } else if sc_with_ext.is_common() {
14✔
316
            Script::Common
6✔
317
        } else if sc_with_ext.is_inherited() {
8✔
318
            Script::Inherited
5✔
319
        } else {
320
            let script_val = sc_with_ext.0;
3✔
321
            Script(script_val)
3✔
322
        }
323
    }
26✔
324
    // Returns the Script_Extensions value for a code_point when the trie value
325
    // is already known.
326
    // This private helper method exists to prevent code duplication in callers like
327
    // `get_script_extensions_val`, `get_script_extensions_set`, and `has_script`.
328
    fn get_scx_val_using_trie_val(
1,958✔
329
        self,
330
        sc_with_ext_ule: &'a <ScriptWithExt as AsULE>::ULE,
331
    ) -> &'a ZeroSlice<Script> {
332
        let sc_with_ext = ScriptWithExt::from_unaligned(*sc_with_ext_ule);
1,958✔
333
        if sc_with_ext.is_other() {
1,958✔
334
            let ext_idx = sc_with_ext.0 & SCRIPT_X_SCRIPT_VAL;
392✔
335
            let ext_subarray = self.data.extensions.get(ext_idx as usize);
392✔
336
            // In the OTHER case, where the 2 higher-order bits of the
337
            // `ScriptWithExt` value in the trie doesn't indicate the Script value,
338
            // the Script value is copied/inserted into the first position of the
339
            // `extensions` array. So we must remove it to return the actual scx array val.
340
            let scx_slice = ext_subarray
392✔
341
                .and_then(|zslice| zslice.as_ule_slice().get(1..))
392✔
342
                .unwrap_or_default();
343
            ZeroSlice::from_ule_slice(scx_slice)
392✔
344
        } else if sc_with_ext.is_common() || sc_with_ext.is_inherited() {
1,566✔
345
            let ext_idx = sc_with_ext.0 & SCRIPT_X_SCRIPT_VAL;
1,556✔
346
            let scx_val = self.data.extensions.get(ext_idx as usize);
1,556✔
347
            scx_val.unwrap_or_default()
1,556✔
348
        } else {
349
            // Note: `Script` and `ScriptWithExt` are both represented as the same
350
            // u16 value when the `ScriptWithExt` has no higher-order bits set.
351
            let script_ule_slice = core::slice::from_ref(sc_with_ext_ule);
10✔
352
            ZeroSlice::from_ule_slice(script_ule_slice)
10✔
353
        }
354
    }
1,958✔
355
    /// Return the `Script_Extensions` property value for this code point.
356
    ///
357
    /// If `code_point` has Script_Extensions, then return the Script codes in
358
    /// the Script_Extensions. In this case, the Script property value
359
    /// (normally Common or Inherited) is not included in the [`ScriptExtensionsSet`].
360
    ///
361
    /// If c does not have Script_Extensions, then the one Script code is put
362
    /// into the [`ScriptExtensionsSet`] and also returned.
363
    ///
364
    /// If c is not a valid code point, then return an empty [`ScriptExtensionsSet`].
365
    ///
366
    /// # Examples
367
    ///
368
    /// ```
369
    /// use icu::properties::{script, Script};
370
    ///
371
    /// let swe = script::script_with_extensions();
372
    ///
373
    /// assert_eq!(
374
    ///     swe.get_script_extensions_val('𐓐' as u32) // U+104D0 OSAGE CAPITAL LETTER KHA
375
    ///         .iter()
376
    ///         .collect::<Vec<Script>>(),
377
    ///     vec![Script::Osage]
378
    /// );
379
    /// assert_eq!(
380
    ///     swe.get_script_extensions_val('🥳' as u32) // U+1F973 FACE WITH PARTY HORN AND PARTY HAT
381
    ///         .iter()
382
    ///         .collect::<Vec<Script>>(),
383
    ///     vec![Script::Common]
384
    /// );
385
    /// assert_eq!(
386
    ///     swe.get_script_extensions_val(0x200D) // ZERO WIDTH JOINER
387
    ///         .iter()
388
    ///         .collect::<Vec<Script>>(),
389
    ///     vec![Script::Inherited]
390
    /// );
391
    /// assert_eq!(
392
    ///     swe.get_script_extensions_val('௫' as u32) // U+0BEB TAMIL DIGIT FIVE
393
    ///         .iter()
394
    ///         .collect::<Vec<Script>>(),
395
    ///     vec![Script::Tamil, Script::Grantha]
396
    /// );
397
    /// ```
398
    pub fn get_script_extensions_val(self, code_point: u32) -> ScriptExtensionsSet<'a> {
20✔
399
        let sc_with_ext_ule = self.data.trie.get32_ule(code_point);
20✔
400

401
        ScriptExtensionsSet {
20✔
402
            values: match sc_with_ext_ule {
40✔
403
                Some(ule_ref) => self.get_scx_val_using_trie_val(ule_ref),
20✔
404
                None => ZeroSlice::from_ule_slice(&[]),
×
405
            },
406
        }
407
    }
20✔
408

409
    /// Returns whether `script` is contained in the Script_Extensions
410
    /// property value if the code_point has Script_Extensions, otherwise
411
    /// if the code point does not have Script_Extensions then returns
412
    /// whether the Script property value matches.
413
    ///
414
    /// Some characters are commonly used in multiple scripts. For more information,
415
    /// see UAX #24: <http://www.unicode.org/reports/tr24/>.
416
    ///
417
    /// # Examples
418
    ///
419
    /// ```
420
    /// use icu::properties::{script, Script};
421
    ///
422
    /// let swe = script::script_with_extensions();
423
    ///
424
    /// // U+0650 ARABIC KASRA
425
    /// assert!(!swe.has_script(0x0650, Script::Inherited)); // main Script value
426
    /// assert!(swe.has_script(0x0650, Script::Arabic));
427
    /// assert!(swe.has_script(0x0650, Script::Syriac));
428
    /// assert!(!swe.has_script(0x0650, Script::Thaana));
429
    ///
430
    /// // U+0660 ARABIC-INDIC DIGIT ZERO
431
    /// assert!(!swe.has_script(0x0660, Script::Common)); // main Script value
432
    /// assert!(swe.has_script(0x0660, Script::Arabic));
433
    /// assert!(!swe.has_script(0x0660, Script::Syriac));
434
    /// assert!(swe.has_script(0x0660, Script::Thaana));
435
    ///
436
    /// // U+FDF2 ARABIC LIGATURE ALLAH ISOLATED FORM
437
    /// assert!(!swe.has_script(0xFDF2, Script::Common));
438
    /// assert!(swe.has_script(0xFDF2, Script::Arabic)); // main Script value
439
    /// assert!(!swe.has_script(0xFDF2, Script::Syriac));
440
    /// assert!(swe.has_script(0xFDF2, Script::Thaana));
441
    /// ```
442
    pub fn has_script(self, code_point: u32, script: Script) -> bool {
59✔
443
        let sc_with_ext_ule = if let Some(scwe_ule) = self.data.trie.get32_ule(code_point) {
59✔
444
            scwe_ule
59✔
445
        } else {
446
            return false;
×
447
        };
448
        let sc_with_ext = <ScriptWithExt as AsULE>::from_unaligned(*sc_with_ext_ule);
59✔
449

450
        if !sc_with_ext.has_extensions() {
59✔
451
            let script_val = sc_with_ext.0;
11✔
452
            script == Script(script_val)
11✔
453
        } else {
454
            let scx_val = self.get_scx_val_using_trie_val(sc_with_ext_ule);
48✔
455
            let script_find = scx_val.iter().find(|&sc| sc == script);
175✔
456
            script_find.is_some()
48✔
457
        }
458
    }
59✔
459

460
    /// Returns all of the matching `CodePointMapRange`s for the given [`Script`]
461
    /// in which `has_script` will return true for all of the contained code points.
462
    ///
463
    /// # Examples
464
    ///
465
    /// ```
466
    /// use icu::properties::{script, Script};
467
    ///
468
    /// let swe = script::script_with_extensions();
469
    ///
470
    /// let syriac_script_extensions_ranges = swe.get_script_extensions_ranges(Script::Syriac);
471
    ///
472
    /// let exp_ranges = vec![
473
    ///     0x060C..=0x060C, // ARABIC COMMA
474
    ///     0x061B..=0x061C, // ARABIC SEMICOLON, ARABIC LETTER MARK
475
    ///     0x061F..=0x061F, // ARABIC QUESTION MARK
476
    ///     0x0640..=0x0640, // ARABIC TATWEEL
477
    ///     0x064B..=0x0655, // ARABIC FATHATAN..ARABIC HAMZA BELOW
478
    ///     0x0670..=0x0670, // ARABIC LETTER SUPERSCRIPT ALEF
479
    ///     0x0700..=0x070D, // Syriac block begins at U+0700
480
    ///     0x070F..=0x074A, // Syriac block
481
    ///     0x074D..=0x074F, // Syriac block ends at U+074F
482
    ///     0x0860..=0x086A, // Syriac Supplement block is U+0860..=U+086F
483
    ///     0x1DF8..=0x1DF8, // U+1DF8 COMBINING DOT ABOVE LEFT
484
    ///     0x1DFA..=0x1DFA, // U+1DFA COMBINING DOT BELOW LEFT
485
    /// ];
486
    /// let mut exp_ranges_iter = exp_ranges.iter();
487
    ///
488
    /// for act_range in syriac_script_extensions_ranges {
489
    ///     let exp_range = exp_ranges_iter
490
    ///         .next()
491
    ///         .expect("There are too many ranges returned by get_script_extensions_ranges()");
492
    ///     assert_eq!(act_range.start(), exp_range.start());
493
    ///     assert_eq!(act_range.end(), exp_range.end());
494
    /// }
495
    /// assert!(
496
    ///     exp_ranges_iter.next().is_none(),
497
    ///     "There are too few ranges returned by get_script_extensions_ranges()"
498
    /// );
499
    /// ```
500
    pub fn get_script_extensions_ranges(
15✔
501
        self,
502
        script: Script,
503
    ) -> impl Iterator<Item = RangeInclusive<u32>> + 'a {
504
        self.data
30✔
505
            .trie
506
            .iter_ranges_mapped(move |value| {
26,415✔
507
                let sc_with_ext = ScriptWithExt(value.0);
26,400✔
508
                if sc_with_ext.has_extensions() {
26,400✔
509
                    self.get_scx_val_using_trie_val(&sc_with_ext.to_unaligned())
3,780✔
510
                        .iter()
511
                        .any(|sc| sc == script)
7,786✔
512
                } else {
513
                    script == sc_with_ext.into()
24,510✔
514
                }
515
            })
26,400✔
516
            .filter(|v| v.value)
924✔
517
            .map(|v| v.range)
455✔
518
    }
15✔
519

520
    /// Returns a [`CodePointInversionList`] for the given [`Script`] which represents all
521
    /// code points for which `has_script` will return true.
522
    ///
523
    /// # Examples
524
    ///
525
    /// ```
526
    /// use icu::properties::{script, Script};
527
    ///
528
    /// let swe = script::script_with_extensions();
529
    ///
530
    /// let syriac = swe.get_script_extensions_set(Script::Syriac);
531
    ///
532
    /// assert!(!syriac.contains32(0x061E)); // ARABIC TRIPLE DOT PUNCTUATION MARK
533
    /// assert!(syriac.contains32(0x061F)); // ARABIC QUESTION MARK
534
    /// assert!(!syriac.contains32(0x0620)); // ARABIC LETTER KASHMIRI YEH
535
    ///
536
    /// assert!(syriac.contains32(0x0700)); // SYRIAC END OF PARAGRAPH
537
    /// assert!(syriac.contains32(0x074A)); // SYRIAC BARREKH
538
    /// assert!(!syriac.contains32(0x074B)); // unassigned
539
    /// assert!(syriac.contains32(0x074F)); // SYRIAC LETTER SOGDIAN FE
540
    /// assert!(!syriac.contains32(0x0750)); // ARABIC LETTER BEH WITH THREE DOTS HORIZONTALLY BELOW
541
    ///
542
    /// assert!(syriac.contains32(0x1DF8)); // COMBINING DOT ABOVE LEFT
543
    /// assert!(!syriac.contains32(0x1DF9)); // COMBINING WIDE INVERTED BRIDGE BELOW
544
    /// assert!(syriac.contains32(0x1DFA)); // COMBINING DOT BELOW LEFT
545
    /// assert!(!syriac.contains32(0x1DFB)); // COMBINING DELETION MARK
546
    /// ```
547
    pub fn get_script_extensions_set(self, script: Script) -> CodePointInversionList<'a> {
14✔
548
        CodePointInversionList::from_iter(self.get_script_extensions_ranges(script))
14✔
549
    }
14✔
550
}
551

552
impl ScriptWithExtensionsBorrowed<'static> {
553
    /// Cheaply converts a `ScriptWithExtensionsBorrowed<'static>` into a `ScriptWithExtensions`.
554
    pub const fn static_to_owned(self) -> ScriptWithExtensions {
×
555
        ScriptWithExtensions {
×
556
            data: DataPayload::from_static_ref(self.data),
×
557
        }
558
    }
×
559
}
560

561
/// Returns a [`ScriptWithExtensionsBorrowed`] struct that represents the data for the Script
562
/// and Script_Extensions properties.
563
///
564
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
565
///
566
/// [📚 Help choosing a constructor](icu_provider::constructors)
567
///
568
/// # Examples
569
///
570
/// ```
571
/// use icu::properties::{script, Script};
572
/// let swe = script::script_with_extensions();
573
///
574
/// // get the `Script` property value
575
/// assert_eq!(swe.get_script_val(0x0640), Script::Common); // U+0640 ARABIC TATWEEL
576
/// assert_eq!(swe.get_script_val(0x0650), Script::Inherited); // U+0650 ARABIC KASRA
577
/// assert_eq!(swe.get_script_val(0x0660), Script::Arabic); // // U+0660 ARABIC-INDIC DIGIT ZERO
578
/// assert_eq!(swe.get_script_val(0xFDF2), Script::Arabic); // U+FDF2 ARABIC LIGATURE ALLAH ISOLATED FORM
579
///
580
/// // get the `Script_Extensions` property value
581
/// assert_eq!(
582
///     swe.get_script_extensions_val(0x0640) // U+0640 ARABIC TATWEEL
583
///         .iter().collect::<Vec<Script>>(),
584
///     vec![Script::Arabic, Script::Syriac, Script::Mandaic, Script::Manichaean,
585
///          Script::PsalterPahlavi, Script::Adlam, Script::HanifiRohingya, Script::Sogdian,
586
///          Script::OldUyghur]
587
/// );
588
/// assert_eq!(
589
///     swe.get_script_extensions_val('🥳' as u32) // U+1F973 FACE WITH PARTY HORN AND PARTY HAT
590
///         .iter().collect::<Vec<Script>>(),
591
///     vec![Script::Common]
592
/// );
593
/// assert_eq!(
594
///     swe.get_script_extensions_val(0x200D) // ZERO WIDTH JOINER
595
///         .iter().collect::<Vec<Script>>(),
596
///     vec![Script::Inherited]
597
/// );
598
/// assert_eq!(
599
///     swe.get_script_extensions_val('௫' as u32) // U+0BEB TAMIL DIGIT FIVE
600
///         .iter().collect::<Vec<Script>>(),
601
///     vec![Script::Tamil, Script::Grantha]
602
/// );
603
///
604
/// // check containment of a `Script` value in the `Script_Extensions` value
605
/// // U+0650 ARABIC KASRA
606
/// assert!(!swe.has_script(0x0650, Script::Inherited)); // main Script value
607
/// assert!(swe.has_script(0x0650, Script::Arabic));
608
/// assert!(swe.has_script(0x0650, Script::Syriac));
609
/// assert!(!swe.has_script(0x0650, Script::Thaana));
610
///
611
/// // get a `CodePointInversionList` for when `Script` value is contained in `Script_Extensions` value
612
/// let syriac = swe.get_script_extensions_set(Script::Syriac);
613
/// assert!(syriac.contains32(0x0650)); // ARABIC KASRA
614
/// assert!(!syriac.contains32(0x0660)); // ARABIC-INDIC DIGIT ZERO
615
/// assert!(!syriac.contains32(0xFDF2)); // ARABIC LIGATURE ALLAH ISOLATED FORM
616
/// assert!(syriac.contains32(0x0700)); // SYRIAC END OF PARAGRAPH
617
/// assert!(syriac.contains32(0x074A)); // SYRIAC BARREKH
618
/// ```
619
#[cfg(feature = "compiled_data")]
620
pub const fn script_with_extensions() -> ScriptWithExtensionsBorrowed<'static> {
8✔
621
    ScriptWithExtensionsBorrowed {
8✔
622
        data: crate::provider::Baked::SINGLETON_PROPS_SCX_V1,
623
    }
624
}
8✔
625

626
icu_provider::gen_any_buffer_data_constructors!(
627
    locale: skip,
628
    options: skip,
629
    result: Result<ScriptWithExtensions, PropertiesError>,
630
    #[cfg(skip)]
631
    functions: [
632
        script_with_extensions,
633
        load_script_with_extensions_with_any_provider,
634
        load_script_with_extensions_with_buffer_provider,
635
        load_script_with_extensions_unstable,
636
    ]
637
);
638

639
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, script_with_extensions)]
640
pub fn load_script_with_extensions_unstable(
4✔
641
    provider: &(impl DataProvider<ScriptWithExtensionsPropertyV1Marker> + ?Sized),
642
) -> Result<ScriptWithExtensions, PropertiesError> {
643
    Ok(ScriptWithExtensions::from_data(
4✔
644
        provider
4✔
645
            .load(Default::default())
4✔
646
            .and_then(DataResponse::take_payload)?,
×
647
    ))
648
}
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc