From 76e332b50a42e2ae5437988d4c9d0d9aa955d352 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 13 Apr 2021 18:54:58 +0200 Subject: [PATCH] Move isHighlight to irc.go --- irc.go | 36 ++++++++++++++++++++++++++++++++++++ upstream.go | 36 ------------------------------------ 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/irc.go b/irc.go index bfc9c99..0c7cb2e 100644 --- a/irc.go +++ b/irc.go @@ -4,6 +4,8 @@ import ( "fmt" "sort" "strings" + "unicode" + "unicode/utf8" "gopkg.in/irc.v3" ) @@ -601,3 +603,37 @@ func (cm *deliveredCasemapMap) Value(name string) deliveredClientMap { } return entry.value.(deliveredClientMap) } + +func isWordBoundary(r rune) bool { + switch r { + case '-', '_', '|': + return false + case '\u00A0': + return true + default: + return !unicode.IsLetter(r) && !unicode.IsNumber(r) + } +} + +func isHighlight(text, nick string) bool { + for { + i := strings.Index(text, nick) + if i < 0 { + return false + } + + // Detect word boundaries + var left, right rune + if i > 0 { + left, _ = utf8.DecodeLastRuneInString(text[:i]) + } + if i < len(text) { + right, _ = utf8.DecodeRuneInString(text[i+len(nick):]) + } + if isWordBoundary(left) && isWordBoundary(right) { + return true + } + + text = text[i+len(nick):] + } +} diff --git a/upstream.go b/upstream.go index f083c0b..8eaee2e 100644 --- a/upstream.go +++ b/upstream.go @@ -13,8 +13,6 @@ import ( "strconv" "strings" "time" - "unicode" - "unicode/utf8" "github.com/emersion/go-sasl" "gopkg.in/irc.v3" @@ -315,40 +313,6 @@ func (uc *upstreamConn) parseMembershipPrefix(s string) (ms *memberships, nick s return &memberships, s[i:] } -func isWordBoundary(r rune) bool { - switch r { - case '-', '_', '|': - return false - case '\u00A0': - return true - default: - return !unicode.IsLetter(r) && !unicode.IsNumber(r) - } -} - -func isHighlight(text, nick string) bool { - for { - i := strings.Index(text, nick) - if i < 0 { - return false - } - - // Detect word boundaries - var left, right rune - if i > 0 { - left, _ = utf8.DecodeLastRuneInString(text[:i]) - } - if i < len(text) { - right, _ = utf8.DecodeRuneInString(text[i+len(nick):]) - } - if isWordBoundary(left) && isWordBoundary(right) { - return true - } - - text = text[i+len(nick):] - } -} - func (uc *upstreamConn) handleMessage(msg *irc.Message) error { var label string if l, ok := msg.GetTag("label"); ok {