I want to process a string containing a backslash followed by an escapable character as if they were one character.
let raw = r#"\""#;
let cooked = raw.process_escape_character_magic();
Right now, raw
has 2 characters: \
and "
. But what I actually want is cooked
, which only has one character: "
.
How do I get cooked
?
I was thinking about using regex, but I feel like there should probably be a better way.
I like using iterators in Rust, and I think that's a perfect usecase:
#[derive(Debug, PartialEq)]
enum MyError {
EscapeAtEndOfString,
InvalidEscapedChar(char),
}
struct InterpretEscapedString<'a> {
s: std::str::Chars<'a>,
}
impl<'a> Iterator for InterpretEscapedString<'a> {
type Item = Result<char, MyError>;
fn next(&mut self) -> Option<Self::Item> {
self.s.next().map(|c| match c {
'\\' => match self.s.next() {
None => Err(MyError::EscapeAtEndOfString),
Some('n') => Ok('\n'),
Some('\\') => Ok('\\'),
// etc.
Some(c) => Err(MyError::InvalidEscapedChar(c)),
},
c => Ok(c),
})
}
}
fn interpret_escaped_string(s: &str) -> Result<String, MyError> {
(InterpretEscapedString { s: s.chars() }).collect()
}
fn main() {
assert_eq!(interpret_escaped_string(r#""#), Ok("".into()));
assert_eq!(interpret_escaped_string(r#"a"#), Ok("a".into()));
assert_eq!(interpret_escaped_string(r#"\"#), Err(MyError::EscapeAtEndOfString));
assert_eq!(interpret_escaped_string(r#"\\"#), Ok("\\".into()));
assert_eq!(interpret_escaped_string(r#"a\n"#), Ok("a\n".into()));
assert_eq!(interpret_escaped_string(r#"a\."#), Err(MyError::InvalidEscapedChar('.')));
}
More complete implementation of such a module in the playground.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments