#![cfg(feature = "std")]
use crate::ir;
impl std::fmt::Display for ir::Literal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ir::Literal::bool(b) => f.write_str(if *b { "true" } else { "false" }),
ir::Literal::int8(v) => write!(f, "{v}"),
ir::Literal::int16(v) => write!(f, "{v}"),
ir::Literal::int32(v) => write!(f, "{v}"),
ir::Literal::int64(v) => write!(f, "{v}"),
ir::Literal::uint8(v) => write!(f, "{v}"),
ir::Literal::uint16(v) => write!(f, "{v}"),
ir::Literal::uint32(v) => write!(f, "{v}"),
ir::Literal::uint64(v) => write!(f, "{v}"),
ir::Literal::float32(v) => write!(f, "{v}"),
ir::Literal::float64(v) => write!(f, "{v}"),
ir::Literal::text(s) => {
write!(f, "{}", quote_string(escape_all_except_quotes(s).as_str()))
}
}
}
}
fn quote_string(s: &str) -> String {
if !s.contains('"') {
return format!(r#""{s}""#);
}
if !s.contains('\'') {
return format!("'{s}'");
}
let quote = if s.starts_with('"') || s.ends_with('"') {
'\''
} else {
'"'
};
let max_consecutive = s
.split(|c| c != quote)
.map(|quote_sequence| quote_sequence.len())
.max()
.unwrap_or(0);
let next_odd = max_consecutive.div_ceil(2) * 2 + 1;
let delim = quote.to_string().repeat(next_odd);
format!("{delim}{s}{delim}")
}
fn escape_all_except_quotes(s: &str) -> String {
let mut result = String::new();
for ch in s.chars() {
if ch == '"' || ch == '\'' {
result.push(ch);
} else {
result.extend(ch.escape_default());
}
}
result
}