pub struct EmojiTool;
impl EmojiTool {
pub fn new() -> Self {
Self
}
pub fn contains_emoji(&self, text: impl AsRef<str>) -> bool {
let text = text.as_ref();
text.chars().any(|c| self.is_emoji_char(c))
}
pub fn remove_all(&self, text: impl AsRef<str>) -> String {
let text = text.as_ref();
text.chars().filter(|c| !self.is_emoji_char(*c)).collect()
}
pub fn replace_all(&self, text: impl AsRef<str>, replacement: impl AsRef<str>) -> String {
let text = text.as_ref();
let rep = replacement.as_ref();
let mut result = String::with_capacity(text.len());
for c in text.chars() {
if self.is_emoji_char(c) {
result.push_str(rep);
} else {
result.push(c);
}
}
result
}
fn is_emoji_char(&self, c: char) -> bool {
let u = c as u32;
(0x1F300..=0x1F5FF).contains(&u) || (0x1F900..=0x1F9FF).contains(&u) || (0x1F600..=0x1F64F).contains(&u) || (0x1F680..=0x1F6FF).contains(&u) || (0x2600..=0x26FF).contains(&u) || (0x2700..=0x27BF).contains(&u) || (0xFE00..=0xFE0F).contains(&u) || (0x1F1E6..=0x1F1FF).contains(&u) || (0x1F018..=0x1F270).contains(&u) || (0x1F700..=0x1F77F).contains(&u) || (0x1F780..=0x1F7FF).contains(&u) || (0x1F800..=0x1F8FF).contains(&u) || (0x1FA70..=0x1FAFF).contains(&u) }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_emoji_tool() {
let tool = EmojiTool::new();
assert!(tool.contains_emoji("Hello 😀!"));
assert!(!tool.contains_emoji("Hello World!"));
let cleaned = tool.remove_all("Bad 💩 Data 🤡!");
assert_eq!(cleaned, "Bad Data !");
}
}