mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
sort: unique option support
This commit is contained in:
parent
e87407f598
commit
d4ffbe0526
4 changed files with 25 additions and 1 deletions
|
@ -40,6 +40,7 @@ struct Settings {
|
||||||
mode: SortMode,
|
mode: SortMode,
|
||||||
reverse: bool,
|
reverse: bool,
|
||||||
outfile: Option<String>,
|
outfile: Option<String>,
|
||||||
|
unique: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Settings {
|
impl Default for Settings {
|
||||||
|
@ -48,6 +49,7 @@ impl Default for Settings {
|
||||||
mode: SortMode::Default,
|
mode: SortMode::Default,
|
||||||
reverse: false,
|
reverse: false,
|
||||||
outfile: None,
|
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("h", "help", "display this help and exit");
|
||||||
opts.optflag("", "version", "output version information and exit");
|
opts.optflag("", "version", "output version information and exit");
|
||||||
opts.optopt("o", "output", "write output to FILENAME instead of stdout", "FILENAME");
|
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..]) {
|
let matches = match opts.parse(&args[1..]) {
|
||||||
Ok(m) => m,
|
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.reverse = matches.opt_present("reverse");
|
||||||
settings.outfile = matches.opt_str("output");
|
settings.outfile = matches.opt_str("output");
|
||||||
|
settings.unique = matches.opt_present("unique");
|
||||||
|
|
||||||
let mut files = matches.free;
|
let mut files = matches.free;
|
||||||
if files.is_empty() {
|
if files.is_empty() {
|
||||||
|
@ -138,6 +142,10 @@ fn exec(files: Vec<String>, settings: &Settings) {
|
||||||
SortMode::Default => lines.sort()
|
SortMode::Default => lines.sort()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if settings.unique {
|
||||||
|
lines.dedup()
|
||||||
|
}
|
||||||
|
|
||||||
let iter = lines.iter();
|
let iter = lines.iter();
|
||||||
if settings.reverse {
|
if settings.reverse {
|
||||||
print_sorted(iter.rev(), &settings.outfile);
|
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.
|
/// 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.
|
/// 1,000 will parse as -inf.
|
||||||
fn numeric_compare(a: &String, b: &String) -> Ordering {
|
fn numeric_compare(a: &String, b: &String) -> Ordering {
|
||||||
let fa = permissive_f64_parse(a);
|
let fa = permissive_f64_parse(a);
|
||||||
|
|
4
tests/fixtures/sort/numeric_unsorted_ints_unique.expected
vendored
Normal file
4
tests/fixtures/sort/numeric_unsorted_ints_unique.expected
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
7
tests/fixtures/sort/numeric_unsorted_ints_unique.txt
vendored
Normal file
7
tests/fixtures/sort/numeric_unsorted_ints_unique.txt
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
4
|
||||||
|
2
|
||||||
|
4
|
||||||
|
3
|
||||||
|
3
|
||||||
|
2
|
||||||
|
1
|
|
@ -42,6 +42,11 @@ fn test_default_unsorted_ints() {
|
||||||
test_helper("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) {
|
fn test_helper(file_name: &str, args: &str) {
|
||||||
let (at, mut ucmd) = testing(UTIL_NAME);
|
let (at, mut ucmd) = testing(UTIL_NAME);
|
||||||
ucmd.arg(args);
|
ucmd.arg(args);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue