Skip to content

Commit 04b7f49

Browse files
committed
idna: combine two tables
1 parent 945908c commit 04b7f49

File tree

3 files changed

+1892
-3782
lines changed

3 files changed

+1892
-3782
lines changed

idna/src/make_uts46_mapping_table.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,27 +150,22 @@ def merge_single_char_ranges(ranges):
150150

151151
optimized_ranges = list(merge_single_char_ranges(optimized_ranges))
152152

153-
154-
print("static TABLE: &[char] = &[")
155-
156-
for ranges in optimized_ranges:
157-
print(" '%s'," % escape_char(char(ranges[0][0])))
158-
159-
print("];\n")
160-
161-
print("static INDEX_TABLE: &[u16] = &[")
162-
163153
SINGLE_MARKER = 1 << 15
164154

155+
print("static TABLE: &[(char, u16)] = &[")
156+
165157
offset = 0
166158
for ranges in optimized_ranges:
167159
assert offset < SINGLE_MARKER
168160

169161
block_len = len(ranges)
170162
single = SINGLE_MARKER if block_len == 1 else 0
171-
print(" %s," % (offset | single))
163+
index = offset | single
172164
offset += block_len
173165

166+
start = escape_char(char(ranges[0][0]))
167+
print(" ('%s', %s)," % (start, index))
168+
174169
print("];\n")
175170

176171
print("static MAPPING_TABLE: &[Mapping] = &[")

idna/src/uts46.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,21 @@ enum Mapping {
5151
}
5252

5353
fn find_char(codepoint: char) -> &'static Mapping {
54-
let idx = match TABLE.binary_search(&codepoint) {
54+
let idx = match TABLE.binary_search_by_key(&codepoint, |&val| val.0) {
5555
Ok(idx) => idx,
5656
Err(idx) => idx - 1,
5757
};
5858

5959
const SINGLE_MARKER: u16 = 1 << 15;
6060

61-
let x = INDEX_TABLE[idx];
61+
let (base, x) = TABLE[idx];
6262
let single = (x & SINGLE_MARKER) != 0;
6363
let offset = !SINGLE_MARKER & x;
6464

6565
if single {
6666
&MAPPING_TABLE[offset as usize]
6767
} else {
68-
&MAPPING_TABLE[(offset + (codepoint as u16 - TABLE[idx] as u16)) as usize]
68+
&MAPPING_TABLE[(offset + (codepoint as u16 - base as u16)) as usize]
6969
}
7070
}
7171

0 commit comments

Comments
 (0)