Remove emoji
This commit is contained in:
parent
f47dff6ca8
commit
94159e8cad
2 changed files with 42 additions and 1 deletions
|
|
@ -15,7 +15,7 @@ Procedure when bumping the version number:
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
- All emoji known to the vanilla client
|
- All emoji known to the vanilla client
|
||||||
- Finding and replacing emoji in text
|
- Finding, replacing and removing emoji in text
|
||||||
- `State` conversion utility methods
|
- `State` conversion utility methods
|
||||||
- `Time::new` constructor
|
- `Time::new` constructor
|
||||||
|
|
||||||
|
|
|
||||||
41
src/emoji.rs
41
src/emoji.rs
|
|
@ -89,6 +89,31 @@ impl Emoji {
|
||||||
|
|
||||||
Cow::Owned(result)
|
Cow::Owned(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn remove<'a>(&self, text: &'a str) -> Cow<'a, str> {
|
||||||
|
let emoji = self.find(text);
|
||||||
|
if emoji.is_empty() {
|
||||||
|
return Cow::Borrowed(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut result = String::new();
|
||||||
|
|
||||||
|
let mut after_last_emoji = 0;
|
||||||
|
for (range, _) in emoji {
|
||||||
|
if *range.start() > after_last_emoji {
|
||||||
|
// There were non-emoji characters between the last and the
|
||||||
|
// current emoji.
|
||||||
|
result.push_str(&text[after_last_emoji..*range.start()]);
|
||||||
|
}
|
||||||
|
after_last_emoji = range.end() + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if after_last_emoji < text.len() {
|
||||||
|
result.push_str(&text[after_last_emoji..]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Cow::Owned(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
@ -133,4 +158,20 @@ mod test {
|
||||||
);
|
);
|
||||||
assert_eq!(emoji.replace("Jan-20 17:58 Z"), "Jan-20 17:58 Z");
|
assert_eq!(emoji.replace("Jan-20 17:58 Z"), "Jan-20 17:58 Z");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn remove() {
|
||||||
|
let emoji = Emoji::load();
|
||||||
|
assert_eq!(emoji.remove("no:emo:ji:here"), "no:emo:ji:here");
|
||||||
|
assert_eq!(emoji.remove(":bad:x:o:"), ":bado:");
|
||||||
|
assert_eq!(emoji.remove(":x:bad:o:"), "bad");
|
||||||
|
assert_eq!(emoji.remove("ab:bad:x:o:cd"), "ab:bado:cd");
|
||||||
|
assert_eq!(emoji.remove("ab:x:bad:o:cd"), "abbadcd");
|
||||||
|
assert_eq!(emoji.remove("chᴜm:crown::ant:"), "chᴜm");
|
||||||
|
assert_eq!(
|
||||||
|
emoji.remove(":waning_crescent_moon: (2% full)"),
|
||||||
|
" (2% full)"
|
||||||
|
);
|
||||||
|
assert_eq!(emoji.remove("Jan-20 17:58 Z"), "Jan-20 17:58 Z");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue