fix(dictgen)!: Allow case-sensitive maps

This commit is contained in:
Ed Page 2024-12-30 16:24:14 -06:00
parent 534e3c5f71
commit d4767767b2
2 changed files with 28 additions and 5 deletions

View file

@ -14,6 +14,7 @@ impl MapGen<'_> {
data.sort_unstable_by_key(|v| unicase::UniCase::new(v.0));
let name = self.gen.name;
let key_type = "dictgen::InsensitiveStr<'static>";
let value_type = self.gen.value_type;
let mut smallest = usize::MAX;
@ -44,7 +45,7 @@ impl MapGen<'_> {
writeln!(
file,
"pub static {name}: dictgen::Map<{value_type}> = dictgen::Map {{"
"pub static {name}: dictgen::Map<{key_type}, {value_type}> = dictgen::Map {{"
)?;
writeln!(file, " map: {builder},")?;
writeln!(file, " range: {smallest}..={largest},")?;
@ -54,12 +55,12 @@ impl MapGen<'_> {
}
}
pub struct Map<V: 'static> {
pub map: phf::Map<crate::InsensitiveStr<'static>, V>,
pub struct Map<K: 'static, V: 'static> {
pub map: phf::Map<K, V>,
pub range: std::ops::RangeInclusive<usize>,
}
impl<V> Map<V> {
impl<V> Map<crate::InsensitiveStr<'_>, V> {
#[inline]
pub fn find(&self, word: &'_ unicase::UniCase<&str>) -> Option<&V> {
if self.range.contains(&word.len()) {
@ -69,3 +70,25 @@ impl<V> Map<V> {
}
}
}
impl<V> Map<crate::InsensitiveAscii<'_>, V> {
#[inline]
pub fn find(&self, word: &'_ unicase::Ascii<&str>) -> Option<&V> {
if self.range.contains(&word.len()) {
self.map.get(&(*word).into())
} else {
None
}
}
}
impl<V> Map<&str, V> {
#[inline]
pub fn find(&self, word: &'_ &str) -> Option<&V> {
if self.range.contains(&word.len()) {
self.map.get(word)
} else {
None
}
}
}

View file

@ -2,7 +2,7 @@
#![allow(clippy::unreadable_literal)]
#![allow(unreachable_pub)]
pub static WORD: dictgen::Map<&[&str]> = dictgen::Map {
pub static WORD: dictgen::Map<dictgen::InsensitiveStr<'static>, &[&str]> = dictgen::Map {
map: ::phf::Map {
key: 12913932095322966823,
disps: &[