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 {