BREAKING CHANGE: improvement(lib): use u64 for Printer::display_offset and Squeezer::process, not usize

Technically a breaking change for architectures where
`size_of::<usize>() > size_of::<u64>()`, but that strikes me as
extremely unlikely.

This change is important because most file seek offsets, even on older
architectures with word sizes smaller than `u32`, accept a `u64`. In
particular, Rust's own `std` acknowledges this. So, we're actually going
against prior art in Rust by using a `usize`.
This commit is contained in:
Erich Gubler authored and David Peter committed 2020-06-07 11:08:01 +02:00
1 parent c5c8d1190f
commit 6d5c61b671
3 files changed
+22 -20

No files matched your search

+1 -1
View File
@@ -149,7 +149,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
let mut stdout_lock = stdout.lock(); let mut stdout_lock = stdout.lock();
let mut printer = Printer::new(&mut stdout_lock, show_color, border_style, squeeze); let mut printer = Printer::new(&mut stdout_lock, show_color, border_style, squeeze);
printer.display_offset(display_offset as usize); printer.display_offset(display_offset);
printer.print_all(&mut reader)?; printer.print_all(&mut reader)?;
Ok(()) Ok(())
+4 -4
View File
@@ -142,7 +142,7 @@ impl BorderStyle {
} }
pub struct Printer<'a, Writer: Write> { pub struct Printer<'a, Writer: Write> {
idx: usize, idx: u64,
/// The raw bytes used as input for the current line. /// The raw bytes used as input for the current line.
raw_line: Vec<u8>, raw_line: Vec<u8>,
/// The buffered line built with each byte, ready to print to writer. /// The buffered line built with each byte, ready to print to writer.
@@ -154,7 +154,7 @@ pub struct Printer<'a, Writer: Write> {
byte_hex_table: Vec<String>, byte_hex_table: Vec<String>,
byte_char_table: Vec<String>, byte_char_table: Vec<String>,
squeezer: Squeezer, squeezer: Squeezer,
display_offset: usize, display_offset: u64,
} }
impl<'a, Writer: Write> Printer<'a, Writer> { impl<'a, Writer: Write> Printer<'a, Writer> {
@@ -197,7 +197,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
} }
} }
pub fn display_offset(&mut self, display_offset: usize) -> &mut Self { pub fn display_offset(&mut self, display_offset: u64) -> &mut Self {
self.display_offset = display_offset; self.display_offset = display_offset;
self self
} }
@@ -247,7 +247,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
} }
let style = COLOR_OFFSET.normal(); let style = COLOR_OFFSET.normal();
let byte_index = format!("{:08x}", (self.idx - 1) + self.display_offset); let byte_index = format!("{:08x}", self.idx - 1 + self.display_offset);
let formatted_string = if self.show_color { let formatted_string = if self.show_color {
format!("{}", style.paint(byte_index)) format!("{}", style.paint(byte_index))
} else { } else {
+17 -15
View File
@@ -32,7 +32,7 @@ pub enum SqueezeAction {
} }
/// line size /// line size
const LSIZE: usize = 16; const LSIZE: u64 = 16;
impl Squeezer { impl Squeezer {
pub fn new(enabled: bool) -> Squeezer { pub fn new(enabled: bool) -> Squeezer {
@@ -46,7 +46,7 @@ impl Squeezer {
} }
} }
pub fn process(&mut self, b: u8, i: usize) { pub fn process(&mut self, b: u8, i: u64) {
use self::SqueezeState::*; use self::SqueezeState::*;
if self.state == Disabled { if self.state == Disabled {
return; return;
@@ -111,10 +111,12 @@ impl Squeezer {
mod tests { mod tests {
use super::*; use super::*;
const LSIZE_USIZE: usize = LSIZE as usize;
#[test] #[test]
fn three_same_lines() { fn three_same_lines() {
const LINES: usize = 3; const LINES: usize = 3;
let v = vec![0u8; LINES * LSIZE]; let v = vec![0u8; LINES * LSIZE_USIZE];
let mut s = Squeezer::new(true); let mut s = Squeezer::new(true);
// just initialized // just initialized
assert_eq!(s.action(), SqueezeAction::Ignore); assert_eq!(s.action(), SqueezeAction::Ignore);
@@ -128,7 +130,7 @@ mod tests {
let mut line = 0; let mut line = 0;
let mut idx = 1; let mut idx = 1;
for z in v.chunks(LSIZE) { for z in v.chunks(LSIZE_USIZE) {
for i in z { for i in z {
s.process(*i, idx); s.process(*i, idx);
idx += 1; idx += 1;
@@ -143,7 +145,7 @@ mod tests {
#[test] #[test]
fn incomplete_while_squeeze() { fn incomplete_while_squeeze() {
// fourth line only has 12 bytes and should be printed // fourth line only has 12 bytes and should be printed
let v = vec![0u8; 3 * LSIZE + 12]; let v = vec![0u8; 3 * LSIZE_USIZE + 12];
let mut s = Squeezer::new(true); let mut s = Squeezer::new(true);
// just initialized // just initialized
assert_eq!(s.action(), SqueezeAction::Ignore); assert_eq!(s.action(), SqueezeAction::Ignore);
@@ -158,7 +160,7 @@ mod tests {
let mut line = 0; let mut line = 0;
let mut idx = 1; let mut idx = 1;
for z in v.chunks(LSIZE) { for z in v.chunks(LSIZE_USIZE) {
for i in z { for i in z {
s.process(*i, idx); s.process(*i, idx);
idx += 1; idx += 1;
@@ -190,7 +192,7 @@ mod tests {
let mut line = 0; let mut line = 0;
let mut idx = 1; let mut idx = 1;
for z in v.chunks(LSIZE) { for z in v.chunks(LSIZE_USIZE) {
for i in z { for i in z {
s.process(*i, idx); s.process(*i, idx);
idx += 1; idx += 1;
@@ -207,7 +209,7 @@ mod tests {
/// print /// print
fn one_squeeze_no_delete() { fn one_squeeze_no_delete() {
const LINES: usize = 3; const LINES: usize = 3;
let mut v = vec![0u8; (LINES - 1) * LSIZE]; let mut v = vec![0u8; (LINES - 1) * LSIZE_USIZE];
v.extend(vec![1u8; 16]); v.extend(vec![1u8; 16]);
let mut s = Squeezer::new(true); let mut s = Squeezer::new(true);
@@ -223,7 +225,7 @@ mod tests {
let mut line = 0; let mut line = 0;
let mut idx = 1; let mut idx = 1;
for z in v.chunks(LSIZE) { for z in v.chunks(LSIZE_USIZE) {
for i in z { for i in z {
s.process(*i, idx); s.process(*i, idx);
idx += 1; idx += 1;
@@ -239,7 +241,7 @@ mod tests {
/// First line all eq, 2nd half eq with first line, then change /// First line all eq, 2nd half eq with first line, then change
fn second_line_different() { fn second_line_different() {
const LINES: usize = 2; const LINES: usize = 2;
let mut v = vec![0u8; (LINES - 1) * LSIZE]; let mut v = vec![0u8; (LINES - 1) * LSIZE_USIZE];
v.extend(vec![0u8; 8]); v.extend(vec![0u8; 8]);
v.extend(vec![1u8; 8]); v.extend(vec![1u8; 8]);
@@ -255,7 +257,7 @@ mod tests {
let mut line = 0; let mut line = 0;
let mut idx = 1; let mut idx = 1;
for z in v.chunks(LSIZE) { for z in v.chunks(LSIZE_USIZE) {
for i in z { for i in z {
s.process(*i, idx); s.process(*i, idx);
idx += 1; idx += 1;
@@ -291,7 +293,7 @@ mod tests {
let mut line = 0; let mut line = 0;
let mut idx = 1; let mut idx = 1;
for z in v.chunks(LSIZE) { for z in v.chunks(LSIZE_USIZE) {
for i in z { for i in z {
s.process(*i, idx); s.process(*i, idx);
idx += 1; idx += 1;
@@ -341,7 +343,7 @@ mod tests {
let mut line = 0; let mut line = 0;
let mut idx = 1; let mut idx = 1;
for z in v.chunks(LSIZE) { for z in v.chunks(LSIZE_USIZE) {
for i in z { for i in z {
s.process(*i, idx); s.process(*i, idx);
idx += 1; idx += 1;
@@ -377,7 +379,7 @@ mod tests {
let mut line = 0; let mut line = 0;
let mut idx = 1; let mut idx = 1;
for z in v.chunks(LSIZE) { for z in v.chunks(LSIZE_USIZE) {
for i in z { for i in z {
s.process(*i, idx); s.process(*i, idx);
idx += 1; idx += 1;
@@ -410,7 +412,7 @@ mod tests {
let mut line = 0; let mut line = 0;
let mut idx = 1; let mut idx = 1;
for z in v.chunks(LSIZE) { for z in v.chunks(LSIZE_USIZE) {
for i in z { for i in z {
s.process(*i, idx); s.process(*i, idx);
idx += 1; idx += 1;