package graphemesimport// FromString returns an iterator for the grapheme clusters in the input string.// Iterate while Next() is true, and access the grapheme via Value().func ( string) *Iterator[string] {return &Iterator[string]{split: splitFuncString,data: , }}// FromBytes returns an iterator for the grapheme clusters in the input bytes.// Iterate while Next() is true, and access the grapheme via Value().func ( []byte) *Iterator[[]byte] {return &Iterator[[]byte]{split: splitFuncBytes,data: , }}// Iterator is a generic iterator for grapheme clusters in strings or byte slices,// with an ASCII hot path optimization.typeIterator[ ~string | ~[]byte] struct {splitfunc(, bool) (int, , error)dataposintstartint// AnsiEscapeSequences treats 7-bit ANSI escape sequences (ECMA-48) as // single grapheme clusters when true. The default is false. // // 8-bit controls are not enabled by this option. See [AnsiEscapeSequences8Bit].AnsiEscapeSequencesbool// AnsiEscapeSequences8Bit treats 8-bit C1 ANSI escape sequences (ECMA-48) as single // grapheme clusters when true. The default is false. // // 8-bit control bytes are not UTF-8 encoded, i.e. not valid UTF-8. If you // choose this option, you are choosing to interpret non-UTF-8 data, caveat // emptor.AnsiEscapeSequences8Bitbool}var (splitFuncString = splitFunc[string]splitFuncBytes = splitFunc[[]byte])const (esc = 0x1Bcr = 0x0Dbel = 0x07can = 0x18sub = 0x1Ast = 0x9C)// Next advances the iterator to the next grapheme cluster.// Returns false when there are no more grapheme clusters.func ( *Iterator[]) () bool {if .pos >= len(.data) {returnfalse } .start = .pos := .data[.pos]if .AnsiEscapeSequences && == esc {if := ansiEscapeLength(.data[.pos:]); > 0 { .pos += returntrue } }if .AnsiEscapeSequences8Bit && >= 0x80 && <= 0x9F {if := ansiEscapeLength8Bit(.data[.pos:]); > 0 { .pos += returntrue } }// ASCII hot path: any ASCII is one grapheme when next byte is ASCII or end.if < utf8.RuneSelf && != cr {if .pos+1 >= len(.data) || .data[.pos+1] < utf8.RuneSelf { .pos++returntrue } }// Fall back to UAX29 grapheme parsing := .data[.pos:] , , := .split(, true)if != nil {panic() }if <= 0 {panic("splitFunc returned a zero or negative advance") } .pos += if .pos > len(.data) {panic("splitFunc advanced beyond end of data") }returntrue}// Value returns the current grapheme cluster.func ( *Iterator[]) () {return .data[.start:.pos]}// Start returns the byte position of the current grapheme in the original data.func ( *Iterator[]) () int {return .start}// End returns the byte position after the current grapheme in the original data.func ( *Iterator[]) () int {return .pos}// Reset resets the iterator to the beginning of the data.func ( *Iterator[]) () { .start = 0 .pos = 0}// SetText sets the data for the iterator to operate on, and resets all state.func ( *Iterator[]) ( ) { .data = .start = 0 .pos = 0}// First returns the first grapheme cluster without advancing the iterator.func ( *Iterator[]) () {iflen(.data) == 0 {return .data }// Use a copy to leverage Next()'s ASCII optimization := * .pos = 0 .start = 0 .Next()return .Value()}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)