1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
package tui
import (
"unicode"
"unicode/utf8"
)
func NextCluster(s string) (cluster string, width, size int) { return nextCluster(s) }
func NextClusterRunes(s string) (cluster string, width, size, runes int) {
cluster, width, size = nextCluster(s)
if size > 0 {
runes = utf8.RuneCountInString(cluster)
}
return
}
func nextCluster(s string) (cluster string, width, size int) {
if len(s) == 0 {
return "", 0, 0
}
if s[0] < 0x80 {
if len(s) == 1 || s[1] < 0x80 {
return s[:1], 1, 1
}
}
w, size, _ := clusterAdvance(decodeStringStep(s))
return s[:size], w, size
}
func nextClusterBytes(b []byte) (width, size int, base rune) {
if len(b) == 0 {
return 0, 0, 0
}
if b[0] < 0x80 {
if len(b) == 1 || b[1] < 0x80 {
return 1, 1, rune(b[0])
}
}
return clusterAdvance(decodeBytesStep(b))
}
type decodeStep func(pos int) (r rune, size, length int)
func decodeStringStep(s string) decodeStep {
return func(pos int) (rune, int, int) {
if pos >= len(s) {
return 0, 0, len(s)
}
r, size := utf8.DecodeRuneInString(s[pos:])
return r, size, len(s)
}
}
func decodeBytesStep(b []byte) decodeStep {
return func(pos int) (rune, int, int) {
if pos >= len(b) {
return 0, 0, len(b)
}
r, size := utf8.DecodeRune(b[pos:])
return r, size, len(b)
}
}
func clusterAdvance(decode decodeStep) (width, size int, base rune) {
r0, s0, length := decode(0)
if s0 == 0 {
return 0, 0, 0
}
pos := s0
w := baseRuneWidth(r0)
if regionalIndicator(r0) {
if pos < length {
r1, s1, _ := decode(pos)
if s1 > 0 && regionalIndicator(r1) {
pos += s1
return 2, pos, r0
}
}
}
lastWasZWJ := false
for pos < length {
r, sz, _ := decode(pos)
if sz == 0 {
break
}
if graphemeExtend(r) {
pos += sz
w = clusterExtendUpdateWidth(r, r0, w)
lastWasZWJ = isZWJ(r)
continue
}
if lastWasZWJ {
pos += sz
w = 2
lastWasZWJ = false
continue
}
break
}
return w, pos, r0
}
func clusterExtendUpdateWidth(extendRune, baseRune rune, currentWidth int) int {
if isVS16(extendRune) {
return 2
}
if extendRune == 0xFE0E && baseRuneWidth(baseRune) == 2 && inRuneRanges(baseRune, emojiWideRanges) {
return 1
}
return currentWidth
}
func baseRuneWidth(r rune) int {
if r < 0 || r > unicode.MaxRune {
return 1
}
if r < 0x20 || (r >= 0x7F && r < 0xA0) {
return 1
}
if inRuneRanges(r, eastAsianWideRanges) || inRuneRanges(r, emojiWideRanges) {
return 2
}
return 1
}
func graphemeExtend(r rune) bool {
if r >= 0x1F3FB && r <= 0x1F3FF {
return true
}
return unicode.In(r, unicode.Mn, unicode.Me, unicode.Mc, unicode.Join_Control, unicode.Variation_Selector)
}
func isZWJ(r rune) bool {
return r == 0x200D
}
func isVS16(r rune) bool {
return r == 0xFE0F
}
func regionalIndicator(r rune) bool {
return r >= 0x1F1E6 && r <= 0x1F1FF
}
func ClusterCount(s string) int {
n := 0
for len(s) > 0 {
_, _, size := NextCluster(s)
if size == 0 {
break
}
n++
s = s[size:]
}
return n
}
func clusterCount(s string) int { return ClusterCount(s) }
func ClusterRuneCount(s string) int {
n := 0
for len(s) > 0 {
_, _, size := NextCluster(s)
if size == 0 {
break
}
n += utf8.RuneCountInString(s[:size])
s = s[size:]
}
return n
}
func clusterEnd(s string, clusterStartRuneIdx int) int {
runeAt := 0
for len(s) > 0 {
_, _, size := nextCluster(s)
if size == 0 {
break
}
clusterRunes := utf8.RuneCountInString(s[:size])
if runeAt+clusterRunes > clusterStartRuneIdx {
return runeAt + clusterRunes
}
runeAt += clusterRunes
s = s[size:]
}
return runeAt
}
func snapRuneToClusterStart(s string, runeIdx int) int {
if len(s) == 0 || runeIdx <= 0 {
return 0
}
runeAt := 0
lastStart := 0
for len(s) > 0 {
_, _, size := nextCluster(s)
if size == 0 {
break
}
clusterRunes := utf8.RuneCountInString(s[:size])
if runeAt+clusterRunes > runeIdx {
return lastStart
}
runeAt += clusterRunes
lastStart = runeAt
s = s[size:]
}
return runeAt
}
func runeIndexToDisplayCol(s string, runeIdx int) int {
col := 0
runeAt := 0
for len(s) > 0 {
_, w, size := nextCluster(s)
if size == 0 {
break
}
clusterRunes := utf8.RuneCountInString(s[:size])
if runeAt+clusterRunes > runeIdx {
break
}
col += w
runeAt += clusterRunes
s = s[size:]
}
return col
}
func runeIndexToClusterIndex(s string, runeIdx int) int {
if runeIdx <= 0 {
return 0
}
clusters := 0
runeAt := 0
for len(s) > 0 {
_, _, size := nextCluster(s)
if size == 0 {
break
}
clusterRunes := utf8.RuneCountInString(s[:size])
if runeAt+clusterRunes > runeIdx {
break
}
clusters++
runeAt += clusterRunes
s = s[size:]
}
return clusters
}
func clusterIndexToRuneIndex(s string, clusterIdx int) int {
runeAt := 0
clusters := 0
for len(s) > 0 {
if clusters == clusterIdx {
return runeAt
}
_, _, size := nextCluster(s)
if size == 0 {
break
}
runeAt += utf8.RuneCountInString(s[:size])
clusters++
s = s[size:]
}
return runeAt
}
|