~emersion/soju

76e332b50a42e2ae5437988d4c9d0d9aa955d352 — Simon Ser 6 months ago 424f676
Move isHighlight to irc.go
2 files changed, 36 insertions(+), 36 deletions(-)

M irc.go
M upstream.go
M irc.go => irc.go +36 -0
@@ 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):]
	}
}

M upstream.go => upstream.go +0 -36
@@ 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 {