libdelimit, Makefile: adds test for libdelimit, changes API; fop(1): changes to new libdelimit API
This commit is contained in:
@@ -23,14 +23,14 @@ use std::{
|
||||
|
||||
const BUFFER_SIZE: usize = 4096;
|
||||
|
||||
pub struct Delimited {
|
||||
stream: Box<dyn Read>,
|
||||
pub struct Delimited<T: Read> {
|
||||
delimiter: Vec<u8>,
|
||||
buffer: Vec<u8>
|
||||
buffer: Vec<u8>,
|
||||
stream: T,
|
||||
}
|
||||
|
||||
impl Delimited {
|
||||
pub fn new(stream: Box<dyn Read>, delimiter: &[u8]) -> Self {
|
||||
impl<T> Delimited<T> where T: Read {
|
||||
pub fn new(stream: T, delimiter: &[u8]) -> Self {
|
||||
Delimited {
|
||||
stream,
|
||||
delimiter: delimiter.to_vec(),
|
||||
@@ -39,7 +39,7 @@ impl Delimited {
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Delimited {
|
||||
impl<T> Iterator for Delimited<T> where T: Read {
|
||||
type Item = Result<Vec<u8>>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
@@ -89,3 +89,23 @@ fn find_subslice(stack: &[u8], key: &[u8]) -> Option<usize> {
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use Delimited;
|
||||
|
||||
#[test]
|
||||
fn testing() {
|
||||
let d = '\u{1E}'.to_string();
|
||||
let input = vec!["meow", "woof", "ribbit"];
|
||||
let r = input.join(&d);
|
||||
|
||||
let mut output = Delimited::new(r.as_bytes(), d.as_bytes());
|
||||
|
||||
let mut i = 0;
|
||||
while let Some(item) = output.next() {
|
||||
assert_eq!(input[i].as_bytes(), item.unwrap());
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user