1 import re
, unicodedata
, sys
, sets
4 if sys
.maxunicode
== 65535:
5 raise RuntimeError, "need UCS-4 Python"
7 def gen_category(cats
):
8 for i
in range(0, 0x110000):
9 if unicodedata
.category(unichr(i
)) in cats
:
12 def gen_bidirectional(cats
):
13 for i
in range(0, 0x110000):
14 if unicodedata
.bidirectional(unichr(i
)) in cats
:
29 tuple.append((prev
,prev
+span
+1))
31 for i
in range(prev
, prev
+span
+1):
38 tuple.append((prev
,prev
+span
+1))
41 tuple = " + ".join(["range(%d,%d)" % t
for t
in tuple])
43 return "sets.Set(%s)" % tuple
45 return "sets.Set(%s)" % repr(single
)
46 return "sets.Set(%s + %s)" % (repr(single
),tuple)
48 ############## Read the tables in the RFC #######################
50 data
= open("rfc3454.txt").readlines()
58 # Skip RFC page breaks
59 if l
.startswith("Hoffman & Blanchet") or\
60 l
.startswith("RFC 3454"):
62 # Find start/end lines
63 m
= re
.match("----- (Start|End) Table ([A-Z](.[0-9])+) -----", l
)
65 if m
.group(1) == "Start":
67 raise "Double Start",(curname
, l
)
70 tables
.append((curname
, table
))
74 raise "End without start", l
79 # Now we are in a table
85 fields
= fields
[0].split("-")
91 raise "Unpacking problem", l
93 start
= end
= fields
[0]
94 start
= int(start
, 16)
96 for i
in range(start
, end
+1):
100 value
= value
.strip()
102 value
= [int(v
, 16) for v
in value
.split(" ")]
106 table
[int(code
, 16)] = value
108 ########### Generate compact Python versions of the tables #############
110 print """# This file is generated by mkstringprep.py. DO NOT EDIT.
111 \"\"\"Library that exposes various tables found in the StringPrep RFC 3454.
113 There are two kinds of tables: sets, for which a member test is provided,
114 and mappings, for which a mapping function is provided.
117 import unicodedata, sets
120 print "assert unicodedata.unidata_version == %s" % repr(unicodedata
.unidata_version
)
122 # A.1 is the table of unassigned characters
123 # XXX Plane 15 PUA is listed as unassigned in Python.
124 name
, table
= tables
[0]
127 table
= Set(table
.keys())
128 Cn
= Set(gen_category(["Cn"]))
130 # FDD0..FDEF are process internal codes
131 Cn
-= Set(range(0xFDD0, 0xFDF0))
133 Cn
-= Set(range(0xFFFE, 0x110000, 0x10000))
134 Cn
-= Set(range(0xFFFF, 0x110000, 0x10000))
139 def in_table_a1(code):
140 if unicodedata.category(code) != 'Cn': return False
142 if 0xFDD0 <= c < 0xFDF0: return False
143 return (c & 0xFFFF) not in (0xFFFE, 0xFFFF)
146 # B.1 cannot easily be derived
147 name
, table
= tables
[0]
153 b1_set = """ + compact_set(table
) + """
154 def in_table_b1(code):
155 return ord(code) in b1_set
158 # B.2 and B.3 is case folding.
159 # It takes CaseFolding.txt into account, which is
160 # not available in the Python database. Since
161 # B.2 is derived from B.3, we process B.3 first.
162 # B.3 supposedly *is* CaseFolding-3.2.0.txt.
164 name
, table_b2
= tables
[0]
168 name
, table_b3
= tables
[0]
172 # B.3 is mostly Python's .lower, except for a number
173 # of special cases, e.g. considering canonical forms.
177 for k
,v
in table_b2
.items():
178 if map(ord, unichr(k
).lower()) != v
:
179 b3_exceptions
[k
] = u
"".join(map(unichr,v
))
181 b3
= b3_exceptions
.items()
186 for i
,(k
,v
) in enumerate(b3
):
187 print "0x%x:%s," % (k
, repr(v
)),
193 def map_table_b3(code):
194 r = b3_exceptions.get(ord(code))
195 if r is not None: return r
199 def map_table_b3(code
):
200 r
= b3_exceptions
.get(ord(code
))
201 if r
is not None: return r
204 # B.2 is case folding for NFKC. This is the same as B.3,
205 # except where NormalizeWithKC(Fold(a)) !=
206 # NormalizeWithKC(Fold(NormalizeWithKC(Fold(a))))
210 b
= unicodedata
.normalize("NFKC", al
)
211 bl
= u
"".join([map_table_b3(ch
) for ch
in b
])
212 c
= unicodedata
.normalize("NFKC", bl
)
219 for k
,v
in table_b2
.items():
220 if map(ord, map_table_b2(unichr(k
))) != v
:
223 # B.3 should not add any additional special cases
224 assert specials
== {}
229 b = unicodedata.normalize("NFKC", al)
230 bl = u"".join([map_table_b3(ch) for ch in b])
231 c = unicodedata.normalize("NFKC", bl)
238 # C.1.1 is a table with a single character
239 name
, table
= tables
[0]
241 assert name
== "C.1.1"
242 assert table
== {0x20:0x20}
245 def in_table_c11(code):
249 # C.1.2 is the rest of all space characters
250 name
, table
= tables
[0]
252 assert name
== "C.1.2"
254 # table = Set(table.keys())
255 # Zs = Set(gen_category(["Zs"])) - Set([0x20])
259 def in_table_c12(code):
260 return unicodedata.category(code) == "Zs" and code != u" "
262 def in_table_c11_c12(code):
263 return unicodedata.category(code) == "Zs"
266 # C.2.1 ASCII control characters
267 name
, table_c21
= tables
[0]
269 assert name
== "C.2.1"
271 Cc
= Set(gen_category(["Cc"]))
272 Cc_ascii
= Cc
& Set(range(128))
273 table_c21
= Set(table_c21
.keys())
274 assert Cc_ascii
== table_c21
277 def in_table_c21(code):
278 return ord(code) < 128 and unicodedata.category(code) == "Cc"
281 # C.2.2 Non-ASCII control characters. It also includes
282 # a number of characters in category Cf.
283 name
, table_c22
= tables
[0]
285 assert name
== "C.2.2"
287 Cc_nonascii
= Cc
- Cc_ascii
288 table_c22
= Set(table_c22
.keys())
289 assert len(Cc_nonascii
- table_c22
) == 0
291 specials
= list(table_c22
- Cc_nonascii
)
294 print """c22_specials = """ + compact_set(specials
) + """
295 def in_table_c22(code):
297 if c < 128: return False
298 if unicodedata.category(code) == "Cc": return True
299 return c in c22_specials
301 def in_table_c21_c22(code):
302 return unicodedata.category(code) == "Cc" or \\
303 ord(code) in c22_specials
307 name
, table
= tables
[0]
311 Co
= Set(gen_category(["Co"]))
312 assert Set(table
.keys()) == Co
315 def in_table_c3(code):
316 return unicodedata.category(code) == "Co"
319 # C.4 Non-character code points, xFFFE, xFFFF
320 # plus process internal codes
321 name
, table
= tables
[0]
325 nonchar
= Set(range(0xFDD0,0xFDF0) +
326 range(0xFFFE,0x110000,0x10000) +
327 range(0xFFFF,0x110000,0x10000))
328 table
= Set(table
.keys())
329 assert table
== nonchar
332 def in_table_c4(code):
334 if c < 0xFDD0: return False
335 if c < 0xFDF0: return True
336 return (ord(code) & 0xFFFF) in (0xFFFE, 0xFFFF)
339 # C.5 Surrogate codes
340 name
, table
= tables
[0]
344 Cs
= Set(gen_category(["Cs"]))
345 assert Set(table
.keys()) == Cs
348 def in_table_c5(code):
349 return unicodedata.category(code) == "Cs"
352 # C.6 Inappropriate for plain text
353 name
, table
= tables
[0]
361 c6_set = """ + compact_set(table
) + """
362 def in_table_c6(code):
363 return ord(code) in c6_set
366 # C.7 Inappropriate for canonical representation
367 name
, table
= tables
[0]
375 c7_set = """ + compact_set(table
) + """
376 def in_table_c7(code):
377 return ord(code) in c7_set
380 # C.8 Change display properties or are deprecated
381 name
, table
= tables
[0]
389 c8_set = """ + compact_set(table
) + """
390 def in_table_c8(code):
391 return ord(code) in c8_set
394 # C.9 Tagging characters
395 name
, table
= tables
[0]
403 c9_set = """ + compact_set(table
) + """
404 def in_table_c9(code):
405 return ord(code) in c9_set
408 # D.1 Characters with bidirectional property "R" or "AL"
409 name
, table
= tables
[0]
413 RandAL
= Set(gen_bidirectional(["R","AL"]))
414 assert Set(table
.keys()) == RandAL
417 def in_table_d1(code):
418 return unicodedata.bidirectional(code) in ("R","AL")
421 # D.2 Characters with bidirectional property "L"
422 name
, table
= tables
[0]
426 L
= Set(gen_bidirectional(["L"]))
427 assert Set(table
.keys()) == L
430 def in_table_d2(code):
431 return unicodedata.bidirectional(code) == "L"