From 80986efee94cad92c582a82ffa8d19bb56d34313 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Fri, 31 Jul 2020 04:39:06 +0800 Subject: [PATCH] refactor `is_ms_filter` * refactor to use matches or patterns * simplify text searching with iterator * match is_ms_filter on bytes level * add regex doc for is_ms_filter * use is_ascii_alphabetic * check equality rather than map_or = Co-authored-by: Connor Skees --- src/builtin/functions/color/opacity.rs | 31 ++++++-------------------- 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/src/builtin/functions/color/opacity.rs b/src/builtin/functions/color/opacity.rs index 2314132..e61501b 100644 --- a/src/builtin/functions/color/opacity.rs +++ b/src/builtin/functions/color/opacity.rs @@ -5,35 +5,18 @@ use crate::{ value::Value, }; +/// Matches regex `^[a-zA-Z]+\s*=`. fn is_ms_filter(s: &str) -> bool { - let mut chars = s.chars(); + let mut bytes = s.bytes(); - if let Some(c) = chars.next() { - if !matches!(c, 'a'..='z' | 'A'..='Z') { - return false; - } - } else { + if !bytes.next().map_or(false, |c| c.is_ascii_alphabetic()) { return false; } - for c in &mut chars { - match c { - ' ' | '\t' | '\n' => break, - 'a'..='z' | 'A'..='Z' => continue, - '=' => return true, - _ => return false, - } - } - - for c in chars { - match c { - ' ' | '\t' | '\n' => continue, - '=' => return true, - _ => return false, - } - } - - false + bytes + .skip_while(|c| c.is_ascii_alphabetic()) + .find(|c| !matches!(c, b' ' | b'\t' | b'\n')) + == Some(b'=') } #[cfg(test)]