Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 18 additions & 10 deletions src/uu/od/src/input_offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use std::fmt;
use std::io::{self, Write};

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -49,16 +50,7 @@ impl InputOffset {

/// returns a string with the current byte offset
pub fn format_byte_offset(&self) -> String {
match (self.radix, self.label) {
(Radix::Decimal, None) => format!("{:07}", self.byte_pos),
(Radix::Decimal, Some(l)) => format!("{:07} ({l:07})", self.byte_pos),
(Radix::Hexadecimal, None) => format!("{:06x}", self.byte_pos),
(Radix::Hexadecimal, Some(l)) => format!("{:06x} ({l:06x})", self.byte_pos),
(Radix::Octal, None) => format!("{:07o}", self.byte_pos),
(Radix::Octal, Some(l)) => format!("{:07o} ({l:07o})", self.byte_pos),
(Radix::NoPrefix, None) => String::new(),
(Radix::NoPrefix, Some(l)) => format!("({l:07o})"),
}
self.to_string()
}

/// Prints the byte offset followed by a newline, or nothing at all if
Expand All @@ -71,6 +63,22 @@ impl InputOffset {
}
}

/// Writes the current byte offset, as [`InputOffset::format_byte_offset`] returns it.
impl fmt::Display for InputOffset {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match (self.radix, self.label) {
(Radix::Decimal, None) => write!(f, "{:07}", self.byte_pos),
(Radix::Decimal, Some(l)) => write!(f, "{:07} ({l:07})", self.byte_pos),
(Radix::Hexadecimal, None) => write!(f, "{:06x}", self.byte_pos),
(Radix::Hexadecimal, Some(l)) => write!(f, "{:06x} ({l:06x})", self.byte_pos),
(Radix::Octal, None) => write!(f, "{:07o}", self.byte_pos),
(Radix::Octal, Some(l)) => write!(f, "{:07o} ({l:07o})", self.byte_pos),
(Radix::NoPrefix, None) => Ok(()),
(Radix::NoPrefix, Some(l)) => write!(f, "({l:07o})"),
}
}
}

#[test]
fn test_input_offset() {
let mut sut = InputOffset::new(Radix::Hexadecimal, 10, None);
Expand Down
11 changes: 6 additions & 5 deletions src/uu/od/src/od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,12 @@ fn extract_strings_from_input(
// Helper function to format and print a string
let mut print_string = |offset: u64, string: &[u8]| -> std::io::Result<()> {
let string_content = String::from_utf8_lossy(string);
match radix {
Radix::NoPrefix => writeln!(writer, "{string_content}"),
Radix::Decimal => writeln!(writer, "{offset:07} {string_content}"),
Radix::Hexadecimal => writeln!(writer, "{offset:06x} {string_content}"),
Radix::Octal => writeln!(writer, "{offset:07o} {string_content}"),
if radix == Radix::NoPrefix {
writeln!(writer, "{string_content}")
} else {
// Same offset format as the normal dump
let offset = InputOffset::new(radix, offset, None);
writeln!(writer, "{offset} {string_content}")
}
};

Expand Down
Loading