Leaf.Collab.Text (Leaf v0.10.1)

Copy Markdown View Source

The unit an operation's offsets are counted in.

An editor describes its edit with the numbers its own strings give it: at and remove from JavaScript's length and slice, which count UTF-16 code units. String.length/1 counts graphemes, and the two agree only while the text has nothing outside the Basic Multilingual Plane and no combining marks — one emoji, and every offset after it is off by one for the room, which then refuses or misplaces every edit. So the room measures and cuts the document in the editor's unit.

Summary

Functions

Length in UTF-16 code units.

Replaces remove code units at at with insert.

The text as big-endian UTF-16, two bytes per code unit.

Functions

length(text)

@spec length(String.t()) :: non_neg_integer()

Length in UTF-16 code units.

splice(text, at, remove, insert)

@spec splice(String.t(), integer(), integer(), String.t()) ::
  {:ok, String.t()} | :error

Replaces remove code units at at with insert.

:error when the splice points outside the text or would cut a surrogate pair in half — neither can be applied without scattering characters.

utf16(text)

@spec utf16(String.t()) :: binary()

The text as big-endian UTF-16, two bytes per code unit.