From d52fbe5e5001adf3cc38bccf3458f79307ae90d7 Mon Sep 17 00:00:00 2001 From: Yunus Ozen <94426948+ozenyunus35@users.noreply.github.com> Date: Fri, 23 Oct 2026 01:10:46 +0300 Subject: [PATCH] od: print -S offsets through InputOffset Follow-up to #15625: the strings mode had its own copy of the offset formats. InputOffset now implements Display, format_byte_offset uses it, and -S writes the offset through it without an extra allocation. --- src/uu/od/src/input_offset.rs | 28 ++++++++++++++++++---------- src/uu/od/src/od.rs | 11 ++++++----- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/uu/od/src/input_offset.rs b/src/uu/od/src/input_offset.rs index 6411b9d678b..6e5e36b5579 100644 --- a/src/uu/od/src/input_offset.rs +++ b/src/uu/od/src/input_offset.rs @@ -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)] @@ -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 @@ -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); diff --git a/src/uu/od/src/od.rs b/src/uu/od/src/od.rs index 79e497b1d31..badc58b00ee 100644 --- a/src/uu/od/src/od.rs +++ b/src/uu/od/src/od.rs @@ -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}") } };