mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-10-24 10:08:00 -06:00
Fix util.printd panic on an escaped multi-byte character (#2386)
Found while testing crates/js/src/lib.rs:785 Co-authored-by: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 5.5
parent
ad6728bf6c
commit
c97ca226e6
@@ -783,8 +783,10 @@ fn printd(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue>
|
||||
out.push_str(if run >= 2 { ampm } else { &m[..1] });
|
||||
}
|
||||
b'\\' if i + 1 < b.len() => {
|
||||
out.push(b[i + 1] as char);
|
||||
i += 2;
|
||||
// The escaped character may be multi-byte; copy it whole.
|
||||
let ch = fmt[i + 1..].chars().next().unwrap_or(' ');
|
||||
out.push(ch);
|
||||
i += 1 + ch.len_utf8();
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
|
||||
@@ -94,6 +94,17 @@ fn util_printf_printd_and_printx() {
|
||||
assert_eq!(o.value, "1,234,567.89|00042|hi|FF|1.234,50|Jan 5, 2024 09:07|Friday|(555) 123-4567|ABC");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn printd_escapes_multibyte_chars() {
|
||||
let o = go(
|
||||
"event.value = [util.printd('yyyy \\\\é', new Date(2024, 0, 5)), util.printd('\\\\€ yyyy', new Date(2024, 0, 5)), \
|
||||
util.printd('\\\\y yyyy', new Date(2024, 0, 5))].join('|');",
|
||||
&Event::field("Calculate", "total", ""),
|
||||
);
|
||||
assert_eq!(o.error, None);
|
||||
assert_eq!(o.value, "2024 é|€ 2024|y 2024");
|
||||
}
|
||||
|
||||
fn assert_printf_error(script: &str, message: &str) {
|
||||
let o = go(script, &Event::doc("Open"));
|
||||
assert!(o.error.as_deref().is_some_and(|e| e.starts_with(message)), "{script}: {:?}", o.error);
|
||||
|
||||
Reference in New Issue
Block a user