Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/uu/shuf/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ shuf-error-read-error = read error
shuf-error-read-random-bytes = reading random bytes failed
shuf-error-end-of-random-bytes = end of random source
shuf-error-no-lines-to-repeat = no lines to repeat
shuf-error-start-exceeds-end = start exceeds end
shuf-error-missing-dash = missing '-'
shuf-error-invalid-input-range = invalid input range: { $range }
shuf-error-invalid-input-range-too-large = invalid input range: { $range }: Value too large to be stored in data type
shuf-error-write-failed = write failed
shuf-error-memory-exhausted = memory exhausted
4 changes: 2 additions & 2 deletions src/uu/shuf/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ shuf-error-failed-to-open-for-writing = échec de l'ouverture de { $file } en é
shuf-error-failed-to-open-random-source = échec de l'ouverture de la source aléatoire { $file }
shuf-error-read-error = erreur de lecture
shuf-error-no-lines-to-repeat = aucune ligne à répéter
shuf-error-start-exceeds-end = le début dépasse la fin
shuf-error-missing-dash = '-' manquant
shuf-error-invalid-input-range = plage d'entrée invalide : { $range }
shuf-error-invalid-input-range-too-large = plage d'entrée invalide : { $range } : valeur trop grande pour être stockée dans le type de données
shuf-error-write-failed = échec de l'écriture
shuf-error-memory-exhausted = mémoire épuisée
48 changes: 35 additions & 13 deletions src/uu/shuf/src/shuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use std::ffi::{OsStr, OsString};
use std::fs::File;
use std::io::{self, BufReader, BufWriter, Read, Write, stdin, stdout};
use std::num::IntErrorKind;
use std::ops::RangeInclusive;
use std::path::{Path, PathBuf};
use std::str::FromStr;
Expand Down Expand Up @@ -78,8 +79,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.cloned()
.collect(),
)
} else if let Some(range) = matches.get_one(options::INPUT_RANGE).cloned() {
Mode::InputRange(range)
} else if let Some(range) = matches.get_one::<String>(options::INPUT_RANGE) {
Mode::InputRange(parse_range(range)?)
} else {
let mut operands = matches
.get_many::<OsString>(options::FILE_OR_ARGS)
Expand Down Expand Up @@ -197,7 +198,6 @@ pub fn uu_app() -> Command {
.long(options::INPUT_RANGE)
.value_name("LO-HI")
.help(translate!("shuf-help-input-range"))
.value_parser(parse_range)
.conflicts_with(options::FILE_OR_ARGS),
)
.arg(
Expand Down Expand Up @@ -445,17 +445,39 @@ fn shuf_exec(
Ok(())
}

fn parse_range(input_range: &str) -> Result<RangeInclusive<u64>, String> {
if let Some((from, to)) = input_range.split_once('-') {
let begin = from.parse::<u64>().map_err(|e| e.to_string())?;
let end = to.parse::<u64>().map_err(|e| e.to_string())?;
if begin <= end || begin == end + 1 {
Ok(begin..=end)
} else {
Err(translate!("shuf-error-start-exceeds-end"))
}
/// Parse the `LO-HI` of `-i`.
///
/// Whatever is wrong with it, GNU reports the range as a whole rather than
/// the part that failed, and adds a detail only when a bound does not fit in
/// a `u64`.
fn parse_range(input_range: &str) -> UResult<RangeInclusive<u64>> {
let invalid = || {
USimpleError::new(
1,
translate!("shuf-error-invalid-input-range", "range" => input_range.quote()),
)
};
let too_large = || {
USimpleError::new(
1,
translate!("shuf-error-invalid-input-range-too-large", "range" => input_range.quote()),
)
};

let Some((from, to)) = input_range.split_once('-') else {
return Err(invalid());
};
let parse = |bound: &str| match bound.parse::<u64>() {
Ok(n) => Ok(n),
Err(e) if *e.kind() == IntErrorKind::PosOverflow => Err(too_large()),
Err(_) => Err(invalid()),
};
let begin = parse(from)?;
let end = parse(to)?;
if begin <= end || begin == end + 1 {
Ok(begin..=end)
} else {
Err(translate!("shuf-error-missing-dash"))
Err(invalid())
}
}

Expand Down
42 changes: 33 additions & 9 deletions tests/by-util/test_shuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,21 +745,45 @@ fn test_shuf_invalid_input_range_one() {
new_ucmd!()
.args(&["-i", "0"])
.fails()
.stderr_contains("invalid value '0' for '--input-range <LO-HI>': missing '-'");
.stderr_contains("shuf: invalid input range: '0'\n");
}

#[test]
fn test_shuf_invalid_input_range_two() {
new_ucmd!().args(&["-i", "a-9"]).fails().stderr_contains(
"invalid value 'a-9' for '--input-range <LO-HI>': invalid digit found in string",
);
new_ucmd!()
.args(&["-i", "a-9"])
.fails()
.stderr_contains("shuf: invalid input range: 'a-9'\n");
}

#[test]
fn test_shuf_invalid_input_range_three() {
new_ucmd!().args(&["-i", "0-b"]).fails().stderr_contains(
"invalid value '0-b' for '--input-range <LO-HI>': invalid digit found in string",
);
new_ucmd!()
.args(&["-i", "0-b"])
.fails()
.stderr_contains("shuf: invalid input range: '0-b'\n");
}

/// Whatever is wrong with the LO-HI of -i, GNU names the range as a whole
/// and exits 1, and adds a detail only when a bound overflows.
#[test]
fn test_shuf_invalid_input_range_message() {
for range in ["5-1", "abc", "1-", "1-2-3", ""] {
new_ucmd!()
.args(&["-i", range])
.fails_with_code(1)
.no_stdout()
.stderr_only(format!("shuf: invalid input range: '{range}'\n"));
}

new_ucmd!()
.args(&["-i", "99999999999999999999999-1"])
.fails_with_code(1)
.no_stdout()
.stderr_only(
"shuf: invalid input range: '99999999999999999999999-1': \
Value too large to be stored in data type\n",
);
}

#[test]
Expand Down Expand Up @@ -867,7 +891,7 @@ fn test_range_empty_minus_one() {
.arg("-i5-3")
.fails()
.no_stdout()
.stderr_contains("invalid value '5-3' for '--input-range <LO-HI>': start exceeds end\n");
.stderr_contains("shuf: invalid input range: '5-3'\n");
}

#[test]
Expand Down Expand Up @@ -897,7 +921,7 @@ fn test_range_repeat_empty_minus_one() {
.arg("-ri5-3")
.fails()
.no_stdout()
.stderr_contains("invalid value '5-3' for '--input-range <LO-HI>': start exceeds end\n");
.stderr_contains("shuf: invalid input range: '5-3'\n");
}

// This test fails if we forget to flush the `BufWriter`.
Expand Down
Loading