icu_collections 2.4.0

Collection of API for use in ICU libraries.
Documentation
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
// 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 ).

#[cfg(feature = "alloc")]
use alloc::string::String;
use yoke::Yokeable;
use zerofrom::ZeroFrom;
use zerovec::{ZeroSlice, ZeroVec};

// Match-node lead unit values, after masking off intermediate-value bits:

// 00..0f: Branch node. If node!=0 then the length is node+1, otherwise
// the length is one more than the next byte.

// For a branch sub-node with at most this many entries, we drop down
// to a linear search.
const MAX_BRANCH_LINEAR_SUB_NODE_LENGTH: usize = 5;

// 0030..003f: Linear-match node, match 1..16 units and continue reading the next node.
const MIN_LINEAR_MATCH: u16 = 0x30;
const MAX_LINEAR_MATCH_LENGTH: u16 = 0x10;

// Match-node lead unit bits 14..6 for the optional intermediate value.
// If these bits are 0, then there is no intermediate value.
// Otherwise, see the *NodeValue* constants below.
const MIN_VALUE_LEAD: u16 = MIN_LINEAR_MATCH + MAX_LINEAR_MATCH_LENGTH; // 0x40
const NODE_TYPE_MASK: u16 = MIN_VALUE_LEAD - 1; // 0x003f

// A final-value node has bit 15 set.
const VALUE_IS_FINAL: u16 = 0x8000;

// Compact value: After testing bit 0, shift right by 15 and then use the following thresholds.
const MAX_ONE_UNIT_VALUE: u16 = 0x3fff;

const MIN_TWO_UNIT_VALUE_LEAD: u16 = MAX_ONE_UNIT_VALUE + 1; // 0x4000

const MAX_ONE_UNIT_NODE_VALUE: u16 = 0xff;

const MIN_TWO_UNIT_NODE_VALUE_LEAD: u16 = MIN_VALUE_LEAD + ((MAX_ONE_UNIT_NODE_VALUE + 1) << 6); // 0x4040

const THREE_UNIT_NODE_VALUE_LEAD: u16 = 0x7fc0;

const THREE_UNIT_VALUE_LEAD: u16 = 0x7fff;

// Compact delta integers.
const MAX_ONE_UNIT_DELTA: u16 = 0xfbff;
const MIN_TWO_UNIT_DELTA_LEAD: u16 = MAX_ONE_UNIT_DELTA + 1; // 0xfc00
const THREE_UNIT_DELTA_LEAD: u16 = 0xffff;

fn skip_value(pos: usize, lead: u16) -> usize {
    if lead < MIN_TWO_UNIT_VALUE_LEAD {
        pos
    } else if lead < THREE_UNIT_VALUE_LEAD {
        pos + 1
    } else {
        pos + 2
    }
}

fn skip_node_value(pos: usize, lead: u16) -> usize {
    if lead < MIN_TWO_UNIT_NODE_VALUE_LEAD {
        pos
    } else if lead < THREE_UNIT_NODE_VALUE_LEAD {
        pos + 1
    } else {
        pos + 2
    }
}

/// This struct represents a de-serialized `Char16Trie` that was exported from
/// ICU binary data.
///
/// Light-weight, non-const reader class for a `CharsTrie`. Traverses a
/// char-serialized data structure with minimal state, for mapping 16-bit-unit
/// sequences to non-negative integer values.
///
/// For more information:
/// - [ICU4C UCharsTrie](https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classicu_1_1UCharsTrie.html)
/// - [ICU4J CharsTrie](https://unicode-org.github.io/icu-docs/apidoc/released/icu4j/com/ibm/icu/util/CharsTrie.html) API.
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "databake", derive(databake::Bake))]
#[cfg_attr(feature = "databake", databake(path = icu_collections::char16trie))]
#[derive(Clone, Debug, PartialEq, Eq, ZeroFrom, Yokeable)]
#[allow(clippy::exhaustive_structs)] // effectively exhaustive, struct-constructible for baking
pub struct Char16Trie<'data> {
    /// An array of u16 containing the trie data.
    #[cfg_attr(feature = "serde", serde(borrow, alias = "trie_data"))]
    #[doc(hidden)] // #2417
    pub data: ZeroVec<'data, u16>,
}

