Debian Codegraph @

debian/rust-vendor/icu_segmenter/src/indices.rs

raw
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 pub use core::str::CharIndices;
6 pub use utf8_iter::Utf8CharIndices;
7
8 /// Similar to [`core::str::CharIndices`] for Latin-1 strings, represented as `[u8]`.
9 ///
10 /// Contrary to [`core::str::CharIndices`], the second element of the
11 /// [`Iterator::Item`] is a [`u8`], representing a Unicode scalar value in the
12 /// range U+0000–U+00FF.
13 #[derive(Clone, Debug)]
14 pub struct Latin1Indices<'a> {
15 front_offset: usize,
16 iter: &'a [u8],
17 }
18
19 impl<'a> Latin1Indices<'a> {
20 pub fn new(input: &'a [u8]) -> Self {
21 Self {
22 front_offset: 0,
23 iter: input,
24 }
25 }
26
27 pub fn as_slice(&self) -> &'a [u8] {
28 self.iter.get(self.front_offset..).unwrap_or_default()
29 }
30
31 pub fn offset(&self) -> usize {
32 self.front_offset
33 }
34 }
35
36 impl Iterator for Latin1Indices<'_> {
37 type Item = (usize, u8);
38
39 #[inline]
40 fn next(&mut self) -> Option<(usize, u8)> {
41 self.iter.get(self.front_offset).map(|ch| {
42 self.front_offset += 1;
43 (self.front_offset - 1, *ch)
44 })
45 }
46 }
47
48 /// Similar to [`core::str::CharIndices`] for UTF-16 strings, represented as `[u16]`.
49 ///
50 /// Contrary to [`core::str::CharIndices`], the second element of the
51 /// [`Iterator::Item`] is a Unicode code point represented by a [`u32`],
52 /// rather than a Unicode scalar value represented by a [`char`], because this
53 /// iterator preserves unpaired surrogates.
54 #[derive(Clone, Debug)]
55 pub struct Utf16Indices<'a> {
56 front_offset: usize,
57 iter: &'a [u16],
58 }
59
60 impl<'a> Utf16Indices<'a> {
61 pub fn new(input: &'a [u16]) -> Self {
62 Self {
63 front_offset: 0,
64 iter: input,
65 }
66 }
67
68 pub fn as_slice(&self) -> &'a [u16] {
69 self.iter.get(self.front_offset..).unwrap_or_default()
70 }
71
72 pub fn offset(&self) -> usize {
73 self.front_offset
74 }
75 }
76
77 impl Iterator for Utf16Indices<'_> {
78 type Item = (usize, u32);
79
80 #[inline]
81 fn next(&mut self) -> Option<(usize, u32)> {
82 let (index, ch) = self.iter.get(self.front_offset).map(|ch| {
83 self.front_offset += 1;
84 (self.front_offset - 1, *ch)
85 })?;
86
87 let mut ch = ch as u32;
88 if (ch & 0xfc00) != 0xd800 {
89 return Some((index, ch));
90 }
91
92 if let Some(next) = self.iter.get(self.front_offset) {
93 let next = *next as u32;
94 if (next & 0xfc00) == 0xdc00 {
95 // Combine low and high surrogates to UTF-32 code point.
96 ch = ((ch & 0x3ff) << 10) + (next & 0x3ff) + 0x10000;
97 self.front_offset += 1;
98 }
99 }
100 Some((index, ch))
101 }
102 }
103
104 #[cfg(test)]
105 mod tests {
106 use crate::indices::*;
107
108 #[test]
109 fn latin1_indices() {
110 let latin1 = [0x30, 0x31, 0x32];
111 let mut indices = Latin1Indices::new(&latin1);
112 let n = indices.next().unwrap();
113 assert_eq!(n.0, 0);
114 assert_eq!(n.1, 0x30);
115 let n = indices.next().unwrap();
116 assert_eq!(n.0, 1);
117 assert_eq!(n.1, 0x31);
118 let n = indices.next().unwrap();
119 assert_eq!(n.0, 2);
120 assert_eq!(n.1, 0x32);
121 let n = indices.next();
122 assert_eq!(n, None);
123 }
124
125 #[test]
126 fn utf16_indices() {
127 let utf16 = [0xd83d, 0xde03, 0x0020, 0xd83c, 0xdf00, 0xd800, 0x0020];
128 let mut indices = Utf16Indices::new(&utf16);
129 let n = indices.next().unwrap();
130 assert_eq!(n.0, 0);
131 assert_eq!(n.1, 0x1f603);
132 let n = indices.next().unwrap();
133 assert_eq!(n.0, 2);
134 assert_eq!(n.1, 0x20);
135 let n = indices.next().unwrap();
136 assert_eq!(n.0, 3);
137 assert_eq!(n.1, 0x1f300);
138 // This is invalid surrogate pair.
139 let n = indices.next().unwrap();
140 assert_eq!(n.0, 5);
141 assert_eq!(n.1, 0xd800);
142 let n = indices.next().unwrap();
143 assert_eq!(n.0, 6);
144 assert_eq!(n.1, 0x0020);
145 let n = indices.next();
146 assert_eq!(n, None);
147 }
148 }