|
10 | 10 | # You can get the latest idna table from
|
11 | 11 | # http://www.unicode.org/Public/idna/latest/IdnaMappingTable.txt
|
12 | 12 |
|
| 13 | +from __future__ import print_function |
13 | 14 | import collections
|
14 | 15 | import itertools
|
15 | 16 |
|
@@ -82,6 +83,7 @@ def rust_slice(s):
|
82 | 83 |
|
83 | 84 | def mergeable_key(r):
|
84 | 85 | mapping = r[2]
|
| 86 | + |
85 | 87 | # These types have associated data, so we should not merge them.
|
86 | 88 | if mapping in ('Mapped', 'Deviation', 'DisallowedStd3Mapped'):
|
87 | 89 | return r
|
@@ -121,23 +123,49 @@ def mergeable_key(r):
|
121 | 123 | unicode_str = group[0][3]
|
122 | 124 | optimized_ranges.append((first, last, mapping, unicode_str))
|
123 | 125 |
|
| 126 | +import sys |
| 127 | +def merge_single_char_ranges(ranges): |
| 128 | + current = [] |
| 129 | + for r in ranges: |
| 130 | + mapping = r[2] |
| 131 | + |
| 132 | + if not current or current[-1][0] == current[-1][1] and r[0] == r[1]: |
| 133 | + current.append(r) |
| 134 | + continue |
| 135 | + if len(current) != 0: |
| 136 | + ret = current |
| 137 | + current = [r] |
| 138 | + yield ret |
| 139 | + continue |
| 140 | + current.append(r) |
| 141 | + ret = current |
| 142 | + current = [] |
| 143 | + yield ret |
| 144 | + |
| 145 | +optimized_ranges = list(merge_single_char_ranges(optimized_ranges)) |
| 146 | + |
124 | 147 |
|
125 | 148 | print("static TABLE: &'static [Range] = &[")
|
126 | 149 |
|
127 |
| -for (first, last, mapping, unicode_str) in optimized_ranges: |
128 |
| - if unicode_str is not None: |
129 |
| - mapping += rust_slice(strtab_slice(unicode_str)) |
| 150 | +for ranges in optimized_ranges: |
| 151 | + first = ranges[0][0] |
| 152 | + last = ranges[-1][1] |
130 | 153 | print(" Range { from: '%s', to: '%s', }," % (escape_char(char(first)),
|
131 |
| - escape_char(char(last)))) |
| 154 | + escape_char(char(last)))) |
132 | 155 |
|
133 | 156 | print("];\n")
|
134 | 157 |
|
135 |
| -print("static MAPPING_TABLE: &'static [Mapping] = &[") |
| 158 | +print("static MAPPING_TABLE: &'static [&[Mapping]] = &[") |
| 159 | + |
| 160 | +for ranges in optimized_ranges: |
| 161 | + print("&[", end='') |
| 162 | + |
| 163 | + for (first, last, mapping, unicode_str) in ranges: |
| 164 | + if unicode_str is not None: |
| 165 | + mapping += rust_slice(strtab_slice(unicode_str)) |
| 166 | + print("%s, " % mapping, end='') |
136 | 167 |
|
137 |
| -for (first, last, mapping, unicode_str) in optimized_ranges: |
138 |
| - if unicode_str is not None: |
139 |
| - mapping += rust_slice(strtab_slice(unicode_str)) |
140 |
| - print(" %s," % mapping) |
| 168 | + print("],") |
141 | 169 |
|
142 | 170 | print("];\n")
|
143 | 171 |
|
|
0 commit comments