-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcasefold.jl
More file actions
367 lines (337 loc) · 10.5 KB
/
casefold.jl
File metadata and controls
367 lines (337 loc) · 10.5 KB
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
#=
Case folding for Unicode Str types
Copyright 2017-2018 Gandalf Software, Inc., Scott P. Jones
Licensed under MIT License, see LICENSE.md
=#
_wide_lower_l(c) = ifelse(c > (V6_COMPAT ? 0xdf : 0xde), c != 0xf7, c == 0xb5)
@inline _wide_lower_ch(ch) =
ch <= 0x7f ? _islower_a(ch) : (ch > 0xff ? _islower_u(ch) : _wide_lower_l(ch))
@inline _isupper_ch(ch) =
ch <= 0x7f ? _isupper_a(ch) : (ch > 0xff ? _isupper_u(ch) : _isupper_l(ch))
_wide_lower_latin(ch) = (ch == 0xb5) | (ch == 0xff) | (!V6_COMPAT && (ch == 0xdf))
_wide_out_upper(ch) =
ifelse(ch == 0xb5, 0x39c,
ifelse(ch == 0xff, 0x178, ifelse(!V6_COMPAT && ch == 0xdf, 0x1e9e, ch%UInt16)))
function uppercase_first(str::MaybeSub{S}) where {C<:ASCIICSE,S<:Str{C}}
(len = ncodeunits(str)) == 0 && return str
@preserve str begin
pnt = pointer(str)
ch = get_codeunit(pnt)
_islower_a(ch) || return str
out = _allocate(len)
unsafe_copyto!(out, pnt, len)
set_codeunit!(out, ch - 0x20)
Str(C, out)
end
end
function lowercase_first(str::MaybeSub{S}) where {C<:ASCIICSE,S<:Str{C}}
(len = ncodeunits(str)) == 0 && return str
@preserve str begin
pnt = pointer(str)
ch = get_codeunit(pnt)
_isupper_a(ch) || return str
out = _allocate(len)
unsafe_copyto!(out, pnt, len)
set_codeunit!(out, ch + 0x20)
Str(C, out)
end
end
function _upper(::Type{C}, beg::Ptr{UInt8}, off, len) where {C<:ASCIICSE}
buf, out = _allocate(UInt8, len)
fin = out + len
unsafe_copyto!(out, beg, len)
out += off
while out < fin
ch = get_codeunit(out)
_islower_a(ch) && set_codeunit!(out, ch - 0x20)
out += 1
end
Str(C, buf)
end
function _lower(::Type{C}, beg::Ptr{UInt8}, off, len) where {C<:ASCIICSE}
buf, out = _allocate(UInt8, len)
fin = out + len
unsafe_copyto!(out, beg, len)
out += off
while out < fin
ch = get_codeunit(out)
_isupper_a(ch) && set_codeunit!(out, ch + 0x20)
out += 1
end
Str(C, buf)
end
function _upper(::Type{C}, beg::Ptr{UInt8}, off, len) where {C<:LatinCSE}
buf, out = _allocate(UInt8, len)
fin = out + len
unsafe_copyto!(out, beg, len)
out += off
while out < fin
ch = get_codeunit(out)
_can_upper(ch) && set_codeunit!(out, ch - 0x20)
out += 1
end
Str(C, buf)
end
function uppercase(str::MaybeSub{S}) where {C<:ASCIICSE,S<:Str{C}}
(len = ncodeunits(str)) == 0 && return str
@preserve str begin
pnt = beg = pointer(str)
fin = beg + len
while pnt < fin
_islower_a(get_codeunit(pnt)) && return _upper(C, beg, pnt-beg, len)
pnt += 1
end
end
str
end
function lowercase(str::MaybeSub{S}) where {C<:ASCIICSE,S<:Str{C}}
(len = ncodeunits(str)) == 0 && return str
@preserve str begin
pnt = beg = pointer(str)
fin = beg + len
while pnt < fin
_isupper_a(get_codeunit(pnt)) && return _lower(C, beg, pnt-beg, len)
pnt += 1
end
end
str
end
function uppercase_first(str::MaybeSub{S}) where {C<:LatinCSE,S<:Str{C}}
(len = ncodeunits(str)) == 0 && return str
@preserve str begin
pnt = pointer(str)
ch = get_codeunit(pnt)
_can_upper(ch) || return str
buf, out = _allocate(UInt8, len)
set_codeunit!(out, ch - 0x20)
len > 1 && unsafe_copyto!(out, pnt+1, len-1)
Str(C, buf)
end
end
# Special handling for characters that can't map into Latin1
function uppercase_first(str::MaybeSub{S}) where {C<:_LatinCSE,S<:Str{C}}
(len = ncodeunits(str)) == 0 && return str
@preserve str begin
pnt = pointer(str)
ch = get_codeunit(pnt)
if _can_upper(ch)
buf, out8 = _allocate(UInt8, len)
set_codeunit!(out8, ch - 0x20)
len > 1 && unsafe_copyto!(out8, pnt+1, len-1)
Str(C, buf)
elseif _wide_lower_latin(ch)
buf, out = _allocate(UInt16, len)
set_codeunit!(out, _wide_out_upper(ch))
# Perform the widen operation on the rest (should be done via SIMD)
@inbounds for i = 2:len
set_codeunit!(out += 2, get_codeunit(pnt += 2)%UInt16)
end
Str(_UCS2CSE, buf)
else
str
end
end
end
function lowercase_first(str::MaybeSub{S}) where {C<:Latin_CSEs,S<:Str{C}}
(len = ncodeunits(str)) == 0 && return str
@preserve str begin
pnt = pointer(str)
ch = get_codeunit(pnt)
_isupper(ch) || return str
buf, out = _allocate(UInt8, len)
set_codeunit!(out, ch + 0x20)
len > 1 && unsafe_copyto!(out, pnt+1, len-1)
Str(C, buf)
end
end
function _upper(::Type{C}, beg::Ptr{UInt8}, off, len) where {C<:_LatinCSE}
fin = beg + len
cur = beg + off
# Need to scan the rest of the string to see if _widenupper needs to be called
while cur < fin
_wide_lower_latin(get_codeunit(cur)) && return _widenupper(beg, off, len)
cur += 1
end
buf, out = _allocate(UInt8, len)
fin = out + len
unsafe_copyto!(out, beg, len)
out += off
while out < fin
ch = get_codeunit(out)
_can_upper(ch) && set_codeunit!(out, ch - 0x20)
out += 1
end
Str(C, buf)
end
function _widen!(dst::Ptr{T}, src::Ptr{S}, fin::Ptr{S}) where {T<:CodeUnitTypes, S<:CodeUnitTypes}
while src < fin
set_codeunit!(dst, get_codeunit(src)%T)
dst += sizeof(T)
src += sizeof(S)
end
nothing
end
const _narrow! = _widen! # When this is optimized for SSE/AVX/etc. instructions, will be different
function _widenupper(beg::Ptr{UInt8}, off, len)
buf, out = _allocate(UInt16, len)
fin = bytoff(out, len)
cur = beg + off
_widen!(out, beg, cur)
out = bytoff(out, off)
while out < fin
ch = get_codeunit(cur)
set_codeunit!(out, _can_upper(ch) ? ch - 0x20 : _wide_out_upper(ch))
cur += 1
out += 2
end
Str(_UCS2CSE, buf)
end
function uppercase(str::MaybeSub{S}) where {C<:LatinCSE,S<:Str{C}}
(len = ncodeunits(str)) == 0 && return str
@preserve str begin
pnt = beg = pointer(str)
fin = beg + len
while pnt < fin
_can_upper(get_codeunit(pnt)) && return _upper(C, beg, pnt-beg, len)
pnt += 1
end
end
str
end
function uppercase(str::MaybeSub{S}) where {C<:_LatinCSE,S<:Str{C}}
(len = ncodeunits(str)) == 0 && return str
@preserve str begin
pnt = beg = pointer(str)
fin = beg + len
while pnt < fin
ch = get_codeunit(pnt)
_wide_lower_latin(ch) && return _widenupper(beg, pnt-beg, len)
_can_upper(ch) && return _upper(C, beg, pnt-beg, len)
pnt += 1
end
end
str
end
function _lower(::Type{C}, beg::Ptr{UInt8}, off, len) where {C<:Latin_CSEs}
buf, out = _allocate(UInt8, len)
fin = out + len
unsafe_copyto!(out, beg, len)
out += off
while out < fin
ch = get_codeunit(out)
_isupper_al(ch) && set_codeunit!(out, ch + 0x20)
out += 1
end
Str(C, buf)
end
function lowercase(str::MaybeSub{S}) where {C<:Latin_CSEs,S<:Str{C}}
(len = ncodeunits(str)) == 0 && return str
@preserve str begin
pnt = beg = pointer(str)
fin = beg + len
while pnt < fin
_isupper_al(get_codeunit(pnt)) && return _lower(C, beg, pnt-beg, len)
pnt += 1
end
end
str
end
# result must have at least one character > 0xff, so if the only character(s)
# > 0xff became <= 0xff, then the result may need to be narrowed and returned as _LatinStr
function _lower(::Type{C}, beg, off, len) where {C<:_UCS2CSE}
CU = codeunit(C)
buf, out = _allocate(CU, len)
unsafe_copyto!(out, beg, len)
fin = out + (len*sizeof(CU))
out += off
flg = false
while out < fin
ch = get_codeunit(out)
if ch <= 0x7f
_isupper_a(ch) && set_codeunit!(out, ch += 0x20)
elseif ch <= 0xff
_isupper_l(ch) && set_codeunit!(out, ch += 0x20)
elseif _isupper_u(ch)
ch = _lowercase_u(ch)
flg = ch <= 0xff
set_codeunit!(out, ch)
end
out += sizeof(CU)
end
if flg && is_latin(buf)
out = pointer(buf)
buf = _allocate(len)
_narrow!(pointer(buf), out, out + len)
Str(_LatinCSE, buf)
else
Str(C, buf)
end
end
function _lower(::Type{C}, beg, off, len) where {C<:Union{UCS2CSE,UTF32_CSEs}}
CU = codeunit(C)
buf, out = _allocate(CU, len)
unsafe_copyto!(out, beg, len)
fin = out + (len*sizeof(CU))
out += off
while out < fin
ch = get_codeunit(out)
if ch <= 0x7f
_isupper_a(ch) && set_codeunit!(out, ch += 0x20)
elseif ch <= 0xff
_isupper_l(ch) && set_codeunit!(out, ch += 0x20)
elseif _isupper_u(ch)
set_codeunit!(out, _lowercase_u(ch))
end
out += sizeof(CU)
end
Str(C, buf)
end
function lowercase(str::MaybeSub{S}) where {C<:Union{UCS2_CSEs,UTF32_CSEs},S<:Str{C}}
@preserve str begin
CU = codeunit(C)
pnt = beg = pointer(str)
fin = beg + sizeof(str)
while pnt < fin
_isupper_ch(get_codeunit(pnt)) && return _lower(C, beg, pnt-beg, ncodeunits(str))
pnt += sizeof(CU)
end
end
str
end
function _upper(::Type{C}, beg, off, len) where {C<:Union{UCS2_CSEs,UTF32_CSEs}}
CU = codeunit(C)
buf, out = _allocate(CU, len)
unsafe_copyto!(out, beg, len)
fin = out + (len*sizeof(CU))
out += off
while out < fin
ch = get_codeunit(out)
if ch <= 0x7f
_islower_a(ch) && set_codeunit!(out, ch -= 0x20)
elseif ch > 0xff
_islower_u(ch) && set_codeunit!(out, _uppercase_u(ch))
elseif _can_upper(ch)
set_codeunit!(out, ch -= 0x20)
elseif ch == 0xb5
set_codeunit!(out, 0x39c)
elseif ch == 0xff
set_codeunit!(out, 0x178)
elseif !V6_COMPAT && ch == 0xdf
set_codeunit!(out, 0x1e9e)
end
out += sizeof(CU)
end
Str(C, buf)
end
function uppercase(str::MaybeSub{S}) where {C<:Union{UCS2_CSEs,UTF32_CSEs},S<:Str{C}}
@preserve str begin
CU = codeunit(C)
pnt = beg = pointer(str)
fin = beg + sizeof(str)
while pnt < fin
_wide_lower_ch(get_codeunit(pnt)) && return _upper(C, beg, pnt-beg, ncodeunits(str))
pnt += sizeof(CU)
end
str
end
end