1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

Fix type-error when calling parse_size from od

This commit is contained in:
Omer Tuchfeld 2022-02-06 21:59:59 +01:00
parent 6856c5dba5
commit 468ff8f0b9
5 changed files with 33 additions and 27 deletions

View file

@ -11,15 +11,15 @@ pub struct InputOffset {
/// The radix to print the byte offset. NoPrefix will not print a byte offset.
radix: Radix,
/// The current position. Initialize at `new`, increase using `increase_position`.
byte_pos: usize,
byte_pos: u64,
/// An optional label printed in parentheses, typically different from `byte_pos`,
/// but will increase with the same value if `byte_pos` in increased.
label: Option<usize>,
label: Option<u64>,
}
impl InputOffset {
/// creates a new `InputOffset` using the provided values.
pub fn new(radix: Radix, byte_pos: usize, label: Option<usize>) -> Self {
pub fn new(radix: Radix, byte_pos: u64, label: Option<u64>) -> Self {
Self {
radix,
byte_pos,
@ -28,7 +28,7 @@ impl InputOffset {
}
/// Increase `byte_pos` and `label` if a label is used.
pub fn increase_position(&mut self, n: usize) {
pub fn increase_position(&mut self, n: u64) {
self.byte_pos += n;
if let Some(l) = self.label {
self.label = Some(l + n);

View file

@ -29,6 +29,7 @@ mod prn_float;
mod prn_int;
use std::cmp;
use std::convert::TryFrom;
use crate::byteorder_io::*;
use crate::formatteriteminfo::*;
@ -111,9 +112,9 @@ pub(crate) mod options {
struct OdOptions {
byte_order: ByteOrder,
skip_bytes: usize,
read_bytes: Option<usize>,
label: Option<usize>,
skip_bytes: u64,
read_bytes: Option<u64>,
label: Option<u64>,
input_strings: Vec<String>,
formats: Vec<ParsedFormatterItemInfo>,
line_bytes: usize,
@ -148,7 +149,7 @@ impl OdOptions {
},
};
let mut label: Option<usize> = None;
let mut label: Option<u64> = None;
let parsed_input = parse_inputs(matches)
.map_err(|e| USimpleError::new(1, format!("Invalid inputs: {}", e)))?;
@ -170,7 +171,8 @@ impl OdOptions {
16
} else {
match parse_number_of_bytes(s) {
Ok(n) => n,
Ok(n) => usize::try_from(n)
.map_err(|_| USimpleError::new(1, format!("{} is too large", s)))?,
Err(e) => {
return Err(USimpleError::new(
1,
@ -569,7 +571,7 @@ where
);
}
input_offset.increase_position(length);
input_offset.increase_position(length as u64);
}
Err(e) => {
show_error!("{}", e);
@ -648,8 +650,8 @@ fn print_bytes(prefix: &str, input_decoder: &MemoryDecoder, output_info: &Output
/// `read_bytes` is an optional limit to the number of bytes to read
fn open_input_peek_reader(
input_strings: &[String],
skip_bytes: usize,
read_bytes: Option<usize>,
skip_bytes: u64,
read_bytes: Option<u64>,
) -> PeekReader<PartialReader<MultifileReader>> {
// should return "impl PeekRead + Read + HasError" when supported in (stable) rust
let inputs = input_strings

View file

@ -32,7 +32,7 @@ impl<'a> CommandLineOpts for ArgMatches {
#[derive(PartialEq, Debug)]
pub enum CommandLineInputs {
FileNames(Vec<String>),
FileAndOffset((String, usize, Option<usize>)),
FileAndOffset((String, u64, Option<u64>)),
}
/// Interprets the command line inputs of od.
@ -141,7 +141,7 @@ pub fn parse_inputs_traditional(input_strings: &[&str]) -> Result<CommandLineInp
}
/// parses format used by offset and label on the command line
pub fn parse_offset_operand(s: &str) -> Result<usize, &'static str> {
pub fn parse_offset_operand(s: &str) -> Result<u64, &'static str> {
let mut start = 0;
let mut len = s.len();
let mut radix = 8;
@ -164,7 +164,7 @@ pub fn parse_offset_operand(s: &str) -> Result<usize, &'static str> {
radix = 10;
}
}
match usize::from_str_radix(&s[start..len], radix) {
match u64::from_str_radix(&s[start..len], radix) {
Ok(i) => Ok(i * multiply),
Err(_) => Err("parse failed"),
}
@ -332,7 +332,7 @@ mod tests {
.unwrap_err();
}
fn parse_offset_operand_str(s: &str) -> Result<usize, &'static str> {
fn parse_offset_operand_str(s: &str) -> Result<u64, &'static str> {
parse_offset_operand(&String::from(s))
}

View file

@ -1,6 +1,6 @@
use uucore::parse_size::{parse_size, ParseSizeError};
pub fn parse_number_of_bytes(s: &str) -> Result<usize, ParseSizeError> {
pub fn parse_number_of_bytes(s: &str) -> Result<u64, ParseSizeError> {
let mut start = 0;
let mut len = s.len();
let mut radix = 16;
@ -65,7 +65,7 @@ pub fn parse_number_of_bytes(s: &str) -> Result<usize, ParseSizeError> {
_ => {}
}
let factor = match usize::from_str_radix(&s[start..len], radix) {
let factor = match u64::from_str_radix(&s[start..len], radix) {
Ok(f) => f,
Err(e) => return Err(ParseSizeError::ParseFailure(e.to_string())),
};

View file

@ -15,15 +15,15 @@ const MAX_SKIP_BUFFER: usize = 16 * 1024;
/// number of bytes.
pub struct PartialReader<R> {
inner: R,
skip: usize,
limit: Option<usize>,
skip: u64,
limit: Option<u64>,
}
impl<R> PartialReader<R> {
/// Create a new `PartialReader` wrapping `inner`, which will skip
/// `skip` bytes, and limits the output to `limit` bytes. Set `limit`
/// to `None` if there should be no limit.
pub fn new(inner: R, skip: usize, limit: Option<usize>) -> Self {
pub fn new(inner: R, skip: u64, limit: Option<u64>) -> Self {
Self { inner, skip, limit }
}
}
@ -34,7 +34,7 @@ impl<R: Read> Read for PartialReader<R> {
let mut bytes = [0; MAX_SKIP_BUFFER];
while self.skip > 0 {
let skip_count = cmp::min(self.skip, MAX_SKIP_BUFFER);
let skip_count: usize = cmp::min(self.skip as usize, MAX_SKIP_BUFFER);
match self.inner.read(&mut bytes[..skip_count])? {
0 => {
@ -44,7 +44,7 @@ impl<R: Read> Read for PartialReader<R> {
"tried to skip past end of input",
));
}
n => self.skip -= n,
n => self.skip -= n as u64,
}
}
}
@ -53,15 +53,15 @@ impl<R: Read> Read for PartialReader<R> {
None => self.inner.read(out),
Some(0) => Ok(0),
Some(ref mut limit) => {
let slice = if *limit > out.len() {
let slice = if *limit > (out.len() as u64) {
out
} else {
&mut out[0..*limit]
&mut out[0..(*limit as usize)]
};
match self.inner.read(slice) {
Err(e) => Err(e),
Ok(r) => {
*limit -= r;
*limit -= r as u64;
Ok(r)
}
}
@ -145,7 +145,11 @@ mod tests {
fn test_read_skipping_huge_number() {
let mut v = [0; 10];
// test if it does not eat all memory....
let mut sut = PartialReader::new(Cursor::new(&b"abcdefgh"[..]), usize::max_value(), None);
let mut sut = PartialReader::new(
Cursor::new(&b"abcdefgh"[..]),
usize::max_value() as u64,
None,
);
sut.read(v.as_mut()).unwrap_err();
}