From d4ffbe05268758b75e16b9d63cee42e04ed2ec26 Mon Sep 17 00:00:00 2001 From: palaviv Date: Sat, 11 Jun 2016 15:46:41 +0300 Subject: [PATCH] sort: unique option support --- src/sort/sort.rs | 10 +++++++++- .../sort/numeric_unsorted_ints_unique.expected | 4 ++++ tests/fixtures/sort/numeric_unsorted_ints_unique.txt | 7 +++++++ tests/test_sort.rs | 5 +++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/sort/numeric_unsorted_ints_unique.expected create mode 100644 tests/fixtures/sort/numeric_unsorted_ints_unique.txt diff --git a/src/sort/sort.rs b/src/sort/sort.rs index cc217ae80..6fd61a7f3 100644 --- a/src/sort/sort.rs +++ b/src/sort/sort.rs @@ -40,6 +40,7 @@ struct Settings { mode: SortMode, reverse: bool, outfile: Option, + 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) -> 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, 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); diff --git a/tests/fixtures/sort/numeric_unsorted_ints_unique.expected b/tests/fixtures/sort/numeric_unsorted_ints_unique.expected new file mode 100644 index 000000000..94ebaf900 --- /dev/null +++ b/tests/fixtures/sort/numeric_unsorted_ints_unique.expected @@ -0,0 +1,4 @@ +1 +2 +3 +4 diff --git a/tests/fixtures/sort/numeric_unsorted_ints_unique.txt b/tests/fixtures/sort/numeric_unsorted_ints_unique.txt new file mode 100644 index 000000000..08a9ecef0 --- /dev/null +++ b/tests/fixtures/sort/numeric_unsorted_ints_unique.txt @@ -0,0 +1,7 @@ +4 +2 +4 +3 +3 +2 +1 diff --git a/tests/test_sort.rs b/tests/test_sort.rs index 3676d9e6c..f4f85f0ad 100644 --- a/tests/test_sort.rs +++ b/tests/test_sort.rs @@ -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);