From c15c05a64e82c7c5f7af0539745855c0f6821869 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 20 Jan 2023 19:12:10 +0100 Subject: [PATCH] Take emoji into account when calculating nick hue --- CHANGELOG.md | 2 ++ src/huehash.rs | 15 +++++++++++++-- src/lib.rs | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3237718..b48a2c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ Procedure when bumping the version number: ### Changed - Rewrite `conn` module (backwards-imcompatible) +- Take emoji into account when calculating nick hue using `nick_hue` (backwards-incompatible) +- Rename `nick_hue` to `nick_hue_without_removing_emoji` ## v0.2.0 - 2022-12-10 diff --git a/src/huehash.rs b/src/huehash.rs index 9af7b4c..d44ce88 100644 --- a/src/huehash.rs +++ b/src/huehash.rs @@ -1,5 +1,7 @@ +use crate::emoji::Emoji; + +/// Does not remove emoji. fn normalize(text: &str) -> String { - // TODO Remove emoji names? text.chars() .filter(|&c| c.is_ascii_alphanumeric() || c == '_' || c == '-') .map(|c| c.to_ascii_lowercase()) @@ -22,7 +24,12 @@ fn hue_hash(text: &str, offset: i64) -> u8 { const GREENIE_OFFSET: i64 = 148 - 192; // 148 - hue_hash("greenie", 0) -pub fn nick_hue(nick: &str) -> u8 { +/// Calculate the nick hue without removing colon-delimited emoji as part of +/// normalization. +/// +/// This should be slightly faster than [`nick_hue`] but produces incorrect +/// results if any colon-delimited emoji are present. +pub fn nick_hue_without_removing_emoji(nick: &str) -> u8 { let normalized = normalize(nick); if normalized.is_empty() { hue_hash(nick, GREENIE_OFFSET) @@ -30,3 +37,7 @@ pub fn nick_hue(nick: &str) -> u8 { hue_hash(&normalized, GREENIE_OFFSET) } } + +pub fn nick_hue(emoji: &Emoji, nick: &str) -> u8 { + nick_hue_without_removing_emoji(&emoji.remove(nick)) +} diff --git a/src/lib.rs b/src/lib.rs index bf93af1..457e011 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,4 +15,4 @@ pub mod emoji; mod huehash; mod replies; -pub use huehash::nick_hue; +pub use huehash::{nick_hue, nick_hue_without_removing_emoji};