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

fix(tail): add support for negative indexing (#1865)

closes: https://github.com/uutils/coreutils/issues/1860
This commit is contained in:
Yagiz Degirmenci 2021-03-22 12:01:54 +03:00 committed by GitHub
parent 21be280c5a
commit 27b7552ef4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View file

@ -80,6 +80,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.short("c") .short("c")
.long(options::BYTES) .long(options::BYTES)
.takes_value(true) .takes_value(true)
.allow_hyphen_values(true)
.help("Number of bytes to print"), .help("Number of bytes to print"),
) )
.arg( .arg(
@ -93,6 +94,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.short("n") .short("n")
.long(options::LINES) .long(options::LINES)
.takes_value(true) .takes_value(true)
.allow_hyphen_values(true)
.help("Number of lines to print"), .help("Number of lines to print"),
) )
.arg( .arg(
@ -343,9 +345,9 @@ pub fn parse_size(mut size_slice: &str) -> Result<u64, ParseSizeErr> {
// sole B is not a valid suffix // sole B is not a valid suffix
Err(ParseSizeErr::parse_failure(size_slice)) Err(ParseSizeErr::parse_failure(size_slice))
} else { } else {
let value: Option<u64> = size_slice.parse().ok(); let value: Option<i64> = size_slice.parse().ok();
value value
.map(|v| Ok(multiplier * v)) .map(|v| Ok((multiplier as i64 * v.abs()) as u64))
.unwrap_or_else(|| Err(ParseSizeErr::parse_failure(size_slice))) .unwrap_or_else(|| Err(ParseSizeErr::parse_failure(size_slice)))
} }
} }

View file

@ -329,3 +329,17 @@ fn test_multiple_input_quiet_flag_overrides_verbose_flag_for_suppressing_headers
.run() .run()
.stdout_is_fixture("foobar_multiple_quiet.expected"); .stdout_is_fixture("foobar_multiple_quiet.expected");
} }
#[test]
fn test_negative_indexing() {
let positive_lines_index = new_ucmd!().arg("-n").arg("5").arg(FOOBAR_TXT).run();
let negative_lines_index = new_ucmd!().arg("-n").arg("-5").arg(FOOBAR_TXT).run();
let positive_bytes_index = new_ucmd!().arg("-c").arg("20").arg(FOOBAR_TXT).run();
let negative_bytes_index = new_ucmd!().arg("-c").arg("-20").arg(FOOBAR_TXT).run();
assert_eq!(positive_lines_index.stdout, negative_lines_index.stdout);
assert_eq!(positive_bytes_index.stdout, negative_bytes_index.stdout);
}