-
Notifications
You must be signed in to change notification settings - Fork 169
/
UnicodeData.py
90 lines (82 loc) · 2.35 KB
/
UnicodeData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import sys
import os.path
import unicodedata
MaxCharacter = sys.maxunicode
UnicodeCharacterCount = sys.maxunicode + 1
BMPCharacterCharacterCount = 0xffff + 1
DBCSCharacterCount = 0xffff + 1
def getCharacterName(ch):
try:
return unicodedata.name(ch).title()
except ValueError:
return ''
def readUnicodePropertyFile(path, propertyIndex=1):
filename, ext = os.path.splitext(os.path.basename(path))
version = ''
propertyList = {}
missingList = {}
with open(path, encoding='utf-8') as fd:
for line in fd.readlines():
line = line.strip()
missing = False
if not line:
continue
if line[0] == '#':
line = line[1:].strip()
if line.startswith(filename) and line.endswith(ext):
version = line[len(filename) + 1:-len(ext)]
continue
if not line.startswith('@missing:'):
continue
missing = True
print(path, line)
line = line[len('@missing:'):]
index = line.find('#') # ignore comment
if index > 0:
line = line[:index]
items = line.split(';')
code = [int(item, 16) for item in items[0].replace('.', ' ').split() if item]
if missing:
propertyName = items[1].strip()
value = items[2] if len(items) > 2 else None
assert propertyName not in missingList
missingList[propertyName] = (code, value)
else:
propertyName = items[propertyIndex].strip()
if propertyName in propertyList:
propertyList[propertyName].append(code)
else:
propertyList[propertyName] = [code]
print(path, version, 'property:', ', '.join(sorted(propertyList.keys())))
return version, propertyList
def updateUnicodePropertyTable(table, propertyMap, propertyList):
for name, codeList in propertyList.items():
if name not in propertyMap:
continue
value = int(propertyMap[name])
for code in codeList:
if len(code) == 1:
table[code[0]] = value
else:
assert len(code) == 2
start, end = code
while start <= end:
table[start] = value
start += 1
def flattenUnicodePropertyTable(table, propertyList):
for name, codeList in propertyList.items():
for code in codeList:
if len(code) == 1:
table[code[0]] = name
else:
assert len(code) == 2
start, end = code
while start <= end:
table[start] = name
start += 1
def flattenPropertyMap(propertyMap):
result = {}
for key, items in propertyMap.items():
for value in items:
result[value] = key
return result