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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
# How to update the Unicode files
#
# Unicode files can be found in https://www.unicode.org/Public/VERSION_NUMBER/ where
# VERSION_NUMBER is the current Unicode version.
#
# 1. Replace UnicodeData.txt by copying original
# 2. Replace PropertyValueAliases.txt by copying original
# 3. Replace PropList.txt by copying original
# 4. Replace ScriptExtensions.txt by copying original
# 5. Replace Scripts.txt by copying original
# 6. Replace SpecialCasing.txt by copying original
# 7. Replace confusables.txt by copying original
# (from https://www.unicode.org/Public/security/VERSION_NUMBER/)
# 8. Replace IdentifierType.txt by copying original
# (from https://www.unicode.org/Public/security/VERSION_NUMBER/)
# 9. Update String.Unicode.version/0 and on String module docs (version and link)
# 10. make unicode
data_path = Path.join(__DIR__, "UnicodeData.txt")
to_binary = fn
"" ->
nil
codepoints ->
codepoints
|> :binary.split(" ", [:global])
|> Enum.map(&<<String.to_integer(&1, 16)::utf8>>)
|> IO.iodata_to_binary()
end
rangify = fn [head | tail] ->
{first, last, acc} =
Enum.reduce(tail, {head, head, []}, fn
number, {first, last, acc} when number == first - 1 ->
{number, last, acc}
number, {first, last, acc} ->
{number, number, [{first, last} | acc]}
end)
[{first, last} | acc]
end
# A character is case ignorable if:
#
# Word_Break(C) = MidLetter or MidNumLet or Single_Quote, or
# General_Category(C) = Nonspacing_Mark (Mn), Enclosing_Mark (Me), Format (Cf),
# Modifier_Letter (Lm), or Modifier_Symbol (Sk).
#
# Word breaks are defined below based on TR29 (https://unicode.org/reports/tr29/).
# The categories are computed later.
case_ignorable = [
0x0027,
0x002E,
0x2018,
0x2019,
0x2024,
0xFE52,
0xFF07,
0xFF0E,
0x00B7,
0x0387,
0x05F4,
0x2027,
0x003A,
0xFE13,
0xFE55,
0xFF1A
]
acc = {[], [], case_ignorable, [], %{}, %{}}
cased_letter_categories = :binary.compile_pattern(["Ll", "Lt", "Lu"])
case_ignorable_categories = :binary.compile_pattern(["Mn", "Me", "Cf", "Lm", "Sk"])
{codes, cased_letters, case_ignorable, non_breakable, decompositions, combining_classes} =
data_path
|> File.read!()
|> String.split(["\r\n", "\n"], trim: true)
|> Enum.reduce(acc, fn line, {cacc, lacc, iacc, wacc, dacc, kacc} ->
[
codepoint,
_name,
category,
class,
_bidi,
decomposition,
_numeric_1,
_numeric_2,
_numeric_3,
_bidi_mirror,
_unicode_1,
_iso,
upper,
lower,
title
] = :binary.split(line, ";", [:global])
cacc =
if upper != "" or lower != "" or title != "" do
[{to_binary.(codepoint), to_binary.(upper), to_binary.(lower), to_binary.(title)} | cacc]
else
cacc
end
cased_letter_categories = :binary.compile_pattern(["Ll", "Lt", "Lu"])
case_ignorable_categories = :binary.compile_pattern(["Mn", "Me", "Cf", "Lm", "Sk"])
{lacc, iacc} =
cond do
match?({0, _}, :binary.match(category, cased_letter_categories)) ->
{[String.to_integer(codepoint, 16) | lacc], iacc}
match?({0, _}, :binary.match(category, case_ignorable_categories)) ->
{lacc, [String.to_integer(codepoint, 16) | iacc]}
true ->
{lacc, iacc}
end
wacc =
case decomposition do
"<noBreak>" <> _ -> [to_binary.(codepoint) | wacc]
_ -> wacc
end
dacc =
case decomposition do
# Decomposition
<<h, _::binary>> when h != ?< ->
decomposition =
decomposition
|> :binary.split(" ", [:global])
|> Enum.map(&String.to_integer(&1, 16))
:maps.put(String.to_integer(codepoint, 16), decomposition, dacc)
_ ->
dacc
end
kacc =
case String.to_integer(class) do
0 -> kacc
n -> :maps.put(String.to_integer(codepoint, 16), n, kacc)
end
{cacc, lacc, iacc, wacc, dacc, kacc}
end)
defmodule String.Unicode do
@moduledoc false
def version, do: {15, 0, 0}
[unconditional_mappings, _conditional_mappings] =
Path.join(__DIR__, "SpecialCasing.txt")
|> File.read!()
|> :binary.split("# Conditional Mappings")
codes =
unconditional_mappings
|> String.split(["\r\n", "\n"], trim: true)
|> Enum.reduce(codes, fn
"", acc ->
acc
"#" <> _, acc ->
acc
line, acc ->
[codepoint, lower, title, upper, _] = :binary.split(line, "; ", [:global])
key = to_binary.(codepoint)
:lists.keystore(
key,
1,
acc,
{key, to_binary.(upper), to_binary.(lower), to_binary.(title)}
)
end)
# The function computes byte lookups based on the prefix. For example,
# Á, É, etc all have the same prefix <<195>>, so they are lumped
# together for lookup and then we just do a byte lookup later. We
# tried doing the byte lookup on 64-element tuple (since the byte
# is always within 0b10000000 and 0b10111111) but that's slower,
# especially because we need to check the byte range for invalid
# Unicode, instead the last byte lookup is a case. Grouping the
# top-level lookup makes the cost of a miss 3x cheaper albeit a
# hit is 10% more expensive) and reduces bytecode size.
compute_lookup = fn key_values ->
prefixes =
Enum.reduce(key_values, %{}, fn {codepoint, result}, acc ->
prefix_size = bit_size(codepoint) - 8
<<prefix::size(prefix_size)-bits, byte>> = codepoint
Map.update(acc, prefix, [{byte, result}], &[{byte, result} | &1])
end)
{singles, tables} =
Enum.reduce(Map.delete(prefixes, ""), {[], []}, fn {prefix, pairs}, {singles, tables} ->
case pairs do
[{byte, result}] ->
{[{prefix <> <<byte>>, result} | singles], tables}
_ ->
clauses =
Enum.flat_map(pairs, fn {byte, result} ->
quote do
unquote(byte) -> unquote(result)
end
end)
clauses = clauses ++ quote do: (byte -> <<unquote(prefix), byte>>)
{singles, [{prefix, clauses} | tables]}
end
end)
{Enum.sort(singles), Enum.sort_by(tables, &(-byte_size(elem(&1, 0))))}
end
# Sigma variants for Greek
@letter_sigma <<0x03A3::utf8>>
@letter_small_sigma_final <<0x03C2::utf8>>
@letter_small_sigma <<0x03C3::utf8>>
# Letter I variants for Turkic languages
@letter_I <<0x0049::utf8>>
@dotless_letter_i <<0x0131::utf8>>
@letter_i <<0x0069::utf8>>
@letter_I_dot_above <<0x0130::utf8>>
@combining_dot_above <<0x0307::utf8>>
# Downcase
# Turkic İ -> i
def downcase(<<unquote(@letter_I_dot_above), rest::bits>>, acc, mode) do
char = if mode == :turkic, do: @letter_i, else: <<@letter_i, @combining_dot_above>>
downcase(rest, [char | acc], mode)
end
def downcase(<<@letter_I, @combining_dot_above, rest::bits>>, acc, mode) do
char = if mode == :turkic, do: @letter_i, else: <<@letter_i, @combining_dot_above>>
downcase(rest, [char | acc], mode)
end
# Turkic I -> ı
def downcase(<<@letter_I, rest::bits>>, acc, mode) do
char = if mode == :turkic, do: @dotless_letter_i, else: @letter_i
downcase(rest, [char | acc], mode)
end
# Greek sigma
def downcase(<<@letter_sigma, rest::bits>>, acc, mode) do
downcased =
if mode == :greek and cased_letter_list?(acc) and not cased_letter_binary?(rest) do
@letter_small_sigma_final
else
@letter_small_sigma
end
downcase(rest, [downcased | acc], mode)
end
conditional_downcase = [@letter_I, @letter_I_dot_above, @letter_sigma]
{singles, tables} =
compute_lookup.(
for {codepoint, _upper, lower, _title} <- codes,
lower && lower != codepoint,
codepoint not in conditional_downcase,
do: {codepoint, lower}
)
for {codepoint, lower} <- singles do
def downcase(<<unquote(codepoint), rest::bits>>, acc, mode) do
downcase(rest, [unquote(lower) | acc], mode)
end
end
for {prefix, clauses} <- tables do
def downcase(<<unquote(prefix), byte, rest::bits>>, acc, mode) do
value = case byte, do: unquote(clauses)
downcase(rest, [value | acc], mode)
end
end
def downcase(<<byte, rest::bits>>, acc, mode) do
if byte >= ?A and byte <= ?Z do
downcase(rest, [byte + 32 | acc], mode)
else
downcase(rest, [byte | acc], mode)
end
end
def downcase("", acc, _mode), do: IO.iodata_to_binary(:lists.reverse(acc))
# Sigma handling
defp cased_letter_binary?(<<codepoint::utf8, rest::bits>>) do
if case_ignorable?(codepoint) do
cased_letter_binary?(rest)
else
cased_letter?(codepoint)
end
end
defp cased_letter_binary?(_), do: false
defp cased_letter_list?([<<codepoint::utf8>> | rest]) do
if case_ignorable?(codepoint) do
cased_letter_list?(rest)
else
cased_letter?(codepoint)
end
end
defp cased_letter_list?(_), do: false
for {first, last} <- rangify.(cased_letters) do
if first == last do
defp cased_letter?(unquote(first)), do: true
else
defp cased_letter?(codepoint)
when codepoint >= unquote(first) and codepoint <= unquote(last),
do: true
end
end
defp cased_letter?(_), do: false
for {first, last} <- rangify.(case_ignorable) do
if first == last do
defp case_ignorable?(unquote(first)), do: true
else
defp case_ignorable?(codepoint)
when codepoint >= unquote(first) and codepoint <= unquote(last),
do: true
end
end
defp case_ignorable?(_), do: false
# Upcase
# Turkic i -> İ
def upcase(<<@letter_i, rest::bits>>, acc, mode) do
char = if mode == :turkic, do: @letter_I_dot_above, else: @letter_I
upcase(rest, [char | acc], mode)
end
conditional_upcase = [@letter_i]
{singles, tables} =
compute_lookup.(
for {codepoint, upper, _lower, _title} <- codes,
upper && upper != codepoint,
codepoint not in conditional_upcase,
do: {codepoint, upper}
)
for {codepoint, upper} <- singles do
def upcase(<<unquote(codepoint), rest::bits>>, acc, mode) do
upcase(rest, [unquote(upper) | acc], mode)
end
end
for {prefix, clauses} <- tables do
def upcase(<<unquote(prefix), byte, rest::bits>>, acc, mode) do
value = case byte, do: unquote(clauses)
upcase(rest, [value | acc], mode)
end
end
def upcase(<<byte, rest::bits>>, acc, mode) do
if byte >= ?a and byte <= ?z do
upcase(rest, [byte - 32 | acc], mode)
else
upcase(rest, [byte | acc], mode)
end
end
def upcase("", acc, _mode), do: IO.iodata_to_binary(:lists.reverse(acc))
# Titlecase once
def titlecase_once("", _mode), do: {"", ""}
# Turkic i -> İ
def titlecase_once(<<@letter_i, rest::binary>>, mode) do
char = if mode == :turkic, do: @letter_I_dot_above, else: @letter_I
{char, rest}
end
conditional_titlecase = [@letter_i]
{singles, tables} =
compute_lookup.(
for {codepoint, _upper, _lower, title} <- codes,
title && title != codepoint,
codepoint not in conditional_titlecase,
do: {codepoint, title}
)
for {codepoint, title} <- singles do
def titlecase_once(<<unquote(codepoint), rest::bits>>, _mode) do
{unquote(title), rest}
end
end
for {prefix, clauses} <- tables do
def titlecase_once(<<unquote(prefix), byte, rest::bits>>, _mode) do
value = case byte, do: unquote(clauses)
{value, rest}
end
end
def titlecase_once(<<char::utf8, rest::binary>>, _mode) do
if char >= ?a and char <= ?z do
{<<char - 32::utf8>>, rest}
else
{<<char::utf8>>, rest}
end
end
def titlecase_once(<<char, rest::binary>>, _mode) do
{<<char>>, rest}
end
end
defmodule String.Break do
@moduledoc false
@whitespace_max_size 3
prop_path = Path.join(__DIR__, "PropList.txt")
whitespace =
prop_path
|> File.read!()
|> String.split(["\r\n", "\n"])
|> Enum.reduce([], fn line, acc ->
case :binary.split(line, ";") do
[<<first::4-bytes, "..", last::4-bytes, _::binary>>, <<" White_Space", _::binary>>] ->
first = String.to_integer(first, 16)
last = String.to_integer(last, 16)
Enum.map(first..last, fn int -> <<int::utf8>> end) ++ acc
[<<single::4-bytes, _::binary>>, <<" White_Space", _::binary>>] ->
[<<String.to_integer(single, 16)::utf8>> | acc]
_ ->
acc
end
end)
IO.puts(:stderr, "[Unicode] Break on #{length(whitespace)} whitespace codepoints")
# trim_leading
def trim_leading(string) when is_binary(string) do
do_trim_leading(string)
end
for codepoint <- whitespace do
def do_trim_leading(<<unquote(codepoint), rest::bits>>), do: do_trim_leading(rest)
end
def do_trim_leading(<<rest::bits>>), do: rest
# trim_trailing
for cp <- whitespace do
# We need to increment @whitespace_max_size as well
# as the small table (_s) if we add a new entry here.
case byte_size(cp) do
3 ->
defp do_trim_trailing_l(unquote(cp)), do: -3
2 ->
defp do_trim_trailing_l(<<_, unquote(cp)>>), do: -2
defp do_trim_trailing_s(unquote(cp)), do: <<>>
1 ->
defp do_trim_trailing_l(<<unquote(cp), unquote(cp), unquote(cp)>>), do: -3
defp do_trim_trailing_l(<<_, unquote(cp), unquote(cp)>>), do: -2
defp do_trim_trailing_l(<<_, _, unquote(cp)>>), do: -1
defp do_trim_trailing_s(<<x, unquote(cp)>>), do: do_trim_trailing_s(<<x>>)
defp do_trim_trailing_s(unquote(cp)), do: <<>>
end
end
defp do_trim_trailing_l(_), do: 0
defp do_trim_trailing_s(o), do: o
def trim_trailing(string) when is_binary(string) do
trim_trailing(string, byte_size(string))
end
defp trim_trailing(string, size) when size < @whitespace_max_size do
do_trim_trailing_s(string)
end
defp trim_trailing(string, size) do
trail = binary_part(string, size, -@whitespace_max_size)
case do_trim_trailing_l(trail) do
0 -> string
x -> trim_trailing(binary_part(string, 0, size + x), size + x)
end
end
# Split
def split(string) do
:binary.split(string, unquote(whitespace -- non_breakable), [:global, :trim_all])
end
# Decompose
def decompose(entries, map) do
for entry <- entries do
case map do
%{^entry => match} -> decompose(match, map)
%{} -> <<entry::utf8>>
end
end
end
end
|