mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +00:00
Merge pull request #576 from jbcrail/update-paste
Update paste and add test.
This commit is contained in:
commit
4494a13051
5 changed files with 122 additions and 38 deletions
1
Makefile
1
Makefile
|
@ -164,6 +164,7 @@ TEST_PROGS := \
|
||||||
mkdir \
|
mkdir \
|
||||||
mv \
|
mv \
|
||||||
nl \
|
nl \
|
||||||
|
paste \
|
||||||
seq \
|
seq \
|
||||||
sort \
|
sort \
|
||||||
test \
|
test \
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#![crate_name = "paste"]
|
#![crate_name = "paste"]
|
||||||
#![feature(collections, core, old_io, old_path, rustc_private)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
|
@ -13,8 +13,10 @@
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use std::old_io as io;
|
use std::io::{BufRead, BufReader, Read, stdin, Write};
|
||||||
use std::iter::repeat;
|
use std::iter::repeat;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
#[path = "../common/util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
@ -24,7 +26,7 @@ static NAME: &'static str = "paste";
|
||||||
static VERSION: &'static str = "1.0.0";
|
static VERSION: &'static str = "1.0.0";
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let program = args[0].clone();
|
let program = &args[0];
|
||||||
|
|
||||||
let opts = [
|
let opts = [
|
||||||
getopts::optflag("s", "serial", "paste one file at a time instead of in parallel"),
|
getopts::optflag("s", "serial", "paste one file at a time instead of in parallel"),
|
||||||
|
@ -32,9 +34,9 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
getopts::optflag("h", "help", "display this help and exit"),
|
getopts::optflag("h", "help", "display this help and exit"),
|
||||||
getopts::optflag("V", "version", "output version information and exit")
|
getopts::optflag("V", "version", "output version information and exit")
|
||||||
];
|
];
|
||||||
let matches = match getopts::getopts(args.tail(), &opts) {
|
let matches = match getopts::getopts(&args[1..], &opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => crash!(1, "{}", f)
|
Err(e) => crash!(1, "{}", e)
|
||||||
};
|
};
|
||||||
if matches.opt_present("help") {
|
if matches.opt_present("help") {
|
||||||
println!("{} {}", NAME, VERSION);
|
println!("{} {}", NAME, VERSION);
|
||||||
|
@ -47,75 +49,81 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
println!("{} {}", NAME, VERSION);
|
println!("{} {}", NAME, VERSION);
|
||||||
} else {
|
} else {
|
||||||
let serial = matches.opt_present("serial");
|
let serial = matches.opt_present("serial");
|
||||||
let delimiters = match matches.opt_str("delimiters") {
|
let delimiters = matches.opt_str("delimiters").unwrap_or("\t".to_string());
|
||||||
Some(m) => m,
|
paste(matches.free, serial, delimiters);
|
||||||
None => "\t".to_string()
|
|
||||||
};
|
|
||||||
paste(matches.free, serial, delimiters.as_slice());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn paste(filenames: Vec<String>, serial: bool, delimiters: &str) {
|
fn paste(filenames: Vec<String>, serial: bool, delimiters: String) {
|
||||||
let mut files: Vec<io::BufferedReader<Box<Reader>>> = filenames.into_iter().map(|name|
|
let mut files: Vec<BufReader<Box<Read>>> = filenames.into_iter().map(|name|
|
||||||
io::BufferedReader::new(
|
BufReader::new(
|
||||||
if name.as_slice() == "-" {
|
if name == "-" {
|
||||||
Box::new(io::stdio::stdin_raw()) as Box<Reader>
|
Box::new(stdin()) as Box<Read>
|
||||||
} else {
|
} else {
|
||||||
let r = crash_if_err!(1, io::File::open(&Path::new(name)));
|
let r = crash_if_err!(1, File::open(Path::new(&name)));
|
||||||
Box::new(r) as Box<Reader>
|
Box::new(r) as Box<Read>
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
).collect();
|
).collect();
|
||||||
let delimiters: Vec<String> = delimiters.chars().map(|x| x.to_string()).collect();
|
|
||||||
|
let delimiters: Vec<String> = unescape(delimiters).chars().map(|x| x.to_string()).collect();
|
||||||
let mut delim_count = 0;
|
let mut delim_count = 0;
|
||||||
|
|
||||||
if serial {
|
if serial {
|
||||||
for file in files.iter_mut() {
|
for file in files.iter_mut() {
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
loop {
|
loop {
|
||||||
match file.read_line() {
|
let mut line = String::new();
|
||||||
Ok(line) => {
|
match file.read_line(&mut line) {
|
||||||
output.push_str(line.as_slice().trim_right());
|
Ok(0) => break,
|
||||||
output.push_str(delimiters[delim_count % delimiters.len()].as_slice());
|
Ok(_) => {
|
||||||
}
|
output.push_str(line.trim_right());
|
||||||
Err(f) => if f.kind == io::EndOfFile {
|
output.push_str(&delimiters[delim_count % delimiters.len()]);
|
||||||
break
|
|
||||||
} else {
|
|
||||||
crash!(1, "{}", f.to_string())
|
|
||||||
}
|
}
|
||||||
|
Err(e) => crash!(1, "{}", e.to_string())
|
||||||
}
|
}
|
||||||
delim_count += 1;
|
delim_count += 1;
|
||||||
}
|
}
|
||||||
println!("{}", output.as_slice().slice_to(output.len() - 1));
|
println!("{}", &output[..output.len()-1]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let mut eof : Vec<bool> = repeat(false).take(files.len()).collect();
|
let mut eof: Vec<bool> = repeat(false).take(files.len()).collect();
|
||||||
loop {
|
loop {
|
||||||
let mut output = "".to_string();
|
let mut output = String::new();
|
||||||
let mut eof_count = 0;
|
let mut eof_count = 0;
|
||||||
for (i, file) in files.iter_mut().enumerate() {
|
for (i, file) in files.iter_mut().enumerate() {
|
||||||
if eof[i] {
|
if eof[i] {
|
||||||
eof_count += 1;
|
eof_count += 1;
|
||||||
} else {
|
} else {
|
||||||
match file.read_line() {
|
let mut line = String::new();
|
||||||
Ok(line) => output.push_str(&line.as_slice()[..line.len() - 1]),
|
match file.read_line(&mut line) {
|
||||||
Err(f) => if f.kind == io::EndOfFile {
|
Ok(0) => {
|
||||||
eof[i] = true;
|
eof[i] = true;
|
||||||
eof_count += 1;
|
eof_count += 1;
|
||||||
} else {
|
|
||||||
crash!(1, "{}", f.to_string());
|
|
||||||
}
|
}
|
||||||
|
Ok(_) => output.push_str(line.trim_right()),
|
||||||
|
Err(e) => crash!(1, "{}", e.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
output.push_str(delimiters[delim_count % delimiters.len()].as_slice());
|
output.push_str(&delimiters[delim_count % delimiters.len()]);
|
||||||
delim_count += 1;
|
delim_count += 1;
|
||||||
}
|
}
|
||||||
if files.len() == eof_count {
|
if files.len() == eof_count {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
println!("{}", output.as_slice().slice_to(output.len() - 1));
|
println!("{}", &output[..output.len()-1]);
|
||||||
delim_count = 0;
|
delim_count = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unescape all special characters
|
||||||
|
// TODO: this will need work to conform to GNU implementation
|
||||||
|
fn unescape(s: String) -> String {
|
||||||
|
s.replace("\\n", "\n")
|
||||||
|
.replace("\\t", "\t")
|
||||||
|
.replace("\\\\", "\\")
|
||||||
|
.replace("\\", "")
|
||||||
|
}
|
||||||
|
|
16
test/fixtures/paste/html_colors.expected
vendored
Normal file
16
test/fixtures/paste/html_colors.expected
vendored
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
white #FFFFFF
|
||||||
|
silver #C0C0C0
|
||||||
|
gray #808080
|
||||||
|
black #000000
|
||||||
|
red #FF0000
|
||||||
|
maroon #800000
|
||||||
|
yellow #FFFF00
|
||||||
|
olive #808000
|
||||||
|
lime #00FF00
|
||||||
|
green #008000
|
||||||
|
aqua #00FFFF
|
||||||
|
teal #008080
|
||||||
|
blue #0000FF
|
||||||
|
navy #000080
|
||||||
|
fuchsia #FF00FF
|
||||||
|
purple #800080
|
32
test/fixtures/paste/html_colors.txt
vendored
Normal file
32
test/fixtures/paste/html_colors.txt
vendored
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
white
|
||||||
|
#FFFFFF
|
||||||
|
silver
|
||||||
|
#C0C0C0
|
||||||
|
gray
|
||||||
|
#808080
|
||||||
|
black
|
||||||
|
#000000
|
||||||
|
red
|
||||||
|
#FF0000
|
||||||
|
maroon
|
||||||
|
#800000
|
||||||
|
yellow
|
||||||
|
#FFFF00
|
||||||
|
olive
|
||||||
|
#808000
|
||||||
|
lime
|
||||||
|
#00FF00
|
||||||
|
green
|
||||||
|
#008000
|
||||||
|
aqua
|
||||||
|
#00FFFF
|
||||||
|
teal
|
||||||
|
#008080
|
||||||
|
blue
|
||||||
|
#0000FF
|
||||||
|
navy
|
||||||
|
#000080
|
||||||
|
fuchsia
|
||||||
|
#FF00FF
|
||||||
|
purple
|
||||||
|
#800080
|
27
test/paste.rs
Normal file
27
test/paste.rs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
static PROGNAME: &'static str = "./paste";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_combine_pairs_of_lines() {
|
||||||
|
let po = Command::new(PROGNAME)
|
||||||
|
.arg("-s")
|
||||||
|
.arg("-d")
|
||||||
|
.arg("\t\n")
|
||||||
|
.arg("html_colors.txt")
|
||||||
|
.output()
|
||||||
|
.unwrap_or_else(|err| panic!("{}", err));
|
||||||
|
|
||||||
|
let mut f = File::open(Path::new("html_colors.expected")).unwrap_or_else(|err| {
|
||||||
|
panic!("{}", err)
|
||||||
|
});
|
||||||
|
let mut expected = vec!();
|
||||||
|
match f.read_to_end(&mut expected) {
|
||||||
|
Ok(_) => {},
|
||||||
|
Err(err) => panic!("{}", err)
|
||||||
|
}
|
||||||
|
assert_eq!(String::from_utf8(po.stdout).unwrap(), String::from_utf8(expected).unwrap());
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue