Debian Codegraph @

uc_spec/gen_unicode_mod.escript

raw
1 #!/usr/bin/env escript
2 %% -*- erlang -*-
3 %%! +A0
4 %%
5 %% %CopyrightBegin%
6 %%
7 %% Copyright Ericsson AB 2017-2019. All Rights Reserved.
8 %%
9 %% Licensed under the Apache License, Version 2.0 (the "License");
10 %% you may not use this file except in compliance with the License.
11 %% You may obtain a copy of the License at
12 %%
13 %% http://www.apache.org/licenses/LICENSE-2.0
14 %%
15 %% Unless required by applicable law or agreed to in writing, software
16 %% distributed under the License is distributed on an "AS IS" BASIS,
17 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 %% See the License for the specific language governing permissions and
19 %% limitations under the License.
20 %%
21 %% %CopyrightEnd%
22 %%
23
24 -mode(compile).
25
26 -record(cp, {name, class, dec, comp, cs}).
27 -define(MOD, "unicode_util_compat").
28
29 main(_) ->
30 %% Parse main table
31 {ok, UD} = file:open("../uc_spec/UnicodeData.txt", [read, raw, {read_ahead, 1000000}]),
32 Data0 = foldl(fun parse_unicode_data/2, [], UD),
33 Data1 = array:from_orddict(lists:reverse(Data0)),
34 ok = file:close(UD),
35
36 %% Special Casing table
37 {ok, SC} = file:open("../uc_spec/SpecialCasing.txt", [read, raw, {read_ahead, 1000000}]),
38 Data2 = foldl(fun parse_special_casing/2, Data1, SC),
39 ok = file:close(SC),
40 %% Casing Folding table
41 {ok, CF} = file:open("../uc_spec/CaseFolding.txt", [read, raw, {read_ahead, 1000000}]),
42 Data = foldl(fun parse_case_folding/2, Data2, CF),
43 ok = file:close(CF),
44
45 %% Normalization
46 {ok, ExclF} = file:open("../uc_spec/CompositionExclusions.txt", [read, raw, {read_ahead, 1000000}]),
47 ExclData = foldl(fun parse_comp_excl/2, Data, ExclF),
48 ok = file:close(ExclF),
49
50 %% GraphemeBreakProperty table
51 {ok, Emoji} = file:open("../uc_spec/emoji-data.txt", [read, raw, {read_ahead, 1000000}]),
52 Props00 = foldl(fun parse_properties/2, [], Emoji),
53 %% Filter Extended_Pictographic class which we are interested in.
54 Props0 = [EP || {extended_pictographic, _} = EP <- Props00],
55 ok = file:close(Emoji),
56 {ok, GBPF} = file:open("../uc_spec/GraphemeBreakProperty.txt", [read, raw, {read_ahead, 1000000}]),
57 Props1 = foldl(fun parse_properties/2, Props0, GBPF),
58 ok = file:close(GBPF),
59 {ok, PropF} = file:open("../uc_spec/PropList.txt", [read, raw, {read_ahead, 1000000}]),
60 Props2 = foldl(fun parse_properties/2, Props1, PropF),
61 ok = file:close(PropF),
62 Props = sofs:to_external(sofs:relation_to_family(sofs:relation(Props2))),
63
64 %% Make module
65 OutputPath = filename:join(["..", "src", ?MOD++".erl"]),
66 {ok, Out} = file:open(OutputPath, [write]),
67 gen_file(Out, Data, ExclData, maps:from_list(Props)),
68 ok = file:close(Out),
69 ok.
70
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72
73 -ifdef('OTP_RELEASE').
74 str_chomp(Str) -> string:chomp(Str).
75 str_lexemes(Str, Sep) -> string:lexemes(Str, Sep).
76 str_trim(Str, Dir) -> string:trim(Str, Dir).
77 -else.
78 strp_chomp(Str) -> string:strip(Str, right, $\n).
79 str_lexemes(Str, Sep) -> string:tokens(Str, Sep).
80 str_trim(Str, Dir) -> string:strip(Str, Dir).
81 -endif.
82
83 parse_unicode_data(Line0, Acc) ->
84 Line = str_chomp(Line0),
85 [CodePoint,Name,_Cat,Class,_BiDi,Decomp,
86 _N1,_N2,_N3,_BDMirror,_Uni1,_Iso|Case] = tokens(Line, ";"),
87 {Dec,Comp} = case to_decomp(Decomp) of
88 {_, _} = Compabil -> {[], Compabil};
89 Canon -> {Canon, []}
90 end,
91 [{hex_to_int(CodePoint),
92 #cp{name=list_to_binary(Name),class=to_class(Class),
93 dec=Dec, comp=Comp, cs=to_case(Case)}}
94 |Acc].
95
96 to_class(String) ->
97 list_to_integer(str_trim(String, both)).
98
99 to_decomp("") -> [];
100 to_decomp("<" ++ Str) ->
101 [Tag,Rest] =str_lexemes(Str, ">"),
102 {list_to_atom(Tag), to_decomp(Rest)};
103 to_decomp(CodePoints) ->
104 CPL =str_lexemes(CodePoints, " "),
105 [hex_to_int(CP) || CP <- CPL].
106
107 to_case(["","",""]) -> [];
108 to_case([Upper,Lower,Title]) ->
109 {hex_to_int(Upper),hex_to_int(Lower),hex_to_int(Title),[]}.
110
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112
113 parse_special_casing("03A3;" ++ _, Acc) ->
114 %% Break for conditional handling, which we don't support
115 {done, Acc};
116 parse_special_casing(Line, Table) ->
117 [CodePoint|CaseStr] = tokens(Line, ";"),
118 CP = hex_to_int(CodePoint),
119 Entry = array:get(CP, Table),
120 Case = to_scase(CaseStr),
121 array:set(CP, Entry#cp{cs=Case}, Table).
122
123 to_scase([Lower,Title,Upper|_]) ->
124 {unlist([hex_to_int(CP) || CP <-str_lexemes(Upper, " ")]),
125 unlist([hex_to_int(CP) || CP <-str_lexemes(Lower, " ")]),
126 unlist([hex_to_int(CP) || CP <-str_lexemes(Title, " ")]),
127 []}.
128
129 parse_case_folding(Line, Table) ->
130 [CodePoint, Class0, CaseStr |_Comments] = tokens(Line, ";"),
131 Class = str_trim(Class0, both),
132 if Class =:= "T" -> Table; %% Do not support localization yet
133 Class =:= "S" -> Table; %% Ignore simple
134 true ->
135 CP = hex_to_int(CodePoint),
136 Case = unlist([hex_to_int(CPC) ||
137 CPC <-str_lexemes(CaseStr, " ")]),
138 #cp{cs={U,L,T,_}} = Entry = array:get(CP, Table),
139 array:set(CP, Entry#cp{cs={U,L,T,Case}}, Table)
140 end.
141
142 unlist([A]) -> A;
143 unlist(A) -> A.
144
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 parse_comp_excl(Line, Table) ->
147 [CodePoint|_Comments] = tokens(Line, "#"),
148 CP = hex_to_int(CodePoint),
149 Entry = array:get(CP, Table),
150 array:set(CP, Entry#cp{dec=none}, Table).
151
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153
154 parse_properties(Line0, Acc) ->
155 [Line|_Comments] = tokens(Line0, "#"),
156 [CodePoints, Class] = tokens(Line, ";"),
157 case tokens(CodePoints, ".") of
158 [CodePoint] ->
159 [{to_atom(Class), {hex_to_int(CodePoint), undefined}}|Acc];
160 [CodePoint1,"",CodePoint2] ->
161 [{to_atom(Class), {hex_to_int(CodePoint1), hex_to_int(CodePoint2)}}|Acc]
162 end.
163
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165
166 gen_file(Fd, Data, ExclData, Props) ->
167 gen_header(Fd),
168 gen_static(Fd),
169 gen_norm(Fd),
170 gen_ws(Fd, Props),
171 gen_cp(Fd),
172 gen_gc(Fd, Props),
173 gen_compose_pairs(Fd, ExclData, Data),
174 gen_case_table(Fd, Data),
175 gen_unicode_table(Fd, Data),
176 gen_trailer(Fd),
177 ok.
178
179 gen_header(Fd) ->
180 io:put_chars(Fd, "%%\n%% this file is generated do not modify\n"),
181 io:put_chars(Fd, "%% see ../uc_spec/gen_unicode_mod.escript\n\n"),
182 io:put_chars(Fd, "-module(" ++ ?MOD ++").\n"),
183 io:put_chars(Fd, "-export([cp/1, gc/1]).\n"),
184 io:put_chars(Fd, "-export([nfd/1, nfc/1, nfkd/1, nfkc/1]).\n"),
185 io:put_chars(Fd, "-export([whitespace/0, is_whitespace/1]).\n"),
186 io:put_chars(Fd, "-export([uppercase/1, lowercase/1, titlecase/1, casefold/1]).\n\n"),
187 io:put_chars(Fd, "-export([spec_version/0, lookup/1, get_case/1]).\n"),
188 io:put_chars(Fd, "-ifdef(OTP20).\n"),
189 io:put_chars(Fd, "cp(Argument) -> unicode_util:cp(Argument).\n"),
190 io:put_chars(Fd, "gc(Argument) -> unicode_util:gc(Argument).\n"),
191 io:put_chars(Fd, "nfd(Argument) -> unicode_util:nfd(Argument).\n"),
192 io:put_chars(Fd, "nfc(Argument) -> unicode_util:nfc(Argument).\n"),
193 io:put_chars(Fd, "nfkd(Argument) -> unicode_util:nfkd(Argument).\n"),
194 io:put_chars(Fd, "nfkc(Argument) -> unicode_util:nfkc(Argument).\n"),
195 io:put_chars(Fd, "whitespace() -> unicode_util:whitespace().\n"),
196 io:put_chars(Fd, "is_whitespace(Argument) -> unicode_util:is_whitespace(Argument).\n"),
197 io:put_chars(Fd, "uppercase(Argument) -> unicode_util:uppercase(Argument).\n"),
198 io:put_chars(Fd, "lowercase(Argument) -> unicode_util:lowercase(Argument).\n"),
199 io:put_chars(Fd, "titlecase(Argument) -> unicode_util:titlecase(Argument).\n"),
200 io:put_chars(Fd, "casefold(Argument) -> unicode_util:casefold(Argument).\n"),
201 io:put_chars(Fd, "spec_version() -> unicode_util:spec_version().\n"),
202 io:put_chars(Fd, "lookup(Argument) -> unicode_util:lookup(Argument).\n"),
203 io:put_chars(Fd, "get_case(Argument) -> unicode_util:get_case(Argument).\n"),
204 io:put_chars(Fd, "-else.\n"),
205 io:put_chars(Fd, "-inline([class/1]).\n"),
206 io:put_chars(Fd, "-compile(nowarn_unused_vars).\n"),
207 io:put_chars(Fd, "-dialyzer({no_improper_lists, [cp/1, gc/1, gc_prepend/2]}).\n"),
208 io:put_chars(Fd, "-type gc() :: char()|[char()].\n\n\n"),
209 ok.
210
211 gen_trailer(Fd) ->
212 io:put_chars(Fd, "-endif.\n"),
213 ok.
214
215 gen_static(Fd) ->
216 io:put_chars(Fd, "-spec lookup(char()) -> map().\n"),
217 io:put_chars(Fd, "lookup(Codepoint) ->\n"
218 " {CCC,Can,Comp} = unicode_table(Codepoint),\n"
219 " #{ccc=>CCC, canon=>Can, compat=>Comp}.\n\n"),
220 io:put_chars(Fd, "-spec get_case(char()) -> map().\n"),
221 io:put_chars(Fd, "get_case(Codepoint) ->\n"
222 " case case_table(Codepoint) of\n"
223 " {U,L} -> #{upper=>U,lower=>L,title=>U,fold=>L};\n"
224 " {U,L,T,F} -> #{upper=>U,lower=>L,title=>T,fold=>F}\n"
225 " end.\n\n"),
226 io:put_chars(Fd, "spec_version() -> {13,0}.\n\n\n"),
227 io:put_chars(Fd, "class(Codepoint) -> {CCC,_,_} = unicode_table(Codepoint),\n CCC.\n\n"),
228 io:put_chars(Fd, "-spec uppercase(unicode:chardata()) -> "
229 "maybe_improper_list(gc(),unicode:chardata()).\n"),
230 io:put_chars(Fd, "uppercase(Str0) ->\n"),
231 io:put_chars(Fd, " case cp(Str0) of\n"),
232 io:put_chars(Fd, " [CP|Str] = Str1 ->\n"),
233 io:put_chars(Fd, " case case_table(CP) of\n"),
234 io:put_chars(Fd, " {Upper,_} -> [Upper|Str];\n"),
235 io:put_chars(Fd, " {Upper,_,_,_} -> [Upper|Str]\n"),
236 io:put_chars(Fd, " end;\n"),
237 io:put_chars(Fd, " [] -> [];\n"),
238 io:put_chars(Fd, " {error,Err} -> error({badarg, Err})\n"),
239 io:put_chars(Fd, " end.\n\n"),
240 io:put_chars(Fd, "-spec lowercase(unicode:chardata()) -> "
241 "maybe_improper_list(gc(),unicode:chardata()).\n"),
242 io:put_chars(Fd, "lowercase(Str0) ->\n"),
243 io:put_chars(Fd, " case cp(Str0) of\n"),
244 io:put_chars(Fd, " [CP|Str] = Str1 ->\n"),
245 io:put_chars(Fd, " case case_table(CP) of\n"),
246 io:put_chars(Fd, " {_,Lower} -> [Lower|Str];\n"),
247 io:put_chars(Fd, " {_,Lower,_,_} -> [Lower|Str]\n"),
248 io:put_chars(Fd, " end;\n"),
249 io:put_chars(Fd, " [] -> [];\n"),
250 io:put_chars(Fd, " {error,Err} -> error({badarg, Err})\n"),
251 io:put_chars(Fd, " end.\n\n"),
252 io:put_chars(Fd, "-spec titlecase(unicode:chardata()) -> "
253 "maybe_improper_list(gc(),unicode:chardata()).\n"),
254 io:put_chars(Fd, "titlecase(Str0) ->\n"),
255 io:put_chars(Fd, " case cp(Str0) of\n"),
256 io:put_chars(Fd, " [CP|Str] = Str1 ->\n"),
257 io:put_chars(Fd, " case case_table(CP) of\n"),
258 io:put_chars(Fd, " {_,_,Title,_} -> [Title|Str];\n"),
259 io:put_chars(Fd, " {Upper,_} -> [Upper|Str]\n"),
260 io:put_chars(Fd, " end;\n"),
261 io:put_chars(Fd, " [] -> [];\n"),
262 io:put_chars(Fd, " {error,Err} -> error({badarg, Err})\n"),
263 io:put_chars(Fd, " end.\n\n"),
264 io:put_chars(Fd, "-spec casefold(unicode:chardata()) -> "
265 "maybe_improper_list(gc(),unicode:chardata()).\n"),
266 io:put_chars(Fd, "casefold(Str0) ->\n"),
267 io:put_chars(Fd, " case cp(Str0) of\n"),
268 io:put_chars(Fd, " [CP|Str] = Str1 ->\n"),
269 io:put_chars(Fd, " case case_table(CP) of\n"),
270 io:put_chars(Fd, " {_,_,_,Fold} -> [Fold|Str];\n"),
271 io:put_chars(Fd, " {_,Lower} -> [Lower|Str]\n"),
272 io:put_chars(Fd, " end;\n"),
273 io:put_chars(Fd, " [] -> [];\n"),
274 io:put_chars(Fd, " {error,Err} -> error({badarg, Err})\n"),
275 io:put_chars(Fd, " end.\n\n"),
276
277 ok.
278
279 gen_norm(Fd) ->
280 io:put_chars(Fd,
281 "-spec nfd(unicode:chardata()) -> maybe_improper_list(gc(),unicode:chardata()) | {error, unicode:chardata()}.\n"
282 "nfd(Str0) ->\n"
283 " case gc(Str0) of\n"
284 " [GC|R] when GC < 128 -> [GC|R];\n"
285 " [GC|Str] -> [decompose(GC)|Str];\n"
286 " [] -> [];\n"
287 " {error,_}=Error -> Error\n end.\n\n"
288 ),
289
290 io:put_chars(Fd,
291 "-spec nfkd(unicode:chardata()) -> maybe_improper_list(gc(),unicode:chardata()) | {error, unicode:chardata()}.\n"
292 "nfkd(Str0) ->\n"
293 " case gc(Str0) of\n"
294 " [GC|R] when GC < 128 -> [GC|R];\n"
295 " [GC|Str] -> [decompose_compat(GC)|Str];\n"
296 " [] -> [];\n"
297 " {error,_}=Error -> Error\n end.\n\n"
298 ),
299
300 io:put_chars(Fd,
301 "-spec nfc(unicode:chardata()) -> maybe_improper_list(gc(),unicode:chardata()) | {error, unicode:chardata()}.\n"
302 "nfc(Str0) ->\n"
303 " case gc(Str0) of\n"
304 " [GC|R] when GC < 256 -> [GC|R];\n"
305 " [GC|Str] -> [compose(decompose(GC))|Str];\n"
306 " [] -> [];\n"
307 " {error,_}=Error -> Error\n end.\n\n"
308 ),
309
310 io:put_chars(Fd,
311 "-spec nfkc(unicode:chardata()) -> maybe_improper_list(gc(),unicode:chardata()) | {error, unicode:chardata()}.\n"
312 "nfkc(Str0) ->\n"
313 " case gc(Str0) of\n"
314 " [GC|R] when GC < 128 -> [GC|R];\n"
315 " [GC|Str] -> [compose_compat_0(decompose_compat(GC))|Str];\n"
316 " [] -> [];\n"
317 " {error,_}=Error -> Error\n end.\n\n"
318 ),
319
320 io:put_chars(Fd,
321 "decompose(CP) when is_integer(CP), CP < 16#AC00, 16#D7A3 > CP ->\n"
322 " case unicode_table(CP) of\n"
323 " {_,[],_} -> CP;\n"
324 " {_,CPs,_} -> canonical_order(CPs)\n"
325 " end;\n"
326 "decompose(CP) ->\n"
327 " canonical_order(decompose_1(CP)).\n"
328 "\n"
329 "decompose_1(CP) when 16#AC00 =< CP, CP =< 16#D7A3 ->\n"
330 " Syll = CP-16#AC00,\n"
331 " T = 28,\n"
332 " N = 588,\n"
333 " Lead = 16#1100 + Syll div N,\n"
334 " Vowel = 16#1161 + (Syll rem N) div T,\n"
335 " case Syll rem T of\n"
336 " 0 -> [{0,Lead},{0,Vowel}];\n"
337 " Trail -> [{0,Lead}, {0,Vowel}, {0,Trail+16#11A7}]\n"
338 " end;\n"
339 "decompose_1(CP) when is_integer(CP) ->\n"
340 " case unicode_table(CP) of\n"
341 " {CCC, [],_} -> [{CCC,CP}];\n"
342 " {_, CPs, _} -> CPs\n"
343 " end;\n"
344 "decompose_1([CP|CPs]) ->\n"
345 " decompose_1(CP) ++ decompose_1(CPs);\n"
346 "decompose_1([]) -> [].\n"
347 "\n"
348 "canonical_order([{_,CP}]) -> CP;\n"
349 "canonical_order(CPs) ->\n"
350 " canonical_order_1(CPs).\n"
351 "\n"
352 "canonical_order_1([{0,CP}|TaggedCPs]) ->\n"
353 " [CP|canonical_order_1(TaggedCPs)];\n"
354 "canonical_order_1([_|_]=TaggedCPs) ->\n"
355 " canonical_order_2(TaggedCPs, []);\n"
356 "canonical_order_1([]) -> [].\n"
357 "\n"
358 "canonical_order_2([{CCC,_}=First|Cont], Seq) when CCC > 0 ->\n"
359 " canonical_order_2(Cont, [First|Seq]);\n"
360 "canonical_order_2(Cont, Seq) ->\n"
361 " [CP || {_, CP} <- lists:keysort(1,lists:reverse(Seq))] ++ canonical_order_1(Cont).\n\n"),
362
363 io:put_chars(Fd,
364 "decompose_compat(CP) when is_integer(CP), CP < 16#AC00, 16#D7A3 > CP ->\n"
365 " case unicode_table(CP) of\n"
366 " {_, [], []} -> CP;\n"
367 " {_, _, {_,CPs}} -> canonical_order(CPs);\n"
368 " {_, CPs, _} -> canonical_order(CPs)\n"
369 " end;\n"
370 "decompose_compat(CP) ->\n"
371 " canonical_order(decompose_compat_1(CP)).\n"
372 "\n"
373 "decompose_compat_1(CP) when 16#AC00 =< CP, CP =< 16#D7A3 ->\n"
374 " Syll = CP-16#AC00,\n"
375 " T = 28,\n"
376 " N = 588,\n"
377 " Lead = 16#1100 + Syll div N,\n"
378 " Vowel = 16#1161 + (Syll rem N) div T,\n"
379 " case Syll rem T of\n"
380 " 0 -> [{0,Lead},{0,Vowel}];\n"
381 " Trail -> [{0,Lead}, {0,Vowel}, {0,Trail+16#11A7}]\n"
382 " end;\n"
383 "decompose_compat_1(CP) when is_integer(CP) ->\n"
384 " case unicode_table(CP) of\n"
385 " {CCC, [], []} -> [{CCC,CP}];\n"
386 " {_, _, {_,CPs}} -> CPs;\n"
387 " {_, CPs, _} -> CPs\n"
388 " end;\n"
389 "decompose_compat_1([CP|CPs]) ->\n"
390 " decompose_compat_1(CP) ++ decompose_compat_1(CPs);\n"
391 "decompose_compat_1([]) -> [].\n"),
392
393
394 io:put_chars(Fd,
395 "compose(CP) when is_integer(CP) -> CP;\n"
396 "compose([Lead,Vowel|Trail]) %% Hangul\n"
397 " when 16#1100 =< Lead, Lead =< 16#1112 ->\n"
398 " if 16#1161 =< Vowel, Vowel =< 16#1175 ->\n"
399 " CP = 16#AC00 + ((Lead - 16#1100) * 588) + ((Vowel - 16#1161) * 28),\n"
400 " case Trail of\n"
401 " [T|Acc] when 16#11A7 =< T, T =< 16#11C2 -> nolist(CP+T-16#11A7,Acc);\n"
402 " Acc -> nolist(CP,Acc)\n"
403 " end;\n"
404 " true ->\n"
405 " case compose([Vowel|Trail]) of\n"
406 " [_|_] = CPs -> [Lead|CPs];\n"
407 " CP -> [Lead,CP]\n"
408 " end\n"
409 " end;\n"
410 "compose([Base,Accent]=GC0) ->\n"
411 " case compose_pair(Base,Accent) of\n"
412 " false -> GC0;\n"
413 " GC -> GC\n"
414 " end;\n"
415 "compose([CP|Many]) ->\n"
416 " compose_many(Many, CP, [], class(CP)).\n"
417 "\n"
418 "compose_many([CP|Rest], Base, Accents, Prev) ->\n"
419 " Class = class(CP),\n"
420 " case (Prev =:= 0 orelse Prev < Class) andalso compose_pair(Base, CP) of\n"
421 " false -> compose_many(Rest, Base, [CP|Accents], Class);\n"
422 " Combined -> compose_many(Rest, Combined, Accents, Prev)\n"
423 " end;\n"
424 "compose_many([], Base, [], Prev) ->\n"
425 " Base;\n"
426 "compose_many([], Base, Accents, Prev) ->\n"
427 " [Base|lists:reverse(Accents)].\n"
428 "\n\n"),
429 io:put_chars(Fd,
430 "compose_compat_0(CP) when is_integer(CP) ->\n"
431 " CP;\n"
432 "compose_compat_0(L) ->\n"
433 " case gc(L) of\n"
434 " [First|Rest] ->\n"
435 " case compose_compat(First) of\n"
436 " [_|_] = GC -> GC ++ compose_compat_0(Rest);\n"
437 " CP -> [CP|compose_compat_0(Rest)]\n"
438 " end;\n"
439 " [] -> []\n"
440 " end.\n\n"
441 "compose_compat(CP) when is_integer(CP) -> CP;\n"
442 "compose_compat([Lead,Vowel|Trail]) %% Hangul\n"
443 " when 16#1100 =< Lead, Lead =< 16#1112 ->\n"
444 " if 16#1161 =< Vowel, Vowel =< 16#1175 ->\n"
445 " CP = 16#AC00 + ((Lead - 16#1100) * 588) + ((Vowel - 16#1161) * 28),\n"
446 " case Trail of\n"
447 " [T|Acc] when 16#11A7 =< T, T =< 16#11C2 -> nolist(CP+T-16#11A7,Acc);\n"
448 " Acc -> nolist(CP,Acc)\n"
449 " end;\n"
450 " true ->\n"
451 " case compose_compat([Vowel|Trail]) of\n"
452 " [_|_] = CPs -> [Lead|CPs];\n"
453 " CP -> [Lead,CP]\n"
454 " end\n"
455 " end;\n"
456 "compose_compat([Base,Accent]=GC0) ->\n"
457 " case compose_pair(Base,Accent) of\n"
458 " false -> GC0;\n"
459 " GC -> GC\n"
460 " end;\n"
461 "compose_compat([CP|Many]) ->\n"
462 " compose_compat_many(Many, CP, [], class(CP)).\n"
463 "\n"
464 "compose_compat_many([CP|Rest], Base, Accents, Prev) ->\n"
465 " Class = class(CP),\n"
466 " case (Prev =:= 0 orelse Prev < Class) andalso compose_pair(Base, CP) of\n"
467 " false -> compose_compat_many(Rest, Base, [CP|Accents], Class);\n"
468 " Combined -> compose_compat_many(Rest, Combined, Accents, Prev)\n"
469 " end;\n"
470 "compose_compat_many([], Base, [], Prev) ->\n"
471 " Base;\n"
472 "compose_compat_many([], Base, Accents, Prev) ->\n"
473 " [Base|lists:reverse(Accents)].\n"
474 "\n\n"),
475
476 ok.
477
478 gen_ws(Fd, Props) ->
479 WS0 = maps:get(pattern_white_space, Props),
480 WS = merge_ranges(WS0, split),
481 io:put_chars(Fd, "%% Useful non-breakable whitespace chars\n"
482 "%% defined as Pattern White Space in Unicode Standard Annex #31\n"),
483 io:put_chars(Fd, "-spec whitespace() -> [gc()].\n"),
484 WsChars = [CP || {CP, undefined} <- WS],
485 io:format(Fd, "whitespace() -> ~w.\n\n", [[[$\r,$\n]|WsChars]]),
486
487 io:put_chars(Fd, "-spec is_whitespace(gc()) -> boolean().\n"),
488 IsWS = fun(Range) -> io:format(Fd, "is_whitespace~s true;\n", [gen_single_clause(Range)]) end,
489 io:format(Fd, "is_whitespace([13,10]) -> true;\n", []),
490 [IsWS(CP) || CP <- WS],
491 io:put_chars(Fd, "is_whitespace(_) -> false.\n\n"),
492 ok.
493
494 gen_cp(Fd) ->
495 io:put_chars(Fd, "-spec cp(String::unicode:chardata()) ->"
496 " maybe_improper_list() | {error, unicode:chardata()}.\n"),
497 io:put_chars(Fd, "cp([C|_]=L) when is_integer(C) -> L;\n"),
498 io:put_chars(Fd, "cp([List]) -> cp(List);\n"),
499 io:put_chars(Fd, "cp([List|R]) -> cpl(List, R);\n"),
500 io:put_chars(Fd, "cp([]) -> [];\n"),
501 io:put_chars(Fd, "cp(<<C/utf8, R/binary>>) -> [C|R];\n"),
502 io:put_chars(Fd, "cp(<<>>) -> [];\n"),
503 io:put_chars(Fd, "cp(<<R/binary>>) -> {error,R}.\n"),
504 io:put_chars(Fd, "\n"),
505 io:put_chars(Fd, "cpl([C], R) when is_integer(C) -> [C|cpl_1_cont(R)];\n"),
506 io:put_chars(Fd, "cpl([C|T], R) when is_integer(C) -> [C|cpl_cont(T, R)];\n"),
507 io:put_chars(Fd, "cpl([List], R) -> cpl(List, R);\n"),
508 io:put_chars(Fd, "cpl([List|T], R) -> cpl(List, [T|R]);\n"),
509 io:put_chars(Fd, "cpl([], R) -> cp(R);\n"),
510 io:put_chars(Fd, "cpl(<<C/utf8, T/binary>>, R) -> [C,T|R];\n"),
511 io:put_chars(Fd, "cpl(<<>>, R) -> cp(R);\n"),
512 io:put_chars(Fd, "cpl(<<B/binary>>, R) -> {error,[B|R]}.\n"),
513 io:put_chars(Fd, "\n"),
514 io:put_chars(Fd, "%%%\n"),
515 io:put_chars(Fd, "\n"),
516 io:put_chars(Fd, "cpl_cont([C|T], R) when is_integer(C) -> [C|cpl_cont2(T, R)];\n"),
517 io:put_chars(Fd, "cpl_cont([L], R) -> cpl_cont(L, R);\n"),
518 io:put_chars(Fd, "cpl_cont([L|T], R) -> cpl_cont(L, [T|R]);\n"),
519 io:put_chars(Fd, "cpl_cont([], R) -> cpl_1_cont(R);\n"),
520 io:put_chars(Fd, "cpl_cont(T, R) -> [T|R].\n"),
521 io:put_chars(Fd, "\n"),
522 io:put_chars(Fd, "cpl_cont2([C|T], R) when is_integer(C) -> [C|cpl_cont3(T, R)];\n"),
523 io:put_chars(Fd, "cpl_cont2([L], R) -> cpl_cont2(L, R);\n"),
524 io:put_chars(Fd, "cpl_cont2([L|T], R) -> cpl_cont2(L, [T|R]);\n"),
525 io:put_chars(Fd, "cpl_cont2([], R) -> cpl_1_cont2(R);\n"),
526 io:put_chars(Fd, "cpl_cont2(T, R) -> [T|R].\n"),
527 io:put_chars(Fd, "\n"),
528 io:put_chars(Fd, "cpl_cont3([C], R) when is_integer(C) -> [C|R];\n"),
529 io:put_chars(Fd, "cpl_cont3([C|T], R) when is_integer(C) -> [C,T|R];\n"),
530 io:put_chars(Fd, "cpl_cont3([L], R) -> cpl_cont3(L, R);\n"),
531 io:put_chars(Fd, "cpl_cont3([L|T], R) -> cpl_cont3(L, [T|R]);\n"),
532 io:put_chars(Fd, "cpl_cont3([], R) -> cpl_1_cont3(R);\n"),
533 io:put_chars(Fd, "cpl_cont3(T, R) -> [T|R].\n"),
534 io:put_chars(Fd, "\n"),
535 io:put_chars(Fd, "%%%\n"),
536 io:put_chars(Fd, "\n"),
537 io:put_chars(Fd, "cpl_1_cont([C|T]) when is_integer(C) -> [C|cpl_1_cont2(T)];\n"),
538 io:put_chars(Fd, "cpl_1_cont([L]) -> cpl_1_cont(L);\n"),
539 io:put_chars(Fd, "cpl_1_cont([L|T]) -> cpl_cont(L, T);\n"),
540 io:put_chars(Fd, "cpl_1_cont(T) -> T.\n"),
541 io:put_chars(Fd, "\n"),
542 io:put_chars(Fd, "cpl_1_cont2([C|T]) when is_integer(C) -> [C|cpl_1_cont3(T)];\n"),
543 io:put_chars(Fd, "cpl_1_cont2([L]) -> cpl_1_cont2(L);\n"),
544 io:put_chars(Fd, "cpl_1_cont2([L|T]) -> cpl_cont2(L, T);\n"),
545 io:put_chars(Fd, "cpl_1_cont2(T) -> T.\n"),
546 io:put_chars(Fd, "\n"),
547 io:put_chars(Fd, "cpl_1_cont3([C|_]=T) when is_integer(C) -> T;\n"),
548 io:put_chars(Fd, "cpl_1_cont3([L]) -> cpl_1_cont3(L);\n"),
549 io:put_chars(Fd, "cpl_1_cont3([L|T]) -> cpl_cont3(L, T);\n"),
550 io:put_chars(Fd, "cpl_1_cont3(T) -> T.\n"),
551 io:put_chars(Fd, "\n"),
552 io:put_chars(Fd, "%%%\n"),
553 io:put_chars(Fd, "\n"),
554 io:put_chars(Fd, "cp_no_bin([C|_]=L) when is_integer(C) -> L;\n"),
555 io:put_chars(Fd, "cp_no_bin([List]) -> cp_no_bin(List);\n"),
556 io:put_chars(Fd, "cp_no_bin([List|R]) -> cp_no_binl(List, R);\n"),
557 io:put_chars(Fd, "cp_no_bin([]) -> [];\n"),
558 io:put_chars(Fd, "cp_no_bin(_) -> binary_found.\n"),
559 io:put_chars(Fd, "\n"),
560 io:put_chars(Fd, "cp_no_binl([C], R) when is_integer(C) -> [C|cpl_1_cont(R)];\n"),
561 io:put_chars(Fd, "cp_no_binl([C|T], R) when is_integer(C) -> [C|cpl_cont(T, R)];\n"),
562 io:put_chars(Fd, "cp_no_binl([List], R) -> cp_no_binl(List, R);\n"),
563 io:put_chars(Fd, "cp_no_binl([List|T], R) -> cp_no_binl(List, [T|R]);\n"),
564 io:put_chars(Fd, "cp_no_binl([], R) -> cp_no_bin(R);\n"),
565 io:put_chars(Fd, "cp_no_binl(_, _) -> binary_found.\n\n"),
566 ok.
567
568 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
569
570 gen_gc(Fd, GBP) ->
571 %% see http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundary_Rules
572 io:put_chars(Fd,
573 "-spec gc(String::unicode:chardata()) ->"
574 " maybe_improper_list() | {error, unicode:chardata()}.\n"),
575 io:put_chars(Fd,
576 "gc([]=R) -> R;\n"
577 "gc([CP]=R) when is_integer(CP) -> R;\n"
578 "gc([$\\r=CP|R0]) ->\n"
579 " case cp(R0) of % Don't break CRLF\n"
580 " [$\\n|R1] -> [[$\\r,$\\n]|R1];\n"
581 " T -> [CP|T]\n"
582 " end;\n"
583 "gc([CP1|T1]=T) when CP1 < 256 ->\n"
584 " case T1 of\n"
585 " [CP2|_] when CP2 < 256 -> T; %% Ascii Fast path\n"
586 " _ -> %% Keep the tail binary.\n"
587 " case cp_no_bin(T1) of\n"
588 " [CP2|_]=T3 when CP2 < 256 -> [CP1|T3]; %% Asciii Fast path\n"
589 " binary_found -> gc_1(T);\n"
590 " T4 -> gc_1([CP1|T4])\n"
591 " end\n"
592 " end;\n"
593 "gc(<<>>) -> [];\n"
594 "gc(<<CP1/utf8, Rest/binary>>) ->\n"
595 " if CP1 < 256, CP1 =/= $\\r ->\n"
596 " case Rest of\n"
597 " <<CP2/utf8, _/binary>> when CP2 < 256 -> %% Ascii Fast path\n"
598 " [CP1|Rest];\n"
599 " _ -> gc_1([CP1|Rest])\n"
600 " end;\n"
601 " true -> gc_1([CP1|Rest])\n"
602 " end;\n"
603 "gc([CP|_]=T) when is_integer(CP) -> gc_1(T);\n"
604 "gc(Str) ->\n"
605 " case cp(Str) of\n"
606 " {error,_}=Error -> Error;\n"
607 " CPs -> gc(CPs)\n"
608 " end.\n"
609 ),
610
611 GenExtP = fun(Range) -> io:format(Fd, "gc_1~s gc_ext_pict(R1,[CP]);\n", [gen_clause(Range)]) end,
612 ExtendedPictographic0 = merge_ranges(maps:get(extended_pictographic,GBP)),
613 %% Pick codepoints below 256 (some data knowledge here)
614 {ExtendedPictographicLow,ExtendedPictographicHigh} =
615 lists:splitwith(fun({Start,undefined}) -> Start < 256 end,ExtendedPictographic0),
616 io:put_chars(Fd,
617 "\ngc_1([$\\r|R0] = R) ->\n"
618 " case cp(R0) of % Don't break CRLF\n"
619 " [$\\n|R1] -> [[$\\r,$\\n]|R1];\n"
620 " _ -> R\n"
621 " end;\n"),
622 io:put_chars(Fd, "\n%% Handle control\n"),
623 GenControl = fun(Range) -> io:format(Fd, "gc_1~s R0;\n", [gen_clause(Range)]) end,
624 CRs0 = merge_ranges(maps:get(cr, GBP) ++ maps:get(lf, GBP) ++ maps:get(control, GBP), false),
625 [R1,R2,R3|Crs] = CRs0,
626 [GenControl(CP) || CP <- merge_ranges([R1,R2,R3], split), CP =/= {$\r, undefined}],
627 %%GenControl(R1),GenControl(R2),GenControl(R3),
628 io:put_chars(Fd, "\n%% Optimize Latin-1\n"),
629 [GenExtP(CP) || CP <- merge_ranges(ExtendedPictographicLow)],
630
631 io:format(Fd,
632 "gc_1([CP|R]=R0) when CP < 256 ->\n"
633 " case R of\n"
634 " [CP2|_] when CP2 < 256 -> R0;\n"
635 " _ -> gc_extend(cp(R), R, CP)\n"
636 " end;\n",
637 []),
638 io:put_chars(Fd, "\n%% Continue control\n"),
639 [GenControl(CP) || CP <- Crs],
640 %% One clause per CP
641 %% CRs0 = merge_ranges(maps:get(cr, GBP) ++ maps:get(lf, GBP) ++ maps:get(control, GBP)),
642 %% [GenControl(CP) || CP <- CRs0, CP =/= {$\r, undefined}],
643
644 io:put_chars(Fd, "\n%% Handle prepend\n"),
645 GenPrepend = fun(Range) -> io:format(Fd, "gc_1~s gc_prepend(R1, CP);\n", [gen_clause(Range)]) end,
646 [GenPrepend(CP) || CP <- merge_ranges(maps:get(prepend,GBP))],
647
648 io:put_chars(Fd, "\n%% Handle Hangul L\n"),
649 GenHangulL = fun(Range) -> io:format(Fd, "gc_1~s gc_h_L(R1,[CP]);\n", [gen_clause(Range)]) end,
650 [GenHangulL(CP) || CP <- merge_ranges(maps:get(l,GBP))],
651 io:put_chars(Fd, "%% Handle Hangul V\n"),
652 GenHangulV = fun(Range) -> io:format(Fd, "gc_1~s gc_h_V(R1,[CP]);\n", [gen_clause(Range)]) end,
653 [GenHangulV(CP) || CP <- merge_ranges(maps:get(v,GBP))],
654 io:put_chars(Fd, "%% Handle Hangul T\n"),
655 GenHangulT = fun(Range) -> io:format(Fd, "gc_1~s gc_h_T(R1,[CP]);\n", [gen_clause(Range)]) end,
656 [GenHangulT(CP) || CP <- merge_ranges(maps:get(t,GBP))],
657 io:put_chars(Fd, "%% Handle Hangul LV and LVT special, since they are large\n"),
658 io:put_chars(Fd, "gc_1([CP|_]=R0) when 44000 < CP, CP < 56000 -> gc_h_lv_lvt(R0, R0, []);\n"),
659
660 io:put_chars(Fd, "\n%% Handle Regional\n"),
661 GenRegional = fun(Range) -> io:format(Fd, "gc_1~s gc_regional(R1,CP);\n", [gen_clause(Range)]) end,
662 [GenRegional(CP) || CP <- merge_ranges(maps:get(regional_indicator,GBP))],
663 %% io:put_chars(Fd, "%% Handle E_Base\n"),
664 %% GenEBase = fun(Range) -> io:format(Fd, "gc_1~s gc_e_cont(R1,[CP]);\n", [gen_clause(Range)]) end,
665 %% [GenEBase(CP) || CP <- merge_ranges(maps:get(e_base,GBP))],
666 %% io:put_chars(Fd, "%% Handle EBG\n"),
667 %% GenEBG = fun(Range) -> io:format(Fd, "gc_1~s gc_e_cont(R1,[CP]);\n", [gen_clause(Range)]) end,
668 %% [GenEBG(CP) || CP <- merge_ranges(maps:get(e_base_gaz,GBP))],
669
670 io:put_chars(Fd, "%% Handle extended_pictographic\n"),
671 [GenExtP(CP) || CP <- merge_ranges(ExtendedPictographicHigh)],
672 io:put_chars(Fd, "\n%% default clauses\n"),
673 io:put_chars(Fd, "gc_1([CP|R]) -> gc_extend(cp(R), R, CP).\n\n"),
674
675 io:put_chars(Fd, "%% Handle Prepend\n"),
676 io:put_chars(Fd,
677 "gc_prepend(R00, CP0) ->\n"
678 " case cp(R00) of\n"
679 " [CP1|_] = R0 ->\n"
680 " case is_control(CP1) of\n"
681 " true -> [CP0|R00];\n"
682 " false ->\n"
683 " case gc_1(R0) of\n"
684 " [GC|R1] when is_integer(GC) -> [[CP0,GC]|R1];\n"
685 " [GC|R1] -> [[CP0|GC]|R1]\n"
686 " end\n"
687 " end;\n"
688 " [] -> [CP0];\n"
689 " {error,R} -> [CP0|R]\n"
690 " end.\n\n"),
691
692 IsCtrl = fun(Range) -> io:format(Fd, "is_control~s true;\n", [gen_single_clause(Range)]) end,
693 [IsCtrl(CP) || CP <- merge_ranges(maps:get(cr, GBP) ++ maps:get(lf, GBP) ++ maps:get(control, GBP))],
694 io:put_chars(Fd, "is_control(_) -> false.\n\n"),
695
696 io:put_chars(Fd, "%% Handle Extend\n"
697 "%% To simplify binary handling in libraries the tail should be kept binary\n"
698 "%% and not a lookahead CP\n"
699 ),
700 io:put_chars(Fd,
701 "gc_extend([CP|T], T0, CP0) ->\n"
702 " case is_extend(CP) of\n"
703 " false -> [CP0|T0]; % losing work done on T\n"
704 " _TrueOrZWJ -> gc_extend2(cp(T), T, [CP,CP0])\n"
705 " end;\n"
706 "gc_extend([], _, CP) -> [CP];\n"
707 "gc_extend({error,R}, _, CP) -> [CP|R].\n\n"),
708 io:put_chars(Fd,
709 "gc_extend2([CP|T], T0, Acc) ->\n"
710 " case is_extend(CP) of\n"
711 " false -> [lists:reverse(Acc)|T0]; % losing work done on T\n"
712 " _TrueOrZWJ -> gc_extend2(cp(T), T, [CP|Acc])\n"
713 " end;\n"
714 "gc_extend2([], _, Acc) ->\n"
715 " [lists:reverse(Acc)];\n"
716 "gc_extend2({error,R}, _, Acc) ->\n"
717 " [lists:reverse(Acc)] ++ [R].\n\n"
718 ),
719 [ZWJ] = maps:get(zwj, GBP),
720 GenExtend = fun(R) when R =:= ZWJ -> io:format(Fd, "is_extend~s zwj;\n", [gen_single_clause(ZWJ)]);
721 (Range) -> io:format(Fd, "is_extend~s true;\n", [gen_single_clause(Range)])
722 end,
723 Extends = merge_ranges(maps:get(extend,GBP)++maps:get(spacingmark, GBP) ++ maps:get(zwj, GBP), split),
724 [GenExtend(CP) || CP <- Extends],
725 io:put_chars(Fd, "is_extend(_) -> false.\n\n"),
726
727 io:put_chars(Fd,
728 "gc_ext_pict(T, Acc) ->\n"
729 " gc_ext_pict(cp(T), T, Acc).\n\n"
730 "gc_ext_pict([CP|R1], T0, Acc) ->\n"
731 " case is_extend(CP) of\n"
732 " zwj -> gc_ext_pict_zwj(cp(R1), R1, [CP|Acc]);\n"
733 " true -> gc_ext_pict(R1, [CP|Acc]);\n"
734 " false ->\n"
735 " case Acc of\n"
736 " [A] -> [A|T0];\n"
737 " _ -> [lists:reverse(Acc)|T0]\n"
738 " end\n"
739 " end;\n"
740 "gc_ext_pict([], _T0, Acc) ->\n"
741 " case Acc of\n"
742 " [A] -> [A];\n"
743 " _ -> [lists:reverse(Acc)]\n"
744 " end;\n"
745 "gc_ext_pict({error,R}, T, Acc) ->\n"
746 " gc_ext_pict([], T, Acc) ++ [R].\n\n"),
747 io:put_chars(Fd,
748 "gc_ext_pict_zwj([CP|R1], T0, Acc) ->\n"
749 " case is_ext_pict(CP) of\n"
750 " true -> gc_ext_pict(R1, [CP|Acc]);\n"
751 " false ->\n"
752 " case Acc of\n"
753 " [A] -> [A|T0];\n"
754 " _ -> [lists:reverse(Acc)|T0]\n"
755 " end\n"
756 " end;\n"
757 "gc_ext_pict_zwj([], _, Acc) ->\n"
758 " case Acc of\n"
759 " [A] -> [A];\n"
760 " _ -> [lists:reverse(Acc)]\n"
761 " end;\n"
762 "gc_ext_pict_zwj({error,R}, T, Acc) ->\n"
763 " gc_ext_pict_zwj([], T, Acc) ++ [R].\n\n"),
764
765 GenExtPict = fun(Range) -> io:format(Fd, "is_ext_pict~s true;\n", [gen_single_clause(Range)]) end,
766 [GenExtPict(CP) || CP <- ExtendedPictographic0],
767 io:put_chars(Fd, "is_ext_pict(_) -> false.\n\n"),
768
769 %% --------------------
770 io:put_chars(Fd, "%% Handle Regional\n"),
771 [{RLess,RLarge}] = merge_ranges(maps:get(regional_indicator,GBP)),
772 io:put_chars(Fd,"gc_regional(R0, CP0) ->\n"
773 " case cp(R0) of\n"),
774 io:format(Fd, " [CP|R1] when ~w =< CP,CP =< ~w-> gc_extend2(cp(R1),R1,[CP,CP0]);~n",[RLess, RLarge]),
775 io:put_chars(Fd," R1 -> gc_extend(R1, R0, CP0)\n"
776 " end.\n\n"),
777
778 %% Special hangul
779 io:put_chars(Fd, "%% Handle Hangul L\n"),
780 io:put_chars(Fd, "gc_h_L(R0, Acc) ->\n case cp(R0) of\n"),
781 GenHangulL_1 = fun(Range) -> io:format(Fd, "~8c~s gc_h_L(R1,[CP|Acc]);\n",
782 [$\s,gen_case_clause(Range)]) end,
783 [GenHangulL_1(CP) || CP <- merge_ranges(maps:get(l,GBP))],
784 GenHangulL_2 = fun(Range) -> io:format(Fd, "~8c~s gc_h_V(R1,[CP|Acc]);\n",
785 [$\s,gen_case_clause(Range)]) end,
786 [GenHangulL_2(CP) || CP <- merge_ranges(maps:get(v,GBP))],
787 io:put_chars(Fd, " R1 -> gc_h_lv_lvt(R1, R0, Acc)\n end.\n\n"),
788
789 io:put_chars(Fd, "%% Handle Hangul V\n"),
790 io:put_chars(Fd, "gc_h_V(R0, Acc) ->\n case cp(R0) of\n"),
791 GenHangulV_1 = fun(Range) -> io:format(Fd, "~8c~s gc_h_V(R1,[CP|Acc]);\n",
792 [$\s,gen_case_clause(Range)]) end,
793 [GenHangulV_1(CP) || CP <- merge_ranges(maps:get(v,GBP))],
794 GenHangulV_2 = fun(Range) -> io:format(Fd, "~8c~s gc_h_T(R1,[CP|Acc]);\n",
795 [$\s,gen_case_clause(Range)]) end,
796 [GenHangulV_2(CP) || CP <- merge_ranges(maps:get(t,GBP))],
797 io:put_chars(Fd,
798 " R1 ->\n"
799 " case Acc of\n"
800 " [CP] -> gc_extend(R1, R0, CP);\n"
801 " _ -> gc_extend2(R1, R0, Acc)\n"
802 " end\n end.\n\n"),
803 io:put_chars(Fd, "%% Handle Hangul T\n"),
804 io:put_chars(Fd, "gc_h_T(R0, Acc) ->\n case cp(R0) of\n"),
805 GenHangulT_1 = fun(Range) -> io:format(Fd, "~8c~s gc_h_T(R1,[CP|Acc]);\n",
806 [$\s,gen_case_clause(Range)]) end,
807 [GenHangulT_1(CP) || CP <- merge_ranges(maps:get(t,GBP))],
808 io:put_chars(Fd,
809 " R1 ->\n"
810 " case Acc of\n"
811 " [CP] -> gc_extend(R1, R0, CP);\n"
812 " _ -> gc_extend2(R1, R0, Acc)\n"
813 " end\n end.\n\n"),
814 io:put_chars(Fd, "%% Handle Hangul LV\n"),
815 GenHangulLV = fun(Range) -> io:format(Fd, "gc_h_lv_lvt~s gc_h_V(R1,[CP|Acc]);\n",
816 [gen_clause2(Range)]) end,
817 [GenHangulLV(CP) || CP <- merge_ranges(maps:get(lv,GBP))],
818 io:put_chars(Fd, "%% Handle Hangul LVT\n"),
819 GenHangulLVT = fun(Range) -> io:format(Fd, "gc_h_lv_lvt~s gc_h_T(R1,[CP|Acc]);\n",
820 [gen_clause2(Range)]) end,
821 [GenHangulLVT(CP) || CP <- merge_ranges(maps:get(lvt,GBP))],
822 io:put_chars(Fd, "gc_h_lv_lvt([CP|R1], _, []) -> gc_extend(cp(R1), R1, CP);\n"), %% From gc_1/1
823 io:put_chars(Fd, "%% Also handles error tuples\n"),
824 io:put_chars(Fd, "gc_h_lv_lvt(R1, R0, [CP]) -> gc_extend(R1, R0, CP);\n"),
825 io:put_chars(Fd, "gc_h_lv_lvt(R1, R0, Acc) -> gc_extend2(R1, R0, Acc).\n\n"),
826 ok.
827
828 gen_compose_pairs(Fd, ExclData, Data) ->
829 GetPairs =
830 fun(CP, #cp{dec=[DCP,_]=Pair}, Acc) ->
831 case array:get(DCP,Data) of
832 #cp{class=0} -> [{Pair, CP}|Acc];
833 #cp{} -> Acc %% Ignore Non-Starter Decompositions
834 end;
835 (_, _, Acc) -> Acc
836 end,
837 DeCmp2 = array:sparse_foldl(GetPairs, [], ExclData),
838 [io:format(Fd, "compose_pair(~w,~w) -> ~w;~n", [A,B,CP]) || {[A,B],CP} <- lists:sort(DeCmp2)],
839 io:put_chars(Fd, "compose_pair(_,_) -> false.\n\n"),
840
841 io:put_chars(Fd, "nolist(CP, []) -> CP;\nnolist(CP,L) -> [CP|L].\n\n"),
842 ok.
843
844 gen_case_table(Fd, Data) ->
845 Case = array:foldr(fun(CP, #cp{cs={U0,L0,T0,F0}}, Acc) ->
846 U = def_cp(U0,CP),
847 L = def_cp(L0,CP),
848 T = def_cp(T0,CP),
849 F = def_cp(F0,CP),
850 case T =:= U andalso F =:= L of
851 true ->
852 [{CP,{U,L}}|Acc];
853 false ->
854 [{CP,{U,L,T,F}}|Acc]
855 end;
856 (_CP, _, Acc) -> Acc
857 end, [], Data),
858 [io:format(Fd, "case_table(~w) -> ~w;\n", [CP, Map])|| {CP,Map} <- Case],
859 io:format(Fd, "case_table(CP) -> {CP, CP}.\n\n",[]),
860 ok.
861
862 def_cp([], CP) -> CP;
863 def_cp(CP, _) -> CP.
864
865 gen_unicode_table(Fd, Data) ->
866 FixCanon = fun(_, #cp{class=CCC, dec=Dec, comp=Comp}) ->
867 Canon = decompose(Dec,Data),
868 #{ccc=>CCC, canonical=>Canon, compat=>Comp}
869 end,
870 AofMaps0 = array:sparse_map(FixCanon, Data),
871 FixCompat = fun(_, #{ccc:=CCC, canonical:=Canon, compat:=Comp}) ->
872 Compat = decompose_compat(Canon, Comp, AofMaps0),
873 {CCC, Canon, Compat}
874 end,
875 AofMaps1 = array:sparse_map(FixCompat, AofMaps0),
876
877 Dict0 = array:sparse_to_orddict(AofMaps1),
878 Def = {0, [], []},
879 Dict = lists:filter(fun({_, Map}) -> Map =/= Def end, Dict0),
880
881 [io:format(Fd, "unicode_table(~w) -> ~w;~n", [CP, Map]) || {CP,Map} <- Dict],
882 io:format(Fd, "unicode_table(_) -> ~w.~n~n",[Def]),
883 ok.
884
885 decompose([], _Data) -> [];
886 decompose([CP|CPs], Data) when is_integer(CP) ->
887 case array:get(CP,Data) of
888 undefined -> [{0,CP}|decompose(CPs,Data)];
889 #cp{class=CCC, dec=[]} -> [{CCC,CP}|decompose(CPs,Data)];
890 #cp{dec=Dec} -> decompose(Dec, Data) ++ decompose(CPs,Data)
891 end.
892
893
894 decompose_compat(Canon, [], Data) ->
895 case decompose_compat(Canon, Data) of
896 Canon -> [];
897 Other -> {compat, Other}
898 end;
899 decompose_compat([], {CompatMode, CPs}, Data) ->
900 {CompatMode, decompose_compat(CPs, Data)}.
901
902 decompose_compat([], _Data) -> [];
903 decompose_compat([CP|CPs], Data) when is_integer(CP) ->
904 case array:get(CP,Data) of
905 undefined ->
906 [{0,CP}|decompose_compat(CPs,Data)];
907 #{ccc:=CCC, canonical:=[], compat:=[]} ->
908 [{CCC,CP}|decompose_compat(CPs,Data)];
909 #{canonical:=[], compat:={_,Dec}} ->
910 decompose_compat(Dec, Data) ++ decompose_compat(CPs,Data);
911 #{canonical:=Dec} ->
912 decompose_compat(Dec, Data) ++ decompose_compat(CPs,Data)
913 end;
914 decompose_compat([{_,CP}|CPs], Data) ->
915 decompose_compat([CP|CPs], Data).
916
917
918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
919
920 gen_clause({R0, undefined}) ->
921 io_lib:format("([~w=CP|R1]=R0) ->", [R0]);
922 gen_clause({R0, R1}) ->
923 io_lib:format("([CP|R1]=R0) when ~w =< CP, CP =< ~w ->", [R0,R1]).
924
925 gen_clause2({R0, undefined}) ->
926 io_lib:format("([~w=CP|R1], R0, Acc) ->", [R0]);
927 gen_clause2({R0, R1}) ->
928 io_lib:format("([CP|R1], R0, Acc) when ~w =< CP, CP =< ~w ->", [R0,R1]).
929
930 gen_case_clause({R0, undefined}) ->
931 io_lib:format("[~w=CP|R1] ->", [R0]);
932 gen_case_clause({R0, R1}) ->
933 io_lib:format("[CP|R1] when ~w =< CP, CP =< ~w ->", [R0,R1]).
934
935
936 gen_single_clause({R0, undefined}) ->
937 io_lib:format("(~w) ->", [R0]);
938 gen_single_clause({R0, R1}) ->
939 io_lib:format("(CP) when ~w =< CP, CP =< ~w ->", [R0,R1]).
940
941
942 merge_ranges(List) ->
943 merge_ranges(List, true).
944
945 merge_ranges(List, Opt) ->
946 Res0 = merge_ranges_1(lists:sort(List), []),
947 case Opt of
948 split ->
949 split_ranges(Res0,[]); % One clause per CP
950 true ->
951 Res = Res0,
952 OptRes = optimize_ranges(Res),
953 true = lists:sort(Res) =:= lists:sort(OptRes), %Assertion.
954 OptRes;
955 false ->
956 Res0
957 end.
958
959 merge_ranges_1([{Next, Stop}|R], [{Start,undefined}|Acc]) when Start+1 =:= Next ->
960 case Stop of
961 undefined -> merge_ranges_1(R, [{Start, Next}|Acc]);
962 _ -> merge_ranges_1(R, [{Start,Stop}|Acc])
963 end;
964 merge_ranges_1([{Next, Stop}|R], [{Start,Prev}|Acc]) when Prev+1 =:= Next ->
965 case Stop of
966 undefined -> merge_ranges_1(R, [{Start, Next}|Acc]);
967 _ -> merge_ranges_1(R, [{Start,Stop}|Acc])
968 end;
969 merge_ranges_1([{Next, Stop}|R], [{Start,undefined}|Acc]) when Start+1 =:= Next ->
970 case Stop of
971 undefined -> merge_ranges_1(R, [{Start, Next}|Acc]);
972 _ -> merge_ranges_1(R, [{Start,Stop}|Acc])
973 end;
974 merge_ranges_1([Next|R], Acc) ->
975 merge_ranges_1(R, [Next|Acc]);
976 merge_ranges_1([], Acc) ->
977 lists:reverse(Acc).
978
979 split_ranges([{_,undefined}=CP|Rs], Acc) ->
980 split_ranges(Rs,[CP|Acc]);
981 split_ranges([{F,L}|Rs], Acc) when F < L ->
982 split_ranges([{F+1,L}|Rs],[{F,undefined}|Acc]);
983 split_ranges([{L,L}|Rs], Acc) ->
984 split_ranges(Rs,[{L, undefined}|Acc]);
985 split_ranges([], Acc) ->
986 lists:reverse(Acc).
987
988 optimize_ranges(Rs0) ->
989 PF = fun({N,undefined}) when is_integer(N) -> true;
990 (_) -> false
991 end,
992 {Singles,Rs} = lists:partition(PF, Rs0),
993 Singles ++ optimize_ranges_1(Rs).
994
995 optimize_ranges_1(Rs) ->
996 case length(Rs) of
997 N when N >= 4 ->
998 {R0,[Mid|R1]} = lists:split(N div 2, Rs),
999 [Mid|optimize_ranges_1(R0)++optimize_ranges_1(R1)];
1000 _ ->
1001 Rs
1002 end.
1003
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1005
1006 hex_to_int([]) -> [];
1007 hex_to_int(HexStr) ->
1008 list_to_integer(str_trim(HexStr, both), 16).
1009
1010 to_atom(Str) ->
1011 list_to_atom(string:lowercase(str_trim(Str, both))).
1012
1013 foldl(Fun, Acc, Fd) ->
1014 Get = fun() -> file:read_line(Fd) end,
1015 foldl_1(Fun, Acc, Get).
1016
1017 foldl_1(_Fun, {done, Acc}, _Get) -> Acc;
1018 foldl_1(Fun, Acc, Get) ->
1019 case Get() of
1020 eof -> Acc;
1021 {ok, "#" ++ _} -> %% Ignore comments
1022 foldl_1(Fun, Acc, Get);
1023 {ok, "\n"} -> %% Ignore empty lines
1024 foldl_1(Fun, Acc, Get);
1025 {ok, Line} ->
1026 foldl_1(Fun, Fun(Line, Acc), Get)
1027 end.
1028
1029
1030
1031 %% Differs fromstr_lexemes, it returns empty string as token between two delimiters
1032 tokens(S, [C]) ->
1033 tokens(lists:reverse(S), C, []).
1034
1035 tokens([Sep|S], Sep, Toks) ->
1036 tokens(S, Sep, [[]|Toks]);
1037 tokens([C|S], Sep, Toks) ->
1038 tokens_2(S, Sep, Toks, [C]);
1039 tokens([], _, Toks) ->
1040 Toks.
1041
1042 tokens_2([Sep|S], Sep, Toks, Tok) ->
1043 tokens(S, Sep, [Tok|Toks]);
1044 tokens_2([C|S], Sep, Toks, Tok) ->
1045 tokens_2(S, Sep, Toks, [C|Tok]);
1046 tokens_2([], _Sep, Toks, Tok) ->
1047 [Tok|Toks].