Skip to content

Commit

Permalink
extract Base enum
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Sep 25, 2023
1 parent 1ad10dd commit 45487d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
32 changes: 24 additions & 8 deletions src/uu/echo/src/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,24 @@ mod options {
pub const DISABLE_BACKSLASH_ESCAPE: &str = "disable_backslash_escape";
}

#[repr(u8)]
#[derive(Clone, Copy)]

Check warning on line 25 in src/uu/echo/src/echo.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/echo/src/echo.rs#L25

Added line #L25 was not covered by tests
enum Base {
Oct = 8,
Hex = 16,
}

impl Base {
fn max_digits(&self) -> u8 {
match self {
Self::Oct => 3,
Self::Hex => 2,
}
}
}

/// Parse the numeric part of the `\xHHH` and `\0NNN` escape sequences
fn parse_code(input: &mut Peekable<Chars>, base: u8, max_digits: u32) -> Option<char> {
fn parse_code(input: &mut Peekable<Chars>, base: Base) -> Option<char> {
// All arithmetic on `ret` needs to be wrapping, because octal input can
// take 3 digits, which is 9 bits, and therefore more than what fits in a
// `u8`. GNU just seems to wrap these values.
Expand All @@ -31,15 +47,15 @@ fn parse_code(input: &mut Peekable<Chars>, base: u8, max_digits: u32) -> Option<
// `u8::MAX` as unicode.
let mut ret = input.peek().and_then(|c| c.to_digit(base as u32))? as u8;

// We can safely ifgnore the None case because we just peeked it.
// We can safely ignore the None case because we just peeked it.
let _ = input.next();

for _ in 1..max_digits {
for _ in 1..base.max_digits() {
match input.peek().and_then(|c| c.to_digit(base as u32)) {
Some(n) => ret = ret.wrapping_mul(base).wrapping_add(n as u8),
Some(n) => ret = ret.wrapping_mul(base as u8).wrapping_add(n as u8),
None => break,
}
// We can safely ifgnore the None case because we just peeked it.
// We can safely ignore the None case because we just peeked it.
let _ = input.next();
}

Expand All @@ -58,7 +74,7 @@ fn print_escaped(input: &str, mut output: impl Write) -> io::Result<bool> {
// Note that '0' is intentionally omitted because that
// would be the \0NNN syntax.
if let Some('1'..='8') = iter.peek() {
if let Some(parsed) = parse_code(&mut iter, 8, 3) {
if let Some(parsed) = parse_code(&mut iter, Base::Oct) {
write!(output, "{parsed}")?;
continue;
}
Expand All @@ -77,14 +93,14 @@ fn print_escaped(input: &str, mut output: impl Write) -> io::Result<bool> {
't' => '\t',
'v' => '\x0b',
'x' => {
if let Some(c) = parse_code(&mut iter, 16, 2) {
if let Some(c) = parse_code(&mut iter, Base::Hex) {
c
} else {
write!(output, "\\")?;
'x'
}
}
'0' => parse_code(&mut iter, 8, 3).unwrap_or('\0'),
'0' => parse_code(&mut iter, Base::Oct).unwrap_or('\0'),
c => {
write!(output, "\\")?;
c
Expand Down
4 changes: 2 additions & 2 deletions tests/by-util/test_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ fn old_octal_syntax() {

new_ucmd!()
.arg("-e")
.arg("\\101foo")
.arg("\\101 foo")
.succeeds()
.stdout_is("Afoo\n");
.stdout_is("A foo\n");

new_ucmd!()
.arg("-e")
Expand Down

0 comments on commit 45487d4

Please sign in to comment.