impl<'data> Char16Trie<'data> {
    /// Returns a new [`Char16Trie`] with ownership of the provided data.
    #[inline]
    pub fn new(data: ZeroVec<'data, u16>) -> Self {
        Self { data }
    }

    /// Returns a new [`Char16TrieCursor`] backed by borrowed data from the `trie` data.
    #[inline]
    pub fn cursor(&self) -> Char16TrieCursor<'_> {
        Char16TrieCursor::new(&self.data)
    }

    /// Returns a new [`Char16TrieIterator`] backed by borrowed data from the `trie` data
    #[deprecated(since = "2.4.0", note = "use `Char16Trie::cursor`")]
    #[allow(deprecated)]
    #[inline]
    pub fn iter(&self) -> Char16TrieIterator<'_> {
        Char16TrieIterator::new(&self.data)
    }

    /// Returns an iterator over the key/value pairs in this trie.
    ///
    /// This is called `iter2` because of the deprecated `iter` API, which
    /// is conceptually a lookup cursor, not an iterator.
    ///
    /// ✨ *Enabled with the `alloc` Cargo feature.*
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::collections::char16trie::Char16Trie;
    /// use zerovec::ZeroVec;
    ///
    /// // A Char16Trie containing the ASCII characters 'a' and 'ab'.
    /// let trie_data = [48, 97, 176, 98, 32868];
    /// let trie = Char16Trie::new(ZeroVec::from_slice_or_alloc(&trie_data));
    ///
    /// let mut it = trie.iter2();
    /// assert_eq!(it.next(), Some(("a".into(), 1)));
    /// assert_eq!(it.next(), Some(("ab".into(), 100)));
    /// assert_eq!(it.next(), None);
    /// ```
    #[cfg(feature = "alloc")]
    pub fn iter2(&self) -> impl Iterator<Item = (String, i32)> + '_ {
        let cursor = self.cursor();
        let mut root_value = cursor.value();
        let mut stack = alloc::vec![(cursor, 0)];
        let mut prefix = String::new();
        let mut lead_surrogate = None;
        core::iter::from_fn(move || {
            if let Some(v) = root_value.take() {
                return Some((String::new(), v));
            }
            while let Some((cursor, i)) = stack.last_mut() {
                let mut next_cursor = cursor.clone();
                if let Some(c) = next_cursor.probe(*i) {
                    *i += 1;

                    if let Some(lead) = lead_surrogate.take() {
                        prefix.push(
                            char::decode_utf16([lead, c])
                                .next()
                                .and_then(Result::ok)
                                .unwrap_or(char::REPLACEMENT_CHARACTER),
                        );
                    } else if matches!(c, 0xD800..0xDC00) {
                        lead_surrogate = Some(c);
                    } else {
                        prefix
                            .push(char::from_u32(c as u32).unwrap_or(char::REPLACEMENT_CHARACTER));
                    }

                    let v = next_cursor.value();
                    stack.push((next_cursor, 0));
                    if let Some(v) = v {
                        return Some((prefix.clone(), v));
                    }
                } else {
                    stack.pop();
                    if lead_surrogate.take().is_none()
                        && let Some(ch @ '\u{10000}'..) = prefix.pop()
                    {
                        lead_surrogate = Some(u16_lead(ch as u32));
                    }
                }
            }
            None
        })
    }
}

/// A cursor into a [`Char16Trie`], useful for stepwise lookup.
#[derive(Clone, Debug)]
pub struct Char16TrieCursor<'a> {
    /// A reference to the [`Char16Trie`] data to iterate over.
    trie: &'a ZeroSlice<u16>,
    /// Index of next trie unit to read, or `None` if there are no more matches.
    pos: Option<usize>,
    /// Remaining length of a linear-match node, minus 1, or `None` if not in
    /// such a node.
    remaining_match_length: Option<u8>,
}

/// This struct represents an iterator over a [`Char16Trie`].
#[deprecated(since = "2.4.0", note = "use `Char16TrieCursor`")]
#[derive(Clone, Debug)]
pub struct Char16TrieIterator<'a>(Char16TrieCursor<'a>);

