cat: remove recursion

This commit is contained in:
kevgeniy 2016-10-24 20:18:44 +03:00
parent 6228b06e29
commit 0d56009c3a

View file

@ -188,8 +188,8 @@ fn write_lines(files: Vec<String>,
} }
// write***_to_end methods // write***_to_end methods
// Write all simbols till end of line or end of buffer // Write all symbols till end of line or end of buffer is reached
// Return the number of written bytes - 1 or 0 if the end of buffer is reached // Return the (number of written symbols + 1) or 0 if the end of buffer is reached
fn write_to_end<W: Write>(in_buf: &[u8], writer: &mut W) -> usize { fn write_to_end<W: Write>(in_buf: &[u8], writer: &mut W) -> usize {
match in_buf.iter().position(|c| *c == '\n' as u8) { match in_buf.iter().position(|c| *c == '\n' as u8) {
Some(p) => { Some(p) => {
@ -203,21 +203,23 @@ fn write_to_end<W: Write>(in_buf: &[u8], writer: &mut W) -> usize {
} }
} }
fn write_tab_to_end<W: Write>(in_buf: &[u8], writer: &mut W) -> usize { fn write_tab_to_end<W: Write>(mut in_buf: &[u8], writer: &mut W) -> usize {
match in_buf.iter().position(|c| *c == '\n' as u8 || *c == '\t' as u8) { loop {
Some(p) => { match in_buf.iter().position(|c| *c == '\n' as u8 || *c == '\t' as u8) {
writer.write_all(&in_buf[..p]).unwrap(); Some(p) => {
if in_buf[p] == '\n' as u8 { writer.write_all(&in_buf[..p]).unwrap();
p + 1 if in_buf[p] == '\n' as u8 {
} else { return p + 1;
writer.write_all("^I".as_bytes()).unwrap(); } else {
write_tab_to_end(&in_buf[p + 1..], writer) writer.write_all("^I".as_bytes()).unwrap();
in_buf = &in_buf[p + 1..];
}
} }
} None => {
None => { writer.write_all(in_buf).unwrap();
writer.write_all(in_buf).unwrap(); return 0;
0 }
} };
} }
} }
@ -241,4 +243,4 @@ fn write_nonprint_to_end<W: Write>(in_buf: &[u8], writer: &mut W, tab: &[u8]) ->
count += 1; count += 1;
} }
if count != in_buf.len() { count + 1 } else { 0 } if count != in_buf.len() { count + 1 } else { 0 }
} }