Improve string like filter... again

This commit is contained in:
NGnius (Graham) 2022-03-27 11:59:56 -04:00
parent 313c7e9ab7
commit fb80a06b83

View file

@ -19,6 +19,14 @@ pub struct FieldLikeFilter {
val: VariableOrValue, val: VariableOrValue,
} }
impl FieldLikeFilter {
fn sanitise_string(s: &str) -> String {
s.replace(|c: char| c.is_whitespace() || c == '_' || c == '-', " ")
.replace(|c: char| !(c.is_whitespace() || c.is_alphanumeric()), "")
.to_lowercase()
}
}
impl Display for FieldLikeFilter { impl Display for FieldLikeFilter {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match &self.val { match &self.val {
@ -44,9 +52,9 @@ impl MpsFilterPredicate for FieldLikeFilter {
_ => Err(RuntimeMsg("Value is not type String".to_string())), _ => Err(RuntimeMsg("Value is not type String".to_string())),
}?; }?;
if let Some(field) = music_item_lut.field(&self.field_name) { if let Some(field) = music_item_lut.field(&self.field_name) {
let pattern = |c: char| c.is_whitespace() || c.is_control() || !c.is_alphanumeric(); let field_str = Self::sanitise_string(&field.as_str());
let field_str = field.as_str().replace(pattern, "").to_lowercase(); let var_str = Self::sanitise_string(variable);
Ok(field_str.contains(&variable.replace(pattern, "").to_lowercase())) Ok(field_str.contains(&var_str))
} else { } else {
match self.field_errors { match self.field_errors {
FieldFilterErrorHandling::Error => Err(RuntimeMsg(format!( FieldFilterErrorHandling::Error => Err(RuntimeMsg(format!(