diff --git a/crates/ui-egui/src/i18n/tests.rs b/crates/ui-egui/src/i18n/tests.rs index 2ca2c76e03..25e378a43a 100644 --- a/crates/ui-egui/src/i18n/tests.rs +++ b/crates/ui-egui/src/i18n/tests.rs @@ -4653,6 +4653,55 @@ fn message_scan_joins_a_literal_continued_on_the_next_line() { } } +/// The scan reads each of Rust's character escapes in a message as the character Rust gives it +/// (https://doc.rust-lang.org/reference/tokens.html#character-escapes), and stops at the literal's +/// own closing quote, not at an escaped one. +#[test] +fn message_scan_decodes_rust_character_escapes() { + let cases = [ + (r#"a\rb" tail"#, "a\rb"), + (r#"a\0b""#, "a\0b"), + (r#"\x41\x7f\x7F""#, "A\x7f\x7f"), + (r#"caf\u{e9}""#, "café"), + (r#"caf\u{E_9}""#, "café"), + (r#"\u{1F600}\u{10_FFFF}\u{0}""#, "\u{1F600}\u{10FFFF}\0"), + (r#"a\\b""#, "a\\b"), + (r#"say \"hi\"" tail"#, "say \"hi\""), + (r#"it\'s""#, "it's"), + (r#"\n\t""#, "\n\t"), + ]; + let wrong: Vec<_> = cases.iter().filter(|(body, text)| string_literal(body).as_deref() != Some(*text)).collect(); + assert!(wrong.is_empty(), "{wrong:?}"); + let found = source_messages(r#"fn run() { let _ = Err("bad \x41 value \u{e9}"); }"#, &[], &Default::default()); + assert_eq!(found, ["bad A value é"]); +} + +/// A literal with an escape Rust does not accept is not read as a message: an unknown letter, `\x` +/// with fewer than two hex digits or above `7F`, and `\u` without braces, without digits, with a +/// leading underscore, with more than six digits, or naming a surrogate or a value above `10FFFF`. +#[test] +fn message_scan_rejects_invalid_rust_escapes() { + let bodies = [ + r#"bad \q""#, + r#"bad \x4""#, + r#"bad \x4g""#, + r#"bad \x80""#, + r#"bad \x+1""#, + concat!(r"bad \u", "00e9\""), + r#"bad \u{""#, + r#"bad \u{}""#, + r#"bad \u{_e9}""#, + r#"bad \u{e9""#, + r#"bad \u{D800}""#, + r#"bad \u{110000}""#, + r#"bad \u{1234567}""#, + ]; + let read: Vec<_> = bodies.iter().filter(|body| string_literal(body).is_some()).collect(); + assert!(read.is_empty(), "{read:?}"); + let code = r#"fn run() { let _ = Err("bad \q value"); let _ = Err("bad \u{D800} value"); let _ = Err("good value"); }"#; + assert_eq!(source_messages(code, &[], &Default::default()), ["good value"]); +} + /// The scan reads the message of each of the plug-in host's [`ERROR_HELPERS`]: a literal, a /// `format!` string with or without a `&` before it, after the location of `out_err` (a /// `format!` string too), the number and range of `in_range`, the number and location of `unit`, @@ -7362,8 +7411,12 @@ fn without_test_module(text: &str) -> &str { text } -/// The text of a string literal whose opening quote was just read, escapes resolved. A `\` at the -/// end of a line drops the line break and the whitespace that starts the next line, as Rust does. +/// The text of a string literal whose opening quote was just read, escapes resolved as Rust resolves +/// them (https://doc.rust-lang.org/reference/tokens.html#character-escapes): `\n`, `\r`, `\t`, `\0`, +/// `\\`, `\'`, `\"`, `\x` with two hex digits up to `7F`, and `\u{…}` with one to six hex digits +/// (underscores allowed after the first) naming a Unicode scalar value. A `\` at the end of a line +/// drops the line break and the whitespace that starts the next line. A literal with any other +/// escape, or one left unclosed, is `None`. fn string_literal(s: &str) -> Option { let mut out = String::new(); let mut it = s.chars(); @@ -7372,9 +7425,36 @@ fn string_literal(s: &str) -> Option { '"' => return Some(out), '\\' => match it.next()? { 'n' => out.push('\n'), + 'r' => out.push('\r'), 't' => out.push('\t'), + '0' => out.push('\0'), + c @ ('\\' | '\'' | '"') => out.push(c), + 'x' => { + let (high, low) = (it.next()?.to_digit(16)?, it.next()?.to_digit(16)?); + out.push(char::from_u32(high * 16 + low).filter(char::is_ascii)?); + } + 'u' => { + if it.next()? != '{' { + return None; + } + let (mut value, mut digits) = (0u32, 0); + loop { + match it.next()? { + '}' if digits > 0 => break, + '_' if digits > 0 => {} + c => { + value = value * 16 + c.to_digit(16)?; + digits += 1; + if digits > 6 { + return None; + } + } + } + } + out.push(char::from_u32(value)?); + } '\n' | '\r' => it = it.as_str().trim_start_matches([' ', '\t', '\n', '\r']).chars(), - c => out.push(c), + _ => return None, }, c => out.push(c), } diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 57ac87a396..77b2531f39 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1151,6 +1151,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - Polish labels and messages call drawn art and artwork *kompozycja*, the term the catalog's header sets, instead of *grafika* or *rysunek*. *Grafika* stays for what the English calls a graphic (graphic styles, library and 3D graphics, GPU graphics), and line art, the art brush and an artboard (*obszar kompozycji*) keep their own terms (`polish_rows_call_artwork_kompozycja`). - The status bar translates the limits a plug-in run can exceed (its instruction, time and memory budgets and its output size) and the error of a plug-in removal that leaves the plug-in's grant in the plug-in store, in every language that translates every message. The untranslated-strings report also lists messages that an error or a status takes from a `&str` constant (`plugin_limit_errors_are_translated`, `message_scan_reads_messages_held_in_constants`). - The untranslated-strings report reads a message written over two lines with a `\` at the end of the first as one line, the text the program shows, and the Georgian catalog's (`ka`) row for PDF export's warning about knockout groups with too many translucent elements matches that text (`message_scan_joins_a_literal_continued_on_the_next_line`, `untranslated_report`). + - The untranslated-strings report reads the escapes in a message (`\r`, `\0`, `\x41`, `\u{e9}` and the others the Rust Reference lists at https://doc.rust-lang.org/reference/tokens.html#character-escapes) as the characters the program shows, and leaves out a literal with an escape Rust does not accept, such as `\q` or `\u{D800}` (`message_scan_decodes_rust_character_escapes`, `message_scan_rejects_invalid_rust_escapes`). - The status bar translates the plug-in host's other errors (manifest, module and run checks, the output an importer, exporter or effect returns, and the approval a plug-in's permissions need) in every language that translates every message. A JSON object or array inside a message stays as written, and a message that wraps another one, such as `plug-in manifest: {0}`, is matched by its own row before a row that starts with a placeholder. The reasons inside these errors (what is wrong with a parameter's spec or dialog keys, the value a parameter needs, what is wrong with an object, stroke, paint or effect in a plug-in's output) are translated with them, and the untranslated-strings report lists the reasons the plug-in host's error helpers take (`plugin_messages_are_translated_in_every_complete_catalog`, `plugin_errors_translate_with_their_values`, `plugin_error_reasons_are_translated_with_the_error`, `message_scan_reads_plugin_helpers`, `templates_that_start_with_text_match_first`). - The status bar translates `script.run`'s refusals of `answers` in every language that translates every message: a value that is not a list, more than 10,000 answers, more than 1 MiB, an answer at an index that is not an object, does not have exactly `kind` and `value`, has a `value` its kind of dialog does not take, holds a prompt's text longer than 16 KiB or has an unknown `kind`, and `answers` given to a run in the app. The index, the counts and the code names are shown as they are, and the command's error stays in English. The untranslated-strings report also lists the refusals of `script.run`'s parameters (`script_answers_errors_are_translated`, `message_scan_reads_script_run_param_refusals`). - The status bar also translates `script.run`'s other refusals of its parameters in every language that translates every message: a `maxFuel` outside 1 to 100,000,000,000, `args` nested more than 64 levels deep (with the place in `args`), `args` that is not an object or is larger than 1 MiB, an empty `path`, a `source` given with `installed`, a `path` that does not name a .js or .jsx file, and neither or both of `path` and `source`. The code names, the place in `args`, the file suffixes and the limits are shown as they are, and the command's error stays in English (`script_run_param_errors_are_translated`).