Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tr: octal escape #174

Merged
merged 1 commit into from
Jul 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/bin/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ DESCRIPTION
--truncate
-t first truncate string1 to length of string2

*NOTE* octal escapes are not implemented yet

In either string the notation a-b means a range of charac-
ters from a to b in increasing ASCII order. The character
`\' followed by 1, 2 or 3 octal digits stands for the char-
Expand Down Expand Up @@ -130,7 +128,13 @@ impl<'a> Iterator for Unescape<'a> {
// we know that \ is 1 byte long so we can index into the string safely
let c = self.string[1..].chars().next().unwrap();
// do some matching on '0' (or 'x') here
(Some(unescape_char(c)), 1 + c.len_utf8())
if c.is_digit(8) {
// Octal escape
let len = self.string[1..].chars().take(3).take_while(|c| c.is_digit(8)).count();
(Some(char::from(u8::from_str_radix(&self.string[1..1+len], 8).unwrap())), 1 + len)
} else {
(Some(unescape_char(c)), 1 + c.len_utf8())
}
},
c => (Some(c), c.len_utf8()), // not an escape char
};
Expand Down