libdelimit, Makefile: adds test for libdelimit, changes API; fop(1): changes to new libdelimit API

This commit is contained in:
Emma Tebibyte 2025-10-30 19:32:03 -06:00
parent 4aab77bee4
commit 5cc1e2067b
Signed by: emma
GPG Key ID: 427287A2F16F44FA
3 changed files with 33 additions and 9 deletions

View File

@ -71,12 +71,16 @@ TESTS != printf '%s\n' "$(TESTFILES)" | xargs -n1 basename \
include $(TESTFILES)
.PHONY: test
test: all $(TESTS) /tmp/getopt
test: all $(TESTS) /tmp/delimit /tmp/getopt
@echo $(TESTS)
/tmp/delimit
/tmp/getopt
/tmp/delimit: src/libdelimit.rs
$(RUSTC) --test -o $@ src/libdelimit.rs
/tmp/getopt: src/libgetopt.rs
$(RUSTC) --test -o /tmp/getopt src/libgetopt.rs
$(RUSTC) --test -o $@ src/libgetopt.rs
.PHONY: docs
docs: docs/ build

View File

@ -92,7 +92,7 @@ fn main() -> ExitCode {
exit(usage(&argv[0]).into());
});
let stdin = Box::new(stdin().lock());
let stdin = stdin().lock();
let mut input = Delimited::new(stdin, d.clone().as_bytes());
let mut n = 0;

View File

@ -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;
}
}
}