diff options
Diffstat (limited to 'grep-cli/src/escape.rs')
-rw-r--r-- | grep-cli/src/escape.rs | 92 |
1 files changed, 51 insertions, 41 deletions
diff --git a/grep-cli/src/escape.rs b/grep-cli/src/escape.rs index bf0d5b4f..6d06abb5 100644 --- a/grep-cli/src/escape.rs +++ b/grep-cli/src/escape.rs @@ -95,51 +95,61 @@ pub fn unescape(s: &str) -> Vec<u8> { let mut state = Literal; for c in s.chars() { match state { - Escape => { - match c { - '\\' => { bytes.push(b'\\'); state = Literal; } - 'n' => { bytes.push(b'\n'); state = Literal; } - 'r' => { bytes.push(b'\r'); state = Literal; } - 't' => { bytes.push(b'\t'); state = Literal; } - 'x' => { state = HexFirst; } - c => { - bytes.extend(format!(r"\{}", c).into_bytes()); - state = Literal; - } + Escape => match c { + '\\' => { + bytes.push(b'\\'); + state = Literal; } - } - HexFirst => { - match c { - '0'..='9' | 'A'..='F' | 'a'..='f' => { - state = HexSecond(c); - } - c => { - bytes.extend(format!(r"\x{}", c).into_bytes()); - state = Literal; - } + 'n' => { + bytes.push(b'\n'); + state = Literal; } - } - HexSecond(first) => { - match c { - '0'..='9' | 'A'..='F' | 'a'..='f' => { - let ordinal = format!("{}{}", first, c); - let byte = u8::from_str_radix(&ordinal, 16).unwrap(); - bytes.push(byte); - state = Literal; - } - c => { - let original = format!(r"\x{}{}", first, c); - bytes.extend(original.into_bytes()); - state = Literal; - } + 'r' => { + bytes.push(b'\r'); + state = Literal; } - } - Literal => { - match c { - '\\' => { state = Escape; } - c => { bytes.extend(c.to_string().as_bytes()); } + 't' => { + bytes.push(b'\t'); + state = Literal; } - } + 'x' => { + state = HexFirst; + } + c => { + bytes.extend(format!(r"\{}", c).into_bytes()); + state = Literal; + } + }, + HexFirst => match c { + '0'..='9' | 'A'..='F' | 'a'..='f' => { + state = HexSecond(c); + } + c => { + bytes.extend(format!(r"\x{}", c).into_bytes()); + state = Literal; + } + }, + HexSecond(first) => match c { + '0'..='9' | 'A'..='F' | 'a'..='f' => { + let ordinal = format!("{}{}", first, c); + let byte = u8::from_str_radix(&ordinal, 16).unwrap(); + bytes.push(byte); + state = Literal; + } + c => { + let original = format!(r"\x{}{}", first, c); + bytes.extend(original.into_bytes()); + state = Literal; + } + }, + Literal => match c { + '\\' => { + state = Escape; + } + c => { + bytes.extend(c.to_string().as_bytes()); + } + }, } } match state { |