1 import re
, unicodedata
, sys
3 if sys
.maxunicode
== 65535:
4 raise RuntimeError("need UCS-4 Python")
6 def gen_category(cats
):
7 for i
in range(0, 0x110000):
8 if unicodedata
.category(unichr(i
)) in cats
:
11 def gen_bidirectional(cats
):
12 for i
in range(0, 0x110000):
13 if unicodedata
.bidirectional(unichr(i
)) in cats
:
28 tuple.append((prev
,prev
+span
+1))
30 for i
in range(prev
, prev
+span
+1):
37 tuple.append((prev
,prev
+span
+1))
40 tuple = " + ".join(["range(%d,%d)" % t
for t
in tuple])
42 return "set(%s)" % tuple
44 return "set(%s)" % repr(single
)
45 return "set(%s + %s)" % (repr(single
),tuple)
47 ############## Read the tables in the RFC #######################
49 data
= open("rfc3454.txt").readlines()
57 # Skip RFC page breaks
58 if l
.startswith("Hoffman & Blanchet") or\
59 l
.startswith("RFC 3454"):
61 # Find start/end lines
62 m
= re
.match("----- (Start|End) Table ([A-Z](.[0-9])+) -----", l
)
64 if m
.group(1) == "Start":
66 raise RuntimeError("Double Start", (curname
, l
))
69 tables
.append((curname
, table
))
73 raise RuntimeError("End without start", l
)
78 # Now we are in a table
84 fields
= fields
[0].split("-")
90 raise RuntimeError("Unpacking problem", l
)
92 start
= end
= fields
[0]
93 start
= int(start
, 16)
95 for i
in range(start
, end
+1):
101 value
= [int(v
, 16) for v
in value
.split(" ")]
105 table
[int(code
, 16)] = value
107 ########### Generate compact Python versions of the tables #############
109 print """# This file is generated by mkstringprep.py. DO NOT EDIT.
110 \"\"\"Library that exposes various tables found in the StringPrep RFC 3454.
112 There are two kinds of tables: sets, for which a member test is provided,
113 and mappings, for which a mapping function is provided.
119 print "assert unicodedata.unidata_version == %s" % repr(unicodedata
.unidata_version
)
121 # A.1 is the table of unassigned characters
122 # XXX Plane 15 PUA is listed as unassigned in Python.
123 name
, table
= tables
[0]
126 table
= set(table
.keys())
127 Cn
= set(gen_category(["Cn"]))
129 # FDD0..FDEF are process internal codes
130 Cn
-= set(range(0xFDD0, 0xFDF0))
132 Cn
-= set(range(0xFFFE, 0x110000, 0x10000))
133 Cn
-= set(range(0xFFFF, 0x110000, 0x10000))
138 def in_table_a1(code):
139 if unicodedata.category(code) != 'Cn': return False
141 if 0xFDD0 <= c < 0xFDF0: return False
142 return (c & 0xFFFF) not in (0xFFFE, 0xFFFF)
145 # B.1 cannot easily be derived
146 name
, table
= tables
[0]
149 table
= sorted(table
.keys())
151 b1_set = """ + compact_set(table
) + """
152 def in_table_b1(code):
153 return ord(code) in b1_set
156 # B.2 and B.3 is case folding.
157 # It takes CaseFolding.txt into account, which is
158 # not available in the Python database. Since
159 # B.2 is derived from B.3, we process B.3 first.
160 # B.3 supposedly *is* CaseFolding-3.2.0.txt.
162 name
, table_b2
= tables
[0]
166 name
, table_b3
= tables
[0]
170 # B.3 is mostly Python's .lower, except for a number
171 # of special cases, e.g. considering canonical forms.
175 for k
,v
in table_b2
.items():
176 if map(ord, unichr(k
).lower()) != v
:
177 b3_exceptions
[k
] = u
"".join(map(unichr,v
))
179 b3
= sorted(b3_exceptions
.items())
183 for i
,(k
,v
) in enumerate(b3
):
184 print "0x%x:%s," % (k
, repr(v
)),
190 def map_table_b3(code):
191 r = b3_exceptions.get(ord(code))
192 if r is not None: return r
196 def map_table_b3(code
):
197 r
= b3_exceptions
.get(ord(code
))
198 if r
is not None: return r
201 # B.2 is case folding for NFKC. This is the same as B.3,
202 # except where NormalizeWithKC(Fold(a)) !=
203 # NormalizeWithKC(Fold(NormalizeWithKC(Fold(a))))
207 b
= unicodedata
.normalize("NFKC", al
)
208 bl
= u
"".join([map_table_b3(ch
) for ch
in b
])
209 c
= unicodedata
.normalize("NFKC", bl
)
216 for k
,v
in table_b2
.items():
217 if map(ord, map_table_b2(unichr(k
))) != v
:
220 # B.3 should not add any additional special cases
221 assert specials
== {}
226 b = unicodedata.normalize("NFKC", al)
227 bl = u"".join([map_table_b3(ch) for ch in b])
228 c = unicodedata.normalize("NFKC", bl)
235 # C.1.1 is a table with a single character
236 name
, table
= tables
[0]
238 assert name
== "C.1.1"
239 assert table
== {0x20:0x20}
242 def in_table_c11(code):
246 # C.1.2 is the rest of all space characters
247 name
, table
= tables
[0]
249 assert name
== "C.1.2"
251 # table = set(table.keys())
252 # Zs = set(gen_category(["Zs"])) - set([0x20])
256 def in_table_c12(code):
257 return unicodedata.category(code) == "Zs" and code != u" "
259 def in_table_c11_c12(code):
260 return unicodedata.category(code) == "Zs"
263 # C.2.1 ASCII control characters
264 name
, table_c21
= tables
[0]
266 assert name
== "C.2.1"
268 Cc
= set(gen_category(["Cc"]))
269 Cc_ascii
= Cc
& set(range(128))
270 table_c21
= set(table_c21
.keys())
271 assert Cc_ascii
== table_c21
274 def in_table_c21(code):
275 return ord(code) < 128 and unicodedata.category(code) == "Cc"
278 # C.2.2 Non-ASCII control characters. It also includes
279 # a number of characters in category Cf.
280 name
, table_c22
= tables
[0]
282 assert name
== "C.2.2"
284 Cc_nonascii
= Cc
- Cc_ascii
285 table_c22
= set(table_c22
.keys())
286 assert len(Cc_nonascii
- table_c22
) == 0
288 specials
= list(table_c22
- Cc_nonascii
)
291 print """c22_specials = """ + compact_set(specials
) + """
292 def in_table_c22(code):
294 if c < 128: return False
295 if unicodedata.category(code) == "Cc": return True
296 return c in c22_specials
298 def in_table_c21_c22(code):
299 return unicodedata.category(code) == "Cc" or \\
300 ord(code) in c22_specials
304 name
, table
= tables
[0]
308 Co
= set(gen_category(["Co"]))
309 assert set(table
.keys()) == Co
312 def in_table_c3(code):
313 return unicodedata.category(code) == "Co"
316 # C.4 Non-character code points, xFFFE, xFFFF
317 # plus process internal codes
318 name
, table
= tables
[0]
322 nonchar
= set(range(0xFDD0,0xFDF0) +
323 range(0xFFFE,0x110000,0x10000) +
324 range(0xFFFF,0x110000,0x10000))
325 table
= set(table
.keys())
326 assert table
== nonchar
329 def in_table_c4(code):
331 if c < 0xFDD0: return False
332 if c < 0xFDF0: return True
333 return (ord(code) & 0xFFFF) in (0xFFFE, 0xFFFF)
336 # C.5 Surrogate codes
337 name
, table
= tables
[0]
341 Cs
= set(gen_category(["Cs"]))
342 assert set(table
.keys()) == Cs
345 def in_table_c5(code):
346 return unicodedata.category(code) == "Cs"
349 # C.6 Inappropriate for plain text
350 name
, table
= tables
[0]
354 table
= sorted(table
.keys())
357 c6_set = """ + compact_set(table
) + """
358 def in_table_c6(code):
359 return ord(code) in c6_set
362 # C.7 Inappropriate for canonical representation
363 name
, table
= tables
[0]
367 table
= sorted(table
.keys())
370 c7_set = """ + compact_set(table
) + """
371 def in_table_c7(code):
372 return ord(code) in c7_set
375 # C.8 Change display properties or are deprecated
376 name
, table
= tables
[0]
380 table
= sorted(table
.keys())
383 c8_set = """ + compact_set(table
) + """
384 def in_table_c8(code):
385 return ord(code) in c8_set
388 # C.9 Tagging characters
389 name
, table
= tables
[0]
393 table
= sorted(table
.keys())
396 c9_set = """ + compact_set(table
) + """
397 def in_table_c9(code):
398 return ord(code) in c9_set
401 # D.1 Characters with bidirectional property "R" or "AL"
402 name
, table
= tables
[0]
406 RandAL
= set(gen_bidirectional(["R","AL"]))
407 assert set(table
.keys()) == RandAL
410 def in_table_d1(code):
411 return unicodedata.bidirectional(code) in ("R","AL")
414 # D.2 Characters with bidirectional property "L"
415 name
, table
= tables
[0]
419 L
= set(gen_bidirectional(["L"]))
420 assert set(table
.keys()) == L
423 def in_table_d2(code):
424 return unicodedata.bidirectional(code) == "L"