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

111 lines
3.9 KiB
Rust

//! Web2C character translation and printability for physical TeX byte I/O.
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct ByteTranslation {
input: [[u8; 32]; 8],
output: [[u8; 32]; 8],
printable: [u64; 4],
}
impl Default for ByteTranslation {
fn default() -> Self {
let identity =
std::array::from_fn(|row| std::array::from_fn(|column| (row * 32 + column) as u8));
Self {
input: identity,
output: identity,
printable: [u64::MAX << 32, u64::MAX >> 1, 0, 0],
}
}
}
impl ByteTranslation {
pub fn from_tcx(data: &[u8]) -> Result<Self, String> {
let mut table = Self::default();
for (line, bytes) in data.split(|&b| b == b'\n').enumerate() {
let record = bytes.split(|&b| b == b'%').next().unwrap_or_default();
let record = std::str::from_utf8(record)
.map_err(|_| format!("invalid TCX entry on line {}", line + 1))?;
let mut fields = record.split_ascii_whitespace();
let Some(field) = fields.next() else {
continue;
};
let parse = |field| {
number(field).ok_or_else(|| format!("invalid TCX entry on line {}", line + 1))
};
let external = parse(field)?;
let internal = if let Some(field) = fields.next() {
let internal = parse(field)?;
table.input[usize::from(external) / 32][usize::from(external) % 32] = internal;
table.output[usize::from(internal) / 32][usize::from(internal) % 32] = external;
internal
} else {
external
};
let printable = match fields.next() {
Some(field) => match parse(field)? {
0 => false,
1 => true,
_ => return Err(format!("invalid TCX printable flag on line {}", line + 1)),
},
None => true,
};
let mask = 1_u64 << (internal % 64);
let entry = &mut table.printable[usize::from(internal) / 64];
if printable || (32..=126).contains(&internal) {
*entry |= mask;
} else {
*entry &= !mask;
}
}
Ok(table)
}
pub(super) fn input(&self, value: u8) -> u8 {
self.input[usize::from(value) / 32][usize::from(value) % 32]
}
pub(super) fn output(&self, value: u8) -> u8 {
self.output[usize::from(value) / 32][usize::from(value) % 32]
}
pub(super) fn printable(&self, value: u8) -> bool {
self.printable[usize::from(value) / 64] & (1_u64 << (value % 64)) != 0
}
}
fn number(field: &str) -> Option<u8> {
let field = field.strip_prefix('+').unwrap_or(field);
if let Some(hex) = field
.strip_prefix("0x")
.or_else(|| field.strip_prefix("0X"))
{
u8::from_str_radix(hex, 16).ok()
} else if field.len() > 1 && field.starts_with('0') {
u8::from_str_radix(field, 8).ok()
} else {
field.parse().ok()
}
}
pub(super) fn diagnostic_text(bytes: &[u8]) -> String {
let mut text = String::new();
let mut remaining = bytes;
loop {
match std::str::from_utf8(remaining) {
Ok(valid) => {
text.push_str(valid);
return text;
}
Err(error) => {
let start = error.valid_up_to();
let end = start + error.error_len().unwrap_or(remaining.len() - start);
text.push_str(std::str::from_utf8(&remaining[..start]).unwrap());
for &byte in &remaining[start..end] {
text.push_str(&super::context::encoded_character(char::from(byte)));
}
remaining = &remaining[end..];
}
}
}
}