od: allow trailing characters in address radix

This commit is contained in:
Andrew Liebenow 2024-08-31 14:13:31 -05:00
parent 8fe93ded74
commit 5ca995b21f
2 changed files with 31 additions and 22 deletions

View file

@ -5,6 +5,7 @@
// spell-checker:ignore (clap) dont
// spell-checker:ignore (ToDO) formatteriteminfo inputdecoder inputoffset mockstream nrofbytes partialreader odfunc multifile exitcode
// spell-checker:ignore Anone
mod byteorder_io;
mod formatteriteminfo;
@ -169,29 +170,24 @@ impl OdOptions {
let radix = match matches.get_one::<String>(options::ADDRESS_RADIX) {
None => Radix::Octal,
Some(s) => {
// Other implementations of od only check the first character of this argument's value.
// This means executing "od -Anone" is equivalent to "od -An".
// Existing users of od rely on this behavior:
// https://github.com/landley/toybox/blob/d50372cad35d5dd12e6391c3c7c901a96122dc67/scripts/make.sh#L239
// https://github.com/google/jsonnet/blob/913281d203578bb394995bacc792f2576371e06c/Makefile#L212
let st = s.as_bytes();
if st.len() == 1 {
let radix: char = *(st
.first()
.expect("byte string of length 1 lacks a 0th elem"))
as char;
match radix {
'd' => Radix::Decimal,
'x' => Radix::Hexadecimal,
'o' => Radix::Octal,
'n' => Radix::NoPrefix,
_ => {
return Err(USimpleError::new(
1,
"Radix must be one of [d, o, n, x]".to_string(),
))
}
let radix: char = *(st.first().expect("should be caught by clap")) as char;
match radix {
'd' => Radix::Decimal,
'x' => Radix::Hexadecimal,
'o' => Radix::Octal,
'n' => Radix::NoPrefix,
_ => {
return Err(USimpleError::new(
1,
"Radix must be one of [d, o, n, x]".to_string(),
))
}
} else {
return Err(USimpleError::new(
1,
"Radix must be one of [d, o, n, x]".to_string(),
));
}
}
};

View file

@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore abcdefghijklmnopqrstuvwxyz
// spell-checker:ignore abcdefghijklmnopqrstuvwxyz Anone
use crate::common::util::TestScenario;
use unindent::unindent;
@ -579,6 +579,19 @@ fn test_invalid_offset() {
new_ucmd!().arg("-Ab").fails();
}
#[test]
fn test_offset_compatibility() {
let input = [0u8; 4];
let expected_output = " 000000 000000\n";
new_ucmd!()
.arg("-Anone")
.run_piped_stdin(input)
.no_stderr()
.success()
.stdout_is(expected_output);
}
#[test]
fn test_skip_bytes() {
let input = "abcdefghijklmnopq";