Skip to content

Commit 1ec80f6

Browse files
committed
Auto merge of #33103 - ranma42:escape-unicode-last, r=alexcrichton
Implement `last` for `EscapeUnicode` The implementation is quite trivial as the last character is always `'{'`. As a side-effect it also improves the implementation of `last` for `EscapeUnicode`. Part of #24214, split from #31049. Maybe this (and the other changes that I will split from #31049) should wait for a test like `ed_iterator_specializations` to be added. Would it be sufficient to do the same for each possible escape length?
2 parents 764ef92 + 8169fa2 commit 1ec80f6

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/libcore/char.rs

+12
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,18 @@ impl Iterator for EscapeUnicode {
471471
let n = n + self.hex_digit_idx;
472472
(n, Some(n))
473473
}
474+
475+
fn last(self) -> Option<char> {
476+
match self.state {
477+
EscapeUnicodeState::Done => None,
478+
479+
EscapeUnicodeState::RightBrace |
480+
EscapeUnicodeState::Value |
481+
EscapeUnicodeState::LeftBrace |
482+
EscapeUnicodeState::Type |
483+
EscapeUnicodeState::Backslash => Some('}'),
484+
}
485+
}
474486
}
475487

476488
/// An iterator that yields the literal escape code of a `char`.

src/libcoretest/char.rs

+33
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,37 @@ fn ed_iterator_specializations() {
262262
assert_eq!('\''.escape_default().last(), Some('\''));
263263
}
264264

265+
#[test]
266+
fn eu_iterator_specializations() {
267+
fn check(c: char) {
268+
let len = c.escape_unicode().count();
269+
270+
// Check OoB
271+
assert_eq!(c.escape_unicode().nth(len), None);
272+
273+
// For all possible in-bound offsets
274+
let mut iter = c.escape_unicode();
275+
for offset in 0..len {
276+
// Check last
277+
assert_eq!(iter.clone().last(), Some('}'));
265278

279+
// Check counting
280+
assert_eq!(iter.clone().count(), len - offset);
281+
282+
// Check nth
283+
assert_eq!(c.escape_unicode().nth(offset), iter.next());
284+
}
285+
286+
// Check post-last
287+
assert_eq!(iter.clone().last(), None);
288+
assert_eq!(iter.clone().count(), 0);
289+
}
290+
291+
check('\u{0}');
292+
check('\u{1}');
293+
check('\u{12}');
294+
check('\u{123}');
295+
check('\u{1234}');
296+
check('\u{12340}');
297+
check('\u{10FFFF}');
298+
}

0 commit comments

Comments
 (0)