/// An enum representing the return value from a lookup in [`Char16Trie`].
#[deprecated(since = "2.4.0", note = "use `Char16TrieCursor`")]
#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(clippy::exhaustive_enums)]
pub enum TrieResult {
    /// The input unit(s) did not continue a matching string.
    /// Once `next()` returns `TrieResult::NoMatch`, all further calls to `next()`
    /// will also return `TrieResult::NoMatch`.
    NoMatch,
    /// The input unit(s) matched a string but there is no value for the string
    /// so far.  (It is a prefix of a longer string.)
    NoValue,
    /// The input unit(s) continued a matching string and there is a value for
    /// the string so far. No further input byte/unit can continue a matching
    /// string.
    FinalValue(i32),
    /// The input unit(s) continued a matching string and there is a value for
    /// the string so far.  Another input byte/unit can continue a matching
    /// string.
    Intermediate(i32),
}

// Get the lead surrogate (0xd800..0xdbff) for a
// supplementary code point (0x10000..0x10ffff).
// @param supplementary 32-bit code point (U+10000..U+10ffff)
// @return lead surrogate (U+d800..U+dbff) for supplementary
fn u16_lead(supplementary: u32) -> u16 {
    (((supplementary) >> 10) + 0xd7c0) as u16
}

// Get the trail surrogate (0xdc00..0xdfff) for a
// supplementary code point (0x10000..0x10ffff).
// @param supplementary 32-bit code point (U+10000..U+10ffff)
// @return trail surrogate (U+dc00..U+dfff) for supplementary
fn u16_tail(supplementary: u32) -> u16 {
    (((supplementary) & 0x3ff) | 0xdc00) as u16
}

impl<'a> Char16TrieCursor<'a> {
    /// Returns a new [`Char16TrieCursor`] backed by borrowed data for the `trie` array.
    #[inline]
    pub fn new(trie: &'a ZeroSlice<u16>) -> Self {
        Self {
            trie,
            pos: Some(0),
            remaining_match_length: None,
        }
    }

    /// Steps the cursor one `char` into the trie.
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::collections::char16trie::Char16Trie;
    /// use zerovec::ZeroVec;
    ///
    /// // A Char16Trie containing the ASCII characters 'a' and 'ab'.
    /// let trie_data = [48, 97, 176, 98, 32868];
    /// let trie = Char16Trie::new(ZeroVec::from_slice_or_alloc(&trie_data));
    ///
    /// let mut cursor = trie.cursor();
    /// cursor.step('a');
    /// assert_eq!(cursor.value(), Some(1));
    /// assert!(!cursor.is_empty());
    /// cursor.step('b');
    /// assert_eq!(cursor.value(), Some(100));
    /// assert!(cursor.is_empty());
    /// cursor.step('c');
    /// assert_eq!(cursor.value(), None);
    /// assert!(cursor.is_empty());
    /// ```
    #[inline]
    pub fn step(&mut self, c: char) {
        self.step32(c as u32);
    }

    /// Steps the cursor one code point (up to `0x10ffff`) into the trie.
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::collections::char16trie::Char16Trie;
    /// use zerovec::ZeroVec;
    ///
    /// // A Char16Trie containing the ASCII characters 'a' and 'ab'.
    /// let trie_data = [48, 97, 176, 98, 32868];
    /// let trie = Char16Trie::new(ZeroVec::from_slice_or_alloc(&trie_data));
    ///
    /// let mut cursor = trie.cursor();
    /// cursor.step32('a' as u32);
    /// assert_eq!(cursor.value(), Some(1));
    /// assert!(!cursor.is_empty());
    /// cursor.step32('b' as u32);
    /// assert_eq!(cursor.value(), Some(100));
    /// assert!(cursor.is_empty());
    /// cursor.step32('c' as u32);
    /// assert_eq!(cursor.value(), None);
    /// assert!(cursor.is_empty());
    /// ```
    #[inline]
    pub fn step32(&mut self, c: u32) {
        let c = if let Ok(c) = u16::try_from(c) {
            c
        } else {
            self.step16(u16_lead(c));
            u16_tail(c)
        };
        self.step16(c);
    }

