From 2ebdc4984dd463cf050cc1da3ecd07a7fc34b2b7 Mon Sep 17 00:00:00 2001 From: David Rebbe <1187684+ic3man5@users.noreply.github.com> Date: Mon, 20 Jan 2025 20:35:02 -0500 Subject: [PATCH] sort: set -k arg to usize::MAX on overflow newline, format, and more rust idiomatic code. refactor to remove panic! --- src/uu/sort/src/sort.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 0c39c2442..e61914100 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -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::() { + 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()); }