mirror of
https://github.com/FliegendeWurst/ripgrep-all.git
synced 2024-11-08 22:10:37 +00:00
fixup! Implement async postproc_pagebreaks
This commit is contained in:
parent
af168efe1a
commit
f19e173585
@ -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 {
|
pub fn postproc_pagebreaks(line_prefix: &str, inp: impl AsyncRead) -> impl AsyncRead {
|
||||||
let form_feed = b'\x0c';
|
let form_feed = b'\x0c';
|
||||||
let regex = regex::bytes::Regex::new("\n").unwrap();
|
let regex = regex::bytes::Regex::new("\n").unwrap();
|
||||||
let mut page_count = 0;
|
let mut page_count = 1;
|
||||||
let mut line_prefix = format!("\n{}Page {}:", line_prefix, page_count + 1);
|
let mut line_prefix = format!("\n{}Page {}:", line_prefix, page_count);
|
||||||
|
|
||||||
let inp_stream = ReaderStream::new(inp);
|
let inp_stream = ReaderStream::new(inp);
|
||||||
let oup_stream = stream! {
|
let oup_stream = stream! {
|
||||||
yield Ok(Bytes::copy_from_slice(line_prefix.as_bytes()));
|
|
||||||
for await chunk in inp_stream {
|
for await chunk in inp_stream {
|
||||||
match chunk {
|
match chunk {
|
||||||
Err(e) => yield Err(e),
|
Err(e) => yield Err(e),
|
||||||
Ok(chunk) => {
|
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 {
|
for sub_chunk in chunk_iter {
|
||||||
if sub_chunk.contains(&b'\n') {
|
if sub_chunk == [form_feed] {
|
||||||
yield Ok(Bytes::copy_from_slice(®ex.replace_all(&sub_chunk, line_prefix.as_bytes())));
|
|
||||||
page_count += 1;
|
page_count += 1;
|
||||||
line_prefix = format!("\n{}Page {}:", line_prefix, page_count);
|
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())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user