mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-24 07:44:10 +00:00
Make style more uniform, add tests for specialization of .last(), move tests to libcoretest
Remove unused import Fold nth() method into the match expr
This commit is contained in:
parent
15a5e02486
commit
d026977f25
@ -540,91 +540,46 @@ impl Iterator for EscapeDefault {
|
||||
|
||||
fn count(self) -> usize {
|
||||
match self.state {
|
||||
EscapeDefaultState::Char(_) => 1,
|
||||
EscapeDefaultState::Char(_) => 1,
|
||||
EscapeDefaultState::Unicode(iter) => iter.count(),
|
||||
EscapeDefaultState::Done => 0,
|
||||
EscapeDefaultState::Backslash(_) => 2,
|
||||
EscapeDefaultState::Done => 0,
|
||||
EscapeDefaultState::Backslash(_) => 2,
|
||||
}
|
||||
}
|
||||
|
||||
fn nth(&mut self, n: usize) -> Option<char> {
|
||||
let ch = match self.state {
|
||||
EscapeDefaultState::Backslash(c) => c,
|
||||
EscapeDefaultState::Char(c) => c,
|
||||
EscapeDefaultState::Done => return None,
|
||||
match self.state {
|
||||
EscapeDefaultState::Backslash(c) if n == 0 => {
|
||||
self.state = EscapeDefaultState::Char(c);
|
||||
Some('\\')
|
||||
},
|
||||
EscapeDefaultState::Backslash(c) if n == 1 => {
|
||||
self.state = EscapeDefaultState::Done;
|
||||
Some(c)
|
||||
},
|
||||
EscapeDefaultState::Backslash(_) => {
|
||||
self.state = EscapeDefaultState::Done;
|
||||
None
|
||||
},
|
||||
EscapeDefaultState::Char(c) => {
|
||||
self.state = EscapeDefaultState::Done;
|
||||
|
||||
if n == 0 {
|
||||
Some(c)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
EscapeDefaultState::Done => return None,
|
||||
EscapeDefaultState::Unicode(ref mut i) => return i.nth(n),
|
||||
};
|
||||
|
||||
let start = if let Some(x) = self.get_offset() {
|
||||
x
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
let idx = start + n;
|
||||
|
||||
// Update state
|
||||
self.state = match idx {
|
||||
0 => EscapeDefaultState::Char(ch),
|
||||
_ => EscapeDefaultState::Done,
|
||||
};
|
||||
|
||||
match idx {
|
||||
0 => Some('\\'),
|
||||
1 => Some(ch),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn last(self) -> Option<char> {
|
||||
match self.state {
|
||||
EscapeDefaultState::Unicode(iter) => iter.last(),
|
||||
EscapeDefaultState::Done => None,
|
||||
EscapeDefaultState::Unicode(iter) => iter.last(),
|
||||
EscapeDefaultState::Done => None,
|
||||
EscapeDefaultState::Backslash(c) | EscapeDefaultState::Char(c) => Some(c),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ed_iterator_specializations() {
|
||||
use super::EscapeDefault;
|
||||
|
||||
// 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<usize> {
|
||||
match self.state {
|
||||
EscapeDefaultState::Backslash(_) => Some(0),
|
||||
EscapeDefaultState::Char(_) => Some(1),
|
||||
EscapeDefaultState::Done => None,
|
||||
EscapeDefaultState::Unicode(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -216,3 +216,43 @@ fn test_decode_utf16() {
|
||||
check(&[0xD800, 0x41, 0x42], &[Err(0xD800), Ok('A'), Ok('B')]);
|
||||
check(&[0xD800, 0], &[Err(0xD800), Ok('\0')]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ed_iterator_specializations() {
|
||||
// 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('\''));
|
||||
|
||||
// Check the last char
|
||||
assert_eq!('\n'.escape_default().last(), Some('n'));
|
||||
assert_eq!('c'.escape_default().last(), Some('c'));
|
||||
assert_eq!(' '.escape_default().last(), Some(' '));
|
||||
assert_eq!('\\'.escape_default().last(), Some('\\'));
|
||||
assert_eq!('\''.escape_default().last(), Some('\''));
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user