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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// 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 ).

//! Data and APIs for supporting Script_Extensions property
//! values in an efficient structure.

use crate::props::Script;
use crate::provider::*;

use core::iter::FromIterator;
use core::ops::RangeInclusive;
use icu_collections::codepointinvlist::CodePointInversionList;
use icu_provider::prelude::*;
use zerovec::{ule::AsULE, ZeroSlice};

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

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

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

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

impl AsULE for ScriptWithExt {
    type ULE = <u16 as AsULE>::ULE;

    #[inline]
    fn to_unaligned(self) -> Self::ULE {
        Script(self.0).to_unaligned()
    }

    #[inline]
    fn from_unaligned(unaligned: Self::ULE) -> Self {
        ScriptWithExt(Script::from_unaligned(unaligned).0)
    }
}

#[doc(hidden)] // `ScriptWithExt` not intended as public-facing but for `ScriptWithExtensionsPropertyV1` constructor
impl ScriptWithExt {
    /// Returns whether the [`ScriptWithExt`] value has Script_Extensions and
    /// also indicates a Script value of [`Script::Common`].
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::properties::script::ScriptWithExt;
    ///
    /// assert!(ScriptWithExt(0x04FF).is_common());
    /// assert!(ScriptWithExt(0x0400).is_common());
    ///
    /// assert!(!ScriptWithExt(0x08FF).is_common());
    /// assert!(!ScriptWithExt(0x0800).is_common());
    ///
    /// assert!(!ScriptWithExt(0x0CFF).is_common());
    /// assert!(!ScriptWithExt(0x0C00).is_common());
    ///
    /// assert!(!ScriptWithExt(0xFF).is_common());
    /// assert!(!ScriptWithExt(0x0).is_common());
    /// ```
    pub fn is_common(&self) -> bool {
        self.0 >> SCRIPT_VAL_LENGTH == 1
    }

    /// Returns whether the [`ScriptWithExt`] value has Script_Extensions and
    /// also indicates a Script value of [`Script::Inherited`].
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::properties::script::ScriptWithExt;
    ///
    /// assert!(!ScriptWithExt(0x04FF).is_inherited());
    /// assert!(!ScriptWithExt(0x0400).is_inherited());
    ///
    /// assert!(ScriptWithExt(0x08FF).is_inherited());
    /// assert!(ScriptWithExt(0x0800).is_inherited());
    ///
    /// assert!(!ScriptWithExt(0x0CFF).is_inherited());
    /// assert!(!ScriptWithExt(0x0C00).is_inherited());
    ///
    /// assert!(!ScriptWithExt(0xFF).is_inherited());
    /// assert!(!ScriptWithExt(0x0).is_inherited());
    /// ```
    pub fn is_inherited(&self) -> bool {
        self.0 >> SCRIPT_VAL_LENGTH == 2
    }

    /// Returns whether the [`ScriptWithExt`] value has Script_Extensions and
    /// also indicates that the Script value is neither [`Script::Common`] nor
    /// [`Script::Inherited`].
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::properties::script::ScriptWithExt;
    ///
    /// assert!(!ScriptWithExt(0x04FF).is_other());
    /// assert!(!ScriptWithExt(0x0400).is_other());
    ///
    /// assert!(!ScriptWithExt(0x08FF).is_other());
    /// assert!(!ScriptWithExt(0x0800).is_other());
    ///
    /// assert!(ScriptWithExt(0x0CFF).is_other());
    /// assert!(ScriptWithExt(0x0C00).is_other());
    ///
    /// assert!(!ScriptWithExt(0xFF).is_other());
    /// assert!(!ScriptWithExt(0x0).is_other());
    /// ```
    pub fn is_other(&self) -> bool {
        self.0 >> SCRIPT_VAL_LENGTH == 3
    }

    /// Returns whether the [`ScriptWithExt`] value has Script_Extensions.
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::properties::script::ScriptWithExt;
    ///
    /// assert!(ScriptWithExt(0x04FF).has_extensions());
    /// assert!(ScriptWithExt(0x0400).has_extensions());
    ///
    /// assert!(ScriptWithExt(0x08FF).has_extensions());
    /// assert!(ScriptWithExt(0x0800).has_extensions());
    ///
    /// assert!(ScriptWithExt(0x0CFF).has_extensions());
    /// assert!(ScriptWithExt(0x0C00).has_extensions());
    ///
    /// assert!(!ScriptWithExt(0xFF).has_extensions());
    /// assert!(!ScriptWithExt(0x0).has_extensions());
    /// ```
    pub fn has_extensions(&self) -> bool {
        let high_order_bits = self.0 >> SCRIPT_VAL_LENGTH;
        high_order_bits > 0
    }
}

