mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
join: "support" field numbers larger than usize::MAX
They silently get folded to usize::MAX, which is the official GNU behavior.
This commit is contained in:
parent
ec386fa460
commit
ce3df12eaa
3 changed files with 51 additions and 0 deletions
|
@ -809,8 +809,13 @@ fn get_field_number(keys: Option<usize>, key: Option<usize>) -> UResult<usize> {
|
||||||
/// Parse the specified field string as a natural number and return
|
/// Parse the specified field string as a natural number and return
|
||||||
/// the zero-based field number.
|
/// the zero-based field number.
|
||||||
fn parse_field_number(value: &str) -> UResult<usize> {
|
fn parse_field_number(value: &str) -> UResult<usize> {
|
||||||
|
// TODO: use ParseIntError.kind() once MSRV >= 1.55
|
||||||
|
// For now, store an overflow Err from parsing a value 10x 64 bit usize::MAX
|
||||||
|
// Adapted from https://github.com/rust-lang/rust/issues/22639
|
||||||
|
let overflow = "184467440737095516150".parse::<usize>().err().unwrap();
|
||||||
match value.parse::<usize>() {
|
match value.parse::<usize>() {
|
||||||
Ok(result) if result > 0 => Ok(result - 1),
|
Ok(result) if result > 0 => Ok(result - 1),
|
||||||
|
Err(ref e) if *e == overflow => Ok(usize::MAX),
|
||||||
_ => Err(USimpleError::new(
|
_ => Err(USimpleError::new(
|
||||||
1,
|
1,
|
||||||
format!("invalid field number: {}", value.quote()),
|
format!("invalid field number: {}", value.quote()),
|
||||||
|
|
|
@ -75,6 +75,27 @@ fn different_field() {
|
||||||
.stdout_only_fixture("different_field.expected");
|
.stdout_only_fixture("different_field.expected");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn out_of_bounds_fields() {
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("fields_1.txt")
|
||||||
|
.arg("fields_4.txt")
|
||||||
|
.arg("-1")
|
||||||
|
.arg("3")
|
||||||
|
.arg("-2")
|
||||||
|
.arg("5")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only_fixture("out_of_bounds_fields.expected");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("fields_1.txt")
|
||||||
|
.arg("fields_4.txt")
|
||||||
|
.arg("-j")
|
||||||
|
.arg("100000000000000000000") // > usize::MAX for 64 bits
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only_fixture("out_of_bounds_fields.expected");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unpaired_lines() {
|
fn unpaired_lines() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
|
|
25
tests/fixtures/join/out_of_bounds_fields.expected
vendored
Normal file
25
tests/fixtures/join/out_of_bounds_fields.expected
vendored
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
1 2 c 1 cd
|
||||||
|
1 3 d 2 de
|
||||||
|
1 5 e 3 ef
|
||||||
|
1 7 f 4 fg
|
||||||
|
1 11 g 5 gh
|
||||||
|
2 2 c 1 cd
|
||||||
|
2 3 d 2 de
|
||||||
|
2 5 e 3 ef
|
||||||
|
2 7 f 4 fg
|
||||||
|
2 11 g 5 gh
|
||||||
|
3 2 c 1 cd
|
||||||
|
3 3 d 2 de
|
||||||
|
3 5 e 3 ef
|
||||||
|
3 7 f 4 fg
|
||||||
|
3 11 g 5 gh
|
||||||
|
5 2 c 1 cd
|
||||||
|
5 3 d 2 de
|
||||||
|
5 5 e 3 ef
|
||||||
|
5 7 f 4 fg
|
||||||
|
5 11 g 5 gh
|
||||||
|
8 2 c 1 cd
|
||||||
|
8 3 d 2 de
|
||||||
|
8 5 e 3 ef
|
||||||
|
8 7 f 4 fg
|
||||||
|
8 11 g 5 gh
|
Loading…
Add table
Add a link
Reference in a new issue