| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  | # (re)generate unicode property and type databases | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2002-11-25 09:13:37 +00:00
										 |  |  | # this script converts a unicode 3.2 database file to | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | # Modules/unicodedata_db.h, Modules/unicodename_db.h, | 
					
						
							|  |  |  | # and Objects/unicodetype_db.h | 
					
						
							| 
									
										
										
										
											2000-09-25 08:07:06 +00: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 17:59:57 +00:00
										 |  |  | # 2000-09-25 fl   added character type table | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | # 2000-09-26 fl   added LINEBREAK, DECIMAL, and DIGIT flags/fields (2.0) | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  | # 2000-11-03 fl   expand first/last ranges | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | # 2001-01-19 fl   added character name tables (2.1) | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  | # 2001-01-21 fl   added decomp compression; dynamic phrasebook threshold | 
					
						
							| 
									
										
										
										
											2002-11-23 22:08:15 +00: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 23:05:09 +00:00
										 |  |  | # 2002-11-24 mvl  expand all ranges, sort names version-independently | 
					
						
							| 
									
										
										
										
											2002-11-25 09:13:37 +00:00
										 |  |  | # 2002-11-25 mvl  add UNIDATA_VERSION | 
					
						
							| 
									
										
										
										
											2000-09-25 08:07:06 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  | # written by Fredrik Lundh (fredrik@pythonware.com) | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SCRIPT = sys.argv[0] | 
					
						
							| 
									
										
										
										
											2002-11-25 09:13:37 +00:00
										 |  |  | VERSION = "2.2" | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-25 09:13:37 +00:00
										 |  |  | # The Unicode Database | 
					
						
							|  |  |  | UNIDATA_VERSION = "3.2.0" | 
					
						
							| 
									
										
										
										
											2002-11-23 22:08:15 +00:00
										 |  |  | UNICODE_DATA = "UnicodeData.txt" | 
					
						
							|  |  |  | COMPOSITION_EXCLUSIONS = "CompositionExclusions.txt" | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00: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", | 
					
						
							|  |  |  |     "ON" ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-25 21:01:56 +00:00
										 |  |  | # note: should match definitions in Objects/unicodectype.c | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  | ALPHA_MASK = 0x01 | 
					
						
							|  |  |  | DECIMAL_MASK = 0x02 | 
					
						
							|  |  |  | DIGIT_MASK = 0x04 | 
					
						
							|  |  |  | LOWER_MASK = 0x08 | 
					
						
							| 
									
										
										
										
											2000-09-25 21:01:56 +00:00
										 |  |  | LINEBREAK_MASK = 0x10 | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  | SPACE_MASK = 0x20 | 
					
						
							|  |  |  | TITLE_MASK = 0x40 | 
					
						
							|  |  |  | UPPER_MASK = 0x80 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  | def maketables(trace=0): | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |     print "--- Reading", UNICODE_DATA, "..." | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-23 22:08:15 +00:00
										 |  |  |     unicode = UnicodeData(UNICODE_DATA, COMPOSITION_EXCLUSIONS) | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  |     print len(filter(None, unicode.table)), "characters" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 23:31:52 +00:00
										 |  |  |     makeunicodename(unicode, trace) | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |     makeunicodedata(unicode, trace) | 
					
						
							| 
									
										
										
										
											2001-01-21 23:31:52 +00:00
										 |  |  |     makeunicodetype(unicode, trace) | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # -------------------------------------------------------------------- | 
					
						
							|  |  |  | # unicode character properties | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def makeunicodedata(unicode, trace): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-25 08:07:06 +00:00
										 |  |  |     dummy = (0, 0, 0, 0) | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |     table = [dummy] | 
					
						
							|  |  |  |     cache = {0: dummy} | 
					
						
							|  |  |  |     index = [0] * len(unicode.chars) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |     FILE = "Modules/unicodedata_db.h" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print "--- Preparing", FILE, "..." | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-25 08:07:06 +00:00
										 |  |  |     # 1) database properties | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00: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" | 
					
						
							|  |  |  |             item = ( | 
					
						
							| 
									
										
										
										
											2000-09-25 08:07:06 +00:00
										 |  |  |                 category, combining, bidirectional, mirrored | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00: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 08:07:06 +00:00
										 |  |  |     # 2) decomposition data | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     decomp_data = [0] | 
					
						
							|  |  |  |     decomp_prefix = [""] | 
					
						
							| 
									
										
										
										
											2000-09-25 08:07:06 +00:00
										 |  |  |     decomp_index = [0] * len(unicode.chars) | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     decomp_size = 0 | 
					
						
							| 
									
										
										
										
											2000-09-25 08:07:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-23 22:08:15 +00:00
										 |  |  |     comp_pairs = [] | 
					
						
							|  |  |  |     comp_first = [None] * len(unicode.chars) | 
					
						
							|  |  |  |     comp_last = [None] * len(unicode.chars) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-25 08:07:06 +00:00
										 |  |  |     for char in unicode.chars: | 
					
						
							|  |  |  |         record = unicode.table[char] | 
					
						
							|  |  |  |         if record: | 
					
						
							|  |  |  |             if record[5]: | 
					
						
							| 
									
										
										
										
											2002-09-11 20:36:02 +00:00
										 |  |  |                 decomp = record[5].split() | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00: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 | 
					
						
							|  |  |  |                 decomp = [prefix + (len(decomp)<<8)] +\ | 
					
						
							|  |  |  |                          map(lambda s: int(s, 16), decomp) | 
					
						
							| 
									
										
										
										
											2002-11-23 22:08:15 +00: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 08:07:06 +00:00
										 |  |  |                 try: | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |                     i = decomp_data.index(decomp) | 
					
						
							| 
									
										
										
										
											2000-09-25 08:07:06 +00:00
										 |  |  |                 except ValueError: | 
					
						
							|  |  |  |                     i = len(decomp_data) | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |                     decomp_data.extend(decomp) | 
					
						
							|  |  |  |                     decomp_size = decomp_size + len(decomp) * 2 | 
					
						
							| 
									
										
										
										
											2000-09-25 08:07:06 +00:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 i = 0 | 
					
						
							|  |  |  |             decomp_index[char] = i | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-23 22:08:15 +00: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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  |     print len(table), "unique properties" | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     print len(decomp_prefix), "unique decomposition prefixes" | 
					
						
							|  |  |  |     print len(decomp_data), "unique decomposition entries:", | 
					
						
							|  |  |  |     print decomp_size, "bytes" | 
					
						
							| 
									
										
										
										
											2002-11-23 22:08:15 +00:00
										 |  |  |     print total_first, "first characters in NFC" | 
					
						
							|  |  |  |     print total_last, "last characters in NFC" | 
					
						
							|  |  |  |     print len(comp_pairs), "NFC pairs" | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |     print "--- Writing", FILE, "..." | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |     fp = open(FILE, "w") | 
					
						
							|  |  |  |     print >>fp, "/* this file was generated by %s %s */" % (SCRIPT, VERSION) | 
					
						
							|  |  |  |     print >>fp | 
					
						
							| 
									
										
										
										
											2002-11-25 09:13:37 +00:00
										 |  |  |     print >>fp, '#define UNIDATA_VERSION "%s"' % UNIDATA_VERSION | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |     print >>fp, "/* a list of unique database records */" | 
					
						
							|  |  |  |     print >>fp, \ | 
					
						
							|  |  |  |           "const _PyUnicode_DatabaseRecord _PyUnicode_Database_Records[] = {" | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |     for item in table: | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |         print >>fp, "    {%d, %d, %d, %d}," % item | 
					
						
							|  |  |  |     print >>fp, "};" | 
					
						
							|  |  |  |     print >>fp | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-23 22:08:15 +00:00
										 |  |  |     print >>fp, "/* Reindexing of NFC first characters. */" | 
					
						
							|  |  |  |     print >>fp, "#define TOTAL_FIRST",total_first | 
					
						
							|  |  |  |     print >>fp, "#define TOTAL_LAST",total_last | 
					
						
							|  |  |  |     print >>fp, "struct reindex{int start;short count,index;};" | 
					
						
							|  |  |  |     print >>fp, "struct reindex nfc_first[] = {" | 
					
						
							|  |  |  |     for start,end in comp_first_ranges: | 
					
						
							|  |  |  |         print >>fp,"  { %d, %d, %d}," % (start,end-start,comp_first[start]) | 
					
						
							|  |  |  |     print >>fp,"  {0,0,0}" | 
					
						
							|  |  |  |     print >>fp,"};\n" | 
					
						
							|  |  |  |     print >>fp, "struct reindex nfc_last[] = {" | 
					
						
							|  |  |  |     for start,end in comp_last_ranges: | 
					
						
							|  |  |  |         print >>fp,"  { %d, %d, %d}," % (start,end-start,comp_last[start]) | 
					
						
							|  |  |  |     print >>fp,"  {0,0,0}" | 
					
						
							|  |  |  |     print >>fp,"};\n" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |     # FIXME: <fl> the following tables could be made static, and | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  |     # the support code moved into unicodedatabase.c | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |     print >>fp, "/* string literals */" | 
					
						
							|  |  |  |     print >>fp, "const char *_PyUnicode_CategoryNames[] = {" | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |     for name in CATEGORY_NAMES: | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |         print >>fp, "    \"%s\"," % name | 
					
						
							|  |  |  |     print >>fp, "    NULL" | 
					
						
							|  |  |  |     print >>fp, "};" | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |     print >>fp, "const char *_PyUnicode_BidirectionalNames[] = {" | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |     for name in BIDIRECTIONAL_NAMES: | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |         print >>fp, "    \"%s\"," % name | 
					
						
							|  |  |  |     print >>fp, "    NULL" | 
					
						
							|  |  |  |     print >>fp, "};" | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     print >>fp, "static const char *decomp_prefix[] = {" | 
					
						
							|  |  |  |     for name in decomp_prefix: | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |         print >>fp, "    \"%s\"," % name | 
					
						
							|  |  |  |     print >>fp, "    NULL" | 
					
						
							|  |  |  |     print >>fp, "};" | 
					
						
							| 
									
										
										
										
											2000-09-25 08:07:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  |     # split record index table | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  |     index1, index2, shift = splitbins(index, trace) | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |     print >>fp, "/* index tables for the database records */" | 
					
						
							|  |  |  |     print >>fp, "#define SHIFT", shift | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     Array("index1", index1).dump(fp, trace) | 
					
						
							|  |  |  |     Array("index2", index2).dump(fp, trace) | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  |     # split decomposition index table | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  |     index1, index2, shift = splitbins(decomp_index, trace) | 
					
						
							| 
									
										
										
										
											2000-09-25 08:07:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     print >>fp, "/* decomposition data */" | 
					
						
							|  |  |  |     Array("decomp_data", decomp_data).dump(fp, trace) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |     print >>fp, "/* index tables for the decomposition data */" | 
					
						
							|  |  |  |     print >>fp, "#define DECOMP_SHIFT", shift | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     Array("decomp_index1", index1).dump(fp, trace) | 
					
						
							|  |  |  |     Array("decomp_index2", index2).dump(fp, trace) | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-23 22:08:15 +00:00
										 |  |  |     index, index2, shift = splitbins(comp_data, trace) | 
					
						
							|  |  |  |     print >>fp, "/* NFC pairs */" | 
					
						
							|  |  |  |     print >>fp, "#define COMP_SHIFT", shift | 
					
						
							|  |  |  |     Array("comp_index", index).dump(fp, trace) | 
					
						
							|  |  |  |     Array("comp_data", index2).dump(fp, trace) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |     fp.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # -------------------------------------------------------------------- | 
					
						
							|  |  |  | # unicode character type tables | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def makeunicodetype(unicode, trace): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     FILE = "Objects/unicodetype_db.h" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print "--- Preparing", FILE, "..." | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # extract unicode types | 
					
						
							| 
									
										
										
										
											2000-09-25 21:01:56 +00:00
										 |  |  |     dummy = (0, 0, 0, 0, 0, 0) | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  |     table = [dummy] | 
					
						
							|  |  |  |     cache = {0: dummy} | 
					
						
							|  |  |  |     index = [0] * len(unicode.chars) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for char in unicode.chars: | 
					
						
							|  |  |  |         record = unicode.table[char] | 
					
						
							|  |  |  |         if record: | 
					
						
							|  |  |  |             # extract database properties | 
					
						
							|  |  |  |             category = record[2] | 
					
						
							|  |  |  |             bidirectional = record[4] | 
					
						
							|  |  |  |             flags = 0 | 
					
						
							|  |  |  |             if category in ["Lm", "Lt", "Lu", "Ll", "Lo"]: | 
					
						
							|  |  |  |                 flags |= ALPHA_MASK | 
					
						
							|  |  |  |             if category == "Ll": | 
					
						
							|  |  |  |                 flags |= LOWER_MASK | 
					
						
							| 
									
										
										
										
											2000-09-25 21:01:56 +00:00
										 |  |  |             if category == "Zl" or bidirectional == "B": | 
					
						
							|  |  |  |                 flags |= LINEBREAK_MASK | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  |             if category == "Zs" or bidirectional in ("WS", "B", "S"): | 
					
						
							|  |  |  |                 flags |= SPACE_MASK | 
					
						
							| 
									
										
										
										
											2000-09-25 23:03:34 +00:00
										 |  |  |             if category == "Lt": | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  |                 flags |= TITLE_MASK | 
					
						
							|  |  |  |             if category == "Lu": | 
					
						
							|  |  |  |                 flags |= UPPER_MASK | 
					
						
							|  |  |  |             # use delta predictor for upper/lower/title | 
					
						
							|  |  |  |             if record[12]: | 
					
						
							| 
									
										
										
										
											2002-10-18 17:34:18 +00:00
										 |  |  |                 upper = int(record[12], 16) - char | 
					
						
							|  |  |  |                 assert -32768 <= upper <= 32767 | 
					
						
							|  |  |  |                 upper = upper & 0xffff | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 upper = 0 | 
					
						
							|  |  |  |             if record[13]: | 
					
						
							| 
									
										
										
										
											2002-10-18 17:34:18 +00:00
										 |  |  |                 lower = int(record[13], 16) - char | 
					
						
							|  |  |  |                 assert -32768 <= lower <= 32767 | 
					
						
							|  |  |  |                 lower = lower & 0xffff | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 lower = 0 | 
					
						
							|  |  |  |             if record[14]: | 
					
						
							| 
									
										
										
										
											2002-10-18 17:34:18 +00:00
										 |  |  |                 title = int(record[14], 16) - char | 
					
						
							|  |  |  |                 assert -32768 <= lower <= 32767 | 
					
						
							|  |  |  |                 title = title & 0xffff | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 title = 0 | 
					
						
							| 
									
										
										
										
											2000-09-25 21:01:56 +00: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]) | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  |             item = ( | 
					
						
							| 
									
										
										
										
											2000-09-25 21:01:56 +00:00
										 |  |  |                 flags, upper, lower, title, decimal, digit | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00: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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |     print len(table), "unique character type entries" | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  |     print "--- Writing", FILE, "..." | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |     fp = open(FILE, "w") | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |     print >>fp, "/* this file was generated by %s %s */" % (SCRIPT, VERSION) | 
					
						
							|  |  |  |     print >>fp | 
					
						
							|  |  |  |     print >>fp, "/* a list of unique character type descriptors */" | 
					
						
							|  |  |  |     print >>fp, "const _PyUnicode_TypeRecord _PyUnicode_TypeRecords[] = {" | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  |     for item in table: | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |         print >>fp, "    {%d, %d, %d, %d, %d, %d}," % item | 
					
						
							|  |  |  |     print >>fp, "};" | 
					
						
							|  |  |  |     print >>fp | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # split decomposition index table | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  |     index1, index2, shift = splitbins(index, trace) | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-26 03:56:46 +00:00
										 |  |  |     print >>fp, "/* type indexes */" | 
					
						
							|  |  |  |     print >>fp, "#define SHIFT", shift | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     Array("index1", index1).dump(fp, trace) | 
					
						
							|  |  |  |     Array("index2", index2).dump(fp, trace) | 
					
						
							| 
									
										
										
										
											2000-09-25 17:59:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |     fp.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # -------------------------------------------------------------------- | 
					
						
							|  |  |  | # unicode name database | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def makeunicodename(unicode, trace): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     FILE = "Modules/unicodename_db.h" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print "--- Preparing", FILE, "..." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # 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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print len(filter(lambda n: n is not None, names)), "distinct names" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # 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)] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print n, "words in text;", b, "bytes" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     wordlist = words.items() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-24 23:05:09 +00:00
										 |  |  |     # sort on falling frequency, then by name | 
					
						
							|  |  |  |     def cmpwords((aword, alist),(bword, blist)): | 
					
						
							|  |  |  |         r = -cmp(len(alist),len(blist)) | 
					
						
							|  |  |  |         if r: | 
					
						
							|  |  |  |             return r | 
					
						
							|  |  |  |         return cmp(aword, bword) | 
					
						
							|  |  |  |     wordlist.sort(cmpwords) | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     # figure out how many phrasebook escapes we need | 
					
						
							|  |  |  |     escapes = 0 | 
					
						
							|  |  |  |     while escapes * 256 < len(wordlist): | 
					
						
							|  |  |  |         escapes = escapes + 1 | 
					
						
							|  |  |  |     print escapes, "escapes" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     short = 256 - escapes | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert short > 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print short, "short indexes in lexicon" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |     # statistics | 
					
						
							|  |  |  |     n = 0 | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     for i in range(short): | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |         n = n + len(wordlist[i][1]) | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     print n, "short indexes in phrasebook" | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     # pick the most commonly used words, and sort the rest on falling | 
					
						
							|  |  |  |     # length (to maximize overlap) | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     wordlist, wordtail = wordlist[:short], wordlist[short:] | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |     wordtail.sort(lambda a, b: len(b[0])-len(a[0])) | 
					
						
							|  |  |  |     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 20:36:02 +00:00
										 |  |  |         o = lexicon.find(ww) | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |         if o < 0: | 
					
						
							|  |  |  |             o = offset | 
					
						
							|  |  |  |             lexicon = lexicon + ww | 
					
						
							|  |  |  |             offset = offset + len(w) | 
					
						
							|  |  |  |         words[w] = len(lexicon_offset) | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |         lexicon_offset.append(o) | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lexicon = map(ord, lexicon) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # 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 22:41:08 +00:00
										 |  |  |                 if i < short: | 
					
						
							|  |  |  |                     phrasebook.append(i) | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |                     # store as two bytes | 
					
						
							|  |  |  |                     phrasebook.append((i>>8) + short) | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |                     phrasebook.append(i&255) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     assert getsize(phrasebook) == 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00: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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print "--- Writing", FILE, "..." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fp = open(FILE, "w") | 
					
						
							|  |  |  |     print >>fp, "/* this file was generated by %s %s */" % (SCRIPT, VERSION) | 
					
						
							|  |  |  |     print >>fp | 
					
						
							|  |  |  |     print >>fp, "#define NAME_MAXLEN", 256 | 
					
						
							|  |  |  |     print >>fp | 
					
						
							|  |  |  |     print >>fp, "/* lexicon */" | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     Array("lexicon", lexicon).dump(fp, trace) | 
					
						
							|  |  |  |     Array("lexicon_offset", lexicon_offset).dump(fp, trace) | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # split decomposition index table | 
					
						
							|  |  |  |     offset1, offset2, shift = splitbins(phrasebook_offset, trace) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print >>fp, "/* code->name phrasebook */" | 
					
						
							|  |  |  |     print >>fp, "#define phrasebook_shift", shift | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     print >>fp, "#define phrasebook_short", short | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     Array("phrasebook", phrasebook).dump(fp, trace) | 
					
						
							|  |  |  |     Array("phrasebook_offset1", offset1).dump(fp, trace) | 
					
						
							|  |  |  |     Array("phrasebook_offset2", offset2).dump(fp, trace) | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     print >>fp, "/* name->code dictionary */" | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |     codehash.dump(fp, trace) | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fp.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00: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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-11 20:36:02 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class UnicodeData: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-23 22:08:15 +00:00
										 |  |  |     def __init__(self, filename, exclusions, expand=1): | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |         file = open(filename) | 
					
						
							| 
									
										
										
										
											2002-10-18 16:11:54 +00:00
										 |  |  |         table = [None] * 0x110000 | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |         while 1: | 
					
						
							|  |  |  |             s = file.readline() | 
					
						
							|  |  |  |             if not s: | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2002-09-11 20:36:02 +00:00
										 |  |  |             s = s.strip().split(";") | 
					
						
							|  |  |  |             char = int(s[0], 16) | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |             table[char] = s | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-24 23:05:09 +00:00
										 |  |  |         # expand first-last ranges | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  |         if expand: | 
					
						
							|  |  |  |             field = None | 
					
						
							| 
									
										
										
										
											2002-11-24 23:05:09 +00:00
										 |  |  |             for i in range(0, 0x110000): | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  |                 s = table[i] | 
					
						
							|  |  |  |                 if s: | 
					
						
							|  |  |  |                     if s[1][-6:] == "First>": | 
					
						
							|  |  |  |                         s[1] = "" | 
					
						
							|  |  |  |                         field = s[:] | 
					
						
							|  |  |  |                     elif s[1][-5:] == "Last>": | 
					
						
							|  |  |  |                         s[1] = "" | 
					
						
							|  |  |  |                         field = None | 
					
						
							|  |  |  |                 elif field: | 
					
						
							|  |  |  |                     field[0] = hex(i) | 
					
						
							|  |  |  |                     table[i] = field | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |         # public attributes | 
					
						
							|  |  |  |         self.filename = filename | 
					
						
							|  |  |  |         self.table = table | 
					
						
							| 
									
										
										
										
											2002-10-18 16:11:54 +00:00
										 |  |  |         self.chars = range(0x110000) # unicode 3.2 | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-23 22:08:15 +00:00
										 |  |  |         file = open(exclusions) | 
					
						
							|  |  |  |         self.exclusions = {} | 
					
						
							|  |  |  |         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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |     def uselatin1(self): | 
					
						
							|  |  |  |         # restrict character range to ISO Latin 1 | 
					
						
							|  |  |  |         self.chars = range(256) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00: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 20:36:02 +00:00
										 |  |  |     for c in map(ord, s.upper()): | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |         h = (h * magic) + c | 
					
						
							| 
									
										
										
										
											2002-11-24 23:05:09 +00:00
										 |  |  |         ix = h & 0xff000000L | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00: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: | 
					
						
							|  |  |  |             raise AssertionError, "ran out of polynominals" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         print size, "slots in hash table" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         print n, "collisions" | 
					
						
							|  |  |  |         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 22:41:08 +00:00
										 |  |  |     def dump(self, file, trace): | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00:00
										 |  |  |         # write data to file, as a C array | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |         self.data.dump(file, trace) | 
					
						
							| 
									
										
										
										
											2001-01-21 17:01:31 +00: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 23:18:31 +00: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 22:41:08 +00:00
										 |  |  |     def dump(self, file, trace=0): | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |         # write data to file, as a C array | 
					
						
							|  |  |  |         size = getsize(self.data) | 
					
						
							| 
									
										
										
										
											2001-01-21 22:41:08 +00:00
										 |  |  |         if trace: | 
					
						
							|  |  |  |             print >>sys.stderr, self.name+":", size*len(self.data), "bytes" | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00: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 20:36:02 +00:00
										 |  |  |             if s.strip(): | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00: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 07:13:41 +00: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 20:24:15 +00: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 07:13:41 +00:00
										 |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     import sys | 
					
						
							|  |  |  |     if trace: | 
					
						
							|  |  |  |         def dump(t1, t2, shift, bytes): | 
					
						
							|  |  |  |             print >>sys.stderr, "%d+%d bins at shift %d; %d bytes" % ( | 
					
						
							|  |  |  |                 len(t1), len(t2), shift, bytes) | 
					
						
							|  |  |  |         print >>sys.stderr, "Size of original table:", len(t)*getsize(t), \ | 
					
						
							|  |  |  |                             "bytes" | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  |     bytes = sys.maxint  # smallest total size so far | 
					
						
							|  |  |  |     t = tuple(t)    # so slices can be dict keys | 
					
						
							|  |  |  |     for shift in range(maxshift + 1): | 
					
						
							|  |  |  |         t1 = [] | 
					
						
							|  |  |  |         t2 = [] | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |         size = 2**shift | 
					
						
							|  |  |  |         bincache = {} | 
					
						
							| 
									
										
										
										
											2000-09-25 07:13:41 +00:00
										 |  |  |         for i in range(0, len(t), size): | 
					
						
							|  |  |  |             bin = t[i:i+size] | 
					
						
							|  |  |  |             index = bincache.get(bin) | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |             if index is None: | 
					
						
							| 
									
										
										
										
											2000-09-25 07:13:41 +00:00
										 |  |  |                 index = len(t2) | 
					
						
							|  |  |  |                 bincache[bin] = index | 
					
						
							|  |  |  |                 t2.extend(bin) | 
					
						
							|  |  |  |             t1.append(index >> shift) | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |         # determine memory size | 
					
						
							| 
									
										
										
										
											2000-09-25 07:13:41 +00:00
										 |  |  |         b = len(t1)*getsize(t1) + len(t2)*getsize(t2) | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  |         if trace > 1: | 
					
						
							| 
									
										
										
										
											2000-09-25 07:13:41 +00:00
										 |  |  |             dump(t1, t2, shift, b) | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |         if b < bytes: | 
					
						
							| 
									
										
										
										
											2000-09-25 07:13:41 +00:00
										 |  |  |             best = t1, t2, shift | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  |             bytes = b | 
					
						
							| 
									
										
										
										
											2000-09-25 07:13:41 +00:00
										 |  |  |     t1, t2, shift = best | 
					
						
							|  |  |  |     if trace: | 
					
						
							|  |  |  |         print >>sys.stderr, "Best:", | 
					
						
							|  |  |  |         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 | 
					
						
							|  |  |  |         for i in xrange(len(t)): | 
					
						
							|  |  |  |             assert t[i] == t2[(t1[i >> shift] << shift) + (i & mask)] | 
					
						
							|  |  |  |     return best | 
					
						
							| 
									
										
										
										
											2000-09-24 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2000-11-03 20:24:15 +00:00
										 |  |  |     maketables(1) |