core: Add binary_search and binary_search_elem methods to slices.

These are like the existing bsearch methods but if the search fails,
it returns the next insertion point.

The new `binary_search` returns a `BinarySearchResult` that is either
`Found` or `NotFound`. For convenience, the `found` and `not_found`
methods convert to `Option`, ala `Result`.

Deprecate bsearch and bsearch_elem.
This commit is contained in:
Brian Anderson
2014-08-06 20:48:25 -07:00
parent 76d46af6d4
commit a4b354ca02
8 changed files with 186 additions and 47 deletions

View File

@@ -35,6 +35,7 @@
use std::cmp;
use std::mem;
use std::slice;
use std::slice::MutableSlice;
use compile::{
Program,
@@ -222,8 +223,8 @@ impl<'r, 't> Nfa<'r, 't> {
let negate = flags & FLAG_NEGATED > 0;
let casei = flags & FLAG_NOCASE > 0;
let found = ranges.as_slice();
let found = found.bsearch(|&rc| class_cmp(casei, c, rc));
let found = found.is_some();
let found = found.binary_search(|&rc| class_cmp(casei, c, rc))
.found().is_some();
if found ^ negate {
self.add(nlist, pc+1, caps);
}
@@ -513,7 +514,7 @@ pub fn is_word(c: Option<char>) -> bool {
// Try the common ASCII case before invoking binary search.
match c {
'_' | '0' .. '9' | 'a' .. 'z' | 'A' .. 'Z' => true,
_ => PERLW.bsearch(|&(start, end)| {
_ => PERLW.binary_search(|&(start, end)| {
if c >= start && c <= end {
Equal
} else if start > c {
@@ -521,7 +522,7 @@ pub fn is_word(c: Option<char>) -> bool {
} else {
Less
}
}).is_some()
}).found().is_some()
}
}