From 9b068aafeee96c11acc36a0b7d9f4e6e3f8ab615 Mon Sep 17 00:00:00 2001 From: Arcterus Date: Sat, 22 Nov 2014 13:41:57 -0800 Subject: [PATCH] sort: implement --reverse --- src/sort/sort.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/sort/sort.rs b/src/sort/sort.rs index 51186e493..28c6226c8 100644 --- a/src/sort/sort.rs +++ b/src/sort/sort.rs @@ -14,6 +14,7 @@ extern crate getopts; +use std::fmt::Show; use std::io::{print, File, BufferedReader}; use std::io::stdio::stdin_raw; use std::str::Chars; @@ -31,6 +32,7 @@ pub fn uumain(args: Vec) -> int { let program = args[0].as_slice(); let opts = [ getopts::optflag("n", "numeric-sort", "compare according to string numerical value"), + getopts::optflag("r", "reverse", "reverse the output"), getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("", "version", "output version information and exit"), ]; @@ -54,10 +56,8 @@ pub fn uumain(args: Vec) -> int { return 0; } - let mut numeric = false; - if matches.opt_present("numeric-sort") { - numeric = true; - } + let numeric = matches.opt_present("numeric-sort"); + let reverse = matches.opt_present("reverse"); let mut files = matches.free; if files.is_empty() { @@ -65,12 +65,12 @@ pub fn uumain(args: Vec) -> int { files.push("-".to_string()); } - exec(files, numeric); + exec(files, numeric, reverse); 0 } -fn exec(files: Vec, numeric: bool) { +fn exec(files: Vec, numeric: bool, reverse: bool) { for path in files.iter() { let (reader, _) = match open(path.as_slice()) { Some(x) => x, @@ -94,9 +94,13 @@ fn exec(files: Vec, numeric: bool) { } else { lines.sort(); } - for line in lines.iter() { - print!("{}", line) - } + + let iter = lines.iter(); + if reverse { + print_sorted(iter.rev()); + } else { + print_sorted(iter) + }; } } @@ -140,6 +144,12 @@ fn frac_compare(a: &String, b: &String) -> Ordering { } else { Equal } } +#[inline(always)] +fn print_sorted, S: Show>(mut iter: T) { + for line in iter { + print!("{}", line); + } +} // from cat.rs fn open<'a>(path: &str) -> Option<(Box, bool)> {