1 | n/a | import re, sys |
---|
2 | n/a | from unicodedata import ucd_3_2_0 as unicodedata |
---|
3 | n/a | |
---|
4 | n/a | if sys.maxunicode == 65535: |
---|
5 | n/a | raise RuntimeError("need UCS-4 Python") |
---|
6 | n/a | |
---|
7 | n/a | def gen_category(cats): |
---|
8 | n/a | for i in range(0, 0x110000): |
---|
9 | n/a | if unicodedata.category(chr(i)) in cats: |
---|
10 | n/a | yield(i) |
---|
11 | n/a | |
---|
12 | n/a | def gen_bidirectional(cats): |
---|
13 | n/a | for i in range(0, 0x110000): |
---|
14 | n/a | if unicodedata.bidirectional(chr(i)) in cats: |
---|
15 | n/a | yield(i) |
---|
16 | n/a | |
---|
17 | n/a | def compact_set(l): |
---|
18 | n/a | single = [] |
---|
19 | n/a | tuple = [] |
---|
20 | n/a | prev = None |
---|
21 | n/a | span = 0 |
---|
22 | n/a | for e in l: |
---|
23 | n/a | if prev is None: |
---|
24 | n/a | prev = e |
---|
25 | n/a | span = 0 |
---|
26 | n/a | continue |
---|
27 | n/a | if prev+span+1 != e: |
---|
28 | n/a | if span > 2: |
---|
29 | n/a | tuple.append((prev,prev+span+1)) |
---|
30 | n/a | else: |
---|
31 | n/a | for i in range(prev, prev+span+1): |
---|
32 | n/a | single.append(i) |
---|
33 | n/a | prev = e |
---|
34 | n/a | span = 0 |
---|
35 | n/a | else: |
---|
36 | n/a | span += 1 |
---|
37 | n/a | if span: |
---|
38 | n/a | tuple.append((prev,prev+span+1)) |
---|
39 | n/a | else: |
---|
40 | n/a | single.append(prev) |
---|
41 | n/a | if not single and len(tuple) == 1: |
---|
42 | n/a | tuple = "range(%d,%d)" % tuple[0] |
---|
43 | n/a | else: |
---|
44 | n/a | tuple = " + ".join("list(range(%d,%d))" % t for t in tuple) |
---|
45 | n/a | if not single: |
---|
46 | n/a | return "set(%s)" % tuple |
---|
47 | n/a | if not tuple: |
---|
48 | n/a | return "set(%r)" % (single,) |
---|
49 | n/a | return "set(%r + %s)" % (single, tuple) |
---|
50 | n/a | |
---|
51 | n/a | ############## Read the tables in the RFC ####################### |
---|
52 | n/a | |
---|
53 | n/a | with open("rfc3454.txt") as f: |
---|
54 | n/a | data = f.readlines() |
---|
55 | n/a | |
---|
56 | n/a | tables = [] |
---|
57 | n/a | curname = None |
---|
58 | n/a | for l in data: |
---|
59 | n/a | l = l.strip() |
---|
60 | n/a | if not l: |
---|
61 | n/a | continue |
---|
62 | n/a | # Skip RFC page breaks |
---|
63 | n/a | if l.startswith(("Hoffman & Blanchet", "RFC 3454")): |
---|
64 | n/a | continue |
---|
65 | n/a | # Find start/end lines |
---|
66 | n/a | m = re.match("----- (Start|End) Table ([A-Z](.[0-9])+) -----", l) |
---|
67 | n/a | if m: |
---|
68 | n/a | if m.group(1) == "Start": |
---|
69 | n/a | if curname: |
---|
70 | n/a | raise RuntimeError("Double Start", (curname, l)) |
---|
71 | n/a | curname = m.group(2) |
---|
72 | n/a | table = {} |
---|
73 | n/a | tables.append((curname, table)) |
---|
74 | n/a | continue |
---|
75 | n/a | else: |
---|
76 | n/a | if not curname: |
---|
77 | n/a | raise RuntimeError("End without start", l) |
---|
78 | n/a | if curname != m.group(2): |
---|
79 | n/a | raise RuntimeError("Unexpected end", l) |
---|
80 | n/a | curname = None |
---|
81 | n/a | continue |
---|
82 | n/a | if not curname: |
---|
83 | n/a | continue |
---|
84 | n/a | # Now we are in a table |
---|
85 | n/a | fields = l.split(";") |
---|
86 | n/a | if len(fields) > 1: |
---|
87 | n/a | # Drop comment field |
---|
88 | n/a | fields = fields[:-1] |
---|
89 | n/a | if len(fields) == 1: |
---|
90 | n/a | fields = fields[0].split("-") |
---|
91 | n/a | if len(fields) > 1: |
---|
92 | n/a | # range |
---|
93 | n/a | try: |
---|
94 | n/a | start, end = fields |
---|
95 | n/a | except ValueError: |
---|
96 | n/a | raise RuntimeError("Unpacking problem", l) |
---|
97 | n/a | else: |
---|
98 | n/a | start = end = fields[0] |
---|
99 | n/a | start = int(start, 16) |
---|
100 | n/a | end = int(end, 16) |
---|
101 | n/a | for i in range(start, end+1): |
---|
102 | n/a | table[i] = i |
---|
103 | n/a | else: |
---|
104 | n/a | code, value = fields |
---|
105 | n/a | value = value.strip() |
---|
106 | n/a | if value: |
---|
107 | n/a | value = [int(v, 16) for v in value.split(" ")] |
---|
108 | n/a | else: |
---|
109 | n/a | # table B.1 |
---|
110 | n/a | value = None |
---|
111 | n/a | table[int(code, 16)] = value |
---|
112 | n/a | |
---|
113 | n/a | ########### Generate compact Python versions of the tables ############# |
---|
114 | n/a | |
---|
115 | n/a | print("""# This file is generated by mkstringprep.py. DO NOT EDIT. |
---|
116 | n/a | \"\"\"Library that exposes various tables found in the StringPrep RFC 3454. |
---|
117 | n/a | |
---|
118 | n/a | There are two kinds of tables: sets, for which a member test is provided, |
---|
119 | n/a | and mappings, for which a mapping function is provided. |
---|
120 | n/a | \"\"\" |
---|
121 | n/a | |
---|
122 | n/a | from unicodedata import ucd_3_2_0 as unicodedata |
---|
123 | n/a | """) |
---|
124 | n/a | |
---|
125 | n/a | print("assert unicodedata.unidata_version == %r" % (unicodedata.unidata_version,)) |
---|
126 | n/a | |
---|
127 | n/a | # A.1 is the table of unassigned characters |
---|
128 | n/a | # XXX Plane 15 PUA is listed as unassigned in Python. |
---|
129 | n/a | name, table = tables[0] |
---|
130 | n/a | del tables[0] |
---|
131 | n/a | assert name == "A.1" |
---|
132 | n/a | table = set(table.keys()) |
---|
133 | n/a | Cn = set(gen_category(["Cn"])) |
---|
134 | n/a | |
---|
135 | n/a | # FDD0..FDEF are process internal codes |
---|
136 | n/a | Cn -= set(range(0xFDD0, 0xFDF0)) |
---|
137 | n/a | # not a character |
---|
138 | n/a | Cn -= set(range(0xFFFE, 0x110000, 0x10000)) |
---|
139 | n/a | Cn -= set(range(0xFFFF, 0x110000, 0x10000)) |
---|
140 | n/a | |
---|
141 | n/a | # assert table == Cn |
---|
142 | n/a | |
---|
143 | n/a | print(""" |
---|
144 | n/a | def in_table_a1(code): |
---|
145 | n/a | if unicodedata.category(code) != 'Cn': return False |
---|
146 | n/a | c = ord(code) |
---|
147 | n/a | if 0xFDD0 <= c < 0xFDF0: return False |
---|
148 | n/a | return (c & 0xFFFF) not in (0xFFFE, 0xFFFF) |
---|
149 | n/a | """) |
---|
150 | n/a | |
---|
151 | n/a | # B.1 cannot easily be derived |
---|
152 | n/a | name, table = tables[0] |
---|
153 | n/a | del tables[0] |
---|
154 | n/a | assert name == "B.1" |
---|
155 | n/a | table = sorted(table.keys()) |
---|
156 | n/a | print(""" |
---|
157 | n/a | b1_set = """ + compact_set(table) + """ |
---|
158 | n/a | def in_table_b1(code): |
---|
159 | n/a | return ord(code) in b1_set |
---|
160 | n/a | """) |
---|
161 | n/a | |
---|
162 | n/a | # B.2 and B.3 is case folding. |
---|
163 | n/a | # It takes CaseFolding.txt into account, which is |
---|
164 | n/a | # not available in the Python database. Since |
---|
165 | n/a | # B.2 is derived from B.3, we process B.3 first. |
---|
166 | n/a | # B.3 supposedly *is* CaseFolding-3.2.0.txt. |
---|
167 | n/a | |
---|
168 | n/a | name, table_b2 = tables[0] |
---|
169 | n/a | del tables[0] |
---|
170 | n/a | assert name == "B.2" |
---|
171 | n/a | |
---|
172 | n/a | name, table_b3 = tables[0] |
---|
173 | n/a | del tables[0] |
---|
174 | n/a | assert name == "B.3" |
---|
175 | n/a | |
---|
176 | n/a | # B.3 is mostly Python's .lower, except for a number |
---|
177 | n/a | # of special cases, e.g. considering canonical forms. |
---|
178 | n/a | |
---|
179 | n/a | b3_exceptions = {} |
---|
180 | n/a | |
---|
181 | n/a | for k,v in table_b2.items(): |
---|
182 | n/a | if list(map(ord, chr(k).lower())) != v: |
---|
183 | n/a | b3_exceptions[k] = "".join(map(chr,v)) |
---|
184 | n/a | |
---|
185 | n/a | b3 = sorted(b3_exceptions.items()) |
---|
186 | n/a | |
---|
187 | n/a | print(""" |
---|
188 | n/a | b3_exceptions = {""") |
---|
189 | n/a | for i, kv in enumerate(b3): |
---|
190 | n/a | print("0x%x:%a," % kv, end=' ') |
---|
191 | n/a | if i % 4 == 3: |
---|
192 | n/a | print() |
---|
193 | n/a | print("}") |
---|
194 | n/a | |
---|
195 | n/a | print(""" |
---|
196 | n/a | def map_table_b3(code): |
---|
197 | n/a | r = b3_exceptions.get(ord(code)) |
---|
198 | n/a | if r is not None: return r |
---|
199 | n/a | return code.lower() |
---|
200 | n/a | """) |
---|
201 | n/a | |
---|
202 | n/a | def map_table_b3(code): |
---|
203 | n/a | r = b3_exceptions.get(ord(code)) |
---|
204 | n/a | if r is not None: return r |
---|
205 | n/a | return code.lower() |
---|
206 | n/a | |
---|
207 | n/a | # B.2 is case folding for NFKC. This is the same as B.3, |
---|
208 | n/a | # except where NormalizeWithKC(Fold(a)) != |
---|
209 | n/a | # NormalizeWithKC(Fold(NormalizeWithKC(Fold(a)))) |
---|
210 | n/a | |
---|
211 | n/a | def map_table_b2(a): |
---|
212 | n/a | al = map_table_b3(a) |
---|
213 | n/a | b = unicodedata.normalize("NFKC", al) |
---|
214 | n/a | bl = "".join([map_table_b3(ch) for ch in b]) |
---|
215 | n/a | c = unicodedata.normalize("NFKC", bl) |
---|
216 | n/a | if b != c: |
---|
217 | n/a | return c |
---|
218 | n/a | else: |
---|
219 | n/a | return al |
---|
220 | n/a | |
---|
221 | n/a | specials = {} |
---|
222 | n/a | for k,v in table_b2.items(): |
---|
223 | n/a | if list(map(ord, map_table_b2(chr(k)))) != v: |
---|
224 | n/a | specials[k] = v |
---|
225 | n/a | |
---|
226 | n/a | # B.3 should not add any additional special cases |
---|
227 | n/a | assert specials == {} |
---|
228 | n/a | |
---|
229 | n/a | print(""" |
---|
230 | n/a | def map_table_b2(a): |
---|
231 | n/a | al = map_table_b3(a) |
---|
232 | n/a | b = unicodedata.normalize("NFKC", al) |
---|
233 | n/a | bl = "".join([map_table_b3(ch) for ch in b]) |
---|
234 | n/a | c = unicodedata.normalize("NFKC", bl) |
---|
235 | n/a | if b != c: |
---|
236 | n/a | return c |
---|
237 | n/a | else: |
---|
238 | n/a | return al |
---|
239 | n/a | """) |
---|
240 | n/a | |
---|
241 | n/a | # C.1.1 is a table with a single character |
---|
242 | n/a | name, table = tables[0] |
---|
243 | n/a | del tables[0] |
---|
244 | n/a | assert name == "C.1.1" |
---|
245 | n/a | assert table == {0x20:0x20} |
---|
246 | n/a | |
---|
247 | n/a | print(""" |
---|
248 | n/a | def in_table_c11(code): |
---|
249 | n/a | return code == " " |
---|
250 | n/a | """) |
---|
251 | n/a | |
---|
252 | n/a | # C.1.2 is the rest of all space characters |
---|
253 | n/a | name, table = tables[0] |
---|
254 | n/a | del tables[0] |
---|
255 | n/a | assert name == "C.1.2" |
---|
256 | n/a | |
---|
257 | n/a | # table = set(table.keys()) |
---|
258 | n/a | # Zs = set(gen_category(["Zs"])) - {0x20} |
---|
259 | n/a | # assert Zs == table |
---|
260 | n/a | |
---|
261 | n/a | print(""" |
---|
262 | n/a | def in_table_c12(code): |
---|
263 | n/a | return unicodedata.category(code) == "Zs" and code != " " |
---|
264 | n/a | |
---|
265 | n/a | def in_table_c11_c12(code): |
---|
266 | n/a | return unicodedata.category(code) == "Zs" |
---|
267 | n/a | """) |
---|
268 | n/a | |
---|
269 | n/a | # C.2.1 ASCII control characters |
---|
270 | n/a | name, table_c21 = tables[0] |
---|
271 | n/a | del tables[0] |
---|
272 | n/a | assert name == "C.2.1" |
---|
273 | n/a | |
---|
274 | n/a | Cc = set(gen_category(["Cc"])) |
---|
275 | n/a | Cc_ascii = Cc & set(range(128)) |
---|
276 | n/a | table_c21 = set(table_c21.keys()) |
---|
277 | n/a | assert Cc_ascii == table_c21 |
---|
278 | n/a | |
---|
279 | n/a | print(""" |
---|
280 | n/a | def in_table_c21(code): |
---|
281 | n/a | return ord(code) < 128 and unicodedata.category(code) == "Cc" |
---|
282 | n/a | """) |
---|
283 | n/a | |
---|
284 | n/a | # C.2.2 Non-ASCII control characters. It also includes |
---|
285 | n/a | # a number of characters in category Cf. |
---|
286 | n/a | name, table_c22 = tables[0] |
---|
287 | n/a | del tables[0] |
---|
288 | n/a | assert name == "C.2.2" |
---|
289 | n/a | |
---|
290 | n/a | Cc_nonascii = Cc - Cc_ascii |
---|
291 | n/a | table_c22 = set(table_c22.keys()) |
---|
292 | n/a | assert len(Cc_nonascii - table_c22) == 0 |
---|
293 | n/a | |
---|
294 | n/a | specials = list(table_c22 - Cc_nonascii) |
---|
295 | n/a | specials.sort() |
---|
296 | n/a | |
---|
297 | n/a | print("""c22_specials = """ + compact_set(specials) + """ |
---|
298 | n/a | def in_table_c22(code): |
---|
299 | n/a | c = ord(code) |
---|
300 | n/a | if c < 128: return False |
---|
301 | n/a | if unicodedata.category(code) == "Cc": return True |
---|
302 | n/a | return c in c22_specials |
---|
303 | n/a | |
---|
304 | n/a | def in_table_c21_c22(code): |
---|
305 | n/a | return unicodedata.category(code) == "Cc" or \\ |
---|
306 | n/a | ord(code) in c22_specials |
---|
307 | n/a | """) |
---|
308 | n/a | |
---|
309 | n/a | # C.3 Private use |
---|
310 | n/a | name, table = tables[0] |
---|
311 | n/a | del tables[0] |
---|
312 | n/a | assert name == "C.3" |
---|
313 | n/a | |
---|
314 | n/a | Co = set(gen_category(["Co"])) |
---|
315 | n/a | assert set(table.keys()) == Co |
---|
316 | n/a | |
---|
317 | n/a | print(""" |
---|
318 | n/a | def in_table_c3(code): |
---|
319 | n/a | return unicodedata.category(code) == "Co" |
---|
320 | n/a | """) |
---|
321 | n/a | |
---|
322 | n/a | # C.4 Non-character code points, xFFFE, xFFFF |
---|
323 | n/a | # plus process internal codes |
---|
324 | n/a | name, table = tables[0] |
---|
325 | n/a | del tables[0] |
---|
326 | n/a | assert name == "C.4" |
---|
327 | n/a | |
---|
328 | n/a | nonchar = set(range(0xFDD0,0xFDF0)) |
---|
329 | n/a | nonchar.update(range(0xFFFE,0x110000,0x10000)) |
---|
330 | n/a | nonchar.update(range(0xFFFF,0x110000,0x10000)) |
---|
331 | n/a | table = set(table.keys()) |
---|
332 | n/a | assert table == nonchar |
---|
333 | n/a | |
---|
334 | n/a | print(""" |
---|
335 | n/a | def in_table_c4(code): |
---|
336 | n/a | c = ord(code) |
---|
337 | n/a | if c < 0xFDD0: return False |
---|
338 | n/a | if c < 0xFDF0: return True |
---|
339 | n/a | return (ord(code) & 0xFFFF) in (0xFFFE, 0xFFFF) |
---|
340 | n/a | """) |
---|
341 | n/a | |
---|
342 | n/a | # C.5 Surrogate codes |
---|
343 | n/a | name, table = tables[0] |
---|
344 | n/a | del tables[0] |
---|
345 | n/a | assert name == "C.5" |
---|
346 | n/a | |
---|
347 | n/a | Cs = set(gen_category(["Cs"])) |
---|
348 | n/a | assert set(table.keys()) == Cs |
---|
349 | n/a | |
---|
350 | n/a | print(""" |
---|
351 | n/a | def in_table_c5(code): |
---|
352 | n/a | return unicodedata.category(code) == "Cs" |
---|
353 | n/a | """) |
---|
354 | n/a | |
---|
355 | n/a | # C.6 Inappropriate for plain text |
---|
356 | n/a | name, table = tables[0] |
---|
357 | n/a | del tables[0] |
---|
358 | n/a | assert name == "C.6" |
---|
359 | n/a | |
---|
360 | n/a | table = sorted(table.keys()) |
---|
361 | n/a | |
---|
362 | n/a | print(""" |
---|
363 | n/a | c6_set = """ + compact_set(table) + """ |
---|
364 | n/a | def in_table_c6(code): |
---|
365 | n/a | return ord(code) in c6_set |
---|
366 | n/a | """) |
---|
367 | n/a | |
---|
368 | n/a | # C.7 Inappropriate for canonical representation |
---|
369 | n/a | name, table = tables[0] |
---|
370 | n/a | del tables[0] |
---|
371 | n/a | assert name == "C.7" |
---|
372 | n/a | |
---|
373 | n/a | table = sorted(table.keys()) |
---|
374 | n/a | |
---|
375 | n/a | print(""" |
---|
376 | n/a | c7_set = """ + compact_set(table) + """ |
---|
377 | n/a | def in_table_c7(code): |
---|
378 | n/a | return ord(code) in c7_set |
---|
379 | n/a | """) |
---|
380 | n/a | |
---|
381 | n/a | # C.8 Change display properties or are deprecated |
---|
382 | n/a | name, table = tables[0] |
---|
383 | n/a | del tables[0] |
---|
384 | n/a | assert name == "C.8" |
---|
385 | n/a | |
---|
386 | n/a | table = sorted(table.keys()) |
---|
387 | n/a | |
---|
388 | n/a | print(""" |
---|
389 | n/a | c8_set = """ + compact_set(table) + """ |
---|
390 | n/a | def in_table_c8(code): |
---|
391 | n/a | return ord(code) in c8_set |
---|
392 | n/a | """) |
---|
393 | n/a | |
---|
394 | n/a | # C.9 Tagging characters |
---|
395 | n/a | name, table = tables[0] |
---|
396 | n/a | del tables[0] |
---|
397 | n/a | assert name == "C.9" |
---|
398 | n/a | |
---|
399 | n/a | table = sorted(table.keys()) |
---|
400 | n/a | |
---|
401 | n/a | print(""" |
---|
402 | n/a | c9_set = """ + compact_set(table) + """ |
---|
403 | n/a | def in_table_c9(code): |
---|
404 | n/a | return ord(code) in c9_set |
---|
405 | n/a | """) |
---|
406 | n/a | |
---|
407 | n/a | # D.1 Characters with bidirectional property "R" or "AL" |
---|
408 | n/a | name, table = tables[0] |
---|
409 | n/a | del tables[0] |
---|
410 | n/a | assert name == "D.1" |
---|
411 | n/a | |
---|
412 | n/a | RandAL = set(gen_bidirectional(["R","AL"])) |
---|
413 | n/a | assert set(table.keys()) == RandAL |
---|
414 | n/a | |
---|
415 | n/a | print(""" |
---|
416 | n/a | def in_table_d1(code): |
---|
417 | n/a | return unicodedata.bidirectional(code) in ("R","AL") |
---|
418 | n/a | """) |
---|
419 | n/a | |
---|
420 | n/a | # D.2 Characters with bidirectional property "L" |
---|
421 | n/a | name, table = tables[0] |
---|
422 | n/a | del tables[0] |
---|
423 | n/a | assert name == "D.2" |
---|
424 | n/a | |
---|
425 | n/a | L = set(gen_bidirectional(["L"])) |
---|
426 | n/a | assert set(table.keys()) == L |
---|
427 | n/a | |
---|
428 | n/a | print(""" |
---|
429 | n/a | def in_table_d2(code): |
---|
430 | n/a | return unicodedata.bidirectional(code) == "L" |
---|
431 | n/a | """) |
---|