blob: 2efa9045074a63d50f4bad749aef4f3c5ab9faf2 [file] [log] [blame]
Pavel Labath3b17b842018-02-21 22:36:31 +00001#!/usr/bin/env python
2"""
3Unicode case folding database conversion utility
4
5Parses the database and generates a C++ function which implements the case
6folding algorithm. The database entries are of the form:
7
8 <code>; <status>; <mapping>; # <name>
9
10<status> can be one of four characters:
11 C - Common mappings
12 S - mappings for Simple case folding
13 F - mappings for Full case folding
14 T - special case for Turkish I characters
15
16Right now this generates a function which implements simple case folding (C+S
17entries).
18"""
19
Serge Guelton4a274782019-01-03 14:11:33 +000020from __future__ import print_function
21
Pavel Labath3b17b842018-02-21 22:36:31 +000022import sys
23import re
24import urllib2
25
26# This variable will body of the mappings function
27body = ""
28
29# Reads file line-by-line, extracts Common and Simple case fold mappings and
30# returns a (from_char, to_char, from_name) tuple.
31def mappings(f):
32 previous_from = -1
33 expr = re.compile(r'^(.*); [CS]; (.*); # (.*)')
34 for line in f:
35 m = expr.match(line)
36 if not m: continue
37 from_char = int(m.group(1), 16)
38 to_char = int(m.group(2), 16)
39 from_name = m.group(3)
40
41 if from_char <= previous_from:
42 raise Exception("Duplicate or unsorted characters in input")
43 yield from_char, to_char, from_name
44 previous_from = from_char
45
46# Computes the shift (to_char - from_char) in a mapping.
47def shift(mapping):
48 return mapping[1] - mapping[0]
49
50# Computes the stride (from_char2 - from_char1) of two mappings.
51def stride2(mapping1, mapping2):
52 return mapping2[0] - mapping1[0]
53
54# Computes the stride of a list of mappings. The list should have at least two
55# mappings. All mappings in the list are assumed to have the same stride.
56def stride(block):
57 return stride2(block[0], block[1])
58
59
60# b is a list of mappings. All the mappings are assumed to have the same
61# shift and the stride between adjecant mappings (if any) is constant.
62def dump_block(b):
63 global body
64
65 if len(b) == 1:
66 # Special case for handling blocks of length 1. We don't even need to
67 # emit the "if (C < X) return C" check below as all characters in this
68 # range will be caught by the "C < X" check emitted by the first
69 # non-trivial block.
70 body += " // {2}\n if (C == {0:#06x})\n return {1:#06x};\n".format(*b[0])
71 return
72
73 first = b[0][0]
74 last = first + stride(b) * (len(b)-1)
75 modulo = first % stride(b)
76
77 # All characters before this block map to themselves.
78 body += " if (C < {0:#06x})\n return C;\n".format(first)
79 body += " // {0} characters\n".format(len(b))
80
81 # Generic pattern: check upper bound (lower bound is checked by the "if"
82 # above) and modulo of C, return C+shift.
83 pattern = " if (C <= {0:#06x} && C % {1} == {2})\n return C + {3};\n"
84
85 if stride(b) == 2 and shift(b[0]) == 1 and modulo == 0:
86 # Special case:
87 # We can elide the modulo-check because the expression "C|1" will map
88 # the intervening characters to themselves.
89 pattern = " if (C <= {0:#06x})\n return C | 1;\n"
90 elif stride(b) == 1:
91 # Another special case: X % 1 is always zero, so don't emit the
92 # modulo-check.
93 pattern = " if (C <= {0:#06x})\n return C + {3};\n"
94
95 body += pattern.format(last, stride(b), modulo, shift(b[0]))
96
97current_block = []
98f = urllib2.urlopen(sys.argv[1])
99for m in mappings(f):
100 if len(current_block) == 0:
101 current_block.append(m)
102 continue
103
104 if shift(current_block[0]) != shift(m):
105 # Incompatible shift, start a new block.
106 dump_block(current_block)
107 current_block = [m]
108 continue
109
110 if len(current_block) == 1 or stride(current_block) == stride2(current_block[-1], m):
111 current_block.append(m)
112 continue
113
114 # Incompatible stride, start a new block.
115 dump_block(current_block)
116 current_block = [m]
117f.close()
118
119dump_block(current_block)
120
Serge Guelton4a274782019-01-03 14:11:33 +0000121print('//===---------- Support/UnicodeCaseFold.cpp -------------------------------===//')
122print('//')
123print('// This file was generated by utils/unicode-case-fold.py from the Unicode')
124print('// case folding database at')
125print('// ', sys.argv[1])
126print('//')
127print('// To regenerate this file, run:')
128print('// utils/unicode-case-fold.py \\')
129print('// "{}" \\'.format(sys.argv[1]))
130print('// > lib/Support/UnicodeCaseFold.cpp')
131print('//')
132print('//===----------------------------------------------------------------------===//')
133print('')
134print('#include "llvm/Support/Unicode.h"')
135print('')
136print("int llvm::sys::unicode::foldCharSimple(int C) {")
137print(body)
138print(" return C;")
139print("}")