Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/src/bounded.rs

1.5 KiB, 1 run

created by r153942159:77361, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Lines read in bounded memory: a line of any length, a gigabyte with no newline among them,
2//! costs the reader's buffer and the bytes kept, never the line.
3
4use oxedyne_fe2o3_core::prelude::*;
5
6use std::io::BufRead;
7
8
9#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
10pub struct Line {
11 pub len: u64, // bytes before the line's `\n`, or before the NUL that stopped it
12 pub nul: bool, // a NUL stopped it: the text is binary
13}
14
15/// The next line of `r`, with bytes `skip..skip + cap` of it, not counting its `\n`, put in `buf`
16/// and the rest passed over unheld; `None` at the end. A NUL ends the line, so a caller that
17/// stops on `nul` reads no further into a binary file.
18pub fn line<R: BufRead>(r: &mut R, buf: &mut Vec<u8>, skip: u64, cap: usize) -> Outcome<Option<Line>> {
19 buf.clear();
20 let mut len = 0u64;
21 let mut any = false;
22 loop {
23 let chunk = res!(r.fill_buf(), IO, Read);
24 if chunk.is_empty() {
25 return Ok(any.then_some(Line { len, nul: false }));
26 }
27 any = true;
28 let stop = chunk.iter().position(|b| *b == b'\n' || *b == 0);
29 let part = &chunk[..stop.unwrap_or(chunk.len())];
30 let from = (skip.saturating_sub(len).min(part.len() as u64)) as usize;
31 let to = part.len().min(from.saturating_add(cap - buf.len()));
32 buf.extend_from_slice(&part[from..to]);
33 len += part.len() as u64;
34 let nul = stop.map(|i| chunk[i] == 0);
35 let used = stop.map_or(part.len(), |i| i + 1);
36 r.consume(used);
37 if let Some(nul) = nul {
38 return Ok(Some(Line { len, nul }));
39 }
40 }
41}