Skip to content

Commit 3ac9183

Browse files
Remove emoji from clip titles
1 parent f616b42 commit 3ac9183

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

Cargo.lock

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ lru = "0.8"
1313
tracing = "0.1"
1414
tracing-subscriber = "0.3"
1515
async-trait = "0.1"
16+
emojis = "0.6"
17+
fxhash = "0.2"
1618

1719
[dependencies.once_cell]
1820
version = "1"

emojis.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

src/util.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use fxhash::FxHashSet;
2+
use once_cell::sync::Lazy;
13
use serde::{Deserialize, Serialize};
24
use std::{num::NonZeroU64, ops::Add};
35

@@ -33,3 +35,37 @@ impl Add<u64> for Timestamp {
3335
unsafe { Self(NonZeroU64::new_unchecked(self.0.get() + rhs)) }
3436
}
3537
}
38+
39+
const EMOJI_ARRAY: [u32; 9280] = include!("../emojis.txt");
40+
41+
static EMOJI_SET: Lazy<FxHashSet<u32>> = Lazy::new(|| EMOJI_ARRAY.iter().copied().collect());
42+
43+
pub fn strip_emoji<'a>(text: &'a str) -> String {
44+
text.chars()
45+
.filter(|&c| !EMOJI_SET.contains(&(c as u32)))
46+
.fold(String::with_capacity(text.len()), |mut acc, c| {
47+
if c.is_whitespace() || c.is_control() {
48+
acc.push(' ');
49+
} else {
50+
acc.push(c);
51+
}
52+
acc
53+
})
54+
}
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use super::*;
59+
60+
#[test]
61+
fn test_strip_emoji() {
62+
assert_eq!(strip_emoji("no emoji"), "no emoji");
63+
assert_eq!(strip_emoji("simple smiley 😄"), "simple smiley ");
64+
assert_eq!(strip_emoji("with skin tone 👍🏽"), "with skin tone ");
65+
assert_eq!(strip_emoji("basic flag 🇩🇪 "), "basic flag ");
66+
assert_eq!(
67+
strip_emoji("simple | text - with $ special & chars %"),
68+
"simple | text - with $ special & chars %"
69+
);
70+
}
71+
}

src/watcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
discord::WebhookClient,
1313
error::{AsyncError as Error, RequestError},
1414
twitch::{Game, Stream, TwitchClient, VideoDuration},
15-
util::Timestamp,
15+
util::{Timestamp, strip_emoji},
1616
};
1717

1818
const fn split_duration(secs: u32) -> (u8, u8, u8) {
@@ -385,7 +385,7 @@ impl StreamWatcher {
385385
format!(
386386
"`{}.` [**{} \u{1F855}**]({}) \u{2022} **{}**\u{00A0}views\n",
387387
i + 1,
388-
title,
388+
strip_emoji(&title),
389389
c.url,
390390
c.view_count
391391
)

0 commit comments

Comments
 (0)