pub fn fix_invalid_toml_escapes(toml_str: &str) -> String {
fix_toml_strings(toml_str, |content| {
content.contains("\\<") || content.contains("\\>")
})
}
fn fix_toml_strings(toml_str: &str, needs_fix: impl Fn(&str) -> bool) -> String {
let mut out = String::with_capacity(toml_str.len());
let bytes = toml_str.as_bytes();
let mut i = 0;
while i < bytes.len() {
let c = bytes[i];
if c == b'#' && (i == 0 || bytes[i - 1].is_ascii_whitespace()) {
let start = i;
while i < bytes.len() && bytes[i] != b'\n' {
i += 1;
}
out.push_str(&toml_str[start..i]);
continue;
}
if c <= b' ' {
out.push(c as char);
i += 1;
continue;
}
if c == b'"' && bytes.get(i + 1) == Some(&b'"') && bytes.get(i + 2) == Some(&b'"') {
let start = i;
i += 3;
while i < bytes.len() {
if bytes[i] == b'"'
&& bytes.get(i + 1) == Some(&b'"')
&& bytes.get(i + 2) == Some(&b'"')
{
i += 3;
break;
}
i += 1;
}
out.push_str(&toml_str[start..i]);
continue;
}
if c == b'\'' && bytes.get(i + 1) == Some(&b'\'') && bytes.get(i + 2) == Some(&b'\'') {
let start = i;
i += 3;
while i < bytes.len() {
if bytes[i] == b'\''
&& bytes.get(i + 1) == Some(&b'\'')
&& bytes.get(i + 2) == Some(&b'\'')
{
i += 3;
break;
}
i += 1;
}
out.push_str(&toml_str[start..i]);
continue;
}
if c == b'\'' {
let start = i;
i += 1;
while i < bytes.len() && bytes[i] != b'\'' {
if bytes[i] == b'\n' {
break;
}
i += 1;
}
if i < bytes.len() && bytes[i] == b'\'' {
i += 1;
}
out.push_str(&toml_str[start..i]);
continue;
}
if c == b'"' {
let start = i;
i += 1;
let body_start = i;
let mut closed = false;
while i < bytes.len() {
if bytes[i] == b'"' {
closed = true;
break;
}
if bytes[i] == b'\n' {
break;
}
if bytes[i] == b'\\' && i + 1 < bytes.len() {
i += 2;
} else {
i += 1;
}
}
if !closed {
out.push_str(&toml_str[start..i]);
if i < bytes.len() {
out.push('\n');
i += 1;
}
continue;
}
let body_end = i;
i += 1;
let body = &toml_str[body_start..body_end];
if needs_fix(body) {
let value = decode_basic_string(body);
if can_be_literal_string(&value) {
out.push('\'');
out.push_str(&value);
out.push('\'');
} else {
out.push('"');
out.push_str(&encode_basic_string(&value));
out.push('"');
}
} else {
out.push_str(&toml_str[start..i]);
}
continue;
}
match toml_str[i..].chars().next() {
Some(ch) => {
out.push(ch);
i += ch.len_utf8();
}
None => {
out.push(c as char);
i += 1;
}
}
}
out
}
fn decode_basic_string(body: &str) -> String {
let mut out = String::with_capacity(body.len());
let mut chars = body.chars().peekable();
while let Some(ch) = chars.next() {
if ch != '\\' {
out.push(ch);
continue;
}
match chars.peek().copied() {
Some('\\') => {
chars.next();
out.push('\\');
}
Some('"') => {
chars.next();
out.push('"');
}
Some('b') => {
chars.next();
out.push('\u{8}');
}
Some('e') => {
chars.next();
out.push('\u{1b}');
}
Some('f') => {
chars.next();
out.push('\u{c}');
}
Some('n') => {
chars.next();
out.push('\n');
}
Some('r') => {
chars.next();
out.push('\r');
}
Some('t') => {
chars.next();
out.push('\t');
}
Some(c @ ('u' | 'U')) => {
let width = if c == 'u' { 4 } else { 8 };
chars.next();
let mut hex = String::with_capacity(width);
let mut probe = chars.clone();
for _ in 0..width {
match probe.peek().copied() {
Some(h) if h.is_ascii_hexdigit() => {
hex.push(h);
probe.next();
}
_ => break,
}
}
match u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32) {
Some(cp) if hex.len() == width => {
for _ in 0..width {
chars.next();
}
out.push(cp);
}
_ => {
out.push('\\');
out.push(c);
}
}
}
Some(_) | None => out.push('\\'),
}
}
out
}
fn encode_basic_string(value: &str) -> String {
use std::fmt::Write as _;
let mut out = String::with_capacity(value.len());
for ch in value.chars() {
match ch {
'\\' => out.push_str("\\\\"),
'"' => out.push_str("\\\""),
'\u{8}' => out.push_str("\\b"),
'\u{1b}' => out.push_str("\\e"),
'\u{c}' => out.push_str("\\f"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
c if (c as u32) <= 0x1f || c as u32 == 0x7f => {
let _ = write!(out, "\\u{:04X}", c as u32);
}
c => out.push(c),
}
}
out
}
fn can_be_literal_string(value: &str) -> bool {
!value.contains('\'')
&& !value.chars().any(|c| {
let n = c as u32;
(n < 0x20 && n != 0x09) || n == 0x7f
})
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(serde::Deserialize)]
struct Snippet {
command: String,
}
#[test]
fn test_invalid_escape_is_repaired_without_changing_the_value() {
let input = r#"command = "echo \<hello\>""#;
let output = fix_invalid_toml_escapes(input);
let parsed: Snippet = toml::from_str(&output).unwrap();
assert_eq!(parsed.command, r"echo \<hello\>");
}
#[test]
fn test_valid_escapes_survive_the_repair() {
let input = "command = \"echo \\\\<x\\>\\nnext\"";
let output = fix_invalid_toml_escapes(input);
let parsed: Snippet = toml::from_str(&output).unwrap();
assert_eq!(parsed.command, "echo \\<x\\>\nnext");
}
#[test]
fn test_no_fix_needed_leaves_the_document_untouched() {
let input = r#"command = "echo normal""#;
assert_eq!(fix_invalid_toml_escapes(input), input);
}
#[test]
fn test_single_quoted_strings_are_untouched() {
let input = r#"command = 'echo \<hello\>'"#;
assert_eq!(fix_invalid_toml_escapes(input), input);
}
#[test]
fn test_multiline_basic_strings_are_untouched() {
let input = "command = \"\"\"echo \\\\<x\\>\"\"\"";
assert_eq!(fix_invalid_toml_escapes(input), input);
}
}