1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

sort: set -k arg to usize::MAX on overflow

newline, format, and more rust idiomatic code.

refactor to remove panic!
This commit is contained in:
David Rebbe 2025-01-20 20:35:02 -05:00
parent 3535bfdc72
commit 2ebdc4984d

View file

@ -34,6 +34,7 @@ use std::ffi::{OsStr, OsString};
use std::fs::{File, OpenOptions};
use std::hash::{Hash, Hasher};
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write};
use std::num::IntErrorKind;
use std::ops::Range;
use std::path::Path;
use std::path::PathBuf;
@ -696,9 +697,17 @@ impl KeyPosition {
.ok_or_else(|| format!("invalid key {}", key.quote()))?;
let char = field_and_char.next();
let field = field
.parse()
.map_err(|e| format!("failed to parse field index {}: {}", field.quote(), e))?;
let field = match field.parse::<usize>() {
Ok(f) => f,
Err(e) if *e.kind() == IntErrorKind::PosOverflow => usize::MAX,
Err(e) => {
return Err(format!(
"failed to parse field index {} {}",
field.quote(),
e
))
}
};
if field == 0 {
return Err("field index can not be 0".to_string());
}