From 937e3f65797277df782e613c7284dac62accbb9c Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 30 Mar 2025 13:08:38 +0100 Subject: [PATCH] Patching operation buf-reader impl started MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for rematch's current commit: Future small blessing − 末小吉 --- src/bin/repatch/op.rs | 77 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/src/bin/repatch/op.rs b/src/bin/repatch/op.rs index 0a9f54f..61b8d90 100644 --- a/src/bin/repatch/op.rs +++ b/src/bin/repatch/op.rs @@ -68,7 +68,7 @@ pub trait Operation: Send + Sync /// /// The type must be `Send`, as it is possible for the operation instance to be moved between threads between sequential runs. /// The state is only reused for input streams from the same input. As processing unrelated streams (different inputs) can happen concurrently, each new input has a new state object created for it (see `create_state_for()`.) - type State: Any + Send + 'static; + type State: Any + Send + 'static /* = NoState */; /// Create a state for this input stream. /// @@ -134,3 +134,78 @@ fn _assert_obj_safe(_: &(dyn OperationDyn + Send + Sync + 'static), _: BoxOperat /// A boxed operation. Operations are generated dynamically and so are dispatched on dynamically. pub type BoxOperation<'bx> = Box; + +/// Generic processing functions +pub mod proc { + use super::*; + use io::BufRead; + + pub fn proc_split_stream> + (op: &Op, + mut input: I, + pa: P, + mut output: O) -> io::Result<()> + { + let pa = pa.as_ref(); + let mut buf = io::BufReader::new(input); + + + //TODO: Turn this into an iterator type that takes a stream, reads into a growing buffer until `P` is found, then returns the buffer up to, but not including, `P`, removes that part from the buffer, and continues. + todo!() + } + + /// An iterator that grows an internal buffer from an input stream until a substring of bytes in that buffer is found. + /// The iterator then yeilds the part of the buffer containing the pre-substring section, removes that section and the subsrtring from itself, and continues. + #[derive(Debug)] + pub struct StreamSplitIter<'a, T: ?Sized> + { + stream: &'a mut T, + buffer: Vec, + orig_cap: usize, + split_on: &'a str, + } + + impl<'a, T: ?Sized + 'a> StreamSplitIter<'a, T> + where T: io::Read + { + fn try_extend_buffer_once(&mut self) -> io::Result<&[u8]> + { + let i = self.buffer.len(); + self.buffer.resize(i + self.orig_cap, 0); + let sl = { + let sl = &mut self.buffer[i..]; + match self.stream.read(sl) { + Err(err) => { + // Reset buffer size + self.buffer.resize(i, 0); + return Err(err); + }, + Ok(0) => { + self.buffer.resize(i, 0); + return Ok(&[]); + }, + Ok(n) if n != sl.len() => { + self.buffer.resize(n, 0); + &self.buffer[i..(i+n)] + } + Ok(n) => &self.buffer[i..(i+n)], + } + }; + Ok(sl) + } + } + + impl<'a, T: ?Sized + 'a> Iterator for StreamSplitIter<'a, T> + where T: io::Read + { + type Item = io::Result>; + fn next(&mut self) -> Option + { + todo!() + } + } + +}