    /// Steps the cursor one 16-bit code unit into the trie.
    ///
    /// # Examples
    ///
    /// ```
    /// use icu::collections::char16trie::Char16Trie;
    /// use zerovec::ZeroVec;
    ///
    /// // A Char16Trie containing the ASCII characters 'a' and 'ab'.
    /// let trie_data = [48, 97, 176, 98, 32868];
    /// let trie = Char16Trie::new(ZeroVec::from_slice_or_alloc(&trie_data));
    ///
    /// let mut cursor = trie.cursor();
    /// cursor.step16('a' as u16);
    /// assert_eq!(cursor.value(), Some(1));
    /// assert!(!cursor.is_empty());
    /// cursor.step16('b' as u16);
    /// assert_eq!(cursor.value(), Some(100));
    /// assert!(cursor.is_empty());
    /// cursor.step16('c' as u16);
    /// assert_eq!(cursor.value(), None);
    /// assert!(cursor.is_empty());
    /// ```
    #[inline]
    pub fn step16(&mut self, c: u16) {
        let Some(pos) = self.pos else {
            return;
        };
        self.pos = if let Some(length) = self.remaining_match_length {
            // Remaining part of a linear-match node
            if Some(c) == self.trie.get(pos) {
                self.remaining_match_length = length.checked_sub(1);
                Some(pos + 1)
            } else {
                None
            }
        } else {
            self.next_impl(pos, c)
        };
    }

    #[cfg(feature = "alloc")]
    fn probe(&mut self, mut index: usize) -> Option<u16> {
        let mut pos = self.pos?;
        if let Some(length) = self.remaining_match_length {
            if index > 0 {
                return None;
            }
            let unit = self.trie.get(pos)?;
            self.remaining_match_length = length.checked_sub(1);
            self.pos = Some(pos + 1);
            return Some(unit);
        }
        let mut node = self.trie.get(pos)?;
        pos += 1;
        if node >= MIN_VALUE_LEAD {
            if (node & VALUE_IS_FINAL) != 0 {
                return None;
            }
            // Skip intermediate value.
            pos = skip_node_value(pos, node);
            node &= NODE_TYPE_MASK;
        }
        if node >= MIN_LINEAR_MATCH {
            if index > 0 {
                return None;
            }
            let length = (node - MIN_LINEAR_MATCH) as u8;
            let unit = self.trie.get(pos)?;
            self.remaining_match_length = length.checked_sub(1);
            self.pos = Some(pos + 1);
            return Some(unit);
        }
        let mut length = node as usize;
        if length == 0 {
            length = self.trie.get(pos)? as usize;
            pos += 1;
        }
        length += 1;
        if index >= length {
            return None;
        }

        // The length of the branch is the number of units to select from.
        // The data structure encodes a binary search.
        while length > MAX_BRANCH_LINEAR_SUB_NODE_LENGTH {
            let half = length >> 1;
            if index < half {
                length = half;
                pos = self.jump_by_delta(pos + 1)?;
            } else {
                index -= half;
                length -= half;
                pos = self.skip_delta(pos + 1)?;
            }
        }
        // Drop down to linear search for the last few units.
        while index > 0 {
            index -= 1;
            length -= 1;
            pos = self.skip_value(pos + 1)?;
        }
        let unit = self.trie.get(pos)?;
        pos += 1;
        if length > 1 {
            let node = self.trie.get(pos)?;
            if node & VALUE_IS_FINAL == 0 {
                // Use the non-final value as the jump delta.
                pos += 1;

                if node < MIN_TWO_UNIT_VALUE_LEAD {
                    pos += node as usize;
                } else if node < THREE_UNIT_VALUE_LEAD {
                    pos += (((node - MIN_TWO_UNIT_VALUE_LEAD) as u32) << 16) as usize
                        | self.trie.get(pos)? as usize;
                    pos += 1;
                } else {
                    pos +=
                        ((self.trie.get(pos)? as usize) << 16) | self.trie.get(pos + 1)? as usize;
                    pos += 2;
                }
            }
        }
        self.pos = Some(pos);
        Some(unit)
    }

