//! Job input decoding and the explicit XeTeX compatibility profile. //! 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, /// Per-file Unicode input encodings, XeTeX primitives and XDV semantics. XeTeX, } impl InputProfile { pub(super) fn characters(self, data: &[u8]) -> Result, std::str::Utf8Error> { Ok(match self { Self::TeX8Bit => Characters::Bytes(data.iter().enumerate()), Self::UnicodeScalar | Self::XeTeX => { Characters::Unicode(std::str::from_utf8(data)?.char_indices()) } }) } pub(super) fn width(self, c: char) -> usize { match self { Self::TeX8Bit => 1, Self::UnicodeScalar | Self::XeTeX => c.len_utf8(), } } } pub(super) enum Characters<'a> { Bytes(std::iter::Enumerate>), Unicode(std::str::CharIndices<'a>), } impl Iterator for Characters<'_> { type Item = (usize, char); fn next(&mut self) -> Option { match self { Self::Bytes(iter) => iter.next().map(|(i, &b)| (i, char::from(b))), Self::Unicode(iter) => iter.next(), } } }