sort: unique option support

This commit is contained in:
palaviv 2016-06-11 15:46:41 +03:00
parent e87407f598
commit d4ffbe0526
4 changed files with 25 additions and 1 deletions

View file

@ -40,6 +40,7 @@ struct Settings {
mode: SortMode,
reverse: bool,
outfile: Option<String>,
unique: bool,
}
impl Default for Settings {
@ -48,6 +49,7 @@ impl Default for Settings {
mode: SortMode::Default,
reverse: false,
outfile: None,
unique: false,
}
}
}
@ -63,6 +65,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
opts.optflag("h", "help", "display this help and exit");
opts.optflag("", "version", "output version information and exit");
opts.optopt("o", "output", "write output to FILENAME instead of stdout", "FILENAME");
opts.optflag("u", "unique", "output only the first of an equal run");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
@ -100,6 +103,7 @@ With no FILE, or when FILE is -, read standard input.", NAME, VERSION);
settings.reverse = matches.opt_present("reverse");
settings.outfile = matches.opt_str("output");
settings.unique = matches.opt_present("unique");
let mut files = matches.free;
if files.is_empty() {
@ -138,6 +142,10 @@ fn exec(files: Vec<String>, settings: &Settings) {
SortMode::Default => lines.sort()
}
if settings.unique {
lines.dedup()
}
let iter = lines.iter();
if settings.reverse {
print_sorted(iter.rev(), &settings.outfile);
@ -161,7 +169,7 @@ fn permissive_f64_parse(a: &str) -> f64 {
}
/// Compares two floating point numbers, with errors being assumed to be -inf.
/// Stops coercing at the first whitespace char, so 1e2 will parse as 100 but
/// Stops coercing at the first whitespace char, so 1e2 will parse as 100 but
/// 1,000 will parse as -inf.
fn numeric_compare(a: &String, b: &String) -> Ordering {
let fa = permissive_f64_parse(a);

View file

@ -0,0 +1,4 @@
1
2
3
4

View file

@ -0,0 +1,7 @@
4
2
4
3
3
2
1

View file

@ -42,6 +42,11 @@ fn test_default_unsorted_ints() {
test_helper("default_unsorted_ints", "");
}
#[test]
fn test_numeric_unique_ints() {
test_helper("numeric_unsorted_ints_unique", "-nu");
}
fn test_helper(file_name: &str, args: &str) {
let (at, mut ucmd) = testing(UTIL_NAME);
ucmd.arg(args);