use std::borrow::Cow;
use crate::bytes::{Reader, push_string, push_u32};
use crate::error::{Error, Result};
const MAX_RULES: u32 = 1 << 20;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Normalization {
rules: Vec<(String, String)>,
longest: usize,
}
impl Normalization {
pub fn new(rules: &[(&str, &str)]) -> Result<Self> {
let mut sorted: Vec<(String, String)> = rules
.iter()
.map(|(from, to)| (from.to_string(), to.to_string()))
.collect();
sorted.sort();
sorted.dedup();
Self::from_sorted(sorted)
}
fn from_sorted(rules: Vec<(String, String)>) -> Result<Self> {
if let Some((_, to)) = rules.iter().find(|(from, _)| from.is_empty()) {
return Err(Error::Schema(format!(
"a rule of the normalization replaces an empty string by {to:?}"
)));
}
if let Some(pair) = rules.windows(2).find(|pair| pair[0].0 >= pair[1].0) {
return Err(Error::Schema(format!(
"the rules of the normalization replace {:?} twice, or are not sorted",
pair[1].0
)));
}
let longest = rules.iter().map(|(from, _)| from.len()).max().unwrap_or(0);
Ok(Self { rules, longest })
}
pub fn is_empty(&self) -> bool {
self.rules.is_empty()
}
pub fn rules(&self) -> impl Iterator<Item = (&str, &str)> {
self.rules
.iter()
.map(|(from, to)| (from.as_str(), to.as_str()))
}
pub fn apply<'a>(&self, surface: &'a str) -> Cow<'a, str> {
if self.rules.is_empty() {
return Cow::Borrowed(surface);
}
let mut output: Option<String> = None;
let mut position = 0;
while position < surface.len() {
let rest = &surface[position..];
let step = match self.longest_rule(rest) {
Some((from, to)) => {
output
.get_or_insert_with(|| surface[..position].to_string())
.push_str(to);
from.len()
}
None => {
let step = rest.chars().next().map_or(1, char::len_utf8);
if let Some(output) = output.as_mut() {
output.push_str(&rest[..step]);
}
step
}
};
position += step;
}
output.map_or(Cow::Borrowed(surface), Cow::Owned)
}
fn longest_rule(&self, rest: &str) -> Option<(&str, &str)> {
let mut end = self.longest.min(rest.len());
while end > 0 {
if rest.is_char_boundary(end) {
let prefix = &rest[..end];
if let Ok(index) = self
.rules
.binary_search_by(|(from, _)| from.as_str().cmp(prefix))
{
let (from, to) = &self.rules[index];
return Some((from.as_str(), to.as_str()));
}
}
end -= 1;
}
None
}
pub(super) fn write(&self, bytes: &mut Vec<u8>) {
push_u32(bytes, self.rules.len() as u32);
for (from, to) in &self.rules {
push_string(bytes, from);
push_string(bytes, to);
}
}
pub(super) fn read(reader: &mut Reader) -> Result<Self> {
let count = reader.u32()?;
if count > MAX_RULES {
return Err(reader.error(format_args!(
"has {count} rules of normalization, more than {MAX_RULES}"
)));
}
let mut rules = Vec::new();
for _ in 0..count {
let from = reader.string()?;
let to = reader.string()?;
rules.push((from, to));
}
Self::from_sorted(rules).map_err(|error| {
reader.error(format_args!("has a normalization that is not one: {error}"))
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_longest_rule_at_a_position_is_applied() {
let table = Normalization::new(&[("カ", "カ"), ("ガ", "ガ"), ("゙", "゛")]).unwrap();
assert_eq!(table.apply("ガ"), "ガ");
assert_eq!(table.apply("カ"), "カ");
assert_eq!(table.apply("カガ゙"), "カガ゛");
}
#[test]
fn what_no_rule_matches_is_kept() {
let table = Normalization::new(&[("1", "1"), (" ", "___")]).unwrap();
assert_eq!(table.apply("第10回 大会"), "第10回___大会");
assert_eq!(table.apply(""), "");
}
#[test]
fn the_result_of_a_rule_is_not_normalized_again() {
let table = Normalization::new(&[("a", "b"), ("b", "c")]).unwrap();
assert_eq!(table.apply("ab"), "bc");
}
#[test]
fn a_surface_without_a_match_is_borrowed() {
let table = Normalization::new(&[("1", "1")]).unwrap();
assert!(matches!(table.apply("読ん"), Cow::Borrowed("読ん")));
assert!(matches!(table.apply("1"), Cow::Owned(_)));
assert!(matches!(
Normalization::default().apply("1"),
Cow::Borrowed("1")
));
}
#[test]
fn an_empty_table_changes_nothing() {
let table = Normalization::default();
assert!(table.is_empty());
assert_eq!(table.apply("ガ1 "), "ガ1 ");
assert_eq!(table, Normalization::new(&[]).unwrap());
}
#[test]
fn the_rules_are_sorted_and_each_is_there_once() {
let table = Normalization::new(&[("イ", "イ"), ("ア", "ア"), ("イ", "イ")]).unwrap();
let rules: Vec<(&str, &str)> = table.rules().collect();
assert_eq!(rules, [("ア", "ア"), ("イ", "イ")]);
assert!(!table.is_empty());
}
#[test]
fn refuses_rules_that_are_not_a_table() {
let error = Normalization::new(&[("", "x")]).unwrap_err();
assert!(error.to_string().contains("empty string"), "{error}");
let error = Normalization::new(&[("a", "x"), ("a", "y")]).unwrap_err();
assert!(error.to_string().contains("\"a\" twice"), "{error}");
}
#[test]
fn a_table_is_read_as_it_was_written() {
let table = Normalization::new(&[("ガ", "ガ"), ("1", "1"), (" ", "___")]).unwrap();
let mut bytes = Vec::new();
table.write(&mut bytes);
let mut reader = Reader::new(&bytes, "model", Error::Model);
assert_eq!(Normalization::read(&mut reader).unwrap(), table);
let mut bytes = Vec::new();
Normalization::default().write(&mut bytes);
assert_eq!(bytes, [0, 0, 0, 0]);
}
#[test]
fn refuses_bytes_that_are_not_a_table() {
let mut bytes = Vec::new();
push_u32(&mut bytes, 2);
for text in ["b", "x", "a", "y"] {
push_string(&mut bytes, text);
}
let error =
Normalization::read(&mut Reader::new(&bytes, "model", Error::Model)).unwrap_err();
assert!(error.to_string().contains("normalization"), "{error}");
let mut bytes = Vec::new();
push_u32(&mut bytes, MAX_RULES + 1);
let error =
Normalization::read(&mut Reader::new(&bytes, "model", Error::Model)).unwrap_err();
assert!(
error.to_string().contains("rules of normalization"),
"{error}"
);
let mut bytes = Vec::new();
push_u32(&mut bytes, 1);
assert!(Normalization::read(&mut Reader::new(&bytes, "model", Error::Model)).is_err());
}
}