file to c iters ok

master
Avril 5 years ago
parent 0edf5e44d6
commit f44a998fe8
Signed by: flanchan
GPG Key ID: 284488987C31F630

1
.gitignore vendored

@ -4,3 +4,4 @@ bin/
obj/ obj/
sha256_literal/ sha256_literal/
**/target/ **/target/
test.txt

@ -3,36 +3,23 @@ use std::{
Read, Read,
}, },
}; };
use heaparray::{
heap,
};
pub struct BufferedReadIter<F> pub struct ByteIter<F: Read>
where F: Read (F, [u8; 1]);
{
iter: F,
buffer: Box<[u8]>,
buffer_len: usize,
}
impl<F: Read> Iterator for BufferedReadIter<F> impl<T> Iterator for ByteIter<T>
where T: Read
{ {
type Item = Box<[u8]>; type Item = u8;
fn next(&mut self) -> Option<Self::Item> fn next(&mut self) -> Option<Self::Item>
{ {
if self.buffer_len >= self.buffer.len() { if let Ok(read) = self.0.read(&mut self.1[..])
// Buffer is full.
return Some(self.swap_out());
}
if let Ok(read) = self.iter.read(&mut self.buffer[self.buffer_len..])
{ {
self.buffer_len += read; if read < 1 {
if self.buffer_len ==0 {
None None
} else { } else {
Some(self.swap_out()) Some(self.1[0])
} }
} else { } else {
None None
@ -40,32 +27,19 @@ impl<F: Read> Iterator for BufferedReadIter<F>
} }
} }
impl<F: Read> BufferedReadIter<F> impl<T> ByteIter<T>
{ where T: Read
fn swap_out(&mut self) -> Box<[u8]>
{ {
let len = self.buffer_len; fn new(stream: T) -> Self {
self.buffer_len=0; Self(stream, [0u8; 1])
heaparray::box_slice(&mut self.buffer[..len])
}
pub fn new(iter: F, buffer_len: usize) -> Self {
let buffer = heap![u8; buffer_len].into_box();
Self {
iter,
buffer_len: 0,
buffer,
}
} }
} }
pub trait ReadIterExt: Read + Sized pub trait ByteIterExt: Read + Sized
{ {
fn into_iter(self, buffer_len: usize) -> BufferedReadIter<Self> fn into_byte_iter(self) -> ByteIter<Self>
{ {
BufferedReadIter::new(self, buffer_len) ByteIter::new(self)
} }
} }
impl<T: Read> ReadIterExt for T{} impl<T: Read> ByteIterExt for T{}

@ -1,12 +1,15 @@
mod group; mod group;
pub use group::*; pub use group::*;
mod read;
pub use read::*;
mod file; mod file;
pub use file::*; pub use file::*;
pub mod prelude pub mod prelude
{ {
pub use super::{ pub use super::{
ReadIterExt, GroupExt, ReadIterExt, GroupExt, ByteIterExt,
}; };
} }

@ -0,0 +1,70 @@
use std::{
io::{
Read,
},
};
use heaparray::{
heap,
};
pub struct BufferedReadIter<F>
where F: Read
{
iter: F,
buffer: Box<[u8]>,
buffer_len: usize,
buffer_push: usize,
}
impl<F: Read> Iterator for BufferedReadIter<F>
{
type Item = u8;
fn next(&mut self) -> Option<Self::Item>
{
if self.buffer_len > 0 {
let output = self.buffer[self.buffer_push];
self.buffer_push += 1;
self.buffer_len -= 1;
return Some(output);
}
self.buffer_push = 0;
if let Ok(read) = self.iter.read(&mut self.buffer[self.buffer_len..])
{
self.buffer_len += read;
if self.buffer_len == 0 {
None
} else {
self.next()
}
} else {
None
}
}
}
impl<F: Read> BufferedReadIter<F>
{
pub fn new(iter: F, buffer_len: usize) -> Self {
let buffer = heap![u8; buffer_len].into_box();
Self {
iter,
buffer_len: 0,
buffer_push: 0,
buffer,
}
}
}
pub trait ReadIterExt: Read + Sized
{
fn into_iter(self, buffer_len: usize) -> BufferedReadIter<Self>
{
BufferedReadIter::new(self, buffer_len)
}
}
impl<T: Read> ReadIterExt for T{}

@ -1,3 +1,5 @@
#![allow(dead_code)]
extern crate heaparray; extern crate heaparray;
use std::{ use std::{
@ -11,18 +13,23 @@ use std::{
mod iter; mod iter;
use iter::prelude::*; use iter::prelude::*;
mod translate;
fn main() -> Result<(), Box<dyn Error>>{ fn main() -> Result<(), Box<dyn Error>>{
/*let file = OpenOptions::new() let file = OpenOptions::new()
.read(true) .read(true)
.open("test.txt")?; .open("test.txt")?;
for buf in file.into_iter(2).group_at(2) println!("{{");
for buf in file.into_iter(4)
.map(|byte| format!("0x{:02x},", byte))
.group_at(4)
.map(|strs| format!("\t{}", strs.join(" ")))
{ {
println!("{:?}", buf); println!("{}", buf);
}*/ }
println!("}}");
Ok(()) Ok(())
} }

@ -0,0 +1,2 @@
mod sanitise;
pub use sanitise::*;

@ -0,0 +1,22 @@
/// Escape text for insertion.
pub fn c_escape<T>(input: T) -> String
where T: AsRef<str>
{
let input = input.as_ref();
let mut chr = Vec::new();
for c in input.chars() {
match c {
'\\' => {
chr.push('\\');
chr.push('\\');
},
'\"' => {
chr.push('\\');
chr.push('\"');
},
_ => chr.push(c),
};
}
chr.into_iter().collect()
}
Loading…
Cancel
Save