Discard gobbled arguments and test argument emptiness

This commit is contained in:
gloamxun committed 2026-10-26 02:53:30 +08:00
1 parent cb00e21127
commit d011806343
3 files changed
+61 -3

No files matched your search

+58
View File
@@ -697,6 +697,45 @@ fn latex_internal_at(rest: &str, macros: &[MacroDefinition]) -> Option<(String,
return Some((String::new(), "\\@empty".len()));
}
}
for (command, count) in [("\\@gobble", 1_usize), ("\\@gobbletwo", 2_usize)] {
if let Some(after) = rest.strip_prefix(command) {
if boundary(after) {
let mut offset = command.len();
let mut cursor = after;
for _ in 0..count {
let skipped = cursor.len() - cursor.trim_start().len();
offset += skipped;
cursor = cursor.trim_start();
let (_, used) = braced_group(cursor)?;
offset += used;
cursor = &cursor[used..];
}
return Some((String::new(), offset));
}
}
}
if let Some(after) = rest.strip_prefix("\\@ifmtarg") {
if boundary(after) {
let mut offset = "\\@ifmtarg".len();
let mut cursor = after;
let mut groups = Vec::new();
for _ in 0..3 {
let skipped = cursor.len() - cursor.trim_start().len();
offset += skipped;
cursor = cursor.trim_start();
let (group, used) = braced_group(cursor)?;
offset += used;
cursor = &cursor[used..];
groups.push(group);
}
let chosen = if groups[0].trim().is_empty() {
groups[1]
} else {
groups[2]
};
return Some((chosen.to_string(), offset));
}
}
if let Some(after) = rest.strip_prefix("\\@nameuse") {
if boundary(after) {
let skipped = after.len() - after.trim_start().len();
@@ -9724,6 +9763,25 @@ a & b
assert!(body.contains("[[x] and no y."), "{body}");
}
#[test]
fn gobble_commands_drop_arguments() {
let (body, _) = geometry_and_body(
"\\def\\gob{\\@gobble}\n\\def\\gobtwo{\\@gobbletwo}\n\nA \\gob{X} \\gobtwo{Y}{Z} B",
);
assert!(!body.contains('X'), "{body}");
assert!(!body.contains('Y'), "{body}");
assert!(!body.contains('Z'), "{body}");
assert!(body.contains('A') && body.contains('B'), "{body}");
}
#[test]
fn ifmtarg_checks_for_an_empty_argument() {
let (body, _) = geometry_and_body(
"\\def\\ifempty{\\@ifmtarg}\n\n\\ifempty{}{empty}{not} \\ifempty{x}{empty}{not}",
);
assert!(body.starts_with("empty not"), "{body}");
}
#[test]
fn definition_prefixes_are_transparent() {
let (body, geometry) = geometry_and_body(