Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow negative values for --display-offset #145

Closed
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
67 changes: 62 additions & 5 deletions src/bin/hexyl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
extern crate clap;

use std::convert::TryFrom;
use std::fmt::{self, Display, Formatter};
use std::fs::File;
use std::io::{self, prelude::*, SeekFrom};
use std::num::NonZeroI64;
Expand Down Expand Up @@ -239,10 +240,19 @@ fn run() -> Result<(), AnyhowError> {
let display_offset: u64 = matches
.value_of("display_offset")
.map(|s| {
parse_byte_count(s).context(anyhow!(
"failed to parse `--display-offset` arg {:?} as byte count",
s
))
if skip_arg.is_some() {
parse_byte_offset(s, block_size)?
.checked_add(skip_offset)
.context(anyhow!(
"invalid value when applying `--display-offset` to `--skip`"
))
} else {
let display_offset = parse_byte_count(s).context(anyhow!(
"failed to parse `--display-offset` arg {:?} as byte count",
s
))?;
Ok(display_offset + skip_offset)
}
})
.transpose()?
.unwrap_or(0);
Expand All @@ -251,7 +261,7 @@ fn run() -> Result<(), AnyhowError> {
let mut stdout_lock = stdout.lock();

let mut printer = Printer::new(&mut stdout_lock, show_color, border_style, squeeze);
printer.display_offset(skip_offset + display_offset);
printer.display_offset(display_offset);
printer.print_all(&mut reader).map_err(|e| anyhow!(e))?;

Ok(())
Expand Down Expand Up @@ -393,6 +403,36 @@ struct ByteOffset {
)]
struct NegativeOffsetSpecifiedError;

#[derive(Clone, Debug, ThisError)]
struct OffsetAddError {
base: u64,
offset: u64,
kind: OffsetAddErrorKind,
}

#[derive(Clone, Copy, Debug)]
enum OffsetAddErrorKind {
Overflow,
Underflow,
}

impl Display for OffsetAddError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let &Self { base, offset, kind } = self;

match kind {
OffsetAddErrorKind::Overflow => write!(
f,
"base ({}) + offset ({}) exceeds value range representable by `u64`",
base, offset,
),
OffsetAddErrorKind::Underflow => {
write!(f, "base ({}) - offset ({}) < 0", base, offset)
}
}
}
}

impl ByteOffset {
fn assume_forward_offset_from_start(
&self,
Expand All @@ -405,6 +445,23 @@ impl ByteOffset {
ByteOffsetKind::BackwardFromEnd => Err(NegativeOffsetSpecifiedError),
}
}

fn checked_add(&self, base: u64) -> Result<u64, OffsetAddError> {
let &Self {
value: offset,
kind,
} = self;
let offset = offset.into();
match kind {
ByteOffsetKind::ForwardFromBeginning | ByteOffsetKind::ForwardFromLastOffset => {
base.checked_add(offset).ok_or(OffsetAddErrorKind::Overflow)
}
ByteOffsetKind::BackwardFromEnd => base
.checked_sub(offset)
.ok_or(OffsetAddErrorKind::Underflow),
}
.map_err(|kind| OffsetAddError { base, offset, kind })
}
}

#[derive(Clone, Debug, Eq, PartialEq, ThisError)]
Expand Down