1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #4988 from sylvestre/cut-huge

cut-huge-range.sh: make cut fails on usize::MAX
This commit is contained in:
Sylvestre Ledru 2023-09-26 11:34:16 +02:00 committed by GitHub
commit 7ebdab694c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View file

@ -38,6 +38,8 @@ impl FromStr for Range {
fn parse(s: &str) -> Result<usize, &'static str> {
match s.parse::<usize>() {
Ok(0) => Err("fields and positions are numbered from 1"),
// GNU fails when we are at the limit. Match their behavior
Ok(n) if n == usize::MAX => Err("byte/character offset is too large"),
Ok(n) => Ok(n),
Err(_) => Err("failed to parse range"),
}
@ -159,6 +161,7 @@ pub fn contain(ranges: &[Range], n: usize) -> bool {
#[cfg(test)]
mod test {
use super::{complement, Range};
use std::str::FromStr;
fn m(a: Vec<Range>, b: &[Range]) {
assert_eq!(Range::merge(a), b);
@ -229,4 +232,33 @@ mod test {
// With start and end
assert_eq!(complement(&[r(1, 4), r(6, usize::MAX - 1)]), vec![r(5, 5)]);
}
#[test]
fn test_from_str() {
assert_eq!(Range::from_str("5"), Ok(Range { low: 5, high: 5 }));
assert_eq!(Range::from_str("3-5"), Ok(Range { low: 3, high: 5 }));
assert_eq!(
Range::from_str("5-3"),
Err("high end of range less than low end")
);
assert_eq!(Range::from_str("-"), Err("invalid range with no endpoint"));
assert_eq!(
Range::from_str("3-"),
Ok(Range {
low: 3,
high: usize::MAX - 1
})
);
assert_eq!(Range::from_str("-5"), Ok(Range { low: 1, high: 5 }));
assert_eq!(
Range::from_str("0"),
Err("fields and positions are numbered from 1")
);
let max_value = format!("{}", usize::MAX);
assert_eq!(
Range::from_str(&max_value),
Err("byte/character offset is too large")
);
}
}

View file

@ -117,6 +117,14 @@ fn test_whitespace_with_char() {
.code_is(1);
}
#[test]
fn test_too_large() {
new_ucmd!()
.args(&["-b1-18446744073709551615", "/dev/null"])
.fails()
.code_is(1);
}
#[test]
fn test_specify_delimiter() {
for param in ["-d", "--delimiter", "--del"] {