mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-02 05:57:46 +00:00
sort, sum tac: fix build
This commit is contained in:
parent
5402e69923
commit
7860ef275c
3 changed files with 22 additions and 21 deletions
|
@ -13,6 +13,7 @@
|
||||||
|
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
|
|
||||||
|
use std::cmp::Ordering;
|
||||||
use std::fmt::Show;
|
use std::fmt::Show;
|
||||||
use std::io::{print, File, BufferedReader};
|
use std::io::{print, File, BufferedReader};
|
||||||
use std::io::stdio::stdin_raw;
|
use std::io::stdio::stdin_raw;
|
||||||
|
@ -28,7 +29,7 @@ static VERSION: &'static str = "0.0.1";
|
||||||
static DECIMAL_PT: char = '.';
|
static DECIMAL_PT: char = '.';
|
||||||
static THOUSANDS_SEP: char = ',';
|
static THOUSANDS_SEP: char = ',';
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> int {
|
pub fn uumain(args: Vec<String>) -> isize {
|
||||||
let program = args[0].as_slice();
|
let program = args[0].as_slice();
|
||||||
let opts = [
|
let opts = [
|
||||||
getopts::optflag("n", "numeric-sort", "compare according to string numerical value"),
|
getopts::optflag("n", "numeric-sort", "compare according to string numerical value"),
|
||||||
|
@ -107,9 +108,9 @@ fn exec(files: Vec<String>, numeric: bool, reverse: bool) {
|
||||||
fn skip_zeros(mut char_a: char, char_iter: &mut Chars, ret: Ordering) -> Ordering {
|
fn skip_zeros(mut char_a: char, char_iter: &mut Chars, ret: Ordering) -> Ordering {
|
||||||
char_a = match char_iter.next() { None => 0 as char, Some(t) => t };
|
char_a = match char_iter.next() { None => 0 as char, Some(t) => t };
|
||||||
while char_a == '0' {
|
while char_a == '0' {
|
||||||
char_a = match char_iter.next() { None => return Equal, Some(t) => t };
|
char_a = match char_iter.next() { None => return Ordering::Equal, Some(t) => t };
|
||||||
}
|
}
|
||||||
if char_a.is_digit(10) { ret } else { Equal }
|
if char_a.is_digit(10) { ret } else { Ordering::Equal }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compares two decimal fractions as strings (n < 1)
|
/// Compares two decimal fractions as strings (n < 1)
|
||||||
|
@ -127,25 +128,25 @@ fn frac_compare(a: &String, b: &String) -> Ordering {
|
||||||
char_b = match b_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
|
// hit the end at the same time, they are equal
|
||||||
if !char_a.is_digit(10) {
|
if !char_a.is_digit(10) {
|
||||||
return Equal;
|
return Ordering::Equal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if char_a.is_digit(10) && char_b.is_digit(10) {
|
if char_a.is_digit(10) && char_b.is_digit(10) {
|
||||||
(char_a as int).cmp(&(char_b as int))
|
(char_a as int).cmp(&(char_b as int))
|
||||||
} else if char_a.is_digit(10) {
|
} else if char_a.is_digit(10) {
|
||||||
skip_zeros(char_a, a_chars, Greater)
|
skip_zeros(char_a, a_chars, Ordering::Greater)
|
||||||
} else if char_b.is_digit(10) {
|
} else if char_b.is_digit(10) {
|
||||||
skip_zeros(char_b, b_chars, Less)
|
skip_zeros(char_b, b_chars, Ordering::Less)
|
||||||
} else { Equal }
|
} else { Ordering::Equal }
|
||||||
} else if char_a == DECIMAL_PT {
|
} else if char_a == DECIMAL_PT {
|
||||||
skip_zeros(char_a, a_chars, Greater)
|
skip_zeros(char_a, a_chars, Ordering::Greater)
|
||||||
} else if char_b == DECIMAL_PT {
|
} else if char_b == DECIMAL_PT {
|
||||||
skip_zeros(char_b, b_chars, Less)
|
skip_zeros(char_b, b_chars, Ordering::Less)
|
||||||
} else { Equal }
|
} else { Ordering::Equal }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn print_sorted<T: Iterator<S>, S: Show>(mut iter: T) {
|
fn print_sorted<S, T: Iterator<Item=S>>(mut iter: T) where S: std::fmt::String {
|
||||||
for line in iter {
|
for line in iter {
|
||||||
print!("{}", line);
|
print!("{}", line);
|
||||||
}
|
}
|
||||||
|
@ -156,11 +157,11 @@ fn open<'a>(path: &str) -> Option<(Box<Reader + 'a>, bool)> {
|
||||||
if path == "-" {
|
if path == "-" {
|
||||||
let stdin = stdin_raw();
|
let stdin = stdin_raw();
|
||||||
let interactive = stdin.isatty();
|
let interactive = stdin.isatty();
|
||||||
return Some((box stdin as Box<Reader>, interactive));
|
return Some((Box::new(stdin) as Box<Reader>, interactive));
|
||||||
}
|
}
|
||||||
|
|
||||||
match File::open(&std::path::Path::new(path)) {
|
match File::open(&std::path::Path::new(path)) {
|
||||||
Ok(f) => Some((box f as Box<Reader>, false)),
|
Ok(f) => Some((Box::new(f) as Box<Reader>, false)),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
show_error!("sort: {0}: {1}", path, e.to_string());
|
show_error!("sort: {0}: {1}", path, e.to_string());
|
||||||
None
|
None
|
||||||
|
|
|
@ -22,7 +22,7 @@ mod util;
|
||||||
static VERSION: &'static str = "1.0.0";
|
static VERSION: &'static str = "1.0.0";
|
||||||
static NAME: &'static str = "sum";
|
static NAME: &'static str = "sum";
|
||||||
|
|
||||||
fn bsd_sum(mut reader: Box<Reader>) -> (uint, u16) {
|
fn bsd_sum(mut reader: Box<Reader>) -> (usize, u16) {
|
||||||
let mut buf = [0; 1024];
|
let mut buf = [0; 1024];
|
||||||
let mut blocks_read = 0;
|
let mut blocks_read = 0;
|
||||||
let mut checksum: u16 = 0;
|
let mut checksum: u16 = 0;
|
||||||
|
@ -42,7 +42,7 @@ fn bsd_sum(mut reader: Box<Reader>) -> (uint, u16) {
|
||||||
(blocks_read, checksum)
|
(blocks_read, checksum)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sysv_sum(mut reader: Box<Reader>) -> (uint, u16) {
|
fn sysv_sum(mut reader: Box<Reader>) -> (usize, u16) {
|
||||||
let mut buf = [0; 512];
|
let mut buf = [0; 512];
|
||||||
let mut blocks_read = 0;
|
let mut blocks_read = 0;
|
||||||
let mut ret = 0;
|
let mut ret = 0;
|
||||||
|
@ -67,15 +67,15 @@ fn sysv_sum(mut reader: Box<Reader>) -> (uint, u16) {
|
||||||
|
|
||||||
fn open(name: &str) -> IoResult<Box<Reader>> {
|
fn open(name: &str) -> IoResult<Box<Reader>> {
|
||||||
match name {
|
match name {
|
||||||
"-" => Ok(box stdin_raw() as Box<Reader>),
|
"-" => Ok(Box::new(stdin_raw()) as Box<Reader>),
|
||||||
_ => {
|
_ => {
|
||||||
let f = try!(File::open(&Path::new(name)));
|
let f = try!(File::open(&Path::new(name)));
|
||||||
Ok(box f as Box<Reader>)
|
Ok(Box::new(f) as Box<Reader>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> int {
|
pub fn uumain(args: Vec<String>) -> isize {
|
||||||
let program = args[0].as_slice();
|
let program = args[0].as_slice();
|
||||||
let opts = [
|
let opts = [
|
||||||
getopts::optflag("r", "", "use the BSD compatible algorithm (default)"),
|
getopts::optflag("r", "", "use the BSD compatible algorithm (default)"),
|
||||||
|
|
|
@ -21,7 +21,7 @@ mod util;
|
||||||
static NAME: &'static str = "tac";
|
static NAME: &'static str = "tac";
|
||||||
static VERSION: &'static str = "1.0.0";
|
static VERSION: &'static str = "1.0.0";
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> int {
|
pub fn uumain(args: Vec<String>) -> isize {
|
||||||
let program = args[0].clone();
|
let program = args[0].clone();
|
||||||
|
|
||||||
let opts = [
|
let opts = [
|
||||||
|
@ -72,10 +72,10 @@ fn tac(filenames: Vec<String>, before: bool, _: bool, separator: &str) {
|
||||||
for filename in filenames.into_iter() {
|
for filename in filenames.into_iter() {
|
||||||
let mut file = io::BufferedReader::new(
|
let mut file = io::BufferedReader::new(
|
||||||
if filename.as_slice() == "-" {
|
if filename.as_slice() == "-" {
|
||||||
box io::stdio::stdin_raw() as Box<Reader>
|
Box::new(io::stdio::stdin_raw()) as Box<Reader>
|
||||||
} else {
|
} else {
|
||||||
let r = crash_if_err!(1, io::File::open(&Path::new(filename)));
|
let r = crash_if_err!(1, io::File::open(&Path::new(filename)));
|
||||||
box r as Box<Reader>
|
Box::new(r) as Box<Reader>
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
let mut data = crash_if_err!(1, file.read_to_string());
|
let mut data = crash_if_err!(1, file.read_to_string());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue