Move isHighlight to irc.go

This commit is contained in:
Simon Ser 2021-04-13 18:54:58 +02:00
parent 424f676254
commit 76e332b50a
2 changed files with 36 additions and 36 deletions

36
irc.go
View File

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

View File

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