use crate::Error;
use crate::parse::{Kind, Node};
use crate::scalar::is_bare_key;
use core::fmt::NumBuffer;
use std::fmt::{self, Write};
pub(crate) fn document(value: &Node<'_>) -> Result<String, Error> {
let mut output = String::with_capacity(1024);
match &value.kind {
Kind::Object(object) if !object.members.is_empty() => {
for (key, member) in &object.members {
write_member(&mut output, key, &member.value, 0);
output.push('\n');
}
}
Kind::Object(_) | Kind::Array(_) => {
write_value(&mut output, value, 0);
output.push('\n');
}
kind => {
return Err(Error::write(format!(
"A document must be an object or an array, not {}",
kind.describe()
)));
}
}
Ok(output)
}
fn write_member(output: &mut String, key: &str, value: &Node<'_>, indentation: usize) {
write_key(output, key).expect("writing to a String does not fail");
output.push_str(": ");
write_value(output, value, indentation);
}
pub(crate) fn write_value(output: &mut String, value: &Node<'_>, indentation: usize) {
match &value.kind {
Kind::Null => output.push_str("null"),
Kind::Bool(value) => output.push_str(if *value { "true" } else { "false" }),
Kind::Int(value) => write_int(output, *value),
Kind::Float(value) => write_float(output, *value),
Kind::String(value) => {
write_string(output, value).expect("writing to a String does not fail");
}
Kind::Instant(value) => write_display(output, value),
Kind::Duration(value) => write_display(output, value),
Kind::Array(items) => {
if items.is_empty() {
output.push_str("[]");
return;
}
output.push('[');
for item in items {
push_line(output, indentation + 1);
write_value(output, item, indentation + 1);
}
push_line(output, indentation);
output.push(']');
}
Kind::Object(object) => {
if object.members.is_empty() {
output.push_str("{}");
return;
}
output.push('{');
for (key, member) in &object.members {
push_line(output, indentation + 1);
write_member(output, key, &member.value, indentation + 1);
}
push_line(output, indentation);
output.push('}');
}
}
}
pub(crate) fn push_line(output: &mut String, count: usize) {
if count <= 4 {
output.push('\n');
for _ in 0..count {
output.push('\t');
}
return;
}
const LINE: &str = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
const TAB_COUNT: usize = LINE.len() - 1;
output.push_str(&LINE[..=count.min(TAB_COUNT)]);
if count > TAB_COUNT {
output.extend(std::iter::repeat_n('\t', count - TAB_COUNT));
}
}
pub(crate) fn write_int(output: &mut String, value: i64) {
output.push_str(value.format_into(&mut NumBuffer::new()));
}
pub(crate) fn write_display(output: &mut String, value: &(impl fmt::Display + ?Sized)) {
write!(output, "{value}").expect("writing to a String does not fail");
}
pub(crate) fn check_representable(text: &str, what: &str) -> Result<(), Error> {
if text.contains('\r') {
return Err(Error::write(format!(
"A {what} cannot contain a carriage return (U+000D), because SOML cannot represent one"
)));
}
Ok(())
}
pub(crate) fn write_key(output: &mut impl Write, key: &str) -> fmt::Result {
if is_bare_key(key) {
output.write_str(key)
} else {
write_string(output, key)
}
}
pub(crate) fn needs_escapes(text: &str) -> bool {
const ONES: u64 = u64::from_ne_bytes([0x01; 8]);
const HIGHS: u64 = u64::from_ne_bytes([0x80; 8]);
let has_zero = |word: u64| word.wrapping_sub(ONES) & !word & HIGHS != 0;
let needs = |byte: u8| byte == b'\'' || byte < 0x20 || byte == 0x7F;
let (words, remainder) = text.as_bytes().as_chunks::<8>();
words.iter().any(|word| {
let word = u64::from_ne_bytes(*word);
word.wrapping_sub(ONES * 0x20) & !word & HIGHS != 0
|| has_zero(word ^ (ONES * u64::from(b'\'')))
|| has_zero(word ^ (ONES * 0x7F))
}) || remainder.iter().any(|&byte| needs(byte))
}
pub(crate) fn write_string(output: &mut impl Write, text: &str) -> fmt::Result {
if !needs_escapes(text) {
output.write_char('\'')?;
output.write_str(text)?;
return output.write_char('\'');
}
write_escaped_string(output, text)
}
pub(crate) fn write_literal_string(output: &mut String, text: &str) {
output.reserve(text.len() + 2);
output.push('\'');
output.push_str(text);
output.push('\'');
}
pub(crate) fn write_escaped_string(output: &mut impl Write, text: &str) -> fmt::Result {
output.write_char('"')?;
let mut chunk_start = 0;
for (index, byte) in text.bytes().enumerate() {
let escape = match byte {
b'\\' => "\\\\",
b'"' => "\\\"",
b'\n' => "\\n",
b'\t' => "\\t",
0x00..0x20 | 0x7F => "",
_ => continue,
};
output.write_str(&text[chunk_start..index])?;
if escape.is_empty() {
const HEX: &[u8; 16] = b"0123456789abcdef";
output.write_str("\\u{")?;
if byte >= 16 {
output.write_char(char::from(HEX[usize::from(byte >> 4)]))?;
}
output.write_char(char::from(HEX[usize::from(byte & 15)]))?;
output.write_char('}')?;
} else {
output.write_str(escape)?;
}
chunk_start = index + 1;
}
output.write_str(&text[chunk_start..])?;
output.write_char('"')
}
pub(crate) fn write_float(output: &mut String, value: f64) {
write_float_text(output, value, |buffer| buffer.format_finite(value.abs()));
}
pub(crate) fn write_f32(output: &mut String, value: f32) {
write_float_text(output, f64::from(value), |buffer| {
buffer.format_finite(value.abs())
});
}
fn write_float_text(
output: &mut String,
value: f64,
format: impl FnOnce(&mut zmij::Buffer) -> &str,
) {
debug_assert!(!value.is_nan(), "the serializer rejects NaN");
if value.is_infinite() {
output.push_str(if value > 0.0 { "infinity" } else { "-infinity" });
return;
}
if value == 0.0 {
output.push_str("0.0");
return;
}
if value < 0.0 {
output.push('-');
}
let mut buffer = zmij::Buffer::new();
let text = format(&mut buffer);
if !text.bytes().rev().take(5).any(|byte| byte == b'e') {
output.push_str(text);
return;
}
write_layout(output, text);
}
fn write_layout(output: &mut String, text: &str) {
let mut digit_buffer = [0; 32];
let (digits, exponent) = shortest_digits(text, &mut digit_buffer);
let count = digits.len() as i32;
let point = exponent + count;
if count <= point && point <= 21 {
output.push_str(digits);
output.extend(std::iter::repeat_n('0', (point - count) as usize));
output.push_str(".0");
} else if 0 < point && point < count {
output.push_str(&digits[..point as usize]);
output.push('.');
output.push_str(&digits[point as usize..]);
} else if -6 < point && point <= 0 {
output.push_str("0.");
output.extend(std::iter::repeat_n('0', (-point) as usize));
output.push_str(digits);
} else {
output.push_str(&digits[..1]);
if count > 1 {
output.push('.');
output.push_str(&digits[1..]);
}
output.push('e');
write_int(output, i64::from(point - 1));
}
}
fn shortest_digits<'a>(text: &str, buffer: &'a mut [u8; 32]) -> (&'a str, i32) {
let (significand, exponent) = match text.split_once(['e', 'E']) {
Some((significand, exponent)) => (
significand,
exponent
.parse::<i32>()
.expect("zmij writes a valid exponent"),
),
None => (text, 0),
};
let (whole, fraction) = significand.split_once('.').unwrap_or((significand, ""));
let mut length = 0;
for byte in whole
.bytes()
.chain(fraction.bytes())
.skip_while(|&byte| byte == b'0')
{
buffer[length] = byte;
length += 1;
}
let significant = buffer[..length]
.iter()
.rposition(|&byte| byte != b'0')
.map_or(0, |last| last + 1);
let exponent = exponent - fraction.len() as i32 + (length - significant) as i32;
(
std::str::from_utf8(&buffer[..significant]).expect("the digits are ASCII"),
exponent,
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn needs_escapes_finds_each_byte_at_each_place_in_a_word() {
for filler in ["a", "é"] {
for length in 1..=17 {
for place in 0..length {
for byte in 0..=0x7F_u8 {
let mut text: Vec<u8> = filler.repeat(length).into_bytes();
let place = place * filler.len();
text[place] = byte;
if filler.len() == 2 {
text[place ^ 1] = b'a';
}
let text = String::from_utf8(text).expect("valid UTF-8");
let expected = byte == b'\'' || byte < 0x20 || byte == 0x7F;
assert_eq!(needs_escapes(&text), expected, "{text:?}");
}
}
}
}
}
#[test]
fn the_text_of_zmij_without_an_exponent_is_in_canonical_layout() {
let powers = (-30..=30).flat_map(|exponent| {
let value = 10_f64.powi(exponent);
[value.next_down(), value, value.next_up()]
});
let mut state: u64 = 0x9E37_79B9_7F4A_7C15;
let others = std::iter::repeat_with(move || {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
let power = 10_f64.powi((state % 30) as i32 - 10);
if state & (1 << 40) == 0 {
((state >> 20) % 1_000_000) as f64 * power
} else {
(state >> 11) as f64 / (1_u64 << 53) as f64 * power
}
});
let mut compared = 0;
for value in powers
.chain(others.take(1_000_000))
.filter(|value| *value > 0.0)
{
let mut buffer = zmij::Buffer::new();
let text = buffer.format_finite(value);
if text.contains('e') {
continue;
}
let mut expected = String::new();
write_layout(&mut expected, text);
assert_eq!(text, expected, "{value:e}");
compared += 1;
}
assert!(compared > 500_000, "{compared}");
}
}