pub const NAMES: [&str; 55] = [
"pretolerance",
"tolerance",
"linepenalty",
"hyphenpenalty",
"exhyphenpenalty",
"clubpenalty",
"widowpenalty",
"displaywidowpenalty",
"brokenpenalty",
"binoppenalty",
"relpenalty",
"predisplaypenalty",
"postdisplaypenalty",
"interlinepenalty",
"doublehyphendemerits",
"finalhyphendemerits",
"adjdemerits",
"mag",
"delimiterfactor",
"looseness",
"time",
"day",
"month",
"year",
"showboxbreadth",
"showboxdepth",
"hbadness",
"vbadness",
"pausing",
"tracingonline",
"tracingmacros",
"tracingstats",
"tracingparagraphs",
"tracingpages",
"tracingoutput",
"tracinglostchars",
"tracingcommands",
"tracingrestores",
"uchyph",
"outputpenalty",
"maxdeadcycles",
"hangafter",
"floatingpenalty",
"globaldefs",
"fam",
"escapechar",
"defaulthyphenchar",
"defaultskewchar",
"endlinechar",
"newlinechar",
"language",
"lefthyphenmin",
"righthyphenmin",
"holdinginserts",
"errorcontextlines",
];
pub fn index(name: &str) -> Option<usize> {
NAMES.iter().position(|n| *n == name)
}
pub const ESCAPE_CHAR: usize = 45;
pub const END_LINE_CHAR: usize = 48;
pub const NEW_LINE_CHAR: usize = 49;
#[derive(Clone, Debug, PartialEq)]
pub struct IntPars {
values: [i64; NAMES.len()],
written: u64,
}
impl IntPars {
pub fn new() -> Self {
let mut values = [0; NAMES.len()];
for (name, v) in [
("mag", 1000),
("tolerance", 10000),
("hangafter", 1),
("maxdeadcycles", 25),
("escapechar", i64::from(b'\\')),
("endlinechar", 13),
] {
values[index(name).expect("a §236 name")] = v;
}
let (time, day, month, year) = date_and_time();
for (name, v) in [
("time", time),
("day", day),
("month", month),
("year", year),
] {
values[index(name).expect("a §236 name")] = v;
}
Self {
values,
written: 0,
}
}
pub fn get(&self, i: usize) -> i64 {
self.values[i]
}
pub fn set(&mut self, i: usize, v: i64) {
self.values[i] = v;
self.written |= 1 << i;
}
pub fn assigned(&self, i: usize) -> Option<i64> {
(self.written & (1 << i) != 0).then(|| self.values[i])
}
pub fn differences(&self, other: &IntPars) -> Vec<(String, i64)> {
NAMES
.iter()
.enumerate()
.filter(|(i, _)| self.values[*i] != other.values[*i])
.map(|(i, n)| ((*n).to_string(), self.values[i]))
.collect()
}
}
impl Default for IntPars {
fn default() -> Self {
Self::new()
}
}
fn date_and_time() -> (i64, i64, i64, i64) {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as libc::time_t)
.unwrap_or(0);
let mut tm: libc::tm = unsafe { std::mem::zeroed() };
let ok = unsafe { !libc::localtime_r(&now, &mut tm).is_null() };
if !ok {
return (12 * 60, 4, 7, 1776);
}
(
i64::from(tm.tm_hour) * 60 + i64::from(tm.tm_min),
i64::from(tm.tm_mday),
i64::from(tm.tm_mon) + 1,
i64::from(tm.tm_year) + 1900,
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn escapechar_index_is_the_one_the_printer_reads() {
assert_eq!(index("escapechar"), Some(ESCAPE_CHAR));
}
#[test]
fn line_char_indices_name_their_parameters() {
assert_eq!(index("endlinechar"), Some(END_LINE_CHAR));
assert_eq!(index("newlinechar"), Some(NEW_LINE_CHAR));
}
#[test]
fn initex_values() {
let p = IntPars::new();
let at = |n| p.get(index(n).unwrap());
assert_eq!(at("mag"), 1000);
assert_eq!(at("tolerance"), 10000);
assert_eq!(at("escapechar"), 92);
assert_eq!(at("endlinechar"), 13);
assert_eq!(at("newlinechar"), 0);
assert_eq!(at("pretolerance"), 0);
assert!((1..=12).contains(&at("month")));
}
}