Skip to content

Commit d56b36d

Browse files
committed
idna: combine two tables
1 parent 70a8669 commit d56b36d

File tree

3 files changed

+1660
-3318
lines changed

3 files changed

+1660
-3318
lines changed

idna/src/make_uts46_mapping_table.py

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

149149
optimized_ranges = list(merge_single_char_ranges(optimized_ranges))
150150

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

153+
print("static TABLE: &[(char, u16)] = &[")
154+
163155
offset = 0
164156
for ranges in optimized_ranges:
165157
assert offset < SINGLE_MARKER
166158

167159
block_len = len(ranges)
168160
single = SINGLE_MARKER if block_len == 1 else 0
169-
print(" %s," % (offset | single))
161+
index = offset | single
170162
offset += block_len
171163

164+
start = escape_char(char(ranges[0][0]))
165+
print(" ('%s', %s)," % (start, index))
166+
172167
print("];\n")
173168

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

idna/src/uts46.rs

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

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

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

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

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

0 commit comments

Comments
 (0)