From ecca5757d4fe38a1a7158824b1b9c666dd225e95 Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Tue, 9 Jun 2015 00:33:47 +0200 Subject: Fix name prefix stripping --- src/freq.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/freq.rs b/src/freq.rs index 21c4e62..dc692c6 100644 --- a/src/freq.rs +++ b/src/freq.rs @@ -15,6 +15,14 @@ fn words(s: &str) -> u32 { s.split_whitespace().filter(|s| !s.is_empty()).count() as u32 } +fn strip_nick(s: &str) -> &str { + if s.is_empty() { return s } + match s.as_bytes()[0] { + b'~' | b'&' | b'@' | b'%' | b'+' => &s[1..], + _ => s + } +} + fn main() { let stdin = io::stdin(); @@ -29,12 +37,13 @@ fn main() { match m { Msg { ref from, ref content, .. } => { - if stats.contains_key(from) { - let p: &mut Person = stats.get_mut(from).unwrap(); + let nick = strip_nick(from); + if stats.contains_key(nick) { + let p: &mut Person = stats.get_mut(nick).unwrap(); p.lines += 1; p.words += words(content); } else { - stats.insert(from.clone(), Person { + stats.insert(nick.to_owned(), Person { lines: 1, words: words(content) }); -- cgit v1.2.3