1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 20:47:46 +00:00

Merge pull request #1043 from Shnatsel/stream-processing

Make tr streaming
This commit is contained in:
Jian Zeng img src=404 onerror=alert(document.domain) 2017-05-24 22:41:56 -05:00 committed by GitHub
commit 334c64ae26

View file

@ -6,6 +6,7 @@
* (c) Michael Gehring <mg@ebfe.org> * (c) Michael Gehring <mg@ebfe.org>
* (c) kwantam <kwantam@gmail.com> * (c) kwantam <kwantam@gmail.com>
* 20150428 created `expand` module to eliminate most allocs during setup * 20150428 created `expand` module to eliminate most allocs during setup
* (c) Sergey "Shnatsel" Davidoff <shnatsel@gmail.com>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
@ -19,7 +20,7 @@ extern crate uucore;
use bit_set::BitSet; use bit_set::BitSet;
use getopts::Options; use getopts::Options;
use std::io::{stdin, stdout, BufReader, BufWriter, Read, Write}; use std::io::{stdin, stdout, BufRead, BufWriter, Write};
use std::collections::HashMap; use std::collections::HashMap;
use expand::ExpandSet; use expand::ExpandSet;
@ -32,8 +33,11 @@ const BUFFER_LEN: usize = 1024;
fn delete(set: ExpandSet, complement: bool) { fn delete(set: ExpandSet, complement: bool) {
let mut bset = BitSet::new(); let mut bset = BitSet::new();
let mut stdout = stdout(); let stdin = stdin();
let mut locked_stdin = stdin.lock();
let mut buffered_stdout = BufWriter::new(stdout());
let mut buf = String::with_capacity(BUFFER_LEN + 4); let mut buf = String::with_capacity(BUFFER_LEN + 4);
let mut char_output_buffer: [u8; 4] = [0;4];
for c in set { for c in set {
bset.insert(c as usize); bset.insert(c as usize);
@ -47,15 +51,15 @@ fn delete(set: ExpandSet, complement: bool) {
} }
}; };
let mut reader = BufReader::new(stdin()); while let Ok(length) = locked_stdin.read_line(&mut buf) {
while let Ok(length) = reader.read_to_string(&mut buf) {
if length == 0 { break } if length == 0 { break }
{ // isolation to make borrow checker happy
let filtered = buf.chars() let filtered = buf.chars().filter(|c| is_allowed(*c));
.filter(|c| { is_allowed(*c) }) for c in filtered {
.collect::<String>(); let char_as_bytes = c.encode_utf8(&mut char_output_buffer);
safe_unwrap!(stdout.write_all(filtered.as_bytes())); buffered_stdout.write_all(char_as_bytes.as_bytes()).unwrap();
}
}
buf.clear(); buf.clear();
} }
} }
@ -63,8 +67,11 @@ fn delete(set: ExpandSet, complement: bool) {
fn tr<'a>(set1: ExpandSet<'a>, mut set2: ExpandSet<'a>) { fn tr<'a>(set1: ExpandSet<'a>, mut set2: ExpandSet<'a>) {
//let mut map = VecMap::new(); //let mut map = VecMap::new();
let mut map = HashMap::new(); let mut map = HashMap::new();
let stdout = stdout(); let stdin = stdin();
let mut locked_stdin = stdin.lock();
let mut buffered_stdout = BufWriter::new(stdout());
let mut buf = String::with_capacity(BUFFER_LEN + 4); let mut buf = String::with_capacity(BUFFER_LEN + 4);
let mut char_output_buffer: [u8; 4] = [0;4];
let mut s2_prev = '_'; let mut s2_prev = '_';
for i in set1 { for i in set1 {
@ -73,21 +80,14 @@ fn tr<'a>(set1: ExpandSet<'a>, mut set2: ExpandSet<'a>) {
map.insert(i as usize, s2_prev); map.insert(i as usize, s2_prev);
} }
let mut reader = BufReader::new(stdin()); while let Ok(length) = locked_stdin.read_line(&mut buf) {
let mut writer = BufWriter::new(stdout);
while let Ok(length) = reader.read_to_string(&mut buf) {
if length == 0 { break } if length == 0 { break }
{ { // isolation to make borrow checker happy
let mut chars = buf.chars(); let output_stream = buf.chars().map(|c| *map.get(&(c as usize)).unwrap_or(&c));
for c in output_stream {
while let Some(ch) = chars.next() { let char_as_bytes = c.encode_utf8(&mut char_output_buffer);
let trc = match map.get(&(ch as usize)) { buffered_stdout.write_all(char_as_bytes.as_bytes()).unwrap();
Some(t) => *t,
None => ch,
};
safe_unwrap!(writer.write_all(format!("{}", trc).as_ref()));
} }
} }