use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::fmt::{self, Write as _};
use std::hash::{BuildHasherDefault, DefaultHasher, Hash as _, Hasher};
use einvoice_xpath::QName;
use crate::error::{Result, err};
#[derive(Clone, Default)]
pub struct CompactOutput {
blocks: Vec<String>,
spans: Vec<Span>,
sequence: Vec<u32>,
len: usize,
}
#[derive(Debug, Clone, Copy)]
struct Span {
block: u32,
start: u32,
end: u32,
}
const BLOCK: usize = 64 << 10;
impl CompactOutput {
#[must_use]
pub const fn len(&self) -> usize {
self.len
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.len == 0
}
pub fn pieces(&self) -> impl Iterator<Item = &str> {
self.sequence.iter().map(|&id| self.piece(id))
}
#[must_use]
pub fn to_text(&self) -> String {
let mut text = String::with_capacity(self.len);
for piece in self.pieces() {
text.push_str(piece);
}
text
}
pub fn shrink_to_fit(&mut self) {
if let Some(last) = self.blocks.last_mut() {
last.shrink_to_fit();
}
self.blocks.shrink_to_fit();
self.spans.shrink_to_fit();
self.sequence.shrink_to_fit();
}
fn piece(&self, id: u32) -> &str {
let span = self.spans[id as usize];
&self.blocks[span.block as usize][span.start as usize..span.end as usize]
}
fn add(&mut self, piece: &str) -> Result<u32> {
let fits = self
.blocks
.last()
.is_some_and(|b| b.capacity() - b.len() >= piece.len());
if !fits {
let before: usize = self.blocks.iter().map(String::capacity).sum();
let size = before.clamp(1 << 10, BLOCK).max(piece.len());
self.blocks.push(String::with_capacity(size));
}
let block = self.blocks.len() - 1;
let text = &mut self.blocks[block];
let start = text.len();
text.push_str(piece);
let (Ok(id), Ok(block), Ok(start), Ok(end)) = (
u32::try_from(self.spans.len()),
u32::try_from(block),
u32::try_from(start),
u32::try_from(text.len()),
) else {
return err(
"INTERNAL",
"the result has more than 2^32 distinct pieces or a piece of 4 GiB",
);
};
self.spans.push(Span { block, start, end });
Ok(id)
}
fn text_is(&self, text: &str) -> bool {
if self.len != text.len() {
return false;
}
let mut rest = text.as_bytes();
for piece in self.pieces() {
let (head, tail) = rest.split_at(piece.len());
if head != piece.as_bytes() {
return false;
}
rest = tail;
}
true
}
}
impl PartialEq for CompactOutput {
fn eq(&self, other: &Self) -> bool {
if self.len != other.len {
return false;
}
let mut left = self.pieces().map(str::as_bytes);
let mut right = other.pieces().map(str::as_bytes);
let (mut a, mut b): (&[u8], &[u8]) = (&[], &[]);
loop {
if a.is_empty() {
match left.next() {
Some(next) => a = next,
None => return b.is_empty() && right.all(<[u8]>::is_empty),
}
continue;
}
if b.is_empty() {
match right.next() {
Some(next) => b = next,
None => return false,
}
continue;
}
let n = a.len().min(b.len());
if a[..n] != b[..n] {
return false;
}
a = &a[n..];
b = &b[n..];
}
}
}
impl Eq for CompactOutput {}
impl PartialEq<str> for CompactOutput {
fn eq(&self, other: &str) -> bool {
self.text_is(other)
}
}
impl PartialEq<&str> for CompactOutput {
fn eq(&self, other: &&str) -> bool {
self.text_is(other)
}
}
impl fmt::Display for CompactOutput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for piece in self.pieces() {
f.write_str(piece)?;
}
Ok(())
}
}
impl fmt::Debug for CompactOutput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_char('"')?;
for piece in self.pieces() {
for c in piece.chars() {
if c == '\'' {
f.write_char(c)?;
} else {
for e in c.escape_debug() {
f.write_char(e)?;
}
}
}
}
f.write_char('"')
}
}
impl From<&str> for CompactOutput {
fn from(text: &str) -> Self {
let mut builder = CompactBuilder::default();
if builder.push(text).is_err() {
return Self::default();
}
builder.finish()
}
}
#[derive(Debug, Default)]
struct HashKey(u64);
impl Hasher for HashKey {
fn finish(&self) -> u64 {
self.0
}
fn write(&mut self, bytes: &[u8]) {
for &b in bytes {
self.0 = self.0.rotate_left(8) ^ u64::from(b);
}
}
fn write_u64(&mut self, n: u64) {
self.0 = n;
}
}
#[derive(Debug, Default)]
pub(crate) struct CompactBuilder {
output: CompactOutput,
index: HashMap<u64, u32, BuildHasherDefault<HashKey>>,
}
impl CompactBuilder {
pub(crate) fn push(&mut self, piece: &str) -> Result<()> {
if piece.is_empty() {
return Ok(());
}
let mut hasher = DefaultHasher::new();
piece.hash(&mut hasher);
let hash = hasher.finish();
let id = match self.index.entry(hash) {
Entry::Occupied(entry) if self.output.piece(*entry.get()) == piece => *entry.get(),
Entry::Occupied(_) => self.output.add(piece)?,
Entry::Vacant(vacant) => *vacant.insert(self.output.add(piece)?),
};
self.output.sequence.push(id);
self.output.len += piece.len();
Ok(())
}
pub(crate) const fn is_empty(&self) -> bool {
self.output.is_empty()
}
pub(crate) fn finish(self) -> CompactOutput {
self.output
}
}
pub trait ResultObserver {
fn start_element(&mut self, name: &QName, attributes: &[(QName, String)]);
fn end_element(&mut self);
fn text(&mut self, text: &str);
fn comment(&mut self, text: &str);
fn processing_instruction(&mut self, target: &str, data: &str) {
let _ = (target, data);
}
}
impl ResultObserver for () {
fn start_element(&mut self, _name: &QName, _attributes: &[(QName, String)]) {}
fn end_element(&mut self) {}
fn text(&mut self, _text: &str) {}
fn comment(&mut self, _text: &str) {}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct CompactTransformation {
pub output: CompactOutput,
pub warnings: Vec<String>,
pub messages: Vec<String>,
}
#[cfg(test)]
mod tests {
use super::*;
fn build(pieces: &[&str]) -> CompactOutput {
let mut builder = CompactBuilder::default();
for piece in pieces {
builder.push(piece).unwrap();
}
builder.finish()
}
#[test]
fn keeps_each_distinct_piece_once() {
let output = build(&["<a>", "\n <b/>", "", "\n <b/>", "x", "\n <b/>", "</a>"]);
assert_eq!(output.to_text(), "<a>\n <b/>\n <b/>x\n <b/></a>");
assert_eq!(output.len(), output.to_text().len());
assert_eq!(output.blocks, ["<a>\n <b/>x</a>"]);
assert_eq!(output.sequence, [0, 1, 1, 2, 1, 3]);
assert_eq!(output.to_string(), output.to_text());
}
#[test]
fn pieces_fill_blocks_and_a_larger_piece_has_its_own() {
let small = "x".repeat(1000);
let large = "y".repeat(BLOCK + 1);
let mut pieces: Vec<String> = (0..100).map(|i| format!("{i}{small}")).collect();
pieces.insert(30, large.clone());
pieces.push(large);
let refs: Vec<&str> = pieces.iter().map(String::as_str).collect();
let mut output = build(&refs);
assert_eq!(output.to_text(), pieces.concat());
assert_eq!(output.spans.len(), 101);
let capacities: Vec<usize> = output.blocks.iter().map(String::capacity).collect();
assert_eq!(capacities[..4], [1 << 10, 1 << 10, 2 << 10, 4 << 10]);
assert!(capacities.contains(&(BLOCK + 1)), "{capacities:?}");
assert!(capacities.iter().all(|&c| c <= BLOCK + 1));
let distinct: usize = pieces.iter().take(101).map(String::len).sum();
assert_eq!(
output.blocks.iter().map(String::len).sum::<usize>(),
distinct
);
output.shrink_to_fit();
assert_eq!(output.to_text(), pieces.concat());
}
#[test]
fn equal_when_the_texts_are() {
let a = build(&["ab", "cd", "e"]);
let b = build(&["a", "bcde"]);
let c = build(&["ab", "cd", "f"]);
assert_eq!(a, b);
assert_ne!(a, c);
assert_ne!(a, build(&["ab", "cd"]));
assert_eq!(a, "abcde");
assert_ne!(a, "abcdf");
assert_eq!(CompactOutput::default(), "");
assert_eq!(CompactOutput::default(), build(&["", ""]));
assert_eq!(CompactOutput::from("abcde"), a);
assert_eq!(CompactOutput::from(""), CompactOutput::default());
}
#[test]
fn debug_as_a_string() {
let text = "<a b=\"'\">\n\t\\ \u{301}é\u{0}</a>";
let output = build(&["<a b=\"'\">", "\n\t\\ \u{301}", "é\u{0}</a>"]);
assert_eq!(format!("{output:?}"), format!("{text:?}"));
}
}