Files
texrra-engine/crates/texrra-tex-core/src/execution/input_profile.rs
T

38 lines
1.2 KiB
Rust

//! Input decoding is a job profile, independent of e-TeX primitive availability.
//! Both paths retain original byte offsets; the byte path never recodes a file.
#[derive(
Clone, Copy, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
)]
pub enum InputProfile {
TeX8Bit,
#[default]
UnicodeScalar,
}
impl InputProfile {
pub(super) fn characters(self, data: &[u8]) -> Result<Characters<'_>, std::str::Utf8Error> {
Ok(match self {
Self::TeX8Bit => Characters::Bytes(data.iter().enumerate()),
Self::UnicodeScalar => Characters::Unicode(std::str::from_utf8(data)?.char_indices()),
})
}
pub(super) fn width(self, c: char) -> usize {
match self {
Self::TeX8Bit => 1,
Self::UnicodeScalar => c.len_utf8(),
}
}
}
pub(super) enum Characters<'a> {
Bytes(std::iter::Enumerate<std::slice::Iter<'a, u8>>),
Unicode(std::str::CharIndices<'a>),
}
impl Iterator for Characters<'_> {
type Item = (usize, char);
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Bytes(iter) => iter.next().map(|(i, &b)| (i, char::from(b))),
Self::Unicode(iter) => iter.next(),
}
}
}