[rust][8.0] Pattern match over a window instead

wip
Alpha Chen 9 years ago
parent 84f1d930ec
commit ccc3af56bf

@ -27,26 +27,25 @@ pub fn solve(input: &str) -> usize {
fn decode(string: &str) -> String { fn decode(string: &str) -> String {
let mut out = "".to_owned(); let mut out = "".to_owned();
let mut it = string[1..string.len()-1].chars();
let mut chars = string.chars().map(|c| Some(c)).collect::<Vec<_>>();
chars.append(&mut vec![None, None, None]);
let mut it = chars.windows(4);
loop { loop {
let c = match it.next() {
Some('\\') => {
match it.next() { match it.next() {
Some('\\') => '\\', Some([Some('"'), None, None, None]) => break,
Some('"') => '"', Some([Some('"'), _, _, _]) => {},
Some('x') => { Some([Some('\\'), Some('\\'), _, _]) => { it.next(); out.push('\\'); },
let mut b = it.next().and_then(|c| c.to_digit(16)).unwrap() << 4; Some([Some('\\'), Some('"'), _, _]) => { it.next(); out.push('"'); },
b += it.next().and_then(|c| c.to_digit(16)).unwrap(); Some([Some('\\'), Some('x'), Some(a), Some(b)]) => {
b as u8 as char it.next(); it.next(); it.next();
let c = (a.to_digit(16).unwrap_or(0) << 4) + (b.to_digit(16).unwrap_or(0));
out.push(c as u8 as char);
}, },
Some(c) => panic!("Unexpected escaped char: {}", c), Some([Some(c), _, _, _]) => { out.push(c); },
None => panic!("Unexpected end of string"), Some(x) => panic!("{:?}", x),
} None => panic!(""),
}
Some(c) => c,
None => break,
}; };
out.push(c);
} }
out out
} }

Loading…
Cancel
Save