Skip to content

Commit

Permalink
idna: combine two tables
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Feb 17, 2021
1 parent 945908c commit 04b7f49
Show file tree
Hide file tree
Showing 3 changed files with 1,892 additions and 3,782 deletions.
17 changes: 6 additions & 11 deletions idna/src/make_uts46_mapping_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,22 @@ def merge_single_char_ranges(ranges):

optimized_ranges = list(merge_single_char_ranges(optimized_ranges))


print("static TABLE: &[char] = &[")

for ranges in optimized_ranges:
print(" '%s'," % escape_char(char(ranges[0][0])))

print("];\n")

print("static INDEX_TABLE: &[u16] = &[")

SINGLE_MARKER = 1 << 15

print("static TABLE: &[(char, u16)] = &[")

offset = 0
for ranges in optimized_ranges:
assert offset < SINGLE_MARKER

block_len = len(ranges)
single = SINGLE_MARKER if block_len == 1 else 0
print(" %s," % (offset | single))
index = offset | single
offset += block_len

start = escape_char(char(ranges[0][0]))
print(" ('%s', %s)," % (start, index))

print("];\n")

print("static MAPPING_TABLE: &[Mapping] = &[")
Expand Down
6 changes: 3 additions & 3 deletions idna/src/uts46.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ enum Mapping {
}

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

const SINGLE_MARKER: u16 = 1 << 15;

let x = INDEX_TABLE[idx];
let (base, x) = TABLE[idx];
let single = (x & SINGLE_MARKER) != 0;
let offset = !SINGLE_MARKER & x;

if single {
&MAPPING_TABLE[offset as usize]
} else {
&MAPPING_TABLE[(offset + (codepoint as u16 - TABLE[idx] as u16)) as usize]
&MAPPING_TABLE[(offset + (codepoint as u16 - base as u16)) as usize]
}
}

Expand Down
Loading

0 comments on commit 04b7f49

Please sign in to comment.