aboutsummaryrefslogtreecommitdiff
path: root/src/encoded_string.rs
blob: 55090a9cf10433b37af48d365cb491705705b15f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use std::str;

pub struct EncodedString<'a> {
    pub s: str::Bytes<'a>,
}

impl<'a> Iterator for EncodedString<'a> {
	type Item = u8;

	fn next(&mut self) -> Option<Self::Item> {
		self.s.next().map(|c| match c {
			b'\\' => match self.s.next() {
				Some(b'n') => b'\n',
				Some(b'\\') | None => b'\\',
				Some(c) => c
			},
			c => c
		})
	}
}

impl<'a> EncodedString<'a> {
	pub fn decode(self) -> String {
		String::from_utf8(self.s.collect()).unwrap()
	}
}