refactor/od ~ polish spelling (comments, names, and exceptions)

This commit is contained in:
Roy Ivy III 2021-05-30 18:33:37 -05:00
parent 5079972ba6
commit aa385cf23c
3 changed files with 48 additions and 51 deletions

View file

@ -5,6 +5,7 @@
// * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code.
// spell-checker:ignore (clap) DontDelimitTrailingValues
// spell-checker:ignore (ToDO) formatteriteminfo inputdecoder inputoffset mockstream nrofbytes partialreader odfunc multifile exitcode
#[macro_use]
@ -455,7 +456,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
]);
let clap_matches = clap_opts
.clone() // Clone to reuse clap_otps to print help
.clone() // Clone to reuse clap_opts to print help
.get_matches_from(args.clone());
if clap_matches.is_present(options::VERSION) {

View file

@ -1,5 +1,3 @@
// spell-checker:ignore (ToDO) CHRS itembytes MUTF
use std::str::from_utf8;
use crate::formatteriteminfo::*;
@ -16,7 +14,7 @@ pub static FORMAT_ITEM_C: FormatterItemInfo = FormatterItemInfo {
formatter: FormatWriter::MultibyteWriter(format_item_c),
};
static A_CHRS: [&str; 128] = [
static A_CHARS: [&str; 128] = [
"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", "bs", "ht", "nl", "vt", "ff", "cr",
"so", "si", "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", "can", "em", "sub", "esc",
"fs", "gs", "rs", "us", "sp", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-",
@ -28,12 +26,12 @@ static A_CHRS: [&str; 128] = [
];
fn format_item_a(p: u64) -> String {
// itembytes == 1
// item-bytes == 1
let b = (p & 0x7f) as u8;
format!("{:>4}", A_CHRS.get(b as usize).unwrap_or(&"??"))
format!("{:>4}", A_CHARS.get(b as usize).unwrap_or(&"??"))
}
static C_CHRS: [&str; 128] = [
static C_CHARS: [&str; 128] = [
"\\0", "001", "002", "003", "004", "005", "006", "\\a", "\\b", "\\t", "\\n", "\\v", "\\f",
"\\r", "016", "017", "020", "021", "022", "023", "024", "025", "026", "027", "030", "031",
"032", "033", "034", "035", "036", "037", " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")",
@ -45,11 +43,11 @@ static C_CHRS: [&str; 128] = [
];
fn format_item_c(bytes: &[u8]) -> String {
// itembytes == 1
// item-bytes == 1
let b = bytes[0];
if b & 0x80 == 0x00 {
match C_CHRS.get(b as usize) {
match C_CHARS.get(b as usize) {
Some(s) => format!("{:>4}", s),
None => format!("{:>4}", b),
}
@ -86,7 +84,7 @@ pub fn format_ascii_dump(bytes: &[u8]) -> String {
result.push('>');
for c in bytes.iter() {
if *c >= 0x20 && *c <= 0x7e {
result.push_str(C_CHRS[*c as usize]);
result.push_str(C_CHARS[*c as usize]);
} else {
result.push('.');
}
@ -136,12 +134,12 @@ fn test_format_item_c() {
format_item_c(&[0xf0, 0x9f, 0x92, 0x96, 0x21])
);
assert_eq!(" 300", format_item_c(&[0xc0, 0x80])); // invalid utf-8 (MUTF-8 null)
assert_eq!(" 300", format_item_c(&[0xc0, 0x80])); // invalid utf-8 (UTF-8 null)
assert_eq!(" 301", format_item_c(&[0xc1, 0xa1])); // invalid utf-8
assert_eq!(" 303", format_item_c(&[0xc3, 0xc3])); // invalid utf-8
assert_eq!(" 360", format_item_c(&[0xf0, 0x82, 0x82, 0xac])); // invalid utf-8 (overlong)
assert_eq!(" 360", format_item_c(&[0xf0, 0x9f, 0x92])); // invalid utf-8 (missing octet)
assert_eq!(" \u{10FFFD}", format_item_c(&[0xf4, 0x8f, 0xbf, 0xbd])); // largest valid utf-8
assert_eq!(" \u{10FFFD}", format_item_c(&[0xf4, 0x8f, 0xbf, 0xbd])); // largest valid utf-8 // spell-checker:ignore 10FFFD FFFD
assert_eq!(" 364", format_item_c(&[0xf4, 0x90, 0x00, 0x00])); // invalid utf-8
assert_eq!(" 365", format_item_c(&[0xf5, 0x80, 0x80, 0x80])); // invalid utf-8
assert_eq!(" 377", format_item_c(&[0xff])); // invalid utf-8

View file

@ -1,5 +1,3 @@
// spell-checker:ignore (ToDO) itembytes
use crate::formatteriteminfo::*;
/// format string to print octal using `int_writer_unsigned`
@ -60,9 +58,9 @@ macro_rules! int_writer_signed {
};
}
/// Extends a signed number in `item` of `itembytes` bytes into a (signed) i64
fn sign_extend(item: u64, itembytes: usize) -> i64 {
let shift = 64 - itembytes * 8;
/// Extends a signed number in `item` of `item_bytes` bytes into a (signed) i64
fn sign_extend(item: u64, item_bytes: usize) -> i64 {
let shift = 64 - item_bytes * 8;
(item << shift) as i64 >> shift
}
@ -89,46 +87,46 @@ int_writer_signed!(FORMAT_ITEM_DEC64S, 8, 21, format_item_dec_s64, DEC!()); // m
#[test]
fn test_sign_extend() {
assert_eq!(
0xffffffffffffff80u64 as i64,
sign_extend(0x0000000000000080, 1)
0xffff_ffff_ffff_ff80u64 as i64,
sign_extend(0x0000_0000_0000_0080, 1)
);
assert_eq!(
0xffffffffffff8000u64 as i64,
sign_extend(0x0000000000008000, 2)
0xffff_ffff_ffff_8000u64 as i64,
sign_extend(0x0000_0000_0000_8000, 2)
);
assert_eq!(
0xffffffffff800000u64 as i64,
sign_extend(0x0000000000800000, 3)
0xffff_ffff_ff80_0000u64 as i64,
sign_extend(0x0000_0000_0080_0000, 3)
);
assert_eq!(
0xffffffff80000000u64 as i64,
sign_extend(0x0000000080000000, 4)
0xffff_ffff_8000_0000u64 as i64,
sign_extend(0x0000_0000_8000_0000, 4)
);
assert_eq!(
0xffffff8000000000u64 as i64,
sign_extend(0x0000008000000000, 5)
0xffff_ff80_0000_0000u64 as i64,
sign_extend(0x0000_0080_0000_0000, 5)
);
assert_eq!(
0xffff800000000000u64 as i64,
sign_extend(0x0000800000000000, 6)
0xffff_8000_0000_0000u64 as i64,
sign_extend(0x0000_8000_0000_0000, 6)
);
assert_eq!(
0xff80000000000000u64 as i64,
sign_extend(0x0080000000000000, 7)
0xff80_0000_0000_0000u64 as i64,
sign_extend(0x0080_0000_0000_0000, 7)
);
assert_eq!(
0x8000000000000000u64 as i64,
sign_extend(0x8000000000000000, 8)
0x8000_0000_0000_0000u64 as i64,
sign_extend(0x8000_0000_0000_0000, 8)
);
assert_eq!(0x000000000000007f, sign_extend(0x000000000000007f, 1));
assert_eq!(0x0000000000007fff, sign_extend(0x0000000000007fff, 2));
assert_eq!(0x00000000007fffff, sign_extend(0x00000000007fffff, 3));
assert_eq!(0x000000007fffffff, sign_extend(0x000000007fffffff, 4));
assert_eq!(0x0000007fffffffff, sign_extend(0x0000007fffffffff, 5));
assert_eq!(0x00007fffffffffff, sign_extend(0x00007fffffffffff, 6));
assert_eq!(0x007fffffffffffff, sign_extend(0x007fffffffffffff, 7));
assert_eq!(0x7fffffffffffffff, sign_extend(0x7fffffffffffffff, 8));
assert_eq!(0x0000_0000_0000_007f, sign_extend(0x0000_0000_0000_007f, 1));
assert_eq!(0x0000_0000_0000_7fff, sign_extend(0x0000_0000_0000_7fff, 2));
assert_eq!(0x0000_0000_007f_ffff, sign_extend(0x0000_0000_007f_ffff, 3));
assert_eq!(0x0000_0000_7fff_ffff, sign_extend(0x0000_0000_7fff_ffff, 4));
assert_eq!(0x0000_007f_ffff_ffff, sign_extend(0x0000_007f_ffff_ffff, 5));
assert_eq!(0x0000_7fff_ffff_ffff, sign_extend(0x0000_7fff_ffff_ffff, 6));
assert_eq!(0x007f_ffff_ffff_ffff, sign_extend(0x007f_ffff_ffff_ffff, 7));
assert_eq!(0x7fff_ffff_ffff_ffff, sign_extend(0x7fff_ffff_ffff_ffff, 8));
}
#[test]
@ -138,11 +136,11 @@ fn test_format_item_oct() {
assert_eq!(" 000000", format_item_oct16(0));
assert_eq!(" 177777", format_item_oct16(0xffff));
assert_eq!(" 00000000000", format_item_oct32(0));
assert_eq!(" 37777777777", format_item_oct32(0xffffffff));
assert_eq!(" 37777777777", format_item_oct32(0xffff_ffff));
assert_eq!(" 0000000000000000000000", format_item_oct64(0));
assert_eq!(
" 1777777777777777777777",
format_item_oct64(0xffffffffffffffff)
format_item_oct64(0xffff_ffff_ffff_ffff)
);
}
@ -153,9 +151,9 @@ fn test_format_item_hex() {
assert_eq!(" 0000", format_item_hex16(0));
assert_eq!(" ffff", format_item_hex16(0xffff));
assert_eq!(" 00000000", format_item_hex32(0));
assert_eq!(" ffffffff", format_item_hex32(0xffffffff));
assert_eq!(" ffffffff", format_item_hex32(0xffff_ffff));
assert_eq!(" 0000000000000000", format_item_hex64(0));
assert_eq!(" ffffffffffffffff", format_item_hex64(0xffffffffffffffff));
assert_eq!(" ffffffffffffffff", format_item_hex64(0xffff_ffff_ffff_ffff));
}
#[test]
@ -165,11 +163,11 @@ fn test_format_item_dec_u() {
assert_eq!(" 0", format_item_dec_u16(0));
assert_eq!(" 65535", format_item_dec_u16(0xffff));
assert_eq!(" 0", format_item_dec_u32(0));
assert_eq!(" 4294967295", format_item_dec_u32(0xffffffff));
assert_eq!(" 4294967295", format_item_dec_u32(0xffff_ffff));
assert_eq!(" 0", format_item_dec_u64(0));
assert_eq!(
" 18446744073709551615",
format_item_dec_u64(0xffffffffffffffff)
format_item_dec_u64(0xffff_ffff_ffff_ffff)
);
}
@ -182,15 +180,15 @@ fn test_format_item_dec_s() {
assert_eq!(" 32767", format_item_dec_s16(0x7fff));
assert_eq!(" -32768", format_item_dec_s16(0x8000));
assert_eq!(" 0", format_item_dec_s32(0));
assert_eq!(" 2147483647", format_item_dec_s32(0x7fffffff));
assert_eq!(" -2147483648", format_item_dec_s32(0x80000000));
assert_eq!(" 2147483647", format_item_dec_s32(0x7fff_ffff));
assert_eq!(" -2147483648", format_item_dec_s32(0x8000_0000));
assert_eq!(" 0", format_item_dec_s64(0));
assert_eq!(
" 9223372036854775807",
format_item_dec_s64(0x7fffffffffffffff)
format_item_dec_s64(0x7fff_ffff_ffff_ffff)
);
assert_eq!(
" -9223372036854775808",
format_item_dec_s64(0x8000000000000000)
format_item_dec_s64(0x8000_0000_0000_0000)
);
}