3 Unicode case folding database conversion utility
5 Parses the database and generates a C++ function which implements the case
6 folding algorithm. The database entries are of the form:
8 <code>; <status>; <mapping>; # <name>
10 <status> can be one of four characters:
12 S - mappings for Simple case folding
13 F - mappings for Full case folding
14 T - special case for Turkish I characters
16 Right now this generates a function which implements simple case folding (C+S
20 from __future__
import print_function
26 from urllib
.request
import urlopen
28 from urllib2
import urlopen
31 # This variable will body of the mappings function
34 # Reads file line-by-line, extracts Common and Simple case fold mappings and
35 # returns a (from_char, to_char, from_name) tuple.
38 expr
= re
.compile(r
"^(.*); [CS]; (.*); # (.*)")
43 from_char
= int(m
.group(1), 16)
44 to_char
= int(m
.group(2), 16)
45 from_name
= m
.group(3)
47 if from_char
<= previous_from
:
48 raise Exception("Duplicate or unsorted characters in input")
49 yield from_char
, to_char
, from_name
50 previous_from
= from_char
53 # Computes the shift (to_char - from_char) in a mapping.
55 return mapping
[1] - mapping
[0]
58 # Computes the stride (from_char2 - from_char1) of two mappings.
59 def stride2(mapping1
, mapping2
):
60 return mapping2
[0] - mapping1
[0]
63 # Computes the stride of a list of mappings. The list should have at least two
64 # mappings. All mappings in the list are assumed to have the same stride.
66 return stride2(block
[0], block
[1])
69 # b is a list of mappings. All the mappings are assumed to have the same
70 # shift and the stride between adjecant mappings (if any) is constant.
75 # Special case for handling blocks of length 1. We don't even need to
76 # emit the "if (C < X) return C" check below as all characters in this
77 # range will be caught by the "C < X" check emitted by the first
79 body
+= " // {2}\n if (C == {0:#06x})\n return {1:#06x};\n".format(*b
[0])
83 last
= first
+ stride(b
) * (len(b
) - 1)
84 modulo
= first
% stride(b
)
86 # All characters before this block map to themselves.
87 body
+= " if (C < {0:#06x})\n return C;\n".format(first
)
88 body
+= " // {0} characters\n".format(len(b
))
90 # Generic pattern: check upper bound (lower bound is checked by the "if"
91 # above) and modulo of C, return C+shift.
92 pattern
= " if (C <= {0:#06x} && C % {1} == {2})\n return C + {3};\n"
94 if stride(b
) == 2 and shift(b
[0]) == 1 and modulo
== 0:
96 # We can elide the modulo-check because the expression "C|1" will map
97 # the intervening characters to themselves.
98 pattern
= " if (C <= {0:#06x})\n return C | 1;\n"
100 # Another special case: X % 1 is always zero, so don't emit the
102 pattern
= " if (C <= {0:#06x})\n return C + {3};\n"
104 body
+= pattern
.format(last
, stride(b
), modulo
, shift(b
[0]))
108 f
= urlopen(sys
.argv
[1])
109 for m
in mappings(f
):
110 if len(current_block
) == 0:
111 current_block
.append(m
)
114 if shift(current_block
[0]) != shift(m
):
115 # Incompatible shift, start a new block.
116 dump_block(current_block
)
120 if len(current_block
) == 1 or stride(current_block
) == stride2(
123 current_block
.append(m
)
126 # Incompatible stride, start a new block.
127 dump_block(current_block
)
131 dump_block(current_block
)
134 "//===---------- Support/UnicodeCaseFold.cpp -------------------------------===//"
137 print("// This file was generated by utils/unicode-case-fold.py from the Unicode")
138 print("// case folding database at")
139 print("// ", sys
.argv
[1])
141 print("// To regenerate this file, run:")
142 print("// utils/unicode-case-fold.py \\")
143 print('// "{}" \\'.format(sys
.argv
[1]))
144 print("// > lib/Support/UnicodeCaseFold.cpp")
147 "//===----------------------------------------------------------------------===//"
150 print('#include "llvm/Support/Unicode.h"')
152 print("int llvm::sys::unicode::foldCharSimple(int C) {")