Compare commits

..
4 changed files with 15 additions and 40 deletions
+2 -2
View File
File diff suppressed because one or more lines are too long
+1 -31
View File
@@ -179,18 +179,9 @@ fn char_prop(w: &mut W, name: &str, c: Option<char>) {
val(w, name, &s);
}
/// Most cells a matrix is padded out to (rows × its widest row). A ragged matrix past it (a
/// native document's, or one parsed from a few bytes of text) writes each row as it is, rather
/// than output that grows with the square of its size.
const MAX_PADDED_CELLS: usize = 1 << 20;
fn write_matrix(w: &mut W, n: &MNode, depth: usize) {
let MNode::Matrix { rows, col_jc } = n else { return };
let cols = rows.iter().map(|r| r.len()).max().unwrap_or(0).max(1);
let pad = rows.len().saturating_mul(cols) <= MAX_PADDED_CELLS;
if !pad {
log::warn!("docx: a {} × {cols} matrix is too large to pad; its rows are written as they are", rows.len());
}
w.open("m:m", &[]);
w.open("m:mPr", &[]);
w.open("m:mcs", &[]);
@@ -212,7 +203,7 @@ fn write_matrix(w: &mut W, n: &MNode, depth: usize) {
w.close("m:mPr");
for row in rows {
w.open("m:mr", &[]);
for c in 0..if pad { cols } else { row.len().max(1) } {
for c in 0..cols {
arg(w, "m:e", row.get(c).map(|v| v.as_slice()).unwrap_or(&[]), depth);
}
w.close("m:mr");
@@ -489,24 +480,3 @@ fn run(w: &mut W, r: &MRun) {
w.leaf("m:t", &[("xml:space", "preserve")], &r.text);
w.close("m:r");
}
#[cfg(test)]
mod tests {
use super::*;
fn cells(rows: Vec<Vec<Arg>>) -> usize {
let mut w = W::new();
write_matrix(&mut w, &MNode::Matrix { rows, col_jc: Vec::new() }, 0);
w.s.matches("<m:e/>").count() + w.s.matches("<m:e>").count()
}
#[test]
fn a_ragged_matrix_is_padded_within_a_cell_budget() {
// A small ragged matrix is padded to a rectangle, as before.
assert_eq!(cells(vec![vec![Vec::new(); 3], vec![Vec::new()]]), 6);
// 2,000 one-cell rows and one 2,000-cell row: 4 million cells padded, past the budget.
let mut rows = vec![vec![Vec::new()]; 2000];
rows.push(vec![Vec::new(); 2000]);
assert_eq!(cells(rows), 4000);
}
}
+11 -6
View File
@@ -1,3 +1,5 @@
use std::borrow::Cow;
use super::date::{DateKind, builtin_date, custom_date, iso_datetime, serial_date};
use super::{
BTreeMap, Ev, Grid, HashMap, MAX_CELLS, MAX_COLS, MAX_ROWS, MAX_SHARED, MAX_SHEETS, MAX_XFS, Package, TextBudget, attr, push_capped, walk,
@@ -261,7 +263,7 @@ pub(super) fn xlsx_rows(pkg: &mut Package<'_>, path: &str) -> Result<Vec<Vec<Str
row.resize(c.col + 1, String::new());
}
if let Some(slot) = row.get_mut(c.col) {
*slot = text;
*slot = text.into_owned();
}
}
_ => {}
@@ -274,12 +276,15 @@ pub(super) fn xlsx_rows(pkg: &mut Package<'_>, path: &str) -> Result<Vec<Vec<Str
Ok(grid.rows)
}
fn xlsx_value(c: &XCell, shared: &[String], formats: &[Option<DateKind>], date1904: bool) -> String {
/// A cell's text: a shared string borrowed, so a cell the sheet then refuses (past the text
/// budget, the columns or the rows) costs no copy of it.
fn xlsx_value<'a>(c: &XCell, shared: &'a [String], formats: &[Option<DateKind>], date1904: bool) -> Cow<'a, str> {
let v = c.value.trim();
let kind = c.kind.trim();
if kind.eq_ignore_ascii_case("s") {
v.parse::<usize>().ok().and_then(|i| shared.get(i)).cloned().unwrap_or_default()
} else if kind.eq_ignore_ascii_case("inlineStr") {
return Cow::Borrowed(v.parse::<usize>().ok().and_then(|i| shared.get(i)).map_or("", String::as_str));
}
Cow::Owned(if kind.eq_ignore_ascii_case("inlineStr") {
if c.inline.is_empty() { c.value.clone() } else { c.inline.clone() }
} else if kind.eq_ignore_ascii_case("str") || kind.eq_ignore_ascii_case("e") {
c.value.clone()
@@ -290,12 +295,12 @@ fn xlsx_value(c: &XCell, shared: &[String], formats: &[Option<DateKind>], date19
} else if v.is_empty() {
c.inline.clone()
} else {
let Ok(x) = v.parse::<f64>() else { return v.to_string() };
let Ok(x) = v.parse::<f64>() else { return Cow::Owned(v.to_string()) };
match formats.get(c.style).copied().flatten() {
Some(kind) => serial_date(x, date1904, kind).unwrap_or_else(|| number(x)),
None => number(x),
}
}
})
}
/// A number as a spreadsheet shows it in General format: up to 15 significant digits, no
+1 -1
View File
File diff suppressed because one or more lines are too long