-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoldcase.go
131 lines (124 loc) · 2.71 KB
/
foldcase.go
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
package peg
import (
"unicode"
"unicode/utf8"
)
var (
// performance workaround: avoid ASCII runes being treated as special rune.
foldCaseWorkAround = map[rune]rune{
'ſ': 'ſ', // => ASCII 'S'
'K': 'K', // => ASCII 'K'
}
// runes that utf8len(runeFoldCase(r)) != utf8len(r).
lengthChangedAfterFoldCase = map[rune]Pattern{
'ᲀ': S("ᲀВв"),
'В': S("ᲀВв"),
'в': S("ᲀВв"),
'ᲁ': S("ᲁДд"),
'Д': S("ᲁДд"),
'д': S("ᲁДд"),
'ᲂ': S("ᲂОо"),
'О': S("ᲂОо"),
'о': S("ᲂОо"),
'ᲃ': S("ᲃСс"),
'С': S("ᲃСс"),
'с': S("ᲃСс"),
'ᲄ': S("ᲄᲅТт"),
'ᲅ': S("ᲄᲅТт"),
'Т': S("ᲄᲅТт"),
'т': S("ᲄᲅТт"),
'ᲆ': S("ᲆЪъ"),
'Ъ': S("ᲆЪъ"),
'ъ': S("ᲆЪъ"),
'ᲇ': S("ᲇѢѣ"),
'Ѣ': S("ᲇѢѣ"),
'ѣ': S("ᲇѢѣ"),
'ẞ': S("ẞß"),
'ß': S("ẞß"),
'ι': S("ιͅΙι"),
'ͅ': S("ιͅΙι"),
'Ι': S("ιͅΙι"),
'ι': S("ιͅΙι"),
'Ω': S("ΩΩω"),
'Ω': S("ΩΩω"),
'ω': S("ΩΩω"),
'Å': S("ÅÅå"),
'Å': S("ÅÅå"),
'å': S("ÅÅå"),
'Ɫ': S("Ɫɫ"),
'ɫ': S("Ɫɫ"),
'Ɽ': S("Ɽɽ"),
'ɽ': S("Ɽɽ"),
'ⱥ': S("ⱥȺ"),
'Ⱥ': S("ⱥȺ"),
'ⱦ': S("ⱦȾ"),
'Ⱦ': S("ⱦȾ"),
'Ɑ': S("Ɑɑ"),
'ɑ': S("Ɑɑ"),
'Ɱ': S("Ɱɱ"),
'ɱ': S("Ɱɱ"),
'Ɐ': S("Ɐɐ"),
'ɐ': S("Ɐɐ"),
'Ɒ': S("Ɒɒ"),
'ɒ': S("Ɒɒ"),
'Ȿ': S("Ȿȿ"),
'ȿ': S("Ȿȿ"),
'Ɀ': S("Ɀɀ"),
'ɀ': S("Ɀɀ"),
'Ɥ': S("Ɥɥ"),
'ɥ': S("Ɥɥ"),
'Ɦ': S("Ɦɦ"),
'ɦ': S("Ɦɦ"),
'Ɜ': S("Ɜɜ"),
'ɜ': S("Ɜɜ"),
'Ɡ': S("Ɡɡ"),
'ɡ': S("Ɡɡ"),
'Ɬ': S("Ɬɬ"),
'ɬ': S("Ɬɬ"),
'Ɪ': S("Ɪɪ"),
'ɪ': S("Ɪɪ"),
'Ʞ': S("Ʞʞ"),
'ʞ': S("Ʞʞ"),
'Ʇ': S("Ʇʇ"),
'ʇ': S("Ʇʇ"),
'Ʝ': S("Ʝʝ"),
'ʝ': S("Ʝʝ"),
}
)
// Check if string would keep its length unchanged after case folding.
func couldSafelyFoldCase(text string) bool {
for _, r := range text {
if _, ok := lengthChangedAfterFoldCase[r]; ok {
return false
}
}
return true
}
// Unicode case folding for strings (with several workarounds).
func foldCase(s string) string {
encoded := make([]byte, 0, 16)
buf := make([]byte, 4)
for i, r := range s {
if r == unicode.ReplacementChar {
encoded = append(encoded, s[i])
} else {
n := utf8.EncodeRune(buf, runeFoldCase(r))
encoded = append(encoded, buf[:n]...)
}
}
return string(encoded)
}
// Unicode case folding for runes (with several workarounds).
func runeFoldCase(r rune) rune {
if w, ok := foldCaseWorkAround[r]; ok {
return w
}
r0 := unicode.SimpleFold(r)
if r0 == r {
return r
}
for r0 > r {
r0 = unicode.SimpleFold(r0)
}
return r0
}