aboutsummaryrefslogtreecommitdiff
path: root/src/encoded_string.rs
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2022-11-04 11:32:07 +0100
committerThomas Voss <mail@thomasvoss.com> 2022-11-04 11:37:07 +0100
commitbb0f6f3a76d9e1099460fb2b3b7b41653d697898 (patch)
treed2d44ff8073bceacbbfb92814921f72fad29c035 /src/encoded_string.rs
Initial commit
Diffstat (limited to 'src/encoded_string.rs')
-rw-r--r--src/encoded_string.rs26
1 files changed, 26 insertions, 0 deletions
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::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()
+ }
+}