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
use crate::{StringIter, pattern::{Pattern, Never}};
macro_rules! get {
($e: expr, $v: expr) => {
*$e.get_unchecked($v) as u32
};
}
const B6: u32 = 0b111_111;
const B5: u32 = 0b11_111;
const B4: u32 = 0b1_111;
const B3: u32 = 0b111;
/// SAFETY: s contains a sigle byte UTF-8 code point
#[inline]
unsafe fn s2c1(s: &str) -> char {
let s = s.as_bytes();
char::from_u32_unchecked(get!(s, 0))
}
/// SAFETY: s contains a 2 byte UTF-8 code point
#[inline]
unsafe fn s2c2(s: &str) -> char {
let s = s.as_bytes();
char::from_u32_unchecked(
(get!(s, 0) & B5) << 6 | get!(s, 1) & B6
)
}
/// SAFETY: s contains a 3 byte UTF-8 code point
#[inline]
unsafe fn s2c3(s: &str) -> char {
let s = s.as_bytes();
char::from_u32_unchecked(
((get!(s, 0) & B4) << 6
| get!(s, 1) & B6) << 6
| get!(s, 2) & B6
)
}
/// SAFETY: s contains a 4 byte UTF-8 code point
#[inline]
unsafe fn s2c4(s: &str) -> char {
let s = s.as_bytes();
char::from_u32_unchecked(
(((get!(s, 0) & B3) << 6
| get!(s, 1) & B6) << 6
| get!(s, 2) & B6) << 6
| get!(s, 3) & B6
)
}
impl<'t> StringIter<'t> {
/// Returns a leading [`char`] and its [`&str`](str) representation
/// from the `StringIter` and advances it.
///
/// This is the same as `StringIter::next()`,
/// but available to mapped versions of `StringIter`.
#[inline]
pub fn next_char(&mut self) -> Option<(char, &'t str)> {
let x = *self.str.as_bytes().get(0)?;
// SAFETY: safe since self.str is valid utf-8
unsafe{
if x < 0x80 {
let result = self.str.get_unchecked(..1);
self.str = self.str.get_unchecked(1..);
Some((s2c1(result), result))
} else if x < 0xE0 {
let result = self.str.get_unchecked(..2);
self.str = self.str.get_unchecked(2..);
Some((s2c2(result), result))
} else if x < 0xF0 {
let result = self.str.get_unchecked(..3);
self.str = self.str.get_unchecked(3..);
Some((s2c3(result), result))
} else {
let result = self.str.get_unchecked(..4);
self.str = self.str.get_unchecked(4..);
Some((s2c4(result), result))
}
}
}
/// Returns a trailing [`char`] and its [`&str`](str) representation
/// from the `StringIter` and advances it.
///
/// This is the same as `StringIter::next_back()`,
/// but available to mapped versions of `StringIter`.
#[inline]
pub fn next_char_back(&mut self) -> Option<(char, &'t str)> {
let bytes = self.str.as_bytes();
if bytes.is_empty() {
return None;
}
let mut index = bytes.len() - 1;
// SAFETY: safe since self.str is valid utf-8
unsafe{
if *bytes.get_unchecked(index) < 0x80 {
let result = self.str.get_unchecked(index..);
self.str = self.str.get_unchecked(..index);
return Some((s2c1(result), result))
}
index -= 1;
if *bytes.get_unchecked(index) & 0b1100_0000 == 0b1100_0000 {
let result = self.str.get_unchecked(index..);
self.str = self.str.get_unchecked(..index);
return Some((s2c2(result), result))
}
index -= 1;
if *bytes.get_unchecked(index) & 0b1110_0000 == 0b1110_0000 {
let result = self.str.get_unchecked(index..);
self.str = self.str.get_unchecked(..index);
return Some((s2c3(result), result))
}
index -= 1;
let result = self.str.get_unchecked(index..);
self.str = self.str.get_unchecked(..index);
return Some((s2c4(result), result))
}
}
/// Returns a leading [`char`] and its [`&str`](str) representation
/// from the `StringIter` without advancing it.
#[inline]
pub fn peek(&self) -> Option<(char, &'t str)> {
let x = *self.str.as_bytes().first()?;
// SAFETY: safe since self.str is valid utf-8
unsafe{
if x < 0x80 {
let result = self.str.get_unchecked(..1);
Some((s2c1(result), result))
} else if x < 0xE0 {
let result = self.str.get_unchecked(..2);
Some((s2c2(result), result))
} else if x < 0xF0 {
let result = self.str.get_unchecked(..3);
Some((s2c3(result), result))
} else {
let result = self.str.get_unchecked(..4);
Some((s2c4(result), result))
}
}
}
/// Returns a trailing [`char`] and its [`&str`](str) representation
/// from the `StringIter` without advancing it.
#[inline]
pub fn peek_back(&self) -> Option<(char, &'t str)> {
let bytes = self.str.as_bytes();
if bytes.is_empty() {
return None;
}
let mut index = bytes.len() - 1;
// SAFETY: safe since self.str is valid utf-8
unsafe{
if *bytes.get_unchecked(index) < 0x80 {
let result = self.str.get_unchecked(index..);
return Some((s2c1(result), result))
}
index -= 1;
if *bytes.get_unchecked(index) & 0b1100_0000 == 0b1100_0000 {
let result = self.str.get_unchecked(index..);
return Some((s2c2(result), result))
}
index -= 1;
if *bytes.get_unchecked(index) & 0b1110_0000 == 0b1110_0000 {
let result = self.str.get_unchecked(index..);
return Some((s2c3(result), result))
}
index -= 1;
let result = self.str.get_unchecked(index..);
return Some((s2c4(result), result))
}
}
/// Returns leading [`&str`](str)s with maximum count `n`
/// from the `StringIter` without advancing it.
///
/// Returns OK if n [`char`]s found, Err if less than n [`char`]s found.
pub fn peekn(&self, n: usize) -> Result<&'t str, &'t str> {
let mut index = 0usize;
for _ in 0..n {
let x = *self.str.as_bytes().get(index)
.ok_or_else(|| unsafe {
self.str.get_unchecked(..index)
})?;
if x < 0x80{
index += 1;
} else if x < 0xE0 {
index += 2;
} else if x < 0xF0 {
index += 3;
} else {
index += 4;
}
}
// SAFETY: this is safe because self is valid utf-8
unsafe {
Ok(self.str.get_unchecked(..index))
}
}
/// Returns trailing [`&str`](str)s with maximum count `n`
/// from the `StringIter` without advancing it.
///
/// Returns OK if n [`char`]s found, Err if less than n [`char`]s found.
pub fn peekn_back(&self, n: usize) -> Result<&'t str, &'t str> {
let bytes = self.str.as_bytes();
let mut index = bytes.len();
// SAFETY: this is safe because self is valid utf-8
unsafe{
for _ in 0..n {
if index == 0 {
return Err(self.str.get_unchecked(index..));
}
index -= 1;
if *bytes.get_unchecked(index) < 0x80 {
continue;
}
index -= 1;
if *bytes.get_unchecked(index) & 0b1100_0000 == 0b1100_0000 {
continue;
}
index -= 1;
if *bytes.get_unchecked(index) & 0b1110_0000 == 0b1110_0000 {
continue;
}
index -= 1;
}
Ok(self.str.get_unchecked(index..))
}
}
/// Removes leading and trailing [`char`]s that matches a `Pattern` from the `StringIter`.
pub fn trim_by(&mut self, f: impl Pattern<Err = Never> + Clone){
self.trim_start_by(f.clone());
self.trim_end_by(f);
}
/// Removes leading [`char`]s that matches a `Pattern` from the `StringIter`.
pub fn trim_start_by(&mut self, mut f: impl Pattern<Err = Never>){
let bytes = self.as_bytes();
let mut index = 0;
while let Some(x) = bytes.get(index) {
let x = *x;
// SAFETY: safe since self.str is valid utf-8
let (c, s, len) = unsafe {
if x < 0x80 {
let result = self.str.get_unchecked(index..index+1);
(s2c1(result), result, 1)
} else if x < 0xE0 {
let result = self.str.get_unchecked(index..index+2);
(s2c2(result), result, 2)
} else if x < 0xF0 {
let result = self.str.get_unchecked(index..index+3);
(s2c3(result), result, 3)
} else {
let result = self.str.get_unchecked(index..index+4);
(s2c4(result), result, 4)
}
};
if f.matches(c, s).unwrap() {
index += len;
} else {
break;
}
}
unsafe {
self.str = self.str.get_unchecked(index..)
}
}
/// Removes trailing [`char`]s that matches a `Pattern` from the `StringIter`.
pub fn trim_end_by(&mut self, mut f: impl Pattern<Err = Never>){
let bytes = self.as_bytes();
let mut index = self.len() - 1;
while let Some(x) = bytes.get(index) {
let mut i = index;
// SAFETY: safe since self.str is valid utf-8
unsafe {
i -= 1;
if *x < 0x80 {
let s = self.str.get_unchecked(i..index);
if !f.matches(s2c1(s), s).unwrap() {
break;
} else {
index = i;
continue;
}
}
i -= 1;
if *bytes.get_unchecked(i) & 0b1100_0000 == 0b1100_0000 {
let s = self.str.get_unchecked(i..index);
if !f.matches(s2c2(s), s).unwrap() {
break;
} else {
index = i;
continue;
}
}
i -= 1;
if *bytes.get_unchecked(i) & 0b1110_0000 == 0b1110_0000 {
let s = self.str.get_unchecked(i..index);
if !f.matches(s2c3(s), s).unwrap() {
break;
} else {
index = i;
continue;
}
}
i -= 1;
let s = self.str.get_unchecked(i..index);
if !f.matches(s2c4(s), s).unwrap() {
break;
} else {
index = i;
continue;
}
}
}
unsafe {
self.str = self.str.get_unchecked(..index)
}
}
}