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
// devela::text::char::iter::bytes
use crate::{Char, CharIter, char7, char8, char16, charu, is, unwrap};
/// Methods available when constructed from a byte slice.
impl<'a> CharIter<'a, &[u8]> {
/* constructors */
/// Returns a new iterator over the Unicode scalars of a slice of `bytes`.
pub const fn new(bytes: &'a [u8]) -> Self {
Self::_new(bytes, 0)
}
/// Returns a new iterator over the Unicode scalars of a slice of `bytes`,
/// starting at `index`.
///
/// Returns `None` if the given index is not a valid character boundary.
#[must_use] #[inline(always)] #[rustfmt::skip]
pub const fn new_at(bytes: &'a [u8], index: usize) -> Option<Self> {
if Char(bytes).is_utf8_boundary(index) {
Some(Self::_new(bytes, index))
} else {
None
}
}
/* misc. */
/// Returns the total number of Unicode scalars, consuming the iterator.
pub const fn count(mut self) -> usize {
let mut counter = 0;
while self.pos < self.bytes.len() {
if let Some((_, len)) = Char(self.bytes).to_scalar(self.pos) {
self.pos += len;
counter += 1;
} else {
break;
}
}
counter
}
/* next_char* methods */
/// Returns the next Unicode scalar.
///
/// This is implemented via `Char::`[`to_char`][Char::to_char].
///
/// # Features
/// Uses the `unsafe_niche` feature to skip duplicated validation checks.
#[must_use]
pub const fn next_char(&mut self) -> Option<char> {
is![self.pos >= self.bytes.len(), return None];
let Some((ch, len)) = Char(self.bytes).to_char(self.pos) else { return None };
self.pos += len;
Some(ch)
}
/// Returns the next Unicode scalar, without performing full UTF-8 validation,
/// but mostly the final Unicode scalar.
///
/// If the leading byte is invalid it returns the replacement character (`�`).
///
/// This is implemented via `Char::`[`to_char_lenient`][Char::to_char_lenient].
#[must_use]
pub const fn next_char_lenient(&mut self) -> Option<char> {
is![self.pos >= self.bytes.len(), return None];
let (cp, len) = Char(self.bytes).to_scalar_unchecked(self.pos);
is![let Some(ch) = char::from_u32(cp), { self.pos += len; Some(ch) }, None]
}
/// Returns the next Unicode scalar, without performing UTF-8 validation.
///
/// # Safety
/// The caller must ensure that:
/// - `index` is within bounds of `bytes`.
/// - `bytes[index..]` contains a valid UTF-8 sequence.
/// - The decoded value is a valid Unicode scalar.
///
/// Violating these conditions may lead to undefined behavior.
#[must_use]
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
#[cfg_attr(nightly_doc, doc(cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))))]
pub const unsafe fn next_char_unchecked(&mut self) -> Option<char> {
is![self.pos >= self.bytes.len(), return None];
let (ch, len) = unsafe { Char(self.bytes).to_char_unchecked(self.pos) };
self.pos += len;
Some(ch)
}
/// Returns the next 7-bit Unicode scalar.
///
/// Returns `None` once there are no more characters left,
/// or if the next character is not ASCII.
///
/// # Features
/// Uses the `unsafe_niche` feature to skip validation checks.
#[must_use]
pub const fn next_char7(&mut self) -> Option<char7> {
is![self.pos >= self.bytes.len(), return None];
let byte = self.bytes[0];
is![
byte.is_ascii(),
{
self.pos += 1;
Some(char7::new_unchecked(byte))
},
None
]
}
/// Returns the next 8-bit Unicode scalar.
///
/// Returns `None` once there are no more characters left,
/// or if the next character can't fit in 1 byte.
#[must_use]
pub const fn next_char8(&mut self) -> Option<char8> {
is![self.pos >= self.bytes.len(), return None];
let Some((cp, len)) = Char(self.bytes).to_scalar(self.pos) else { return None };
if Char(cp).len_bytes() == 1 {
self.pos += len;
Some(char8(cp as u8))
} else {
None
}
}
/// Returns the next 8-bit Unicode scalar, without performing UTF-8 validation.
///
/// Returns `None` once there are no more characters left,
/// or if the next character can't fit in 1 byte.
///
/// # Panics
/// It will panic if the index is out of bounds.
///
/// # Safety
/// The caller must ensure that:
/// - `index` is within bounds of `bytes`.
/// - `bytes[index..]` contains a valid UTF-8 sequence.
/// - The decoded value is a valid Unicode scalar.
///
/// Violating these conditions may lead to undefined behavior.
#[must_use]
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
#[cfg_attr(nightly_doc, doc(cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))))]
pub const unsafe fn next_char8_unchecked(&mut self) -> Option<char8> {
let (cp, len) = Char(self.bytes).to_scalar_unchecked(self.pos);
is![
Char(cp).len_bytes() == 1,
{
self.pos += len;
Some(char8(cp as u8))
},
None
]
}
/// Returns the next 16-bit Unicode scalar.
///
/// Returns `None` once there are no more characters left,
/// or if the next character can't fit in 2 bytes.
///
/// # Features
/// Uses the `unsafe_niche` feature to skip validation checks.
#[must_use]
pub const fn next_char16(&mut self) -> Option<char16> {
is![self.pos >= self.bytes.len(), return None];
let Some((cp, len)) = Char(self.bytes).to_scalar(self.pos) else { return None };
if Char(cp).len_bytes() <= 2 {
self.pos += len;
Some(char16::new_unchecked(cp as u16))
} else {
None
}
}
/// Returns the next 16-bit Unicode scalar, without performing UTF-8 validation.
///
/// Returns `None` once there are no more characters left,
/// or if the next character can't fit in 2 bytes.
///
/// # Panics
/// It will panic if the index is out of bounds.
///
/// # Safety
/// The caller must ensure that:
/// - `index` is within bounds of `bytes`.
/// - `bytes[index..]` contains a valid UTF-8 sequence.
/// - The decoded value is a valid Unicode scalar.
///
/// Violating these conditions may lead to undefined behavior.
///
/// # Features
/// Uses the `unsafe_niche` feature to skip validation checks.
#[must_use]
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
#[cfg_attr(nightly_doc, doc(cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))))]
pub const unsafe fn next_char16_unchecked(&mut self) -> Option<char16> {
let (cp, len) = Char(self.bytes).to_scalar_unchecked(self.pos);
if Char(cp).len_bytes() <= 2 {
self.pos += len;
Some(char16::new_unchecked(cp as u16))
} else {
None
}
}
/// Returns the next Unicode scalar using a UTF-8 representation.
///
/// Returns `None` once there are no more characters left.
///
/// # Features
/// Uses the `unsafe_hint` feature to optimize out unreachable branches.
#[must_use] #[rustfmt::skip]
pub const fn next_charu(&mut self) -> Option<charu> {
is![self.pos >= self.bytes.len(), return None];
let (ch, len) = unwrap![some? charu::from_utf8_bytes_with_len(self.bytes)];
self.pos += len as usize;
Some(ch)
}
/// Returns the next Unicode scalar, without performing UTF-8 validation.
///
/// # Safety
/// The caller must ensure that:
/// - `index` is within bounds of `bytes`.
/// - `bytes[index..]` contains a valid UTF-8 sequence.
/// - The decoded value is a valid Unicode scalar.
///
/// Violating these conditions may lead to undefined behavior.
///
/// # Features
/// Uses the `unsafe_hint` feature to optimize out unreachable branches.
#[must_use]
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
#[cfg_attr(nightly_doc, doc(cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))))]
pub const unsafe fn next_charu_unchecked(&mut self) -> Option<charu> {
is![self.pos >= self.bytes.len(), return None];
let (ch, len) = unsafe { charu::from_utf8_bytes_with_len_unchecked(self.bytes) };
self.pos += len as usize;
Some(ch)
}
/* next_scalar* methods */
/// Returns the next Unicode scalar value.
///
/// This is implemented via `Char::`[`to_scalar`][Char::to_scalar].
///
/// # Features
/// Uses the `unsafe_niche` feature to skip duplicated validation checks.
#[must_use]
pub const fn next_scalar(&mut self) -> Option<u32> {
is![self.pos >= self.bytes.len(), return None];
let Some((ch, len)) = Char(self.bytes).to_scalar(self.pos) else { return None };
self.pos += len;
Some(ch)
}
/// Returns the next Unicode scalar, without performing UTF-8 validation.
///
/// This is implemented via `Char::`[`to_scalar_unchecked`][Char::to_scalar_unchecked].
///
/// It assumes `bytes[index..]` contains a valid UTF-8 sequence,
/// and it doesn't validate the resulting Unicode scalar.
///
/// If the leading byte is invalid it returns the replacement character (`�`).
///
/// # Panics
/// It will panic if the index is out of bounds.
#[must_use]
pub const fn next_scalar_unchecked(&mut self) -> Option<u32> {
is![self.pos >= self.bytes.len(), return None];
let (ch, len) = Char(self.bytes).to_scalar_unchecked(self.pos);
self.pos += len;
Some(ch)
}
}