Skip to content

Commit 70a8669

Browse files
committed
idna: only store range start in the initial TABLE
1 parent 3628c22 commit 70a8669

File tree

3 files changed

+1667
-1684
lines changed

3 files changed

+1667
-1684
lines changed

idna/src/make_uts46_mapping_table.py

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

151151

152-
print("static TABLE: &[Range] = &[")
152+
print("static TABLE: &[char] = &[")
153153

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

160157
print("];\n")
161158

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;
@@ -50,36 +49,23 @@ enum Mapping {
5049
DisallowedStd3Mapped(StringTableSlice),
5150
}
5251

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

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

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

8571
struct Mapper<'a> {

0 commit comments

Comments
 (0)