2000-09-24 20:18:31 -03:00
|
|
|
#
|
2000-09-25 14:59:57 -03:00
|
|
|
# (re)generate unicode property and type databases
|
|
|
|
#
|
2002-11-25 05:13:37 -04:00
|
|
|
# this script converts a unicode 3.2 database file to
|
2001-01-21 13:01:31 -04:00
|
|
|
# Modules/unicodedata_db.h, Modules/unicodename_db.h,
|
|
|
|
# and Objects/unicodetype_db.h
|
2000-09-25 05:07:06 -03:00
|
|
|
#
|
|
|
|
# history:
|
|
|
|
# 2000-09-24 fl created (based on bits and pieces from unidb)
|
|
|
|
# 2000-09-25 fl merged tim's splitbin fixes, separate decomposition table
|
2000-09-25 14:59:57 -03:00
|
|
|
# 2000-09-25 fl added character type table
|
2001-01-21 13:01:31 -04:00
|
|
|
# 2000-09-26 fl added LINEBREAK, DECIMAL, and DIGIT flags/fields (2.0)
|
2000-11-03 16:24:15 -04:00
|
|
|
# 2000-11-03 fl expand first/last ranges
|
2001-01-21 13:01:31 -04:00
|
|
|
# 2001-01-19 fl added character name tables (2.1)
|
2001-01-21 18:41:08 -04:00
|
|
|
# 2001-01-21 fl added decomp compression; dynamic phrasebook threshold
|
2002-11-23 18:08:15 -04:00
|
|
|
# 2002-09-11 wd use string methods
|
|
|
|
# 2002-10-18 mvl update to Unicode 3.2
|
|
|
|
# 2002-10-22 mvl generate NFC tables
|
2002-11-24 19:05:09 -04:00
|
|
|
# 2002-11-24 mvl expand all ranges, sort names version-independently
|
2002-11-25 05:13:37 -04:00
|
|
|
# 2002-11-25 mvl add UNIDATA_VERSION
|
2004-06-02 13:49:17 -03:00
|
|
|
# 2004-05-29 perky add east asian width information
|
2006-03-11 08:43:44 -04:00
|
|
|
# 2006-03-10 mvl update to Unicode 4.1; add UCD 3.2 delta
|
2008-07-04 12:55:02 -03:00
|
|
|
# 2008-06-11 gb add PRINTABLE_MASK for Atsuo Ishimoto's ascii() patch
|
2011-10-21 15:57:36 -03:00
|
|
|
# 2011-10-21 ezio add support for name aliases and named sequences
|
2012-01-11 19:17:06 -04:00
|
|
|
# 2012-01 benjamin add full case mappings
|
2000-09-25 05:07:06 -03:00
|
|
|
#
|
2001-01-21 18:41:08 -04:00
|
|
|
# written by Fredrik Lundh (fredrik@pythonware.com)
|
2000-09-24 20:18:31 -03:00
|
|
|
#
|
|
|
|
|
2011-10-21 15:57:36 -03:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
from textwrap import dedent
|
2000-09-24 20:18:31 -03:00
|
|
|
|
|
|
|
SCRIPT = sys.argv[0]
|
2010-08-18 17:44:58 -03:00
|
|
|
VERSION = "3.2"
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2002-11-25 05:13:37 -04:00
|
|
|
# The Unicode Database
|
2014-07-06 17:04:20 -03:00
|
|
|
UNIDATA_VERSION = "7.0.0"
|
2006-03-09 19:38:20 -04:00
|
|
|
UNICODE_DATA = "UnicodeData%s.txt"
|
|
|
|
COMPOSITION_EXCLUSIONS = "CompositionExclusions%s.txt"
|
|
|
|
EASTASIAN_WIDTH = "EastAsianWidth%s.txt"
|
2010-10-11 19:42:28 -03:00
|
|
|
UNIHAN = "Unihan%s.zip"
|
2007-08-14 19:37:03 -03:00
|
|
|
DERIVED_CORE_PROPERTIES = "DerivedCoreProperties%s.txt"
|
2009-04-27 19:31:40 -03:00
|
|
|
DERIVEDNORMALIZATION_PROPS = "DerivedNormalizationProps%s.txt"
|
2010-03-30 16:34:18 -03:00
|
|
|
LINE_BREAK = "LineBreak%s.txt"
|
2011-10-21 15:57:36 -03:00
|
|
|
NAME_ALIASES = "NameAliases%s.txt"
|
|
|
|
NAMED_SEQUENCES = "NamedSequences%s.txt"
|
2012-01-11 19:17:06 -04:00
|
|
|
SPECIAL_CASING = "SpecialCasing%s.txt"
|
2012-01-14 14:23:30 -04:00
|
|
|
CASE_FOLDING = "CaseFolding%s.txt"
|
2011-10-21 15:57:36 -03:00
|
|
|
|
|
|
|
# Private Use Areas -- in planes 1, 15, 16
|
|
|
|
PUA_1 = range(0xE000, 0xF900)
|
|
|
|
PUA_15 = range(0xF0000, 0xFFFFE)
|
|
|
|
PUA_16 = range(0x100000, 0x10FFFE)
|
|
|
|
|
|
|
|
# we use this ranges of PUA_15 to store name aliases and named sequences
|
|
|
|
NAME_ALIASES_START = 0xF0000
|
2012-02-20 23:24:29 -04:00
|
|
|
NAMED_SEQUENCES_START = 0xF0200
|
2006-03-09 19:38:20 -04:00
|
|
|
|
|
|
|
old_versions = ["3.2.0"]
|
2000-09-24 20:18:31 -03:00
|
|
|
|
|
|
|
CATEGORY_NAMES = [ "Cn", "Lu", "Ll", "Lt", "Mn", "Mc", "Me", "Nd",
|
|
|
|
"Nl", "No", "Zs", "Zl", "Zp", "Cc", "Cf", "Cs", "Co", "Cn", "Lm",
|
|
|
|
"Lo", "Pc", "Pd", "Ps", "Pe", "Pi", "Pf", "Po", "Sm", "Sc", "Sk",
|
|
|
|
"So" ]
|
|
|
|
|
|
|
|
BIDIRECTIONAL_NAMES = [ "", "L", "LRE", "LRO", "R", "AL", "RLE", "RLO",
|
|
|
|
"PDF", "EN", "ES", "ET", "AN", "CS", "NSM", "BN", "B", "S", "WS",
|
2013-10-10 18:24:45 -03:00
|
|
|
"ON", "LRI", "RLI", "FSI", "PDI" ]
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2004-08-04 04:38:35 -03:00
|
|
|
EASTASIANWIDTH_NAMES = [ "F", "H", "W", "Na", "A", "N" ]
|
|
|
|
|
2010-03-30 16:34:18 -03:00
|
|
|
MANDATORY_LINE_BREAKS = [ "BK", "CR", "LF", "NL" ]
|
|
|
|
|
2000-09-25 18:01:56 -03:00
|
|
|
# note: should match definitions in Objects/unicodectype.c
|
2000-09-25 14:59:57 -03:00
|
|
|
ALPHA_MASK = 0x01
|
|
|
|
DECIMAL_MASK = 0x02
|
|
|
|
DIGIT_MASK = 0x04
|
|
|
|
LOWER_MASK = 0x08
|
2000-09-25 18:01:56 -03:00
|
|
|
LINEBREAK_MASK = 0x10
|
2000-09-25 14:59:57 -03:00
|
|
|
SPACE_MASK = 0x20
|
|
|
|
TITLE_MASK = 0x40
|
|
|
|
UPPER_MASK = 0x80
|
2007-08-14 19:37:03 -03:00
|
|
|
XID_START_MASK = 0x100
|
|
|
|
XID_CONTINUE_MASK = 0x200
|
2008-07-04 12:55:02 -03:00
|
|
|
PRINTABLE_MASK = 0x400
|
2012-01-11 19:17:06 -04:00
|
|
|
NUMERIC_MASK = 0x800
|
|
|
|
CASE_IGNORABLE_MASK = 0x1000
|
|
|
|
CASED_MASK = 0x2000
|
|
|
|
EXTENDED_CASE_MASK = 0x4000
|
2000-09-25 14:59:57 -03:00
|
|
|
|
2010-11-22 05:00:02 -04:00
|
|
|
# these ranges need to match unicodedata.c:is_unified_ideograph
|
|
|
|
cjk_ranges = [
|
|
|
|
('3400', '4DB5'),
|
2012-02-20 23:24:29 -04:00
|
|
|
('4E00', '9FCC'),
|
2010-11-22 05:00:02 -04:00
|
|
|
('20000', '2A6D6'),
|
|
|
|
('2A700', '2B734'),
|
|
|
|
('2B740', '2B81D')
|
|
|
|
]
|
|
|
|
|
2000-11-03 16:24:15 -04:00
|
|
|
def maketables(trace=0):
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("--- Reading", UNICODE_DATA % "", "...")
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2006-03-09 19:38:20 -04:00
|
|
|
version = ""
|
2010-10-11 19:42:28 -03:00
|
|
|
unicode = UnicodeData(UNIDATA_VERSION)
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2008-06-11 15:37:52 -03:00
|
|
|
print(len(list(filter(None, unicode.table))), "characters")
|
2000-11-03 16:24:15 -04:00
|
|
|
|
2006-03-09 19:38:20 -04:00
|
|
|
for version in old_versions:
|
2007-08-03 14:06:41 -03:00
|
|
|
print("--- Reading", UNICODE_DATA % ("-"+version), "...")
|
2010-11-22 05:00:02 -04:00
|
|
|
old_unicode = UnicodeData(version, cjk_check=False)
|
2008-06-11 15:37:52 -03:00
|
|
|
print(len(list(filter(None, old_unicode.table))), "characters")
|
2006-03-09 19:38:20 -04:00
|
|
|
merge_old_version(version, unicode, old_unicode)
|
|
|
|
|
2001-01-21 19:31:52 -04:00
|
|
|
makeunicodename(unicode, trace)
|
2001-01-21 13:01:31 -04:00
|
|
|
makeunicodedata(unicode, trace)
|
2001-01-21 19:31:52 -04:00
|
|
|
makeunicodetype(unicode, trace)
|
2001-01-21 13:01:31 -04:00
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# unicode character properties
|
|
|
|
|
|
|
|
def makeunicodedata(unicode, trace):
|
|
|
|
|
2009-04-27 19:31:40 -03:00
|
|
|
dummy = (0, 0, 0, 0, 0, 0)
|
2000-09-24 20:18:31 -03:00
|
|
|
table = [dummy]
|
|
|
|
cache = {0: dummy}
|
|
|
|
index = [0] * len(unicode.chars)
|
|
|
|
|
2001-01-21 13:01:31 -04:00
|
|
|
FILE = "Modules/unicodedata_db.h"
|
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("--- Preparing", FILE, "...")
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2000-09-25 05:07:06 -03:00
|
|
|
# 1) database properties
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2000-09-24 20:18:31 -03:00
|
|
|
for char in unicode.chars:
|
|
|
|
record = unicode.table[char]
|
|
|
|
if record:
|
|
|
|
# extract database properties
|
|
|
|
category = CATEGORY_NAMES.index(record[2])
|
|
|
|
combining = int(record[3])
|
|
|
|
bidirectional = BIDIRECTIONAL_NAMES.index(record[4])
|
|
|
|
mirrored = record[9] == "Y"
|
2004-08-04 04:38:35 -03:00
|
|
|
eastasianwidth = EASTASIANWIDTH_NAMES.index(record[15])
|
2009-04-27 19:31:40 -03:00
|
|
|
normalizationquickcheck = record[17]
|
2000-09-24 20:18:31 -03:00
|
|
|
item = (
|
2009-04-27 19:31:40 -03:00
|
|
|
category, combining, bidirectional, mirrored, eastasianwidth,
|
|
|
|
normalizationquickcheck
|
2000-09-24 20:18:31 -03:00
|
|
|
)
|
|
|
|
# add entry to index and item tables
|
|
|
|
i = cache.get(item)
|
|
|
|
if i is None:
|
|
|
|
cache[item] = i = len(table)
|
|
|
|
table.append(item)
|
|
|
|
index[char] = i
|
|
|
|
|
2000-09-25 05:07:06 -03:00
|
|
|
# 2) decomposition data
|
|
|
|
|
2001-01-21 18:41:08 -04:00
|
|
|
decomp_data = [0]
|
|
|
|
decomp_prefix = [""]
|
2000-09-25 05:07:06 -03:00
|
|
|
decomp_index = [0] * len(unicode.chars)
|
2001-01-21 18:41:08 -04:00
|
|
|
decomp_size = 0
|
2000-09-25 05:07:06 -03:00
|
|
|
|
2002-11-23 18:08:15 -04:00
|
|
|
comp_pairs = []
|
|
|
|
comp_first = [None] * len(unicode.chars)
|
|
|
|
comp_last = [None] * len(unicode.chars)
|
|
|
|
|
2000-09-25 05:07:06 -03:00
|
|
|
for char in unicode.chars:
|
|
|
|
record = unicode.table[char]
|
|
|
|
if record:
|
|
|
|
if record[5]:
|
2002-09-11 17:36:02 -03:00
|
|
|
decomp = record[5].split()
|
2006-03-09 19:38:20 -04:00
|
|
|
if len(decomp) > 19:
|
2007-08-22 20:05:06 -03:00
|
|
|
raise Exception("character %x has a decomposition too large for nfd_nfkd" % char)
|
2001-01-21 18:41:08 -04:00
|
|
|
# prefix
|
|
|
|
if decomp[0][0] == "<":
|
|
|
|
prefix = decomp.pop(0)
|
|
|
|
else:
|
|
|
|
prefix = ""
|
|
|
|
try:
|
|
|
|
i = decomp_prefix.index(prefix)
|
|
|
|
except ValueError:
|
|
|
|
i = len(decomp_prefix)
|
|
|
|
decomp_prefix.append(prefix)
|
|
|
|
prefix = i
|
|
|
|
assert prefix < 256
|
|
|
|
# content
|
2008-05-16 14:02:34 -03:00
|
|
|
decomp = [prefix + (len(decomp)<<8)] + [int(s, 16) for s in decomp]
|
2002-11-23 18:08:15 -04:00
|
|
|
# Collect NFC pairs
|
|
|
|
if not prefix and len(decomp) == 3 and \
|
|
|
|
char not in unicode.exclusions and \
|
|
|
|
unicode.table[decomp[1]][3] == "0":
|
|
|
|
p, l, r = decomp
|
|
|
|
comp_first[l] = 1
|
|
|
|
comp_last[r] = 1
|
|
|
|
comp_pairs.append((l,r,char))
|
2000-09-25 05:07:06 -03:00
|
|
|
try:
|
2001-01-21 18:41:08 -04:00
|
|
|
i = decomp_data.index(decomp)
|
2000-09-25 05:07:06 -03:00
|
|
|
except ValueError:
|
|
|
|
i = len(decomp_data)
|
2001-01-21 18:41:08 -04:00
|
|
|
decomp_data.extend(decomp)
|
|
|
|
decomp_size = decomp_size + len(decomp) * 2
|
2000-09-25 05:07:06 -03:00
|
|
|
else:
|
|
|
|
i = 0
|
|
|
|
decomp_index[char] = i
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2002-11-23 18:08:15 -04:00
|
|
|
f = l = 0
|
|
|
|
comp_first_ranges = []
|
|
|
|
comp_last_ranges = []
|
|
|
|
prev_f = prev_l = None
|
|
|
|
for i in unicode.chars:
|
|
|
|
if comp_first[i] is not None:
|
|
|
|
comp_first[i] = f
|
|
|
|
f += 1
|
|
|
|
if prev_f is None:
|
|
|
|
prev_f = (i,i)
|
|
|
|
elif prev_f[1]+1 == i:
|
|
|
|
prev_f = prev_f[0],i
|
|
|
|
else:
|
|
|
|
comp_first_ranges.append(prev_f)
|
|
|
|
prev_f = (i,i)
|
|
|
|
if comp_last[i] is not None:
|
|
|
|
comp_last[i] = l
|
|
|
|
l += 1
|
|
|
|
if prev_l is None:
|
|
|
|
prev_l = (i,i)
|
|
|
|
elif prev_l[1]+1 == i:
|
|
|
|
prev_l = prev_l[0],i
|
|
|
|
else:
|
|
|
|
comp_last_ranges.append(prev_l)
|
|
|
|
prev_l = (i,i)
|
|
|
|
comp_first_ranges.append(prev_f)
|
|
|
|
comp_last_ranges.append(prev_l)
|
|
|
|
total_first = f
|
|
|
|
total_last = l
|
|
|
|
|
|
|
|
comp_data = [0]*(total_first*total_last)
|
|
|
|
for f,l,char in comp_pairs:
|
|
|
|
f = comp_first[f]
|
|
|
|
l = comp_last[l]
|
|
|
|
comp_data[f*total_last+l] = char
|
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print(len(table), "unique properties")
|
|
|
|
print(len(decomp_prefix), "unique decomposition prefixes")
|
|
|
|
print(len(decomp_data), "unique decomposition entries:", end=' ')
|
|
|
|
print(decomp_size, "bytes")
|
|
|
|
print(total_first, "first characters in NFC")
|
|
|
|
print(total_last, "last characters in NFC")
|
|
|
|
print(len(comp_pairs), "NFC pairs")
|
2000-11-03 16:24:15 -04:00
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("--- Writing", FILE, "...")
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2000-10-26 00:56:46 -03:00
|
|
|
fp = open(FILE, "w")
|
2007-08-03 14:06:41 -03:00
|
|
|
print("/* this file was generated by %s %s */" % (SCRIPT, VERSION), file=fp)
|
|
|
|
print(file=fp)
|
|
|
|
print('#define UNIDATA_VERSION "%s"' % UNIDATA_VERSION, file=fp)
|
|
|
|
print("/* a list of unique database records */", file=fp)
|
|
|
|
print("const _PyUnicode_DatabaseRecord _PyUnicode_Database_Records[] = {", file=fp)
|
2000-09-24 20:18:31 -03:00
|
|
|
for item in table:
|
2009-04-27 19:31:40 -03:00
|
|
|
print(" {%d, %d, %d, %d, %d, %d}," % item, file=fp)
|
2007-08-03 14:06:41 -03:00
|
|
|
print("};", file=fp)
|
|
|
|
print(file=fp)
|
|
|
|
|
|
|
|
print("/* Reindexing of NFC first characters. */", file=fp)
|
|
|
|
print("#define TOTAL_FIRST",total_first, file=fp)
|
|
|
|
print("#define TOTAL_LAST",total_last, file=fp)
|
|
|
|
print("struct reindex{int start;short count,index;};", file=fp)
|
2008-06-13 04:50:45 -03:00
|
|
|
print("static struct reindex nfc_first[] = {", file=fp)
|
2002-11-23 18:08:15 -04:00
|
|
|
for start,end in comp_first_ranges:
|
2007-08-03 14:06:41 -03:00
|
|
|
print(" { %d, %d, %d}," % (start,end-start,comp_first[start]), file=fp)
|
|
|
|
print(" {0,0,0}", file=fp)
|
|
|
|
print("};\n", file=fp)
|
2008-06-13 04:50:45 -03:00
|
|
|
print("static struct reindex nfc_last[] = {", file=fp)
|
2002-11-23 18:08:15 -04:00
|
|
|
for start,end in comp_last_ranges:
|
2007-08-03 14:06:41 -03:00
|
|
|
print(" { %d, %d, %d}," % (start,end-start,comp_last[start]), file=fp)
|
|
|
|
print(" {0,0,0}", file=fp)
|
|
|
|
print("};\n", file=fp)
|
2002-11-23 18:08:15 -04:00
|
|
|
|
2001-01-21 13:01:31 -04:00
|
|
|
# FIXME: <fl> the following tables could be made static, and
|
2000-09-25 14:59:57 -03:00
|
|
|
# the support code moved into unicodedatabase.c
|
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("/* string literals */", file=fp)
|
|
|
|
print("const char *_PyUnicode_CategoryNames[] = {", file=fp)
|
2000-09-24 20:18:31 -03:00
|
|
|
for name in CATEGORY_NAMES:
|
2007-08-03 14:06:41 -03:00
|
|
|
print(" \"%s\"," % name, file=fp)
|
|
|
|
print(" NULL", file=fp)
|
|
|
|
print("};", file=fp)
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("const char *_PyUnicode_BidirectionalNames[] = {", file=fp)
|
2000-09-24 20:18:31 -03:00
|
|
|
for name in BIDIRECTIONAL_NAMES:
|
2007-08-03 14:06:41 -03:00
|
|
|
print(" \"%s\"," % name, file=fp)
|
|
|
|
print(" NULL", file=fp)
|
|
|
|
print("};", file=fp)
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("const char *_PyUnicode_EastAsianWidthNames[] = {", file=fp)
|
2004-08-04 04:38:35 -03:00
|
|
|
for name in EASTASIANWIDTH_NAMES:
|
2007-08-03 14:06:41 -03:00
|
|
|
print(" \"%s\"," % name, file=fp)
|
|
|
|
print(" NULL", file=fp)
|
|
|
|
print("};", file=fp)
|
2004-08-04 04:38:35 -03:00
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("static const char *decomp_prefix[] = {", file=fp)
|
2001-01-21 18:41:08 -04:00
|
|
|
for name in decomp_prefix:
|
2007-08-03 14:06:41 -03:00
|
|
|
print(" \"%s\"," % name, file=fp)
|
|
|
|
print(" NULL", file=fp)
|
|
|
|
print("};", file=fp)
|
2000-09-25 05:07:06 -03:00
|
|
|
|
2000-09-25 14:59:57 -03:00
|
|
|
# split record index table
|
2000-11-03 16:24:15 -04:00
|
|
|
index1, index2, shift = splitbins(index, trace)
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("/* index tables for the database records */", file=fp)
|
|
|
|
print("#define SHIFT", shift, file=fp)
|
2001-01-21 18:41:08 -04:00
|
|
|
Array("index1", index1).dump(fp, trace)
|
|
|
|
Array("index2", index2).dump(fp, trace)
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2000-09-25 14:59:57 -03:00
|
|
|
# split decomposition index table
|
2000-11-03 16:24:15 -04:00
|
|
|
index1, index2, shift = splitbins(decomp_index, trace)
|
2000-09-25 05:07:06 -03:00
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("/* decomposition data */", file=fp)
|
2001-01-21 18:41:08 -04:00
|
|
|
Array("decomp_data", decomp_data).dump(fp, trace)
|
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("/* index tables for the decomposition data */", file=fp)
|
|
|
|
print("#define DECOMP_SHIFT", shift, file=fp)
|
2001-01-21 18:41:08 -04:00
|
|
|
Array("decomp_index1", index1).dump(fp, trace)
|
|
|
|
Array("decomp_index2", index2).dump(fp, trace)
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2002-11-23 18:08:15 -04:00
|
|
|
index, index2, shift = splitbins(comp_data, trace)
|
2007-08-03 14:06:41 -03:00
|
|
|
print("/* NFC pairs */", file=fp)
|
|
|
|
print("#define COMP_SHIFT", shift, file=fp)
|
2002-11-23 18:08:15 -04:00
|
|
|
Array("comp_index", index).dump(fp, trace)
|
|
|
|
Array("comp_data", index2).dump(fp, trace)
|
|
|
|
|
2006-03-09 19:38:20 -04:00
|
|
|
# Generate delta tables for old versions
|
|
|
|
for version, table, normalization in unicode.changed:
|
|
|
|
cversion = version.replace(".","_")
|
|
|
|
records = [table[0]]
|
|
|
|
cache = {table[0]:0}
|
|
|
|
index = [0] * len(table)
|
|
|
|
for i, record in enumerate(table):
|
|
|
|
try:
|
|
|
|
index[i] = cache[record]
|
|
|
|
except KeyError:
|
|
|
|
index[i] = cache[record] = len(records)
|
|
|
|
records.append(record)
|
|
|
|
index1, index2, shift = splitbins(index, trace)
|
2007-08-03 14:06:41 -03:00
|
|
|
print("static const change_record change_records_%s[] = {" % cversion, file=fp)
|
2006-03-09 19:38:20 -04:00
|
|
|
for record in records:
|
2007-08-03 14:06:41 -03:00
|
|
|
print("\t{ %s }," % ", ".join(map(str,record)), file=fp)
|
|
|
|
print("};", file=fp)
|
2006-03-09 19:38:20 -04:00
|
|
|
Array("changes_%s_index" % cversion, index1).dump(fp, trace)
|
|
|
|
Array("changes_%s_data" % cversion, index2).dump(fp, trace)
|
2007-08-03 14:06:41 -03:00
|
|
|
print("static const change_record* get_change_%s(Py_UCS4 n)" % cversion, file=fp)
|
|
|
|
print("{", file=fp)
|
|
|
|
print("\tint index;", file=fp)
|
|
|
|
print("\tif (n >= 0x110000) index = 0;", file=fp)
|
|
|
|
print("\telse {", file=fp)
|
|
|
|
print("\t\tindex = changes_%s_index[n>>%d];" % (cversion, shift), file=fp)
|
|
|
|
print("\t\tindex = changes_%s_data[(index<<%d)+(n & %d)];" % \
|
|
|
|
(cversion, shift, ((1<<shift)-1)), file=fp)
|
|
|
|
print("\t}", file=fp)
|
|
|
|
print("\treturn change_records_%s+index;" % cversion, file=fp)
|
|
|
|
print("}\n", file=fp)
|
|
|
|
print("static Py_UCS4 normalization_%s(Py_UCS4 n)" % cversion, file=fp)
|
|
|
|
print("{", file=fp)
|
|
|
|
print("\tswitch(n) {", file=fp)
|
2006-03-09 19:38:20 -04:00
|
|
|
for k, v in normalization:
|
2007-08-03 14:06:41 -03:00
|
|
|
print("\tcase %s: return 0x%s;" % (hex(k), v), file=fp)
|
|
|
|
print("\tdefault: return 0;", file=fp)
|
|
|
|
print("\t}\n}\n", file=fp)
|
2006-03-09 19:38:20 -04:00
|
|
|
|
2001-01-21 13:01:31 -04:00
|
|
|
fp.close()
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# unicode character type tables
|
|
|
|
|
|
|
|
def makeunicodetype(unicode, trace):
|
|
|
|
|
|
|
|
FILE = "Objects/unicodetype_db.h"
|
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("--- Preparing", FILE, "...")
|
2000-09-25 14:59:57 -03:00
|
|
|
|
|
|
|
# extract unicode types
|
2000-09-25 18:01:56 -03:00
|
|
|
dummy = (0, 0, 0, 0, 0, 0)
|
2000-09-25 14:59:57 -03:00
|
|
|
table = [dummy]
|
|
|
|
cache = {0: dummy}
|
|
|
|
index = [0] * len(unicode.chars)
|
2009-10-06 18:03:20 -03:00
|
|
|
numeric = {}
|
|
|
|
spaces = []
|
|
|
|
linebreaks = []
|
2012-01-11 19:17:06 -04:00
|
|
|
extra_casing = []
|
2000-09-25 14:59:57 -03:00
|
|
|
|
|
|
|
for char in unicode.chars:
|
|
|
|
record = unicode.table[char]
|
|
|
|
if record:
|
|
|
|
# extract database properties
|
|
|
|
category = record[2]
|
|
|
|
bidirectional = record[4]
|
2007-08-14 19:37:03 -03:00
|
|
|
properties = record[16]
|
2000-09-25 14:59:57 -03:00
|
|
|
flags = 0
|
2008-09-10 11:08:48 -03:00
|
|
|
delta = True
|
2000-09-25 14:59:57 -03:00
|
|
|
if category in ["Lm", "Lt", "Lu", "Ll", "Lo"]:
|
|
|
|
flags |= ALPHA_MASK
|
2012-01-11 19:17:06 -04:00
|
|
|
if "Lowercase" in properties:
|
2000-09-25 14:59:57 -03:00
|
|
|
flags |= LOWER_MASK
|
2010-03-30 16:34:18 -03:00
|
|
|
if 'Line_Break' in properties or bidirectional == "B":
|
2000-09-25 18:01:56 -03:00
|
|
|
flags |= LINEBREAK_MASK
|
2009-10-06 18:03:20 -03:00
|
|
|
linebreaks.append(char)
|
2000-09-25 14:59:57 -03:00
|
|
|
if category == "Zs" or bidirectional in ("WS", "B", "S"):
|
|
|
|
flags |= SPACE_MASK
|
2009-10-06 18:03:20 -03:00
|
|
|
spaces.append(char)
|
2000-09-25 20:03:34 -03:00
|
|
|
if category == "Lt":
|
2000-09-25 14:59:57 -03:00
|
|
|
flags |= TITLE_MASK
|
2012-01-11 19:17:06 -04:00
|
|
|
if "Uppercase" in properties:
|
2000-09-25 14:59:57 -03:00
|
|
|
flags |= UPPER_MASK
|
2009-03-26 14:15:46 -03:00
|
|
|
if char == ord(" ") or category[0] not in ("C", "Z"):
|
2008-07-04 12:55:02 -03:00
|
|
|
flags |= PRINTABLE_MASK
|
2007-08-14 19:37:03 -03:00
|
|
|
if "XID_Start" in properties:
|
|
|
|
flags |= XID_START_MASK
|
|
|
|
if "XID_Continue" in properties:
|
|
|
|
flags |= XID_CONTINUE_MASK
|
2012-01-11 19:17:06 -04:00
|
|
|
if "Cased" in properties:
|
|
|
|
flags |= CASED_MASK
|
|
|
|
if "Case_Ignorable" in properties:
|
|
|
|
flags |= CASE_IGNORABLE_MASK
|
|
|
|
sc = unicode.special_casing.get(char)
|
2012-01-14 14:23:30 -04:00
|
|
|
cf = unicode.case_folding.get(char, [char])
|
|
|
|
if record[12]:
|
|
|
|
upper = int(record[12], 16)
|
|
|
|
else:
|
|
|
|
upper = char
|
|
|
|
if record[13]:
|
|
|
|
lower = int(record[13], 16)
|
|
|
|
else:
|
|
|
|
lower = char
|
|
|
|
if record[14]:
|
|
|
|
title = int(record[14], 16)
|
|
|
|
else:
|
|
|
|
title = upper
|
|
|
|
if sc is None and cf != [lower]:
|
|
|
|
sc = ([lower], [title], [upper])
|
2012-01-11 19:17:06 -04:00
|
|
|
if sc is None:
|
|
|
|
if upper == lower == title:
|
|
|
|
upper = lower = title = 0
|
2012-01-15 22:19:20 -04:00
|
|
|
else:
|
|
|
|
upper = upper - char
|
|
|
|
lower = lower - char
|
|
|
|
title = title - char
|
|
|
|
assert (abs(upper) <= 2147483647 and
|
|
|
|
abs(lower) <= 2147483647 and
|
|
|
|
abs(title) <= 2147483647)
|
2000-09-25 14:59:57 -03:00
|
|
|
else:
|
2012-01-14 14:23:30 -04:00
|
|
|
# This happens either when some character maps to more than one
|
|
|
|
# character in uppercase, lowercase, or titlecase or the
|
|
|
|
# casefolded version of the character is different from the
|
|
|
|
# lowercase. The extra characters are stored in a different
|
|
|
|
# array.
|
2012-01-11 19:17:06 -04:00
|
|
|
flags |= EXTENDED_CASE_MASK
|
|
|
|
lower = len(extra_casing) | (len(sc[0]) << 24)
|
|
|
|
extra_casing.extend(sc[0])
|
2012-01-14 14:23:30 -04:00
|
|
|
if cf != sc[0]:
|
|
|
|
lower |= len(cf) << 20
|
|
|
|
extra_casing.extend(cf)
|
2012-01-11 19:17:06 -04:00
|
|
|
upper = len(extra_casing) | (len(sc[2]) << 24)
|
|
|
|
extra_casing.extend(sc[2])
|
|
|
|
# Title is probably equal to upper.
|
|
|
|
if sc[1] == sc[2]:
|
|
|
|
title = upper
|
|
|
|
else:
|
|
|
|
title = len(extra_casing) | (len(sc[1]) << 24)
|
|
|
|
extra_casing.extend(sc[1])
|
2000-09-25 18:01:56 -03:00
|
|
|
# decimal digit, integer digit
|
|
|
|
decimal = 0
|
|
|
|
if record[6]:
|
|
|
|
flags |= DECIMAL_MASK
|
|
|
|
decimal = int(record[6])
|
|
|
|
digit = 0
|
|
|
|
if record[7]:
|
|
|
|
flags |= DIGIT_MASK
|
|
|
|
digit = int(record[7])
|
2009-10-06 18:03:20 -03:00
|
|
|
if record[8]:
|
|
|
|
flags |= NUMERIC_MASK
|
|
|
|
numeric.setdefault(record[8], []).append(char)
|
2000-09-25 14:59:57 -03:00
|
|
|
item = (
|
2004-06-02 13:49:17 -03:00
|
|
|
upper, lower, title, decimal, digit, flags
|
2000-09-25 14:59:57 -03:00
|
|
|
)
|
|
|
|
# add entry to index and item tables
|
|
|
|
i = cache.get(item)
|
|
|
|
if i is None:
|
|
|
|
cache[item] = i = len(table)
|
|
|
|
table.append(item)
|
|
|
|
index[char] = i
|
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print(len(table), "unique character type entries")
|
2009-10-06 18:03:20 -03:00
|
|
|
print(sum(map(len, numeric.values())), "numeric code points")
|
|
|
|
print(len(spaces), "whitespace code points")
|
|
|
|
print(len(linebreaks), "linebreak code points")
|
2012-01-11 19:17:06 -04:00
|
|
|
print(len(extra_casing), "extended case array")
|
2000-09-25 14:59:57 -03:00
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("--- Writing", FILE, "...")
|
2000-11-03 16:24:15 -04:00
|
|
|
|
2001-01-21 13:01:31 -04:00
|
|
|
fp = open(FILE, "w")
|
2007-08-03 14:06:41 -03:00
|
|
|
print("/* this file was generated by %s %s */" % (SCRIPT, VERSION), file=fp)
|
|
|
|
print(file=fp)
|
|
|
|
print("/* a list of unique character type descriptors */", file=fp)
|
|
|
|
print("const _PyUnicode_TypeRecord _PyUnicode_TypeRecords[] = {", file=fp)
|
2000-09-25 14:59:57 -03:00
|
|
|
for item in table:
|
2007-08-03 14:06:41 -03:00
|
|
|
print(" {%d, %d, %d, %d, %d, %d}," % item, file=fp)
|
|
|
|
print("};", file=fp)
|
|
|
|
print(file=fp)
|
2000-09-25 14:59:57 -03:00
|
|
|
|
2012-01-11 19:17:06 -04:00
|
|
|
print("/* extended case mappings */", file=fp)
|
|
|
|
print(file=fp)
|
|
|
|
print("const Py_UCS4 _PyUnicode_ExtendedCase[] = {", file=fp)
|
|
|
|
for c in extra_casing:
|
|
|
|
print(" %d," % c, file=fp)
|
|
|
|
print("};", file=fp)
|
|
|
|
print(file=fp)
|
|
|
|
|
2000-09-25 14:59:57 -03:00
|
|
|
# split decomposition index table
|
2000-11-03 16:24:15 -04:00
|
|
|
index1, index2, shift = splitbins(index, trace)
|
2000-09-25 14:59:57 -03:00
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("/* type indexes */", file=fp)
|
|
|
|
print("#define SHIFT", shift, file=fp)
|
2001-01-21 18:41:08 -04:00
|
|
|
Array("index1", index1).dump(fp, trace)
|
|
|
|
Array("index2", index2).dump(fp, trace)
|
2000-09-25 14:59:57 -03:00
|
|
|
|
2009-10-06 18:03:20 -03:00
|
|
|
# Generate code for _PyUnicode_ToNumeric()
|
|
|
|
numeric_items = sorted(numeric.items())
|
|
|
|
print('/* Returns the numeric value as double for Unicode characters', file=fp)
|
|
|
|
print(' * having this property, -1.0 otherwise.', file=fp)
|
|
|
|
print(' */', file=fp)
|
2010-08-18 17:44:58 -03:00
|
|
|
print('double _PyUnicode_ToNumeric(Py_UCS4 ch)', file=fp)
|
2009-10-06 18:03:20 -03:00
|
|
|
print('{', file=fp)
|
|
|
|
print(' switch (ch) {', file=fp)
|
|
|
|
for value, codepoints in numeric_items:
|
2009-10-13 20:18:53 -03:00
|
|
|
# Turn text into float literals
|
|
|
|
parts = value.split('/')
|
|
|
|
parts = [repr(float(part)) for part in parts]
|
|
|
|
value = '/'.join(parts)
|
|
|
|
|
2009-10-06 18:03:20 -03:00
|
|
|
codepoints.sort()
|
|
|
|
for codepoint in codepoints:
|
|
|
|
print(' case 0x%04X:' % (codepoint,), file=fp)
|
|
|
|
print(' return (double) %s;' % (value,), file=fp)
|
|
|
|
print(' }', file=fp)
|
|
|
|
print(' return -1.0;', file=fp)
|
|
|
|
print('}', file=fp)
|
|
|
|
print(file=fp)
|
|
|
|
|
|
|
|
# Generate code for _PyUnicode_IsWhitespace()
|
|
|
|
print("/* Returns 1 for Unicode characters having the bidirectional", file=fp)
|
|
|
|
print(" * type 'WS', 'B' or 'S' or the category 'Zs', 0 otherwise.", file=fp)
|
|
|
|
print(" */", file=fp)
|
2013-08-13 15:18:52 -03:00
|
|
|
print('int _PyUnicode_IsWhitespace(const Py_UCS4 ch)', file=fp)
|
2009-10-06 18:03:20 -03:00
|
|
|
print('{', file=fp)
|
|
|
|
print(' switch (ch) {', file=fp)
|
|
|
|
|
2010-03-19 11:25:03 -03:00
|
|
|
for codepoint in sorted(spaces):
|
2009-10-06 18:03:20 -03:00
|
|
|
print(' case 0x%04X:' % (codepoint,), file=fp)
|
|
|
|
print(' return 1;', file=fp)
|
|
|
|
|
|
|
|
print(' }', file=fp)
|
|
|
|
print(' return 0;', file=fp)
|
|
|
|
print('}', file=fp)
|
|
|
|
print(file=fp)
|
|
|
|
|
|
|
|
# Generate code for _PyUnicode_IsLinebreak()
|
2010-03-30 16:34:18 -03:00
|
|
|
print("/* Returns 1 for Unicode characters having the line break", file=fp)
|
|
|
|
print(" * property 'BK', 'CR', 'LF' or 'NL' or having bidirectional", file=fp)
|
|
|
|
print(" * type 'B', 0 otherwise.", file=fp)
|
2009-10-06 18:03:20 -03:00
|
|
|
print(" */", file=fp)
|
2013-08-13 15:18:52 -03:00
|
|
|
print('int _PyUnicode_IsLinebreak(const Py_UCS4 ch)', file=fp)
|
2009-10-06 18:03:20 -03:00
|
|
|
print('{', file=fp)
|
|
|
|
print(' switch (ch) {', file=fp)
|
2010-03-19 11:25:03 -03:00
|
|
|
for codepoint in sorted(linebreaks):
|
2009-10-06 18:03:20 -03:00
|
|
|
print(' case 0x%04X:' % (codepoint,), file=fp)
|
|
|
|
print(' return 1;', file=fp)
|
|
|
|
|
|
|
|
print(' }', file=fp)
|
|
|
|
print(' return 0;', file=fp)
|
|
|
|
print('}', file=fp)
|
|
|
|
print(file=fp)
|
|
|
|
|
2001-01-21 13:01:31 -04:00
|
|
|
fp.close()
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# unicode name database
|
|
|
|
|
|
|
|
def makeunicodename(unicode, trace):
|
|
|
|
|
|
|
|
FILE = "Modules/unicodename_db.h"
|
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("--- Preparing", FILE, "...")
|
2001-01-21 13:01:31 -04:00
|
|
|
|
|
|
|
# collect names
|
|
|
|
names = [None] * len(unicode.chars)
|
|
|
|
|
|
|
|
for char in unicode.chars:
|
|
|
|
record = unicode.table[char]
|
|
|
|
if record:
|
|
|
|
name = record[1].strip()
|
|
|
|
if name and name[0] != "<":
|
|
|
|
names[char] = name + chr(0)
|
|
|
|
|
2008-06-11 15:37:52 -03:00
|
|
|
print(len(list(n for n in names if n is not None)), "distinct names")
|
2001-01-21 13:01:31 -04:00
|
|
|
|
|
|
|
# collect unique words from names (note that we differ between
|
|
|
|
# words inside a sentence, and words ending a sentence. the
|
|
|
|
# latter includes the trailing null byte.
|
|
|
|
|
|
|
|
words = {}
|
|
|
|
n = b = 0
|
|
|
|
for char in unicode.chars:
|
|
|
|
name = names[char]
|
|
|
|
if name:
|
|
|
|
w = name.split()
|
|
|
|
b = b + len(name)
|
|
|
|
n = n + len(w)
|
|
|
|
for w in w:
|
|
|
|
l = words.get(w)
|
|
|
|
if l:
|
|
|
|
l.append(None)
|
|
|
|
else:
|
|
|
|
words[w] = [len(words)]
|
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print(n, "words in text;", b, "bytes")
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2007-08-14 19:37:03 -03:00
|
|
|
wordlist = list(words.items())
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2002-11-24 19:05:09 -04:00
|
|
|
# sort on falling frequency, then by name
|
2009-01-27 14:17:45 -04:00
|
|
|
def word_key(a):
|
2007-08-14 19:37:03 -03:00
|
|
|
aword, alist = a
|
2009-01-27 14:17:45 -04:00
|
|
|
return -len(alist), aword
|
|
|
|
wordlist.sort(key=word_key)
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2001-01-21 18:41:08 -04:00
|
|
|
# figure out how many phrasebook escapes we need
|
|
|
|
escapes = 0
|
|
|
|
while escapes * 256 < len(wordlist):
|
|
|
|
escapes = escapes + 1
|
2007-08-03 14:06:41 -03:00
|
|
|
print(escapes, "escapes")
|
2001-01-21 18:41:08 -04:00
|
|
|
|
|
|
|
short = 256 - escapes
|
|
|
|
|
|
|
|
assert short > 0
|
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print(short, "short indexes in lexicon")
|
2001-01-21 18:41:08 -04:00
|
|
|
|
2001-01-21 13:01:31 -04:00
|
|
|
# statistics
|
|
|
|
n = 0
|
2001-01-21 18:41:08 -04:00
|
|
|
for i in range(short):
|
2001-01-21 13:01:31 -04:00
|
|
|
n = n + len(wordlist[i][1])
|
2007-08-03 14:06:41 -03:00
|
|
|
print(n, "short indexes in phrasebook")
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2001-01-21 18:41:08 -04:00
|
|
|
# pick the most commonly used words, and sort the rest on falling
|
|
|
|
# length (to maximize overlap)
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2001-01-21 18:41:08 -04:00
|
|
|
wordlist, wordtail = wordlist[:short], wordlist[short:]
|
2008-01-29 22:55:10 -04:00
|
|
|
wordtail.sort(key=lambda a: a[0], reverse=True)
|
2001-01-21 13:01:31 -04:00
|
|
|
wordlist.extend(wordtail)
|
|
|
|
|
|
|
|
# generate lexicon from words
|
|
|
|
|
|
|
|
lexicon_offset = [0]
|
|
|
|
lexicon = ""
|
|
|
|
words = {}
|
|
|
|
|
|
|
|
# build a lexicon string
|
|
|
|
offset = 0
|
|
|
|
for w, x in wordlist:
|
|
|
|
# encoding: bit 7 indicates last character in word (chr(128)
|
|
|
|
# indicates the last character in an entire string)
|
|
|
|
ww = w[:-1] + chr(ord(w[-1])+128)
|
|
|
|
# reuse string tails, when possible
|
2002-09-11 17:36:02 -03:00
|
|
|
o = lexicon.find(ww)
|
2001-01-21 13:01:31 -04:00
|
|
|
if o < 0:
|
|
|
|
o = offset
|
|
|
|
lexicon = lexicon + ww
|
|
|
|
offset = offset + len(w)
|
|
|
|
words[w] = len(lexicon_offset)
|
2001-01-21 18:41:08 -04:00
|
|
|
lexicon_offset.append(o)
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2007-08-14 19:37:03 -03:00
|
|
|
lexicon = list(map(ord, lexicon))
|
2001-01-21 13:01:31 -04:00
|
|
|
|
|
|
|
# generate phrasebook from names and lexicon
|
|
|
|
phrasebook = [0]
|
|
|
|
phrasebook_offset = [0] * len(unicode.chars)
|
|
|
|
for char in unicode.chars:
|
|
|
|
name = names[char]
|
|
|
|
if name:
|
|
|
|
w = name.split()
|
|
|
|
phrasebook_offset[char] = len(phrasebook)
|
|
|
|
for w in w:
|
|
|
|
i = words[w]
|
2001-01-21 18:41:08 -04:00
|
|
|
if i < short:
|
|
|
|
phrasebook.append(i)
|
2001-01-21 13:01:31 -04:00
|
|
|
else:
|
2001-01-21 18:41:08 -04:00
|
|
|
# store as two bytes
|
|
|
|
phrasebook.append((i>>8) + short)
|
2001-01-21 13:01:31 -04:00
|
|
|
phrasebook.append(i&255)
|
|
|
|
|
2001-01-21 18:41:08 -04:00
|
|
|
assert getsize(phrasebook) == 1
|
|
|
|
|
2001-01-21 13:01:31 -04:00
|
|
|
#
|
|
|
|
# unicode name hash table
|
|
|
|
|
|
|
|
# extract names
|
|
|
|
data = []
|
|
|
|
for char in unicode.chars:
|
|
|
|
record = unicode.table[char]
|
|
|
|
if record:
|
|
|
|
name = record[1].strip()
|
|
|
|
if name and name[0] != "<":
|
|
|
|
data.append((name, char))
|
|
|
|
|
|
|
|
# the magic number 47 was chosen to minimize the number of
|
|
|
|
# collisions on the current data set. if you like, change it
|
|
|
|
# and see what happens...
|
|
|
|
|
|
|
|
codehash = Hash("code", data, 47)
|
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("--- Writing", FILE, "...")
|
2001-01-21 13:01:31 -04:00
|
|
|
|
|
|
|
fp = open(FILE, "w")
|
2007-08-03 14:06:41 -03:00
|
|
|
print("/* this file was generated by %s %s */" % (SCRIPT, VERSION), file=fp)
|
|
|
|
print(file=fp)
|
|
|
|
print("#define NAME_MAXLEN", 256, file=fp)
|
|
|
|
print(file=fp)
|
|
|
|
print("/* lexicon */", file=fp)
|
2001-01-21 18:41:08 -04:00
|
|
|
Array("lexicon", lexicon).dump(fp, trace)
|
|
|
|
Array("lexicon_offset", lexicon_offset).dump(fp, trace)
|
2001-01-21 13:01:31 -04:00
|
|
|
|
|
|
|
# split decomposition index table
|
|
|
|
offset1, offset2, shift = splitbins(phrasebook_offset, trace)
|
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("/* code->name phrasebook */", file=fp)
|
|
|
|
print("#define phrasebook_shift", shift, file=fp)
|
|
|
|
print("#define phrasebook_short", short, file=fp)
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2001-01-21 18:41:08 -04:00
|
|
|
Array("phrasebook", phrasebook).dump(fp, trace)
|
|
|
|
Array("phrasebook_offset1", offset1).dump(fp, trace)
|
|
|
|
Array("phrasebook_offset2", offset2).dump(fp, trace)
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print("/* name->code dictionary */", file=fp)
|
2001-01-21 18:41:08 -04:00
|
|
|
codehash.dump(fp, trace)
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2011-10-21 15:57:36 -03:00
|
|
|
print(file=fp)
|
|
|
|
print('static const unsigned int aliases_start = %#x;' %
|
|
|
|
NAME_ALIASES_START, file=fp)
|
|
|
|
print('static const unsigned int aliases_end = %#x;' %
|
|
|
|
(NAME_ALIASES_START + len(unicode.aliases)), file=fp)
|
|
|
|
|
|
|
|
print('static const unsigned int name_aliases[] = {', file=fp)
|
|
|
|
for name, codepoint in unicode.aliases:
|
|
|
|
print(' 0x%04X,' % codepoint, file=fp)
|
|
|
|
print('};', file=fp)
|
|
|
|
|
|
|
|
# In Unicode 6.0.0, the sequences contain at most 4 BMP chars,
|
|
|
|
# so we are using Py_UCS2 seq[4]. This needs to be updated if longer
|
|
|
|
# sequences or sequences with non-BMP chars are added.
|
|
|
|
# unicodedata_lookup should be adapted too.
|
|
|
|
print(dedent("""
|
|
|
|
typedef struct NamedSequence {
|
|
|
|
int seqlen;
|
|
|
|
Py_UCS2 seq[4];
|
|
|
|
} named_sequence;
|
|
|
|
"""), file=fp)
|
|
|
|
|
|
|
|
print('static const unsigned int named_sequences_start = %#x;' %
|
|
|
|
NAMED_SEQUENCES_START, file=fp)
|
|
|
|
print('static const unsigned int named_sequences_end = %#x;' %
|
|
|
|
(NAMED_SEQUENCES_START + len(unicode.named_sequences)), file=fp)
|
|
|
|
|
|
|
|
print('static const named_sequence named_sequences[] = {', file=fp)
|
|
|
|
for name, sequence in unicode.named_sequences:
|
|
|
|
seq_str = ', '.join('0x%04X' % cp for cp in sequence)
|
|
|
|
print(' {%d, {%s}},' % (len(sequence), seq_str), file=fp)
|
|
|
|
print('};', file=fp)
|
|
|
|
|
2001-01-21 13:01:31 -04:00
|
|
|
fp.close()
|
|
|
|
|
2006-03-09 19:38:20 -04:00
|
|
|
|
|
|
|
def merge_old_version(version, new, old):
|
|
|
|
# Changes to exclusion file not implemented yet
|
|
|
|
if old.exclusions != new.exclusions:
|
2007-08-22 20:05:06 -03:00
|
|
|
raise NotImplementedError("exclusions differ")
|
2006-03-09 19:38:20 -04:00
|
|
|
|
|
|
|
# In these change records, 0xFF means "no change"
|
|
|
|
bidir_changes = [0xFF]*0x110000
|
|
|
|
category_changes = [0xFF]*0x110000
|
|
|
|
decimal_changes = [0xFF]*0x110000
|
2008-09-10 11:08:48 -03:00
|
|
|
mirrored_changes = [0xFF]*0x110000
|
2006-03-09 19:38:20 -04:00
|
|
|
# In numeric data, 0 means "no change",
|
|
|
|
# -1 means "did not have a numeric value
|
|
|
|
numeric_changes = [0] * 0x110000
|
|
|
|
# normalization_changes is a list of key-value pairs
|
|
|
|
normalization_changes = []
|
|
|
|
for i in range(0x110000):
|
|
|
|
if new.table[i] is None:
|
|
|
|
# Characters unassigned in the new version ought to
|
|
|
|
# be unassigned in the old one
|
|
|
|
assert old.table[i] is None
|
|
|
|
continue
|
|
|
|
# check characters unassigned in the old version
|
|
|
|
if old.table[i] is None:
|
|
|
|
# category 0 is "unassigned"
|
|
|
|
category_changes[i] = 0
|
|
|
|
continue
|
|
|
|
# check characters that differ
|
|
|
|
if old.table[i] != new.table[i]:
|
|
|
|
for k in range(len(old.table[i])):
|
|
|
|
if old.table[i][k] != new.table[i][k]:
|
|
|
|
value = old.table[i][k]
|
2011-10-21 15:57:36 -03:00
|
|
|
if k == 1 and i in PUA_15:
|
|
|
|
# the name is not set in the old.table, but in the
|
|
|
|
# new.table we are using it for aliases and named seq
|
|
|
|
assert value == ''
|
|
|
|
elif k == 2:
|
2006-03-09 19:38:20 -04:00
|
|
|
#print "CATEGORY",hex(i), old.table[i][k], new.table[i][k]
|
|
|
|
category_changes[i] = CATEGORY_NAMES.index(value)
|
|
|
|
elif k == 4:
|
|
|
|
#print "BIDIR",hex(i), old.table[i][k], new.table[i][k]
|
|
|
|
bidir_changes[i] = BIDIRECTIONAL_NAMES.index(value)
|
|
|
|
elif k == 5:
|
|
|
|
#print "DECOMP",hex(i), old.table[i][k], new.table[i][k]
|
|
|
|
# We assume that all normalization changes are in 1:1 mappings
|
|
|
|
assert " " not in value
|
|
|
|
normalization_changes.append((i, value))
|
|
|
|
elif k == 6:
|
|
|
|
#print "DECIMAL",hex(i), old.table[i][k], new.table[i][k]
|
|
|
|
# we only support changes where the old value is a single digit
|
|
|
|
assert value in "0123456789"
|
|
|
|
decimal_changes[i] = int(value)
|
|
|
|
elif k == 8:
|
|
|
|
# print "NUMERIC",hex(i), `old.table[i][k]`, new.table[i][k]
|
|
|
|
# Since 0 encodes "no change", the old value is better not 0
|
|
|
|
if not value:
|
|
|
|
numeric_changes[i] = -1
|
|
|
|
else:
|
2009-10-06 18:03:20 -03:00
|
|
|
numeric_changes[i] = float(value)
|
|
|
|
assert numeric_changes[i] not in (0, -1)
|
2008-09-10 11:08:48 -03:00
|
|
|
elif k == 9:
|
|
|
|
if value == 'Y':
|
|
|
|
mirrored_changes[i] = '1'
|
|
|
|
else:
|
|
|
|
mirrored_changes[i] = '0'
|
2006-03-09 19:38:20 -04:00
|
|
|
elif k == 11:
|
|
|
|
# change to ISO comment, ignore
|
|
|
|
pass
|
|
|
|
elif k == 12:
|
|
|
|
# change to simple uppercase mapping; ignore
|
|
|
|
pass
|
|
|
|
elif k == 13:
|
|
|
|
# change to simple lowercase mapping; ignore
|
|
|
|
pass
|
|
|
|
elif k == 14:
|
|
|
|
# change to simple titlecase mapping; ignore
|
|
|
|
pass
|
2007-08-14 19:37:03 -03:00
|
|
|
elif k == 16:
|
|
|
|
# derived property changes; not yet
|
|
|
|
pass
|
2010-10-11 19:42:28 -03:00
|
|
|
elif k == 17:
|
|
|
|
# normalization quickchecks are not performed
|
|
|
|
# for older versions
|
|
|
|
pass
|
2006-03-09 19:38:20 -04:00
|
|
|
else:
|
|
|
|
class Difference(Exception):pass
|
2007-08-22 20:05:06 -03:00
|
|
|
raise Difference(hex(i), k, old.table[i], new.table[i])
|
2007-08-14 19:37:03 -03:00
|
|
|
new.changed.append((version, list(zip(bidir_changes, category_changes,
|
2008-09-10 11:08:48 -03:00
|
|
|
decimal_changes, mirrored_changes,
|
|
|
|
numeric_changes)),
|
2006-03-09 19:38:20 -04:00
|
|
|
normalization_changes))
|
2006-03-10 19:39:56 -04:00
|
|
|
|
2010-10-11 19:42:28 -03:00
|
|
|
def open_data(template, version):
|
|
|
|
local = template % ('-'+version,)
|
|
|
|
if not os.path.exists(local):
|
|
|
|
import urllib.request
|
|
|
|
if version == '3.2.0':
|
|
|
|
# irregular url structure
|
|
|
|
url = 'http://www.unicode.org/Public/3.2-Update/' + local
|
|
|
|
else:
|
|
|
|
url = ('http://www.unicode.org/Public/%s/ucd/'+template) % (version, '')
|
|
|
|
urllib.request.urlretrieve(url, filename=local)
|
|
|
|
if local.endswith('.txt'):
|
|
|
|
return open(local, encoding='utf-8')
|
|
|
|
else:
|
|
|
|
# Unihan.zip
|
|
|
|
return open(local, 'rb')
|
2006-03-09 19:38:20 -04:00
|
|
|
|
2000-09-24 20:18:31 -03:00
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# the following support code is taken from the unidb utilities
|
|
|
|
# Copyright (c) 1999-2000 by Secret Labs AB
|
|
|
|
|
|
|
|
# load a unicode-data file from disk
|
|
|
|
|
|
|
|
class UnicodeData:
|
2007-08-14 19:37:03 -03:00
|
|
|
# Record structure:
|
|
|
|
# [ID, name, category, combining, bidi, decomp, (6)
|
|
|
|
# decimal, digit, numeric, bidi-mirrored, Unicode-1-name, (11)
|
|
|
|
# ISO-comment, uppercase, lowercase, titlecase, ea-width, (16)
|
|
|
|
# derived-props] (17)
|
|
|
|
|
2010-10-11 19:42:28 -03:00
|
|
|
def __init__(self, version,
|
|
|
|
linebreakprops=False,
|
2010-11-22 05:00:02 -04:00
|
|
|
expand=1,
|
|
|
|
cjk_check=True):
|
2006-03-09 19:38:20 -04:00
|
|
|
self.changed = []
|
2002-10-18 13:11:54 -03:00
|
|
|
table = [None] * 0x110000
|
2011-09-30 02:46:25 -03:00
|
|
|
with open_data(UNICODE_DATA, version) as file:
|
|
|
|
while 1:
|
|
|
|
s = file.readline()
|
|
|
|
if not s:
|
|
|
|
break
|
|
|
|
s = s.strip().split(";")
|
|
|
|
char = int(s[0], 16)
|
|
|
|
table[char] = s
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2010-11-22 05:00:02 -04:00
|
|
|
cjk_ranges_found = []
|
|
|
|
|
2002-11-24 19:05:09 -04:00
|
|
|
# expand first-last ranges
|
2000-11-03 16:24:15 -04:00
|
|
|
if expand:
|
|
|
|
field = None
|
2002-11-24 19:05:09 -04:00
|
|
|
for i in range(0, 0x110000):
|
2000-11-03 16:24:15 -04:00
|
|
|
s = table[i]
|
|
|
|
if s:
|
|
|
|
if s[1][-6:] == "First>":
|
|
|
|
s[1] = ""
|
2006-03-09 19:38:20 -04:00
|
|
|
field = s
|
2000-11-03 16:24:15 -04:00
|
|
|
elif s[1][-5:] == "Last>":
|
2010-11-22 05:00:02 -04:00
|
|
|
if s[1].startswith("<CJK Ideograph"):
|
|
|
|
cjk_ranges_found.append((field[0],
|
|
|
|
s[0]))
|
2000-11-03 16:24:15 -04:00
|
|
|
s[1] = ""
|
|
|
|
field = None
|
|
|
|
elif field:
|
2006-03-09 19:38:20 -04:00
|
|
|
f2 = field[:]
|
|
|
|
f2[0] = "%X" % i
|
|
|
|
table[i] = f2
|
2010-11-22 05:00:02 -04:00
|
|
|
if cjk_check and cjk_ranges != cjk_ranges_found:
|
|
|
|
raise ValueError("CJK ranges deviate: have %r" % cjk_ranges_found)
|
2000-11-03 16:24:15 -04:00
|
|
|
|
2000-09-24 20:18:31 -03:00
|
|
|
# public attributes
|
2010-10-11 19:42:28 -03:00
|
|
|
self.filename = UNICODE_DATA % ''
|
2000-09-24 20:18:31 -03:00
|
|
|
self.table = table
|
2008-05-16 14:02:34 -03:00
|
|
|
self.chars = list(range(0x110000)) # unicode 3.2
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2011-10-21 15:57:36 -03:00
|
|
|
# check for name aliases and named sequences, see #12753
|
|
|
|
# aliases and named sequences are not in 3.2.0
|
|
|
|
if version != '3.2.0':
|
|
|
|
self.aliases = []
|
|
|
|
# store aliases in the Private Use Area 15, in range U+F0000..U+F00FF,
|
|
|
|
# in order to take advantage of the compression and lookup
|
|
|
|
# algorithms used for the other characters
|
|
|
|
pua_index = NAME_ALIASES_START
|
|
|
|
with open_data(NAME_ALIASES, version) as file:
|
|
|
|
for s in file:
|
|
|
|
s = s.strip()
|
|
|
|
if not s or s.startswith('#'):
|
|
|
|
continue
|
2012-02-20 23:24:29 -04:00
|
|
|
char, name, abbrev = s.split(';')
|
2011-10-21 15:57:36 -03:00
|
|
|
char = int(char, 16)
|
|
|
|
self.aliases.append((name, char))
|
|
|
|
# also store the name in the PUA 1
|
|
|
|
self.table[pua_index][1] = name
|
|
|
|
pua_index += 1
|
|
|
|
assert pua_index - NAME_ALIASES_START == len(self.aliases)
|
|
|
|
|
|
|
|
self.named_sequences = []
|
2013-08-25 19:32:56 -03:00
|
|
|
# store named sequences in the PUA 1, in range U+F0100..,
|
2011-10-21 15:57:36 -03:00
|
|
|
# in order to take advantage of the compression and lookup
|
|
|
|
# algorithms used for the other characters.
|
|
|
|
|
2012-02-20 23:24:29 -04:00
|
|
|
assert pua_index < NAMED_SEQUENCES_START
|
2011-10-21 15:57:36 -03:00
|
|
|
pua_index = NAMED_SEQUENCES_START
|
|
|
|
with open_data(NAMED_SEQUENCES, version) as file:
|
|
|
|
for s in file:
|
|
|
|
s = s.strip()
|
|
|
|
if not s or s.startswith('#'):
|
|
|
|
continue
|
|
|
|
name, chars = s.split(';')
|
|
|
|
chars = tuple(int(char, 16) for char in chars.split())
|
|
|
|
# check that the structure defined in makeunicodename is OK
|
|
|
|
assert 2 <= len(chars) <= 4, "change the Py_UCS2 array size"
|
|
|
|
assert all(c <= 0xFFFF for c in chars), ("use Py_UCS4 in "
|
|
|
|
"the NamedSequence struct and in unicodedata_lookup")
|
|
|
|
self.named_sequences.append((name, chars))
|
|
|
|
# also store these in the PUA 1
|
|
|
|
self.table[pua_index][1] = name
|
|
|
|
pua_index += 1
|
|
|
|
assert pua_index - NAMED_SEQUENCES_START == len(self.named_sequences)
|
|
|
|
|
2002-11-23 18:08:15 -04:00
|
|
|
self.exclusions = {}
|
2011-09-30 02:46:25 -03:00
|
|
|
with open_data(COMPOSITION_EXCLUSIONS, version) as file:
|
|
|
|
for s in file:
|
|
|
|
s = s.strip()
|
|
|
|
if not s:
|
|
|
|
continue
|
|
|
|
if s[0] == '#':
|
|
|
|
continue
|
|
|
|
char = int(s.split()[0],16)
|
|
|
|
self.exclusions[char] = 1
|
2002-11-23 18:08:15 -04:00
|
|
|
|
2004-06-02 13:49:17 -03:00
|
|
|
widths = [None] * 0x110000
|
2011-09-30 02:46:25 -03:00
|
|
|
with open_data(EASTASIAN_WIDTH, version) as file:
|
|
|
|
for s in file:
|
|
|
|
s = s.strip()
|
|
|
|
if not s:
|
|
|
|
continue
|
|
|
|
if s[0] == '#':
|
|
|
|
continue
|
|
|
|
s = s.split()[0].split(';')
|
|
|
|
if '..' in s[0]:
|
|
|
|
first, last = [int(c, 16) for c in s[0].split('..')]
|
|
|
|
chars = list(range(first, last+1))
|
|
|
|
else:
|
|
|
|
chars = [int(s[0], 16)]
|
|
|
|
for char in chars:
|
|
|
|
widths[char] = s[1]
|
|
|
|
|
2004-06-02 13:49:17 -03:00
|
|
|
for i in range(0, 0x110000):
|
|
|
|
if table[i] is not None:
|
|
|
|
table[i].append(widths[i])
|
|
|
|
|
2007-08-14 19:37:03 -03:00
|
|
|
for i in range(0, 0x110000):
|
|
|
|
if table[i] is not None:
|
|
|
|
table[i].append(set())
|
|
|
|
|
2011-09-30 02:46:25 -03:00
|
|
|
with open_data(DERIVED_CORE_PROPERTIES, version) as file:
|
|
|
|
for s in file:
|
|
|
|
s = s.split('#', 1)[0].strip()
|
|
|
|
if not s:
|
|
|
|
continue
|
|
|
|
|
|
|
|
r, p = s.split(";")
|
|
|
|
r = r.strip()
|
|
|
|
p = p.strip()
|
|
|
|
if ".." in r:
|
|
|
|
first, last = [int(c, 16) for c in r.split('..')]
|
|
|
|
chars = list(range(first, last+1))
|
|
|
|
else:
|
|
|
|
chars = [int(r, 16)]
|
|
|
|
for char in chars:
|
|
|
|
if table[char]:
|
|
|
|
# Some properties (e.g. Default_Ignorable_Code_Point)
|
|
|
|
# apply to unassigned code points; ignore them
|
|
|
|
table[char][-1].add(p)
|
|
|
|
|
|
|
|
with open_data(LINE_BREAK, version) as file:
|
|
|
|
for s in file:
|
|
|
|
s = s.partition('#')[0]
|
|
|
|
s = [i.strip() for i in s.split(';')]
|
|
|
|
if len(s) < 2 or s[1] not in MANDATORY_LINE_BREAKS:
|
|
|
|
continue
|
|
|
|
if '..' not in s[0]:
|
|
|
|
first = last = int(s[0], 16)
|
|
|
|
else:
|
|
|
|
first, last = [int(c, 16) for c in s[0].split('..')]
|
|
|
|
for char in range(first, last+1):
|
|
|
|
table[char][-1].add('Line_Break')
|
2010-10-11 19:42:28 -03:00
|
|
|
|
|
|
|
# We only want the quickcheck properties
|
|
|
|
# Format: NF?_QC; Y(es)/N(o)/M(aybe)
|
|
|
|
# Yes is the default, hence only N and M occur
|
|
|
|
# In 3.2.0, the format was different (NF?_NO)
|
|
|
|
# The parsing will incorrectly determine these as
|
|
|
|
# "yes", however, unicodedata.c will not perform quickchecks
|
|
|
|
# for older versions, and no delta records will be created.
|
|
|
|
quickchecks = [0] * 0x110000
|
|
|
|
qc_order = 'NFD_QC NFKD_QC NFC_QC NFKC_QC'.split()
|
2011-09-30 02:46:25 -03:00
|
|
|
with open_data(DERIVEDNORMALIZATION_PROPS, version) as file:
|
|
|
|
for s in file:
|
|
|
|
if '#' in s:
|
|
|
|
s = s[:s.index('#')]
|
|
|
|
s = [i.strip() for i in s.split(';')]
|
|
|
|
if len(s) < 2 or s[1] not in qc_order:
|
|
|
|
continue
|
|
|
|
quickcheck = 'MN'.index(s[2]) + 1 # Maybe or No
|
|
|
|
quickcheck_shift = qc_order.index(s[1])*2
|
|
|
|
quickcheck <<= quickcheck_shift
|
|
|
|
if '..' not in s[0]:
|
|
|
|
first = last = int(s[0], 16)
|
|
|
|
else:
|
|
|
|
first, last = [int(c, 16) for c in s[0].split('..')]
|
|
|
|
for char in range(first, last+1):
|
|
|
|
assert not (quickchecks[char]>>quickcheck_shift)&3
|
|
|
|
quickchecks[char] |= quickcheck
|
2010-10-11 19:42:28 -03:00
|
|
|
for i in range(0, 0x110000):
|
|
|
|
if table[i] is not None:
|
|
|
|
table[i].append(quickchecks[i])
|
|
|
|
|
2011-09-30 02:46:25 -03:00
|
|
|
with open_data(UNIHAN, version) as file:
|
|
|
|
zip = zipfile.ZipFile(file)
|
|
|
|
if version == '3.2.0':
|
|
|
|
data = zip.open('Unihan-3.2.0.txt').read()
|
|
|
|
else:
|
|
|
|
data = zip.open('Unihan_NumericValues.txt').read()
|
2010-10-11 19:42:28 -03:00
|
|
|
for line in data.decode("utf-8").splitlines():
|
2009-10-06 18:03:20 -03:00
|
|
|
if not line.startswith('U+'):
|
|
|
|
continue
|
|
|
|
code, tag, value = line.split(None, 3)[:3]
|
|
|
|
if tag not in ('kAccountingNumeric', 'kPrimaryNumeric',
|
|
|
|
'kOtherNumeric'):
|
|
|
|
continue
|
|
|
|
value = value.strip().replace(',', '')
|
|
|
|
i = int(code[2:], 16)
|
|
|
|
# Patch the numeric field
|
|
|
|
if table[i] is not None:
|
|
|
|
table[i][8] = value
|
2012-01-11 19:17:06 -04:00
|
|
|
sc = self.special_casing = {}
|
|
|
|
with open_data(SPECIAL_CASING, version) as file:
|
|
|
|
for s in file:
|
|
|
|
s = s[:-1].split('#', 1)[0]
|
|
|
|
if not s:
|
|
|
|
continue
|
|
|
|
data = s.split("; ")
|
|
|
|
if data[4]:
|
|
|
|
# We ignore all conditionals (since they depend on
|
|
|
|
# languages) except for one, which is hardcoded. See
|
|
|
|
# handle_capital_sigma in unicodeobject.c.
|
|
|
|
continue
|
|
|
|
c = int(data[0], 16)
|
|
|
|
lower = [int(char, 16) for char in data[1].split()]
|
|
|
|
title = [int(char, 16) for char in data[2].split()]
|
|
|
|
upper = [int(char, 16) for char in data[3].split()]
|
|
|
|
sc[c] = (lower, title, upper)
|
2012-01-14 14:23:30 -04:00
|
|
|
cf = self.case_folding = {}
|
|
|
|
if version != '3.2.0':
|
|
|
|
with open_data(CASE_FOLDING, version) as file:
|
|
|
|
for s in file:
|
|
|
|
s = s[:-1].split('#', 1)[0]
|
|
|
|
if not s:
|
|
|
|
continue
|
|
|
|
data = s.split("; ")
|
|
|
|
if data[1] in "CF":
|
|
|
|
c = int(data[0], 16)
|
|
|
|
cf[c] = [int(char, 16) for char in data[2].split()]
|
2009-10-06 18:03:20 -03:00
|
|
|
|
2000-09-24 20:18:31 -03:00
|
|
|
def uselatin1(self):
|
|
|
|
# restrict character range to ISO Latin 1
|
2008-05-16 14:02:34 -03:00
|
|
|
self.chars = list(range(256))
|
2000-09-24 20:18:31 -03:00
|
|
|
|
2001-01-21 13:01:31 -04:00
|
|
|
# hash table tools
|
|
|
|
|
|
|
|
# this is a straight-forward reimplementation of Python's built-in
|
|
|
|
# dictionary type, using a static data structure, and a custom string
|
|
|
|
# hash algorithm.
|
|
|
|
|
|
|
|
def myhash(s, magic):
|
|
|
|
h = 0
|
2002-09-11 17:36:02 -03:00
|
|
|
for c in map(ord, s.upper()):
|
2001-01-21 13:01:31 -04:00
|
|
|
h = (h * magic) + c
|
Merged revisions 55817-55961 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r55837 | guido.van.rossum | 2007-06-08 16:04:42 -0700 (Fri, 08 Jun 2007) | 2 lines
PEP 3119 -- the abc module.
................
r55838 | guido.van.rossum | 2007-06-08 17:38:55 -0700 (Fri, 08 Jun 2007) | 2 lines
Implement part of PEP 3119 -- One Trick Ponies.
................
r55847 | guido.van.rossum | 2007-06-09 08:28:06 -0700 (Sat, 09 Jun 2007) | 2 lines
Different way to do one trick ponies, allowing registration (per PEP strawman).
................
r55849 | guido.van.rossum | 2007-06-09 18:06:38 -0700 (Sat, 09 Jun 2007) | 3 lines
Make sure that the magic looking for __hash__ (etc.) doesn't apply to
real subclasses of Hashable.
................
r55852 | guido.van.rossum | 2007-06-10 08:29:51 -0700 (Sun, 10 Jun 2007) | 2 lines
Add some more examples, e.g. generators and dict views.
................
r55853 | guido.van.rossum | 2007-06-10 08:31:59 -0700 (Sun, 10 Jun 2007) | 2 lines
keys() and items() *are* containers -- just values() isn't.
................
r55864 | georg.brandl | 2007-06-10 15:29:40 -0700 (Sun, 10 Jun 2007) | 2 lines
PEP 3127: new octal literals, binary literals.
................
r55865 | georg.brandl | 2007-06-10 15:31:37 -0700 (Sun, 10 Jun 2007) | 2 lines
Some octal literal fixes in Tools.
................
r55866 | georg.brandl | 2007-06-10 15:37:43 -0700 (Sun, 10 Jun 2007) | 2 lines
Tokenizer changes for PEP 3127.
................
r55867 | georg.brandl | 2007-06-10 15:37:55 -0700 (Sun, 10 Jun 2007) | 2 lines
Some docs for PEP 3127.
................
r55868 | georg.brandl | 2007-06-10 15:44:39 -0700 (Sun, 10 Jun 2007) | 2 lines
Missed a place in intobject.c. Is that used anymore anyway?
................
r55871 | neal.norwitz | 2007-06-10 18:31:49 -0700 (Sun, 10 Jun 2007) | 182 lines
Merged revisions 55729-55868 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r55731 | neal.norwitz | 2007-06-01 00:29:12 -0700 (Fri, 01 Jun 2007) | 7 lines
SF 1668596/1720897: distutils now copies data files
even if package_dir is empty.
This needs to be backported. I'm too tired tonight. It would be great
if someone backports this if the buildbots are ok with it. Otherwise,
I will try to get to it tomorrow.
........
r55732 | georg.brandl | 2007-06-01 04:33:33 -0700 (Fri, 01 Jun 2007) | 2 lines
Bug #1722484: remove docstrings again when running with -OO.
........
r55735 | georg.brandl | 2007-06-01 12:20:27 -0700 (Fri, 01 Jun 2007) | 2 lines
Fix wrong issue number.
........
r55739 | brett.cannon | 2007-06-01 20:02:29 -0700 (Fri, 01 Jun 2007) | 3 lines
Have configure raise an error when building on AtheOS. Code specific to AtheOS
will be removed in Python 2.7.
........
r55746 | neal.norwitz | 2007-06-02 11:33:53 -0700 (Sat, 02 Jun 2007) | 1 line
Update expected birthday of 2.6
........
r55751 | neal.norwitz | 2007-06-03 13:32:50 -0700 (Sun, 03 Jun 2007) | 10 lines
Backout the original 'fix' to 1721309 which had no effect.
Different versions of Berkeley DB handle this differently.
The comments and bug report should have the details. Memory is allocated
in 4.4 (and presumably earlier), but not in 4.5. Thus
4.5 has the free error, but not earlier versions.
Mostly update comments, plus make the free conditional.
This fix was already applied to the 2.5 branch.
........
r55752 | brett.cannon | 2007-06-03 16:13:41 -0700 (Sun, 03 Jun 2007) | 6 lines
Make _strptime.TimeRE().pattern() use ``\s+`` for matching whitespace instead
of ``\s*``. This prevents patterns from "stealing" bits from other patterns in
order to make a match work.
Closes bug #1730389. Will be backported.
........
r55766 | hyeshik.chang | 2007-06-05 11:16:52 -0700 (Tue, 05 Jun 2007) | 4 lines
Fix build on FreeBSD. Bluetooth HCI API in FreeBSD is quite different
from Linux's. Just fix the build for now but the code doesn't
support the complete capability of HCI on FreeBSD yet.
........
r55770 | hyeshik.chang | 2007-06-05 11:58:51 -0700 (Tue, 05 Jun 2007) | 4 lines
Bug #1728403: Fix a bug that CJKCodecs StreamReader hangs when it
reads a file that ends with incomplete sequence and sizehint argument
for .read() is specified.
........
r55775 | hyeshik.chang | 2007-06-05 12:28:15 -0700 (Tue, 05 Jun 2007) | 2 lines
Fix for Windows: close a temporary file before trying to delete it.
........
r55783 | guido.van.rossum | 2007-06-05 14:24:47 -0700 (Tue, 05 Jun 2007) | 2 lines
Patch by Tim Delany (missing DECREF). SF #1731330.
........
r55785 | collin.winter | 2007-06-05 17:17:35 -0700 (Tue, 05 Jun 2007) | 3 lines
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
........
r55786 | facundo.batista | 2007-06-06 08:13:37 -0700 (Wed, 06 Jun 2007) | 4 lines
FTP.ntransfercmd method now uses create_connection when passive,
using the timeout received in connection time.
........
r55792 | facundo.batista | 2007-06-06 10:15:23 -0700 (Wed, 06 Jun 2007) | 7 lines
Added an optional timeout parameter to function urllib2.urlopen,
with tests in test_urllib2net.py (must have network resource
enabled to execute them). Also modified test_urllib2.py because
testing mock classes must take it into acount. Docs are also
updated.
........
r55793 | thomas.heller | 2007-06-06 13:19:19 -0700 (Wed, 06 Jun 2007) | 1 line
Build _ctypes and _ctypes_test in the ReleaseAMD64 configuration.
........
r55802 | georg.brandl | 2007-06-07 06:23:24 -0700 (Thu, 07 Jun 2007) | 3 lines
Disallow function calls like foo(None=1).
Backport from py3k rev. 55708 by Guido.
........
r55804 | georg.brandl | 2007-06-07 06:30:24 -0700 (Thu, 07 Jun 2007) | 2 lines
Make reindent.py executable.
........
r55805 | georg.brandl | 2007-06-07 06:34:10 -0700 (Thu, 07 Jun 2007) | 2 lines
Patch #1667860: Fix UnboundLocalError in urllib2.
........
r55821 | kristjan.jonsson | 2007-06-07 16:53:49 -0700 (Thu, 07 Jun 2007) | 1 line
Fixing changes to getbuildinfo.c that broke linux builds
........
r55828 | thomas.heller | 2007-06-08 09:10:27 -0700 (Fri, 08 Jun 2007) | 1 line
Make this test work with older Python releases where struct has no 't' format character.
........
r55829 | martin.v.loewis | 2007-06-08 10:29:20 -0700 (Fri, 08 Jun 2007) | 3 lines
Bug #1733488: Fix compilation of bufferobject.c on AIX.
Will backport to 2.5.
........
r55831 | thomas.heller | 2007-06-08 11:20:09 -0700 (Fri, 08 Jun 2007) | 2 lines
[ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
with small modifications.
........
r55832 | thomas.heller | 2007-06-08 12:01:06 -0700 (Fri, 08 Jun 2007) | 1 line
Fix gcc warnings intruduced by passing Py_ssize_t to PyErr_Format calls.
........
r55833 | thomas.heller | 2007-06-08 12:08:31 -0700 (Fri, 08 Jun 2007) | 2 lines
Fix wrong documentation, and correct the punktuation.
Closes [1700455].
........
r55834 | thomas.heller | 2007-06-08 12:14:23 -0700 (Fri, 08 Jun 2007) | 1 line
Fix warnings by using proper function prototype.
........
r55839 | neal.norwitz | 2007-06-08 20:36:34 -0700 (Fri, 08 Jun 2007) | 7 lines
Prevent expandtabs() on string and unicode objects from causing a segfault when
a large width is passed on 32-bit platforms. Found by Google.
It would be good for people to review this especially carefully and verify
I don't have an off by one error and there is no other way to cause overflow.
........
r55841 | neal.norwitz | 2007-06-08 21:48:22 -0700 (Fri, 08 Jun 2007) | 1 line
Use macro version of GET_SIZE to avoid Coverity warning (#150) about a possible error.
........
r55842 | martin.v.loewis | 2007-06-09 00:42:52 -0700 (Sat, 09 Jun 2007) | 3 lines
Patch #1733960: Allow T_LONGLONG to accept ints.
Will backport to 2.5.
........
r55843 | martin.v.loewis | 2007-06-09 00:58:05 -0700 (Sat, 09 Jun 2007) | 2 lines
Fix Windows build.
........
r55845 | martin.v.loewis | 2007-06-09 03:10:26 -0700 (Sat, 09 Jun 2007) | 2 lines
Provide LLONG_MAX for S390.
........
r55854 | thomas.heller | 2007-06-10 08:59:17 -0700 (Sun, 10 Jun 2007) | 4 lines
First version of build scripts for Windows/AMD64 (no external
components are built yet, and 'kill_python' is disabled).
........
r55855 | thomas.heller | 2007-06-10 10:55:51 -0700 (Sun, 10 Jun 2007) | 3 lines
For now, disable the _bsddb, _sqlite3, _ssl, _testcapi, _tkinter
modules in the ReleaseAMD64 configuration because they do not compile.
........
r55856 | thomas.heller | 2007-06-10 11:27:54 -0700 (Sun, 10 Jun 2007) | 1 line
Need to set the environment variables, otherwise devenv.com is not found.
........
r55860 | thomas.heller | 2007-06-10 14:01:17 -0700 (Sun, 10 Jun 2007) | 1 line
Revert commit 55855.
........
................
r55880 | neal.norwitz | 2007-06-10 22:07:36 -0700 (Sun, 10 Jun 2007) | 5 lines
Fix the refleak counter on test_collections. The ABC metaclass creates
a registry which must be cleared on each run. Otherwise, there *seem*
to be refleaks when there really aren't any. (The class is held within
the registry even though it's no longer needed.)
................
r55884 | neal.norwitz | 2007-06-10 22:46:33 -0700 (Sun, 10 Jun 2007) | 1 line
These tests have been removed, so they are no longer needed here
................
r55886 | georg.brandl | 2007-06-11 00:26:37 -0700 (Mon, 11 Jun 2007) | 3 lines
Optimize access to True and False in the compiler (if True)
and the peepholer (LOAD_NAME True).
................
r55905 | georg.brandl | 2007-06-11 10:02:26 -0700 (Mon, 11 Jun 2007) | 5 lines
Remove __oct__ and __hex__ and use __index__ for converting
non-ints before formatting in a base.
Add a bin() builtin.
................
r55906 | georg.brandl | 2007-06-11 10:04:44 -0700 (Mon, 11 Jun 2007) | 2 lines
int(x, 0) does not "guess".
................
r55907 | georg.brandl | 2007-06-11 10:05:47 -0700 (Mon, 11 Jun 2007) | 2 lines
Add a comment to explain that nb_oct and nb_hex are nonfunctional.
................
r55908 | guido.van.rossum | 2007-06-11 10:49:18 -0700 (Mon, 11 Jun 2007) | 2 lines
Get rid of unused imports and comment.
................
r55910 | guido.van.rossum | 2007-06-11 13:05:17 -0700 (Mon, 11 Jun 2007) | 2 lines
_Abstract.__new__ now requires either no arguments or __init__ overridden.
................
r55911 | guido.van.rossum | 2007-06-11 13:07:49 -0700 (Mon, 11 Jun 2007) | 7 lines
Move the collections ABCs to a separate file, _abcoll.py, in order to avoid
needing to import _collections.so during the bootstrap (this will become
apparent in the next submit of os.py).
Add (plain and mutable) ABCs for Set, Mapping, Sequence.
................
r55912 | guido.van.rossum | 2007-06-11 13:09:31 -0700 (Mon, 11 Jun 2007) | 2 lines
Rewrite the _Environ class to use the new collections ABCs.
................
r55913 | guido.van.rossum | 2007-06-11 13:59:45 -0700 (Mon, 11 Jun 2007) | 72 lines
Merged revisions 55869-55912 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r55869 | neal.norwitz | 2007-06-10 17:42:11 -0700 (Sun, 10 Jun 2007) | 1 line
Add Atul Varma for patch # 1667860
........
r55870 | neal.norwitz | 2007-06-10 18:22:03 -0700 (Sun, 10 Jun 2007) | 1 line
Ignore valgrind problems on Ubuntu from ld
........
r55872 | neal.norwitz | 2007-06-10 18:48:46 -0700 (Sun, 10 Jun 2007) | 2 lines
Ignore config.status.lineno which seems new (new autoconf?)
........
r55873 | neal.norwitz | 2007-06-10 19:14:39 -0700 (Sun, 10 Jun 2007) | 1 line
Prevent these tests from running on Win64 since they don\'t apply there either
........
r55874 | neal.norwitz | 2007-06-10 19:16:10 -0700 (Sun, 10 Jun 2007) | 5 lines
Fix a bug when there was a newline in the string expandtabs was called on.
This also catches another condition that can overflow.
Will backport.
........
r55879 | neal.norwitz | 2007-06-10 21:52:37 -0700 (Sun, 10 Jun 2007) | 1 line
Prevent hang if the port cannot be opened.
........
r55881 | neal.norwitz | 2007-06-10 22:28:45 -0700 (Sun, 10 Jun 2007) | 4 lines
Add all of the distuils modules that don't seem to have explicit tests. :-(
Move an import in mworkscompiler so that this module can be imported on
any platform. Hopefully this works on all platforms.
........
r55882 | neal.norwitz | 2007-06-10 22:35:10 -0700 (Sun, 10 Jun 2007) | 4 lines
SF #1734732, lower case the module names per PEP 8.
Will backport.
........
r55885 | neal.norwitz | 2007-06-10 23:16:48 -0700 (Sun, 10 Jun 2007) | 4 lines
Not sure why this only fails sometimes on Unix machines. Better
to disable it and only import msvccompiler on Windows since that's
the only place it can work anyways.
........
r55887 | neal.norwitz | 2007-06-11 00:29:43 -0700 (Mon, 11 Jun 2007) | 4 lines
Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute.
Will backport
........
r55889 | neal.norwitz | 2007-06-11 00:36:24 -0700 (Mon, 11 Jun 2007) | 1 line
Reflow long line
........
r55896 | thomas.heller | 2007-06-11 08:58:33 -0700 (Mon, 11 Jun 2007) | 3 lines
Use "O&" in calls to PyArg_Parse when we need a 'void*' instead of "k"
or "K" codes.
........
r55901 | facundo.batista | 2007-06-11 09:27:08 -0700 (Mon, 11 Jun 2007) | 5 lines
Added versionchanged flag to all the methods which received
a new optional timeout parameter, and a versionadded flag to
the socket.create_connection function.
........
................
r55914 | guido.van.rossum | 2007-06-11 14:19:50 -0700 (Mon, 11 Jun 2007) | 3 lines
New super() implementation, for PEP 3135 (though the PEP is not yet updated
to this design, and small tweaks may still be made later).
................
r55923 | guido.van.rossum | 2007-06-11 21:15:24 -0700 (Mon, 11 Jun 2007) | 4 lines
I'm guessing this module broke when Neal ripped out the types module --
it used 'list' both as a local variable and as the built-in list type.
Renamed the local variable since that was easier.
................
r55924 | guido.van.rossum | 2007-06-11 21:20:05 -0700 (Mon, 11 Jun 2007) | 5 lines
Change all occurrences of super(<thisclass>, <firstarg>) to super().
Seems to have worked, all the tests still pass.
Exception: test_descr and test_descrtut, which have tons of these
and are there to test the various usages.
................
r55939 | collin.winter | 2007-06-12 13:57:33 -0700 (Tue, 12 Jun 2007) | 1 line
Patch #1735485: remove StandardError from the exception hierarchy.
................
r55954 | neal.norwitz | 2007-06-12 21:56:32 -0700 (Tue, 12 Jun 2007) | 51 lines
Merged revisions 55913-55950 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r55926 | marc-andre.lemburg | 2007-06-12 02:09:58 -0700 (Tue, 12 Jun 2007) | 3 lines
Apply patch #1734945 to support TurboLinux as distribution.
........
r55927 | marc-andre.lemburg | 2007-06-12 02:26:49 -0700 (Tue, 12 Jun 2007) | 3 lines
Add patch #1726668: Windows Vista support.
........
r55929 | thomas.heller | 2007-06-12 08:36:22 -0700 (Tue, 12 Jun 2007) | 1 line
Checkout, but do not yet try to build, exernal sources.
........
r55930 | thomas.heller | 2007-06-12 09:08:27 -0700 (Tue, 12 Jun 2007) | 6 lines
Add bufferoverflowU.lib to the libraries needed by _ssl (is this the
right thing to do?).
Set the /XP64 /RETAIL build enviroment in the makefile when building
ReleaseAMD64.
........
r55931 | thomas.heller | 2007-06-12 09:23:19 -0700 (Tue, 12 Jun 2007) | 5 lines
Revert this change, since it breaks the win32 build:
Add bufferoverflowU.lib to the libraries needed by _ssl (is this the
right thing to do?).
........
r55934 | thomas.heller | 2007-06-12 10:28:31 -0700 (Tue, 12 Jun 2007) | 3 lines
Specify the bufferoverflowU.lib to the makefile on the command line
(for ReleaseAMD64 builds).
........
r55937 | thomas.heller | 2007-06-12 12:02:59 -0700 (Tue, 12 Jun 2007) | 3 lines
Add bufferoverflowU.lib to PCBuild\_bsddb.vcproj.
Build sqlite3.dll and bsddb.
........
r55938 | thomas.heller | 2007-06-12 12:56:12 -0700 (Tue, 12 Jun 2007) | 2 lines
Don't rebuild Berkeley DB if not needed (this was committed by accident).
........
r55948 | martin.v.loewis | 2007-06-12 20:42:19 -0700 (Tue, 12 Jun 2007) | 3 lines
Provide PY_LLONG_MAX on all systems having long long.
Will backport to 2.5.
........
................
r55959 | guido.van.rossum | 2007-06-13 09:22:41 -0700 (Wed, 13 Jun 2007) | 2 lines
Fix a compilation warning.
................
2007-06-13 15:07:49 -03:00
|
|
|
ix = h & 0xff000000
|
2001-01-21 13:01:31 -04:00
|
|
|
if ix:
|
|
|
|
h = (h ^ ((ix>>24) & 0xff)) & 0x00ffffff
|
|
|
|
return h
|
|
|
|
|
|
|
|
SIZES = [
|
|
|
|
(4,3), (8,3), (16,3), (32,5), (64,3), (128,3), (256,29), (512,17),
|
|
|
|
(1024,9), (2048,5), (4096,83), (8192,27), (16384,43), (32768,3),
|
|
|
|
(65536,45), (131072,9), (262144,39), (524288,39), (1048576,9),
|
|
|
|
(2097152,5), (4194304,3), (8388608,33), (16777216,27)
|
|
|
|
]
|
|
|
|
|
|
|
|
class Hash:
|
|
|
|
def __init__(self, name, data, magic):
|
|
|
|
# turn a (key, value) list into a static hash table structure
|
|
|
|
|
|
|
|
# determine table size
|
|
|
|
for size, poly in SIZES:
|
|
|
|
if size > len(data):
|
|
|
|
poly = size + poly
|
|
|
|
break
|
|
|
|
else:
|
2011-03-16 06:05:33 -03:00
|
|
|
raise AssertionError("ran out of polynomials")
|
2001-01-21 13:01:31 -04:00
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print(size, "slots in hash table")
|
2001-01-21 13:01:31 -04:00
|
|
|
|
|
|
|
table = [None] * size
|
|
|
|
|
|
|
|
mask = size-1
|
|
|
|
|
|
|
|
n = 0
|
|
|
|
|
|
|
|
hash = myhash
|
|
|
|
|
|
|
|
# initialize hash table
|
|
|
|
for key, value in data:
|
|
|
|
h = hash(key, magic)
|
|
|
|
i = (~h) & mask
|
|
|
|
v = table[i]
|
|
|
|
if v is None:
|
|
|
|
table[i] = value
|
|
|
|
continue
|
|
|
|
incr = (h ^ (h >> 3)) & mask;
|
|
|
|
if not incr:
|
|
|
|
incr = mask
|
|
|
|
while 1:
|
|
|
|
n = n + 1
|
|
|
|
i = (i + incr) & mask
|
|
|
|
v = table[i]
|
|
|
|
if v is None:
|
|
|
|
table[i] = value
|
|
|
|
break
|
|
|
|
incr = incr << 1
|
|
|
|
if incr > mask:
|
|
|
|
incr = incr ^ poly
|
|
|
|
|
2007-08-03 14:06:41 -03:00
|
|
|
print(n, "collisions")
|
2001-01-21 13:01:31 -04:00
|
|
|
self.collisions = n
|
|
|
|
|
|
|
|
for i in range(len(table)):
|
|
|
|
if table[i] is None:
|
|
|
|
table[i] = 0
|
|
|
|
|
|
|
|
self.data = Array(name + "_hash", table)
|
|
|
|
self.magic = magic
|
|
|
|
self.name = name
|
|
|
|
self.size = size
|
|
|
|
self.poly = poly
|
|
|
|
|
2001-01-21 18:41:08 -04:00
|
|
|
def dump(self, file, trace):
|
2001-01-21 13:01:31 -04:00
|
|
|
# write data to file, as a C array
|
2001-01-21 18:41:08 -04:00
|
|
|
self.data.dump(file, trace)
|
2001-01-21 13:01:31 -04:00
|
|
|
file.write("#define %s_magic %d\n" % (self.name, self.magic))
|
|
|
|
file.write("#define %s_size %d\n" % (self.name, self.size))
|
|
|
|
file.write("#define %s_poly %d\n" % (self.name, self.poly))
|
|
|
|
|
2000-09-24 20:18:31 -03:00
|
|
|
# stuff to deal with arrays of unsigned integers
|
|
|
|
|
|
|
|
class Array:
|
|
|
|
|
|
|
|
def __init__(self, name, data):
|
|
|
|
self.name = name
|
|
|
|
self.data = data
|
|
|
|
|
2001-01-21 18:41:08 -04:00
|
|
|
def dump(self, file, trace=0):
|
2000-09-24 20:18:31 -03:00
|
|
|
# write data to file, as a C array
|
|
|
|
size = getsize(self.data)
|
2001-01-21 18:41:08 -04:00
|
|
|
if trace:
|
2007-08-03 14:06:41 -03:00
|
|
|
print(self.name+":", size*len(self.data), "bytes", file=sys.stderr)
|
2000-09-24 20:18:31 -03:00
|
|
|
file.write("static ")
|
|
|
|
if size == 1:
|
|
|
|
file.write("unsigned char")
|
|
|
|
elif size == 2:
|
|
|
|
file.write("unsigned short")
|
|
|
|
else:
|
|
|
|
file.write("unsigned int")
|
|
|
|
file.write(" " + self.name + "[] = {\n")
|
|
|
|
if self.data:
|
|
|
|
s = " "
|
|
|
|
for item in self.data:
|
|
|
|
i = str(item) + ", "
|
|
|
|
if len(s) + len(i) > 78:
|
|
|
|
file.write(s + "\n")
|
|
|
|
s = " " + i
|
|
|
|
else:
|
|
|
|
s = s + i
|
2002-09-11 17:36:02 -03:00
|
|
|
if s.strip():
|
2000-09-24 20:18:31 -03:00
|
|
|
file.write(s + "\n")
|
|
|
|
file.write("};\n\n")
|
|
|
|
|
|
|
|
def getsize(data):
|
|
|
|
# return smallest possible integer size for the given array
|
|
|
|
maxdata = max(data)
|
|
|
|
if maxdata < 256:
|
|
|
|
return 1
|
|
|
|
elif maxdata < 65536:
|
|
|
|
return 2
|
|
|
|
else:
|
|
|
|
return 4
|
|
|
|
|
2000-09-25 04:13:41 -03:00
|
|
|
def splitbins(t, trace=0):
|
|
|
|
"""t, trace=0 -> (t1, t2, shift). Split a table to save space.
|
|
|
|
|
|
|
|
t is a sequence of ints. This function can be useful to save space if
|
|
|
|
many of the ints are the same. t1 and t2 are lists of ints, and shift
|
|
|
|
is an int, chosen to minimize the combined size of t1 and t2 (in C
|
|
|
|
code), and where for each i in range(len(t)),
|
|
|
|
t[i] == t2[(t1[i >> shift] << shift) + (i & mask)]
|
|
|
|
where mask is a bitmask isolating the last "shift" bits.
|
|
|
|
|
2000-11-03 16:24:15 -04:00
|
|
|
If optional arg trace is non-zero (default zero), progress info
|
|
|
|
is printed to sys.stderr. The higher the value, the more info
|
|
|
|
you'll get.
|
2000-09-25 04:13:41 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
if trace:
|
|
|
|
def dump(t1, t2, shift, bytes):
|
2007-08-03 14:06:41 -03:00
|
|
|
print("%d+%d bins at shift %d; %d bytes" % (
|
|
|
|
len(t1), len(t2), shift, bytes), file=sys.stderr)
|
|
|
|
print("Size of original table:", len(t)*getsize(t), \
|
|
|
|
"bytes", file=sys.stderr)
|
2000-09-25 04:13:41 -03:00
|
|
|
n = len(t)-1 # last valid index
|
|
|
|
maxshift = 0 # the most we can shift n and still have something left
|
|
|
|
if n > 0:
|
|
|
|
while n >> 1:
|
|
|
|
n >>= 1
|
|
|
|
maxshift += 1
|
|
|
|
del n
|
2007-12-04 19:02:19 -04:00
|
|
|
bytes = sys.maxsize # smallest total size so far
|
2000-09-25 04:13:41 -03:00
|
|
|
t = tuple(t) # so slices can be dict keys
|
|
|
|
for shift in range(maxshift + 1):
|
|
|
|
t1 = []
|
|
|
|
t2 = []
|
2000-09-24 20:18:31 -03:00
|
|
|
size = 2**shift
|
|
|
|
bincache = {}
|
2000-09-25 04:13:41 -03:00
|
|
|
for i in range(0, len(t), size):
|
|
|
|
bin = t[i:i+size]
|
|
|
|
index = bincache.get(bin)
|
2000-09-24 20:18:31 -03:00
|
|
|
if index is None:
|
2000-09-25 04:13:41 -03:00
|
|
|
index = len(t2)
|
|
|
|
bincache[bin] = index
|
|
|
|
t2.extend(bin)
|
|
|
|
t1.append(index >> shift)
|
2000-09-24 20:18:31 -03:00
|
|
|
# determine memory size
|
2000-09-25 04:13:41 -03:00
|
|
|
b = len(t1)*getsize(t1) + len(t2)*getsize(t2)
|
2000-11-03 16:24:15 -04:00
|
|
|
if trace > 1:
|
2000-09-25 04:13:41 -03:00
|
|
|
dump(t1, t2, shift, b)
|
2000-09-24 20:18:31 -03:00
|
|
|
if b < bytes:
|
2000-09-25 04:13:41 -03:00
|
|
|
best = t1, t2, shift
|
2000-09-24 20:18:31 -03:00
|
|
|
bytes = b
|
2000-09-25 04:13:41 -03:00
|
|
|
t1, t2, shift = best
|
|
|
|
if trace:
|
2007-08-03 14:06:41 -03:00
|
|
|
print("Best:", end=' ', file=sys.stderr)
|
2000-09-25 04:13:41 -03:00
|
|
|
dump(t1, t2, shift, bytes)
|
|
|
|
if __debug__:
|
|
|
|
# exhaustively verify that the decomposition is correct
|
|
|
|
mask = ~((~0) << shift) # i.e., low-bit mask of shift bits
|
2007-05-07 19:24:25 -03:00
|
|
|
for i in range(len(t)):
|
2000-09-25 04:13:41 -03:00
|
|
|
assert t[i] == t2[(t1[i >> shift] << shift) + (i & mask)]
|
|
|
|
return best
|
2000-09-24 20:18:31 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2000-11-03 16:24:15 -04:00
|
|
|
maketables(1)
|