impl From<ScriptWithExt> for u32 {
    fn from(swe: ScriptWithExt) -> Self {
        swe.0 as u32
    }
}

impl From<ScriptWithExt> for Script {
    fn from(swe: ScriptWithExt) -> Self {
        Script(swe.0)
    }
}

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

impl<'a> ScriptExtensionsSet<'a> {
    /// Returns whether this set contains the given script.
    ///
    /// # Example
    ///
    /// ```
    /// use icu::properties::script::ScriptWithExtensions;
    /// use icu::properties::props::Script;
    /// let swe = ScriptWithExtensions::new();
    ///
    /// assert!(swe
    ///     .get_script_extensions_val('\u{11303}') // GRANTHA SIGN VISARGA
    ///     .contains(&Script::Grantha));
    /// ```
    pub fn contains(&self, x: &Script) -> bool {
        ZeroSlice::binary_search(self.values, x).is_ok()
    }

    /// Gets an iterator over the elements.
    ///
    /// # Example
    ///
    /// ```
    /// use icu::properties::script::ScriptWithExtensions;
    /// use icu::properties::props::Script;
    /// let swe = ScriptWithExtensions::new();
    ///
    /// assert_eq!(
    ///     swe.get_script_extensions_val('௫') // U+0BEB TAMIL DIGIT FIVE
    ///         .iter()
    ///         .collect::<Vec<_>>(),
    ///     [Script::Tamil, Script::Grantha]
    /// );
    /// ```
    pub fn iter(&self) -> impl DoubleEndedIterator<Item = Script> + 'a {
        ZeroSlice::iter(self.values)
    }

    /// For accessing this set as an array instead of an iterator
    #[doc(hidden)] // used by FFI code
    pub fn array_len(&self) -> usize {
        self.values.len()
    }
    /// For accessing this set as an array instead of an iterator
    #[doc(hidden)] // used by FFI code
    pub fn array_get(&self, index: usize) -> Option<Script> {
        self.values.get(index)
    }
}

/// A struct that represents the data for the Script and Script_Extensions properties.
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
///
/// [📚 Help choosing a constructor](icu_provider::constructors)
///
/// Most useful methods are on [`ScriptWithExtensionsBorrowed`] obtained by calling [`ScriptWithExtensions::as_borrowed()`]
///
/// # Examples
///
/// ```
/// use icu::properties::script::ScriptWithExtensions;
/// use icu::properties::props::Script;
/// let swe = ScriptWithExtensions::new();
///
/// // get the `Script` property value
/// assert_eq!(swe.get_script_val('ـ'), Script::Common); // U+0640 ARABIC TATWEEL
/// assert_eq!(swe.get_script_val('\u{0650}'), Script::Inherited); // U+0650 ARABIC KASRA
/// assert_eq!(swe.get_script_val('٠'), Script::Arabic); // // U+0660 ARABIC-INDIC DIGIT ZERO
/// assert_eq!(swe.get_script_val('ﷲ'), Script::Arabic); // U+FDF2 ARABIC LIGATURE ALLAH ISOLATED FORM
///
/// // get the `Script_Extensions` property value
/// assert_eq!(
///     swe.get_script_extensions_val('ـ') // U+0640 ARABIC TATWEEL
///         .iter().collect::<Vec<_>>(),
///     [Script::Arabic, Script::Syriac, Script::Mandaic, Script::Manichaean,
///          Script::PsalterPahlavi, Script::Adlam, Script::HanifiRohingya, Script::Sogdian,
///          Script::OldUyghur]
/// );
/// assert_eq!(
///     swe.get_script_extensions_val('🥳') // U+1F973 FACE WITH PARTY HORN AND PARTY HAT
///         .iter().collect::<Vec<_>>(),
///     [Script::Common]
/// );
/// assert_eq!(
///     swe.get_script_extensions_val('\u{200D}') // ZERO WIDTH JOINER
///         .iter().collect::<Vec<_>>(),
///     [Script::Inherited]
/// );
/// assert_eq!(
///     swe.get_script_extensions_val('௫') // U+0BEB TAMIL DIGIT FIVE
///         .iter().collect::<Vec<_>>(),
///     [Script::Tamil, Script::Grantha]
/// );
///
/// // check containment of a `Script` value in the `Script_Extensions` value
/// // U+0650 ARABIC KASRA
/// assert!(!swe.has_script('\u{0650}', Script::Inherited)); // main Script value
/// assert!(swe.has_script('\u{0650}', Script::Arabic));
/// assert!(swe.has_script('\u{0650}', Script::Syriac));
/// assert!(!swe.has_script('\u{0650}', Script::Thaana));
///
/// // get a `CodePointInversionList` for when `Script` value is contained in `Script_Extensions` value
/// let syriac = swe.get_script_extensions_set(Script::Syriac);
/// assert!(syriac.contains('\u{0650}')); // ARABIC KASRA
/// assert!(!syriac.contains('٠')); // ARABIC-INDIC DIGIT ZERO
/// assert!(!syriac.contains('ﷲ')); // ARABIC LIGATURE ALLAH ISOLATED FORM
/// assert!(syriac.contains('܀')); // SYRIAC END OF PARAGRAPH
/// assert!(syriac.contains('\u{074A}')); // SYRIAC BARREKH
/// ```
#[derive(Debug)]
pub struct ScriptWithExtensions {
    data: DataPayload<ScriptWithExtensionsPropertyV1Marker>,
}

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

