-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use custom unescape code and remove dep
- Loading branch information
Showing
5 changed files
with
37 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#[derive(thiserror::Error, Debug)] | ||
#[error("Invalid escape sequence")] | ||
pub struct InvalidEscapeError; | ||
|
||
/// Unescape a udev-escaped devnode name. | ||
pub fn unescape_devnode(escaped: &str) -> Result<String, InvalidEscapeError> { | ||
let mut result = String::with_capacity(escaped.len()); | ||
let mut iter = escaped.chars(); | ||
|
||
while let Some(c) = iter.next() { | ||
if c != '\\' { | ||
result.push(c); | ||
continue; | ||
} | ||
|
||
// Udev escaped devnode names only use hexadecimal escapes, so we only need to handle those. | ||
if iter.next() != Some('x') { | ||
return Err(InvalidEscapeError); | ||
} | ||
|
||
let hex = iter.as_str().get(..2).ok_or(InvalidEscapeError)?; | ||
result.push( | ||
u8::from_str_radix(hex, 16) | ||
.map_err(|_| InvalidEscapeError)? | ||
.into(), | ||
); | ||
iter.next(); | ||
iter.next(); | ||
} | ||
|
||
Ok(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod escape; | ||
pub mod tty_mode_guard; |