    /// Returns the value at the current position.
    #[inline]
    pub fn value(&self) -> Option<i32> {
        if self.remaining_match_length.is_some() {
            return None;
        }
        let pos = self.pos?;
        let lead_unit = self.trie.get(pos)?;
        if lead_unit < MIN_VALUE_LEAD {
            None
        } else if lead_unit & VALUE_IS_FINAL != 0 {
            let v = self.read_value(pos + 1, lead_unit & 0x7fff);
            debug_assert!(v.is_some());
            Some(v.unwrap_or(0))
        } else {
            let v = self.read_node_value(pos + 1, lead_unit);
            debug_assert!(v.is_some());
            Some(v.unwrap_or(0))
        }
    }

    /// Checks whether the cursor points to an empty trie (i.e., no further
    /// units can be matched from the current position).
    ///
    /// Use this to determine when to stop iterating.
    #[inline]
    pub fn is_empty(&self) -> bool {
        let Some(pos) = self.pos else {
            return true;
        };
        self.remaining_match_length.is_none()
            && self.trie.get(pos).is_none_or(|u| u & VALUE_IS_FINAL != 0)
    }

    fn branch_next(&self, mut pos: usize, mut length: usize, in_unit: u16) -> Option<usize> {
        if length == 0 {
            length = self.trie.get(pos)? as usize;
            pos += 1;
        }
        length += 1;

        // The length of the branch is the number of units to select from.
        // The data structure encodes a binary search.
        while length > MAX_BRANCH_LINEAR_SUB_NODE_LENGTH {
            if in_unit < self.trie.get(pos)? {
                length >>= 1;
                pos = self.jump_by_delta(pos + 1)?;
            } else {
                length = length - (length >> 1);
                pos = self.skip_delta(pos + 1)?;
            }
        }
        // Drop down to linear search for the last few bytes.
        // length>=2 because the loop body above sees length>kMaxBranchLinearSubNodeLength>=3
        // and divides length by 2.
        loop {
            if in_unit == self.trie.get(pos)? {
                pos += 1;
                let node = self.trie.get(pos)?;
                if node & VALUE_IS_FINAL != 0 {
                    return Some(pos);
                }
                // Use the non-final value as the jump delta.
                pos += 1;

                if node < MIN_TWO_UNIT_VALUE_LEAD {
                    pos += node as usize;
                } else if node < THREE_UNIT_VALUE_LEAD {
                    pos += (((node - MIN_TWO_UNIT_VALUE_LEAD) as u32) << 16) as usize
                        | self.trie.get(pos)? as usize;
                    pos += 1;
                } else {
                    pos +=
                        ((self.trie.get(pos)? as usize) << 16) | self.trie.get(pos + 1)? as usize;
                    pos += 2;
                }
                return Some(pos);
            }
            length -= 1;
            pos = self.skip_value(pos + 1)?;
            if length <= 1 {
                break;
            }
        }

        (in_unit == self.trie.get(pos)?).then_some(pos + 1)
    }

    fn next_impl(&mut self, pos: usize, in_unit: u16) -> Option<usize> {
        let mut node = self.trie.get(pos)?;
        let mut pos = pos + 1;
        loop {
            if node < MIN_LINEAR_MATCH {
                return self.branch_next(pos, node as usize, in_unit);
            } else if node < MIN_VALUE_LEAD {
                // Match the first of length+1 units.
                let length = (node - MIN_LINEAR_MATCH) as u8;
                if in_unit == self.trie.get(pos)? {
                    self.remaining_match_length = length.checked_sub(1);
                    return Some(pos + 1);
                }
                // No match
                break;
            } else if (node & VALUE_IS_FINAL) != 0 {
                // No further matching units.
                break;
            } else {
                // Skip intermediate value.
                pos = skip_node_value(pos, node);
                node &= NODE_TYPE_MASK;
            }
        }
        None
    }

