Skip to content

Commit 945908c

Browse files
committed
idna: only store range start in the initial TABLE
1 parent 245aba3 commit 945908c

File tree

3 files changed

+1899
-1916
lines changed

3 files changed

+1899
-1916
lines changed

idna/src/make_uts46_mapping_table.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,10 @@ def merge_single_char_ranges(ranges):
151151
optimized_ranges = list(merge_single_char_ranges(optimized_ranges))
152152

153153

154-
print("static TABLE: &[Range] = &[")
154+
print("static TABLE: &[char] = &[")
155155

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

162159
print("];\n")
163160

idna/src/uts46.rs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
1212
use self::Mapping::*;
1313
use crate::punycode;
14-
use std::cmp::Ordering::{Equal, Greater, Less};
1514
use std::{error::Error as StdError, fmt};
1615
use unicode_bidi::{bidi_class, BidiClass};
1716
use unicode_normalization::char::is_combining_mark;
@@ -51,36 +50,23 @@ enum Mapping {
5150
DisallowedIdna2008,
5251
}
5352

54-
struct Range {
55-
from: char,
56-
to: char,
57-
}
58-
5953
fn find_char(codepoint: char) -> &'static Mapping {
60-
let r = TABLE.binary_search_by(|ref range| {
61-
if codepoint > range.to {
62-
Less
63-
} else if codepoint < range.from {
64-
Greater
65-
} else {
66-
Equal
67-
}
68-
});
69-
r.ok()
70-
.map(|i| {
71-
const SINGLE_MARKER: u16 = 1 << 15;
54+
let idx = match TABLE.binary_search(&codepoint) {
55+
Ok(idx) => idx,
56+
Err(idx) => idx - 1,
57+
};
7258

73-
let x = INDEX_TABLE[i];
74-
let single = (x & SINGLE_MARKER) != 0;
75-
let offset = !SINGLE_MARKER & x;
59+
const SINGLE_MARKER: u16 = 1 << 15;
7660

77-
if single {
78-
&MAPPING_TABLE[offset as usize]
79-
} else {
80-
&MAPPING_TABLE[(offset + (codepoint as u16 - TABLE[i].from as u16)) as usize]
81-
}
82-
})
83-
.unwrap()
61+
let x = INDEX_TABLE[idx];
62+
let single = (x & SINGLE_MARKER) != 0;
63+
let offset = !SINGLE_MARKER & x;
64+
65+
if single {
66+
&MAPPING_TABLE[offset as usize]
67+
} else {
68+
&MAPPING_TABLE[(offset + (codepoint as u16 - TABLE[idx] as u16)) as usize]
69+
}
8470
}
8571

8672
struct Mapper<'a> {

0 commit comments

Comments
 (0)