Skip to content

Commit e5f7dfc

Browse files
committed
idna: use standard binary_search_by
Doing this is arguably nicer and avoids an edge case when searching for the last element in the table.
1 parent d794e04 commit e5f7dfc

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

idna/src/uts46.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use self::Mapping::*;
1313
use punycode;
1414
use std::ascii::AsciiExt;
15+
use std::cmp::Ordering::{Equal, Less, Greater};
1516
use unicode_normalization::UnicodeNormalization;
1617
use unicode_normalization::char::is_combining_mark;
1718
use unicode_bidi::{BidiClass, bidi_class};
@@ -49,20 +50,16 @@ struct Range {
4950
}
5051

5152
fn find_char(codepoint: char) -> &'static Mapping {
52-
let mut min = 0;
53-
let mut max = TABLE.len() - 1;
54-
while max > min {
55-
let mid = (min + max) >> 1;
56-
if codepoint > TABLE[mid].to {
57-
min = mid;
58-
} else if codepoint < TABLE[mid].from {
59-
max = mid;
53+
let r = TABLE.binary_search_by(|ref range| {
54+
if codepoint > range.to {
55+
Less
56+
} else if codepoint < range.from {
57+
Greater
6058
} else {
61-
min = mid;
62-
max = mid;
59+
Equal
6360
}
64-
}
65-
&TABLE[min].mapping
61+
});
62+
r.ok().map(|i| &TABLE[i].mapping).unwrap()
6663
}
6764

6865
fn map_char(codepoint: char, flags: Flags, output: &mut String, errors: &mut Vec<Error>) {

0 commit comments

Comments
 (0)