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:
parent
21be280c5a
commit
27b7552ef4
2 changed files with 18 additions and 2 deletions
|
@ -80,6 +80,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
.short("c")
|
||||
.long(options::BYTES)
|
||||
.takes_value(true)
|
||||
.allow_hyphen_values(true)
|
||||
.help("Number of bytes to print"),
|
||||
)
|
||||
.arg(
|
||||
|
@ -93,6 +94,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
.short("n")
|
||||
.long(options::LINES)
|
||||
.takes_value(true)
|
||||
.allow_hyphen_values(true)
|
||||
.help("Number of lines to print"),
|
||||
)
|
||||
.arg(
|
||||
|
@ -343,9 +345,9 @@ pub fn parse_size(mut size_slice: &str) -> Result<u64, ParseSizeErr> {
|
|||
// sole B is not a valid suffix
|
||||
Err(ParseSizeErr::parse_failure(size_slice))
|
||||
} else {
|
||||
let value: Option<u64> = size_slice.parse().ok();
|
||||
let value: Option<i64> = size_slice.parse().ok();
|
||||
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)))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -329,3 +329,17 @@ fn test_multiple_input_quiet_flag_overrides_verbose_flag_for_suppressing_headers
|
|||
.run()
|
||||
.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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue