| 1 |
|
| 2 |
from __future__ import print_function |
| 3 |
|
| 4 |
import sys, os |
| 5 |
import itertools |
| 6 |
from collections import defaultdict |
| 7 |
|
| 8 |
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) |
| 9 |
|
| 10 |
from rpython.rlib.rarithmetic import r_longlong, r_int32, r_uint32, intmask |
| 11 |
from rpython.rtyper.lltypesystem.rffi import r_ushort, r_short |
| 12 |
from rpython.rlib.unicodedata.codegen import CodeWriter, getsize_unsigned, get_char_list_offset |
| 13 |
|
| 14 |
MAXUNICODE = 0x10FFFF |
| 15 |
|
| 16 |
MANDATORY_LINE_BREAKS = ["BK", "CR", "LF", "NL"] |
| 17 |
|
| 18 |
|
| 19 |
PUA_1 = range(0xE000, 0xF900) |
| 20 |
PUA_15 = range(0xF0000, 0xFFFFE) |
| 21 |
PUA_16 = range(0x100000, 0x10FFFE) |
| 22 |
|
| 23 |
class Fraction: |
| 24 |
def __init__(self, numerator, denominator): |
| 25 |
self.numerator = numerator |
| 26 |
self.denominator = denominator |
| 27 |
|
| 28 |
def __repr__(self): |
| 29 |
return '%r / %r' % (self.numerator, self.denominator) |
| 30 |
|
| 31 |
def __str__(self): |
| 32 |
return repr(self) |
| 33 |
|
| 34 |
class UnicodeChar: |
| 35 |
def __init__(self, data=None): |
| 36 |
if data is None: |
| 37 |
return |
| 38 |
if not data[1] or data[1][0] == '<' and data[1][-1] == '>': |
| 39 |
self.name = None |
| 40 |
else: |
| 41 |
self.name = data[1] |
| 42 |
self.category = data[2] |
| 43 |
self.east_asian_width = 'N' |
| 44 |
self.combining = 0 |
| 45 |
if data[3]: |
| 46 |
self.combining = int(data[3]) |
| 47 |
self.bidirectional = data[4] |
| 48 |
self.raw_decomposition = '' |
| 49 |
self.decomposition = [] |
| 50 |
self.isCompatibility = False |
| 51 |
self.canonical_decomp = None |
| 52 |
self.compat_decomp = None |
| 53 |
self.excluded = False |
| 54 |
self.linebreak = False |
| 55 |
self.decompositionTag = '' |
| 56 |
self.properties = () |
| 57 |
self.casefolding = None |
| 58 |
if data[5]: |
| 59 |
self.raw_decomposition = data[5] |
| 60 |
if data[5][0] == '<': |
| 61 |
self.isCompatibility = True |
| 62 |
self.decompositionTag, decomp = data[5].split(None, 1) |
| 63 |
else: |
| 64 |
decomp = data[5] |
| 65 |
self.decomposition = map(lambda x:int(x, 16), decomp.split()) |
| 66 |
self.decimal = None |
| 67 |
if data[6]: |
| 68 |
self.decimal = int(data[6]) |
| 69 |
self.digit = None |
| 70 |
if data[7]: |
| 71 |
self.digit = int(data[7]) |
| 72 |
self.numeric = None |
| 73 |
if data[8]: |
| 74 |
try: |
| 75 |
numerator, denomenator = data[8].split('/') |
| 76 |
self.numeric = Fraction(float(numerator), float(denomenator)) |
| 77 |
except ValueError: |
| 78 |
self.numeric = float(data[8]) |
| 79 |
self.mirrored = (data[9] == 'Y') |
| 80 |
self.upper = None |
| 81 |
if data[12]: |
| 82 |
self.upper = int(data[12], 16) |
| 83 |
self.lower = None |
| 84 |
if data[13]: |
| 85 |
self.lower = int(data[13], 16) |
| 86 |
self.title = None |
| 87 |
if data[14]: |
| 88 |
self.title = int(data[14], 16) |
| 89 |
|
| 90 |
def copy(self): |
| 91 |
uc = UnicodeChar() |
| 92 |
uc.__dict__.update(self.__dict__) |
| 93 |
return uc |
| 94 |
|
| 95 |
class UnicodeData(object): |
| 96 |
|
| 97 |
NAME_ALIASES_START = 0xF0000 |
| 98 |
NAMED_SEQUENCES_START = 0xF0200 |
| 99 |
|
| 100 |
def __init__(self): |
| 101 |
self.table = [None] * (MAXUNICODE + 1) |
| 102 |
self.aliases = [] |
| 103 |
self.named_sequences = [] |
| 104 |
|
| 105 |
def add_char(self, code, char): |
| 106 |
assert self.table[code] is None, ( |
| 107 |
'Multiply defined character %04X' % code) |
| 108 |
if isinstance(char, list): |
| 109 |
char = UnicodeChar(char) |
| 110 |
self.table[code] = char |
| 111 |
return char |
| 112 |
|
| 113 |
def all_codes(self): |
| 114 |
return range(len(self.table)) |
| 115 |
|
| 116 |
def enum_chars(self): |
| 117 |
for code in range(len(self.table)): |
| 118 |
yield code, self.table[code] |
| 119 |
|
| 120 |
def get_char(self, code): |
| 121 |
return self.table[code] |
| 122 |
|
| 123 |
def clone_char(self, code): |
| 124 |
clone = self.table[code] = self.table[code].copy() |
| 125 |
return clone |
| 126 |
|
| 127 |
def set_excluded(self, code): |
| 128 |
self.table[code].excluded = True |
| 129 |
|
| 130 |
def set_linebreak(self, code): |
| 131 |
self.table[code].linebreak = True |
| 132 |
|
| 133 |
def set_east_asian_width(self, code, width): |
| 134 |
self.table[code].east_asian_width = width |
| 135 |
|
| 136 |
def add_property(self, code, p): |
| 137 |
self.table[code].properties += (p,) |
| 138 |
|
| 139 |
def get_compat_decomposition(self, code): |
| 140 |
if not self.table[code].decomposition: |
| 141 |
return [code] |
| 142 |
if not self.table[code].compat_decomp: |
| 143 |
result = [] |
| 144 |
for decomp in self.table[code].decomposition: |
| 145 |
result.extend(self.get_compat_decomposition(decomp)) |
| 146 |
self.table[code].compat_decomp = result |
| 147 |
return self.table[code].compat_decomp |
| 148 |
|
| 149 |
def get_canonical_decomposition(self, code): |
| 150 |
if (not self.table[code].decomposition or |
| 151 |
self.table[code].isCompatibility): |
| 152 |
return [code] |
| 153 |
if not self.table[code].canonical_decomp: |
| 154 |
result = [] |
| 155 |
for decomp in self.table[code].decomposition: |
| 156 |
result.extend(self.get_canonical_decomposition(decomp)) |
| 157 |
self.table[code].canonical_decomp = result |
| 158 |
return self.table[code].canonical_decomp |
| 159 |
|
| 160 |
def add_alias(self, name, char): |
| 161 |
pua_index = self.NAME_ALIASES_START + len(self.aliases) |
| 162 |
self.aliases.append((name, char)) |
| 163 |
|
| 164 |
self.table[pua_index].name = name |
| 165 |
|
| 166 |
def add_named_sequence(self, name, chars): |
| 167 |
pua_index = self.NAMED_SEQUENCES_START + len(self.named_sequences) |
| 168 |
self.named_sequences.append((name, chars)) |
| 169 |
|
| 170 |
self.table[pua_index].name = name |
| 171 |
|
| 172 |
def add_casefold_sequence(self, code, chars): |
| 173 |
self.table[code].casefolding = chars |
| 174 |
|
| 175 |
|
| 176 |
def read_unicodedata(files): |
| 177 |
rangeFirst = {} |
| 178 |
rangeLast = {} |
| 179 |
table = UnicodeData() |
| 180 |
for line in files['data']: |
| 181 |
line = line.split('#', 1)[0].strip() |
| 182 |
if not line: |
| 183 |
continue |
| 184 |
data = [ v.strip() for v in line.split(';') ] |
| 185 |
if data[1].endswith(', First>'): |
| 186 |
code = int(data[0], 16) |
| 187 |
name = data[1][1:-len(', First>')] |
| 188 |
rangeFirst[name] = (code, data) |
| 189 |
continue |
| 190 |
if data[1].endswith(', Last>'): |
| 191 |
code = int(data[0], 16) |
| 192 |
rangeLast[name] = code |
| 193 |
continue |
| 194 |
code = int(data[0], 16) |
| 195 |
table.add_char(code, data) |
| 196 |
|
| 197 |
|
| 198 |
ranges = {} |
| 199 |
for name, (start, data) in rangeFirst.iteritems(): |
| 200 |
end = rangeLast[name] |
| 201 |
ranges[(start, end)] = ['0000', None] + data[2:] |
| 202 |
|
| 203 |
|
| 204 |
for line in files['exclusions']: |
| 205 |
line = line.split('#', 1)[0].strip() |
| 206 |
if not line: |
| 207 |
continue |
| 208 |
table.set_excluded(int(line, 16)) |
| 209 |
|
| 210 |
|
| 211 |
for line in files['linebreak']: |
| 212 |
line = line.split('#', 1)[0].strip() |
| 213 |
if not line: |
| 214 |
continue |
| 215 |
data = [v.strip() for v in line.split(';')] |
| 216 |
if len(data) < 2 or data[1] not in MANDATORY_LINE_BREAKS: |
| 217 |
continue |
| 218 |
if '..' not in data[0]: |
| 219 |
first = last = int(data[0], 16) |
| 220 |
else: |
| 221 |
first, last = [int(c, 16) for c in data[0].split('..')] |
| 222 |
for char in range(first, last+1): |
| 223 |
table.set_linebreak(char) |
| 224 |
|
| 225 |
|
| 226 |
for (first, last), data in ranges.iteritems(): |
| 227 |
for code in range(first, last + 1): |
| 228 |
table.add_char(code, data) |
| 229 |
|
| 230 |
|
| 231 |
for line in files['east_asian_width']: |
| 232 |
line = line.split('#', 1)[0].strip() |
| 233 |
if not line: |
| 234 |
continue |
| 235 |
code, width = line.split(';') |
| 236 |
if '..' in code: |
| 237 |
first, last = map(lambda x:int(x,16), code.split('..')) |
| 238 |
for code in range(first, last + 1): |
| 239 |
uc = table.get_char(code) |
| 240 |
if not uc: |
| 241 |
uc = table.add_char(code, ['0000', None, |
| 242 |
'Cn'] + [''] * 12) |
| 243 |
uc.east_asian_width = width |
| 244 |
else: |
| 245 |
table.set_east_asian_width(int(code, 16), width) |
| 246 |
|
| 247 |
|
| 248 |
for line in files['derived_core_properties']: |
| 249 |
line = line.split('#', 1)[0].strip() |
| 250 |
if not line: |
| 251 |
continue |
| 252 |
|
| 253 |
r, p = line.split(";") |
| 254 |
r = r.strip() |
| 255 |
p = p.strip() |
| 256 |
if ".." in r: |
| 257 |
first, last = [int(c, 16) for c in r.split('..')] |
| 258 |
chars = list(range(first, last+1)) |
| 259 |
else: |
| 260 |
chars = [int(r, 16)] |
| 261 |
for char in chars: |
| 262 |
if not table.get_char(char): |
| 263 |
|
| 264 |
|
| 265 |
continue |
| 266 |
table.add_property(char, p) |
| 267 |
|
| 268 |
defaultChar = UnicodeChar(['0000', None, 'Cn'] + [''] * 12) |
| 269 |
for code, char in table.enum_chars(): |
| 270 |
if not char: |
| 271 |
table.add_char(code, defaultChar) |
| 272 |
|
| 273 |
extra_numeric = read_unihan(files['unihan']) |
| 274 |
for code, value in extra_numeric.iteritems(): |
| 275 |
table.clone_char(code).numeric = value |
| 276 |
|
| 277 |
table.special_casing = {} |
| 278 |
if 'special_casing' in files: |
| 279 |
for line in files['special_casing']: |
| 280 |
line = line[:-1].split('#', 1)[0] |
| 281 |
if not line: |
| 282 |
continue |
| 283 |
data = line.split("; ") |
| 284 |
if data[4]: |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
continue |
| 289 |
c = int(data[0], 16) |
| 290 |
lower = [int(char, 16) for char in data[1].split()] |
| 291 |
title = [int(char, 16) for char in data[2].split()] |
| 292 |
upper = [int(char, 16) for char in data[3].split()] |
| 293 |
table.special_casing[c] = (lower, title, upper) |
| 294 |
|
| 295 |
|
| 296 |
for code, char in table.enum_chars(): |
| 297 |
table.get_canonical_decomposition(code) |
| 298 |
table.get_compat_decomposition(code) |
| 299 |
|
| 300 |
|
| 301 |
for line in files['name_aliases']: |
| 302 |
line = line.strip() |
| 303 |
if not line or line.startswith('#'): |
| 304 |
continue |
| 305 |
items = line.split(';') |
| 306 |
char = int(items[0], 16) |
| 307 |
name = items[1] |
| 308 |
table.add_alias(name, char) |
| 309 |
|
| 310 |
|
| 311 |
for line in files['named_sequences']: |
| 312 |
line = line.strip() |
| 313 |
if not line or line.startswith('#'): |
| 314 |
continue |
| 315 |
name, chars = line.split(';') |
| 316 |
chars = tuple(int(char, 16) for char in chars.split()) |
| 317 |
table.add_named_sequence(name, chars) |
| 318 |
|
| 319 |
|
| 320 |
for line in files['casefolding']: |
| 321 |
line = line.strip().split('#', 1)[0] |
| 322 |
if not line or line.startswith('#'): |
| 323 |
continue |
| 324 |
code, status, mapping, _ = line.split('; ') |
| 325 |
code = int(code, 16) |
| 326 |
if status in 'CF': |
| 327 |
chars = [int(char, 16) for char in mapping.split()] |
| 328 |
table.add_casefold_sequence(code, chars) |
| 329 |
|
| 330 |
return table |
| 331 |
|
| 332 |
def read_unihan(unihan_file): |
| 333 |
if unihan_file is None: |
| 334 |
return {} |
| 335 |
extra_numeric = {} |
| 336 |
for line in unihan_file: |
| 337 |
if not line.startswith('U+'): |
| 338 |
continue |
| 339 |
code, tag, value = line.split(None, 3)[:3] |
| 340 |
if tag not in ('kAccountingNumeric', 'kPrimaryNumeric', 'kOtherNumeric'): |
| 341 |
continue |
| 342 |
value = value.strip().replace(',', '') |
| 343 |
if '/' in value: |
| 344 |
numerator, denomenator = value.split('/') |
| 345 |
numeric = Fraction(float(numerator), float(denomenator)) |
| 346 |
else: |
| 347 |
numeric = float(value) |
| 348 |
code = int(code[2:], 16) |
| 349 |
extra_numeric[code] = numeric |
| 350 |
return extra_numeric |
| 351 |
|
| 352 |
class Database(object): |
| 353 |
""" Code generation for compactly storing a "database", which maps |
| 354 |
consecutive integers (typically 0, ..., maxunicode) to tuples of |
| 355 |
information about them (called the columns). The storage is compact if the |
| 356 |
tuples repeat a lot. """ |
| 357 |
|
| 358 |
def __init__(self, outfile, name, column_headers, need_index=True): |
| 359 |
self.outfile = outfile |
| 360 |
self.records = [] |
| 361 |
self.records_index = {} |
| 362 |
self.name = name |
| 363 |
|
| 364 |
self.input_to_record_index = [] |
| 365 |
self.column_headers = column_headers |
| 366 |
self.need_index = need_index |
| 367 |
|
| 368 |
def add_entry(self, data): |
| 369 |
data = tuple(data) |
| 370 |
assert len(data) == len(self.column_headers) |
| 371 |
if data not in self.records_index: |
| 372 |
self.records_index[data] = len(self.records) |
| 373 |
self.records.append(data) |
| 374 |
index = self.records_index[data] |
| 375 |
self.input_to_record_index.append(index) |
| 376 |
return index |
| 377 |
|
| 378 |
def output(self): |
| 379 |
print("====", self.name, "====") |
| 380 |
print("number of records", len(self.records)) |
| 381 |
self._output_columns() |
| 382 |
if self.need_index: |
| 383 |
self._output_index() |
| 384 |
|
| 385 |
def _output_columns(self): |
| 386 |
prefix = "_" + self.name + "_" |
| 387 |
for tupindex, name in enumerate(self.column_headers): |
| 388 |
columndata = [record[tupindex] for record in self.records] |
| 389 |
self.outfile.print_listlike("lookup" + prefix + name, columndata) |
| 390 |
|
| 391 |
def _output_index(self): |
| 392 |
prefix = "_" + self.name + "_" |
| 393 |
lookupfuncname = prefix + 'index' |
| 394 |
write_pages(self.outfile, prefix, lookupfuncname, self.input_to_record_index) |
| 395 |
|
| 396 |
def write_pages(outfile, prefix, lookupfuncname, data): |
| 397 |
pgtbl, pages, pgbits = splitbins(data) |
| 398 |
pgsize = 1 << pgbits |
| 399 |
bytemask = ~(-1 << pgbits) |
| 400 |
outfile.print_listlike(prefix + "pgtbl", pgtbl) |
| 401 |
outfile.print_listlike(prefix + "pages", pages) |
| 402 |
outfile.print_code(''' |
| 403 |
def %s(code): |
| 404 |
return %spages((%spgtbl(code >> %d) << %d) + (code & %d)) |
| 405 |
'''%(lookupfuncname, prefix, prefix, pgbits, pgbits, bytemask)) |
| 406 |
|
| 407 |
def writeDbRecord(outfile, table, char_list_index, base_mod): |
| 408 |
IS_SPACE = 1 |
| 409 |
IS_ALPHA = 2 |
| 410 |
IS_LINEBREAK = 4 |
| 411 |
IS_UPPER = 8 |
| 412 |
IS_TITLE = 16 |
| 413 |
IS_LOWER = 32 |
| 414 |
IS_NUMERIC = 64 |
| 415 |
IS_DIGIT = 128 |
| 416 |
IS_DECIMAL = 256 |
| 417 |
IS_MIRRORED = 512 |
| 418 |
IS_XID_START = 1024 |
| 419 |
IS_XID_CONTINUE = 2048 |
| 420 |
IS_PRINTABLE = 4096 |
| 421 |
IS_CASE_IGNORABLE = 8192 |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
sc_db = Database(outfile, "special_casing", ("lower_len", "lower", |
| 426 |
"title_len", "title", "upper_len", "upper", "casefold_len", |
| 427 |
"casefold"), need_index=False) |
| 428 |
sc_code_to_index = {} |
| 429 |
for code, char in table.enum_chars(): |
| 430 |
sc_data = table.special_casing.get(code, (None, None, None)) |
| 431 |
if sc_data != (None, None, None): |
| 432 |
full_lower = sc_data[0] |
| 433 |
elif char.lower is None: |
| 434 |
full_lower = [code] |
| 435 |
else: |
| 436 |
full_lower = [char.lower] |
| 437 |
|
| 438 |
|
| 439 |
full_casefold = char.casefolding |
| 440 |
if full_casefold is None: |
| 441 |
full_casefold = [code] |
| 442 |
|
| 443 |
|
| 444 |
if full_casefold != full_lower: |
| 445 |
sc_data += (full_casefold, ) |
| 446 |
else: |
| 447 |
sc_data += (None, ) |
| 448 |
if sc_data != (None, None, None, None): |
| 449 |
columns = [] |
| 450 |
for cl in sc_data: |
| 451 |
if cl is None: |
| 452 |
columns.append(0) |
| 453 |
columns.append(0) |
| 454 |
else: |
| 455 |
columns.append(len(cl)) |
| 456 |
columns.append(get_char_list_offset( |
| 457 |
cl, None, char_list_index)) |
| 458 |
sc_code_to_index[code] = sc_db.add_entry(columns) |
| 459 |
sc_db.output() |
| 460 |
|
| 461 |
|
| 462 |
db = Database(outfile, "db", ("category", "bidirectional", "east_asian_width", "numeric", "decimal", |
| 463 |
"digit", "upperdist", "lowerdist", "titledist", "special_casing_index", |
| 464 |
"flags")) |
| 465 |
for code, char in table.enum_chars(): |
| 466 |
|
| 467 |
decimal = digit = 0 |
| 468 |
numeric = 0.0 |
| 469 |
|
| 470 |
|
| 471 |
flags = 0 |
| 472 |
if char.category == "Zs" or char.bidirectional in ("WS", "B", "S"): |
| 473 |
flags |= IS_SPACE |
| 474 |
if char.category in ("Lm", "Lt", "Lu", "Ll", "Lo"): |
| 475 |
flags |= IS_ALPHA |
| 476 |
if char.linebreak or char.bidirectional == "B": |
| 477 |
flags |= IS_LINEBREAK |
| 478 |
if char.numeric is not None: |
| 479 |
flags |= IS_NUMERIC |
| 480 |
numeric = char.numeric |
| 481 |
if char.digit is not None: |
| 482 |
flags |= IS_DIGIT |
| 483 |
digit = char.digit |
| 484 |
if char.decimal is not None: |
| 485 |
flags |= IS_DECIMAL |
| 486 |
decimal = char.decimal |
| 487 |
if char.category == "Lu" or (table.upper_lower_from_properties and |
| 488 |
"Uppercase" in char.properties): |
| 489 |
flags |= IS_UPPER |
| 490 |
if char.category == "Lt": |
| 491 |
flags |= IS_TITLE |
| 492 |
if char.category == "Ll" or (table.upper_lower_from_properties and |
| 493 |
"Lowercase" in char.properties): |
| 494 |
flags |= IS_LOWER |
| 495 |
if char.mirrored: |
| 496 |
flags |= IS_MIRRORED |
| 497 |
if code == ord(" ") or char.category[0] not in ("C", "Z"): |
| 498 |
flags |= IS_PRINTABLE |
| 499 |
if "XID_Start" in char.properties: |
| 500 |
flags |= IS_XID_START |
| 501 |
if "XID_Continue" in char.properties: |
| 502 |
flags |= IS_XID_CONTINUE |
| 503 |
if "Case_Ignorable" in char.properties: |
| 504 |
flags |= IS_CASE_IGNORABLE |
| 505 |
|
| 506 |
|
| 507 |
upperdist = 0 |
| 508 |
if char.upper: |
| 509 |
if code < 128: |
| 510 |
assert ord('a') <= code <= ord('z') |
| 511 |
assert char.upper == code - 32 |
| 512 |
assert code not in table.special_casing |
| 513 |
else: |
| 514 |
upperdist = code - char.upper |
| 515 |
lowerdist = 0 |
| 516 |
if char.lower: |
| 517 |
if code < 128: |
| 518 |
assert ord('A') <= code <= ord('Z') |
| 519 |
assert char.lower == code + 32 |
| 520 |
assert code not in table.special_casing |
| 521 |
else: |
| 522 |
lowerdist = code - char.lower |
| 523 |
if char.title: |
| 524 |
titledist = code - char.title |
| 525 |
else: |
| 526 |
titledist = 0 |
| 527 |
|
| 528 |
special_casing_index = sc_code_to_index.get(code, -1) |
| 529 |
db_record = (char.category, char.bidirectional, |
| 530 |
char.east_asian_width, numeric, decimal, digit, upperdist, |
| 531 |
lowerdist, titledist, special_casing_index, flags) |
| 532 |
db.add_entry(db_record) |
| 533 |
db.output() |
| 534 |
|
| 535 |
outfile.print_code('def category(code): return lookup_db_category(_db_index(code))') |
| 536 |
outfile.print_code('def bidirectional(code): return lookup_db_bidirectional(_db_index(code))') |
| 537 |
outfile.print_code('def east_asian_width(code): return lookup_db_east_asian_width(_db_index(code))') |
| 538 |
outfile.print_code('def isspace(code): return lookup_db_flags(_db_index(code)) & %d != 0'% IS_SPACE) |
| 539 |
outfile.print_code('def isalpha(code): return lookup_db_flags(_db_index(code)) & %d != 0'% IS_ALPHA) |
| 540 |
outfile.print_code('def islinebreak(code): return lookup_db_flags(_db_index(code)) & %d != 0'% IS_LINEBREAK) |
| 541 |
outfile.print_code('def isnumeric(code): return lookup_db_flags(_db_index(code)) & %d != 0'% IS_NUMERIC) |
| 542 |
outfile.print_code('def isdigit(code): return lookup_db_flags(_db_index(code)) & %d != 0'% IS_DIGIT) |
| 543 |
outfile.print_code('def isdecimal(code): return lookup_db_flags(_db_index(code)) & %d != 0'% IS_DECIMAL) |
| 544 |
outfile.print_code('def isalnum(code): return lookup_db_flags(_db_index(code)) & %d != 0'% (IS_ALPHA | IS_NUMERIC)) |
| 545 |
outfile.print_code('def isupper(code): return lookup_db_flags(_db_index(code)) & %d != 0'% IS_UPPER) |
| 546 |
outfile.print_code('def istitle(code): return lookup_db_flags(_db_index(code)) & %d != 0'% IS_TITLE) |
| 547 |
outfile.print_code('def islower(code): return lookup_db_flags(_db_index(code)) & %d != 0'% IS_LOWER) |
| 548 |
outfile.print_code('def iscased(code): return lookup_db_flags(_db_index(code)) & %d != 0'% (IS_UPPER | IS_TITLE | IS_LOWER)) |
| 549 |
outfile.print_code('def isxidstart(code): return lookup_db_flags(_db_index(code)) & %d != 0'% (IS_XID_START)) |
| 550 |
outfile.print_code('def isxidcontinue(code): return lookup_db_flags(_db_index(code)) & %d != 0'% (IS_XID_CONTINUE)) |
| 551 |
outfile.print_code('def isprintable(code): return lookup_db_flags(_db_index(code)) & %d != 0'% IS_PRINTABLE) |
| 552 |
outfile.print_code('def mirrored(code): return lookup_db_flags(_db_index(code)) & %d != 0'% IS_MIRRORED) |
| 553 |
outfile.print_code('def iscaseignorable(code): return lookup_db_flags(_db_index(code)) & %d != 0'% IS_CASE_IGNORABLE) |
| 554 |
outfile.print_code(''' |
| 555 |
def decimal(code): |
| 556 |
if isdecimal(code): |
| 557 |
return lookup_db_decimal(_db_index(code)) |
| 558 |
else: |
| 559 |
raise KeyError |
| 560 |
|
| 561 |
def digit(code): |
| 562 |
if isdigit(code): |
| 563 |
return lookup_db_digit(_db_index(code)) |
| 564 |
else: |
| 565 |
raise KeyError |
| 566 |
|
| 567 |
def numeric(code): |
| 568 |
if isnumeric(code): |
| 569 |
return lookup_db_numeric(_db_index(code)) |
| 570 |
else: |
| 571 |
raise KeyError |
| 572 |
|
| 573 |
''') |
| 574 |
|
| 575 |
def get_index(d, l, key): |
| 576 |
try: |
| 577 |
return d[key] |
| 578 |
except KeyError: |
| 579 |
res = len(l) |
| 580 |
d[key] = res |
| 581 |
l.append(key) |
| 582 |
return res |
| 583 |
|
| 584 |
def write_composition_data(outfile, table, char_list_index, base_mod): |
| 585 |
|
| 586 |
compositions = [] |
| 587 |
for code, unichar in table.enum_chars(): |
| 588 |
if (not unichar.decomposition or |
| 589 |
unichar.isCompatibility or |
| 590 |
unichar.excluded or |
| 591 |
len(unichar.decomposition) != 2 or |
| 592 |
table.get_char(unichar.decomposition[0]).combining): |
| 593 |
continue |
| 594 |
left, right = unichar.decomposition |
| 595 |
compositions.append((left, right, code)) |
| 596 |
|
| 597 |
|
| 598 |
left_index = {} |
| 599 |
right_index = {} |
| 600 |
for left, right, code in compositions: |
| 601 |
if left not in left_index: |
| 602 |
left_index[left] = len(left_index) |
| 603 |
if right not in right_index: |
| 604 |
right_index[right] = len(right_index) |
| 605 |
composition_values = [0] * (len(left_index) * len(right_index)) |
| 606 |
for left, right, code in compositions: |
| 607 |
composition_values[left_index[left] * len(right_index) + right_index[right]] = code |
| 608 |
|
| 609 |
print("composition_values") |
| 610 |
write_pages(outfile, "_comp_pairs_", "_composition", composition_values) |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
def get_index_comp(comp): |
| 615 |
return get_char_list_offset(comp, None, char_list_index) |
| 616 |
|
| 617 |
db = Database(outfile, "composition", ("prefix_index", "decomp_len", |
| 618 |
"decomp", "compat_decomp_len", "compat_decomp", "canon_decomp_len", |
| 619 |
"canon_decomp", "combining", "left_index", "right_index")) |
| 620 |
|
| 621 |
prefixes = {'': 0} |
| 622 |
prefix_list = [''] |
| 623 |
|
| 624 |
composition_db_index = [] |
| 625 |
for code, char in table.enum_chars(): |
| 626 |
prefix_index = 0 |
| 627 |
decomp_len = 0 |
| 628 |
decomp = 0 |
| 629 |
compat_decomp = 0 |
| 630 |
compat_decomp_len = 0 |
| 631 |
canon_decomp = 0 |
| 632 |
canon_decomp_len = 0 |
| 633 |
combining = 0 |
| 634 |
if char.raw_decomposition: |
| 635 |
if char.isCompatibility: |
| 636 |
index = char.raw_decomposition.index("> ") |
| 637 |
prefix = char.raw_decomposition[:index + 1] |
| 638 |
prefix_index = get_index(prefixes, prefix_list, prefix) |
| 639 |
decomp = get_index_comp(char.decomposition) |
| 640 |
decomp_len = len(char.decomposition) |
| 641 |
if char.compat_decomp: |
| 642 |
compat_decomp = get_index_comp(char.compat_decomp) |
| 643 |
compat_decomp_len = len(char.compat_decomp) |
| 644 |
if char.canonical_decomp: |
| 645 |
canon_decomp = get_index_comp(char.canonical_decomp) |
| 646 |
canon_decomp_len = len(char.canonical_decomp) |
| 647 |
if char.combining: |
| 648 |
combining = char.combining |
| 649 |
|
| 650 |
db_record = (prefix_index, decomp_len, decomp, |
| 651 |
compat_decomp_len, compat_decomp, canon_decomp_len, |
| 652 |
canon_decomp, combining, left_index.get(code, -1), right_index.get(code, -1)) |
| 653 |
db.add_entry(db_record) |
| 654 |
|
| 655 |
db.output() |
| 656 |
|
| 657 |
outfile.print_code(""" |
| 658 |
def composition(current, next): |
| 659 |
l = lookup_composition_left_index(_composition_index(current)) |
| 660 |
if l < 0: |
| 661 |
raise KeyError |
| 662 |
r = lookup_composition_right_index(_composition_index(next)) |
| 663 |
if r < 0: |
| 664 |
raise KeyError |
| 665 |
key = l * %s + r |
| 666 |
result = _composition(key) |
| 667 |
if result == 0: |
| 668 |
raise KeyError |
| 669 |
return result |
| 670 |
""" % len(right_index)) |
| 671 |
|
| 672 |
outfile.print_listlike("_composition_prefixes", prefix_list) |
| 673 |
outfile.print_code(""" |
| 674 |
def decomposition(code): |
| 675 |
index = _composition_index(code) |
| 676 |
prefix = _composition_prefixes(lookup_composition_prefix_index(index)) |
| 677 |
if prefix: |
| 678 |
res = [prefix] |
| 679 |
else: |
| 680 |
res = [] |
| 681 |
start = lookup_composition_decomp(index) |
| 682 |
for i in range(lookup_composition_decomp_len(index)): |
| 683 |
s = hex(char_list_data(start + i))[2:].upper() |
| 684 |
if len(s) < 4: |
| 685 |
s = "0" * (4 - len(s)) + s |
| 686 |
res.append(s) |
| 687 |
return " ".join(res) |
| 688 |
|
| 689 |
def canon_decomposition(code): |
| 690 |
index = _composition_index(code) |
| 691 |
length = lookup_composition_canon_decomp_len(index) |
| 692 |
start = lookup_composition_canon_decomp(index) |
| 693 |
return _get_char_list(length, start) |
| 694 |
|
| 695 |
def compat_decomposition(code): |
| 696 |
index = _composition_index(code) |
| 697 |
length = lookup_composition_compat_decomp_len(index) |
| 698 |
start = lookup_composition_compat_decomp(index) |
| 699 |
return _get_char_list(length, start) |
| 700 |
|
| 701 |
def combining(code): |
| 702 |
index = _composition_index(code) |
| 703 |
return lookup_composition_combining(index) |
| 704 |
""") |
| 705 |
|
| 706 |
|
| 707 |
def write_character_names(outfile, table, base_mod): |
| 708 |
from rpython.rlib.unicodedata import dawg |
| 709 |
|
| 710 |
names = dict((table.get_char(code).name, code) |
| 711 |
for code in table.all_codes() |
| 712 |
if table.get_char(code).name) |
| 713 |
sorted_names_codes = sorted(names.iteritems()) |
| 714 |
if base_mod is None: |
| 715 |
d = dawg.build_compression_dawg(outfile, names) |
| 716 |
outfile.print_code("# the following dictionary is used by modules that take this as a base") |
| 717 |
outfile.print_code("# only used by generate_unicodedb, not after translation") |
| 718 |
outfile.print_code("_orig_names = {") |
| 719 |
for name, code in sorted_names_codes: |
| 720 |
outfile.print_code("%r: %r," % (name, code)) |
| 721 |
outfile.print_code("}") |
| 722 |
else: |
| 723 |
corrected_names = [] |
| 724 |
|
| 725 |
for name, code in sorted_names_codes: |
| 726 |
try: |
| 727 |
if base_mod.lookup_charcode(code) == name: |
| 728 |
continue |
| 729 |
except KeyError: |
| 730 |
pass |
| 731 |
corrected_names.append((name, code)) |
| 732 |
corrected_names_dict = dict(corrected_names) |
| 733 |
dawg.build_compression_dawg(outfile, corrected_names_dict) |
| 734 |
|
| 735 |
removed_names = [] |
| 736 |
for name, code in sorted(base_mod._orig_names.iteritems()): |
| 737 |
if name not in names: |
| 738 |
removed_names.append((name, code)) |
| 739 |
assert not removed_names |
| 740 |
|
| 741 |
def writeUnicodedata(version, version_tuple, table, outfile, base): |
| 742 |
if base: |
| 743 |
outfile.print_code('import %s as base_mod' % base) |
| 744 |
base_mod = __import__(base) |
| 745 |
else: |
| 746 |
outfile.print_code("base_mod = None") |
| 747 |
base_mod = None |
| 748 |
|
| 749 |
outfile.print_code('version = %r\n' % version) |
| 750 |
|
| 751 |
if version_tuple < (4, 1, 0): |
| 752 |
cjk_interval = ("(0x3400 <= code <= 0x4DB5 or" |
| 753 |
" 0x4E00 <= code <= 0x9FA5 or" |
| 754 |
" 0x20000 <= code <= 0x2A6D6)") |
| 755 |
elif version_tuple < (5, 0, 0): |
| 756 |
cjk_interval = ("(0x3400 <= code <= 0x4DB5 or" |
| 757 |
" 0x4E00 <= code <= 0x9FBB or" |
| 758 |
" 0x20000 <= code <= 0x2A6D6)") |
| 759 |
elif version_tuple < (6, 0, 0): |
| 760 |
cjk_interval = ("(0x3400 <= code <= 0x4DB5 or" |
| 761 |
" 0x4E00 <= code <= 0x9FCB or" |
| 762 |
" 0x20000 <= code <= 0x2A6D6 or" |
| 763 |
" 0x2A700 <= code <= 0x2B734)") |
| 764 |
elif version_tuple < (6, 1, 0): |
| 765 |
cjk_interval = ("(0x3400 <= code <= 0x4DB5 or" |
| 766 |
" 0x4E00 <= code <= 0x9FCB or" |
| 767 |
" 0x20000 <= code <= 0x2A6D6 or" |
| 768 |
" 0x2A700 <= code <= 0x2B734 or" |
| 769 |
" 0x2B740 <= code <= 0x2B81D)") |
| 770 |
elif version_tuple < (8, 0, 0): |
| 771 |
cjk_interval = ("(0x3400 <= code <= 0x4DB5 or" |
| 772 |
" 0x4E00 <= code <= 0x9FCC or" |
| 773 |
" 0x20000 <= code <= 0x2A6D6 or" |
| 774 |
" 0x2A700 <= code <= 0x2B734 or" |
| 775 |
" 0x2B740 <= code <= 0x2B81D)") |
| 776 |
elif version_tuple < (10, 0, 0): |
| 777 |
cjk_interval = ("(0x3400 <= code <= 0x4DB5 or" |
| 778 |
" 0x4E00 <= code <= 0x9FD5 or" |
| 779 |
" 0x20000 <= code <= 0x2A6D6 or" |
| 780 |
" 0x2A700 <= code <= 0x2B734 or" |
| 781 |
" 0x2B740 <= code <= 0x2B81D or" |
| 782 |
" 0x2B820 <= code <= 0x2CEA1)") |
| 783 |
elif version_tuple == (11, 0, 0): |
| 784 |
cjk_interval = ("(0x3400 <= code <= 0x4DB5 or" |
| 785 |
" 0x4E00 <= code <= 0x9FEF or" |
| 786 |
" 0x20000 <= code <= 0x2A6D6 or" |
| 787 |
" 0x2A700 <= code <= 0x2B734 or" |
| 788 |
" 0x2B740 <= code <= 0x2B81D or" |
| 789 |
" 0x2B820 <= code <= 0x2CEA1)") |
| 790 |
elif version_tuple == (12, 1, 0): |
| 791 |
cjk_interval = ("(0x3400 <= code <= 0x4DB5 or" |
| 792 |
" 0x4E00 <= code <= 0x9FEF or" |
| 793 |
" 0x20000 <= code <= 0x2A6D6 or" |
| 794 |
" 0x2A700 <= code <= 0x2B734 or" |
| 795 |
" 0x2B740 <= code <= 0x2CEA1 or" |
| 796 |
" 0x2CEB0 <= code <= 0x2EBE0)") |
| 797 |
elif version_tuple == (13, 0, 0): |
| 798 |
cjk_interval = ("(0x3400 <= code <= 0x4DB5 or" |
| 799 |
" 0x4E00 <= code <= 0x9FFC or" |
| 800 |
" 0x20000 <= code <= 0x2A6D6 or" |
| 801 |
" 0x2A700 <= code <= 0x2B734 or" |
| 802 |
" 0x2B740 <= code <= 0x2CEA1 or" |
| 803 |
" 0x2CEB0 <= code <= 0x2EBE0) or" |
| 804 |
" 0x30000 <= code <= 0x3134A") |
| 805 |
elif version_tuple == (14, 0, 0): |
| 806 |
cjk_interval = ("(0x3400 <= code <= 0x4DB5 or" |
| 807 |
" 0x4E00 <= code <= 0x9FFD or" |
| 808 |
" 0x20000 <= code <= 0x2A6DF or" |
| 809 |
" 0x2A700 <= code <= 0x2B738 or" |
| 810 |
" 0x2B740 <= code <= 0x2CEA1 or" |
| 811 |
" 0x2CEB0 <= code <= 0x2EBE0) or" |
| 812 |
" 0x30000 <= code <= 0x3134A") |
| 813 |
else: |
| 814 |
raise ValueError("please look up CJK ranges and fix the script, e.g. here: https://www.unicode.org/reports/tr38/tr38-29.html#BlockListing") |
| 815 |
|
| 816 |
write_character_names(outfile, table, base_mod) |
| 817 |
|
| 818 |
outfile.print_code(''' |
| 819 |
def _lookup_cjk(cjk_code): |
| 820 |
if len(cjk_code) != 4 and len(cjk_code) != 5: |
| 821 |
raise KeyError |
| 822 |
for c in cjk_code: |
| 823 |
if not ('0' <= c <= '9' or 'A' <= c <= 'F'): |
| 824 |
raise KeyError |
| 825 |
code = int(cjk_code, 16) |
| 826 |
if %(cjk_interval)s: |
| 827 |
return code |
| 828 |
raise KeyError |
| 829 |
|
| 830 |
def lookup(name, with_named_sequence=False, with_alias=False): |
| 831 |
from rpython.rlib.rstring import startswith |
| 832 |
if startswith(name, _cjk_prefix): |
| 833 |
return _lookup_cjk(name[len(_cjk_prefix):]) |
| 834 |
if startswith(name, _hangul_prefix): |
| 835 |
return _lookup_hangul(name[len(_hangul_prefix):]) |
| 836 |
|
| 837 |
if not base_mod: |
| 838 |
code = dawg_lookup(name) |
| 839 |
else: |
| 840 |
try: |
| 841 |
code = dawg_lookup(name) |
| 842 |
except KeyError: |
| 843 |
code = base_mod.dawg_lookup(name) |
| 844 |
if not with_named_sequence and %(named_sequence_interval)s: |
| 845 |
raise KeyError |
| 846 |
if not with_alias and %(alias_interval)s: |
| 847 |
raise KeyError |
| 848 |
return code |
| 849 |
|
| 850 |
def name(code): |
| 851 |
if %(cjk_interval)s: |
| 852 |
return "CJK UNIFIED IDEOGRAPH-" + hex(code)[2:].upper() |
| 853 |
if 0xAC00 <= code <= 0xD7A3: |
| 854 |
# vl_code, t_code = divmod(code - 0xAC00, len(_hangul_T)) |
| 855 |
vl_code = (code - 0xAC00) // len(_hangul_T) |
| 856 |
t_code = (code - 0xAC00) %% len(_hangul_T) |
| 857 |
# l_code, v_code = divmod(vl_code, len(_hangul_V)) |
| 858 |
l_code = vl_code // len(_hangul_V) |
| 859 |
v_code = vl_code %% len(_hangul_V) |
| 860 |
return ("HANGUL SYLLABLE " + _hangul_L[l_code] + |
| 861 |
_hangul_V[v_code] + _hangul_T[t_code]) |
| 862 |
if %(pua_interval)s: |
| 863 |
raise KeyError |
| 864 |
|
| 865 |
if base_mod is None: |
| 866 |
return lookup_charcode(code) |
| 867 |
else: |
| 868 |
try: |
| 869 |
return lookup_charcode(code) |
| 870 |
except KeyError: |
| 871 |
return base_mod.lookup_charcode(code) |
| 872 |
''' % dict(cjk_interval=cjk_interval, |
| 873 |
pua_interval="0xF0000 <= code < 0xF0400", |
| 874 |
named_sequence_interval="0xF0200 <= code < 0xF0400", |
| 875 |
alias_interval="0xF0000 <= code < 0xF0200")) |
| 876 |
|
| 877 |
|
| 878 |
char_list_index = {} |
| 879 |
char_list_data = [] |
| 880 |
if base_mod is not None: |
| 881 |
i = 0 |
| 882 |
while 1: |
| 883 |
try: |
| 884 |
char_list_data.append(base_mod._char_list_data(i)) |
| 885 |
except KeyError: |
| 886 |
break |
| 887 |
i += 1 |
| 888 |
for length in range(1, 20): |
| 889 |
for startindex in range(len(char_list_data) - length + 1): |
| 890 |
key = tuple(char_list_data[startindex: startindex + length]) |
| 891 |
char_list_index[key] = startindex |
| 892 |
|
| 893 |
char_list_data_startsize = len(char_list_data) |
| 894 |
|
| 895 |
all_code_lists = set() |
| 896 |
for code, char in table.enum_chars(): |
| 897 |
sc_data = table.special_casing.get(code, ()) |
| 898 |
for cl in [char.decomposition, char.compat_decomp, char.canonical_decomp, char.casefolding] + list(sc_data): |
| 899 |
if cl: |
| 900 |
all_code_lists.add(tuple(cl)) |
| 901 |
|
| 902 |
for cl in sorted(all_code_lists, key=lambda cl: (-len(cl), cl)): |
| 903 |
get_char_list_offset(cl, char_list_data, char_list_index) |
| 904 |
outfile.print_listlike("_char_list_data", char_list_data[char_list_data_startsize:]) |
| 905 |
outfile.print_code(""" |
| 906 |
def char_list_data(index): |
| 907 |
if index < %s: |
| 908 |
assert base_mod is not None |
| 909 |
return base_mod._char_list_data(index) |
| 910 |
return _char_list_data(index - %s) |
| 911 |
|
| 912 |
def _get_char_list(length, start): |
| 913 |
res = [0] * length |
| 914 |
for i in range(length): |
| 915 |
res[i] = char_list_data(start + i) |
| 916 |
return res |
| 917 |
""" % (char_list_data_startsize, char_list_data_startsize)) |
| 918 |
|
| 919 |
|
| 920 |
write_composition_data(outfile, table, char_list_index, base_mod) |
| 921 |
|
| 922 |
|
| 923 |
writeDbRecord(outfile, table, char_list_index, base_mod) |
| 924 |
|
| 925 |
|
| 926 |
outfile.print_code(''' |
| 927 |
def toupper(code): |
| 928 |
if code < 128: |
| 929 |
if ord('a') <= code <= ord('z'): |
| 930 |
return code - 32 |
| 931 |
return code |
| 932 |
return code - lookup_db_upperdist(_db_index(code)) |
| 933 |
|
| 934 |
def tolower(code): |
| 935 |
if code < 128: |
| 936 |
if ord('A') <= code <= ord('Z'): |
| 937 |
return code + 32 |
| 938 |
return code |
| 939 |
return code - lookup_db_lowerdist(_db_index(code)) |
| 940 |
|
| 941 |
def totitle(code): |
| 942 |
if code < 128: |
| 943 |
if ord('a') <= code <= ord('z'): |
| 944 |
return code - 32 |
| 945 |
return code |
| 946 |
return code - lookup_db_titledist(_db_index(code)) |
| 947 |
|
| 948 |
def toupper_full(code): |
| 949 |
if code < 128: |
| 950 |
if ord('a') <= code <= ord('z'): |
| 951 |
return [code - 32] |
| 952 |
return [code] |
| 953 |
index = lookup_db_special_casing_index(_db_index(code)) |
| 954 |
if index == -1: |
| 955 |
return [toupper(code)] |
| 956 |
length = lookup_special_casing_upper_len(index) |
| 957 |
if length == 0: |
| 958 |
return [toupper(code)] |
| 959 |
start = lookup_special_casing_upper(index) |
| 960 |
return _get_char_list(length, start) |
| 961 |
|
| 962 |
def tolower_full(code): |
| 963 |
if code < 128: |
| 964 |
if ord('A') <= code <= ord('Z'): |
| 965 |
return [code + 32] |
| 966 |
return [code] |
| 967 |
index = lookup_db_special_casing_index(_db_index(code)) |
| 968 |
if index == -1: |
| 969 |
return [tolower(code)] |
| 970 |
length = lookup_special_casing_lower_len(index) |
| 971 |
if length == 0: |
| 972 |
return [tolower(code)] |
| 973 |
start = lookup_special_casing_lower(index) |
| 974 |
return _get_char_list(length, start) |
| 975 |
|
| 976 |
def totitle_full(code): |
| 977 |
index = lookup_db_special_casing_index(_db_index(code)) |
| 978 |
if index == -1: |
| 979 |
return [totitle(code)] |
| 980 |
length = lookup_special_casing_title_len(index) |
| 981 |
if length == 0: |
| 982 |
return [totitle(code)] |
| 983 |
start = lookup_special_casing_title(index) |
| 984 |
return _get_char_list(length, start) |
| 985 |
|
| 986 |
def casefold_lookup(code): |
| 987 |
index = lookup_db_special_casing_index(_db_index(code)) |
| 988 |
if index == -1: |
| 989 |
return tolower_full(code) |
| 990 |
length = lookup_special_casing_casefold_len(index) |
| 991 |
if length == 0: |
| 992 |
return tolower_full(code) |
| 993 |
start = lookup_special_casing_casefold(index) |
| 994 |
return _get_char_list(length, start) |
| 995 |
return lookup_special_casing_casefold(index) |
| 996 |
|
| 997 |
''') |
| 998 |
|
| 999 |
|
| 1000 |
lst = [(u''.join(unichr(c) for c in chars)).encode("utf-8") |
| 1001 |
for _, chars in table.named_sequences] |
| 1002 |
outfile.print_listlike("_named_sequences", lst) |
| 1003 |
lengths = [len(chars) for _, chars in table.named_sequences] |
| 1004 |
outfile.print_listlike("_named_sequence_lengths", lengths) |
| 1005 |
outfile.print_code(''' |
| 1006 |
|
| 1007 |
def lookup_named_sequence(code): |
| 1008 |
if 0 <= code - %(start)s < %(len)s: |
| 1009 |
return _named_sequences(code - %(start)s) |
| 1010 |
else: |
| 1011 |
return None |
| 1012 |
|
| 1013 |
def lookup_named_sequence_length(code): |
| 1014 |
if 0 <= code - %(start)s < %(len)s: |
| 1015 |
return _named_sequence_lengths(code - %(start)s) |
| 1016 |
else: |
| 1017 |
return -1 |
| 1018 |
''' % dict(start=table.NAMED_SEQUENCES_START, len=len(lst))) |
| 1019 |
|
| 1020 |
|
| 1021 |
_name_aliases = [char for name, char in table.aliases] |
| 1022 |
assert len(_name_aliases) < 0x200 |
| 1023 |
outfile.print_listlike("_name_aliases", _name_aliases) |
| 1024 |
outfile.print_code(''' |
| 1025 |
|
| 1026 |
def lookup_with_alias(name, with_named_sequence=False): |
| 1027 |
code = lookup(name, with_named_sequence=with_named_sequence, with_alias=True) |
| 1028 |
if 0 <= code - %(start)s < %(length)s: |
| 1029 |
return _name_aliases(code - %(start)s) |
| 1030 |
else: |
| 1031 |
return code |
| 1032 |
''' % dict(start=table.NAME_ALIASES_START, |
| 1033 |
length=len(_name_aliases))) |
| 1034 |
|
| 1035 |
def main(): |
| 1036 |
import sys |
| 1037 |
from optparse import OptionParser |
| 1038 |
infile = None |
| 1039 |
outfile = sys.stdout |
| 1040 |
|
| 1041 |
parser = OptionParser('Usage: %prog [options]') |
| 1042 |
parser.add_option('--base', metavar='FILENAME', help='Base python version (for import)') |
| 1043 |
parser.add_option('--output', metavar='OUTPUT_MODULE', help='Output module (implied py extension)') |
| 1044 |
parser.add_option('--unidata_version', metavar='#.#.#', help='Unidata version') |
| 1045 |
options, args = parser.parse_args() |
| 1046 |
|
| 1047 |
if not options.unidata_version: |
| 1048 |
raise Exception("No version specified") |
| 1049 |
|
| 1050 |
if options.output: |
| 1051 |
outfile = open(options.output + '.py', "w") |
| 1052 |
|
| 1053 |
filenames = dict( |
| 1054 |
data='UnicodeData-%(version)s.txt', |
| 1055 |
exclusions='CompositionExclusions-%(version)s.txt', |
| 1056 |
east_asian_width='EastAsianWidth-%(version)s.txt', |
| 1057 |
unihan='UnihanNumeric-%(version)s.txt', |
| 1058 |
linebreak='LineBreak-%(version)s.txt', |
| 1059 |
derived_core_properties='DerivedCoreProperties-%(version)s.txt', |
| 1060 |
name_aliases='NameAliases-%(version)s.txt', |
| 1061 |
named_sequences = 'NamedSequences-%(version)s.txt', |
| 1062 |
casefolding = 'CaseFolding-%(version)s.txt', |
| 1063 |
) |
| 1064 |
version_tuple = tuple(int(x) for x in options.unidata_version.split(".")) |
| 1065 |
if version_tuple[0] >= 5: |
| 1066 |
filenames['special_casing'] = 'SpecialCasing-%(version)s.txt' |
| 1067 |
filenames = dict((name, filename % dict(version=options.unidata_version)) |
| 1068 |
for (name, filename) in filenames.items()) |
| 1069 |
files = dict((name, open(filename)) |
| 1070 |
for (name, filename) in filenames.items()) |
| 1071 |
|
| 1072 |
table = read_unicodedata(files) |
| 1073 |
table.upper_lower_from_properties = (version_tuple[0] >= 6) |
| 1074 |
|
| 1075 |
print("_" * 60) |
| 1076 |
print("starting", options.unidata_version) |
| 1077 |
|
| 1078 |
outfile = CodeWriter(outfile) |
| 1079 |
outfile.print_comment('UNICODE CHARACTER DATABASE') |
| 1080 |
outfile.print_comment('# This file was generated with the command:') |
| 1081 |
outfile.print_comment('# ' + ' '.join(sys.argv)) |
| 1082 |
outfile.print_code('') |
| 1083 |
outfile.print_code('from rpython.rlib.rarithmetic import r_longlong, r_int32, r_uint32, intmask') |
| 1084 |
outfile.print_code('''\ |
| 1085 |
from rpython.rlib.unicodedata.supportcode import (signed_ord, _all_short, |
| 1086 |
_all_ushort, _all_int32, _all_uint32, _cjk_prefix, _hangul_prefix, |
| 1087 |
_lookup_hangul, _hangul_L, _hangul_V, _hangul_T)''') |
| 1088 |
outfile.print_code('') |
| 1089 |
writeUnicodedata(options.unidata_version, version_tuple, table, outfile, options.base) |
| 1090 |
|
| 1091 |
outfile.print_stats() |
| 1092 |
|
| 1093 |
|
| 1094 |
def splitbins(t, trace=1): |
| 1095 |
"""t, trace=0 -> (t1, t2, shift). Split a table to save space. |
| 1096 |
|
| 1097 |
t is a sequence of ints. This function can be useful to save space if |
| 1098 |
many of the ints are the same. t1 and t2 are lists of ints, and shift |
| 1099 |
is an int, chosen to minimize the combined size of t1 and t2 (in C |
| 1100 |
code), and where for each i in range(len(t)), |
| 1101 |
t[i] == t2[(t1[i >> shift] << shift) + (i & mask)] |
| 1102 |
where mask is a bitmask isolating the last "shift" bits. |
| 1103 |
|
| 1104 |
If optional arg trace is non-zero (default zero), progress info |
| 1105 |
is printed to sys.stderr. The higher the value, the more info |
| 1106 |
you'll get. |
| 1107 |
""" |
| 1108 |
|
| 1109 |
if trace: |
| 1110 |
def dump(t1, t2, shift, bytes): |
| 1111 |
print("%d+%d bins at shift %d; %d bytes" % ( |
| 1112 |
len(t1), len(t2), shift, bytes)) |
| 1113 |
print("Size of original table:", len(t)*getsize_unsigned(t), "bytes") |
| 1114 |
n = len(t)-1 |
| 1115 |
maxshift = 0 |
| 1116 |
if n > 0: |
| 1117 |
while n >> 1: |
| 1118 |
n >>= 1 |
| 1119 |
maxshift += 1 |
| 1120 |
del n |
| 1121 |
bytes = sys.maxsize |
| 1122 |
t = tuple(t) |
| 1123 |
for shift in range(maxshift + 1): |
| 1124 |
t1, t2, b = _split(t, shift) |
| 1125 |
if trace > 1: |
| 1126 |
dump(t1, t2, shift, b) |
| 1127 |
if b < bytes: |
| 1128 |
best = t1, t2, shift |
| 1129 |
bytes = b |
| 1130 |
t1, t2, shift = best |
| 1131 |
if trace: |
| 1132 |
print("Best:", end=" ") |
| 1133 |
dump(t1, t2, shift, bytes) |
| 1134 |
if 1: |
| 1135 |
|
| 1136 |
mask = ~((~0) << shift) |
| 1137 |
for i in range(len(t)): |
| 1138 |
assert t[i] == t2[(t1[i >> shift] << shift) + (i & mask)] |
| 1139 |
return best |
| 1140 |
|
| 1141 |
def _split(t, shift): |
| 1142 |
t1 = [] |
| 1143 |
t2 = [] |
| 1144 |
size = 2**shift |
| 1145 |
bincache = {} |
| 1146 |
for i in range(0, len(t), size): |
| 1147 |
bin = t[i:i+size] |
| 1148 |
index = bincache.get(bin) |
| 1149 |
if index is None: |
| 1150 |
index = len(t2) |
| 1151 |
bincache[bin] = index |
| 1152 |
t2.extend(bin) |
| 1153 |
t1.append(index >> shift) |
| 1154 |
|
| 1155 |
b = len(t1)*getsize_unsigned(t1) + len(t2)*getsize_unsigned(t2) |
| 1156 |
return t1, t2, b |
| 1157 |
|
| 1158 |
if __name__ == '__main__': |
| 1159 |
main() |