Take emoji into account when calculating nick hue

This commit is contained in:
Joscha 2023-01-20 19:12:10 +01:00
parent 94159e8cad
commit c15c05a64e
3 changed files with 16 additions and 3 deletions

View file

@ -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

View file

@ -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))
}

View file

@ -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};