Skip to content

Commit 23c5da0

Browse files
committed
Implement EscapeUnicode::nth
as a step from the appropriate state. Part of #24214.
1 parent f6c8757 commit 23c5da0

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libcore/char.rs

+17
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,23 @@ impl Iterator for EscapeUnicode {
466466
self.len()
467467
}
468468

469+
fn nth(&mut self, n: usize) -> Option<char> {
470+
let remaining = self.len().saturating_sub(n);
471+
472+
// offset = (number of hex digits still to be emitted) - 1
473+
// It can be computed from the remaining number of items by keeping
474+
// into account that:
475+
// - offset can never increase
476+
// - the last 2 items (last hex digit, '}') are not counted in offset
477+
let offset = ::cmp::min(self.offset, remaining.saturating_sub(2));
478+
479+
// state = number of items to be emitted for the state (as per state_len())
480+
// It can be computed because (remaining number of items) = state + offset
481+
let state = remaining - offset;
482+
483+
self.step(state, offset)
484+
}
485+
469486
fn last(self) -> Option<char> {
470487
match self.state {
471488
EscapeUnicodeState::Done => None,

0 commit comments

Comments
 (0)