diff --git a/src/common/c_types.rs b/src/common/c_types.rs index ca571ebe9..93be7ec71 100644 --- a/src/common/c_types.rs +++ b/src/common/c_types.rs @@ -111,7 +111,7 @@ pub fn get_pw_from_args(free: &Vec) -> Option { let username = free[0].as_slice(); // Passed user as id - if username.chars().all(|c| c.is_digit()) { + if username.chars().all(|c| c.is_digit(10)) { let id = from_str::(username).unwrap(); let pw_pointer = unsafe { getpwuid(id as uid_t) }; @@ -138,7 +138,7 @@ pub fn get_pw_from_args(free: &Vec) -> Option { } pub fn get_group(groupname: &str) -> Option { - let group = if groupname.chars().all(|c| c.is_digit()) { + let group = if groupname.chars().all(|c| c.is_digit(10)) { unsafe { getgrgid(from_str::(groupname).unwrap()) } } else { unsafe { getgrnam(groupname.to_c_str().unwrap() as *const c_char) } diff --git a/src/du/du.rs b/src/du/du.rs index 91250519e..f07319cd0 100644 --- a/src/du/du.rs +++ b/src/du/du.rs @@ -235,10 +235,10 @@ ers of 1000).", let mut numbers = vec!(); let mut letters = vec!(); for c in s.as_slice().chars() { - if found_letter && c.is_digit() || !found_number && !c.is_digit() { + if found_letter && c.is_digit(10) || !found_number && !c.is_digit(10) { show_error!("invalid --block-size argument '{}'", s); return 1; - } else if c.is_digit() { + } else if c.is_digit(10) { found_number = true; numbers.push(c as u8); } else if c.is_alphabetic() { diff --git a/src/fold/fold.rs b/src/fold/fold.rs index 399d034d6..ba8c4ca56 100644 --- a/src/fold/fold.rs +++ b/src/fold/fold.rs @@ -81,16 +81,14 @@ pub fn uumain(args: Vec) -> int { } fn handle_obsolete(args: &[String]) -> (Vec, Option) { - let mut args = args.to_vec(); - let mut i = 0; - while i < args.len() { - if args[i].as_slice().char_at(0) == '-' && args[i].len() > 1 && args[i].as_slice().char_at(1).is_digit() { - return (args.clone(), - Some(args.remove(i).unwrap().as_slice().slice_from(1).to_string())); + for (i, arg) in args.iter().enumerate() { + let slice = arg.as_slice(); + if slice.char_at(0) == '-' && slice.len() > 1 && slice.char_at(1).is_digit(10) { + return (args.slice_to(i).to_vec() + args.slice_from(i + 1), + Some(slice.slice_from(1).to_string())); } - i += 1; } - (args, None) + (args.to_vec(), None) } #[inline] @@ -141,6 +139,10 @@ fn fold_file(file: BufferedReader, bytes: bool, spaces: bool, let mut len = line.char_len(); let newline = line.ends_with("\n"); if newline { + if len == 1 { + println!(""); + continue; + } line = line.slice_to(line.len() - 1); len -= 1; } diff --git a/src/head/head.rs b/src/head/head.rs index 3ddebe779..941bc7726 100644 --- a/src/head/head.rs +++ b/src/head/head.rs @@ -14,7 +14,7 @@ extern crate getopts; -use std::char; +use std::char::UnicodeChar; use std::io::{stdin}; use std::io::{BufferedReader, BytesReader}; use std::io::fs::File; @@ -145,7 +145,7 @@ fn obsolete(options: &[String]) -> (Vec, Option) { let len = current.len(); for pos in range(1, len) { // Ensure that the argument is only made out of digits - if !char::is_digit(current[pos] as char) { break; } + if !UnicodeChar::is_numeric(current[pos] as char) { break; } // If this is the last number if pos == len - 1 { diff --git a/src/kill/kill.rs b/src/kill/kill.rs index 910655729..17a71f4c1 100644 --- a/src/kill/kill.rs +++ b/src/kill/kill.rs @@ -105,7 +105,7 @@ fn handle_obsolete(mut args: Vec) -> (Vec, Option) { while i < args.len() { // this is safe because slice is valid when it is referenced let slice: &str = unsafe { std::mem::transmute(args[i].as_slice()) }; - if slice.char_at(0) == '-' && slice.len() > 1 && slice.char_at(1).is_digit() { + if slice.char_at(0) == '-' && slice.len() > 1 && slice.char_at(1).is_digit(10) { let val = slice.slice_from(1); match from_str(val) { Some(num) => { diff --git a/src/seq/seq.rs b/src/seq/seq.rs index 52609657f..b7986c31b 100644 --- a/src/seq/seq.rs +++ b/src/seq/seq.rs @@ -22,7 +22,10 @@ struct SeqOptions { widths: bool } -fn parse_float(s: &str) -> Result{ +fn parse_float(mut s: &str) -> Result { + if s.starts_with("+") { + s = s.slice_from(1); + } match from_str(s) { Some(n) => Ok(n), None => Err(format!("seq: invalid floating point argument: {}", s)) @@ -179,7 +182,7 @@ pub fn uumain(args: Vec) -> int { let dec = slice.find('.').unwrap_or(len); largest_dec = cmp::max(largest_dec, len - dec); padding = cmp::max(padding, dec); - match parse_float(free[1].as_slice()) { + match parse_float(slice) { Ok(n) => n, Err(s) => { show_error!("{}", s); return 1; } } diff --git a/src/sort/sort.rs b/src/sort/sort.rs index 536b412b6..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) + }; } } @@ -105,7 +109,7 @@ fn skip_zeros(mut char_a: char, char_iter: &mut Chars, ret: Ordering) -> Orderin while char_a == '0' { char_a = match char_iter.next() { None => return Equal, Some(t) => t }; } - if char_a.is_digit() { ret } else { Equal } + if char_a.is_digit(10) { ret } else { Equal } } /// Compares two decimal fractions as strings (n < 1) @@ -122,15 +126,15 @@ fn frac_compare(a: &String, b: &String) -> Ordering { char_a = match a_chars.next() { None => 0 as char, Some(t) => t }; char_b = match b_chars.next() { None => 0 as char, Some(t) => t }; // hit the end at the same time, they are equal - if !char_a.is_digit() { + if !char_a.is_digit(10) { return Equal; } } - if char_a.is_digit() && char_b.is_digit() { + if char_a.is_digit(10) && char_b.is_digit(10) { (char_a as int).cmp(&(char_b as int)) - } else if char_a.is_digit() { + } else if char_a.is_digit(10) { skip_zeros(char_a, a_chars, Greater) - } else if char_b.is_digit() { + } else if char_b.is_digit(10) { skip_zeros(char_b, b_chars, Less) } else { Equal } } else if char_a == DECIMAL_PT { @@ -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)> { diff --git a/src/tail/tail.rs b/src/tail/tail.rs index 999683ff1..06cb28c99 100644 --- a/src/tail/tail.rs +++ b/src/tail/tail.rs @@ -13,7 +13,7 @@ extern crate getopts; -use std::char; +use std::char::UnicodeChar; use std::io::{stdin}; use std::io::{BufferedReader, BytesReader}; use std::io::fs::File; @@ -165,7 +165,7 @@ fn obsolete(options: &[String]) -> (Vec, Option) { let len = current.len(); for pos in range(1, len) { // Ensure that the argument is only made out of digits - if !char::is_digit(current[pos] as char) { break; } + if !UnicodeChar::is_numeric(current[pos] as char) { break; } // If this is the last number if pos == len - 1 { diff --git a/src/uutils/uutils.rs b/src/uutils/uutils.rs index 2ab5fa00c..056a31d64 100644 --- a/src/uutils/uutils.rs +++ b/src/uutils/uutils.rs @@ -46,7 +46,7 @@ fn main() { let binary = Path::new(args[0].as_slice()); let binary_as_util = binary.filename_str().unwrap(); - match umap.find_equiv(binary_as_util) { + match umap.get(binary_as_util) { Some(&uumain) => { os::set_exit_status(uumain(args)); return @@ -70,7 +70,7 @@ fn main() { args.remove(0); let util = args[0].as_slice(); - match umap.find_equiv(util) { + match umap.get(util) { Some(&uumain) => { os::set_exit_status(uumain(args.clone())); return @@ -80,7 +80,7 @@ fn main() { // see if they want help on a specific util if args.len() >= 2 { let util = args[1].as_slice(); - match umap.find_equiv(util) { + match umap.get(util) { Some(&uumain) => { os::set_exit_status(uumain(vec![util.to_string(), "--help".to_string()])); return