@@ -482,6 +482,24 @@ fn is_ascii_printable(byte: u8) -> bool {
482
482
c. is_ascii ( ) && !c. is_ascii_control ( )
483
483
}
484
484
485
+ #[ inline]
486
+ fn format_octal ( byte : u8 , buf : & mut [ u8 ; 3 ] ) -> & str {
487
+ * buf = [ b' ' , b' ' , b'0' ] ;
488
+
489
+ let mut num = byte;
490
+ let mut idx = 2 ; // Start at the last position in the buffer
491
+
492
+ // Generate octal digits
493
+ while num > 0 {
494
+ buf[ idx] = b'0' + num % 8 ;
495
+ num /= 8 ;
496
+ idx = idx. saturating_sub ( 1 ) ;
497
+ }
498
+
499
+ // SAFETY: the operations we do above always land within ascii range.
500
+ unsafe { std:: str:: from_utf8_unchecked ( & buf[ ..] ) }
501
+ }
502
+
485
503
#[ inline]
486
504
fn format_byte ( byte : u8 ) -> String {
487
505
let mut byte = byte;
@@ -519,15 +537,20 @@ fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) -> Result<
519
537
// Obtain the width of the first column from the last byte offset.
520
538
let width = format ! ( "{}" , offset) . len ( ) ;
521
539
540
+ let mut at_byte_buf = itoa:: Buffer :: new ( ) ;
541
+ let mut from_oct = [ 0u8 ; 3 ] ; // for octal conversions
542
+ let mut to_oct = [ 0u8 ; 3 ] ;
543
+
522
544
if params. print_bytes {
523
545
for ( at_byte, from_byte, to_byte) in diffs {
546
+ let at_byte_str = at_byte_buf. format ( at_byte) ;
524
547
writeln ! (
525
548
stdout,
526
- "{:>width$} {:>3o } {:4} {:>3o } {}" ,
527
- at_byte ,
528
- from_byte,
549
+ "{:>width$} {} {:4} {} {}" ,
550
+ at_byte_str ,
551
+ format_octal ( from_byte, & mut from_oct ) ,
529
552
format_byte( from_byte) ,
530
- to_byte,
553
+ format_octal ( to_byte, & mut to_oct ) ,
531
554
format_byte( to_byte) ,
532
555
)
533
556
. map_err ( |e| {
@@ -539,12 +562,13 @@ fn report_verbose_diffs(diffs: Vec<(usize, u8, u8)>, params: &Params) -> Result<
539
562
}
540
563
} else {
541
564
for ( at_byte, from_byte, to_byte) in diffs {
565
+ let at_byte_str = at_byte_buf. format ( at_byte) ;
542
566
writeln ! (
543
567
stdout,
544
- "{:>width$} {:>3o } {:>3o }" ,
545
- at_byte ,
546
- from_byte,
547
- to_byte,
568
+ "{:>width$} {} {}" ,
569
+ at_byte_str ,
570
+ format_octal ( from_byte, & mut from_oct ) ,
571
+ format_octal ( to_byte, & mut to_oct ) ,
548
572
width = width
549
573
)
550
574
. map_err ( |e| {
0 commit comments