Add comment to Language::words_by_prefix

This commit is contained in:
Steven Roose 2020-06-19 11:56:20 +01:00
parent e6cb3dfa1f
commit b81e1bd75d
No known key found for this signature in database
GPG Key ID: 2F2A88D7F8D68E87
1 changed files with 15 additions and 10 deletions

View File

@ -54,16 +54,6 @@ pub enum Language {
}
impl Language {
/// Get words from the wordlist that start with the given prefix.
pub fn words_by_prefix(self, prefix: &str) -> &[&'static str] {
let first = match self.word_list().iter().position(|w| w.starts_with(prefix)) {
Some(i) => i,
None => return &[],
};
let count = self.word_list()[first..].iter().take_while(|w| w.starts_with(prefix)).count();
&self.word_list()[first .. first + count]
}
/// The word list for this language.
#[inline]
pub(crate) fn word_list(self) -> &'static [&'static str; 2048] {
@ -88,6 +78,21 @@ impl Language {
}
}
/// Get words from the wordlist that start with the given prefix.
pub fn words_by_prefix(self, prefix: &str) -> &[&'static str] {
// The words in the wordlist are ordered lexicographically. This means
// that we cannot use `binary_search` to find words more efficiently,
// because the Rust ordering is based on the byte values. However, it
// does mean that words that share a prefix will follow each other.
let first = match self.word_list().iter().position(|w| w.starts_with(prefix)) {
Some(i) => i,
None => return &[],
};
let count = self.word_list()[first..].iter().take_while(|w| w.starts_with(prefix)).count();
&self.word_list()[first .. first + count]
}
/// Get the index of the word in the word list.
#[inline]
pub(crate) fn find_word(self, word: &str) -> Option<usize> {