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
use core::fmt::{self, Write};
use core::iter::FusedIterator;
use core::mem;
use core::str::Chars;
use bstr::{ByteSlice, Bytes};
use crate::{Encoding, String};
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum CodepointsError {
InvalidUtf8Codepoint,
}
impl CodepointsError {
pub const EXCEPTION_TYPE: &'static str = "ArgumentError";
#[inline]
#[must_use]
pub const fn invalid_utf8_codepoint() -> Self {
Self::InvalidUtf8Codepoint
}
#[inline]
#[must_use]
pub const fn message(self) -> &'static str {
"invalid byte sequence in UTF-8"
}
}
impl fmt::Display for CodepointsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let CodepointsError::InvalidUtf8Codepoint = self;
f.write_str(self.message())
}
}
#[cfg(feature = "std")]
impl std::error::Error for CodepointsError {}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
enum CodePointRangeError {
InvalidUtf8Codepoint(u32),
OutOfRange(i64),
}
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct InvalidCodepointError(CodePointRangeError);
impl InvalidCodepointError {
pub const EXCEPTION_TYPE: &'static str = "RangeError";
#[inline]
#[must_use]
pub const fn invalid_utf8_codepoint(codepoint: u32) -> Self {
Self(CodePointRangeError::InvalidUtf8Codepoint(codepoint))
}
#[inline]
#[must_use]
pub const fn codepoint_out_of_range(codepoint: i64) -> Self {
Self(CodePointRangeError::OutOfRange(codepoint))
}
#[inline]
#[must_use]
pub const fn is_invalid_utf8(self) -> bool {
matches!(self.0, CodePointRangeError::InvalidUtf8Codepoint(_))
}
#[inline]
#[must_use]
pub const fn is_out_of_range(self) -> bool {
matches!(self.0, CodePointRangeError::OutOfRange(_))
}
#[inline]
#[must_use]
pub fn message(self) -> alloc::string::String {
const MESSAGE_MAX_LENGTH: usize = 27 + 2 + mem::size_of::<u32>() * 2;
let mut s = alloc::string::String::with_capacity(MESSAGE_MAX_LENGTH);
let _ = write!(s, "{self}");
s
}
}
impl fmt::Display for InvalidCodepointError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
CodePointRangeError::InvalidUtf8Codepoint(codepoint) => {
write!(f, "invalid codepoint {codepoint:X} in UTF-8")
}
CodePointRangeError::OutOfRange(codepoint) => write!(f, "{codepoint} out of char range"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for InvalidCodepointError {}
#[derive(Debug, Default, Clone)]
pub struct Codepoints<'a>(State<'a>);
impl<'a> TryFrom<&'a String> for Codepoints<'a> {
type Error = CodepointsError;
#[inline]
fn try_from(s: &'a String) -> Result<Self, Self::Error> {
let state = match s.encoding() {
Encoding::Utf8 => {
if let Ok(s) = s.inner.as_slice().to_str() {
State::Utf8(s.chars())
} else {
return Err(CodepointsError::invalid_utf8_codepoint());
}
}
Encoding::Ascii => {
let iter = s.as_slice().bytes();
State::Ascii(iter)
}
Encoding::Binary => {
let iter = s.as_slice().bytes();
State::Binary(iter)
}
};
Ok(Self(state))
}
}
impl<'a> Iterator for Codepoints<'a> {
type Item = u32;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl<'a> FusedIterator for Codepoints<'a> {}
#[derive(Debug, Clone)]
enum State<'a> {
Utf8(Chars<'a>),
Ascii(Bytes<'a>),
Binary(Bytes<'a>),
}
impl<'a> Default for State<'a> {
fn default() -> Self {
Self::Utf8("".chars())
}
}
impl<'a> Iterator for State<'a> {
type Item = u32;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Ascii(iter) | Self::Binary(iter) => iter.next().map(u32::from),
Self::Utf8(iter) => iter.next().map(u32::from),
}
}
}
impl<'a> FusedIterator for State<'a> {}