shuf: use split_once for parsing the range

This commit is contained in:
Terts Diepraam 2022-02-08 14:20:24 +01:00
parent 30ae952b83
commit 9538814702

View file

@ -288,17 +288,16 @@ fn shuf_bytes(input: &mut Vec<&[u8]>, opts: Options) -> UResult<()> {
}
fn parse_range(input_range: &str) -> Result<(usize, usize), String> {
let split: Vec<&str> = input_range.split('-').collect();
if split.len() != 2 {
Err(format!("invalid input range: {}", input_range.quote()))
} else {
let begin = split[0]
if let Some((from, to)) = input_range.split_once('-') {
let begin = from
.parse::<usize>()
.map_err(|_| format!("invalid input range: {}", split[0].quote()))?;
let end = split[1]
.map_err(|_| format!("invalid input range: {}", from.quote()))?;
let end = to
.parse::<usize>()
.map_err(|_| format!("invalid input range: {}", split[1].quote()))?;
.map_err(|_| format!("invalid input range: {}", to.quote()))?;
Ok((begin, end + 1))
} else {
Err(format!("invalid input range: {}", input_range.quote()))
}
}