From 26788f9d37e57b9fa9f824902a1538dd62581b6a Mon Sep 17 00:00:00 2001 From: VC Date: Wed, 29 Nov 2023 11:25:27 +0100 Subject: [PATCH] fix: properly count URL when preceeded by '\n' --- src/utils.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index a91cc38..68de7ac 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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]