diff options
Diffstat (limited to 'Tools/unicode/mkstringprep.py')
-rw-r--r-- | Tools/unicode/mkstringprep.py | 137 |
1 files changed, 67 insertions, 70 deletions
diff --git a/Tools/unicode/mkstringprep.py b/Tools/unicode/mkstringprep.py index 4271883..99fcae7 100644 --- a/Tools/unicode/mkstringprep.py +++ b/Tools/unicode/mkstringprep.py @@ -1,14 +1,16 @@ -import re -from unicodedata import ucd_3_2_0 as unicodedata +import re, unicodedata, sys + +if sys.maxunicode == 65535: + raise RuntimeError("need UCS-4 Python") def gen_category(cats): for i in range(0, 0x110000): - if unicodedata.category(chr(i)) in cats: + if unicodedata.category(unichr(i)) in cats: yield(i) def gen_bidirectional(cats): for i in range(0, 0x110000): - if unicodedata.bidirectional(chr(i)) in cats: + if unicodedata.bidirectional(unichr(i)) in cats: yield(i) def compact_set(l): @@ -35,20 +37,16 @@ def compact_set(l): tuple.append((prev,prev+span+1)) else: single.append(prev) - if not single and len(tuple) == 1: - tuple = "range(%d,%d)" % tuple[0] - else: - tuple = " + ".join("list(range(%d,%d))" % t for t in tuple) + tuple = " + ".join(["range(%d,%d)" % t for t in tuple]) if not single: return "set(%s)" % tuple if not tuple: - return "set(%r)" % (single,) - return "set(%r + %s)" % (single, tuple) + return "set(%s)" % repr(single) + return "set(%s + %s)" % (repr(single),tuple) ############## Read the tables in the RFC ####################### -with open("rfc3454.txt") as f: - data = f.readlines() +data = open("rfc3454.txt").readlines() tables = [] curname = None @@ -57,7 +55,8 @@ for l in data: if not l: continue # Skip RFC page breaks - if l.startswith(("Hoffman & Blanchet", "RFC 3454")): + if l.startswith("Hoffman & Blanchet") or\ + l.startswith("RFC 3454"): continue # Find start/end lines m = re.match("----- (Start|End) Table ([A-Z](.[0-9])+) -----", l) @@ -72,8 +71,6 @@ for l in data: else: if not curname: raise RuntimeError("End without start", l) - if curname != m.group(2): - raise RuntimeError("Unexpected end", l) curname = None continue if not curname: @@ -109,17 +106,17 @@ for l in data: ########### Generate compact Python versions of the tables ############# -print("""# This file is generated by mkstringprep.py. DO NOT EDIT. +print """# This file is generated by mkstringprep.py. DO NOT EDIT. \"\"\"Library that exposes various tables found in the StringPrep RFC 3454. There are two kinds of tables: sets, for which a member test is provided, and mappings, for which a mapping function is provided. \"\"\" -from unicodedata import ucd_3_2_0 as unicodedata -""") +import unicodedata +""" -print("assert unicodedata.unidata_version == %r" % (unicodedata.unidata_version,)) +print "assert unicodedata.unidata_version == %s" % repr(unicodedata.unidata_version) # A.1 is the table of unassigned characters # XXX Plane 15 PUA is listed as unassigned in Python. @@ -137,24 +134,24 @@ Cn -= set(range(0xFFFF, 0x110000, 0x10000)) # assert table == Cn -print(""" +print """ def in_table_a1(code): if unicodedata.category(code) != 'Cn': return False c = ord(code) if 0xFDD0 <= c < 0xFDF0: return False return (c & 0xFFFF) not in (0xFFFE, 0xFFFF) -""") +""" # B.1 cannot easily be derived name, table = tables[0] del tables[0] assert name == "B.1" table = sorted(table.keys()) -print(""" +print """ b1_set = """ + compact_set(table) + """ def in_table_b1(code): return ord(code) in b1_set -""") +""" # B.2 and B.3 is case folding. # It takes CaseFolding.txt into account, which is @@ -176,25 +173,25 @@ assert name == "B.3" b3_exceptions = {} for k,v in table_b2.items(): - if list(map(ord, chr(k).lower())) != v: - b3_exceptions[k] = "".join(map(chr,v)) + if map(ord, unichr(k).lower()) != v: + b3_exceptions[k] = u"".join(map(unichr,v)) b3 = sorted(b3_exceptions.items()) -print(""" -b3_exceptions = {""") -for i, kv in enumerate(b3): - print("0x%x:%a," % kv, end=' ') +print """ +b3_exceptions = {""" +for i,(k,v) in enumerate(b3): + print "0x%x:%s," % (k, repr(v)), if i % 4 == 3: - print() -print("}") + print +print "}" -print(""" +print """ def map_table_b3(code): r = b3_exceptions.get(ord(code)) if r is not None: return r return code.lower() -""") +""" def map_table_b3(code): r = b3_exceptions.get(ord(code)) @@ -208,7 +205,7 @@ def map_table_b3(code): def map_table_b2(a): al = map_table_b3(a) b = unicodedata.normalize("NFKC", al) - bl = "".join([map_table_b3(ch) for ch in b]) + bl = u"".join([map_table_b3(ch) for ch in b]) c = unicodedata.normalize("NFKC", bl) if b != c: return c @@ -217,23 +214,23 @@ def map_table_b2(a): specials = {} for k,v in table_b2.items(): - if list(map(ord, map_table_b2(chr(k)))) != v: + if map(ord, map_table_b2(unichr(k))) != v: specials[k] = v # B.3 should not add any additional special cases assert specials == {} -print(""" +print """ def map_table_b2(a): al = map_table_b3(a) b = unicodedata.normalize("NFKC", al) - bl = "".join([map_table_b3(ch) for ch in b]) + bl = u"".join([map_table_b3(ch) for ch in b]) c = unicodedata.normalize("NFKC", bl) if b != c: return c else: return al -""") +""" # C.1.1 is a table with a single character name, table = tables[0] @@ -241,10 +238,10 @@ del tables[0] assert name == "C.1.1" assert table == {0x20:0x20} -print(""" +print """ def in_table_c11(code): - return code == " " -""") + return code == u" " +""" # C.1.2 is the rest of all space characters name, table = tables[0] @@ -252,16 +249,16 @@ del tables[0] assert name == "C.1.2" # table = set(table.keys()) -# Zs = set(gen_category(["Zs"])) - {0x20} +# Zs = set(gen_category(["Zs"])) - set([0x20]) # assert Zs == table -print(""" +print """ def in_table_c12(code): - return unicodedata.category(code) == "Zs" and code != " " + return unicodedata.category(code) == "Zs" and code != u" " def in_table_c11_c12(code): return unicodedata.category(code) == "Zs" -""") +""" # C.2.1 ASCII control characters name, table_c21 = tables[0] @@ -273,10 +270,10 @@ Cc_ascii = Cc & set(range(128)) table_c21 = set(table_c21.keys()) assert Cc_ascii == table_c21 -print(""" +print """ def in_table_c21(code): return ord(code) < 128 and unicodedata.category(code) == "Cc" -""") +""" # C.2.2 Non-ASCII control characters. It also includes # a number of characters in category Cf. @@ -291,7 +288,7 @@ assert len(Cc_nonascii - table_c22) == 0 specials = list(table_c22 - Cc_nonascii) specials.sort() -print("""c22_specials = """ + compact_set(specials) + """ +print """c22_specials = """ + compact_set(specials) + """ def in_table_c22(code): c = ord(code) if c < 128: return False @@ -301,7 +298,7 @@ def in_table_c22(code): def in_table_c21_c22(code): return unicodedata.category(code) == "Cc" or \\ ord(code) in c22_specials -""") +""" # C.3 Private use name, table = tables[0] @@ -311,10 +308,10 @@ assert name == "C.3" Co = set(gen_category(["Co"])) assert set(table.keys()) == Co -print(""" +print """ def in_table_c3(code): return unicodedata.category(code) == "Co" -""") +""" # C.4 Non-character code points, xFFFE, xFFFF # plus process internal codes @@ -322,19 +319,19 @@ name, table = tables[0] del tables[0] assert name == "C.4" -nonchar = set(range(0xFDD0,0xFDF0)) -nonchar.update(range(0xFFFE,0x110000,0x10000)) -nonchar.update(range(0xFFFF,0x110000,0x10000)) +nonchar = set(range(0xFDD0,0xFDF0) + + range(0xFFFE,0x110000,0x10000) + + range(0xFFFF,0x110000,0x10000)) table = set(table.keys()) assert table == nonchar -print(""" +print """ def in_table_c4(code): c = ord(code) if c < 0xFDD0: return False if c < 0xFDF0: return True return (ord(code) & 0xFFFF) in (0xFFFE, 0xFFFF) -""") +""" # C.5 Surrogate codes name, table = tables[0] @@ -344,10 +341,10 @@ assert name == "C.5" Cs = set(gen_category(["Cs"])) assert set(table.keys()) == Cs -print(""" +print """ def in_table_c5(code): return unicodedata.category(code) == "Cs" -""") +""" # C.6 Inappropriate for plain text name, table = tables[0] @@ -356,11 +353,11 @@ assert name == "C.6" table = sorted(table.keys()) -print(""" +print """ c6_set = """ + compact_set(table) + """ def in_table_c6(code): return ord(code) in c6_set -""") +""" # C.7 Inappropriate for canonical representation name, table = tables[0] @@ -369,11 +366,11 @@ assert name == "C.7" table = sorted(table.keys()) -print(""" +print """ c7_set = """ + compact_set(table) + """ def in_table_c7(code): return ord(code) in c7_set -""") +""" # C.8 Change display properties or are deprecated name, table = tables[0] @@ -382,11 +379,11 @@ assert name == "C.8" table = sorted(table.keys()) -print(""" +print """ c8_set = """ + compact_set(table) + """ def in_table_c8(code): return ord(code) in c8_set -""") +""" # C.9 Tagging characters name, table = tables[0] @@ -395,11 +392,11 @@ assert name == "C.9" table = sorted(table.keys()) -print(""" +print """ c9_set = """ + compact_set(table) + """ def in_table_c9(code): return ord(code) in c9_set -""") +""" # D.1 Characters with bidirectional property "R" or "AL" name, table = tables[0] @@ -409,10 +406,10 @@ assert name == "D.1" RandAL = set(gen_bidirectional(["R","AL"])) assert set(table.keys()) == RandAL -print(""" +print """ def in_table_d1(code): return unicodedata.bidirectional(code) in ("R","AL") -""") +""" # D.2 Characters with bidirectional property "L" name, table = tables[0] @@ -422,7 +419,7 @@ assert name == "D.2" L = set(gen_bidirectional(["L"])) assert set(table.keys()) == L -print(""" +print """ def in_table_d2(code): return unicodedata.bidirectional(code) == "L" -""") +""" |