mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
tr: add --complement
This commit is contained in:
parent
1669c76442
commit
5fb465a9cd
1 changed files with 27 additions and 6 deletions
33
tr/tr.rs
33
tr/tr.rs
|
@ -1,4 +1,5 @@
|
||||||
#![crate_id(name="tr", vers="1.0.0", author="Michael Gehring")]
|
#![crate_id(name="tr", vers="1.0.0", author="Michael Gehring")]
|
||||||
|
#![feature(macro_rules)]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
|
@ -22,6 +23,9 @@ use std::iter::FromIterator;
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
|
#[path="../common/util.rs"]
|
||||||
|
mod util;
|
||||||
|
|
||||||
static NAME : &'static str = "tr";
|
static NAME : &'static str = "tr";
|
||||||
static VERSION : &'static str = "1.0.0";
|
static VERSION : &'static str = "1.0.0";
|
||||||
|
|
||||||
|
@ -82,7 +86,7 @@ fn expand_set(s: &str) -> Vec<char> {
|
||||||
set
|
set
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete(set: Vec<char>) {
|
fn delete(set: Vec<char>, complement: bool) {
|
||||||
let mut bset = BitvSet::new();
|
let mut bset = BitvSet::new();
|
||||||
let mut out = stdout();
|
let mut out = stdout();
|
||||||
|
|
||||||
|
@ -90,9 +94,15 @@ fn delete(set: Vec<char>) {
|
||||||
bset.insert(c as uint);
|
bset.insert(c as uint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let is_allowed = if complement {
|
||||||
|
|c: char| bset.contains(&(c as uint))
|
||||||
|
} else {
|
||||||
|
|c: char| !bset.contains(&(c as uint))
|
||||||
|
};
|
||||||
|
|
||||||
for c in stdin().chars() {
|
for c in stdin().chars() {
|
||||||
match c {
|
match c {
|
||||||
Ok(c) if !bset.contains(&(c as uint)) => out.write_char(c).unwrap(),
|
Ok(c) if is_allowed(c) => out.write_char(c).unwrap(),
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => fail!("{}", err),
|
Err(err) => fail!("{}", err),
|
||||||
};
|
};
|
||||||
|
@ -139,6 +149,8 @@ fn usage(opts: &[OptGroup]) {
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let args: Vec<StrBuf> = os::args().iter().map(|x| x.to_strbuf()).collect();
|
let args: Vec<StrBuf> = os::args().iter().map(|x| x.to_strbuf()).collect();
|
||||||
let opts = [
|
let opts = [
|
||||||
|
getopts::optflag("c", "complement", "use the complement of SET1"),
|
||||||
|
getopts::optflag("C", "", "same as -c"),
|
||||||
getopts::optflag("d", "delete", "delete characters in SET1"),
|
getopts::optflag("d", "delete", "delete characters in SET1"),
|
||||||
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"),
|
||||||
|
@ -146,12 +158,15 @@ pub fn main() {
|
||||||
|
|
||||||
let matches = match getopts::getopts(args.tail(), opts) {
|
let matches = match getopts::getopts(args.tail(), opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(err) => fail!("{}", err.to_err_msg()),
|
Err(err) => {
|
||||||
|
show_error!(1, "{}", err.to_err_msg());
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches.opt_present("help") {
|
if matches.opt_present("help") {
|
||||||
usage(opts);
|
usage(opts);
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches.opt_present("version") {
|
if matches.opt_present("version") {
|
||||||
|
@ -162,15 +177,21 @@ pub fn main() {
|
||||||
if matches.free.len() == 0 {
|
if matches.free.len() == 0 {
|
||||||
usage(opts);
|
usage(opts);
|
||||||
os::set_exit_status(1);
|
os::set_exit_status(1);
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let dflag = matches.opt_present("d");
|
let dflag = matches.opt_present("d");
|
||||||
|
let cflag = matches.opts_present(["c".to_strbuf(), "C".to_strbuf()]);
|
||||||
let sets = matches.free;
|
let sets = matches.free;
|
||||||
|
|
||||||
|
if cflag && !dflag {
|
||||||
|
show_error!(1, "-c is only supported with -d");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if dflag {
|
if dflag {
|
||||||
let set1 = expand_set(sets.get(0).as_slice());
|
let set1 = expand_set(sets.get(0).as_slice());
|
||||||
delete(set1);
|
delete(set1, cflag);
|
||||||
} else {
|
} else {
|
||||||
let set1 = expand_set(sets.get(0).as_slice());
|
let set1 = expand_set(sets.get(0).as_slice());
|
||||||
let set2 = expand_set(sets.get(1).as_slice());
|
let set2 = expand_set(sets.get(1).as_slice());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue