1 |
#! /usr/bin/python
|
2 |
|
3 |
# Multistage table builder
|
4 |
# (c) Peter Kankowski, 2008
|
5 |
|
6 |
import re
|
7 |
import string
|
8 |
import sys
|
9 |
|
10 |
MAX_UNICODE = 0x110000
|
11 |
NOTACHAR = 0xffffffff
|
12 |
|
13 |
# Parse a line of CaseFolding.txt, Scripts.txt, and DerivedGeneralCategory.txt file
|
14 |
def make_get_names(enum):
|
15 |
return lambda chardata: enum.index(chardata[1])
|
16 |
|
17 |
def get_case_folding_value(chardata):
|
18 |
if chardata[1] != 'C' and chardata[1] != 'S':
|
19 |
return 0
|
20 |
return int(chardata[2], 16) - int(chardata[0], 16)
|
21 |
|
22 |
def get_other_case(chardata):
|
23 |
if chardata[12] != '':
|
24 |
return int(chardata[12], 16) - int(chardata[0], 16)
|
25 |
if chardata[13] != '':
|
26 |
return int(chardata[13], 16) - int(chardata[0], 16)
|
27 |
return 0
|
28 |
|
29 |
# Read the whole table in memory
|
30 |
def read_table(file_name, get_value, default_value):
|
31 |
file = open(file_name, 'r')
|
32 |
table = [default_value] * MAX_UNICODE
|
33 |
for line in file:
|
34 |
line = re.sub(r'#.*', '', line)
|
35 |
chardata = map(string.strip, line.split(';'))
|
36 |
if len(chardata) <= 1:
|
37 |
continue
|
38 |
value = get_value(chardata)
|
39 |
|
40 |
m = re.match(r'([0-9a-fA-F]+)(\.\.([0-9a-fA-F]+))?$', chardata[0])
|
41 |
char = int(m.group(1), 16)
|
42 |
#PH last = char if m.group(3) is None else int(m.group(3), 16)
|
43 |
if m.group(3) is None:
|
44 |
last = char
|
45 |
else:
|
46 |
last = int(m.group(3), 16)
|
47 |
for i in range(char, last + 1):
|
48 |
table[i] = value
|
49 |
file.close()
|
50 |
return table
|
51 |
|
52 |
# Get the smallest possible C language type for the values
|
53 |
def get_type_size(table):
|
54 |
type_size = [("uschar", 1), ("pcre_uint16", 2), ("pcre_uint32", 4),
|
55 |
("signed char", 1), ("pcre_int16", 2), ("pcre_int32", 4)]
|
56 |
limits = [(0, 255), (0, 65535), (0, 4294967295),
|
57 |
(-128, 127), (-32768, 32767), (-2147483648, 2147483647)]
|
58 |
minval = min(table)
|
59 |
maxval = max(table)
|
60 |
for num, (minlimit, maxlimit) in enumerate(limits):
|
61 |
if minlimit <= minval and maxval <= maxlimit:
|
62 |
return type_size[num]
|
63 |
else:
|
64 |
raise OverflowError, "Too large to fit into C types"
|
65 |
|
66 |
def get_tables_size(*tables):
|
67 |
total_size = 0
|
68 |
for table in tables:
|
69 |
type, size = get_type_size(table)
|
70 |
total_size += size * len(table)
|
71 |
return total_size
|
72 |
|
73 |
# Compress the table into the two stages
|
74 |
def compress_table(table, block_size):
|
75 |
blocks = {} # Dictionary for finding identical blocks
|
76 |
stage1 = [] # Stage 1 table contains block numbers (indices into stage 2 table)
|
77 |
stage2 = [] # Stage 2 table contains the blocks with property values
|
78 |
table = tuple(table)
|
79 |
for i in range(0, len(table), block_size):
|
80 |
block = table[i:i+block_size]
|
81 |
start = blocks.get(block)
|
82 |
if start is None:
|
83 |
# Allocate a new block
|
84 |
start = len(stage2) / block_size
|
85 |
stage2 += block
|
86 |
blocks[block] = start
|
87 |
stage1.append(start)
|
88 |
|
89 |
return stage1, stage2
|
90 |
|
91 |
# Print a table
|
92 |
def print_table(table, table_name, block_size = None):
|
93 |
type, size = get_type_size(table)
|
94 |
ELEMS_PER_LINE = 16
|
95 |
|
96 |
s = "const %s %s[] = { /* %d bytes" % (type, table_name, size * len(table))
|
97 |
if block_size:
|
98 |
s += ", block = %d" % block_size
|
99 |
print s + " */"
|
100 |
table = tuple(table)
|
101 |
if block_size is None:
|
102 |
fmt = "%3d," * ELEMS_PER_LINE + " /* U+%04X */"
|
103 |
mult = MAX_UNICODE / len(table)
|
104 |
for i in range(0, len(table), ELEMS_PER_LINE):
|
105 |
print fmt % (table[i:i+ELEMS_PER_LINE] + (i * mult,))
|
106 |
else:
|
107 |
#PH fmt = "%3d," * (ELEMS_PER_LINE if block_size > ELEMS_PER_LINE else block_size) + "\n"
|
108 |
if block_size > ELEMS_PER_LINE:
|
109 |
fmt = "%3d," * ELEMS_PER_LINE + "\n"
|
110 |
else:
|
111 |
fmt = "%3d," * block_size + "\n"
|
112 |
if block_size > ELEMS_PER_LINE:
|
113 |
fmt = fmt * (block_size / ELEMS_PER_LINE)
|
114 |
for i in range(0, len(table), block_size):
|
115 |
print ("/* block %d */\n" + fmt) % ((i / block_size,) + table[i:i+block_size])
|
116 |
print "};\n"
|
117 |
|
118 |
# Extract the unique combinations of properties into records
|
119 |
def combine_tables(*tables):
|
120 |
records = {}
|
121 |
index = []
|
122 |
for t in zip(*tables):
|
123 |
i = records.get(t)
|
124 |
if i is None:
|
125 |
i = records[t] = len(records)
|
126 |
index.append(i)
|
127 |
return index, records
|
128 |
|
129 |
def print_records(records):
|
130 |
print 'const ucd_record ucd_records[] = { /* %d bytes */' % (len(records) * 4)
|
131 |
records = zip(records.keys(), records.values())
|
132 |
records.sort(None, lambda x: x[1])
|
133 |
for i, record in enumerate(records):
|
134 |
print (' {' + '%6d, ' * len(record[0]) + '}, /* %3d */') % (record[0] + (i,))
|
135 |
print '};\n'
|
136 |
|
137 |
script_names = ['Arabic', 'Armenian', 'Bengali', 'Bopomofo', 'Braille', 'Buginese', 'Buhid', 'Canadian_Aboriginal', \
|
138 |
'Cherokee', 'Common', 'Coptic', 'Cypriot', 'Cyrillic', 'Deseret', 'Devanagari', 'Ethiopic', 'Georgian', \
|
139 |
'Glagolitic', 'Gothic', 'Greek', 'Gujarati', 'Gurmukhi', 'Han', 'Hangul', 'Hanunoo', 'Hebrew', 'Hiragana', \
|
140 |
'Inherited', 'Kannada', 'Katakana', 'Kharoshthi', 'Khmer', 'Lao', 'Latin', 'Limbu', 'Linear_B', 'Malayalam', \
|
141 |
'Mongolian', 'Myanmar', 'New_Tai_Lue', 'Ogham', 'Old_Italic', 'Old_Persian', 'Oriya', 'Osmanya', 'Runic', \
|
142 |
'Shavian', 'Sinhala', 'Syloti_Nagri', 'Syriac', 'Tagalog', 'Tagbanwa', 'Tai_Le', 'Tamil', 'Telugu', 'Thaana', \
|
143 |
'Thai', 'Tibetan', 'Tifinagh', 'Ugaritic', 'Yi', \
|
144 |
'Balinese', 'Cuneiform', 'Nko', 'Phags_Pa', 'Phoenician']
|
145 |
|
146 |
category_names = ['Cc', 'Cf', 'Cn', 'Co', 'Cs', 'Ll', 'Lm', 'Lo', 'Lt', 'Lu',
|
147 |
'Mc', 'Me', 'Mn', 'Nd', 'Nl', 'No', 'Pc', 'Pd', 'Pe', 'Pf', 'Pi', 'Po', 'Ps',
|
148 |
'Sc', 'Sk', 'Sm', 'So', 'Zl', 'Zp', 'Zs' ]
|
149 |
|
150 |
|
151 |
script = read_table('Unicode.tables/Scripts.txt', make_get_names(script_names), script_names.index('Common'))
|
152 |
category = read_table('Unicode.tables/DerivedGeneralCategory.txt', make_get_names(category_names), category_names.index('Cn'))
|
153 |
other_case = read_table('Unicode.tables/UnicodeData.txt', get_other_case, 0)
|
154 |
# case_fold = read_table('CaseFolding.txt', get_case_folding_value, 0)
|
155 |
|
156 |
table, records = combine_tables(script, category, other_case)
|
157 |
|
158 |
# Find the optimum block size for the two-stage table
|
159 |
min_size = sys.maxint
|
160 |
for block_size in [2 ** i for i in range(5,10)]:
|
161 |
size = len(records) * 4
|
162 |
stage1, stage2 = compress_table(table, block_size)
|
163 |
size += get_tables_size(stage1, stage2)
|
164 |
#print "/* block size %5d => %5d bytes */" % (block_size, size)
|
165 |
if size < min_size:
|
166 |
min_size = size
|
167 |
min_stage1, min_stage2 = stage1, stage2
|
168 |
min_block_size = block_size
|
169 |
|
170 |
print "#ifdef HAVE_CONFIG_H"
|
171 |
print "#include \"config.h\""
|
172 |
print "#endif"
|
173 |
print "#include \"pcre_internal.h\""
|
174 |
print
|
175 |
print "/* Unicode character database. */"
|
176 |
print "/* This file was autogenerated by MultiStage2.py script. */"
|
177 |
print "/* Total size: %d bytes, block size: %d. */" % (min_size, min_block_size)
|
178 |
print_records(records)
|
179 |
print_table(min_stage1, 'ucd_stage1')
|
180 |
print_table(min_stage2, 'ucd_stage2', min_block_size)
|
181 |
print "#if UCD_BLOCK_SIZE != %d" % min_block_size
|
182 |
print "#error Please correct UCD_BLOCK_SIZE in pcre_internal.h"
|
183 |
print "#endif"
|
184 |
|
185 |
"""
|
186 |
|
187 |
# Three-stage tables:
|
188 |
|
189 |
# Find the optimum block size for 3-stage table
|
190 |
min_size = sys.maxint
|
191 |
for stage3_block in [2 ** i for i in range(2,6)]:
|
192 |
stage_i, stage3 = compress_table(table, stage3_block)
|
193 |
for stage2_block in [2 ** i for i in range(5,10)]:
|
194 |
size = len(records) * 4
|
195 |
stage1, stage2 = compress_table(stage_i, stage2_block)
|
196 |
size += get_tables_size(stage1, stage2, stage3)
|
197 |
# print "/* %5d / %3d => %5d bytes */" % (stage2_block, stage3_block, size)
|
198 |
if size < min_size:
|
199 |
min_size = size
|
200 |
min_stage1, min_stage2, min_stage3 = stage1, stage2, stage3
|
201 |
min_stage2_block, min_stage3_block = stage2_block, stage3_block
|
202 |
|
203 |
print "/* Total size: %d bytes" % min_size */
|
204 |
print_records(records)
|
205 |
print_table(min_stage1, 'ucd_stage1')
|
206 |
print_table(min_stage2, 'ucd_stage2', min_stage2_block)
|
207 |
print_table(min_stage3, 'ucd_stage3', min_stage3_block)
|
208 |
|
209 |
"""
|