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 -35
View File
@@ -112,6 +112,7 @@ struct Input {
reader: Box<dyn BufRead + Send>,
path: PathBuf,
offset: u64,
xe_lookahead: std::collections::VecDeque<u8>,
}
struct Output {
handle: u64,
@@ -1424,6 +1425,67 @@ pub(crate) fn absolute_output(cwd: &Path, path: &Path) -> Result<PathBuf, String
Ok(parent.join(path.file_name().ok_or("output requires a filename")?))
}
impl Files {
fn read_input_line(
&mut self,
stream: u8,
framing: Option<texrra_tex_core::execution::XeLineFraming>,
) -> Result<Option<Vec<u8>>, String> {
let Some(input) = self.inputs.get_mut(&stream) else {
let error = "host input stream is not open".to_owned();
self.stream_io.push(StreamEffect::ReadLine {
stream,
handle: None,
consumed_before: None,
consumed: std::sync::Arc::from([]),
observed_eof: false,
delivered: false,
error: Some(error.clone()),
});
return Err(error);
};
let before = input.offset;
let transfer = match framing {
Some(framing) => native_stream::xe_physical_line(
input.reader.as_mut(), &mut input.xe_lookahead, framing,
),
None => native_stream::physical_line(input.reader.as_mut()),
};
input.offset += transfer.bytes.len() as u64;
let error = transfer.error.map(|e| format!("read stream {stream}: {e}"));
self.stream_io.push(StreamEffect::ReadLine {
stream,
handle: Some(input.handle),
consumed_before: Some(before),
consumed: std::sync::Arc::from(transfer.bytes.clone()),
observed_eof: transfer.observed_eof,
delivered: error.is_none(),
error: error.clone(),
});
if let Some(error) = error {
self.world.observe_read_error(&input.path, error.clone());
Err(error)
} else {
let line = (!transfer.bytes.is_empty()).then_some(transfer.bytes);
self.world.observe_stream_read(&input.path, before, line.as_deref());
if framing == Some(texrra_tex_core::execution::XeLineFraming::Auto) {
// The detection may have inspected bytes in the following
// line. Validate those bytes without advancing delivered I/O.
let peeked = input.xe_lookahead.make_contiguous();
if !peeked.is_empty() {
self.world.observe_stream_read(&input.path, input.offset, Some(peeked));
}
if transfer.observed_eof && line.is_some() {
self.world.observe_stream_read(
&input.path, input.offset + peeked.len() as u64, None,
);
}
}
Ok(line)
}
}
}
impl SourceProvider for Files {
#[cfg(all(target_os = "linux", target_env = "gnu"))]
fn match_pdf_regex(
@@ -1647,6 +1709,7 @@ impl SourceProvider for Files {
reader,
path,
offset: 0,
xe_lookahead: Default::default(),
},
);
Ok(Some(opened))
@@ -1660,41 +1723,14 @@ impl SourceProvider for Files {
result.map(|opened| opened.map(|o| o.path.display().to_string()))
}
fn read_line(&mut self, stream: u8) -> Result<Option<Vec<u8>>, String> {
let Some(input) = self.inputs.get_mut(&stream) else {
let error = "host input stream is not open".to_owned();
self.stream_io.push(StreamEffect::ReadLine {
stream,
handle: None,
consumed_before: None,
consumed: std::sync::Arc::from([]),
observed_eof: false,
delivered: false,
error: Some(error.clone()),
});
return Err(error);
};
let before = input.offset;
let transfer = native_stream::physical_line(input.reader.as_mut());
input.offset += transfer.bytes.len() as u64;
let error = transfer.error.map(|e| format!("read stream {stream}: {e}"));
self.stream_io.push(StreamEffect::ReadLine {
stream,
handle: Some(input.handle),
consumed_before: Some(before),
consumed: std::sync::Arc::from(transfer.bytes.clone()),
observed_eof: transfer.observed_eof,
delivered: error.is_none(),
error: error.clone(),
});
if let Some(error) = error {
self.world.observe_read_error(&input.path, error.clone());
Err(error)
} else {
let line = (!transfer.bytes.is_empty()).then_some(transfer.bytes);
self.world
.observe_stream_read(&input.path, before, line.as_deref());
Ok(line)
}
self.read_input_line(stream, None)
}
fn read_xe_line(
&mut self,
stream: u8,
framing: texrra_tex_core::execution::XeLineFraming,
) -> Result<Option<Vec<u8>>, String> {
self.read_input_line(stream, Some(framing))
}
fn close_in(&mut self, stream: u8) -> Result<(), String> {
self.release_input(stream, ReleaseCause::Explicit, true);
@@ -2562,6 +2598,7 @@ mod generated_file_tests {
handle,
path: root.join("actual-stream"),
offset: 0,
xe_lookahead: Default::default(),
reader: Box::new(BufReader::new(reader)),
},
);