Overview
Grapheme clusters, the characters a reader sees, by Unicodeβs rules (UAX #29).
(import (std unicode grapheme))
(string->graphemes "eΜπΈπͺ\r\n") ; ["Γ©" "πΈπͺ" "\r\n"]: 6 characters, 3 clusters
(string-grapheme-count "π¨βπ©βπ§") ; 1
(def c (string-cursor-next-grapheme s (string-cursor-start s))) ; past the first cluster
(string-cursor-prev-grapheme s (string-cursor-end s)) ; the start of the last
A grapheme cluster is what a reader takes for one character: a letter
and its accents, a flag of two regional indicators, an emoji sequence
joined by ZWJs, a Hangul syllable of jamo, CR LF. An editor moves its
cursor and deletes by clusters. These are the extended grapheme clusters
of UAX #29, for the Unicode version grapheme-unicode-version
gives.
Cursors on boundaries. The cursor functions take a cursor on a
cluster boundary: the start, the end, or one a grapheme function
returned. From a cursor that is not on one, they treat it as a boundary.
string-cursor-grapheme-boundary? tells which a cursor is.
Cost. A cluster has no limit on its length: a letter can carry any number of marks. A step forward reads each character of the cluster once. A step back also reads, for the three rules that look back (Indic conjuncts, emoji ZWJ sequences and flags), the run of characters the rule looks back over. A run of regional indicators is counted once: from a boundary before one, the parity is known. A loop over a text, in either direction, takes O(n) time, so long runs cannot make it quadratic.
The state machines. The rules are two state machines over
characters, which know nothing of the container, and a front end drives
them with its own cursors. This module is the front end for strings;
(std text) has the one for texts. A new container needs a few
lines: see grapheme-step and grapheme-back-pair.
The table. The properties are a table of two stages,
generated by tools/unicode/grapheme.bjo from the Unicode
Character Database into grapheme-table.protobjo, and checked
against every case of
GraphemeBreakTest.txt.
See also: string-cursor-next-grapheme, string-cursor-prev-grapheme, string-grapheme-count, string->graphemes, grapheme-step, grapheme-back-pair
Reference
grapheme-back-break?grapheme-back-moregrapheme-back-needs-after?grapheme-back-pairgrapheme-back-sotgrapheme-back-startgrapheme-back-start-beforegrapheme-back-wants?grapheme-break?grapheme-plain-break?grapheme-startgrapheme-stepstring->graphemesstring-cursor-grapheme-boundary?string-cursor-next-graphemestring-cursor-prev-graphemestring-grapheme-count
Functions
grapheme-plain-break?
function β Functions
(: grapheme-plain-break? (-> int int bool))
(grapheme-plain-break? a b)
Whether the rules put a boundary between two code points with no context.
a- The code point before.
b- The code point after.
- returns
- #t when both are printable ASCII: a front end can test bytes with it and skip the state machines.
See also: grapheme-step, grapheme-back-pair
grapheme-start
function β Functions
(: grapheme-start (-> char int))
(grapheme-start ch)
The forward state after the first character of a cluster.
ch- The character after a cluster boundary.
- returns
- A state, an int, to give to grapheme-step with the next character.
See also: grapheme-step, grapheme-break?
grapheme-step
function β Functions
(: grapheme-step (-> int char int))
(grapheme-step st ch)
The forward state after one more character.
st- The state after the character before ch.
ch- The next character.
- returns
- The state after ch: grapheme-break? says whether a boundary falls before ch.
;; The cursor after the cluster that starts at c.
(def (Tuple ch0 c1) (string-cursor-ref+next s c))
(let go ((st (grapheme-start ch0)) (c c1))
(if (string-cursor-end? s c)
c
(begin
(def (Tuple ch nxt) (string-cursor-ref+next s c))
(def st2 (grapheme-step st ch))
(if (grapheme-break? st2) c (go st2 nxt)))))
The state holds what the rules need of the text before: the last characterβs properties, and whether it ends in an emoji and a ZWJ, a Linker, or an odd number of regional indicators. Nothing is allocated.
See also: grapheme-start, grapheme-break?, grapheme-back-pair
grapheme-break?
function β Functions
(: grapheme-break? (-> int bool))
(grapheme-break? st)
Whether a forward state has a boundary before its last character.
st- A state from grapheme-step, or from grapheme-start, which has one.
- returns
- #t when a cluster ends before the character the state was last given.
See also: grapheme-step
grapheme-back-start
function β Functions
(: grapheme-back-start (-> int))
(grapheme-back-start)
The backward state when nothing is known of what follows the cursor.
- returns
- A state for the first grapheme-back-pair: at the end of a text, or for a cursor that may not be on a boundary.
See also: grapheme-back-start-before, grapheme-back-pair
grapheme-back-needs-after?
function β Functions
(: grapheme-back-needs-after? (-> char bool))
(grapheme-back-needs-after? b)
Whether the backward state needs the character after a boundary cursor.
b- The character before the cursor.
- returns
- #t when b is a regional indicator: only then does grapheme-back-start-before know more than grapheme-back-start.
See also: grapheme-back-start-before
grapheme-back-start-before
function β Functions
(: grapheme-back-start-before (-> char int))
(grapheme-back-start-before ch)
The backward state of a cursor on a cluster boundary, from the character after it.
ch- The character after the cursor.
- returns
- A state for the first grapheme-back-pair.
A boundary before a regional indicator has an even number of them before it. The state keeps that, so a loop back through a run of them does not count the run again for each flag.
See also: grapheme-back-needs-after?, grapheme-back-start
grapheme-back-pair
function β Functions
(: grapheme-back-pair (-> int char char int))
(grapheme-back-pair w a b)
The backward state for the boundary between two characters.
w- The state from the pair after this one, or from grapheme-back-start or -start-before.
a- The character before the boundary.
b- The character after it: the a of the pair given before, or the character before the cursor.
- returns
- A state that is known, for grapheme-back-break?, or that grapheme-back-wants? the characters before a.
;; The start of the cluster that ends at c, a boundary of s.
(def start (string-cursor-start s))
(def c1 (string-cursor-prev s c))
(def b1 (string-cursor-ref s c1))
(def w0 (if (and (grapheme-back-needs-after? b1) (not (string-cursor-end? s c)))
(grapheme-back-start-before (string-cursor-ref s c))
(grapheme-back-start)))
(let go ((w w0) (bc c1) (b b1))
(if (string-cursor=? bc start)
bc
(begin
(def ac (string-cursor-prev s bc))
(def a (string-cursor-ref s ac))
;; Give the characters before a while the state wants them.
(def w2 (let more ((w (grapheme-back-pair w a b)) (k ac))
(cond ((not (grapheme-back-wants? w)) w)
((string-cursor=? k start) (grapheme-back-sot w))
(else (let ((k2 (string-cursor-prev s k)))
(more (grapheme-back-more w (string-cursor-ref s k2)) k2))))))
(if (grapheme-back-break? w2) bc (go w2 ac a)))))
Most pairs are decided by the two characters. Three rules need the
characters before a: GB9c (a Linker before Extend characters and
a Consonant), GB11 (a pictograph before Extend characters, a ZWJ and a
pictograph) and GB12/13 (how many regional indicators come before). Each
reads back over one run of characters of one kind, and stops at the
first that is not.
See also: grapheme-back-wants?, grapheme-back-more, grapheme-back-break?
grapheme-back-wants?
function β Functions
(: grapheme-back-wants? (-> int bool))
(grapheme-back-wants? w)
Whether a backward state needs the character before the last one given.
w- A state from grapheme-back-pair or grapheme-back-more.
- returns
- #t until the state is known.
See also: grapheme-back-more, grapheme-back-sot
grapheme-back-more
function β Functions
(: grapheme-back-more (-> int char int))
(grapheme-back-more w ch)
The backward state after the character before the last one given.
w- A state that grapheme-back-wants? the character for.
ch- The character before the last one given.
- returns
- The state, known or still wanting the character before ch.
See also: grapheme-back-wants?, grapheme-back-sot
grapheme-back-sot
function β Functions
(: grapheme-back-sot (-> int int))
(grapheme-back-sot w)
The backward state when there is no character before the last one given.
w- A state that grapheme-back-wants? a character for.
- returns
- The known state at the start of the text.
See also: grapheme-back-more
grapheme-back-break?
function β Functions
(: grapheme-back-break? (-> int bool))
(grapheme-back-break? w)
Whether a known backward state is a boundary.
w- A state that does not want more characters.
- returns
- #t when a cluster boundary falls between the pair the state was given.
See also: grapheme-back-pair
string-cursor-next-grapheme
function β Functions
(: string-cursor-next-grapheme (-> string StringCursor StringCursor))
(string-cursor-next-grapheme s c)
The cursor after the grapheme cluster that starts at a cursor.
s- The string.
c- A cursor of s on a cluster boundary, not at the end.
- returns
- The cursor at the end of the cluster that starts at c, which is the next boundary.
- raises
System.ArgumentOutOfRangeException - When c is at the end of s.
See also: string-cursor-prev-grapheme, string-cursor-next
string-cursor-prev-grapheme
function β Functions
(: string-cursor-prev-grapheme (-> string StringCursor StringCursor))
(string-cursor-prev-grapheme s c)
The cursor at the start of the grapheme cluster that ends at a cursor.
s- The string.
c- A cursor of s on a cluster boundary, not at the start.
- returns
- The cursor at the start of the cluster before c, which is the boundary before.
- raises
System.ArgumentOutOfRangeException - When c is at the start of s.
See also: string-cursor-next-grapheme, string-cursor-prev
string-cursor-grapheme-boundary?
function β Functions
(: string-cursor-grapheme-boundary? (-> string StringCursor bool))
(string-cursor-grapheme-boundary? s c)
Whether a cursor is on a grapheme cluster boundary.
s- The string.
c- A cursor of s.
- returns
- #t at the start, at the end, and between two clusters.
Looks back as far as the rules need, with nothing known of what follows: in a run of regional indicators, that is the whole run.
See also: string-cursor-next-grapheme
string-grapheme-count
function β Functions
(: string-grapheme-count (-> string int))
(string-grapheme-count s)
The number of grapheme clusters in a string.
s- The string.
- returns
- How many clusters s holds: 0 for the empty string.
See also: string->graphemes, string-count
string->graphemes
function β Functions
(: string->graphemes (-> string (Vec string)))
(string->graphemes s)
The grapheme clusters of a string.
s- The string.
- returns
- A vec of the clusters of s, in order, each a string.
See also: string-grapheme-count, string-cursor-next-grapheme
Values
grapheme-unicode-version
value β Values
(: grapheme-unicode-version string)
The version of Unicode the rules and the table are of.
See also: grapheme-step