mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-31 13:07:46 +00:00
tr: improve speed slightly
This commit is contained in:
parent
f07fedfcf9
commit
f628e70d4a
1 changed files with 26 additions and 13 deletions
39
src/tr/tr.rs
39
src/tr/tr.rs
|
@ -13,10 +13,9 @@
|
|||
extern crate collections;
|
||||
extern crate getopts;
|
||||
|
||||
use collections::bitv_set::BitvSet;
|
||||
use collections::vec_map::VecMap;
|
||||
use getopts::OptGroup;
|
||||
use std::char::from_u32;
|
||||
use std::collections::{BitvSet, VecMap};
|
||||
use std::io::print;
|
||||
use std::io::stdio::{stdin,stdout};
|
||||
use std::iter::FromIterator;
|
||||
|
@ -28,6 +27,7 @@ mod util;
|
|||
static NAME : &'static str = "tr";
|
||||
static VERSION : &'static str = "1.0.0";
|
||||
|
||||
#[inline]
|
||||
fn unescape_char(c: char) -> char {
|
||||
match c {
|
||||
'a' => 0x07u8 as char,
|
||||
|
@ -41,6 +41,7 @@ fn unescape_char(c: char) -> char {
|
|||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unescape(v: Vec<char>) -> Vec<char> {
|
||||
let mut out = Vec::new();
|
||||
let mut input = v.as_slice();
|
||||
|
@ -60,6 +61,7 @@ fn unescape(v: Vec<char>) -> Vec<char> {
|
|||
out
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn expand_range(from: char, to: char) -> Vec<char> {
|
||||
range(from as u32, to as u32 + 1).map(|c| from_u32(c).unwrap()).collect()
|
||||
}
|
||||
|
@ -109,12 +111,16 @@ fn delete(set: Vec<char>, complement: bool) {
|
|||
}
|
||||
|
||||
fn tr(set1: &[char], set2: &[char]) {
|
||||
let mut map = VecMap::<char>::new();
|
||||
let mut out = stdout();
|
||||
const BUFFER_LEN: uint = 1024;
|
||||
|
||||
let mut map = VecMap::new();
|
||||
let mut stdout = stdout();
|
||||
let mut outbuffer = String::with_capacity(BUFFER_LEN);
|
||||
|
||||
let set2_len = set2.len();
|
||||
for i in range(0, set1.len()) {
|
||||
if i >= set2.len() {
|
||||
map.insert(set1[i] as uint, set2[set2.len()-1]);
|
||||
if i >= set2_len {
|
||||
map.insert(set1[i] as uint, set2[set2_len - 1]);
|
||||
} else {
|
||||
map.insert(set1[i] as uint, set2[i]);
|
||||
}
|
||||
|
@ -127,22 +133,29 @@ fn tr(set1: &[char], set2: &[char]) {
|
|||
Some(t) => *t,
|
||||
None => inc,
|
||||
};
|
||||
out.write_char(trc).unwrap();
|
||||
outbuffer.push(trc);
|
||||
if outbuffer.len() >= BUFFER_LEN {
|
||||
stdout.write_str(outbuffer.as_slice()).unwrap();
|
||||
outbuffer.clear();
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
panic!("{}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
if outbuffer.len() > 0 {
|
||||
stdout.write_str(outbuffer.as_slice()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn usage(opts: &[OptGroup]) {
|
||||
println!("{} {}", NAME, VERSION);
|
||||
println!("");
|
||||
println!("Usage:");
|
||||
println!(" {} [OPTIONS] SET1 [SET2]", NAME);
|
||||
println!("");
|
||||
print(getopts::usage("Translate or delete characters.", opts).as_slice());
|
||||
println!("{} {}", NAME, VERSION);
|
||||
println!("");
|
||||
println!("Usage:");
|
||||
println!(" {} [OPTIONS] SET1 [SET2]", NAME);
|
||||
println!("");
|
||||
print(getopts::usage("Translate or delete characters.", opts).as_slice());
|
||||
}
|
||||
|
||||
pub fn uumain(args: Vec<String>) -> int {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue