Decode XeTeX input files and streams with original byte spans

This commit is contained in:
gloamxun committed 2026-11-21 17:10:46 +08:00
1 parent 22e3ba993d
commit d11ac0cba1
18 files changed
+1040 -109

No files matched your search

+72 -1
View File
@@ -2,12 +2,13 @@
//! by this handle, not an OS descriptor cursor hidden behind BufReader. This
//! journal does not serialize read-ahead buffers or authorize effect replay.
use std::{
collections::VecDeque,
fs::File,
io::{self, BufRead, Write},
path::PathBuf,
sync::Arc,
};
use texrra_tex_core::resources::ResourceVersion;
use texrra_tex_core::{execution::XeLineFraming, resources::ResourceVersion};
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DescriptorIdentity {
@@ -208,6 +209,76 @@ pub(crate) fn physical_line(reader: &mut (impl BufRead + ?Sized)) -> LineTransfe
error: result.err(),
}
}
/// One XeTeX physical delimiter. Bytes read for BOM detection that belong to
/// a later line stay in this handle's lookahead until that line is delivered.
pub(crate) fn xe_physical_line(
reader: &mut (impl BufRead + ?Sized),
pending: &mut VecDeque<u8>,
mut framing: XeLineFraming,
) -> LineTransfer {
let mut bytes = Vec::new();
let mut observed_eof = false;
let result = (|| -> io::Result<()> {
if framing == XeLineFraming::Auto {
while pending.len() < 3 {
let buffer = reader.fill_buf()?;
if buffer.is_empty() {
observed_eof = true;
break;
}
let count = (3 - pending.len()).min(buffer.len());
pending.extend(&buffer[..count]);
reader.consume(count);
}
let (detected, bom) = XeLineFraming::detect(pending.make_contiguous());
framing = detected;
bytes.extend(pending.drain(..bom));
}
let width = framing.unit_width();
loop {
if width == 1 && pending.is_empty() {
let buffer = reader.fill_buf()?;
if buffer.is_empty() {
observed_eof = true;
break;
}
let terminator = buffer.iter().position(|&byte| matches!(byte, b'\r' | b'\n'));
let count = terminator.map_or(buffer.len(), |at| at + 1);
bytes.extend_from_slice(&buffer[..count]);
reader.consume(count);
if terminator.is_some() {
break;
}
continue;
}
let start = bytes.len();
while bytes.len() - start < width {
if let Some(byte) = pending.pop_front() {
bytes.push(byte);
continue;
}
let buffer = reader.fill_buf()?;
if buffer.is_empty() {
observed_eof = true;
return Ok(());
}
let count = (width - (bytes.len() - start)).min(buffer.len());
bytes.extend_from_slice(&buffer[..count]);
reader.consume(count);
}
if matches!(framing.code_unit(&bytes[start..]), Some(10 | 13)) {
break;
}
}
Ok(())
})();
LineTransfer {
bytes,
observed_eof,
error: result.err(),
}
}
pub(crate) struct WriteTransfer {
pub accepted: usize,
pub attempts: Vec<WriteAttempt>,