use std::fmt::Write;
pub(super) fn quoted(output: &mut String, value: &str) {
output.push('"');
for character in value.chars() {
match character {
'"' => output.push_str("\\\""),
'\\' => output.push_str("\\\\"),
'\u{08}' => output.push_str("\\b"),
'\u{0c}' => output.push_str("\\f"),
'\n' => output.push_str("\\n"),
'\r' => output.push_str("\\r"),
'\t' => output.push_str("\\t"),
value if value < '\u{20}' => {
let _ = write!(output, "\\u{:04x}", u32::from(value));
}
value => output.push(value),
}
}
output.push('"');
}
pub(super) fn uri(path: &str) -> String {
let normalized = path.replace('\\', "/");
let prefix = if normalized.starts_with('/') {
"file://"
} else if normalized.as_bytes().get(1) == Some(&b':') {
"file:///"
} else {
""
};
let mut output = String::with_capacity(prefix.len() + normalized.len());
output.push_str(prefix);
for byte in normalized.bytes() {
if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b'~' | b'/' | b':') {
output.push(char::from(byte));
} else {
let _ = write!(output, "%{byte:02X}");
}
}
output
}