mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
head: return to parse::<usize> and switch to parse_size_u64_max
This commit is contained in:
parent
1ffaf2d629
commit
cc1112660a
2 changed files with 19 additions and 26 deletions
|
@ -4,7 +4,7 @@
|
||||||
// file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
|
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use uucore::parser::parse_size::{ParseSizeError, parse_size_u64};
|
use uucore::parser::parse_size::{ParseSizeError, parse_size_u64_max};
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug)]
|
#[derive(PartialEq, Eq, Debug)]
|
||||||
pub struct ParseError;
|
pub struct ParseError;
|
||||||
|
@ -49,10 +49,11 @@ fn process_num_block(
|
||||||
last_char: char,
|
last_char: char,
|
||||||
chars: &mut std::str::CharIndices,
|
chars: &mut std::str::CharIndices,
|
||||||
) -> Option<Result<Vec<OsString>, ParseError>> {
|
) -> Option<Result<Vec<OsString>, ParseError>> {
|
||||||
let num = src.chars().fold(0u32, |acc, ch| {
|
let num = match src.parse::<usize>() {
|
||||||
acc.saturating_mul(10)
|
Ok(n) => n,
|
||||||
.saturating_add(ch.to_digit(10).unwrap())
|
Err(e) if *e.kind() == std::num::IntErrorKind::PosOverflow => usize::MAX,
|
||||||
});
|
_ => return Some(Err(ParseError)),
|
||||||
|
};
|
||||||
let mut quiet = false;
|
let mut quiet = false;
|
||||||
let mut verbose = false;
|
let mut verbose = false;
|
||||||
let mut zero_terminated = false;
|
let mut zero_terminated = false;
|
||||||
|
@ -129,7 +130,7 @@ pub fn parse_num(src: &str) -> Result<(u64, bool), ParseSizeError> {
|
||||||
if trimmed_string.is_empty() {
|
if trimmed_string.is_empty() {
|
||||||
Ok((0, all_but_last))
|
Ok((0, all_but_last))
|
||||||
} else {
|
} else {
|
||||||
parse_size_u64(trimmed_string).map(|n| (n, all_but_last))
|
parse_size_u64_max(trimmed_string).map(|n| (n, all_but_last))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,11 +194,11 @@ mod tests {
|
||||||
fn test_parse_obsolete_overflow_x64() {
|
fn test_parse_obsolete_overflow_x64() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
obsolete("-1000000000000000m"),
|
obsolete("-1000000000000000m"),
|
||||||
obsolete_result(&["-c", "4294967295"])
|
obsolete_result(&["-c", "18446744073709551615"])
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
obsolete("-10000000000000000000000"),
|
obsolete("-10000000000000000000000"),
|
||||||
obsolete_result(&["-n", "4294967295"])
|
obsolete_result(&["-n", "18446744073709551615"])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -321,24 +321,20 @@ fn test_bad_utf8_lines() {
|
||||||
fn test_head_invalid_num() {
|
fn test_head_invalid_num() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["-c", "1024R", "emptyfile.txt"])
|
.args(&["-c", "1024R", "emptyfile.txt"])
|
||||||
.fails()
|
.succeeds()
|
||||||
.stderr_is(
|
.no_output();
|
||||||
"head: invalid number of bytes: '1024R': Value too large for defined data type\n",
|
|
||||||
);
|
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["-n", "1024R", "emptyfile.txt"])
|
.args(&["-n", "1024R", "emptyfile.txt"])
|
||||||
.fails()
|
.succeeds()
|
||||||
.stderr_is(
|
.no_output();
|
||||||
"head: invalid number of lines: '1024R': Value too large for defined data type\n",
|
|
||||||
);
|
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["-c", "1Y", "emptyfile.txt"])
|
.args(&["-c", "1Y", "emptyfile.txt"])
|
||||||
.fails()
|
.succeeds()
|
||||||
.stderr_is("head: invalid number of bytes: '1Y': Value too large for defined data type\n");
|
.no_output();
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["-n", "1Y", "emptyfile.txt"])
|
.args(&["-n", "1Y", "emptyfile.txt"])
|
||||||
.fails()
|
.succeeds()
|
||||||
.stderr_is("head: invalid number of lines: '1Y': Value too large for defined data type\n");
|
.no_output();
|
||||||
#[cfg(target_pointer_width = "32")]
|
#[cfg(target_pointer_width = "32")]
|
||||||
{
|
{
|
||||||
let sizes = ["1000G", "10T"];
|
let sizes = ["1000G", "10T"];
|
||||||
|
@ -350,10 +346,7 @@ fn test_head_invalid_num() {
|
||||||
{
|
{
|
||||||
let sizes = ["-1000G", "-10T"];
|
let sizes = ["-1000G", "-10T"];
|
||||||
for size in &sizes {
|
for size in &sizes {
|
||||||
new_ucmd!()
|
new_ucmd!().args(&["-c", size]).succeeds().no_output();
|
||||||
.args(&["-c", size])
|
|
||||||
.fails()
|
|
||||||
.stderr_is("head: out of range integral type conversion attempted: number of -bytes or -lines is too large\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
|
@ -778,8 +771,7 @@ fn test_value_too_large() {
|
||||||
|
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.args(&["-n", format!("{MAX}0").as_str(), "lorem_ipsum.txt"])
|
.args(&["-n", format!("{MAX}0").as_str(), "lorem_ipsum.txt"])
|
||||||
.fails()
|
.succeeds();
|
||||||
.stderr_contains("Value too large for defined data type");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue