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 lookupCaseRange(r rune, caseRange []CaseRange) *CaseRange {
212
213 lo := 0
214 hi := len(caseRange)
215 for lo < hi {
216 m := int(uint(lo+hi) >> 1)
217 cr := &caseRange[m]
218 if rune(cr.Lo) <= r && r <= rune(cr.Hi) {
219 return cr
220 }
221 if r < rune(cr.Lo) {
222 hi = m
223 } else {
224 lo = m + 1
225 }
226 }
227 return nil
228 }
229
230
231 func convertCase(_case int, r rune, cr *CaseRange) rune {
232 delta := cr.Delta[_case]
233 if delta > MaxRune {
234
235
236
237
238
239
240
241
242
243
244 return rune(cr.Lo) + ((r-rune(cr.Lo))&^1 | rune(_case&1))
245 }
246 return r + delta
247 }
248
249
250
251 func to(_case int, r rune, caseRange []CaseRange) (mappedRune rune, foundMapping bool) {
252 if _case < 0 || MaxCase <= _case {
253 return ReplacementChar, false
254 }
255 if cr := lookupCaseRange(r, caseRange); cr != nil {
256 return convertCase(_case, r, cr), true
257 }
258 return r, false
259 }
260
261
262 func To(_case int, r rune) rune {
263 r, _ = to(_case, r, CaseRanges)
264 return r
265 }
266
267
268 func ToUpper(r rune) rune {
269 if r <= MaxASCII {
270 if 'a' <= r && r <= 'z' {
271 r -= 'a' - 'A'
272 }
273 return r
274 }
275 return To(UpperCase, r)
276 }
277
278
279 func ToLower(r rune) rune {
280 if r <= MaxASCII {
281 if 'A' <= r && r <= 'Z' {
282 r += 'a' - 'A'
283 }
284 return r
285 }
286 return To(LowerCase, r)
287 }
288
289
290 func ToTitle(r rune) rune {
291 if r <= MaxASCII {
292 if 'a' <= r && r <= 'z' {
293 r -= 'a' - 'A'
294 }
295 return r
296 }
297 return To(TitleCase, r)
298 }
299
300
301 func (special SpecialCase) ToUpper(r rune) rune {
302 r1, hadMapping := to(UpperCase, r, []CaseRange(special))
303 if r1 == r && !hadMapping {
304 r1 = ToUpper(r)
305 }
306 return r1
307 }
308
309
310 func (special SpecialCase) ToTitle(r rune) rune {
311 r1, hadMapping := to(TitleCase, r, []CaseRange(special))
312 if r1 == r && !hadMapping {
313 r1 = ToTitle(r)
314 }
315 return r1
316 }
317
318
319 func (special SpecialCase) ToLower(r rune) rune {
320 r1, hadMapping := to(LowerCase, r, []CaseRange(special))
321 if r1 == r && !hadMapping {
322 r1 = ToLower(r)
323 }
324 return r1
325 }
326
327
328
329
330
331 type foldPair struct {
332 From uint16
333 To uint16
334 }
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354 func SimpleFold(r rune) rune {
355 if r < 0 || r > MaxRune {
356 return r
357 }
358
359 if int(r) < len(asciiFold) {
360 return rune(asciiFold[r])
361 }
362
363
364 lo := 0
365 hi := len(caseOrbit)
366 for lo < hi {
367 m := int(uint(lo+hi) >> 1)
368 if rune(caseOrbit[m].From) < r {
369 lo = m + 1
370 } else {
371 hi = m
372 }
373 }
374 if lo < len(caseOrbit) && rune(caseOrbit[lo].From) == r {
375 return rune(caseOrbit[lo].To)
376 }
377
378
379
380
381 if cr := lookupCaseRange(r, CaseRanges); cr != nil {
382 if l := convertCase(LowerCase, r, cr); l != r {
383 return l
384 }
385 return convertCase(UpperCase, r, cr)
386 }
387 return r
388 }
389
View as plain text