From f19e173585c454355b9221059fed4dc1a9115792 Mon Sep 17 00:00:00 2001 From: Joseph LaFreniere Date: Thu, 22 Dec 2022 16:45:53 -0600 Subject: [PATCH] fixup! Implement async `postproc_pagebreaks` --- src/adapters/postproc.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/adapters/postproc.rs b/src/adapters/postproc.rs index 7ca1bb5..6e311ac 100644 --- a/src/adapters/postproc.rs +++ b/src/adapters/postproc.rs @@ -149,22 +149,22 @@ pub fn postproc_prefix(line_prefix: &str, inp: impl AsyncRead + Send) -> impl As pub fn postproc_pagebreaks(line_prefix: &str, inp: impl AsyncRead) -> impl AsyncRead { let form_feed = b'\x0c'; let regex = regex::bytes::Regex::new("\n").unwrap(); - let mut page_count = 0; - let mut line_prefix = format!("\n{}Page {}:", line_prefix, page_count + 1); + let mut page_count = 1; + let mut line_prefix = format!("\n{}Page {}:", line_prefix, page_count); let inp_stream = ReaderStream::new(inp); let oup_stream = stream! { - yield Ok(Bytes::copy_from_slice(line_prefix.as_bytes())); for await chunk in inp_stream { match chunk { Err(e) => yield Err(e), Ok(chunk) => { - let chunk_iter = chunk.split(|byte| byte == &form_feed); + let chunk_iter = chunk.split_inclusive(|byte| byte == &form_feed); for sub_chunk in chunk_iter { - if sub_chunk.contains(&b'\n') { - yield Ok(Bytes::copy_from_slice(®ex.replace_all(&sub_chunk, line_prefix.as_bytes()))); + if sub_chunk == [form_feed] { page_count += 1; line_prefix = format!("\n{}Page {}:", line_prefix, page_count); + } else { + yield Ok(Bytes::copy_from_slice(®ex.replace_all(&sub_chunk, line_prefix.as_bytes()))); } } }