pub type PropertyTable = &'static [(&'static str, &'static str)];
pub fn canonical_property_name(
property_table: PropertyTable,
normalized_property_name: &str,
) -> Option<&'static str> {
property_table
.binary_search_by_key(&normalized_property_name, |&(n, _)| n)
.ok()
.map(|i| property_table[i].1)
}
pub type PropertyValueTable = &'static [(&'static str, PropertyValues)];
pub type PropertyValues = &'static [(&'static str, &'static str)];
pub fn property_values(
property_value_table: PropertyValueTable,
canonical_property_name: &str,
) -> Option<PropertyValues> {
property_value_table
.binary_search_by_key(&canonical_property_name, |&(n, _)| n)
.ok()
.map(|i| property_value_table[i].1)
}
pub fn canonical_property_value(
property_values: PropertyValues,
normalized_property_value: &str,
) -> Option<&'static str> {
canonical_property_name(property_values, normalized_property_value)
}
#[cfg(test)]
mod tests {
use crate::unicode_tables::property_names::PROPERTY_NAMES;
use crate::unicode_tables::property_values::PROPERTY_VALUES;
use super::{
canonical_property_name, canonical_property_value, property_values,
};
#[test]
fn canonical_property_name_1() {
assert_eq!(
canonical_property_name(PROPERTY_NAMES, "gc"),
Some("General_Category")
);
assert_eq!(
canonical_property_name(PROPERTY_NAMES, "generalcategory"),
Some("General_Category")
);
assert_eq!(canonical_property_name(PROPERTY_NAMES, "g c"), None);
}
#[test]
fn property_values_1() {
assert_eq!(
property_values(PROPERTY_VALUES, "White_Space"),
Some(
&[
("f", "No"),
("false", "No"),
("n", "No"),
("no", "No"),
("t", "Yes"),
("true", "Yes"),
("y", "Yes"),
("yes", "Yes"),
][..]
)
);
}
#[test]
fn canonical_property_value_1() {
let values = property_values(PROPERTY_VALUES, "White_Space").unwrap();
assert_eq!(canonical_property_value(values, "false"), Some("No"));
assert_eq!(canonical_property_value(values, "t"), Some("Yes"));
assert_eq!(canonical_property_value(values, "F"), None);
}
}