1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

paste: make MUCH faster and fix a couple bugs

This commit is contained in:
Arcterus 2014-02-28 10:11:24 -08:00
parent fac630d07c
commit 2a875c0f49

View file

@ -59,13 +59,14 @@ fn paste(filenames: ~[~str], serial: bool, delimiters: ~str) {
let mut files: ~[io::BufferedReader<io::File>] = filenames.move_iter().map(|name| let mut files: ~[io::BufferedReader<io::File>] = filenames.move_iter().map(|name|
io::BufferedReader::new(crash_if_err!(1, io::File::open(&Path::new(name)))) io::BufferedReader::new(crash_if_err!(1, io::File::open(&Path::new(name))))
).collect(); ).collect();
let delimiters: ~[~str] = delimiters.chars().map(|x| x.to_str()).collect();
let mut delim_count = 0; let mut delim_count = 0;
if serial { if serial {
for file in files.mut_iter() { for file in files.mut_iter() {
let mut output = ~""; let mut output = ~"";
loop { loop {
output = output + match file.read_line() { output = output + match file.read_line() {
Ok(line) => format!("{}{}", line.trim_right(), delimiters.char_at(delim_count % delimiters.len())), Ok(line) => line.trim_right() + delimiters[delim_count % delimiters.len()],
Err(f) => if f.kind == io::EndOfFile { Err(f) => if f.kind == io::EndOfFile {
break break
} else { } else {
@ -78,25 +79,33 @@ fn paste(filenames: ~[~str], serial: bool, delimiters: ~str) {
println!("{}", output); println!("{}", output);
} }
} else { } else {
let mut eof = std::vec::from_elem(files.len(), false);
loop { loop {
let mut output = ~""; let mut output = ~"";
let mut eof = 0; let mut eof_count = 0;
for file in files.mut_iter() { for (i, file) in files.mut_iter().enumerate() {
output = output + match file.read_line() { if eof[i] {
Ok(line) => format!("{}{}", line.trim_right(), delimiters.char_at(delim_count % delimiters.len())), eof_count += 1;
Err(f) => if f.kind == io::EndOfFile {
eof += 1;
delimiters.char_at(delim_count % delimiters.len()).to_str()
} else { } else {
crash!(1, "{}", f.to_str()) match file.read_line() {
Ok(line) => output = output + line.slice_to(line.len() - 1),
Err(f) => if f.kind == io::EndOfFile {
eof[i] = true;
eof_count += 1;
} else {
crash!(1, "{}", f.to_str());
} }
};
} }
if files.len() == eof { }
output = output + delimiters[delim_count % delimiters.len()];
delim_count += 1;
}
if files.len() == eof_count {
break; break;
} }
output.pop_char(); output.pop_char();
println!("{}", output); println!("{}", output);
delim_count = 0;
} }
} }
} }