[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() { match it.next() {
Some('\\') => { Some([Some('"'), None, None, None]) => break,
match it.next() { Some([Some('"'), _, _, _]) => {},
Some('\\') => '\\', Some([Some('\\'), Some('\\'), _, _]) => { it.next(); out.push('\\'); },
Some('"') => '"', Some([Some('\\'), Some('"'), _, _]) => { it.next(); out.push('"'); },
Some('x') => { Some([Some('\\'), Some('x'), Some(a), Some(b)]) => {
let mut b = it.next().and_then(|c| c.to_digit(16)).unwrap() << 4; it.next(); it.next(); it.next();
b += it.next().and_then(|c| c.to_digit(16)).unwrap(); let c = (a.to_digit(16).unwrap_or(0) << 4) + (b.to_digit(16).unwrap_or(0));
b as u8 as char 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