Compare commits

..
6 changed files with 53 additions and 28 deletions
+2 -2
View File
File diff suppressed because one or more lines are too long
+2 -6
View File
@@ -474,18 +474,14 @@ fn parse_blocks(lines: &[String], depth: usize, level: u8, refs: &Refs) -> Vec<F
let p = Para { align: a, inlines: parse_inlines(s, &Fmt::default(), depth, refs), ..Default::default() };
Cell { blocks: vec![FBlock::Para(p)], header, ..Default::default() }
};
// A document row keeps MAX_COLS cells: only those are built, so a wide header
// doesn't pad every sparse row below it out to its width.
let cols = aligns.len().min(wordcraft_doc::table::MAX_COLS);
table.rows.push(head.iter().zip(&aligns).take(cols).map(|(s, a)| cell(s, true, *a)).collect());
table.rows.push(head.iter().zip(&aligns).map(|(s, a)| cell(s, true, *a)).collect());
i += 2;
while let Some(l) = lines.get(i) {
if is_blank(l) || !l.contains('|') || interrupts(l) {
break;
}
let mut cells = split_row(l);
cells.truncate(cols);
cells.resize(cols, String::new());
cells.resize(aligns.len(), String::new());
table.rows.push(cells.iter().zip(&aligns).map(|(s, a)| cell(s, false, *a)).collect());
i += 1;
}
-16
View File
@@ -714,19 +714,3 @@ fn exported_link_destinations_read_back_unchanged() {
assert_eq!(md("https://example.com/a b"), "[x](<https://example.com/a b>)");
assert_eq!(md("https://e.com/x\ny"), "[x](https://e.com/xy)");
}
#[test]
fn a_wide_table_builds_only_the_cells_a_row_keeps() {
// A 5,000-column header padded every one-cell row below it to 5,000 cells.
let n = 5000;
let md = format!("{}|\n{}|\n{}", "|h".repeat(n), "|-".repeat(n), "|x|\n".repeat(200));
// A body row wider than the limit is cut to it too.
let md = format!("{md}{}|\n", "|y".repeat(n));
let flow = parse(&md);
let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") };
assert_eq!(t.rows.len(), 202);
assert!(t.rows.iter().all(|r| r.len() == wordcraft_doc::table::MAX_COLS));
// A narrow table is unchanged.
let FBlock::Table(t) = &parse("| a | b |\n| - | - |\n| x |\n").blocks[0] else { panic!("expected table") };
assert_eq!(t.rows[1].len(), 2);
}
+37 -3
View File
@@ -118,6 +118,23 @@ struct Builder<'a> {
revs: std::collections::HashMap<(u8, RevMark), u32>,
/// Link and font bytes given to runs so far ([`MAX_RUN_STRINGS`]).
run_strings: usize,
/// The ids the next list's abstract and concrete numbering take: past every id in `doc`,
/// found once rather than by a scan of every numbering per list.
next_abstract: u32,
next_num: u32,
/// Ids in use, for when a document already holds `u32::MAX` and the next id wraps.
used_abstract: std::collections::HashSet<u32>,
used_num: std::collections::HashSet<u32>,
}
/// The next id from `next` that is not in `used`, wrapping past `u32::MAX` to `min`; marks it used.
fn free_id(used: &mut std::collections::HashSet<u32>, next: &mut u32, min: u32) -> u32 {
let mut id = *next;
while !used.insert(id) {
id = id.checked_add(1).unwrap_or(min);
}
*next = id.checked_add(1).unwrap_or(min);
id
}
fn rev_key(kind: RevisionKind) -> u8 {
@@ -137,7 +154,11 @@ impl<'a> Builder<'a> {
revs.entry((rev_key(r.kind), mark)).or_insert(i as u32);
}
}
Builder { doc, revs, run_strings: 0 }
let next_abstract = doc.numbering.abstracts.iter().map(|a| a.id.saturating_add(1)).max().unwrap_or(0);
let next_num = doc.numbering.nums.iter().map(|n| n.id.saturating_add(1)).max().unwrap_or(1).max(1);
let used_abstract = doc.numbering.abstracts.iter().map(|a| a.id).collect();
let used_num = doc.numbering.nums.iter().map(|n| n.id).collect();
Builder { doc, revs, run_strings: 0, next_abstract, next_num, used_abstract, used_num }
}
/// The document revision for a tracked change, added the first time it is seen.
@@ -291,9 +312,9 @@ impl<'a> Builder<'a> {
})
.collect();
let numbering = &mut self.doc.numbering;
let aid = numbering.abstracts.iter().map(|a| a.id + 1).max().unwrap_or(0);
let aid = free_id(&mut self.used_abstract, &mut self.next_abstract, 0);
let nid = free_id(&mut self.used_num, &mut self.next_num, 1);
numbering.abstracts.push(AbstractNum { id: aid, name: None, levels, ..Default::default() });
let nid = numbering.nums.iter().map(|n| n.id + 1).max().unwrap_or(1).max(1);
numbering.nums.push(Num { id: nid, abstract_id: aid, ..Default::default() });
nid
}
@@ -449,3 +470,16 @@ pub fn part_blocks(doc: &mut Document, blocks: &[FBlock]) -> Blocks {
}
out
}
#[cfg(test)]
mod free_id_tests {
use super::free_id;
#[test]
fn a_document_holding_the_largest_id_still_gets_free_ones() {
let mut used: std::collections::HashSet<u32> = [1, 2, u32::MAX].into();
let mut next = u32::MAX;
let ids: Vec<u32> = (0..3).map(|_| free_id(&mut used, &mut next, 1)).collect();
assert_eq!(ids, [3, 4, 5]);
}
}
+11
View File
@@ -1019,3 +1019,14 @@ fn latex_export_keeps_repeated_spaces() {
let d = crate::import("tex", tex.as_bytes()).unwrap().unwrap();
assert_eq!(para_texts(&d), vec!["a b c".to_string()]);
}
#[test]
fn many_imported_lists_take_fresh_numbering_ids_in_turn() {
// Each list found its ids by scanning every numbering made before it (quadratic in lists).
let md: String = (0..4000).map(|i| if i % 2 == 0 { "- a\n\n" } else { "1. b\n\n" }).collect();
let d = import("md", md.as_bytes()).expect("handled").expect("import");
let ids: Vec<u32> = d.numbering.nums.iter().map(|n| n.id).collect();
assert_eq!(ids, (1..=4000).collect::<Vec<u32>>());
let abstracts: Vec<u32> = d.numbering.abstracts.iter().map(|a| a.id).collect();
assert_eq!(abstracts, (0..4000).collect::<Vec<u32>>());
}
+1 -1
View File
File diff suppressed because one or more lines are too long