fix: properly count URL when preceeded by '\n'

This commit is contained in:
VC
2023-11-29 11:25:27 +01:00
parent ca9b388a50
commit 26788f9d37

View File

@@ -37,10 +37,15 @@ fn twitter_count(content: &str) -> usize {
count += split_content.clone().count() - 1; // count the spaces
for word in split_content {
if word.starts_with("http://") || word.starts_with("https://") {
count += 23;
} else {
count += word.chars().count();
let cr_words = word.split('\n');
count += cr_words.clone().count() - 1; // count the chariot returns
for w in cr_words {
if w.starts_with("http://") || w.starts_with("https://") {
count += 23;
} else {
count += w.chars().count();
}
}
}
@@ -105,6 +110,14 @@ mod tests {
let content = "this is the link https://www.google.com/tamerelol/youpi/tonperemdr/tarace.html if you like! What if I shit a final";
assert_eq!(twitter_count(content), 76);
let content = "multi ple space";
assert_eq!(twitter_count(content), content.chars().count());
let content = "This link is LEEEEET\n\nhttps://www.factornews.com/actualites/ca-sent-le-sapin-pour-free-radical-design-49985.html";
assert_eq!(twitter_count(content), 45);
}
#[test]