    #[inline(always)] // 1 call site and we want the Option to go away
    fn jump_by_delta(&self, pos: usize) -> Option<usize> {
        let delta = self.trie.get(pos)?;
        let v = if delta < MIN_TWO_UNIT_DELTA_LEAD {
            // nothing to do
            pos + 1 + delta as usize
        } else if delta == THREE_UNIT_DELTA_LEAD {
            let delta =
                ((self.trie.get(pos + 1)? as usize) << 16) | (self.trie.get(pos + 2)? as usize);
            pos + delta + 3
        } else {
            let delta = (((delta - MIN_TWO_UNIT_DELTA_LEAD) as usize) << 16)
                | (self.trie.get(pos + 1)? as usize);
            pos + delta + 2
        };
        Some(v)
    }

    #[inline(always)] // 1 call site and we want the Option to go away
    fn skip_value(&self, pos: usize) -> Option<usize> {
        let lead_unit = self.trie.get(pos)?;
        Some(skip_value(pos + 1, lead_unit & 0x7fff))
    }

    #[inline(always)] // 1 call site and we want the Option to go away
    fn skip_delta(&self, pos: usize) -> Option<usize> {
        let delta = self.trie.get(pos)?;
        let v = if delta < MIN_TWO_UNIT_DELTA_LEAD {
            pos + 1
        } else if delta == THREE_UNIT_DELTA_LEAD {
            pos + 3
        } else {
            pos + 2
        };
        Some(v)
    }

    #[inline(always)] // 1 call site and we want the Option to go away
    fn read_value(&self, pos: usize, lead_unit: u16) -> Option<i32> {
        let v = if lead_unit < MIN_TWO_UNIT_VALUE_LEAD {
            lead_unit.into()
        } else if lead_unit < THREE_UNIT_VALUE_LEAD {
            (((lead_unit - MIN_TWO_UNIT_VALUE_LEAD) as i32) << 16) | self.trie.get(pos)? as i32
        } else {
            ((self.trie.get(pos)? as i32) << 16) | self.trie.get(pos + 1)? as i32
        };
        Some(v)
    }

    #[inline(always)] // 1 call site and we want the Option to go away
    fn read_node_value(&self, pos: usize, lead_unit: u16) -> Option<i32> {
        let v = if lead_unit < (MIN_TWO_UNIT_NODE_VALUE_LEAD) {
            ((lead_unit >> 6) - 1).into()
        } else if lead_unit < THREE_UNIT_NODE_VALUE_LEAD {
            ((((lead_unit & 0x7fc0) - MIN_TWO_UNIT_NODE_VALUE_LEAD) as i32) << 10)
                | self.trie.get(pos)? as i32
        } else {
            ((self.trie.get(pos)? as i32) << 16) | self.trie.get(pos + 1)? as i32
        };
        Some(v)
    }
}

impl core::fmt::Write for Char16TrieCursor<'_> {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        for c in s.encode_utf16() {
            self.step16(c);
        }
        Ok(())
    }

    fn write_char(&mut self, c: char) -> core::fmt::Result {
        self.step(c);
        Ok(())
    }
}

#[allow(deprecated)]
impl<'a> Char16TrieIterator<'a> {
    /// Returns a new [`Char16TrieIterator`] backed by borrowed data for the `trie` array
    #[inline]
    pub fn new(trie: &'a ZeroSlice<u16>) -> Self {
        Self(Char16TrieCursor::new(trie))
    }

    /// Traverses the trie from the current state for this input char.
    #[inline]
    pub fn next(&mut self, c: char) -> TrieResult {
        self.next32(c as u32)
    }

    /// Traverses the trie from the current state for this input char.
    #[inline]
    pub fn next32(&mut self, c: u32) -> TrieResult {
        if c <= 0xffff {
            self.next16(c as u16)
        } else {
            self.0.step16(u16_lead(c));
            self.next16(u16_tail(c))
        }
    }

    /// Traverses the trie from the current state for this input char.
    #[inline]
    pub fn next16(&mut self, c: u16) -> TrieResult {
        self.0.step16(c);
        match (self.0.value(), self.0.is_empty()) {
            (Some(v), true) => TrieResult::FinalValue(v),
            (Some(v), false) => TrieResult::Intermediate(v),
            (None, true) => TrieResult::NoMatch,
            (None, false) => TrieResult::NoValue,
        }
    }
}