-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e80b77
commit 3edd964
Showing
18 changed files
with
307 additions
and
228 deletions.
There are no files selected for viewing
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
Binary file not shown.
Binary file not shown.
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[cfg(feature = "inline-rs")] | ||
pub mod rs; | ||
|
||
#[cfg(feature = "inline-source")] | ||
pub mod source; |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
mod rules; | ||
|
||
use crate::DataProviderType; | ||
use crate::PluralRuleEnum; | ||
use crate::PluralRuleType; | ||
use icu_locale::LanguageIdentifier; | ||
|
||
#[derive(Default)] | ||
pub struct DataProvider {} | ||
|
||
impl DataProviderType for DataProvider { | ||
fn get_rule( | ||
&self, | ||
locale: &LanguageIdentifier, | ||
type_: PluralRuleType, | ||
) -> Option<PluralRuleEnum> { | ||
let rules = match type_ { | ||
PluralRuleType::Cardinal => rules::PRS_CARDINAL, | ||
PluralRuleType::Ordinal => rules::PRS_ORDINAL, | ||
}; | ||
if let Ok(idx) = rules.binary_search_by(|(lsv, _)| { | ||
lsv.language.cmp(&locale.language).then( | ||
lsv.script | ||
.cmp(&locale.script) | ||
.then(lsv.region.cmp(&locale.region)), | ||
) | ||
}) { | ||
Some(PluralRuleEnum::Function(rules[idx].1)) | ||
} else { | ||
None | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
mod rules; | ||
|
||
use crate::rules::ast; | ||
use crate::rules::parser::Parser; | ||
use crate::DataProviderType; | ||
use crate::PluralCategory; | ||
use crate::PluralRuleEnum; | ||
use crate::PluralRuleType; | ||
use icu_locale::LanguageIdentifier; | ||
|
||
#[derive(Default)] | ||
pub struct DataProvider {} | ||
|
||
impl DataProviderType for DataProvider { | ||
fn get_rule( | ||
&self, | ||
locale: &LanguageIdentifier, | ||
type_: PluralRuleType, | ||
) -> Option<PluralRuleEnum> { | ||
let rules = get_rules(locale, type_)?; | ||
Some(PluralRuleEnum::Conditions(rules)) | ||
} | ||
} | ||
|
||
pub fn get_rules( | ||
locale: &LanguageIdentifier, | ||
type_: PluralRuleType, | ||
) -> Option<Box<[(PluralCategory, ast::Condition)]>> { | ||
let data = match type_ { | ||
PluralRuleType::Cardinal => rules::DATA_CARDINAL, | ||
PluralRuleType::Ordinal => rules::DATA_ORDINAL, | ||
}; | ||
let r = if let Ok(idx) = data.binary_search_by(|(lsv, _)| { | ||
lsv.language.cmp(&locale.language).then( | ||
lsv.script | ||
.cmp(&locale.script) | ||
.then(lsv.region.cmp(&locale.region)), | ||
) | ||
}) { | ||
Some(data[idx].1) | ||
} else { | ||
None | ||
}; | ||
|
||
let mut rules = vec![]; | ||
|
||
for (category, input) in r? { | ||
let p = Parser::new(input.as_bytes()); | ||
let ast = p | ||
.parse() | ||
.unwrap_or_else(|_| panic!("Failed to parse: {:#?}, {:#?}", locale, category)); | ||
rules.push((*category, ast)); | ||
} | ||
Some(rules.into_boxed_slice()) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use icu_locale::subtags; | ||
|
||
use crate::PluralCategory; | ||
|
||
#[derive(PartialOrd, PartialEq, Ord, Eq)] | ||
pub struct LSV { | ||
pub language: subtags::Language, | ||
pub script: Option<subtags::Script>, | ||
pub region: Option<subtags::Region>, | ||
} | ||
|
||
macro_rules! langid { | ||
( $ lang : expr , $ script : expr , $ region : expr ) => {{ | ||
unsafe { | ||
LSV { | ||
language: $lang, | ||
script: $script, | ||
region: $region, | ||
} | ||
} | ||
}}; | ||
} | ||
|
||
pub const DATA_CARDINAL: &[(LSV, &[(PluralCategory, &str)])] = &[ | ||
( | ||
langid!(subtags::Language::from_raw_unchecked(28261u64), None, None), | ||
&[ | ||
(PluralCategory::One, "i = 1 and v = 0 @integer 1"), | ||
(PluralCategory::Other, " @integer 0, 2~16, 100, 1000, 10000, 100000, 1000000, … @decimal 0.0~1.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, …"), | ||
] | ||
), | ||
( | ||
langid!(subtags::Language::from_raw_unchecked(27760u64), None, None), | ||
&[ | ||
(PluralCategory::One, "i = 1 and v = 0"), | ||
(PluralCategory::Few, "v = 0 and i % 10 = 2..4 and i % 100 != 12..14"), | ||
(PluralCategory::Many, "v = 0 and i != 1 and i % 10 = 0..1 or v = 0 and i % 10 = 5..9 or v = 0 and i % 100 = 12..14"), | ||
(PluralCategory::Other, ""), | ||
] | ||
) | ||
]; | ||
|
||
pub const DATA_ORDINAL: &[(LSV, &[(PluralCategory, &str)])] = &[]; |
Oops, something went wrong.