impl ScriptWithExtensions {
    /// Creates a new instance of `ScriptWithExtensionsBorrowed` using compiled data.
    ///
    /// ✨ *Enabled with the `compiled_data` Cargo feature.*
    ///
    /// [📚 Help choosing a constructor](icu_provider::constructors)
    #[cfg(feature = "compiled_data")]
    #[allow(clippy::new_ret_no_self)]
    pub fn new() -> ScriptWithExtensionsBorrowed<'static> {
        ScriptWithExtensionsBorrowed::new()
    }

    icu_provider::gen_any_buffer_data_constructors!(
        () -> result: Result<ScriptWithExtensions, DataError>,
        functions: [
            new: skip,
            try_new_with_any_provider,
            try_new_with_buffer_provider,
            try_new_unstable,
            Self,
        ]
    );

    #[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::new)]
    pub fn try_new_unstable(
        provider: &(impl DataProvider<ScriptWithExtensionsPropertyV1Marker> + ?Sized),
    ) -> Result<Self, DataError> {
        Ok(ScriptWithExtensions::from_data(
            provider.load(Default::default())?.payload,
        ))
    }

    /// Construct a borrowed version of this type that can be queried.
    ///
    /// This avoids a potential small underlying cost per API call (ex: `contains()`) by consolidating it
    /// up front.
    #[inline]
    pub fn as_borrowed(&self) -> ScriptWithExtensionsBorrowed<'_> {
        ScriptWithExtensionsBorrowed {
            data: self.data.get(),
        }
    }

    /// Construct a new one from loaded data
    ///
    /// Typically it is preferable to use getters like [`load_script_with_extensions_unstable()`] instead
    pub(crate) fn from_data(data: DataPayload<ScriptWithExtensionsPropertyV1Marker>) -> Self {
        Self { data }
    }
}

impl<'a> ScriptWithExtensionsBorrowed<'a> {
    /// Returns the `Script` property value for this code point.
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::properties::script::ScriptWithExtensions;
    /// use icu::properties::props::Script;
    ///
    /// let swe = ScriptWithExtensions::new();
    ///
    /// // U+0640 ARABIC TATWEEL
    /// assert_eq!(swe.get_script_val('ـ'), Script::Common); // main Script value
    /// assert_ne!(swe.get_script_val('ـ'), Script::Arabic);
    /// assert_ne!(swe.get_script_val('ـ'), Script::Syriac);
    /// assert_ne!(swe.get_script_val('ـ'), Script::Thaana);
    ///
    /// // U+0650 ARABIC KASRA
    /// assert_eq!(swe.get_script_val('\u{0650}'), Script::Inherited); // main Script value
    /// assert_ne!(swe.get_script_val('\u{0650}'), Script::Arabic);
    /// assert_ne!(swe.get_script_val('\u{0650}'), Script::Syriac);
    /// assert_ne!(swe.get_script_val('\u{0650}'), Script::Thaana);
    ///
    /// // U+0660 ARABIC-INDIC DIGIT ZERO
    /// assert_ne!(swe.get_script_val('٠'), Script::Common);
    /// assert_eq!(swe.get_script_val('٠'), Script::Arabic); // main Script value
    /// assert_ne!(swe.get_script_val('٠'), Script::Syriac);
    /// assert_ne!(swe.get_script_val('٠'), Script::Thaana);
    ///
    /// // U+FDF2 ARABIC LIGATURE ALLAH ISOLATED FORM
    /// assert_ne!(swe.get_script_val('ﷲ'), Script::Common);
    /// assert_eq!(swe.get_script_val('ﷲ'), Script::Arabic); // main Script value
    /// assert_ne!(swe.get_script_val('ﷲ'), Script::Syriac);
    /// assert_ne!(swe.get_script_val('ﷲ'), Script::Thaana);
    /// ```
    pub fn get_script_val(self, ch: char) -> Script {
        self.get_script_val32(ch as u32)
    }

