Import Upstream version 0.2.0
Thorsten Alteholz
3 years ago
0 | 0 | package uniseg |
1 | ||
2 | import "unicode/utf8" | |
1 | 3 | |
2 | 4 | // The states of the grapheme cluster parser. |
3 | 5 | const ( |
117 | 119 | |
118 | 120 | // NewGraphemes returns a new grapheme cluster iterator. |
119 | 121 | func NewGraphemes(s string) *Graphemes { |
120 | g := &Graphemes{} | |
121 | for index, codePoint := range s { | |
122 | g.codePoints = append(g.codePoints, codePoint) | |
123 | g.indices = append(g.indices, index) | |
124 | } | |
125 | g.indices = append(g.indices, len(s)) | |
122 | l := utf8.RuneCountInString(s) | |
123 | codePoints := make([]rune, l) | |
124 | indices := make([]int, l+1) | |
125 | i := 0 | |
126 | for pos, r := range s { | |
127 | codePoints[i] = r | |
128 | indices[i] = pos | |
129 | i++ | |
130 | } | |
131 | indices[l] = len(s) | |
132 | g := &Graphemes{ | |
133 | codePoints: codePoints, | |
134 | indices: indices, | |
135 | } | |
126 | 136 | g.Next() // Parse ahead. |
127 | 137 | return g |
128 | 138 | } |