From bb0f6f3a76d9e1099460fb2b3b7b41653d697898 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 4 Nov 2022 11:32:07 +0100 Subject: Initial commit --- src/encoded_string.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/encoded_string.rs (limited to 'src/encoded_string.rs') diff --git a/src/encoded_string.rs b/src/encoded_string.rs new file mode 100644 index 0000000..55090a9 --- /dev/null +++ b/src/encoded_string.rs @@ -0,0 +1,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.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() + } +} -- cgit v1.2.3