    /// See [`Self::get_script_val`].
    pub fn get_script_val32(self, code_point: u32) -> Script {
        let sc_with_ext = self.data.trie.get32(code_point);

        if sc_with_ext.is_other() {
            let ext_idx = sc_with_ext.0 & SCRIPT_X_SCRIPT_VAL;
            let scx_val = self.data.extensions.get(ext_idx as usize);
            let scx_first_sc = scx_val.and_then(|scx| scx.get(0));

            let default_sc_val = Script::Unknown;

            scx_first_sc.unwrap_or(default_sc_val)
        } else if sc_with_ext.is_common() {
            Script::Common
        } else if sc_with_ext.is_inherited() {
            Script::Inherited
        } else {
            let script_val = sc_with_ext.0;
            Script(script_val)
        }
    }
    // Returns the Script_Extensions value for a code_point when the trie value
    // is already known.
    // This private helper method exists to prevent code duplication in callers like
    // `get_script_extensions_val`, `get_script_extensions_set`, and `has_script`.
    fn get_scx_val_using_trie_val(
        self,
        sc_with_ext_ule: &'a <ScriptWithExt as AsULE>::ULE,
    ) -> &'a ZeroSlice<Script> {
        let sc_with_ext = ScriptWithExt::from_unaligned(*sc_with_ext_ule);
        if sc_with_ext.is_other() {
            let ext_idx = sc_with_ext.0 & SCRIPT_X_SCRIPT_VAL;
            let ext_subarray = self.data.extensions.get(ext_idx as usize);
            // In the OTHER case, where the 2 higher-order bits of the
            // `ScriptWithExt` value in the trie doesn't indicate the Script value,
            // the Script value is copied/inserted into the first position of the
            // `extensions` array. So we must remove it to return the actual scx array val.
            let scx_slice = ext_subarray
                .and_then(|zslice| zslice.as_ule_slice().get(1..))
                .unwrap_or_default();
            ZeroSlice::from_ule_slice(scx_slice)
        } else if sc_with_ext.is_common() || sc_with_ext.is_inherited() {
            let ext_idx = sc_with_ext.0 & SCRIPT_X_SCRIPT_VAL;
            let scx_val = self.data.extensions.get(ext_idx as usize);
            scx_val.unwrap_or_default()
        } else {
            // Note: `Script` and `ScriptWithExt` are both represented as the same
            // u16 value when the `ScriptWithExt` has no higher-order bits set.
            let script_ule_slice = core::slice::from_ref(sc_with_ext_ule);
            ZeroSlice::from_ule_slice(script_ule_slice)
        }
    }
    /// Return the `Script_Extensions` property value for this code point.
    ///
    /// If `code_point` has Script_Extensions, then return the Script codes in
    /// the Script_Extensions. In this case, the Script property value
    /// (normally Common or Inherited) is not included in the [`ScriptExtensionsSet`].
    ///
    /// If c does not have Script_Extensions, then the one Script code is put
    /// into the [`ScriptExtensionsSet`] and also returned.
    ///
    /// If c is not a valid code point, then return an empty [`ScriptExtensionsSet`].
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::properties::script::ScriptWithExtensions;
    /// use icu::properties::props::Script;
    ///
    /// let swe = ScriptWithExtensions::new();
    ///
    /// assert_eq!(
    ///     swe.get_script_extensions_val('𐓐') // U+104D0 OSAGE CAPITAL LETTER KHA
    ///         .iter()
    ///         .collect::<Vec<_>>(),
    ///     [Script::Osage]
    /// );
    /// assert_eq!(
    ///     swe.get_script_extensions_val('🥳') // U+1F973 FACE WITH PARTY HORN AND PARTY HAT
    ///         .iter()
    ///         .collect::<Vec<_>>(),
    ///     [Script::Common]
    /// );
    /// assert_eq!(
    ///     swe.get_script_extensions_val('\u{200D}') // ZERO WIDTH JOINER
    ///         .iter()
    ///         .collect::<Vec<_>>(),
    ///     [Script::Inherited]
    /// );
    /// assert_eq!(
    ///     swe.get_script_extensions_val('௫') // U+0BEB TAMIL DIGIT FIVE
    ///         .iter()
    ///         .collect::<Vec<_>>(),
    ///     [Script::Tamil, Script::Grantha]
    /// );
    /// ```
    pub fn get_script_extensions_val(self, ch: char) -> ScriptExtensionsSet<'a> {
        self.get_script_extensions_val32(ch as u32)
    }

    /// See [`Self::get_script_extensions_val`].
    pub fn get_script_extensions_val32(self, code_point: u32) -> ScriptExtensionsSet<'a> {
        let sc_with_ext_ule = self.data.trie.get32_ule(code_point);

        ScriptExtensionsSet {
            values: match sc_with_ext_ule {
                Some(ule_ref) => self.get_scx_val_using_trie_val(ule_ref),
                None => ZeroSlice::from_ule_slice(&[]),
            },
        }
    }

    /// Returns whether `script` is contained in the Script_Extensions
    /// property value if the code_point has Script_Extensions, otherwise
    /// if the code point does not have Script_Extensions then returns
    /// whether the Script property value matches.
    ///
    /// Some characters are commonly used in multiple scripts. For more information,
    /// see UAX #24: <http://www.unicode.org/reports/tr24/>.
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::properties::script::ScriptWithExtensions;
    /// use icu::properties::props::Script;
    ///
    /// let swe = ScriptWithExtensions::new();
    ///
    /// // U+0650 ARABIC KASRA
    /// assert!(!swe.has_script('\u{0650}', Script::Inherited)); // main Script value
    /// assert!(swe.has_script('\u{0650}', Script::Arabic));
    /// assert!(swe.has_script('\u{0650}', Script::Syriac));
    /// assert!(!swe.has_script('\u{0650}', Script::Thaana));
    ///
    /// // U+0660 ARABIC-INDIC DIGIT ZERO
    /// assert!(!swe.has_script('٠', Script::Common)); // main Script value
    /// assert!(swe.has_script('٠', Script::Arabic));
    /// assert!(!swe.has_script('٠', Script::Syriac));
    /// assert!(swe.has_script('٠', Script::Thaana));
    ///
    /// // U+FDF2 ARABIC LIGATURE ALLAH ISOLATED FORM
    /// assert!(!swe.has_script('ﷲ', Script::Common));
    /// assert!(swe.has_script('ﷲ', Script::Arabic)); // main Script value
    /// assert!(!swe.has_script('ﷲ', Script::Syriac));
    /// assert!(swe.has_script('ﷲ', Script::Thaana));
    /// ```
    pub fn has_script(self, ch: char, script: Script) -> bool {
        self.has_script32(ch as u32, script)
    }

    /// See [`Self::has_script`].
    pub fn has_script32(self, code_point: u32, script: Script) -> bool {
        let sc_with_ext_ule = if let Some(scwe_ule) = self.data.trie.get32_ule(code_point) {
            scwe_ule
        } else {
            return false;
        };
        let sc_with_ext = <ScriptWithExt as AsULE>::from_unaligned(*sc_with_ext_ule);

        if !sc_with_ext.has_extensions() {
            let script_val = sc_with_ext.0;
            script == Script(script_val)
        } else {
            let scx_val = self.get_scx_val_using_trie_val(sc_with_ext_ule);
            let script_find = scx_val.iter().find(|&sc| sc == script);
            script_find.is_some()
        }
    }

    /// Returns all of the matching `CodePointMapRange`s for the given [`Script`]
    /// in which `has_script` will return true for all of the contained code points.
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::properties::script::ScriptWithExtensions;
    /// use icu::properties::props::Script;
    ///
    /// let swe = ScriptWithExtensions::new();
    ///
    /// let syriac_script_extensions_ranges = swe.get_script_extensions_ranges(Script::Syriac);
    ///
    /// let exp_ranges = [
    ///     0x060C..=0x060C, // ARABIC COMMA
    ///     0x061B..=0x061C, // ARABIC SEMICOLON, ARABIC LETTER MARK
    ///     0x061F..=0x061F, // ARABIC QUESTION MARK
    ///     0x0640..=0x0640, // ARABIC TATWEEL
    ///     0x064B..=0x0655, // ARABIC FATHATAN..ARABIC HAMZA BELOW
    ///     0x0670..=0x0670, // ARABIC LETTER SUPERSCRIPT ALEF
    ///     0x0700..=0x070D, // Syriac block begins at U+0700
    ///     0x070F..=0x074A, // Syriac block
    ///     0x074D..=0x074F, // Syriac block ends at U+074F
    ///     0x0860..=0x086A, // Syriac Supplement block is U+0860..=U+086F
    ///     0x1DF8..=0x1DF8, // U+1DF8 COMBINING DOT ABOVE LEFT
    ///     0x1DFA..=0x1DFA, // U+1DFA COMBINING DOT BELOW LEFT
    /// ];
    ///
    /// assert_eq!(syriac_script_extensions_ranges.collect::<Vec<_>>(), exp_ranges);
    /// ```
    pub fn get_script_extensions_ranges(
        self,
        script: Script,
    ) -> impl Iterator<Item = RangeInclusive<u32>> + 'a {
        self.data
            .trie
            .iter_ranges_mapped(move |value| {
                let sc_with_ext = ScriptWithExt(value.0);
                if sc_with_ext.has_extensions() {
                    self.get_scx_val_using_trie_val(&sc_with_ext.to_unaligned())
                        .iter()
                        .any(|sc| sc == script)
                } else {
                    script == sc_with_ext.into()
                }
            })
            .filter(|v| v.value)
            .map(|v| v.range)
    }

    /// Returns a [`CodePointInversionList`] for the given [`Script`] which represents all
    /// code points for which `has_script` will return true.
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::properties::script::ScriptWithExtensions;
    /// use icu::properties::props::Script;
    ///
    /// let swe = ScriptWithExtensions::new();
    ///
    /// let syriac = swe.get_script_extensions_set(Script::Syriac);
    ///
    /// assert!(!syriac.contains('؞')); // ARABIC TRIPLE DOT PUNCTUATION MARK
    /// assert!(syriac.contains('؟')); // ARABIC QUESTION MARK
    /// assert!(!syriac.contains('ؠ')); // ARABIC LETTER KASHMIRI YEH
    ///
    /// assert!(syriac.contains('܀')); // SYRIAC END OF PARAGRAPH
    /// assert!(syriac.contains('\u{074A}')); // SYRIAC BARREKH
    /// assert!(!syriac.contains('\u{074B}')); // unassigned
    /// assert!(syriac.contains('ݏ')); // SYRIAC LETTER SOGDIAN FE
    /// assert!(!syriac.contains('ݐ')); // ARABIC LETTER BEH WITH THREE DOTS HORIZONTALLY BELOW
    ///
    /// assert!(syriac.contains('\u{1DF8}')); // COMBINING DOT ABOVE LEFT
    /// assert!(!syriac.contains('\u{1DF9}')); // COMBINING WIDE INVERTED BRIDGE BELOW
    /// assert!(syriac.contains('\u{1DFA}')); // COMBINING DOT BELOW LEFT
    /// assert!(!syriac.contains('\u{1DFB}')); // COMBINING DELETION MARK
    /// ```
    pub fn get_script_extensions_set(self, script: Script) -> CodePointInversionList<'a> {
        CodePointInversionList::from_iter(self.get_script_extensions_ranges(script))
    }
}

#[cfg(feature = "compiled_data")]
impl Default for ScriptWithExtensionsBorrowed<'static> {
    fn default() -> Self {
        Self::new()
    }
}

impl ScriptWithExtensionsBorrowed<'static> {
    /// Creates a new instance of `ScriptWithExtensionsBorrowed` using compiled data.
    ///
    /// ✨ *Enabled with the `compiled_data` Cargo feature.*
    ///
    /// [📚 Help choosing a constructor](icu_provider::constructors)
    #[cfg(feature = "compiled_data")]
    pub fn new() -> Self {
        Self {
            data: crate::provider::Baked::SINGLETON_SCRIPT_WITH_EXTENSIONS_PROPERTY_V1_MARKER,
        }
    }

    /// Cheaply converts a [`ScriptWithExtensionsBorrowed<'static>`] into a [`ScriptWithExtensions`].
    ///
    /// Note: Due to branching and indirection, using [`ScriptWithExtensions`] might inhibit some
    /// compile-time optimizations that are possible with [`ScriptWithExtensionsBorrowed`].
    pub const fn static_to_owned(self) -> ScriptWithExtensions {
        ScriptWithExtensions {
            data: DataPayload::from_static_ref(self.data),
        }
    }
}