Skip to content

Commit

Permalink
add fst::raw::Fst::find_longest_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Bogus committed Apr 8, 2020
1 parent 7c8b718 commit fc7f281
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,34 @@ impl<D: AsRef<[u8]>> Fst<D> {
self.as_ref().get_key_into(value, key)
}

/// find the longest key that is prefix of the given value.
///
/// If the key exists, then `Some((value, key_len))` is returned, where
/// `value` is the value associated with the key, and `key_len` is the
/// length of the found key. Otherwise `None` is returned.
///
/// This can be used to e.g. build tokenizing functions.
#[inline]
pub fn find_longest_prefix(&self, value: &[u8]) -> Option<(u64, usize)> {
let mut node = self.root();
let mut out = Output::zero();
let mut last_match = None;
for (i, &b) in input.iter().enumerate() {
if let Some(trans_index) = node.find_input(b) {
let t = node.transition(trans_index);
node = self.node(t.addr);
if node.is_final() {
last_match =
Some((out.cat(node.final_output()).value(), i + 1));
}
out = out.cat(t.out);
} else {
return last_match;
}
}
last_match
}

/// Return a lexicographically ordered stream of all key-value pairs in
/// this fst.
#[inline]
Expand Down

0 comments on commit fc7f281

Please sign in to comment.