-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dict): Perform case-insensitive comparisons
- Loading branch information
Showing
4 changed files
with
55 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,34 @@ | ||
include!(concat!(env!("OUT_DIR"), "/codegen.rs")); | ||
|
||
pub struct Dictionary { | ||
} | ||
pub struct Dictionary {} | ||
|
||
impl Dictionary { | ||
pub fn new() -> Self { | ||
Dictionary { } | ||
Dictionary {} | ||
} | ||
|
||
pub fn correct_str<'s, 'w>(&'s self, word: &'w str) -> Option<&'s str> { | ||
map_lookup(&DICTIONARY, word) | ||
} | ||
|
||
pub fn correct_str<'s>(&'s self, word: &str) -> Option<&'s str> { | ||
DICTIONARY.get(word).map(|s| *s) | ||
pub fn correct_bytes<'s, 'w>(&'s self, word: &'w [u8]) -> Option<&'s str> { | ||
std::str::from_utf8(word) | ||
.ok() | ||
.and_then(|word| self.correct_str(word)) | ||
} | ||
} | ||
|
||
pub fn correct_bytes<'s>(&'s self, word: &[u8]) -> Option<&'s str> { | ||
std::str::from_utf8(word).ok().and_then(|word| DICTIONARY.get(word)).map(|s| *s) | ||
fn map_lookup( | ||
map: &'static phf::Map<UniCase<&'static str>, &'static str>, | ||
key: &str, | ||
) -> Option<&'static str> { | ||
// This transmute should be safe as `get` will not store the reference with | ||
// the expanded lifetime. This is due to `Borrow` being overly strict and | ||
// can't have an impl for `&'static str` to `Borrow<&'a str>`. | ||
// | ||
// See https://github.com/rust-lang/rust/issues/28853#issuecomment-158735548 | ||
unsafe { | ||
let key = ::std::mem::transmute::<_, &'static str>(key); | ||
map.get(&UniCase(key)).map(|s| *s) | ||
} | ||
} |