diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 10b75c1a94336..0cacfe11c468c 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -588,21 +588,43 @@ impl Iterator for EscapeDefault { fn ed_iterator_specializations() { use super::EscapeDefault; - /// Check counting + // Check counting assert_eq!('\n'.escape_default().count(), 2); assert_eq!('c'.escape_default().count(), 1); assert_eq!(' '.escape_default().count(), 1); assert_eq!('\\'.escape_default().count(), 2); + assert_eq!('\''.escape_default().count(), 2); + + // Check nth + + // Check that OoB is handled correctly + assert_eq!('\n'.escape_default().nth(2), None); + assert_eq!('c'.escape_default().nth(1), None); + assert_eq!(' '.escape_default().nth(1), None); + assert_eq!('\\'.escape_default().nth(2), None); + assert_eq!('\''.escape_default().nth(2), None); + + // Check the first char + assert_eq!('\n'.escape_default().nth(0), Some('\\')); + assert_eq!('c'.escape_default().nth(0), Some('c')); + assert_eq!(' '.escape_default().nth(0), Some(' ')); + assert_eq!('\\'.escape_default().nth(0), Some('\\')); + assert_eq!('\''.escape_default().nth(0), Some('\\')); + + // Check the second char + assert_eq!('\n'.escape_default().nth(1), Some('n')); + assert_eq!('\\'.escape_default().nth(1), Some('\\')); + assert_eq!('\''.escape_default().nth(1), Some('\'')); } impl EscapeDefault { fn get_offset(&self) -> Option { match self.state { - EscapeDefaultState::Backslash(c) => Some(0), - EscapeDefaultState::Char(c) => Some(1), - EscapeDefaultState::Done => None, - EscapeDefaultState::Unicode(ref mut iter) => None, + EscapeDefaultState::Backslash(c) => Some(0), + EscapeDefaultState::Char(c) => Some(1), + EscapeDefaultState::Done => None, + EscapeDefaultState::Unicode(_) => None, } } }