use std::fmt::Write as _;
pub(super) fn string_literal(value: &str) -> String {
let mut out = String::with_capacity(value.len() + 2);
out.push('"');
for character in value.chars() {
match character {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
control if control.is_control() => {
let _ = write!(out, "\\u{{{:x}}}", control as u32);
}
other => out.push(other),
}
}
out.push('"');
out
}
pub(super) fn doc_lines(out: &mut String, indent: usize, text: &str) {
let pad = " ".repeat(indent);
for line in text.lines() {
if line.is_empty() {
let _ = writeln!(out, "{pad}///");
} else {
let _ = writeln!(out, "{pad}/// {line}");
}
}
}
pub(super) fn quoted_list(names: impl IntoIterator<Item = impl AsRef<str>>) -> String {
names
.into_iter()
.map(|name| format!("`{}`", name.as_ref()))
.collect::<Vec<_>>()
.join(", ")
}