Skip to content

Commit b6f8a5b

Browse files
committed
Use stdlib binary search
1 parent 4d14013 commit b6f8a5b

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

src/tables.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Character Tables
22
use unicode_bidi::{bidi_class, BidiClass};
3+
use std::cmp::Ordering;
34

45
use super::rfc3454;
56

@@ -193,28 +194,17 @@ type TableEntry<'a> = (char, Option<char>, Option<&'a str>);
193194

194195
// Try to find a character in a lookup table.
195196
fn table_lookup<'a>(table: &'a [TableEntry<'a>], c: char) -> Option<TableEntry<'a>> {
196-
let mut low = 0;
197-
let mut high = table.len();
198-
199-
// Binary search.
200-
while low < high {
201-
let middle = (low + high) / 2;
202-
let entry = table[middle];
203-
let (start, end, _) = entry;
204-
if c == start {
205-
return Some(entry);
206-
}
207-
if c < start {
208-
high = middle;
209-
continue;
210-
}
211-
if let Some(end) = end {
212-
if c <= end {
213-
return Some(entry);
197+
table
198+
.binary_search_by(|&(start, end, _)| match start.cmp(&c) {
199+
Ordering::Greater => Ordering::Greater,
200+
Ordering::Equal => Ordering::Equal,
201+
Ordering::Less => {
202+
match end {
203+
Some(end) if c <= end => Ordering::Equal,
204+
_ => Ordering::Less,
205+
}
214206
}
215-
}
216-
low = middle + 1;
217-
}
218-
219-
return None
207+
})
208+
.ok()
209+
.map(|i| table[i])
220210
}

0 commit comments

Comments
 (0)