Back to API Reference
Struct

GraphemeCountCache

sealed

Incremental grapheme-cluster count for a document — the per-keystroke companion of Count. The count is cached by Version; PredictCount evaluates a pending edit by re-segmenting only a boundary-safe window around it, and CommitEdit promotes that prediction to the new cached count when the applied EditShape matches. Any mutation the cache was not told about degrades to one full recount on the next query.

Remarks

Window edges are placed on boundaries that UAX #29 guarantees regardless of surrounding context (no RI-parity, ZWJ-emoji, or Indic-conjunct chain can cross them), so clusters outside the window are provably unaffected by the edit. When no safe edge exists within MaxAnchorScan codepoints (a pathological run of joiners), the prediction falls back to per-part arithmetic and the commit invalidates instead.
public int Count()

Grapheme-cluster count of the whole document. O(1) while the cached version matches; one full segmentation (pooled, allocation-free steady state) otherwise.

public int PredictCount()

Grapheme-cluster count of the document AFTER applying the proposed edit, computed from a boundary-safe window around it — never a whole-document pass while the cache is warm. Follow up with CommitEdit once the edit is applied so the prediction becomes the cached count.

public void CommitEdit()

Applies a mutation to the cached count: an edit matching the last prediction — same shape AND same inserted content (a later hook may have rewritten the text) — promotes it; anything else invalidates, deferring to a full recount on the next query.

public void Invalidate()

Discards all cached state.