diff --git a/src/relpath/relpath.rs b/src/relpath/relpath.rs index 8b03c4779..2b194332f 100644 --- a/src/relpath/relpath.rs +++ b/src/relpath/relpath.rs @@ -64,14 +64,13 @@ pub fn uumain(args: Vec) -> isize { } let mut suffix_pos = 0; - absfrom.components() - .zip(absto.components()) - .take_while( - |&(f, t)| if f == t { - suffix_pos += 1; true - } else { - false - }).last(); + for (f, t) in absfrom.components().zip(absto.components()) { + if f == t { + suffix_pos += 1; + } else { + break; + } + } let mut result = Path::new(""); absfrom.components().skip(suffix_pos).map(|_| result.push("..")).last(); diff --git a/src/split/split.rs b/src/split/split.rs index c0a154ad7..1ba74c5e8 100644 --- a/src/split/split.rs +++ b/src/split/split.rs @@ -111,7 +111,7 @@ pub fn uumain(args: Vec) -> isize { struct Settings { prefix: String, numeric_suffix: bool, - suffix_length: uint, + suffix_length: usize, input: String, strategy: String, strategy_param: String, @@ -124,30 +124,29 @@ struct SplitControl { } trait Splitter { - // Factory pattern - fn new(_hint: Option, &Settings) -> Box; - // Consume the current_line and return the consumed string fn consume(&mut self, &mut SplitControl) -> String; } struct LineSplitter { - saved_lines_to_write: uint, - lines_to_write: uint, + saved_lines_to_write: usize, + lines_to_write: usize, } - -impl Splitter for LineSplitter { - fn new(_: Option, settings: &Settings) -> Box { +impl LineSplitter { + fn new(settings: &Settings) -> Box { let n = match settings.strategy_param.as_slice().parse() { Some(a) => a, _ => crash!(1, "invalid number of lines") }; - box LineSplitter { + Box::new(LineSplitter { saved_lines_to_write: n, lines_to_write: n, - } as Box + }) as Box } +} + +impl Splitter for LineSplitter { fn consume(&mut self, control: &mut SplitControl) -> String { self.lines_to_write -= 1; if self.lines_to_write == 0 { @@ -159,42 +158,44 @@ impl Splitter for LineSplitter { } struct ByteSplitter { - saved_bytes_to_write: uint, - bytes_to_write: uint, + saved_bytes_to_write: usize, + bytes_to_write: usize, break_on_line_end: bool, require_whole_line: bool, } -impl Splitter for ByteSplitter { - fn new(_: Option, settings: &Settings) -> Box { +impl ByteSplitter { + fn new(settings: &Settings) -> Box { let mut strategy_param : Vec = settings.strategy_param.chars().collect(); let suffix = strategy_param.pop().unwrap(); let multiplier = match suffix { - '0'...'9' => 1u, - 'b' => 512u, - 'k' => 1024u, - 'm' => 1024u * 1024u, + '0'...'9' => 1us, + 'b' => 512us, + 'k' => 1024us, + 'm' => 1024us * 1024us, _ => crash!(1, "invalid number of bytes") }; let n = if suffix.is_alphabetic() { - match strategy_param.as_slice().iter().map(|c| *c).collect::().as_slice().parse::() { + match strategy_param.as_slice().iter().map(|c| *c).collect::().as_slice().parse::() { Some(a) => a, _ => crash!(1, "invalid number of bytes") } } else { - match settings.strategy_param.as_slice().parse::() { + match settings.strategy_param.as_slice().parse::() { Some(a) => a, _ => crash!(1, "invalid number of bytes") } }; - box ByteSplitter { + Box::new(ByteSplitter { saved_bytes_to_write: n * multiplier, bytes_to_write: n * multiplier, break_on_line_end: if settings.strategy == "b" { false } else { true }, require_whole_line: false, - } as Box + }) as Box } +} +impl Splitter for ByteSplitter { fn consume(&mut self, control: &mut SplitControl) -> String { let line = control.current_line.clone(); let n = std::cmp::min(line.as_slice().chars().count(), self.bytes_to_write); @@ -217,13 +218,13 @@ impl Splitter for ByteSplitter { } // (1, 3) -> "aab" -fn str_prefix(i: uint, width: uint) -> String { +fn str_prefix(i: usize, width: usize) -> String { let mut c = "".to_string(); let mut n = i; let mut w = width; while w > 0 { w -= 1; - let div = Int::pow(26 as uint, w); + let div = Int::pow(26 as usize, w); let r = n / div; n -= r * div; c.push(char::from_u32((r as u32) + 97).unwrap()); @@ -232,13 +233,13 @@ fn str_prefix(i: uint, width: uint) -> String { } // (1, 3) -> "001" -fn num_prefix(i: uint, width: uint) -> String { +fn num_prefix(i: usize, width: usize) -> String { let mut c = "".to_string(); let mut n = i; let mut w = width; while w > 0 { w -= 1; - let div = Int::pow(10 as uint, w); + let div = Int::pow(10 as usize, w); let r = n / div; n -= r * div; c.push(char::from_digit(r, 10).unwrap()); @@ -246,23 +247,23 @@ fn num_prefix(i: uint, width: uint) -> String { c } -fn split(settings: &Settings) -> int { +fn split(settings: &Settings) -> isize { let mut reader = io::BufferedReader::new( if settings.input.as_slice() == "-" { - box io::stdio::stdin_raw() as Box + Box::new(io::stdio::stdin_raw()) as Box } else { let r = match io::File::open(&Path::new(settings.input.clone())) { Ok(a) => a, Err(_) => crash!(1, "cannot open '{}' for reading: No such file or directory", settings.input) }; - box r as Box + Box::new(r) as Box } ); let mut splitter: Box = match settings.strategy.as_slice() { - "l" => Splitter::new(None::, settings), - "b" | "C" => Splitter::new(None::, settings), + "l" => LineSplitter::new(settings), + "b" | "C" => ByteSplitter::new(settings), a @ _ => crash!(1, "strategy {} not supported", a) }; @@ -271,7 +272,7 @@ fn split(settings: &Settings) -> int { request_new_file: true, // Request new file }; - let mut writer = io::BufferedWriter::new(box io::stdio::stdout_raw() as Box); + let mut writer = io::BufferedWriter::new(Box::new(io::stdio::stdout_raw()) as Box); let mut fileno = 0; loop { if control.current_line.as_slice().chars().count() == 0 { @@ -293,7 +294,7 @@ fn split(settings: &Settings) -> int { crash_if_err!(1, writer.flush()); } fileno += 1; - writer = io::BufferedWriter::new(box io::File::open_mode(&Path::new(filename.as_slice()), io::Open, io::Write) as Box); + writer = io::BufferedWriter::new(Box::new(io::File::open_mode(&Path::new(filename.as_slice()), io::Open, io::Write)) as Box); control.request_new_file = false; if settings.verbose { println!("creating file '{}'", filename); diff --git a/src/tee/tee.rs b/src/tee/tee.rs index bbb1bc865..d2374af1d 100644 --- a/src/tee/tee.rs +++ b/src/tee/tee.rs @@ -1,5 +1,4 @@ #![crate_name = "tee"] -#![feature(phase)] /* * This file is part of the uutils coreutils package. * @@ -66,7 +65,7 @@ fn options(args: &[String]) -> Result { append: m.opt_present("append"), ignore_interrupts: m.opt_present("ignore-interrupts"), print_and_exit: to_print, - files: box names.iter().map(|name| Path::new(name.clone())).collect() + files: Box::new(names.iter().map(|name| Path::new(name.clone())).collect()) }) }).map_err(|message| warn(message.as_slice())) } @@ -81,7 +80,7 @@ fn exec(options: Options) -> Result<(), ()> { fn tee(options: Options) -> Result<(), ()> { let writers = options.files.iter().map(|path| open(path, options.append)).collect(); let output = &mut MultiWriter::new(writers); - let input = &mut NamedReader { inner: box stdin() as Box }; + let input = &mut NamedReader { inner: Box::new(stdin()) as Box }; if copy(input, output).is_err() || output.flush().is_err() { Err(()) } else { @@ -91,15 +90,15 @@ fn tee(options: Options) -> Result<(), ()> { fn open(path: &Path, append: bool) -> Box { let inner = if *path == Path::new("-") { - box stdout() as Box + Box::new(stdout()) as Box } else { let mode = if append { Append } else { Truncate }; match File::open_mode(path, mode, Write) { - Ok(file) => box file as Box, - Err(_) => box NullWriter as Box + Ok(file) => Box::new(file) as Box, + Err(_) => Box::new(NullWriter) as Box } }; - box NamedWriter { inner: inner, path: box path.clone() } as Box + Box::new(NamedWriter { inner: inner, path: Box::new(path.clone()) }) as Box } struct NamedWriter { @@ -112,7 +111,7 @@ impl Writer for NamedWriter { with_path(&*self.path.clone(), || { let val = self.inner.write(buf); if val.is_err() { - self.inner = box NullWriter as Box; + self.inner = Box::new(NullWriter) as Box; } val }) @@ -122,7 +121,7 @@ impl Writer for NamedWriter { with_path(&*self.path.clone(), || { let val = self.inner.flush(); if val.is_err() { - self.inner = box NullWriter as Box; + self.inner = Box::new(NullWriter) as Box; } val }) @@ -134,14 +133,14 @@ struct NamedReader { } impl Reader for NamedReader { - fn read(&mut self, buf: &mut [u8]) -> IoResult { + fn read(&mut self, buf: &mut [u8]) -> IoResult { with_path(&Path::new("stdin"), || { self.inner.read(buf) }) } } -fn with_path(path: &Path, cb: F) -> IoResult where F: Fn() -> IoResult { +fn with_path(path: &Path, mut cb: F) -> IoResult where F: FnMut() -> IoResult { match cb() { Err(f) => { warn(format!("{}: {}", path.display(), f.to_string()).as_slice()); Err(f) } okay => okay diff --git a/src/unexpand/unexpand.rs b/src/unexpand/unexpand.rs index 65fa1927e..c862e27e5 100644 --- a/src/unexpand/unexpand.rs +++ b/src/unexpand/unexpand.rs @@ -21,9 +21,9 @@ mod util; static NAME: &'static str = "unexpand"; static VERSION: &'static str = "0.0.1"; -static DEFAULT_TABSTOP: uint = 8; +static DEFAULT_TABSTOP: usize = 8; -fn tabstops_parse(s: String) -> Vec { +fn tabstops_parse(s: String) -> Vec { let words = s.as_slice().split(',').collect::>(); let nums = words.into_iter() @@ -31,7 +31,7 @@ fn tabstops_parse(s: String) -> Vec { .unwrap_or_else( || crash!(1, "{}\n", "tab size contains invalid character(s)")) ) - .collect::>(); + .collect::>(); if nums.iter().any(|&n| n == 0) { crash!(1, "{}\n", "tab size cannot be 0"); @@ -47,7 +47,7 @@ fn tabstops_parse(s: String) -> Vec { struct Options { files: Vec, - tabstops: Vec, + tabstops: Vec, aflag: bool } @@ -108,24 +108,24 @@ pub fn uumain(args: Vec) -> isize { fn open(path: String) -> io::BufferedReader> { let mut file_buf; if path.as_slice() == "-" { - io::BufferedReader::new(box io::stdio::stdin_raw() as Box) + io::BufferedReader::new(Box::new(io::stdio::stdin_raw()) as Box) } else { file_buf = match io::File::open(&Path::new(path.as_slice())) { Ok(a) => a, _ => crash!(1, "{}: {}\n", path, "No such file or directory") }; - io::BufferedReader::new(box file_buf as Box) + io::BufferedReader::new(Box::new(file_buf) as Box) } } -fn is_tabstop(tabstops: &[uint], col: uint) -> bool { +fn is_tabstop(tabstops: &[usize], col: usize) -> bool { match tabstops { [tabstop] => col % tabstop == 0, - tabstops => tabstops.binary_search(|&e| e.cmp(&col)).found().is_some() + tabstops => tabstops.binary_search_by(|&e| e.cmp(&col)).is_ok() } } -fn to_next_stop(tabstops: &[uint], col: uint) -> Option { +fn to_next_stop(tabstops: &[usize], col: usize) -> Option { match tabstops { [tabstop] => Some(tabstop - col % tabstop), tabstops => tabstops.iter().skip_while(|&t| *t <= col).next() @@ -134,7 +134,7 @@ fn to_next_stop(tabstops: &[uint], col: uint) -> Option { } fn unexpandspan(mut output: &mut io::LineBufferedWriter, - tabstops: &[uint], nspaces: uint, col: uint, init: bool) { + tabstops: &[usize], nspaces: usize, col: usize, init: bool) { let mut cur = col - nspaces; if nspaces > 1 || init { loop {