Choose ifcase branches and fall back to else

This commit is contained in:
gloamxun committed 2026-10-26 02:08:16 +08:00
1 parent 4352922240
commit ded738d445
4 files changed
+105 -4

No files matched your search

+101
View File
@@ -1152,6 +1152,64 @@ fn conditional_at(
Some((flag.value, consumed))
}
fn case_conditional_at(
text: &str,
index: usize,
counts: &[(String, i64)],
counters: &[(String, u32)],
) -> Option<(i64, usize)> {
let rest = text.get(index..)?;
let after = rest.strip_prefix("\\ifcase")?;
if after
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic())
{
return None;
}
let (value, used) = conditional_operand(after, counts, counters)?;
Some((value, "\\ifcase".len() + used))
}
/// Split the body of an `\ifcase` into `\or` branches and the `\else` branch.
fn case_branches(source: &str, start: usize) -> Option<(Vec<&str>, Option<&str>, usize)> {
let mut branches = Vec::new();
let mut otherwise_start = None;
let mut current_start = start;
let mut depth = 0_usize;
let mut search = start;
while let Some(offset) = source[search..].find('\\') {
let position = search + offset;
let Some((command, consumed)) = control_sequence(&source[position..]) else {
search = position + 1;
continue;
};
let word = command.trim_start_matches('\\');
if word.starts_with("if") && word.len() > 2 {
depth += 1;
} else if word == "fi" {
if depth == 0 {
if let Some(else_start) = otherwise_start {
let _ = else_start;
return Some((branches, Some(&source[current_start..position]), position));
}
branches.push(&source[current_start..position]);
return Some((branches, None, position));
}
depth -= 1;
} else if depth == 0 && word == "or" {
branches.push(&source[current_start..position]);
current_start = position + consumed;
} else if depth == 0 && word == "else" && otherwise_start.is_none() {
branches.push(&source[current_start..position]);
current_start = position + consumed;
otherwise_start = Some(position);
}
search = position + consumed;
}
None
}
fn conditional_end(text: &str, start: usize) -> Option<(Option<usize>, usize)> {
let mut index = start;
let mut depth = 0_usize;
@@ -1211,6 +1269,25 @@ fn expand_conditionals_at(
index = position + consumed;
continue;
}
if let Some((value, consumed)) = case_conditional_at(source, position, counts, counters) {
if let Some((branches, otherwise, fi_at)) = case_branches(source, position + consumed) {
let chosen = if value >= 0 {
branches.get(value as usize).copied().or(otherwise)
} else {
otherwise
};
output.push_str(&expand_conditionals_at(
chosen.unwrap_or("").trim_start(),
flags,
macros,
counts,
counters,
depth + 1,
));
index = fi_at + 3;
continue;
}
}
if let Some((value, consumed)) =
conditional_at(source, position, flags, macros, counts, counters)
{
@@ -8533,6 +8610,30 @@ a & b
assert_eq!(body.trim(), "B");
}
#[test]
fn ifcase_selects_the_numbered_branch() {
let (body, _) = geometry_and_body("\\ifcase 2 zero\\or one\\or two\\else other\\fi");
assert_eq!(body.trim(), "two");
}
#[test]
fn ifcase_falls_back_to_else() {
let (body, _) = geometry_and_body("\\ifcase 5 zero\\or one\\else other\\fi");
assert_eq!(body.trim(), "other");
}
#[test]
fn ifcase_reads_count_registers() {
let (body, _) = geometry_and_body("\\newcount\\k\n\\k=1\n\n\\ifcase\\k zero\\or one\\fi");
assert_eq!(body.trim(), "one");
}
#[test]
fn ifcase_handles_nested_conditionals() {
let (body, _) = geometry_and_body("\\ifcase 0 yes\\iftrue A\\else B\\fi\\or no\\fi");
assert_eq!(body.trim(), "yesA");
}
#[test]
fn page_numbers_follow_the_page_counter() {
let offset = preview_json("\\setcounter{page}{5}\n\nAlpha\n\n\\newpage\n\nBeta")