Skip to content

Commit

Permalink
idna: only store range start in the initial TABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Feb 17, 2021
1 parent 245aba3 commit 945908c
Show file tree
Hide file tree
Showing 3 changed files with 1,899 additions and 1,916 deletions.
7 changes: 2 additions & 5 deletions idna/src/make_uts46_mapping_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,10 @@ def merge_single_char_ranges(ranges):
optimized_ranges = list(merge_single_char_ranges(optimized_ranges))


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

for ranges in optimized_ranges:
first = ranges[0][0]
last = ranges[-1][1]
print(" Range { from: '%s', to: '%s', }," % (escape_char(char(first)),
escape_char(char(last))))
print(" '%s'," % escape_char(char(ranges[0][0])))

print("];\n")

Expand Down
42 changes: 14 additions & 28 deletions idna/src/uts46.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use self::Mapping::*;
use crate::punycode;
use std::cmp::Ordering::{Equal, Greater, Less};
use std::{error::Error as StdError, fmt};
use unicode_bidi::{bidi_class, BidiClass};
use unicode_normalization::char::is_combining_mark;
Expand Down Expand Up @@ -51,36 +50,23 @@ enum Mapping {
DisallowedIdna2008,
}

struct Range {
from: char,
to: char,
}

fn find_char(codepoint: char) -> &'static Mapping {
let r = TABLE.binary_search_by(|ref range| {
if codepoint > range.to {
Less
} else if codepoint < range.from {
Greater
} else {
Equal
}
});
r.ok()
.map(|i| {
const SINGLE_MARKER: u16 = 1 << 15;
let idx = match TABLE.binary_search(&codepoint) {
Ok(idx) => idx,
Err(idx) => idx - 1,
};

let x = INDEX_TABLE[i];
let single = (x & SINGLE_MARKER) != 0;
let offset = !SINGLE_MARKER & x;
const SINGLE_MARKER: u16 = 1 << 15;

if single {
&MAPPING_TABLE[offset as usize]
} else {
&MAPPING_TABLE[(offset + (codepoint as u16 - TABLE[i].from as u16)) as usize]
}
})
.unwrap()
let x = INDEX_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]
}
}

struct Mapper<'a> {
Expand Down
Loading

0 comments on commit 945908c

Please sign in to comment.