JFLib

diagram_core/src/strings.rs#

Source: lib/diagram_core/src/strings.rs

strings -- the document's string table: names, formulas and texts kept once, as Sym

  Strings::new()                  "" is Sym::EMPTY, then the names the crate knows
  s.intern("PinX") -> Sym         the string's handle (the same text -> the same Sym)
  s.get("PinX") -> Option<Sym>    without adding it
  s.str(sym) -> &str

A drawing with thousands of shapes repeats a few hundred cell names and formulas many times
(THEMEVAL(), GUARD(Width*0.5) ...); each text is kept once and a cell holds four-byte
handles.  The table only grows: a Sym stays valid as long as its document, so undo steps can
keep them.  A Sym belongs to one document; between documents texts go through str().

The texts come from files, which are untrusted: the index is an ordered map, so no choice of
texts makes a lookup slower than log n comparisons (a hash table with a fixed hash can be
flooded with texts that all fall on one slot).
  • struct Sym(pub(crate) u32) a text of a document's string table (cell and row names, formulas, values, raw XML)

impl Sym

  • const EMPTY: Sym = Sym(0) the empty text

  • fn index(self) -> u32 the position in the table (0 = the empty text)

  • struct Strings the string table of a document

impl Strings

  • fn new() -> Self a table with the empty text and the names the crate itself refers to
  • fn intern(&mut self, text: &str) -> Sym the handle of text, added when it is new
  • fn get(&self, text: &str) -> Option<Sym> the handle of text when the table has it
  • fn str(&self, sym: Sym) -> &str the text of a handle ("" for a handle of another, larger table)
  • fn len(&self) -> usize the number of texts in the table
  • fn is_empty(&self) -> bool whether the table holds nothing but the empty text and the known names

Source: docs/API.md in the JFLib repository.