last-grapheme ( str -- i ) (original) (raw)

last-grapheme ( str -- i )
Word and grapheme breaks

Prev: first-grapheme-from ( start str -- i )
Next: last-grapheme-from ( end str -- i )

Vocabulary
unicode

Inputs

str a string

Outputs

i an index

Word description
Finds the index of the start of the last grapheme of the string. This can be used to traverse the graphemes of a string backwards.

Definition

USING: kernel math math.order ranges sequences
unicode.breaks.private ;

IN: unicode

:: last-grapheme ( str -- i )
str length 1 - :> pos! pos 0 =
[ 0 ] [
str grapheme-class pos 1 - 0 max pos! 0 pos [a..b] [
0 swap 1 + str
grapheme-class dup rot grapheme-break?
] find-last drop ?1+ nip
] if ;