Source file
src/unicode/letter.go
1
2
3
4
5
6
7 package unicode
8
9 const (
10 MaxRune = '\U0010FFFF'
11 ReplacementChar = '\uFFFD'
12 MaxASCII = '\u007F'
13 MaxLatin1 = '\u00FF'
14 )
15
16
17
18
19
20
21 type RangeTable struct {
22 R16 []Range16
23 R32 []Range32
24 LatinOffset int
25 }
26
27
28
29 type Range16 struct {
30 Lo uint16
31 Hi uint16
32 Stride uint16
33 }
34
35
36
37
38 type Range32 struct {
39 Lo uint32
40 Hi uint32
41 Stride uint32
42 }
43
44
45
46
47
48
49
50
51
52
53
54
55
56 type CaseRange struct {
57 Lo uint32
58 Hi uint32
59 Delta d
60 }
61
62
63
64 type SpecialCase []CaseRange
65
66
67
68
69
70 const (
71 UpperCase = iota
72 LowerCase
73 TitleCase
74 MaxCase
75 )
76
77 type d [MaxCase]rune
78
79
80
81
82 const (
83 UpperLower = MaxRune + 1
84 )
85
86
87
88 const linearMax = 18
89
90
91 func is16(ranges []Range16, r uint16) bool {
92 if len(ranges) <= linearMax || r <= MaxLatin1 {
93 for i := range ranges {
94 range_ := &ranges[i]
95 if r < range_.Lo {
96 return false
97 }
98 if r <= range_.Hi {
99 return range_.Stride == 1 || (r-range_.Lo)%range_.Stride == 0
100 }
101 }
102 return false
103 }
104
105
106 lo := 0
107 hi := len(ranges)
108 for lo < hi {
109 m := int(uint(lo+hi) >> 1)
110 range_ := &ranges[m]
111 if range_.Lo <= r && r <= range_.Hi {
112 return range_.Stride == 1 || (r-range_.Lo)%range_.Stride == 0
113 }
114 if r < range_.Lo {
115 hi = m
116 } else {
117 lo = m + 1
118 }
119 }
120 return false
121 }
122
123
124 func is32(ranges []Range32, r uint32) bool {
125 if len(ranges) <= linearMax {
126 for i := range ranges {
127 range_ := &ranges[i]
128 if r < range_.Lo {
129 return false
130 }
131 if r <= range_.Hi {
132 return range_.Stride == 1 || (r-range_.Lo)%range_.Stride == 0
133 }
134 }
135 return false
136 }
137
138
139 lo := 0
140 hi := len(ranges)
141 for lo < hi {
142 m := int(uint(lo+hi) >> 1)
143 range_ := ranges[m]
144 if range_.Lo <= r && r <= range_.Hi {
145 return range_.Stride == 1 || (r-range_.Lo)%range_.Stride == 0
146 }
147 if r < range_.Lo {
148 hi = m
149 } else {
150 lo = m + 1
151 }
152 }
153 return false
154 }
155
156
157 func Is(rangeTab *RangeTable, r rune) bool {
158 r16 := rangeTab.R16
159
160 if len(r16) > 0 && uint32(r) <= uint32(r16[len(r16)-1].Hi) {
161 return is16(r16, uint16(r))
162 }
163 r32 := rangeTab.R32
164 if len(r32) > 0 && r >= rune(r32[0].Lo) {
165 return is32(r32, uint32(r))
166 }
167 return false
168 }
169
170 func isExcludingLatin(rangeTab *RangeTable, r rune) bool {
171 r16 := rangeTab.R16
172
173 if off := rangeTab.LatinOffset; len(r16) > off && uint32(r) <= uint32(r16[len(r16)-1].Hi) {
174 return is16(r16[off:], uint16(r))
175 }
176 r32 := rangeTab.R32
177 if len(r32) > 0 && r >= rune(r32[0].Lo) {
178 return is32(r32, uint32(r))
179 }
180 return false
181 }
182
183
184 func IsUpper(r rune) bool {
185
186 if uint32(r) <= MaxLatin1 {
187 return properties[uint8(r)]&pLmask == pLu
188 }
189 return isExcludingLatin(Upper, r)
190 }
191
192
193 func IsLower(r rune) bool {
194
195 if uint32(r) <= MaxLatin1 {
196 return properties[uint8(r)]&pLmask == pLl
197 }
198 return isExcludingLatin(Lower, r)
199 }
200
201
202 func IsTitle(r rune) bool {
203 if r <= MaxLatin1 {
204 return false
205 }
206 return isExcludingLatin(Title, r)
207 }
208
209
210
211 func to(_case int, r rune, caseRange []CaseRange) (mappedRune rune, foundMapping bool) {
212 if _case < 0 || MaxCase <= _case {
213 return ReplacementChar, false
214 }
215
216 lo := 0
217 hi := len(caseRange)
218 for lo < hi {
219 m := int(uint(lo+hi) >> 1)
220 cr := caseRange[m]
221 if rune(cr.Lo) <= r && r <= rune(cr.Hi) {
222 delta := cr.Delta[_case]
223 if delta > MaxRune {
224
225
226
227
228
229
230
231
232
233
234 return rune(cr.Lo) + ((r-rune(cr.Lo))&^1 | rune(_case&1)), true
235 }
236 return r + delta, true
237 }
238 if r < rune(cr.Lo) {
239 hi = m
240 } else {
241 lo = m + 1
242 }
243 }
244 return r, false
245 }
246
247
248 func To(_case int, r rune) rune {
249 r, _ = to(_case, r, CaseRanges)
250 return r
251 }
252
253
254 func ToUpper(r rune) rune {
255 if r <= MaxASCII {
256 if 'a' <= r && r <= 'z' {
257 r -= 'a' - 'A'
258 }
259 return r
260 }
261 return To(UpperCase, r)
262 }
263
264
265 func ToLower(r rune) rune {
266 if r <= MaxASCII {
267 if 'A' <= r && r <= 'Z' {
268 r += 'a' - 'A'
269 }
270 return r
271 }
272 return To(LowerCase, r)
273 }
274
275
276 func ToTitle(r rune) rune {
277 if r <= MaxASCII {
278 if 'a' <= r && r <= 'z' {
279 r -= 'a' - 'A'
280 }
281 return r
282 }
283 return To(TitleCase, r)
284 }
285
286
287 func (special SpecialCase) ToUpper(r rune) rune {
288 r1, hadMapping := to(UpperCase, r, []CaseRange(special))
289 if r1 == r && !hadMapping {
290 r1 = ToUpper(r)
291 }
292 return r1
293 }
294
295
296 func (special SpecialCase) ToTitle(r rune) rune {
297 r1, hadMapping := to(TitleCase, r, []CaseRange(special))
298 if r1 == r && !hadMapping {
299 r1 = ToTitle(r)
300 }
301 return r1
302 }
303
304
305 func (special SpecialCase) ToLower(r rune) rune {
306 r1, hadMapping := to(LowerCase, r, []CaseRange(special))
307 if r1 == r && !hadMapping {
308 r1 = ToLower(r)
309 }
310 return r1
311 }
312
313
314
315
316
317 type foldPair struct {
318 From uint16
319 To uint16
320 }
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340 func SimpleFold(r rune) rune {
341 if r < 0 || r > MaxRune {
342 return r
343 }
344
345 if int(r) < len(asciiFold) {
346 return rune(asciiFold[r])
347 }
348
349
350 lo := 0
351 hi := len(caseOrbit)
352 for lo < hi {
353 m := int(uint(lo+hi) >> 1)
354 if rune(caseOrbit[m].From) < r {
355 lo = m + 1
356 } else {
357 hi = m
358 }
359 }
360 if lo < len(caseOrbit) && rune(caseOrbit[lo].From) == r {
361 return rune(caseOrbit[lo].To)
362 }
363
364
365
366
367 if l := ToLower(r); l != r {
368 return l
369 }
370 return ToUpper(r)
371 }
372
View as plain text