use std::fmt::Write;
pub(crate) fn push_str(out: &mut String, text: &str) {
let len_before = out.len();
out.push('"');
for ch in text.chars() {
match ch {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\u{2028}' => out.push_str("\\u2028"),
'\u{2029}' => out.push_str("\\u2029"),
c if u32::from(c) < 0x20 || c == '\u{7f}' => {
let _ = write!(out, "\\u{:04x}", u32::from(c));
}
c => out.push(c),
}
}
out.push('"');
assert!(out.len() >= len_before + text.len() + 2);
}
pub(crate) fn push_f64(out: &mut String, value: f64) {
if value.is_finite() {
let _ = write!(out, "{value}");
} else {
out.push_str("null");
}
}
pub(crate) fn push_u64(out: &mut String, value: u64) {
let _ = write!(out, "{value}");
}
pub(crate) fn push_i64(out: &mut String, value: i64) {
let _ = write!(out, "{value}");
}
pub(crate) fn push_key(out: &mut String, key: &str, first: bool) {
if !first {
out.push(',');
}
push_str(out, key);
out.push(':');
}
#[cfg(test)]
mod tests {
use super::*;
fn quoted(text: &str) -> String {
let mut out = String::new();
push_str(&mut out, text);
out
}
#[test]
fn escapes_every_special_character() {
assert_eq!(quoted("a\"b\\c"), r#""a\"b\\c""#);
assert_eq!(
quoted("\n\t\u{0}\u{1f}\u{7f}"),
r#""\u000a\u0009\u0000\u001f\u007f""#
);
assert_eq!(quoted("\u{2028}\u{2029}é"), "\"\\u2028\\u2029é\"");
}
#[test]
fn non_finite_floats_are_null() {
let mut out = String::new();
push_f64(&mut out, f64::NAN);
push_f64(&mut out, 1.5);
assert_eq!(out, "null1.5");
}
}