Track integer count registers
This commit is contained in:
1 parent
206591c725
commit
a0f4a35c94
4 files changed
+159
-4
No files matched your search
+155
@@ -185,6 +185,8 @@ struct PageGeometry {
|
||||
counters: Vec<(String, u32)>,
|
||||
/// Named lengths declared with `\newlength` and their current values.
|
||||
lengths: Vec<(String, i64)>,
|
||||
/// Integer registers declared with `\newcount` and their current values.
|
||||
count_registers: Vec<(String, i64)>,
|
||||
/// `\pagenumbering` style used for the printed page numbers.
|
||||
page_numbering: Option<String>,
|
||||
/// Zero-based page counter offset set with `\setcounter{page}`.
|
||||
@@ -1302,6 +1304,19 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(rest) = trimmed.strip_prefix("\\newcount") {
|
||||
if let Some((command, _)) = control_sequence(rest.trim_start()) {
|
||||
let name = command.trim_start_matches('\\').to_string();
|
||||
if !geometry
|
||||
.count_registers
|
||||
.iter()
|
||||
.any(|(known, _)| known == &name)
|
||||
{
|
||||
geometry.count_registers.push((name, 0));
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(name) = newif_definition(trimmed) {
|
||||
geometry
|
||||
.conditionals
|
||||
@@ -1955,6 +1970,121 @@ fn table_reference(text: &str) -> Option<usize> {
|
||||
|
||||
/// Strip geometry assignments from the whole source (preamble included) and
|
||||
/// return the LaTeX body with them removed.
|
||||
/// Parse an optionally signed decimal integer, returning its value and the
|
||||
/// number of bytes consumed (including leading spaces).
|
||||
fn scan_signed_integer(text: &str) -> Option<(i64, usize)> {
|
||||
let trimmed = text.trim_start();
|
||||
let skipped = text.len() - trimmed.len();
|
||||
let mut characters = trimmed.char_indices().peekable();
|
||||
let mut negative = false;
|
||||
if let Some((_, sign)) = characters.peek().copied() {
|
||||
if sign == '-' || sign == '+' {
|
||||
negative = sign == '-';
|
||||
characters.next();
|
||||
}
|
||||
}
|
||||
let mut digits = String::new();
|
||||
for (offset, character) in characters {
|
||||
if let Some(digit) = character.to_digit(10) {
|
||||
digits.push(char::from_digit(digit, 10).unwrap_or('0'));
|
||||
} else {
|
||||
return if digits.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let value: i64 = digits.parse().ok()?;
|
||||
Some((if negative { -value } else { value }, skipped + offset))
|
||||
};
|
||||
}
|
||||
}
|
||||
if digits.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let value: i64 = digits.parse().ok()?;
|
||||
Some((
|
||||
if negative { -value } else { value },
|
||||
skipped + trimmed.len(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Resolve `\newcount` registers in a body: assignments, `\advance` /
|
||||
/// `\multiply` / `\divide`, and `\the` / `\number` printing.
|
||||
fn expand_count_registers(body: &str, registers: &mut Vec<(String, i64)>) -> String {
|
||||
let mut result = String::new();
|
||||
let mut index = 0;
|
||||
while index < body.len() {
|
||||
let rest = &body[index..];
|
||||
let Some((command, length)) = control_sequence(rest) else {
|
||||
let character = rest.chars().next().expect("body remainder");
|
||||
result.push(character);
|
||||
index += character.len_utf8();
|
||||
continue;
|
||||
};
|
||||
let name = command.trim_start_matches('\\');
|
||||
if matches!(name, "advance" | "multiply" | "divide") {
|
||||
let after = rest[length..].trim_start();
|
||||
if let Some((target, target_length)) = control_sequence(after) {
|
||||
let target_name = target.trim_start_matches('\\');
|
||||
if let Some(position) = registers.iter().position(|(known, _)| known == target_name)
|
||||
{
|
||||
let rest_value = after[target_length..].trim_start();
|
||||
let rest_value = rest_value
|
||||
.strip_prefix("by")
|
||||
.map(str::trim_start)
|
||||
.unwrap_or(rest_value);
|
||||
if let Some((value, value_length)) = scan_signed_integer(rest_value) {
|
||||
let slot = &mut registers[position].1;
|
||||
match name {
|
||||
"advance" => *slot = slot.saturating_add(value),
|
||||
"multiply" => *slot = slot.saturating_mul(value),
|
||||
_ => {
|
||||
if value != 0 {
|
||||
*slot /= value;
|
||||
}
|
||||
}
|
||||
}
|
||||
index = body.len() - rest_value[value_length..].len();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
result.push_str(&command);
|
||||
index += length;
|
||||
continue;
|
||||
}
|
||||
if let Some(position) = registers.iter().position(|(known, _)| known == name) {
|
||||
let after = &rest[length..];
|
||||
let trimmed = after.trim_start();
|
||||
if let Some(value) = trimmed.strip_prefix('=') {
|
||||
if let Some((assigned, assigned_length)) = scan_signed_integer(value) {
|
||||
registers[position].1 = assigned;
|
||||
index = body.len() - value[assigned_length..].len();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
result.push_str(&command);
|
||||
index += length;
|
||||
continue;
|
||||
}
|
||||
if name == "the" || name == "number" {
|
||||
let after = rest[length..].trim_start();
|
||||
if let Some((target, target_length)) = control_sequence(after) {
|
||||
let target_name = target.trim_start_matches('\\');
|
||||
if let Some((_, value)) = registers.iter().find(|(known, _)| known == target_name) {
|
||||
result.push_str(&value.to_string());
|
||||
index = body.len() - after[target_length..].len();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
result.push_str(&command);
|
||||
index += length;
|
||||
continue;
|
||||
}
|
||||
result.push_str(&command);
|
||||
index += length;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn geometry_and_body(source: &str) -> (String, PageGeometry) {
|
||||
let (stripped, mut geometry) = strip_page_geometry(source);
|
||||
// Removed assignments can leave blank lines at the edges; they are not
|
||||
@@ -1965,6 +2095,9 @@ fn geometry_and_body(source: &str) -> (String, PageGeometry) {
|
||||
let macros = geometry.macros.clone();
|
||||
body = expand_conditionals(&body, &mut geometry.conditionals, ¯os);
|
||||
body = expand_macros(&body, ¯os);
|
||||
let mut count_registers = geometry.count_registers.clone();
|
||||
body = expand_count_registers(&body, &mut count_registers);
|
||||
geometry.count_registers = count_registers;
|
||||
body = expand_links(&body);
|
||||
body = expand_conditionals(&body, &mut geometry.conditionals, ¯os);
|
||||
let today = today_string();
|
||||
@@ -8142,6 +8275,28 @@ a & b
|
||||
assert_eq!(body, "yes and no");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_registers_track_integer_arithmetic() {
|
||||
let source = "\\newcount\\steps\n\\steps=5\n\\advance\\steps by 2\n\\multiply\\steps by 3\n\\divide\\steps by 2\n\nSteps \\the\\steps.";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
assert_eq!(body.trim(), "Steps 10.");
|
||||
assert!(!body.contains("newcount"));
|
||||
assert_eq!(
|
||||
geometry.count_registers,
|
||||
vec![("steps".to_string(), 10)],
|
||||
"register value"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_registers_accept_negative_values() {
|
||||
let source =
|
||||
"\\newcount\\depth\n\\depth=-4\n\\advance\\depth by 1\n\nDepth \\number\\depth.";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
assert_eq!(body.trim(), "Depth -3.");
|
||||
assert_eq!(geometry.count_registers, vec![("depth".to_string(), -3)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn page_numbers_follow_the_page_counter() {
|
||||
let offset = preview_json("\\setcounter{page}{5}\n\nAlpha\n\n\\newpage\n\nBeta")
|
||||
|
||||
Reference in new issue
Block a user